Skip to content

Commit

Permalink
Merge pull request #58 from tinchodias/29-Fix-mongo3CollectionNames-t…
Browse files Browse the repository at this point in the history
…o-list-beyond-firstBatch

Add command find and getmore
  • Loading branch information
estebanlm authored Aug 4, 2019
2 parents 64cec22 + 8486067 commit 85778a5
Show file tree
Hide file tree
Showing 61 changed files with 341 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"commentStamp" : "TorstenBergmann 3/4/2015 09:38",
"super" : "MongoOperation",
"category" : "Mongo-Core-Collections",
"category" : "Mongo-Core-Operations",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
Expand Down
3 changes: 3 additions & 0 deletions mc/Mongo-Core.package/Mongo.class/instance/databaseNames.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
operations
databaseNames
^ self listDatabases collect: [:each | each at: 'name' ]
7 changes: 1 addition & 6 deletions mc/Mongo-Core.package/Mongo.class/instance/databases.st
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
operations
databases
| raw command |
command := (OrderedIdentityDictionary new)
at: #listDatabases put: 1;
yourself.
raw := (self admin command: command) at: 'databases'.
^raw collect: [:each | self databaseNamed: (each at: 'name')]
^ self databaseNames collect: [ :each | self databaseNamed: each ]
7 changes: 7 additions & 0 deletions mc/Mongo-Core.package/Mongo.class/instance/listDatabases.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
operations
listDatabases
| command |
command := (OrderedIdentityDictionary new)
at: #listDatabases put: 1;
yourself.
^ (self admin command: command) at: 'databases'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
operations
commandFind: filterDictionary readConcern: aConcern
"Find (select) using Mongo command. Answer the output of the command.
See more on https://docs.mongodb.com/manual/reference/command/find/"

^ database
commandFind: filterDictionary
collection: name
readConcern: aConcern
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
operations
commandGetMore: cursorId batchSize: batchSizeOrNil timeout: aDurationOrNil
"Get a batch of documents from this collection using a cursor id obtained via commandFind* (for example).
Translates to:
{
'getMore': <long>,
'collection': <string>,
'batchSize': <int>,
'maxTimeMS': <int>
}
See: https://docs.mongodb.com/manual/reference/command/getMore/#dbcmd.getMore"

| dictionary |
dictionary := OrderedIdentityDictionary new
at: #getMore put: cursorId;
at: #collection put: name;
yourself.

