- vdisk done?

- vdiskman done?
This commit is contained in:
2026-09-19 06:27:12 +02:00
parent 849e0e61f5
commit 933b1fc5e8
2 changed files with 279 additions and 172 deletions
+237 -150
View File
@@ -1,12 +1,14 @@
import type { ReadableStream } from "@core/util/stream";
import { NodeType, type DirectoryNode, type Node, type NodeEntry, type NodeHash, type NodePointer } from "./node"; import { NodeType, type DirectoryNode, type Node, type NodeEntry, type NodeHash, type NodePointer } from "./node";
export interface VDiskConfig { export interface VDiskConfig {
id: number; id: number;
label: number; label: string;
index: number; index: number;
readonly: boolean;
size: number;
base: NodeHash; base: NodeHash;
dataC: DataConfig; dataC: DataConfig;
//metaC: MetaConfig;
} }
export interface DataConfig { export interface DataConfig {
@@ -15,11 +17,9 @@ export interface DataConfig {
cacheTTL: number; cacheTTL: number;
} }
//export interface MetaConfig {}
export interface IDataStore { export interface IDataStore {
read(uuid: string): Promise<Uint8Array>; read(uuid: string): Promise<Uint8Array>;
stream(uuid: string): null; // Stream stream(uuid: string): ReadableStream;
put(uuid: string, blob: Blob): Promise<void>; put(uuid: string, blob: Blob): Promise<void>;
delete(uuid: string): Promise<void>; delete(uuid: string): Promise<void>;
has(uuid: string): Promise<boolean>; has(uuid: string): Promise<boolean>;
@@ -28,8 +28,8 @@ export interface IDataStore {
export interface IMetaStore { export interface IMetaStore {
loadAll(): Promise<Map<number, Node | null>>; loadAll(): Promise<Map<number, Node | null>>;
write(inode: number, node: Node): Promise<void>; write(entries: Array<[number, Node | null]>): Promise<void>;
delete(inode: number): Promise<void>; delete(entries: Array<number>): Promise<void>;
} }
export class VDisk { export class VDisk {
@@ -39,23 +39,88 @@ export class VDisk {
private readonly _totalSize: number; private readonly _totalSize: number;
private readonly _base: NodeHash; private readonly _base: NodeHash;
private readonly _dataStore: IDataStore; private readonly _dataStore: IDataStore;
private readonly _metaStore: IMetaStore;
private _iIndex: number; private _iIndex: number;
private _rIndex: ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>>; private _rIndex: ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>>;
private _overlay: Map<number, Node | null>; private _overlay: Map<number, Node | null>;
private _metaStore: IMetaStore; private _pending: Map<number, Node | null> | null;
private _rIndexInvalid: boolean; private _rIndexInvalid: boolean;
private _flush: Promise<void>;
private constructor(config: VDiskConfig, data: IDataStore, meta: IMetaStore) {
this._id = config.id;
this._label = config.label;
this._readonly = config.readonly;
this._totalSize = config.size;
this._base = config.base;
this._iIndex = config.index;
this._dataStore = data;
this._metaStore = meta;
this._overlay = new Map();
this._rIndex = new Map();
this._rIndexInvalid = true;
this._pending = null;
this._flush = Promise.resolve();
}
private async _batch<T>(fn: () => T): Promise<T> {
if (this._pending) {
console.error("Nested batch not ok");
}
this._pending = new Map();
let result: T;
try {
result = fn();
} catch (err) {
this._pending = null;
throw err;
}
const pending = this._pending;
this._pending = null;
for (const [inode, node] of pending) {
this._overlay.set(inode, node);
}
const entries = Array.from(pending.entries());
const next = this._flush.then(() => this._metaStore.write(entries));
this._flush = next.catch(() => {});
await next;
return result;
}
private _isWritable(): void { private _isWritable(): void {
if (this._readonly) throw Error(`The ${this._label} device under id - ${this._id} is marked readonly`); if (this._readonly) throw Error(`The ${this._label} device under id - ${this._id} is marked readonly`);
} }
private _putNode(inode: number, node: Node | null): void { private _putNode(inode: number, node: Node | null): void {
this._overlay.set(inode, node); if (!this._pending)
throw new Error(
"_putNode called outside of batch. This is not a BUG. Every mutation must be wrapped in a _batch call",
);
this._pending.set(inode, node);
}
public static async open(config: VDiskConfig, data: IDataStore, meta: IMetaStore): Promise<VDisk> {
const vdisk = new VDisk(config, data, meta);
await vdisk.loadOverlay();
return vdisk;
} }
public getNode(inode: number): Node | undefined { public getNode(inode: number): Node | undefined {
if (this._pending && this._pending.has(inode)) {
return this._pending.get(inode) ?? undefined;
}
const node = this._overlay.get(inode); const node = this._overlay.get(inode);
if (node) return node; if (node) return node;
if (node === null) return undefined; if (node === null) return undefined;
@@ -70,195 +135,217 @@ export class VDisk {
| { type: NodeType.File; mode: number; owner: number; group: number; content: string; size: number } | { type: NodeType.File; mode: number; owner: number; group: number; content: string; size: number }
| { type: NodeType.Directory; mode: number; owner: number; group: number } | { type: NodeType.Directory; mode: number; owner: number; group: number }
| { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string }, | { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string },
): number { ): Promise<number> {
this._isWritable(); return this._batch(() => {
this._isWritable();
const index = this._iIndex++;
const time = Date.now();
const link_count = data.type === NodeType.Directory ? 2 : 1;
const index = this._iIndex++; const base = {
const time = Date.now(); inode: index,
const link_count = data.type === NodeType.Directory ? 2 : 1; link_count,
type: data.type,
mode: data.mode,
owner: data.owner,
group: data.group,
mtime: time,
ctime: time,
atime: time,
};
const base = { const pnode = this.getNode(pinode) as DirectoryNode;
inode: index,
link_count,
type: data.type,
mode: data.mode,
owner: data.owner,
group: data.group,
mtime: time,
ctime: time,
atime: time,
};
const pnode = this.getNode(pinode) as DirectoryNode; let node: Node;
switch (data.type) {
case NodeType.File:
node = { ...base, type: NodeType.File, content: data.content, size: data.size };
break;
case NodeType.Directory:
node = { ...base, type: NodeType.Directory, children: new Map() };
break;
case NodeType.Symlink:
node = { ...base, type: NodeType.Symlink, target: data.target };
break;
}
let node: Node; const children = new Map(pnode.children);
switch (data.type) { children.set(name, index);
case NodeType.File:
node = { ...base, type: NodeType.File, content: data.content, size: data.size };
break;
case NodeType.Directory:
node = { ...base, type: NodeType.Directory, children: new Map() };
break;
case NodeType.Symlink:
node = { ...base, type: NodeType.Symlink, target: data.target };
break;
}
const children = new Map(pnode.children); const parent = {
children.set(name, index); ...pnode,
children,
link_count: pnode.link_count + (data.type === NodeType.Directory ? 1 : 0),
mtime: time,
ctime: time,
};
const parent = { this._putNode(index, node);
...pnode, this._putNode(pinode, parent);
children, this.invalidateRIndex();
link_count: pnode.link_count + (data.type === NodeType.Directory ? 1 : 0),
mtime: time,
ctime: time,
};
this._putNode(index, node); return index;
this._putNode(pinode, parent); });
this.invalidateRIndex();
return index;
} }
public linkNodeEntry(pinode: number, name: string, tinode: number): void { public linkNodeEntry(pinode: number, name: string, tinode: number): Promise<void> {
this._isWritable(); return this._batch(() => {
this._isWritable();
const pnode = this.getNode(pinode) as DirectoryNode; const pnode = this.getNode(pinode) as DirectoryNode;
const tnode = this.getNode(tinode) as Node; const tnode = this.getNode(tinode) as Node;
const time = Date.now(); const time = Date.now();
const children = new Map(pnode.children); const children = new Map(pnode.children);
children.set(name, tnode.inode); children.set(name, tnode.inode);
const parent = { ...pnode, children, mtime: time, ctime: time }; const parent = { ...pnode, children, mtime: time, ctime: time };
const target = { ...tnode, link_count: tnode.link_count + 1, ctime: time }; const target = { ...tnode, link_count: tnode.link_count + 1, ctime: time };
this._putNode(tinode, target); this._putNode(tinode, target);
this._putNode(pinode, parent); this._putNode(pinode, parent);
this.invalidateRIndex(); this.invalidateRIndex();
});
} }
public unlinkNodeEntry(inode: number, name: string): void { public unlinkNodeEntry(inode: number, name: string): Promise<void> {
this._isWritable(); return this._batch(() => {
this._isWritable();
const pnode = this.getNode(inode) as DirectoryNode; const pnode = this.getNode(inode) as DirectoryNode;
const tinode = pnode.children.get(name)!; const tinode = pnode.children.get(name)!;
const tnode = this.getNode(tinode) as Node; const tnode = this.getNode(tinode) as Node;
const time = Date.now(); const time = Date.now();
const children = new Map(pnode.children); const children = new Map(pnode.children);
children.delete(name); children.delete(name);
const parent = { ...pnode, children, mtime: time, ctime: time }; const parent = { ...pnode, children, mtime: time, ctime: time };
const link_count = tnode.link_count - 1; const link_count = tnode.link_count - 1;
const wasLast = link_count === 0; const wasLast = link_count === 0;
this._putNode(inode, parent); this._putNode(inode, parent);
this._putNode(tinode, wasLast ? null : { ...tnode, link_count, ctime: Date.now() }); this._putNode(tinode, wasLast ? null : { ...tnode, link_count, ctime: Date.now() });
this.invalidateRIndex(); this.invalidateRIndex();
});
} }
public moveNodeEntry(pinode: number, dinode: number, tname: string, dname?: string): void { public moveNodeEntry(pinode: number, dinode: number, tname: string, dname?: string): Promise<void> {
this._isWritable(); return this._batch(() => {
this._isWritable();
const pnode = this.getNode(pinode) as DirectoryNode; const pnode = this.getNode(pinode) as DirectoryNode;
const dnode = this.getNode(dinode) as DirectoryNode; const dnode = this.getNode(dinode) as DirectoryNode;
const tinode = pnode.children.get(tname)!; const tinode = pnode.children.get(tname)!;
const tnode = this.getNode(tinode)!; const tnode = this.getNode(tinode)!;
const name = dname ?? tname; const name = dname ?? tname;
const isDir = tnode.type === NodeType.Directory; const isDir = tnode.type === NodeType.Directory;
const time = Date.now(); const time = Date.now();
if (pinode === dinode) {
const pchildren = new Map(pnode.children);
pchildren.delete(tname);
pchildren.set(name, tinode);
const parent = { ...pnode, children: pchildren, mtime: time, ctime: time };
const target = { ...tnode, ctime: time };
this._putNode(pinode, parent);
this._putNode(tinode, target);
this.invalidateRIndex();
return;
}
if (pinode === dinode) {
const pchildren = new Map(pnode.children); const pchildren = new Map(pnode.children);
pchildren.delete(tname); pchildren.delete(tname);
pchildren.set(name, tinode);
const parent = { ...pnode, children: pchildren, mtime: time, ctime: time }; const dchildren = new Map(dnode.children);
dchildren.set(name, tinode);
const parent = {
...pnode,
children: pchildren,
mtime: time,
ctime: time,
link_count: pnode.link_count - (isDir ? 1 : 0),
};
const target = { ...tnode, ctime: time }; const target = { ...tnode, ctime: time };
const destination = {
...dnode,
children: dchildren,
mtime: time,
ctime: time,
link_count: dnode.link_count + (isDir ? 1 : 0),
};
this._putNode(pinode, parent); this._putNode(pinode, parent);
this._putNode(tinode, target); this._putNode(tinode, target);
this._putNode(dinode, destination);
this.invalidateRIndex(); this.invalidateRIndex();
return; });
}
const pchildren = new Map(pnode.children);
pchildren.delete(tname);
const dchildren = new Map(dnode.children);
dchildren.set(name, tinode);
const parent = {
...pnode,
children: pchildren,
mtime: time,
ctime: time,
link_count: pnode.link_count - (isDir ? 1 : 0),
};
const target = { ...tnode, ctime: time };
const destination = {
...dnode,
children: dchildren,
mtime: time,
ctime: time,
link_count: dnode.link_count + (isDir ? 1 : 0),
};
this._putNode(pinode, parent);
this._putNode(tinode, target);
this._putNode(dinode, destination);
this.invalidateRIndex();
} }
public writeNodeEntry(inode: number, data: { mode?: number; owner?: number; group?: number }): void { public writeNodeEntry(inode: number, data: { mode?: number; owner?: number; group?: number }): Promise<void> {
return this._batch(() => {
this._isWritable();
const node = this.getNode(inode)!;
const newNode: Node = {
...node,
mode: data.mode ?? node.mode,
owner: data.owner ?? node.owner,
group: data.group ?? node.group,
ctime: Date.now(),
};
this._putNode(inode, newNode);
});
}
public readContent(uuid: string): Promise<Uint8Array> {
return this._dataStore.read(uuid);
}
public streamContent(uuid: string): ReadableStream {
return this._dataStore.stream(uuid);
}
public async writeContent(uuid: string, blob: Blob): Promise<void> {
this._isWritable(); this._isWritable();
await this._dataStore.put(uuid, blob);
const node = this.getNode(inode)!;
const newNode: Node = {
...node,
mode: data.mode ?? node.mode,
owner: data.owner ?? node.owner,
group: data.group ?? node.group,
ctime: Date.now(),
};
this._putNode(inode, newNode);
} }
public readContent(uuid: string): Promise<Uint8Array> {} public async deleteContent(uuid: string): Promise<void> {
this._isWritable();
public streamContent(uuid: string): Stream {} await this._dataStore.delete(uuid);
public writeContent(uuid: string, blob: Blob): Promise<void> {
this.isWritable();
} }
public deleteContent(uuid: string): Promise<void> { public hasContent(uuid: string): Promise<boolean> {
this.isWritable(); return this._dataStore.has(uuid);
} }
public hasContent(uuid: string): Promise<boolean> {} public async loadOverlay(): Promise<void> {
const overlay = await this._metaStore.loadAll();
public loadOverlay(): Promise<void> {} this._overlay = new Map(overlay);
public createOverlayEntry(inode: number, node: Node | null): Promise<void> {
this.isWritable();
} }
// public sweepOrphans(): Promise<void> {}
// public sweepDangling(): Promise<void> {}
public invalidateRIndex(): void { public invalidateRIndex(): void {
this._rIndexInvalid = true; this._rIndexInvalid = true;
} }
public getIndex(): ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>> {} public getIndex(): ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>> {
if (this._rIndexInvalid) {
// public sweepOrphans(): Promise<void> {} this._rIndex = this._buildRIndex();
// public sweepDangling(): Promise<void> {} this._rIndexInvalid = false;
}
return this._rIndex;
}
public get ID(): number { public get ID(): number {
return this._id; return this._id;
+42 -22
View File
@@ -1,30 +1,50 @@
import type { IDataStore, NodeHash, ShadowHash, VDisk } from "@core/filesystem/vdisk"; import { IDataStore, IMetaStore, VDisk, VDiskConfig } from "@core/filesystem/vdisk";
//export interface VDiskManagerConfig {}
export class VDiskManager { export class VDiskManager {
private _VDiskHash: Map<number, VDisk> = new Map(); private _disks: Map<number, VDisk>;
private _isMounted: (id: number) => boolean;
public getVDisk(id: number): VDisk | undefined { constructor(isMounted: (id: number) => boolean) {
const result = this._VDiskHash.get(id); this._disks = new Map();
if (result) return result; this._isMounted = isMounted;
return undefined;
} }
public registerVDisk( public async registerDisk(config: VDiskConfig, data: IDataStore, meta: IMetaStore): Promise<VDisk> {
store: IDataStore, if (this._disks.has(config.id)) {
base: NodeHash = new Map(), throw new Error("Disk of this id already exists");
overlay: ShadowHash = new Map(), }
id: number = 0,
): number {
const vdisk: VDisk = {
id,
store,
base,
overlay,
};
this._VDiskHash.set(id, vdisk); const vd = await VDisk.open(config, data, meta);
return id; this._disks.set(config.id, vd);
return vd;
}
public getDisk(id: number): VDisk | undefined {
const vd = this._disks.get(id);
return vd;
}
public hasDisk(id: number): boolean {
return this._disks.get(id) ? true : false;
}
public listDisk(): VDisk[] {
return Array.from(this._disks.values());
}
public listID(): number[] {
return Array.from(this._disks.keys());
}
public unregisterDisk(id: number): void {
if (!this._disks.has(id)) {
throw new Error("Disk of this id doesnt exist");
}
if (this._isMounted(id)) {
throw new Error("Could not unregister disk. Is mounted.");
}
this._disks.delete(id);
} }
} }