Skip to content

Commit ff57111

Browse files
authored
Implement bookmark flush API
1 parent d274b5e commit ff57111

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

neo4j/bookmarks.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type BookmarkManager interface {
4343
// GetBookmarks returns all the bookmarks associated with the specified database
4444
// Note: the order of the returned bookmark slice does not need to be deterministic
4545
GetBookmarks(database string) Bookmarks
46+
47+
// Forget removes all databases' bookmarks
48+
// Note: it is the driver user's responsibility to call this
49+
Forget(databases ...string)
4650
}
4751

4852
type BookmarkManagerConfig struct {
@@ -118,6 +122,12 @@ func (b *bookmarkManager) GetBookmarks(database string) Bookmarks {
118122
return bookmarks.Values()
119123
}
120124

125+
func (b *bookmarkManager) Forget(databases ...string) {
126+
for _, db := range databases {
127+
b.bookmarks.Delete(db)
128+
}
129+
}
130+
121131
func NewBookmarkManager(config BookmarkManagerConfig) BookmarkManager {
122132
return &bookmarkManager{
123133
bookmarks: initializeBookmarks(config.InitialBookmarks),

neo4j/bookmarks_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,42 @@ func TestBookmarkManager(outer *testing.T) {
197197
t.Errorf("notify hook should have been called")
198198
}
199199
})
200+
201+
outer.Run("forgets bookmarks of specified databases", func(t *testing.T) {
202+
bookmarkManager := neo4j.NewBookmarkManager(neo4j.BookmarkManagerConfig{
203+
InitialBookmarks: map[string]neo4j.Bookmarks{
204+
"db": {"z", "cooper"},
205+
"foo": {"bar", "fighters"},
206+
"par": {"rot", "don my French"},
207+
},
208+
})
209+
210+
bookmarkManager.Forget("db", "par")
211+
212+
allBookmarks := bookmarkManager.GetAllBookmarks()
213+
AssertEqualsInAnyOrder(t, allBookmarks, []string{"bar", "fighters"})
214+
AssertIntEqual(t, len(bookmarkManager.GetBookmarks("db")), 0)
215+
AssertEqualsInAnyOrder(t, bookmarkManager.GetBookmarks("foo"),
216+
[]string{"bar", "fighters"})
217+
AssertIntEqual(t, len(bookmarkManager.GetBookmarks("par")), 0)
218+
})
219+
220+
outer.Run("can forget untracked databases", func(t *testing.T) {
221+
bookmarkManager := neo4j.NewBookmarkManager(neo4j.BookmarkManagerConfig{
222+
InitialBookmarks: map[string]neo4j.Bookmarks{
223+
"db": {"z", "cooper"},
224+
},
225+
})
226+
227+
bookmarkManager.Forget("wat", "nope")
228+
229+
allBookmarks := bookmarkManager.GetAllBookmarks()
230+
AssertEqualsInAnyOrder(t, allBookmarks, []string{"z", "cooper"})
231+
AssertEqualsInAnyOrder(t, bookmarkManager.GetBookmarks("db"),
232+
[]string{"z", "cooper"})
233+
AssertIntEqual(t, len(bookmarkManager.GetBookmarks("wat")), 0)
234+
AssertIntEqual(t, len(bookmarkManager.GetBookmarks("nope")), 0)
235+
})
200236
}
201237

202238
type simpleBookmarkSupplier struct {

neo4j/session_bookmarks_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ func (f *fakeBookmarkManager) GetAllBookmarks() Bookmarks {
153153
return nil
154154
}
155155

156+
func (f *fakeBookmarkManager) Forget(...string) {
157+
}
158+
156159
func (f *fakeBookmarkManager) called(times int, function string, args ...any) bool {
157160
count := 0
158161
for _, call := range f.recordedCalls {

0 commit comments

Comments
 (0)