batchSizeOrNil ifNotNil: [
dictionary at: #batchSize put: batchSizeOrNil ].

aDurationOrNil ifNotNil: [
dictionary at: #maxTimeMS put: aDurationOrNil asMilliSeconds ].

^ MongoQueryBatch newFromGetMoreResponse: (database command: dictionary)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
convenience
ensureAdded
[ database addCollection: name ]
on: MongoCollectionAlreadyExists
do: [ :error | ]
6 changes: 6 additions & 0 deletions mc/Mongo-Core.package/MongoCommandCursor.class/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Mongodb's provides find-like commands respond the first batch of a cursor. I help to enumerate documents in a database command reply with methods such as #contents, #do: and #collect:.

See:
https://docs.mongodb.com/manual/reference/command/find/
https://docs.mongodb.com/manual/reference/command/listCollections/
https://docs.mongodb.com/manual/reference/command/getMore/#dbcmd.getMore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instance creation
database: aMongoDatabase dictionary: aCollection
^ self basicNew
initializeDatabase: aMongoDatabase
command: aCollection;
yourself.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
batchSizeOrNil: anObject
batchSizeOrNil := anObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
batchSizeOrNil
^ batchSizeOrNil
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enumerating
collect: aBlockClosure
^ Array streamContents: [ :stream |
self do: [ :each | stream nextPut: (aBlockClosure value: each) ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
command
^ command
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enumerating
contents
^ self collect: #yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
database
^ database
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enumerating
do: aBlockClosure
batch := MongoQueryBatch newFromFindResponse: (database command: command).

[ batch documents do: aBlockClosure.
batch atEnd ifTrue: [ ^self ].
batch := (database collectionAt: batch collectionName) commandGetMore: batch cursorId batchSize: self batchSizeOrNil timeout: self timeoutOrNil
] repeat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
initialization
initializeDatabase: aMongoDatabase command: aCollection
self initialize.

database := aMongoDatabase.
command := aCollection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
timeoutOrNil: aDuration
timeoutOrNil := aDuration
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
timeoutOrNil
^ timeoutOrNil
17 changes: 17 additions & 0 deletions mc/Mongo-Core.package/MongoCommandCursor.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"commentStamp" : "MartinDias 7/30/2019 12:47",
"super" : "Object",
"category" : "Mongo-Core-Utilities",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"database",
"batch",
"command",
"batchSizeOrNil",
"timeoutOrNil"
],
"name" : "MongoCommandCursor",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
operations
addCollection: aString
^self addCollection: aString capped: false size: nil max: nil.
^ self
addCollection: aString
capped: false
size: nil
max: nil
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ addCollection: aString capped: aCapped size: aSize max: aMax
error isCollectionAlreadyExists
ifTrue: [ (MongoCollectionAlreadyExists new collectionName: aString) signal ]
ifFalse: [ error signal ] ].
^MongoCollection database: self name: aString
^ self collectionAt: aString
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
accessing
collectionAt: aString
"Answers the collection in this database that has aString as name (or nil)"
"Answers a MongoCollection for this database that has aString as name (may not exist)."

^ self collections
detect: [ :e | e name = aString ]
ifNone: [ nil ]
^ MongoCollection database: self name: aString
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
operations
collectionNames
"Answer the names of the collections in this database (for MongoDB >= 3.0).
"Answer the names of the collections in this database.
See more at https://docs.mongodb.com/manual/reference/command/listCollections/"

| reply |
reply := self command: {(#listCollections -> 1)} asDictionary.
^ ((reply at: 'cursor') at: 'firstBatch') collect: [ :each | each at: 'name' ]
| dictionary |
dictionary := OrderedIdentityDictionary new
at: #listCollections put: 1;
at: #nameOnly put: true;
yourself.

^ (MongoCommandCursor
database: self
dictionary: dictionary)
collect: [ :each | each at: 'name' ].
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ operations
collections
"Answer the collections in this database"

^ self collectionNames
collect: [ :each | MongoCollection database: self name: each ]
^ self collectionNames collect: [ :each | self collectionAt: each ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
operations
commandFind: filterDictionary collection: collectionName readConcern: aConcern

| dict |
dict := {
'find' -> collectionName.
'filter' -> filterDictionary.
} as: OrderedDictionary.

aConcern ifNotNil: [
dict at: 'readConcern' put: aConcern concernForCommand ].

^ MongoCommandCursor database: self dictionary: dict
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
operations
getCollection: aString
^ [ self addCollection: aString capped: false size: nil max: nil ]
on: MongoCollectionAlreadyExists
do: [ :err |
MongoCollection database: self name: aString ]
^ (self collectionAt: aString)
ensureAdded;
yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
operations
listCollections
"Answer the raw response of listCollections command.
See more at https://docs.mongodb.com/manual/reference/command/listCollections/"

^ self command: { #listCollections -> 1 } asDictionary
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
instance creation
newFromFindResponse: aDictionary

| cursorDictionary |
cursorDictionary := aDictionary at: 'cursor'.
^ self basicNew
initializeWithCollectionName: (cursorDictionary at: 'ns')
documents: (cursorDictionary at: 'firstBatch')
cursorId: (cursorDictionary at: 'id');
yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
instance creation
newFromGetMoreResponse: aDictionary

| cursorDictionary |
cursorDictionary := aDictionary at: 'cursor'.
^ self basicNew
initializeWithCollectionName: (cursorDictionary at: 'ns')
documents: (cursorDictionary at: 'nextBatch')
cursorId: (cursorDictionary at: 'id');
yourself
3 changes: 3 additions & 0 deletions mc/Mongo-Core.package/MongoQueryBatch.class/instance/atEnd.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
atEnd
^ self cursorId = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
collectionName
^ collectionName
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
cursorId
^ cursorId
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
documents
^ documents
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
initialization
initializeWithCollectionName: aString documents: aCollection cursorId: anInteger
self initialize.

self flag: #todo. "This code removes the db name, and it's a bit strange to have it here."
collectionName := aString readStream upTo: $.; upToEnd.
documents := aCollection.
cursorId := anInteger.
15 changes: 15 additions & 0 deletions mc/Mongo-Core.package/MongoQueryBatch.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"commentStamp" : "",
"super" : "Object",
"category" : "Mongo-Core-Responses",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"documents",
"collectionName",
"cursorId"
],
"name" : "MongoQueryBatch",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"commentStamp" : "MartinDias 5/12/2016 01:30",
"super" : "Object",
"category" : "Mongo-Core-Replication",
"category" : "Mongo-Core-Responses",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"commentStamp" : "MartinDias 5/12/2016 01:31",
"super" : "Object",
"category" : "Mongo-Core-Replication",
"category" : "Mongo-Core-Responses",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"commentStamp" : "MartinDias 5/12/2016 01:31",
"super" : "Object",
"category" : "Mongo-Core-Replication",
"category" : "Mongo-Core-Responses",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"commentStamp" : "MartinDias 5/12/2016 01:32",
"super" : "Object",
"category" : "Mongo-Core-Replication",
"category" : "Mongo-Core-Responses",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
Expand Down
1 change: 1 addition & 0 deletions mc/Mongo-Core.package/monticello.meta/categories.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SystemOrganization addCategory: #'Mongo-Core'!
SystemOrganization addCategory: #'Mongo-Core-Auth'!
SystemOrganization addCategory: #'Mongo-Core-Base'!
SystemOrganization addCategory: #'Mongo-Core-Collections'!
SystemOrganization addCategory: #'Mongo-Core-Commands'!
SystemOrganization addCategory: #'Mongo-Core-Errors'!
SystemOrganization addCategory: #'Mongo-Core-Operations'!
SystemOrganization addCategory: #'Mongo-Core-Replication'!
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests
assertExpectedDocuments: aCollection
self assert: aCollection size equals: self expectedDocumentCount.

aCollection doWithIndex: [ :each :index |
self baseDocumentForTesting keysAndValuesDo: [ :key :value |
self assert: (each at: key) equals: value ].
self assert: (each at: 'index') equals: index ].
Loading

0 comments on commit 85778a5

Please sign in to comment.