Skip to content

Commit

Permalink
perf: skip maxDepth filtering if natively supported
Browse files Browse the repository at this point in the history
Skips `maxDepth` filtering if _all_ drivers in the current storage
instance natively support it (via the `maxDepth` flag).

Also enables the `maxDepth` flag for `fs` and `fs-lite` drivers.
  • Loading branch information
43081j committed Jan 3, 2025
1 parent cc0b0ca commit 5a40fb2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/drivers/fs-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
return {
name: DRIVER_NAME,
options: opts,
flags: {
maxDepth: true,
},
hasItem(key) {
return existsSync(r(key));
},
Expand Down
3 changes: 3 additions & 0 deletions src/drivers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
return {
name: DRIVER_NAME,
options: opts,
flags: {
maxDepth: true,
},
hasItem(key) {
return existsSync(r(key));
},
Expand Down
9 changes: 7 additions & 2 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ export function createStorage<T extends StorageValue>(
const mounts = getMounts(base, true);
let maskedMounts: string[] = [];
const allKeys = [];
let allMountsSupportMaxDepth = true;
for (const mount of mounts) {
if (!mount.driver.flags?.maxDepth) {
allMountsSupportMaxDepth = false;
}
const rawKeys = await asyncCall(
mount.driver.getKeys,
mount.relativeBase,
Expand All @@ -368,10 +372,11 @@ export function createStorage<T extends StorageValue>(
...maskedMounts.filter((p) => !p.startsWith(mount.mountpoint)),
];
}
const shouldFilterByDepth =
opts.maxDepth !== undefined && !allMountsSupportMaxDepth;
return allKeys.filter(
(key) =>
(opts.maxDepth === undefined ||
filterKeyByDepth(key, opts.maxDepth)) &&
(!shouldFilterByDepth || filterKeyByDepth(key, opts.maxDepth)) &&
filterKeyByBase(key, base)
);
},
Expand Down

0 comments on commit 5a40fb2

Please sign in to comment.