-
Notifications
You must be signed in to change notification settings - Fork 917
Add a cursor abstraction #1
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
Conversation
…e collection does not exist
core/command_types.go
Outdated
@@ -0,0 +1,19 @@ | |||
package core |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a results.go file these can go into. I'd probably also create a CursorResult interface that has 3 methods, NS(), ID(), and FirstBatch()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core/command_types.go
Outdated
// The first batch of the cursor | ||
FirstBatch []bson.Raw `bson:"firstBatch"` | ||
// The namespace to use for iterating the cursor | ||
NS string `bson:"ns"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create that Namespace struct type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core/cursor.go
Outdated
) | ||
|
||
// Create a new cursor | ||
func NewCursor(databaseName string, collectionName string, firstBatch []bson.Raw, cursorId int64, batchSize int32, connection Connection) Cursor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you have a CursorResult interface, this should just be able to take that as an argument for the first 4 parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core/cursor.go
Outdated
c.currentBatch = response.Cursor.NextBatch | ||
} | ||
|
||
type getMoreCommand struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't necessarily see these types getting used anywhere outside this particular file. It'd be nice to define these types right where they will be used inside the functions. In some cases, they could probably even be anonymous structs.
For example:
getMoreCommand := struct {
CursorId int64 `bson:"getMore"`
Collection string `bson:"collection"`
BatchSize int32 `bson:"batchSize"`
} {
CursorId: c.cursorId,
Collection: c.collectionName,
}
Same with the responses...
I could probably do this in other places too...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core/cursor_test.go
Outdated
"testing" | ||
) | ||
|
||
const collectionName = "CursorTest" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This collectionName variable is now available to all the things using the core_test package. Meaning, that this name is probably not what we want to be used in other packages that aren't testing cursors...
If we didn't use the same collection name for each test, we could add t.Parallel() to each of these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, though didn't make the tests parallel yet
core/cursor.go
Outdated
currentBatch []bson.Raw | ||
cursorId int64 | ||
err error | ||
connection Connection // TODO: missing abstraction. Shouldn't require a connection here, but just a way to acquire and release one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have this take a Server instead...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deferred, as discussed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. Little nits and fix the example, but push when you are ready.
examples/cursor/main.go
Outdated
log.Fatalf("Failed to execute command: %v", err) | ||
} | ||
|
||
cursor := core.NewCursor(databaseName, collectionName, response.Cursor.FirstBatch, response.Cursor.ID, 0, connection) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like this isn't gonna work anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -0,0 +1,199 @@ | |||
package core_test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since you are using different collections, might as well mark each with t.Parallel().
Also, might be nice to find a way to get the current method name from a stack trace, then use that for the collection name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marked as parallel. Leaving the current method name detection for another time.
core/cursor_test.go
Outdated
"testing" | ||
) | ||
|
||
func TestEmpty(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe let's prefix each of these with what it's testing... TestCursor_Empty, TestCursor_Close, TestCursor_SingleBatch... etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, though without the underscore separator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. 2 debug comments left in.
core/cursor.go
Outdated
@@ -44,9 +45,11 @@ type cursorImpl struct { | |||
func (c *cursorImpl) Next(result interface{}) bool { | |||
found := c.getNextFromCurrentBatch(result) | |||
if found { | |||
fmt.Println("found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core/cursor.go
Outdated
return true | ||
} | ||
if c.err != nil { | ||
fmt.Printf("error: %v\n", c.err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Rebasing and committing |
STITCH-4296: Export contextWithSession
GODRIVER-672 Change session IDs to be stored as bson.Raw (mongodb#339)
Added a cursor abstraction to core. Some notes: