Skip to content

Commit 777fc45

Browse files
committed
Replace Session.LastBookmark() with Session.LastBookmarks()
* Returns `[]string` instead of string. * Returns all (not just the last) initial bookmarks if no bookmark has been received from the server.
1 parent 4c74525 commit 777fc45

File tree

5 files changed

+34
-38
lines changed

5 files changed

+34
-38
lines changed

neo4j/session.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type TransactionWork func(tx Transaction) (interface{}, error)
3535
// Session represents a logical connection (which is not tied to a physical connection)
3636
// to the server
3737
type Session interface {
38-
// LastBookmark returns the bookmark received following the last successfully completed transaction.
39-
// If no bookmark was received or if this transaction was rolled back, the bookmark value will not be changed.
40-
LastBookmark() string
38+
// LastBookmarks returns the bookmark received following the last successfully completed transaction.
39+
// If no bookmark was received or if this transaction was rolled back, the initial set of bookmarks will be
40+
// returned.
41+
LastBookmarks() []string
4142
// BeginTransaction starts a new explicit transaction on this session
4243
BeginTransaction(configurers ...func(*TransactionConfig)) (Transaction, error)
4344
// ReadTransaction executes the given unit of work in a AccessModeRead transaction with
@@ -169,18 +170,14 @@ func newSession(config *Config, sessConfig SessionConfig, router sessionRouter,
169170
}
170171
}
171172

