Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Propagate getRange support in Listable when available #252

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-horses-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zarrita/core": patch
---

Propagate getRange support in `Listable` when available
14 changes: 13 additions & 1 deletion packages/core/__tests__/consolidated.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "node:path";
import * as url from "node:url";
import { describe, expect, it } from "vitest";
import { assert, describe, expect, it } from "vitest";

import { FileSystemStore } from "@zarrita/storage";
import { tryWithConsolidated, withConsolidated } from "../src/consolidated.js";
Expand Down Expand Up @@ -124,3 +124,15 @@ describe("tryWithConsolidated", () => {
expect(store).toBeInstanceOf(FileSystemStore);
});
});

describe("Listable.getRange", () => {
it("does not expose getRange if the underlying store does not support it", async () => {
let store = await tryWithConsolidated(new Map());
expect("getRange" in store).toBeFalsy();
});
it("retrieves a byte range from an underlying store", async () => {
let root = path.join(__dirname, "../../../fixtures/v2/data.zarr");
let store = await tryWithConsolidated(new FileSystemStore(root));
assert(typeof store.getRange === "function");
});
});
9 changes: 8 additions & 1 deletion packages/core/src/consolidated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ type ConsolidatedMetadata = {
*/
export interface Listable<Store extends Readable> {
/** Get the bytes at a given path. */
get: Store["get"];
get: (...args: Parameters<Store["get"]>) => Promise<Uint8Array | undefined>;
/** Get a byte range at a given path. */
getRange: Store["getRange"];
/** List the contents of the store. */
contents(): { path: AbsolutePath; kind: "array" | "group" }[];
}
Expand Down Expand Up @@ -93,6 +95,7 @@ export async function withConsolidated<Store extends Readable>(
for (let [key, value] of Object.entries(v2_meta.metadata)) {
known_meta[`/${key}`] = value;
}

return {
async get(
...args: Parameters<Store["get"]>
Expand All @@ -108,6 +111,10 @@ export async function withConsolidated<Store extends Readable>(
}
return maybe_bytes;
},
// Delegate range requests to the underlying store.
// Note: Supporting range requests for consolidated metadata is possible
// but unlikely to be useful enough to justify the effort.
getRange: store.getRange?.bind(store),
contents(): { path: AbsolutePath; kind: "array" | "group" }[] {
let contents: { path: AbsolutePath; kind: "array" | "group" }[] = [];
for (let [key, value] of Object.entries(known_meta)) {
Expand Down