Skip to content

Commit

Permalink
repo: upgrade to Level 9.x
Browse files Browse the repository at this point in the history
abstract-level 1.x is no longer supported.
  • Loading branch information
yoursunny committed Dec 9, 2024
1 parent 2d13ed1 commit 14a8ed7
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 58 deletions.
6 changes: 3 additions & 3 deletions integ/browser-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"devDependencies": {
"@nodelib/fs.walk": "^3.0.1",
"@types/webpack": "^5.28.5",
"@zenfs/core": "^1.6.5",
"@zenfs/core": "^1.6.6",
"@zenfs/dom": "^1.0.7",
"fork-ts-checker-webpack-plugin": "^9.0.2",
"html-webpack-plugin": "^5.6.3",
"puppeteer": "^23.9.0",
"puppeteer": "^23.10.1",
"ts-loader": "^9.5.1",
"tslib": "^2.8.1",
"type-fest": "^4.30.0",
"webpack": "^5.96.1",
"webpack": "^5.97.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion integ/cxx-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"tslib": "^2.8.1"
},
"devDependencies": {
"execa": "^9.5.1",
"execa": "^9.5.2",
"streaming-iterables": "^8.0.1"
}
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
"test": "vitest",
"typedoc": "bash mk/typedoc.sh"
},
"packageManager": "pnpm@9.14.4+sha512.c8180b3fbe4e4bca02c94234717896b5529740a6cbadf19fa78254270403ea2f27d4e1d46a08a0f56c89b63dc8ebfd3ee53326da720273794e6200fcf0d184ab",
"packageManager": "pnpm@9.15.0+sha512.76e2379760a4328ec4415815bcd6628dee727af3779aaa4c914e3944156c4299921a89f976381ee107d41f12cfa4b66681ca9c718f0668fa0831ed4c6d8ba56c",
"devDependencies": {
"@types/node": "^22.10.1",
"@types/wtfnode": "^0.7.3",
"@typescript/lib-dom": "npm:@types/[email protected].185",
"@typescript/lib-dom": "npm:@types/[email protected].186",
"@vitest/coverage-v8": "^2.1.8",
"@yoursunny/xo-config": "0.59.0",
"codedown": "^3.2.1",
"tslib": "^2.8.1",
"typedoc": "^0.27.2",
"typedoc": "^0.27.4",
"typescript": "~5.7.2",
"vitest": "^2.1.8"
},
Expand All @@ -39,7 +39,6 @@
},
"updateConfig": {
"ignoreDependencies": [
"abstract-level-1",
"graphql-request"
]
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@ndn/node-transport": "workspace:*",
"@ndn/packet": "workspace:*",
"@ndn/util": "workspace:*",
"dotenv": "^16.4.6",
"dotenv": "^16.4.7",
"env-var": "^7.5.0",
"tslib": "^2.8.1",
"wtfnode": "^0.9.4"
Expand Down
2 changes: 1 addition & 1 deletion pkg/fileserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@ndn/segmented-object": "workspace:*",
"@ndn/tlv": "workspace:*",
"@ndn/util": "workspace:*",
"@zenfs/core": "^1.6.5",
"@zenfs/core": "^1.6.6",
"mnemonist": "^0.39.8",
"obliterator": "^2.0.4",
"streaming-iterables": "^8.0.1",
Expand Down
2 changes: 1 addition & 1 deletion pkg/ndnsec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@ndn/tlv": "workspace:*",
"@ndn/util": "workspace:*",
"@yoursunny/asn1": "0.0.20200718",
"execa": "^9.5.1",
"execa": "^9.5.2",
"tslib": "^2.8.1"
}
}
2 changes: 1 addition & 1 deletion pkg/pyrepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@ndn/ndnsec": "workspace:^",
"@ndn/nfdmgmt": "workspace:^",
"@types/netmask": "^2.0.5",
"execa": "^9.5.1",
"execa": "^9.5.2",
"netmask": "^2.0.2"
}
}
2 changes: 1 addition & 1 deletion pkg/repo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import assert from "node:assert/strict";

## DataStore

