[Glass] Can't we use indexes for sorting too?

Dale Henrichs via Glass glass at lists.gemtalksystems.com
Mon Aug 7 18:15:39 PDT 2017


Mariano,

Here's a doit that illustrates 3 different ways to get at sorted dates 
from an indexed collection. For the query I happen to be using a date 
that predates al of the dates in the collection, but if you had a 
specific range in mind the #readStream or #do: methods allow you to 
access the elements in sorted order ... #sortAscending: produces an 
array with all of the elements sorted ...


       | nsc random maxYear query ar1 ar3 stream ar2 |
       nsc := IdentityBag new.
       random := HostRandom new.
       GsIndexSpec new
         equalityIndex: '' lastElementClass: Date;
         createIndexesOn: nsc.
       1 to: 100 do: [ :index |
         nsc
           add:
             (Date
               newDay: (random integerBetween: 1 and: 365)
               year: (random integerBetween: 2000 and: 2017)) ].
       query := (GsQuery fromString: 'each > x' on: nsc)
         bind: 'x'
         to: (Date newDay: 1 year: 1999).
       "traverse query results using stream ... allows for early return 
without evaluating all query results"
       ar1 := {}.
       stream := query readStream.
       [ stream atEnd ] whileFalse: [ ar1 add: stream next ].
      "traverse query results using do: ... allows for early return 
without evaluating all query results"
       ar2 := {}.
       query do: [ :date | ar2 add: date ].
       "create a sorted Array on nsc using sortAscending:. no early return."
       ar3 := nsc sortAscending: ''.
       {ar1.
       ar2.
       ar3}


On 8/4/17 12:07 PM, Mariano Martinez Peck via Glass wrote:
> Say I have an OrderedCollection with instances of XXX that has a field 
> "date" and I create a a equality index on it. So... ok, I can do the
>
> select: { :entry | entry.date = aDate }
>
> But... I would love to take advantage of the index and also do this:
>
> sorted: {:a :b | a.date <= b.date }
>
> Is there a way to do that?
>
> Thanks in advance
>
>
>
> Mariano
> http://marianopeck.wordpress.com
>
>
> _______________________________________________
> Glass mailing list
> Glass at lists.gemtalksystems.com
> http://lists.gemtalksystems.com/mailman/listinfo/glass

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gemtalksystems.com/mailman/private/glass/attachments/20170807/3c21459f/attachment.html>


More information about the Glass mailing list