-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed747c6
commit 440e057
Showing
11 changed files
with
459 additions
and
419 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,47 @@ | ||
import { Entry, ShallowEntry } from "@peerbit/log"; | ||
import { type ReplicationDomain } from "@peerbit/shared-log"; | ||
import { Documents, type Operation, isPutOperation } from "@peerbit/document"; | ||
|
||
type RangeArgs = { from: number; to: number }; | ||
export type CustomDomain = ReplicationDomain<RangeArgs, Operation>; | ||
|
||
export const createDocumentDomain = <T extends object>( | ||
db: Documents<T, any, CustomDomain>, | ||
options: { | ||
fromValue: (value: T) => number; | ||
fromMissing?: (entry: ShallowEntry | Entry<Operation>) => number; | ||
} | ||
): CustomDomain => { | ||
const fromValue = options.fromValue; | ||
const fromMissing = options.fromMissing || (() => 0xffffffff); | ||
return { | ||
type: "custom", | ||
fromArgs(args, log) { | ||
if (!args) { | ||
return { offset: log.node.identity.publicKey }; | ||
} | ||
return { | ||
offset: args.from, | ||
length: args.to - args.from, | ||
}; | ||
}, | ||
fromEntry: async (entry) => { | ||
const item = await (entry instanceof ShallowEntry | ||
? await db.log.log.get(entry.hash) | ||
: entry | ||
)?.getPayloadValue(); | ||
if (!item) { | ||
// @eslint-ignore no-console | ||
console.error("Item not found"); | ||
return fromMissing(entry); | ||
} | ||
|
||
if (isPutOperation(item)) { | ||
const document = db.index.valueEncoding.decoder(item.data); | ||
return fromValue(document); | ||
} | ||
|
||
return fromMissing(entry); | ||
}, | ||
}; | ||
}; |
This file contains 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 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 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 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 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 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.