Skip to content

Commit

Permalink
chore: make the name required
Browse files Browse the repository at this point in the history
  • Loading branch information
mabels committed Feb 6, 2025
1 parent eb667cc commit 0b4944e
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/blockstore/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export abstract class BaseStoreImpl {
storeKeyName.push(this.storeType);
return storeKeyName.join(":");
});

if (skRes.isErr()) {
return skRes as Result<URI>;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function isLedger(db: unknown): db is Ledger {
return db instanceof LedgerImpl || db instanceof LedgerShell;
}

export function LedgerFactory(name: string | undefined, opts?: ConfigOpts): Ledger {
export function LedgerFactory(name: string, opts?: ConfigOpts): Ledger {
const sthis = ensureSuperThis(opts);
return new LedgerShell(
ledgers.get(keyConfigOpts(sthis, name, opts)).once((key) => {
Expand Down Expand Up @@ -136,7 +136,8 @@ class LedgerImpl implements Ledger {
readonly context = new Context();

get name(): string {
return this.opts.storeUrls.data.data.getParam(PARAM.NAME) ?? "default";
return this.opts.name;
// this.opts.storeUrls.data.data.getParam(PARAM.NAME) ?? "default";
}

addShell(shell: LedgerShell) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/gateways/file/key-bag-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class KeyBagProviderFile implements KeyBagProvider {
if (isNotFoundError(e)) {
return undefined;
}
throw this.logger.Error().Err(e).Str("file", ctx.dirName).Msg("read bag failed").AsError();
throw this.logger.Error().Err(e).Any("file", ctx).Msg("read bag failed").AsError();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/gateways/memory/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { VoidResult } from "../../../blockstore/serde-gateway.js";
export class MemoryGateway implements Gateway {
readonly memorys: Map<string, Uint8Array>;
readonly sthis: SuperThis;
// readonly logger: Logger;
constructor(sthis: SuperThis, memorys: Map<string, Uint8Array>) {
this.memorys = memorys;
// this.logger = ensureLogger(sthis, "MemoryGateway");
this.sthis = sthis;
}

Expand All @@ -30,11 +32,13 @@ export class MemoryGateway implements Gateway {
}

async put(url: URI, bytes: Uint8Array): Promise<VoidResult> {
// ensureLogger(sthis, "MemoryGateway").Debug().Str("url", url.toString()).Msg("put");
this.memorys.set(url.toString(), bytes);
return Result.Ok(undefined);
}
// get could return a NotFoundError if the key is not found
get(url: URI): Promise<GetResult> {
// ensureLogger(sthis, "MemoryGateway").Debug().Str("url", url.toString()).Msg("put");
const x = this.memorys.get(url.toString());
if (!x) {
return Promise.resolve(Result.Err(new NotFoundError("not found")));
Expand Down
11 changes: 8 additions & 3 deletions src/runtime/key-bag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ export class keysByFingerprint implements KeysByFingerprint {
if (found) {
return found;
}
throw this.keybag.logger.Error().Msg("KeyBag: keysByFingerprint: no default").AsError();
throw this.keybag.logger
.Error()
.Str("fpr", fingerPrint)
.Any("fprs", Object.keys(this.keys))
.Msg("keysByFingerprint: not found")
.AsError();
}

async upsert(materialStrOrUint8: string | Uint8Array, def?: boolean, keyBagAction = true): Promise<Result<KeyUpsertResult>> {
Expand Down Expand Up @@ -235,15 +240,15 @@ export class KeyBag {
const keyName = `@${keyFactory()}@`;
const ret = await this.getNamedKey(keyName);
if (ret.isErr()) {
return ret as unknown as Result<URI>;
return Result.Err(ret);
}
const urb = url.build().setParam(PARAM.STORE_KEY, keyName);
return Result.Ok(urb.URI());
}
if (storeKey.startsWith("@") && storeKey.endsWith("@")) {
const ret = await this.getNamedKey(storeKey);
if (ret.isErr()) {
return ret as unknown as Result<URI>;
return Result.Err(ret);
}
}
return Result.Ok(url);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export interface WriteQueue<T extends DocUpdate<S>, S extends DocTypes = DocType
}

export interface LedgerOpts {
readonly name?: string;
readonly name: string;
// readonly public?: boolean;
readonly meta?: DbMeta;
readonly gatewayInterceptor?: SerdeGatewayInterceptor;
Expand Down
9 changes: 9 additions & 0 deletions tests/fireproof/crdt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Fresh crdt", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, "test-crdt-cold"),
Expand Down Expand Up @@ -56,6 +57,7 @@ describe("CRDT with one record", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, `test@${sthis.nextId().str}`),
Expand Down Expand Up @@ -112,6 +114,7 @@ describe("CRDT with a multi-write", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, "test-crdt-cold"),
Expand Down Expand Up @@ -182,6 +185,7 @@ describe("CRDT with two multi-writes", function () {
beforeEach(async () => {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, `test-multiple-writes@${sthis.nextId().str}`),
Expand Down Expand Up @@ -236,6 +240,7 @@ describe("Compact a named CRDT with writes", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, `named-crdt-compaction`),
Expand Down Expand Up @@ -298,6 +303,7 @@ describe("CRDT with an index", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, "test-crdt-cold"),
Expand Down Expand Up @@ -347,6 +353,7 @@ describe("Loader with a committed transaction", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, dbname),
Expand Down Expand Up @@ -398,6 +405,7 @@ describe("Loader with two committed transactions", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, "test-loader"),
Expand Down Expand Up @@ -451,6 +459,7 @@ describe("Loader with many committed transactions", function () {
beforeEach(async function () {
await sthis.start();
const dbOpts: LedgerOpts = {
name: "test-crdt",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, "test-loader-many"),
Expand Down
35 changes: 35 additions & 0 deletions tests/fireproof/fireproof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
Database,
isDatabase,
} from "@fireproof/core";
import { getDefaultURI } from "../../src/blockstore/register-store-protocol.js";
import { URI } from "@adviser/cement";

export function carLogIncludesGroup(list: bs.AnyLink[], cid: CID) {
return list.some((c) => c.equals(cid));
Expand Down Expand Up @@ -114,6 +116,39 @@ describe("public API", function () {
});
});

describe("database fullconfig", () => {
const sthis = ensureSuperThis();
it("have the right name", async () => {
let protocol: string | undefined;
const url = sthis.env.get("FP_STORAGE_URL");
if (url) {
protocol = URI.from(url).protocol;
}
const base = getDefaultURI(sthis, protocol);
const db = fireproof("my-funky-name", {
storeUrls: {
base: base,
// meta: `${base}/meta?taste=${taste}`,
data: {
meta: base.build().pathname("dist/full/meta"),
data: base.build().pathname("dist/full/data"),
wal: base.build().pathname("dist/full/wal"),
},
idx: {
meta: base.build().pathname("dist/full/idx-meta"),
data: base.build().pathname("dist/full/idx-data"),
wal: base.build().pathname("dist/full/idx-wal"),
},
// wal: `${base}/wal?taste=${taste}`,
},
});
expect(db).toBeTruthy();
expect(db.name).toBe("my-funky-name");
await db.put({ _id: "test", foo: "bar" });
expect(db.name).toBe("my-funky-name");
});
});

describe("basic ledger", function () {
interface Doc {
foo: string;
Expand Down
1 change: 1 addition & 0 deletions tests/fireproof/indexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ describe("basic Index upon cold start", function () {
const logger = sthis.logger.With().Module("IndexerTest").Logger();
logger.Debug().Msg("enter beforeEach");
dbOpts = {
name: "test-indexer-cold",
writeQueue: defaultWriteQueueOpts({}),
keyBag: rt.kb.defaultKeyBagOpts(sthis),
storeUrls: toStoreURIRuntime(sthis, "test-indexer-cold"),
Expand Down

0 comments on commit 0b4944e

Please sign in to comment.