generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 100
Event log #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Event log #290
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6ae0eaa
implement `EventLog`
mistermoe 085e0c9
make iterator options optional. this matches levels api for
mistermoe f57a161
address PR comments
mistermoe 9ad3c30
address PR comments
mistermoe d76748e
prevent computing message cid twice
mistermoe 66f5623
adjust test based on PR comment
mistermoe 1f9f08a
remove tenant encoding
mistermoe d0c1e26
improve test based on PR comment
mistermoe a267bac
add `dump` to `eventLogLevel`
mistermoe 902d7ba
add `deleteEventsByCid`. refactor `getEventsAfter` method signature
mistermoe 513ab06
add missing types to method signature and include method in interface
mistermoe 74a91ab
delete events whenever intermediary `RecordsWrite`s are deleted
mistermoe eb7f697
fix comment placement
mistermoe 758a47f
Merge branch 'main' into event-log
mistermoe 946b57a
fix merge related issues
mistermoe 8d18971
fix import order
mistermoe f44f90d
add functional tests
mistermoe 4527355
remove duplicate code
mistermoe c17f6d5
add jsdoc
mistermoe e1643a0
delete older ProtocolsConfigure event when one is overwritten
mistermoe 6674e2a
Merge branch 'main' into event-log
mistermoe 6625b71
strict mode :sob:
mistermoe 7b5be72
store events in watermark order and cid order
mistermoe a17156b
Merge remote-tracking branch 'origin' into event-log
mistermoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import type { ULID } from 'ulid'; | ||
| import type { Event, EventLog, GetEventsOptions } from './event-log.js'; | ||
|
|
||
| import { monotonicFactory } from 'ulid'; | ||
| import { createLevelDatabase, LevelWrapper } from '../store/level-wrapper.js'; | ||
|
|
||
|
|
||
| type EventLogLevelConfig = { | ||
| /** | ||
| * must be a directory path (relative or absolute) where | ||
| * LevelDB will store its files, or in browsers, the name of the | ||
| * {@link https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase IDBDatabase} to be opened. | ||
| */ | ||
| location: string, | ||
| createLevelDatabase?: typeof createLevelDatabase, | ||
| }; | ||
|
|
||
| export class EventLogLevel implements EventLog { | ||
| config: EventLogLevelConfig; | ||
| db: LevelWrapper<string>; | ||
| ulid: ULID; | ||
|
|
||
| constructor(config?: EventLogLevelConfig) { | ||
| this.config = { | ||
| location: 'EVENTLOG', | ||
| createLevelDatabase, | ||
| ...config, | ||
| }; | ||
|
|
||
| this.db = new LevelWrapper<string>({ ...this.config, valueEncoding: 'utf8' }); | ||
| this.ulid = monotonicFactory(); | ||
thehenrytsai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| async open(): Promise<void> { | ||
| return this.db.open(); | ||
| } | ||
|
|
||
| async close(): Promise<void> { | ||
| return this.db.close(); | ||
| } | ||
|
|
||
| async clear(): Promise<void> { | ||
| return this.db.clear(); | ||
| } | ||
|
|
||
| async append(tenant: string, messageCid: string): Promise<string> { | ||
| const tenantEventLog = await this.db.partition(tenant); | ||
|
|
||
| const watermark = this.ulid(); | ||
| await tenantEventLog.put(watermark, messageCid); | ||
|
|
||
| return watermark; | ||
diehuxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| async getEvents(tenant: string, options?: GetEventsOptions): Promise<Event[]> { | ||
| const tenantEventLog = await this.db.partition(tenant); | ||
| const events: Array<Event> = []; | ||
|
|
||
| for await (const [key, value] of tenantEventLog.iterator(options)) { | ||
| const event = { watermark: key, messageCid: value }; | ||
mistermoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| events.push(event); | ||
| } | ||
|
|
||
| return events; | ||
mistermoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| async deleteEventsByCid(tenant: string, cids: Array<string>): Promise<number> { | ||
| if (cids.length === 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| const cidSet = new Set(cids); | ||
| const tenantEventLog = await this.db.partition(tenant); | ||
| const ops = []; | ||
|
|
||
| let numEventsDeleted = 0; | ||
|
|
||
| for await (const [key, value] of tenantEventLog.iterator()) { | ||
| if (cidSet.has(value)) { | ||
| ops.push({ type: 'del', key }); | ||
| numEventsDeleted += 1; | ||
| } | ||
| } | ||
|
|
||
| await tenantEventLog.batch(ops); | ||
|
|
||
| return numEventsDeleted; | ||
| } | ||
|
|
||
| async dump(): Promise<void> { | ||
| console.group('db'); | ||
| await this.db['dump']?.(); | ||
mistermoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.groupEnd(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| export type Event = { | ||
| watermark: string, | ||
| messageCid: string | ||
| }; | ||
|
|
||
|
|
||
| export type GetEventsOptions = { | ||
| gt: string | ||
| }; | ||
|
|
||
| export interface EventLog { | ||
| /** | ||
| * opens a connection to the underlying store | ||
| */ | ||
| open(): Promise<void>; | ||
|
|
||
| /** | ||
| * closes the connection to the underlying store | ||
| */ | ||
| close(): Promise<void>; | ||
|
|
||
| /** | ||
| * adds an event to a tenant's event log | ||
| * @param tenant - the tenant's DID | ||
| * @param messageCid - the CID of the message | ||
mistermoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @returns {Promise<string>} watermark | ||
| */ | ||
| append(tenant: string, messageCid: string): Promise<string> | ||
mistermoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * retrieves all of a tenant's events that occurred after the watermark provided. | ||
| * If no watermark is provided, all events for a given tenant will be returned. | ||
| * | ||
| * @param tenant | ||
| * @param watermark | ||
| */ | ||
| getEvents(tenant: string, options?: GetEventsOptions): Promise<Array<Event>> | ||
|
|
||
| /** | ||
| * deletes any events that have any of the cids provided | ||
mistermoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @param tenant | ||
| * @param cids | ||
| */ | ||
| deleteEventsByCid(tenant: string, cids: Array<string>): Promise<number> | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.