Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find limited to max number of results #107

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions source/Sagan-Core-Tests/RepositoryBasedTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,31 @@ RepositoryBasedTest >> testFindAllMatching [
equals: 2
]

{ #category : 'tests - querying' }
RepositoryBasedTest >> testFindAllMatchingLimitedToSortedByAscending [

| results |
self extraterrestrials
store: self silvesterStallone;
store: self silvesterMcCoy;
store: self johnTravolta;
store: self johnLock.

results := self extraterrestrials
findAllMatching: [ :extraterrestrial :criteria |
criteria does: extraterrestrial lastName asUppercase includeSubstring: 'L' ]
limitedTo: 2
sortedByAscending: #lastName.

self
assert: results size equals: 2;
assert: ( results anySatisfy: [ :result | result lastName = 'Lock' ] );
deny: ( results anySatisfy: [ :result | result lastName = 'McCoy' ] );
assert: ( results anySatisfy: [ :result | result lastName = 'Stallone' ] );
deny: ( results anySatisfy: [ :result | result lastName = 'Travolta' ] )

]

{ #category : 'tests - querying' }
RepositoryBasedTest >> testFindAllMatchingSortedBy [

Expand Down
28 changes: 19 additions & 9 deletions source/Sagan-Core/InMemoryRepository.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ InMemoryRepository >> conflictCheckingStrategy [
^ conflictCheckingStrategy
]

{ #category : 'private - querying' }
InMemoryRepository >> contentsSortedByAscending: aVariableName [

^ contents sorted: [ :a :b |
( self valueOf: aVariableName in: a ) <= ( self valueOf: aVariableName in: b ) ]
]

{ #category : 'querying' }
InMemoryRepository >> countAll [

Expand All @@ -62,6 +69,13 @@ InMemoryRepository >> findAllMatching: aCriteriaOrBlock [
^ contents select: ( self asMatchingCriteria: aCriteriaOrBlock )
]

{ #category : 'querying' }
InMemoryRepository >> findAllMatching: aCriteriaOrBlock limitedTo: aMaxNumberOfResults sortedByAscending: aVariableName [

^ ( ( self contentsSortedByAscending: aVariableName ) select:
( self asMatchingCriteria: aCriteriaOrBlock ) ) copyFirst: aMaxNumberOfResults
]

{ #category : 'querying' }
InMemoryRepository >> findAllMatching: aCriteriaOrBlock sortedBy: aSortFunction [

Expand Down Expand Up @@ -130,17 +144,13 @@ InMemoryRepository >> updateAfterCheckingConflicts: aDomainObject with: anUpdate
{ #category : 'private - querying' }
InMemoryRepository >> validatedFindAllFrom: aStartingPosition upTo: aMaximumPosition sortedByAscending: aVariableName [

| from to |

( contents isEmpty or: [ aStartingPosition > contents size ] ) ifTrue: [ ^ Set new ].
| from to |
( contents isEmpty or: [ aStartingPosition > contents size ] ) ifTrue: [ ^ Set new ].

from := aStartingPosition max: 1.
to := ( aMaximumPosition min: contents size ) max: 1.
from := aStartingPosition max: 1.
to := ( aMaximumPosition min: contents size ) max: 1.

^ ( contents sorted: [ :a :b |
( self valueOf: aVariableName in: a ) <= ( self valueOf: aVariableName in: b ) ] )
copyFrom: from
to: to
^ ( self contentsSortedByAscending: aVariableName ) copyFrom: from to: to
]

{ #category : 'private - accessing' }
Expand Down
6 changes: 6 additions & 0 deletions source/Sagan-Core/RepositoryBehavior.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ RepositoryBehavior >> findAllMatching: aCriteria [
^ self subclassResponsibility
]

{ #category : 'querying' }
RepositoryBehavior >> findAllMatching: aCriteriaOrBlock limitedTo: aMaxNumberOfResults sortedByAscending: aVariableName [

^ self subclassResponsibility
]

{ #category : 'querying' }
RepositoryBehavior >> findAllMatching: aCriteria sortedBy: aSortCriteria [

Expand Down
21 changes: 21 additions & 0 deletions source/Sagan-GemStone/GemStoneRepository.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ GemStoneRepository >> findAllMatching: aCriteriaOrBlock [
^ contents select: ( self asMatchingCriteria: aCriteriaOrBlock )
]

{ #category : 'querying' }
GemStoneRepository >> findAllMatching: aCriteriaOrBlock limitedTo: aMaxNumberOfResults sortedByAscending: aVariableName [

| results matchingCriteria |
results := OrderedCollection new.
matchingCriteria := self asMatchingCriteria: aCriteriaOrBlock.
self
withQueryFrom: ( 'each.<1s> >= ''''' expandMacrosWith: aVariableName asString )
do: [ :query |
| stream current |
stream := query readStream.
[ stream atEnd ] whileFalse: [
current := stream next.
results size < aMaxNumberOfResults
ifTrue: [ ( matchingCriteria value: current ) ifTrue: [ results add: current ] ]
ifFalse: [ ^ results ]
]
].
^ results
]

{ #category : 'querying' }
GemStoneRepository >> findAllMatching: aCriteriaOrBlock sortedBy: aSortFunction [

Expand Down
14 changes: 14 additions & 0 deletions source/Sagan-RDBMS/RDBMSRepository.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ RDBMSRepository >> findAllMatching: aCriteriaOrBlock [
^ self executeQuery: ( SimpleQuery read: modelObjectClass where: ( self asMatchingCriteria: aCriteriaOrBlock ) )
]

{ #category : 'querying' }
RDBMSRepository >> findAllMatching: aCriteriaOrBlock limitedTo: aMaxNumberOfResults sortedByAscending: aVariableName [

| query |
query := SimpleQuery
read: modelObjectClass
where: ( self asMatchingCriteria: aCriteriaOrBlock )
limit: aMaxNumberOfResults.

aVariableName ascending asSortFunction asOrderByIn: query.

^ self executeQuery: query
]

{ #category : 'querying' }
RDBMSRepository >> findAllMatching: aCriteriaOrBlock sortedBy: aSortFunction [

Expand Down