From 933b1fc5e8f5c4da159acd386bf99643abcbb756 Mon Sep 17 00:00:00 2001 From: orion Date: Sat, 19 Sep 2026 06:27:12 +0200 Subject: [PATCH] - vdisk done? - vdiskman done? --- src/core/filesystem/vdisk.ts | 387 ++++++++++++++++++++------------- src/core/util/vdisk_manager.ts | 64 ++++-- 2 files changed, 279 insertions(+), 172 deletions(-) diff --git a/src/core/filesystem/vdisk.ts b/src/core/filesystem/vdisk.ts index 6972c31..a26f5c1 100644 --- a/src/core/filesystem/vdisk.ts +++ b/src/core/filesystem/vdisk.ts @@ -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"; export interface VDiskConfig { id: number; - label: number; + label: string; index: number; + readonly: boolean; + size: number; base: NodeHash; dataC: DataConfig; - //metaC: MetaConfig; } export interface DataConfig { @@ -15,11 +17,9 @@ export interface DataConfig { cacheTTL: number; } -//export interface MetaConfig {} - export interface IDataStore { read(uuid: string): Promise; - stream(uuid: string): null; // Stream + stream(uuid: string): ReadableStream; put(uuid: string, blob: Blob): Promise; delete(uuid: string): Promise; has(uuid: string): Promise; @@ -28,8 +28,8 @@ export interface IDataStore { export interface IMetaStore { loadAll(): Promise>; - write(inode: number, node: Node): Promise; - delete(inode: number): Promise; + write(entries: Array<[number, Node | null]>): Promise; + delete(entries: Array): Promise; } export class VDisk { @@ -39,23 +39,88 @@ export class VDisk { private readonly _totalSize: number; private readonly _base: NodeHash; private readonly _dataStore: IDataStore; + private readonly _metaStore: IMetaStore; private _iIndex: number; private _rIndex: ReadonlyMap>; private _overlay: Map; - private _metaStore: IMetaStore; + private _pending: Map | null; private _rIndexInvalid: boolean; + private _flush: Promise; + + 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(fn: () => T): Promise { + 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 { if (this._readonly) throw Error(`The ${this._label} device under id - ${this._id} is marked readonly`); } 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 { + const vdisk = new VDisk(config, data, meta); + await vdisk.loadOverlay(); + return vdisk; } 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); if (node) return node; 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.Directory; mode: number; owner: number; group: number } | { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string }, - ): number { - this._isWritable(); + ): Promise { + 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 time = Date.now(); - const link_count = data.type === NodeType.Directory ? 2 : 1; + const base = { + inode: index, + link_count, + type: data.type, + mode: data.mode, + owner: data.owner, + group: data.group, + mtime: time, + ctime: time, + atime: time, + }; - const base = { - 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; - 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; - 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; - } + const children = new Map(pnode.children); + children.set(name, index); - const children = new Map(pnode.children); - children.set(name, index); + const parent = { + ...pnode, + children, + link_count: pnode.link_count + (data.type === NodeType.Directory ? 1 : 0), + mtime: time, + ctime: time, + }; - const parent = { - ...pnode, - children, - link_count: pnode.link_count + (data.type === NodeType.Directory ? 1 : 0), - mtime: time, - ctime: time, - }; + this._putNode(index, node); + this._putNode(pinode, parent); + this.invalidateRIndex(); - this._putNode(index, node); - this._putNode(pinode, parent); - this.invalidateRIndex(); - - return index; + return index; + }); } - public linkNodeEntry(pinode: number, name: string, tinode: number): void { - this._isWritable(); + public linkNodeEntry(pinode: number, name: string, tinode: number): Promise { + return this._batch(() => { + this._isWritable(); - const pnode = this.getNode(pinode) as DirectoryNode; - const tnode = this.getNode(tinode) as Node; - const time = Date.now(); + const pnode = this.getNode(pinode) as DirectoryNode; + const tnode = this.getNode(tinode) as Node; + const time = Date.now(); - const children = new Map(pnode.children); - children.set(name, tnode.inode); + const children = new Map(pnode.children); + children.set(name, tnode.inode); - const parent = { ...pnode, children, mtime: time, ctime: time }; - const target = { ...tnode, link_count: tnode.link_count + 1, ctime: time }; + const parent = { ...pnode, children, mtime: time, ctime: time }; + const target = { ...tnode, link_count: tnode.link_count + 1, ctime: time }; - this._putNode(tinode, target); - this._putNode(pinode, parent); - this.invalidateRIndex(); + this._putNode(tinode, target); + this._putNode(pinode, parent); + this.invalidateRIndex(); + }); } - public unlinkNodeEntry(inode: number, name: string): void { - this._isWritable(); + public unlinkNodeEntry(inode: number, name: string): Promise { + return this._batch(() => { + this._isWritable(); - const pnode = this.getNode(inode) as DirectoryNode; - const tinode = pnode.children.get(name)!; - const tnode = this.getNode(tinode) as Node; - const time = Date.now(); + const pnode = this.getNode(inode) as DirectoryNode; + const tinode = pnode.children.get(name)!; + const tnode = this.getNode(tinode) as Node; + const time = Date.now(); - const children = new Map(pnode.children); - children.delete(name); - const parent = { ...pnode, children, mtime: time, ctime: time }; + const children = new Map(pnode.children); + children.delete(name); + const parent = { ...pnode, children, mtime: time, ctime: time }; - const link_count = tnode.link_count - 1; - const wasLast = link_count === 0; + const link_count = tnode.link_count - 1; + const wasLast = link_count === 0; - this._putNode(inode, parent); - this._putNode(tinode, wasLast ? null : { ...tnode, link_count, ctime: Date.now() }); - this.invalidateRIndex(); + this._putNode(inode, parent); + this._putNode(tinode, wasLast ? null : { ...tnode, link_count, ctime: Date.now() }); + this.invalidateRIndex(); + }); } - public moveNodeEntry(pinode: number, dinode: number, tname: string, dname?: string): void { - this._isWritable(); + public moveNodeEntry(pinode: number, dinode: number, tname: string, dname?: string): Promise { + return this._batch(() => { + this._isWritable(); - const pnode = this.getNode(pinode) as DirectoryNode; - const dnode = this.getNode(dinode) as DirectoryNode; + const pnode = this.getNode(pinode) as DirectoryNode; + const dnode = this.getNode(dinode) as DirectoryNode; - const tinode = pnode.children.get(tname)!; - const tnode = this.getNode(tinode)!; + const tinode = pnode.children.get(tname)!; + const tnode = this.getNode(tinode)!; - const name = dname ?? tname; - const isDir = tnode.type === NodeType.Directory; - const time = Date.now(); + const name = dname ?? tname; + const isDir = tnode.type === NodeType.Directory; + 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); 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 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(); - 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 { + 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 { + return this._dataStore.read(uuid); + } + + public streamContent(uuid: string): ReadableStream { + return this._dataStore.stream(uuid); + } + + public async writeContent(uuid: string, blob: Blob): Promise { 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); + await this._dataStore.put(uuid, blob); } - public readContent(uuid: string): Promise {} - - public streamContent(uuid: string): Stream {} - - public writeContent(uuid: string, blob: Blob): Promise { - this.isWritable(); + public async deleteContent(uuid: string): Promise { + this._isWritable(); + await this._dataStore.delete(uuid); } - public deleteContent(uuid: string): Promise { - this.isWritable(); + public hasContent(uuid: string): Promise { + return this._dataStore.has(uuid); } - public hasContent(uuid: string): Promise {} - - public loadOverlay(): Promise {} - - public createOverlayEntry(inode: number, node: Node | null): Promise { - this.isWritable(); + public async loadOverlay(): Promise { + const overlay = await this._metaStore.loadAll(); + this._overlay = new Map(overlay); } + // public sweepOrphans(): Promise {} + // public sweepDangling(): Promise {} + public invalidateRIndex(): void { this._rIndexInvalid = true; } - public getIndex(): ReadonlyMap> {} - - // public sweepOrphans(): Promise {} - // public sweepDangling(): Promise {} + public getIndex(): ReadonlyMap> { + if (this._rIndexInvalid) { + this._rIndex = this._buildRIndex(); + this._rIndexInvalid = false; + } + return this._rIndex; + } public get ID(): number { return this._id; diff --git a/src/core/util/vdisk_manager.ts b/src/core/util/vdisk_manager.ts index fe07eb3..3b583ac 100644 --- a/src/core/util/vdisk_manager.ts +++ b/src/core/util/vdisk_manager.ts @@ -1,30 +1,50 @@ -import type { IDataStore, NodeHash, ShadowHash, VDisk } from "@core/filesystem/vdisk"; - -//export interface VDiskManagerConfig {} +import { IDataStore, IMetaStore, VDisk, VDiskConfig } from "@core/filesystem/vdisk"; export class VDiskManager { - private _VDiskHash: Map = new Map(); + private _disks: Map; + private _isMounted: (id: number) => boolean; - public getVDisk(id: number): VDisk | undefined { - const result = this._VDiskHash.get(id); - if (result) return result; - return undefined; + constructor(isMounted: (id: number) => boolean) { + this._disks = new Map(); + this._isMounted = isMounted; } - public registerVDisk( - store: IDataStore, - base: NodeHash = new Map(), - overlay: ShadowHash = new Map(), - id: number = 0, - ): number { - const vdisk: VDisk = { - id, - store, - base, - overlay, - }; + public async registerDisk(config: VDiskConfig, data: IDataStore, meta: IMetaStore): Promise { + if (this._disks.has(config.id)) { + throw new Error("Disk of this id already exists"); + } - this._VDiskHash.set(id, vdisk); - return id; + const vd = await VDisk.open(config, data, meta); + 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); } }