Can't we use indexes for sorting too?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't we use indexes for sorting too?

GLASS mailing list
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



_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: Can't we use indexes for sorting too?

GLASS mailing list
mmmm maybe the btrees do no help for sorting..

On Fri, Aug 4, 2017 at 4:07 PM, Mariano Martinez Peck <[hidden email]> 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





--

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: Can't we use indexes for sorting too?

GLASS mailing list
In reply to this post by GLASS mailing list

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




_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass


_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: Can't we use indexes for sorting too?

GLASS mailing list


On Mon, Aug 7, 2017 at 10:15 PM, Dale Henrichs via Glass <[hidden email]> wrote:

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}




Thanks Dale, that snippet was of great help as I wasn't aware of any of them. 
This is very cool. Good job on improving all index support!!!



 
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




_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass


_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass




--

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass