- 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
+120 -33
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,9 +135,9 @@ 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> {
return this._batch(() => {
this._isWritable(); this._isWritable();
const index = this._iIndex++; const index = this._iIndex++;
const time = Date.now(); const time = Date.now();
const link_count = data.type === NodeType.Directory ? 2 : 1; const link_count = data.type === NodeType.Directory ? 2 : 1;
@@ -120,9 +185,11 @@ export class VDisk {
this.invalidateRIndex(); this.invalidateRIndex();
return index; return index;
});
} }
public linkNodeEntry(pinode: number, name: string, tinode: number): void { public linkNodeEntry(pinode: number, name: string, tinode: number): Promise<void> {
return this._batch(() => {
this._isWritable(); this._isWritable();
const pnode = this.getNode(pinode) as DirectoryNode; const pnode = this.getNode(pinode) as DirectoryNode;
@@ -138,9 +205,11 @@ export class VDisk {
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> {
return this._batch(() => {
this._isWritable(); this._isWritable();
const pnode = this.getNode(inode) as DirectoryNode; const pnode = this.getNode(inode) as DirectoryNode;
@@ -158,9 +227,11 @@ export class VDisk {
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> {
return this._batch(() => {
this._isWritable(); this._isWritable();
const pnode = this.getNode(pinode) as DirectoryNode; const pnode = this.getNode(pinode) as DirectoryNode;
@@ -213,9 +284,11 @@ export class VDisk {
this._putNode(tinode, target); this._putNode(tinode, target);
this._putNode(dinode, destination); this._putNode(dinode, destination);
this.invalidateRIndex(); 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(); this._isWritable();
const node = this.getNode(inode)!; const node = this.getNode(inode)!;
@@ -229,36 +302,50 @@ export class VDisk {
}; };
this._putNode(inode, newNode); this._putNode(inode, newNode);
});
} }
public readContent(uuid: string): Promise<Uint8Array> {} public readContent(uuid: string): Promise<Uint8Array> {
return this._dataStore.read(uuid);
public streamContent(uuid: string): Stream {}
public writeContent(uuid: string, blob: Blob): Promise<void> {
this.isWritable();
} }
public deleteContent(uuid: string): Promise<void> { public streamContent(uuid: string): ReadableStream {
this.isWritable(); return this._dataStore.stream(uuid);
} }
public hasContent(uuid: string): Promise<boolean> {} public async writeContent(uuid: string, blob: Blob): Promise<void> {
this._isWritable();
public loadOverlay(): Promise<void> {} await this._dataStore.put(uuid, blob);
public createOverlayEntry(inode: number, node: Node | null): Promise<void> {
this.isWritable();
} }
public async deleteContent(uuid: string): Promise<void> {
this._isWritable();
await this._dataStore.delete(uuid);
}
public hasContent(uuid: string): Promise<boolean> {
return this._dataStore.has(uuid);
}
public async loadOverlay(): Promise<void> {
const overlay = await this._metaStore.loadAll();
this._overlay = new Map(overlay);
}
// 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);
} }
} }