<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
ETS 'bag' and 'duplicate_bag' allow key duplicates,<br>
but are not at all optimized for large number of duplicates.<br>
<br>
Both hashing and iteration is only based on keys. All key duplicates
will<br>
thus end up in the same hash bucket as a linked list. All iterations
(next,prev,match,select)<br>
have to return all duplicates in one call as there is no way to
yield/resume in the middle of<br>
a cluster of duplicates. That is why the continuation term contains
the rest of them.<br>
<br>
The ordering strangeness seems more like a plain bug.<br>
<br>
/Sverker, Erlang/OTP Ericsson<br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 09/15/2015 02:33 AM, Robert Virding
wrote:<br>
</div>
<blockquote
cite="mid:CAH_GQaP0tvwv=GunandOJ2si1aSFfVQSM-ogTOqPHyk6Ty7=Kw@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<div dir="ltr">
<div>
<div>
<div>
<div>
<div>I have an ETS table called <b><span
style="font-family:monospace,monospace">sune</span></b>
which is a duplicate_bag containing many elements with
the same key. The <b><span
style="font-family:monospace,monospace">bert</span></b>
elements have the structure <b><span
style="font-family:monospace,monospace">{bert,SequenceNo,Data}</span></b>
so I can see the ordering. If I do <b><span
style="font-family:monospace,monospace">ets:match_object(sune,
{bert,'_','_'})</span></b> I will get back a list
of all the matching elements in the order they were
inserted, which is what I want. However, there can be
quite a few elements with the same key so I would
prefer to get them in chunks instead of all at once.
The solution for this is to start with <b><span
style="font-family:monospace,monospace">ets:match_object(sune,
{bert,'_','_'}, 5)</span></b> which returns a
continuation and then follow with a sequence of <b><span
style="font-family:monospace,monospace">ets:match_object(Continuation)</span></b>
until I get all the elements in chunks of 5. That is
when things start to get strange:<br>
<br>
</div>
- Now the ordering is completely messed up. Each chunk
of 5 contains a sequence but some in ascending order and
some in descending order, for example
440,439,438,437,436.<br>
</div>
- The chunks come in what looks like random order, for
example chunks starting with 571,425,566,430 but starting
with the outermost first working towards the middle.<br>
</div>
- The really weird thing is that the continuation actually
contains all the matching elements which haven't been
returned yet.<br>
<br>
</div>
The last is the really strange. According to the documentation
for match_object/3:<br>
<br>
"Works like <span class="">ets:match_object/2</span> but only
returns a limited (<span class="">Limit</span>) number of
matching objects. The <span class="">Continuation</span> term
can then be used in subsequent calls to <span class="">ets:match_object/1</span>
to get the next chunk of matching objects. This is a space
efficient way to work on objects in a table which is still
faster than traversing the table object by object using <span
class="">ets:first/1</span> and <span class="">ets:next/1</span>."<br>
<br>
</div>
<div>But the continuation contains them all so where is the
space efficiency? I mean why bother to use this at all as it
gives me no benefits.<br>
<br>
</div>
<div>I tried the same thing using <b><span
style="font-family:monospace,monospace">ets:select/3/1</span></b>
and got the same result.<br>
<br>
</div>
<div>My original goal was to work how this would interact with
inserting or deleting elements while I was scanning. My got my
answer.<br>
<br>
</div>
<div>Robert<br>
<br>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
</blockquote>
<br>
</body>
</html>