**DataStore** is a Data packet storage, based on an [abstract-level](https://www.npmjs.com/package/abstract-level) compatible key-value database.
**DataStore** is a Data packet storage, based on an [abstract-level](https://www.npmjs.com/package/abstract-level) 2.x compatible key-value database.
It implements most of `DataStore` interfaces defined in `@ndn/repo-api` package, offering APIs to insert and delete Data packets.

`DataStore.create()` function creates a **DataStore** instance, but it's more convenient to use a helper function:
Expand Down
3 changes: 1 addition & 2 deletions pkg/repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"@ndn/tlv": "workspace:*",
"@ndn/util": "workspace:*",
"abstract-level": "^2.0.1",
"abstract-level-1": "npm:abstract-level@^1.0.4",
"level": "^8.0.1",
"level": "^9.0.0",
"memory-level": "^2.0.0",
"streaming-iterables": "^8.0.1",
"tslib": "^2.8.1",
Expand Down
25 changes: 5 additions & 20 deletions pkg/repo/src/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export class DataStore extends TypedEventTarget<EventMap>
return this.db.close();
}

private async *iterRecords(prefix?: Name): AsyncGenerator<DB.Record> {
private async *iterRecords(prefix?: Name): AsyncGenerator<DB.Value> {
const range = prefix ? { gte: prefix } : {};
const it = (this.db as DB.Db2).iterator(range);
for await (const [name, record] of it as unknown as AsyncIterable<[Name, DB.Record]>) {
const it = this.db.iterator(range);
for await (const [name, record] of it as unknown as AsyncIterable<[Name, DB.Value]>) {
if (prefix?.isPrefixOf(name) === false) {
break;
}
Expand Down Expand Up @@ -101,20 +101,8 @@ export class DataStore extends TypedEventTarget<EventMap>

/** Retrieve Data by exact name. */
public async get(name: Name): Promise<Data | undefined> {
let record: DB.Record | undefined;
try {
record = await this.db.get(name);
// if record does not exist:
// - abstract-level@1 throws not-found error
// - abstract-level@2 returns undefined
} catch (err: unknown) {
if (DB.isNotFound(err)) {
return undefined;
}
throw err;
}

return record === undefined || DB.isExpired(record) ? undefined : record.data;
const record = await this.db.get(name) as DB.Value | undefined;
return !record || DB.isExpired(record) ? undefined : record.data;
}

/** Find Data that satisfies Interest. */
Expand All @@ -140,7 +128,6 @@ export class DataStore extends TypedEventTarget<EventMap>
*/
public async insert(...args: S.Insert.Args<DataStore.InsertOptions>): Promise<void> {
const { opts, pkts } = S.Insert.parseArgs<DataStore.InsertOptions>(args);
await this.db.open(); // workaround for constructor(AbstractLevelDOWN)
const tx = this.tx();
for await (const pkt of pkts) {
tx.insert(pkt, opts);
Expand All @@ -153,7 +140,6 @@ export class DataStore extends TypedEventTarget<EventMap>
* @see {@link Transaction.delete}
*/
public async delete(...names: readonly Name[]): Promise<void> {
await this.db.open(); // workaround for constructor(AbstractLevelDOWN)
const tx = this.tx();
for (const name of names) {
tx.delete(name);
Expand All @@ -163,7 +149,6 @@ export class DataStore extends TypedEventTarget<EventMap>

/** Delete all expired records. */
public async clearExpired(): Promise<void> {
await this.db.open(); // workaround for constructor(AbstractLevelDOWN)
const tx = this.tx();
const it = filter(DB.filterExpired(true), this.iterRecords());
for await (const { name } of it) {
Expand Down
35 changes: 13 additions & 22 deletions pkg/repo/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Data, Name } from "@ndn/packet";
import { Decoder, Encoder } from "@ndn/tlv";
import { assert, fromUtf8 } from "@ndn/util";
import type { AbstractChainedBatch, AbstractDatabaseOptions, AbstractLevel } from "abstract-level";
import type { AbstractChainedBatch as Batch1, AbstractLevel as Level1 } from "abstract-level-1";
import type { Promisable } from "type-fest";

/** Value stored in database. */
export interface Record {
export interface Value {
readonly data: Data;
readonly name: Name;
readonly insertTime: number;
Expand All @@ -15,7 +14,7 @@ export interface Record {
const textEncoder = new TextEncoder();

/** Required options when creating abstract-level compatible key-value database. */
export const AbstractLevelOptions: AbstractDatabaseOptions<Name, Record> = {
export const AbstractLevelOptions: AbstractDatabaseOptions<Name, Value> = {
keyEncoding: {
name: "d6d494ba-4d45-4b51-a102-26b8867c5034",
format: "view",
Expand All @@ -29,19 +28,19 @@ export const AbstractLevelOptions: AbstractDatabaseOptions<Name, Record> = {
valueEncoding: {
name: "bb613530-3278-45f1-b5ae-7ced392eb602",
format: "view",
encode(record: Record): Uint8Array {
encode(record: Value): Uint8Array {
const encoder = new Encoder();
const jText = JSON.stringify(record, ["insertTime", "expireTime"] satisfies Array<keyof Record>);
const jText = JSON.stringify(record, ["insertTime", "expireTime"] satisfies Array<keyof Value>);
const jBufCap = 3 * jText.length;
const jBuf = encoder.prependRoom(jBufCap);
const { read: jTextLen = 0, written: jBufLen = 0 } = textEncoder.encodeInto(jText, jBuf);
assert(jTextLen === jText.length);
encoder.encode(record.data);
return encoder.output.subarray(0, encoder.size - jBufCap + jBufLen);
},
decode(stored: Uint8Array): Record {
decode(stored: Uint8Array): Value {
const tlv = new Decoder(stored).read();
const record = JSON.parse(fromUtf8(tlv.after)) as Record;
const record = JSON.parse(fromUtf8(tlv.after)) as Value;
Object.defineProperties(record, {
data: {
configurable: true,
Expand All @@ -64,37 +63,29 @@ export const AbstractLevelOptions: AbstractDatabaseOptions<Name, Record> = {
},
};

export type Db2 = AbstractLevel<any, Name, Record>;

/** An abstract-level compatible key-value database. */
export type Db = Db2 | Level1<any, Name, Record>;
export type Db = AbstractLevel<any, Name, Value>;

/** Function to create Db. */
export type DbOpener = (opts: AbstractDatabaseOptions<Name, Record>) => Promisable<Db>;
export type DbOpener = (opts: AbstractDatabaseOptions<Name, Value>) => Promisable<Db>;

/**
* Constructor of AbstractLevel subclass.
* @typeParam A - Constructor arguments, excluding the last.
* @typeParam O - Last constructor argument, which must be the options object.
*/
export type DbCtor<A extends unknown[], O extends {}> =
new(...a: [...A, O & AbstractDatabaseOptions<Name, Record>]) => Db;
new(...a: [...A, O & AbstractDatabaseOptions<Name, Value>]) => Db;

/** A transaction chain in key-value database. */
export type DbChain = AbstractChainedBatch<Db, Name, Record> | Batch1<Db, Name, Record>;

/** Determine whether `err` represents "not found". */
export function isNotFound(err: unknown): boolean {
const { code, notFound } = err as { code?: string; notFound?: true };
return code === "LEVEL_NOT_FOUND" || notFound === true;
}
export type DbChain = AbstractChainedBatch<Db, Name, Value>;

/** Determine whether a record has expired. */
export function isExpired({ expireTime = Infinity }: Record, now = Date.now()): boolean {
export function isExpired({ expireTime = Infinity }: Value, now = Date.now()): boolean {
return expireTime < now;
}

/** Create a filter function for either expired or unexpired records. */
export function filterExpired(expired: boolean, now = Date.now()): (record: Record) => boolean {
return (record: Record) => isExpired(record, now) === expired;
export function filterExpired(expired: boolean, now = Date.now()): (record: Value) => boolean {
return (record: Value) => isExpired(record, now) === expired;
}
2 changes: 1 addition & 1 deletion pkg/segmented-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@ndn/naming-convention2": "workspace:*",
"@ndn/packet": "workspace:*",
"@ndn/util": "workspace:*",
"@zenfs/core": "^1.6.5",
"@zenfs/core": "^1.6.6",
"it-keepalive": "^1.2.0",
"mnemonist": "^0.39.8",
"obliterator": "^2.0.4",
Expand Down

0 comments on commit 14a8ed7

Please sign in to comment.