Skip to content

Commit

Permalink
file cache -- also use for attr's directly even if readdir not used
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Nov 16, 2023
1 parent 67de0e7 commit 359789b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 28 deletions.
78 changes: 52 additions & 26 deletions websocketfs/lib/metadata-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { stat, readFile } from "fs/promises";
import { decode } from "lz4";
import binarySearch from "binarysearch";
import { symbolicToMode } from "./util";
import { join } from "path";

const log = debug("websocketfs:metadata-file");
const log_cache = debug("cache");

const METADATA_FILE_INTERVAL_MS = 3000;

Expand Down Expand Up @@ -99,22 +99,60 @@ export class MetadataFile {
}
};

private find = (path: string): number => {
return binarySearch(
this.metadataFileContents,
path,
(value: string, find: string) => {
const name = "/" + value.split("\0")[0];
if (name < find) {
return -1;
}
if (name > find) {
return 1;
}
return 0;
},
);
};

private cacheAttrs = (i: number) => {
const v = this.metadataFileContents[i].split("\0");
const data = v[1].split(" ");
const mtime = new Date(parseFloat(data[0]) * 1000);
const attr = {
mtime,
atime: new Date(parseFloat(data[1]) * 1000),
ctime: mtime,
blocks: parseInt(data[2]),
size: parseInt(data[3]),
mode: symbolicToMode(data[4]),
flags: 0,
uid: 0,
gid: 0,
};
this.attrCache.set("/" + v[0], { attr });
return { attr };
};

getattr = (path: string): { errno?: number; attr? } => {
const i = this.find(path);
if (i == -1) {
// error -- no such file
return { errno: -2 };
} else {
return this.cacheAttrs(i);
}
};

readdir = (path: string) => {
if (!this.isReady()) {
throw Error("MetadataFile is not ready");
}
log("readdir", path);
let i = binarySearch(this.metadataFileContents, path, (value, find) => {
const path = "/" + value.split("\0")[0];
if (path < find) {
return -1;
}
if (path > find) {
return 1;
}
return 0;
});
let i = this.find(path);
if (i == -1) {
log_cache("readdir ", path, " WORKED");
log("readdir", path, " -- does not exist");
return [];
}
Expand All @@ -125,32 +163,20 @@ export class MetadataFile {
const v = this.metadataFileContents[i].split("\0");
const name = "/" + v[0];
if (!name.startsWith(path)) {
// definitely done.
// done.
break;
}
if (name.startsWith(pathDir)) {
const filename = name.slice(pathDir.length);
if (!filename.includes("/")) {
filenames.push(filename);
const data = v[1].split(" ");
const mtime = new Date(parseFloat(data[0]) * 1000);
const attr = {
mtime,
atime: new Date(parseFloat(data[1]) * 1000),
ctime: mtime,
blocks: parseInt(data[2]),
size: parseInt(data[3]),
mode: symbolicToMode(data[4]),
flags: 0,
uid: 0,
gid: 0,
};
this.attrCache.set(join(path, filename), { attr });
this.cacheAttrs(i);
}
}
i += 1;
}
this.dirCache.set(path, filenames);
log_cache("readdir ", path, " WORKED");
return filenames;
};
}
15 changes: 14 additions & 1 deletion websocketfs/lib/sftp-fuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { MetadataFile } from "./metadata-file";
export type { IClientOptions };

const log = debug("websocketfs:sftp-fuse");
const log_cache = debug("cache");

type Callback = Function;

Expand Down Expand Up @@ -266,6 +267,12 @@ export default class SftpFuse {
cb(errno ?? 0, attr);
return;
}
if (this.meta?.isReady()) {
const { errno, attr } = this.meta.getattr(path);
cb(errno ?? 0, attr);
return;
}
log_cache("getattr -- cache miss", path);
log("getattr -- cache miss", path);
this.sftp.lstat(path, (err, attr) => {
// log("getattr -- lstat", { path, err, attr });
Expand All @@ -291,6 +298,12 @@ export default class SftpFuse {
cb(errno ?? 0, attr);
return;
}
if (this.meta?.isReady()) {
const { errno, attr } = this.meta.getattr(path);
cb(errno ?? 0, attr);
return;
}
log_cache("fgetattr -- cache miss", path);
const handle = this.sftp.fileDescriptorToHandle(fd);
this.sftp.fstat(handle, (err, attr) => {
if (err) {
Expand Down Expand Up @@ -381,14 +394,14 @@ export default class SftpFuse {
cb(0, this.dirCache.get(path));
return;
}

if (this.meta?.isReady() && !path.startsWith(".")) {
try {
cb(0, this.meta.readdir(path));
} catch (err) {
log("readdir error using metadata file cache:", err);
}
}
log_cache("readdir -- cache miss", path);

try {
let handle;
Expand Down
2 changes: 1 addition & 1 deletion websocketfs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websocketfs",
"version": "0.12.1",
"version": "0.12.2",
"description": "Like sshfs, but over a WebSocket",
"main": "./dist/lib/index.js",
"scripts": {
Expand Down

0 comments on commit 359789b

Please sign in to comment.