Skip to content

Commit a788d7c

Browse files
authored
Expose context and error in BookmarkManager API
This makes the bookmark manager easier to work with, when interfacing with third-party data sources.
1 parent b7ffc78 commit a788d7c

16 files changed

+437
-176
lines changed

neo4j/bookmarks.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package neo4j
2121

2222
import (
23+
"context"
2324
"github.com/neo4j/neo4j-go-driver/v5/neo4j/internal/collection"
2425
"sync"
2526
)
@@ -30,23 +31,25 @@ import (
3031
// from raw values and BookmarksToRawValues for accessing the raw values.
3132
type Bookmarks = []string
3233

34+
// BookmarkManager centralizes bookmark manager supply and notification
35+
// This API is experimental and may be changed or removed without prior notice
3336
type BookmarkManager interface {
3437
// UpdateBookmarks updates the bookmark for the specified database
3538
// previousBookmarks are the initial bookmarks of the bookmark holder (like a Session)
3639
// newBookmarks are the bookmarks that are received after completion of the bookmark holder operation (like the end of a Session)
37-
UpdateBookmarks(database string, previousBookmarks, newBookmarks Bookmarks)
40+
UpdateBookmarks(ctx context.Context, database string, previousBookmarks, newBookmarks Bookmarks) error
3841

3942
// GetAllBookmarks returns all the bookmarks tracked by this bookmark manager
4043
// Note: the order of the returned bookmark slice is not guaranteed
41-
GetAllBookmarks() Bookmarks
44+
GetAllBookmarks(ctx context.Context) (Bookmarks, error)
4245

4346
// GetBookmarks returns all the bookmarks associated with the specified database
4447
// Note: the order of the returned bookmark slice does not need to be deterministic
45-
GetBookmarks(database string) Bookmarks
48+
GetBookmarks(ctx context.Context, database string) (Bookmarks, error)
4649

4750
// Forget removes all databases' bookmarks
4851
// Note: it is the driver user's responsibility to call this
49-
Forget(databases ...string)
52+
Forget(ctx context.Context, databases ...string) error
5053
}
5154

5255
// BookmarkManagerConfig is an experimental API and may be changed or removed
@@ -61,26 +64,26 @@ type BookmarkManagerConfig struct {
6164
// Hook called whenever bookmarks for a given database get updated
6265
// The hook is called with the database and the new bookmarks
6366
// Note: the order of the supplied bookmark slice is not guaranteed
64-
BookmarkUpdateNotifier func(string, Bookmarks)
67+
BookmarkConsumer func(ctx context.Context, database string, bookmarks Bookmarks) error
6568
}
6669

6770
type BookmarkSupplier interface {
6871
// GetAllBookmarks returns all known bookmarks to the bookmark manager
69-
GetAllBookmarks() Bookmarks
72+
GetAllBookmarks(ctx context.Context) (Bookmarks, error)
7073

7174
// GetBookmarks returns all the bookmarks of the specified database to the bookmark manager
72-
GetBookmarks(database string) Bookmarks
75+
GetBookmarks(ctx context.Context, database string) (Bookmarks, error)
7376
}
7477

7578
type bookmarkManager struct {
76-
bookmarks *sync.Map
77-
supplier BookmarkSupplier
78-
notifyUpdatesFn func(string, Bookmarks)
79+
bookmarks *sync.Map
80+
supplier BookmarkSupplier
81+
consumer func(context.Context, string, Bookmarks) error
7982
}
8083

81-
func (b *bookmarkManager) UpdateBookmarks(database string, previousBookmarks, newBookmarks Bookmarks) {
84+
func (b *bookmarkManager) UpdateBookmarks(ctx context.Context, database string, previousBookmarks, newBookmarks Bookmarks) error {
8285
if len(newBookmarks) == 0 {
83-
return
86+
return nil
8487
}
8588
var bookmarksToNotify Bookmarks
8689
storedNewBookmarks := collection.NewSet(newBookmarks)
@@ -92,52 +95,62 @@ func (b *bookmarkManager) UpdateBookmarks(database string, previousBookmarks, ne
9295
currentBookmarks.AddAll(newBookmarks)
9396
bookmarksToNotify = currentBookmarks.Values()
9497
}
95-
if b.notifyUpdatesFn != nil {
96-
b.notifyUpdatesFn(database, bookmarksToNotify)
98+
if b.consumer != nil {
99+
return b.consumer(ctx, database, bookmarksToNotify)
97100
}
101+
return nil
98102
}
99103

100-
func (b *bookmarkManager) GetAllBookmarks() Bookmarks {
104+
func (b *bookmarkManager) GetAllBookmarks(ctx context.Context) (Bookmarks, error) {
101105
allBookmarks := collection.NewSet([]string{})
102106
if b.supplier != nil {
103-
allBookmarks.AddAll(b.supplier.GetAllBookmarks())
107+
bookmarks, err := b.supplier.GetAllBookmarks(ctx)
108+
if err != nil {
109+
return nil, err
110+
}
111+
allBookmarks.AddAll(bookmarks)
104112
}
105113
b.bookmarks.Range(func(db, rawBookmarks any) bool {
106114
bookmarks := rawBookmarks.(collection.Set[string])
107115
allBookmarks.Union(bookmarks)
108116
return true
109117
})
110-
return allBookmarks.Values()
118+
return allBookmarks.Values(), nil
111119
}
112120

113-
func (b *bookmarkManager) GetBookmarks(database string) Bookmarks {
121+
func (b *bookmarkManager) GetBookmarks(ctx context.Context, database string) (Bookmarks, error) {
114122
var extraBookmarks Bookmarks
115123
if b.supplier != nil {
116-
extraBookmarks = b.supplier.GetBookmarks(database)
124+
bookmarks, err := b.supplier.GetBookmarks(ctx, database)
125+
if err != nil {
126+
return nil, err
127+
}
128+
extraBookmarks = bookmarks
117129
}
118130
rawBookmarks, found := b.bookmarks.Load(database)
119131
if !found {
120-
return extraBookmarks
132+
return extraBookmarks, nil
121133
}
122134
bookmarks := rawBookmarks.(collection.Set[string]).Copy()
123135
if extraBookmarks == nil {
124-
return bookmarks.Values()
136+
return bookmarks.Values(), nil
125137
}
126138
bookmarks.AddAll(extraBookmarks)
127-
return bookmarks.Values()
139+
return bookmarks.Values(), nil
128140
}
129141

130-
func (b *bookmarkManager) Forget(databases ...string) {
142+
func (b *bookmarkManager) Forget(ctx context.Context, databases ...string) error {
131143
for _, db := range databases {
132144
b.bookmarks.Delete(db)
133145
}
146+
return nil
134147
}
135148

136149
func NewBookmarkManager(config BookmarkManagerConfig) BookmarkManager {
137150
return &bookmarkManager{
138-
bookmarks: initializeBookmarks(config.InitialBookmarks),
139-
supplier: config.BookmarkSupplier,
140-
notifyUpdatesFn: config.BookmarkUpdateNotifier,
151+
bookmarks: initializeBookmarks(config.InitialBookmarks),
152+
supplier: config.BookmarkSupplier,
153+
consumer: config.BookmarkConsumer,
141154
}
142155
}
143156

0 commit comments

Comments
 (0)