172-
func (s *session) LastBookmark() string {
173+
func (s *session) LastBookmarks() []string {
173174
// Pick up bookmark from pending auto-commit if there is a bookmark on it
174175
if s.txAuto != nil {
175176
s.retrieveBookmarks(s.txAuto.conn)
176177
}
177178

178-
// Report bookmark from previously closed connection or from initial set
179-
if len(s.bookmarks) > 0 {
180-
return s.bookmarks[len(s.bookmarks)-1]
181-
}
182-
183-
return ""
179+
// Report bookmarks from previously closed connection or from initial set
180+
return s.bookmarks
184181
}
185182

186183
func (s *session) BeginTransaction(configurers ...func(*TransactionConfig)) (Transaction, error) {
@@ -501,8 +498,8 @@ type sessionWithError struct {
501498
err error
502499
}
503500

504-
func (s *sessionWithError) LastBookmark() string {
505-
return ""
501+
func (s *sessionWithError) LastBookmarks() []string {
502+
return []string{}
506503
}
507504

508505
func (s *sessionWithError) BeginTransaction(configurers ...func(*TransactionConfig)) (Transaction, error) {

neo4j/session_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ func TestSession(st *testing.T) {
165165
})
166166

167167
st.Run("Bookmarking", func(bt *testing.T) {
168-
bt.Run("Initial bookmark is used for LastBookmark", func(t *testing.T) {
168+
bt.Run("Initial bookmarks are returned from LastBookmarks", func(t *testing.T) {
169169
_, _, sess := createSessionWithBookmarks([]string{"b1", "b2"})
170-
AssertStringEqual(t, sess.LastBookmark(), "b2")
170+
AssertDeepEquals(t, sess.LastBookmarks(), []string{"b1", "b2"})
171171
})
172172

173173
bt.Run("Initial bookmarks are used and cleaned up before usage", func(t *testing.T) {
@@ -194,9 +194,9 @@ func TestSession(st *testing.T) {
194194
}
195195
})
196196

197-
bt.Run("LastBookmark is empty when no initial bookmark", func(t *testing.T) {
197+
bt.Run("LastBookmarks is empty when no initial bookmark", func(t *testing.T) {
198198
_, _, sess := createSession()
199-
AssertStringEqual(t, sess.LastBookmark(), "")
199+
AssertLen(t, sess.LastBookmarks(), 0)
200200
})
201201
})
202202

@@ -256,17 +256,17 @@ func TestSession(st *testing.T) {
256256

257257
sess.Run("cypher", nil)
258258
AssertIntEqual(t, bufferCalls, 0)
259-
AssertStringEqual(t, sess.LastBookmark(), "")
259+
AssertLen(t, sess.LastBookmarks(), 0)
260260
// Should call Buffer on connection to ensure that first Run is buffered and
261261
// it's bookmark retrieved
262262
sess.Run("cypher", nil)
263-
AssertStringEqual(t, sess.LastBookmark(), "1")
263+
AssertDeepEquals(t, sess.LastBookmarks(), []string{"1"})
264264
result, _ := sess.Run("cypher", nil)
265-
AssertStringEqual(t, sess.LastBookmark(), "2")
265+
AssertDeepEquals(t, sess.LastBookmarks(), []string{"2"})
266266
// And finally consuming the last result should give a new bookmark
267267
AssertIntEqual(t, consumeCalls, 0)
268268
result.Consume()
269-
AssertStringEqual(t, sess.LastBookmark(), "1")
269+
AssertDeepEquals(t, sess.LastBookmarks(), []string{"1"})
270270
})
271271

272272
bt.Run("Pending and invoke tx function", func(t *testing.T) {
@@ -291,7 +291,7 @@ func TestSession(st *testing.T) {
291291
if !reflect.DeepEqual([]string{"1"}, rtx.Bookmarks) {
292292
t.Errorf("Using unclean or no bookmarks: %+v", rtx)
293293
}
294-
AssertStringEqual(t, sess.LastBookmark(), "1")
294+
AssertDeepEquals(t, sess.LastBookmarks(), []string{"1"})
295295
AssertIntEqual(t, bufferCalls, 1)
296296
})
297297

@@ -315,7 +315,7 @@ func TestSession(st *testing.T) {
315315
if !reflect.DeepEqual([]string{"1"}, rtx.Bookmarks) {
316316
t.Errorf("Using unclean or no bookmarks: %+v", rtx)
317317
}
318-
AssertStringEqual(t, sess.LastBookmark(), "1")
318+
AssertDeepEquals(t, sess.LastBookmarks(), []string{"1"})
319319
AssertIntEqual(t, bufferCalls, 1)
320320
})
321321

@@ -477,7 +477,7 @@ func TestSession(st *testing.T) {
477477
// Begin and commit a transaction on the session
478478
tx, _ := sess.BeginTransaction()
479479
tx.Commit()
480-
AssertStringEqual(t, sess.LastBookmark(), bookmark)
480+
AssertDeepEquals(t, sess.LastBookmarks(), []string{bookmark})
481481
// The bookmark should be used in next transaction
482482
tx, _ = sess.BeginTransaction()
483483
AssertLen(t, conn.RecordedTxs, 2)

test-stress/executors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func newStressSession(driver neo4j.Driver, useBookmark bool, accessMode neo4j.Ac
6666
var err error
6767

6868
if useBookmark {
69-
session, err = driver.Session(accessMode, ctx.getBookmark())
69+
session, err = driver.Session(accessMode, ctx.getBookmarks()...)
7070
} else {
7171
session, err = driver.Session(accessMode)
7272
}
@@ -181,7 +181,7 @@ func WriteQueryExecutor(driver neo4j.Driver, useBookmark bool) func(ctx *TestCon
181181
ExpectNoError(err)
182182
ExpectInt(summary.Counters().NodesCreated(), 1)
183183

184-
ctx.setBookmark(session.LastBookmark())
184+
ctx.setBookmarks(session.LastBookmarks())
185185

186186
ctx.addCreated()
187187
}
@@ -207,7 +207,7 @@ func WriteQueryInTxExecutor(driver neo4j.Driver, useBookmark bool) func(ctx *Tes
207207
err = tx.Commit()
208208
ExpectNoError(err)
209209

210-
ctx.setBookmark(session.LastBookmark())
210+
ctx.setBookmarks(session.LastBookmarks())
211211

212212
ctx.addCreated()
213213
}
@@ -229,7 +229,7 @@ func WriteQueryWithWriteTransactionExecutor(driver neo4j.Driver, useBookmark boo
229229
ExpectNoError(err)
230230
ExpectNotNil(summary)
231231
ExpectInt(summary.(neo4j.ResultSummary).Counters().NodesCreated(), 1)
232-
ctx.setBookmark(session.LastBookmark())
232+
ctx.setBookmarks(session.LastBookmarks())
233233
ctx.addCreated()
234234
}
235235
}

test-stress/testcontext.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
type TestContext struct {
3232
driver neo4j.Driver
3333
stop int32
34-
bookmark atomic.Value
34+
bookmarks atomic.Value
3535

3636
readNodeCountsByServer sync.Map
3737

@@ -45,13 +45,13 @@ func NewTestContext(driver neo4j.Driver) *TestContext {
4545
result := &TestContext{
4646
driver: driver,
4747
stop: 0,
48-
bookmark: atomic.Value{},
48+
bookmarks: atomic.Value{},
4949
readNodeCountsByServer: sync.Map{},
5050
readNodeCount: 0,
5151
createdNodeCount: 0,
5252
}
5353

54-
result.bookmark.Store("")
54+
result.bookmarks.Store([]string{""})
5555

5656
return result
5757
}
@@ -74,12 +74,12 @@ func (ctx *TestContext) addRead() {
7474
atomic.AddInt32(&ctx.readNodeCount, 1)
7575
}
7676

77-
func (ctx *TestContext) getBookmark() string {
78-
return ctx.bookmark.Load().(string)
77+
func (ctx *TestContext) getBookmarks() []string {
78+
return ctx.bookmarks.Load().([]string)
7979
}
8080

81-
func (ctx *TestContext) setBookmark(bookmark string) {
82-
ctx.bookmark.Store(bookmark)
81+
func (ctx *TestContext) setBookmarks(bookmarks []string) {
82+
ctx.bookmarks.Store(bookmarks)
8383
}
8484

8585
func (ctx *TestContext) processSummary(summary neo4j.ResultSummary) {

testkit-backend/backend.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,9 @@ func (b *backend) handleRequest(req map[string]interface{}) {
493493

494494
case "SessionLastBookmarks":
495495
sessionState := b.sessionStates[data["sessionId"].(string)]
496-
bookmark := sessionState.session.LastBookmark()
497-
bookmarks := []string{}
498-
if bookmark != "" {
499-
bookmarks = append(bookmarks, bookmark)
496+
bookmarks := sessionState.session.LastBookmarks()
497+
if (bookmarks == nil) {
498+
bookmarks = []string{}
500499
}
501500
b.writeResponse("Bookmarks", map[string]interface{}{"bookmarks": bookmarks})
502501

0 commit comments

Comments
 (0)