-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
15 changed files
with
167 additions
and
59 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
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,50 @@ | ||
import type { Data, Interest, Name } from "@ndn/packet"; | ||
import { collect, filter, map, pipeline, take } from "streaming-iterables"; | ||
|
||
import * as S from "./data-store"; | ||
|
||
/** | ||
* Data packet storage based on array. | ||
* | ||
* @remarks | ||
* This is a minimal implementation of DataStore interfaces. It has small code size but every | ||
* operation has O(N) time complexity. | ||
*/ | ||
export class DataArray implements S.ListNames, S.ListData, S.Get, S.Find, S.Insert, S.Delete { | ||
private array: Data[] = []; | ||
|
||
public listNames(prefix?: Name): AsyncIterable<Name> { | ||
return map((data) => data.name, this.listData(prefix)); | ||
} | ||
|
||
public async *listData(prefix?: Name): AsyncIterable<Data> { | ||
if (prefix === undefined) { | ||
yield* this.array; | ||
} else { | ||
yield* filter((data) => prefix.isPrefixOf(data.name), this.array); | ||
} | ||
} | ||
|
||
public async get(name: Name): Promise<Data | undefined> { | ||
return this.array.find((data) => name.equals(data.name)); | ||
} | ||
|
||
public async find(interest: Interest): Promise<Data | undefined> { | ||
const [found] = await pipeline( | ||
() => this.array, | ||
filter((data: Data) => data.canSatisfy(interest)), | ||
take(1), | ||
collect, | ||
); | ||
return found; | ||
} | ||
|
||
public async insert(...args: S.Insert.Args<{}>): Promise<void> { | ||
const { pkts } = S.Insert.parseArgs<{}>(args); | ||
this.array.push(...await collect(pkts)); | ||
} | ||
|
||
public async delete(...names: readonly Name[]): Promise<void> { | ||
this.array = this.array.filter((data) => !names.some((name) => name.equals(data.name))); | ||
} | ||
} |
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,65 @@ | ||
import "@ndn/packet/test-fixture/expect"; | ||
|
||
import { Forwarder } from "@ndn/fw"; | ||
import { Data, Interest, Name, type NameLike } from "@ndn/packet"; | ||
import { BufferChunkSource, fetch, serve } from "@ndn/segmented-object"; | ||
import { makeObjectBody } from "@ndn/segmented-object/test-fixture/object-body"; | ||
import { collect, map } from "streaming-iterables"; | ||
import { expect } from "vitest"; | ||
|
||
import type { DataStore as S } from ".."; | ||
|
||
export async function testDataStoreBasic(store: S.Insert & Partial<S.ListNames & S.ListData & S.Get & S.Find & S.Delete>): Promise<void> { | ||
const checkNames = async (prefix: NameLike | undefined, positive: Iterable<NameLike>, negative: Iterable<NameLike> = []) => { | ||
prefix = prefix ? Name.from(prefix) : undefined; | ||
if (store.listNames) { | ||
await expect(collect(store.listNames(prefix))).resolves.toEqualNames(positive); | ||
} | ||
if (store.listData) { | ||
await expect(collect(map((data) => data.name, store.listData(prefix)))) | ||
.resolves.toEqualNames(positive); | ||
} | ||
if (store.get) { | ||
for (const name of positive) { | ||
await expect(store.get(Name.from(name))).resolves.toHaveName(name); | ||
} | ||
for (const name of negative) { | ||
await expect(store.get(Name.from(name))).resolves.toBeUndefined(); | ||
} | ||
} | ||
if (store.find) { | ||
for (const name of positive) { | ||
await expect(store.find(new Interest(name))).resolves.toHaveName(name); | ||
} | ||
for (const name of negative) { | ||
await expect(store.find(new Interest(name))).resolves.toBeUndefined(); | ||
} | ||
} | ||
}; | ||
|
||
await store.insert(new Data("/A/1"), new Data("/A/2")); | ||
await checkNames(undefined, ["/A/1", "/A/2"], ["/B/1", "/B/2"]); | ||
|
||
await store.insert(new Data("/B/3"), (async function*() { | ||
yield new Data("/B/2"); | ||
yield new Data("/B/1"); | ||
})()); | ||
await checkNames(undefined, ["/A/1", "/A/2", "/B/1", "/B/2", "/B/3"]); | ||
await checkNames("/A", ["/A/1", "/A/2"]); | ||
await checkNames("/B", ["/B/1", "/B/2", "/B/3"]); | ||
|
||
if (!store.delete) { | ||
return; | ||
} | ||
await store.delete(new Name("/A/0"), new Name("/A/1"), new Name("/B/3")); | ||
await checkNames(undefined, ["/A/2", "/B/1", "/B/2"], ["/A/0", "/B/3"]); | ||
} | ||
|
||
export async function testDataStoreSegmentedObject(store: S.ListNames & S.Insert): Promise<void> { | ||
const fw = Forwarder.create(); | ||
const body = makeObjectBody(500 * 25); | ||
const producer = serve("/S", new BufferChunkSource(body, { chunkSize: 500 }), { pOpts: { fw } }); | ||
await store.insert(fetch("/S", { fw })); | ||
producer.close(); | ||
await expect(collect(store.listNames())).resolves.toHaveLength(25); | ||
} |
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,16 @@ | ||
import "@ndn/packet/test-fixture/expect"; | ||
|
||
import { test } from "vitest"; | ||
|
||
import { DataArray } from ".."; | ||
import { testDataStoreBasic, testDataStoreSegmentedObject } from "../test-fixture/data-store"; | ||
|
||
test("basic", async () => { | ||
const store = new DataArray(); | ||
await testDataStoreBasic(store); | ||
}); | ||
|
||
test("segmented object", async () => { | ||
const store = new DataArray(); | ||
await testDataStoreSegmentedObject(store); | ||
}); |
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