20
20
package neo4j
21
21
22
22
import (
23
+ "context"
23
24
"github.com/neo4j/neo4j-go-driver/v5/neo4j/internal/collection"
24
25
"sync"
25
26
)
@@ -30,23 +31,25 @@ import (
30
31
// from raw values and BookmarksToRawValues for accessing the raw values.
31
32
type Bookmarks = []string
32
33
34
+ // BookmarkManager centralizes bookmark manager supply and notification
35
+ // This API is experimental and may be changed or removed without prior notice
33
36
type BookmarkManager interface {
34
37
// UpdateBookmarks updates the bookmark for the specified database
35
38
// previousBookmarks are the initial bookmarks of the bookmark holder (like a Session)
36
39
// 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
38
41
39
42
// GetAllBookmarks returns all the bookmarks tracked by this bookmark manager
40
43
// Note: the order of the returned bookmark slice is not guaranteed
41
- GetAllBookmarks () Bookmarks
44
+ GetAllBookmarks (ctx context. Context ) ( Bookmarks , error )
42
45
43
46
// GetBookmarks returns all the bookmarks associated with the specified database
44
47
// 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 )
46
49
47
50
// Forget removes all databases' bookmarks
48
51
// Note: it is the driver user's responsibility to call this
49
- Forget (databases ... string )
52
+ Forget (ctx context. Context , databases ... string ) error
50
53
}
51
54
52
55
// BookmarkManagerConfig is an experimental API and may be changed or removed
@@ -61,26 +64,26 @@ type BookmarkManagerConfig struct {
61
64
// Hook called whenever bookmarks for a given database get updated
62
65
// The hook is called with the database and the new bookmarks
63
66
// 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
65
68
}
66
69
67
70
type BookmarkSupplier interface {
68
71
// GetAllBookmarks returns all known bookmarks to the bookmark manager
69
- GetAllBookmarks () Bookmarks
72
+ GetAllBookmarks (ctx context. Context ) ( Bookmarks , error )
70
73
71
74
// 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 )
73
76
}
74
77
75
78
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
79
82
}
80
83
81
- func (b * bookmarkManager ) UpdateBookmarks (database string , previousBookmarks , newBookmarks Bookmarks ) {
84
+ func (b * bookmarkManager ) UpdateBookmarks (ctx context. Context , database string , previousBookmarks , newBookmarks Bookmarks ) error {
82
85
if len (newBookmarks ) == 0 {
83
- return
86
+ return nil
84
87
}
85
88
var bookmarksToNotify Bookmarks
86
89
storedNewBookmarks := collection .NewSet (newBookmarks )
@@ -92,52 +95,62 @@ func (b *bookmarkManager) UpdateBookmarks(database string, previousBookmarks, ne
92
95
currentBookmarks .AddAll (newBookmarks )
93
96
bookmarksToNotify = currentBookmarks .Values ()
94
97
}
95
- if b .notifyUpdatesFn != nil {
96
- b . notifyUpdatesFn ( database , bookmarksToNotify )
98
+ if b .consumer != nil {
99
+ return b . consumer ( ctx , database , bookmarksToNotify )
97
100
}
101
+ return nil
98
102
}
99
103
100
- func (b * bookmarkManager ) GetAllBookmarks () Bookmarks {
104
+ func (b * bookmarkManager ) GetAllBookmarks (ctx context. Context ) ( Bookmarks , error ) {
101
105
allBookmarks := collection .NewSet ([]string {})
102
106
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 )
104
112
}
105
113
b .bookmarks .Range (func (db , rawBookmarks any ) bool {
106
114
bookmarks := rawBookmarks .(collection.Set [string ])
107
115
allBookmarks .Union (bookmarks )
108
116
return true
109
117
})
110
- return allBookmarks .Values ()
118
+ return allBookmarks .Values (), nil
111
119
}
112
120
113
- func (b * bookmarkManager ) GetBookmarks (database string ) Bookmarks {
121
+ func (b * bookmarkManager ) GetBookmarks (ctx context. Context , database string ) ( Bookmarks , error ) {
114
122
var extraBookmarks Bookmarks
115
123
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
117
129
}
118
130
rawBookmarks , found := b .bookmarks .Load (database )
119
131
if ! found {
120
- return extraBookmarks
132
+ return extraBookmarks , nil
121
133
}
122
134
bookmarks := rawBookmarks .(collection.Set [string ]).Copy ()
123
135
if extraBookmarks == nil {
124
- return bookmarks .Values ()
136
+ return bookmarks .Values (), nil
125
137
}
126
138
bookmarks .AddAll (extraBookmarks )
127
- return bookmarks .Values ()
139
+ return bookmarks .Values (), nil
128
140
}
129
141
130
- func (b * bookmarkManager ) Forget (databases ... string ) {
142
+ func (b * bookmarkManager ) Forget (ctx context. Context , databases ... string ) error {
131
143
for _ , db := range databases {
132
144
b .bookmarks .Delete (db )
133
145
}
146
+ return nil
134
147
}
135
148
136
149
func NewBookmarkManager (config BookmarkManagerConfig ) BookmarkManager {
137
150
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 ,
141
154
}
142
155
}
143
156
0 commit comments