Skip to content

Commit 287bb9b

Browse files
committed
Update Bookmark manager for no longer tracking per database
Changes in the BookmarkManagerConfig: * `initialBookmarks` type changed from `Map<string,Iterable<string>>` to `Iterable<string>` * `bookmarksSupplier` type changed from `(db?: string) => Promise<Iterable<string>>` to `() => Promise<Iterable<string>>` * `bookmarksConsumer` type changed from `(db: string, bookmarks: Iterable<string>) => Promise<void>` to `(bookmarks: Iterable<string>) => Promise<void>` Changes in the BookmarkManager: * `getAllBookmarks` and `forget` were removed * `updateBookmarks` signature changed to `updateBookmarks (previousBookmarks: Iterable<string>, newBookmarks: Iterable<string>): Promise<void>` * `getBookmarks` signature changed to `getBookmarks (): Promise<Iterable<string>>` ⚠️ This is a experimental feature.
1 parent 84d6f66 commit 287bb9b

File tree

10 files changed

+126
-456
lines changed

10 files changed

+126
-456
lines changed

packages/core/src/bookmark-manager.ts

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -36,66 +36,40 @@ export default class BookmarkManager {
3636
* Method called when the bookmarks get updated when a transaction finished.
3737
*
3838
* This method will be called when auto-commit queries finish and when explicit transactions
39-
* get commited.
39+
* get committed.
4040
*
41-
* @param {string} database The database which the bookmarks belongs to
4241
* @param {Iterable<string>} previousBookmarks The bookmarks used when starting the transaction
4342
* @param {Iterable<string>} newBookmarks The new bookmarks received at the end of the transaction.
4443
* @returns {void}
4544
*/
46-
async updateBookmarks (database: string, previousBookmarks: Iterable<string>, newBookmarks: Iterable<string>): Promise<void> {
45+
async updateBookmarks (previousBookmarks: Iterable<string>, newBookmarks: Iterable<string>): Promise<void> {
4746
throw new Error('Not implemented')
4847
}
4948

5049
/**
51-
* Method called by the driver to get the bookmarks for one specific database
50+
* Method called by the driver to get the bookmarks.
5251
*
53-
* @param {string} database The database which the bookmarks belong to
5452
* @returns {Iterable<string>} The set of bookmarks
5553
*/
56-
async getBookmarks (database: string): Promise<Iterable<string>> {
57-
throw new Error('Not implemented')
58-
}
59-
60-
/**
61-
* Method called by the driver for getting all the bookmarks.
62-
*
63-
* This method should return all bookmarks for all databases present in the BookmarkManager.
64-
*
65-
* @returns {Iterable<string>} The set of bookmarks
66-
*/
67-
async getAllBookmarks (): Promise<Iterable<string>> {
68-
throw new Error('Not implemented')
69-
}
70-
71-
/**
72-
* Forget the databases and its bookmarks
73-
*
74-
* This method is not called by the driver. Forgetting unused databases is the user's responsibility.
75-
*
76-
* @param {Iterable<string>} databases The databases which the bookmarks will be removed for.
77-
*/
78-
async forget (databases: Iterable<string>): Promise<void> {
54+
async getBookmarks (): Promise<Iterable<string>> {
7955
throw new Error('Not implemented')
8056
}
8157
}
8258

8359
export interface BookmarkManagerConfig {
84-
initialBookmarks?: Map<string, Iterable<string>>
85-
bookmarksSupplier?: (database?: string) => Promise<Iterable<string>>
86-
bookmarksConsumer?: (database: string, bookmarks: Iterable<string>) => Promise<void>
60+
initialBookmarks?: Iterable<string>
61+
bookmarksSupplier?: () => Promise<Iterable<string>>
62+
bookmarksConsumer?: (bookmarks: Iterable<string>) => Promise<void>
8763
}
8864

8965
/**
9066
* @typedef {Object} BookmarkManagerConfig
9167
*
9268
* @since 5.0
9369
* @experimental
94-
* @property {Map<string,Iterable<string>>} [initialBookmarks@experimental] Defines the initial set of bookmarks. The key is the database name and the values are the bookmarks.
95-
* @property {function([database]: string):Promise<Iterable<string>>} [bookmarksSupplier] Called for supplying extra bookmarks to the BookmarkManager
96-
* 1. supplying bookmarks from the given database when the default BookmarkManager's `.getBookmarks(database)` gets called.
97-
* 2. supplying all the bookmarks when the default BookmarkManager's `.getAllBookmarks()` gets called
98-
* @property {function(database: string, bookmarks: Iterable<string>): Promise<void>} [bookmarksConsumer] Called when the set of bookmarks for database get updated
70+
* @property {Iterable<string>} [initialBookmarks@experimental] Defines the initial set of bookmarks. The key is the database name and the values are the bookmarks.
71+
* @property {function():Promise<Iterable<string>>} [bookmarksSupplier] Called for supplying extra bookmarks to the BookmarkManager
72+
* @property {function(bookmarks: Iterable<string>): Promise<void>} [bookmarksConsumer] Called when the set of bookmarks get updated
9973
*/
10074
/**
10175
* Provides an configured {@link BookmarkManager} instance.
@@ -106,9 +80,7 @@ export interface BookmarkManagerConfig {
10680
* @returns {BookmarkManager}
10781
*/
10882
export function bookmarkManager (config: BookmarkManagerConfig = {}): BookmarkManager {
109-
const initialBookmarks = new Map<string, Set<string>>()
110-
111-
config.initialBookmarks?.forEach((v, k) => initialBookmarks.set(k, new Set(v)))
83+
const initialBookmarks = new Set(config.initialBookmarks)
11284

11385
return new Neo4jBookmarkManager(
11486
initialBookmarks,
@@ -119,69 +91,36 @@ export function bookmarkManager (config: BookmarkManagerConfig = {}): BookmarkMa
11991

12092
class Neo4jBookmarkManager implements BookmarkManager {
12193
constructor (
122-
private readonly _bookmarksPerDb: Map<string, Set<string>>,
123-
private readonly _bookmarksSupplier?: (database?: string) => Promise<Iterable<string>>,
124-
private readonly _bookmarksConsumer?: (database: string, bookmark: Iterable<string>) => Promise<void>
94+
private readonly _bookmarks: Set<string>,
95+
private readonly _bookmarksSupplier?: () => Promise<Iterable<string>>,
96+
private readonly _bookmarksConsumer?: (bookmark: Iterable<string>) => Promise<void>
12597
) {
12698

12799
}
128100

129-
async updateBookmarks (database: string, previousBookmarks: Iterable<string>, newBookmarks: Iterable<string>): Promise<void> {
130-
const bookmarks = this._getOrInitializeBookmarks(database)
101+
async updateBookmarks (previousBookmarks: Iterable<string>, newBookmarks: Iterable<string>): Promise<void> {
102+
const bookmarks = this._bookmarks
131103
for (const bm of previousBookmarks) {
132104
bookmarks.delete(bm)
133105
}
134106
for (const bm of newBookmarks) {
135107
bookmarks.add(bm)
136108
}
137109
if (typeof this._bookmarksConsumer === 'function') {
138-
await this._bookmarksConsumer(database, [...bookmarks])
139-
}
140-
}
141-
142-
private _getOrInitializeBookmarks (database: string): Set<string> {
143-
let maybeBookmarks = this._bookmarksPerDb.get(database)
144-
if (maybeBookmarks === undefined) {
145-
maybeBookmarks = new Set()
146-
this._bookmarksPerDb.set(database, maybeBookmarks)
147-
}
148-
return maybeBookmarks
149-
}
150-
151-
async getBookmarks (database: string): Promise<Iterable<string>> {
152-
const bookmarks = new Set(this._bookmarksPerDb.get(database))
153-
154-
if (typeof this._bookmarksSupplier === 'function') {
155-
const suppliedBookmarks = await this._bookmarksSupplier(database) ?? []
156-
for (const bm of suppliedBookmarks) {
157-
bookmarks.add(bm)
158-
}
110+
await this._bookmarksConsumer([...bookmarks])
159111
}
160-
161-
return [...bookmarks]
162112
}
163113

164-
async getAllBookmarks (): Promise<Iterable<string>> {
165-
const bookmarks = new Set<string>()
114+
async getBookmarks (): Promise<Iterable<string>> {
115+
const bookmarks = new Set(this._bookmarks)
166116

167-
for (const [, dbBookmarks] of this._bookmarksPerDb) {
168-
for (const bm of dbBookmarks) {
169-
bookmarks.add(bm)
170-
}
171-
}
172117
if (typeof this._bookmarksSupplier === 'function') {
173118
const suppliedBookmarks = await this._bookmarksSupplier() ?? []
174119
for (const bm of suppliedBookmarks) {
175120
bookmarks.add(bm)
176121
}
177122
}
178123

179-
return bookmarks
180-
}
181-
182-
async forget (databases: Iterable<string>): Promise<void> {
183-
for (const database of databases) {
184-
this._bookmarksPerDb.delete(database)
185-
}
124+
return [...bookmarks]
186125
}
187126
}

packages/core/src/driver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ class SessionConfig {
159159
* Enabling it is done by supplying an BookmarkManager implementation instance to this param.
160160
* A default implementation could be acquired by calling the factory function {@link bookmarkManager}.
161161
*
162-
* **Warning**: Share the same BookmarkManager instance accross all session can have a negative impact
162+
* **Warning**: Share the same BookmarkManager instance across all session can have a negative impact
163163
* on performance since all the queries will wait for the latest changes being propagated across the cluster.
164164
* For keeping consistency between a group of queries, use {@link Session} for grouping them.
165-
* For keeping consistency between a group of sessions, use {@link BookmarkManager} instance for groupping them.
165+
* For keeping consistency between a group of sessions, use {@link BookmarkManager} instance for grouping them.
166166
*
167167
* @example
168168
* const bookmarkManager = neo4j.bookmarkManager()
@@ -179,7 +179,7 @@ class SessionConfig {
179179
*
180180
* // Reading Driver User will wait of the changes being propagated to the server before RUN the query
181181
* // So the 'Driver User' person should exist in the Result, unless deleted.
182-
* const linkedSesssion2 = await linkedSession2.run('CREATE (p:Person {name: $name}) RETURN p', { name: 'Driver User'})
182+
* const linkedResult = await linkedSession2.run('CREATE (p:Person {name: $name}) RETURN p', { name: 'Driver User'})
183183
*
184184
* await linkedSession1.close()
185185
* await linkedSession2.close()

packages/core/src/session.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class Session {
343343
}
344344

345345
private async _bookmarks (): Promise<Bookmarks> {
346-
const bookmarks = await this._bookmarkManager?.getAllBookmarks()
346+
const bookmarks = await this._bookmarkManager?.getBookmarks()
347347
if (bookmarks === undefined) {
348348
return this._lastBookmarks
349349
}
@@ -489,7 +489,7 @@ class Session {
489489
}
490490

491491
private async _getConnectionAcquistionBookmarks (): Promise<Bookmarks> {
492-
const bookmarks = await this._bookmarkManager?.getBookmarks('system')
492+
const bookmarks = await this._bookmarkManager?.getBookmarks()
493493
if (bookmarks === undefined) {
494494
return this._lastBookmarks
495495
}
@@ -505,7 +505,6 @@ class Session {
505505
_updateBookmarks (newBookmarks?: Bookmarks, previousBookmarks?: Bookmarks, database?: string): void {
506506
if ((newBookmarks != null) && !newBookmarks.isEmpty()) {
507507
this._bookmarkManager?.updateBookmarks(
508-
database ?? this._database,
509508
previousBookmarks?.values() ?? [],
510509
newBookmarks?.values() ?? []
511510
)

0 commit comments

Comments
 (0)