Try initializing FileSystem now
- vdisk finished - vdiskmanager finished - devices can properly initialize and hold aswell as retireve data - added mock synchronous store classes for testing
This commit is contained in:
@@ -10,15 +10,8 @@ import type {
|
||||
NodeNotFoundError,
|
||||
NotADirectoryError,
|
||||
} from "./errors";
|
||||
import {
|
||||
type DirectoryNode,
|
||||
type FileNode,
|
||||
type NodeEntry,
|
||||
type NodeHash,
|
||||
type NodePointer,
|
||||
type ShadowHash,
|
||||
} from "./node";
|
||||
import type { IDataStore } from "./vdisk";
|
||||
import { NodeType, type DirectoryNode, type FileNode, type Node, type NodePointer } from "./node";
|
||||
import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk";
|
||||
|
||||
export interface FileSystemConfig {
|
||||
maxPathLength: number;
|
||||
@@ -33,24 +26,23 @@ export class FileSystem {
|
||||
|
||||
private _SysPartitionID: number = 0;
|
||||
private _Mounts: Map<NodePointer, { tinode: number; tdevice_id: number }> = new Map();
|
||||
private _VDiskManager: VDiskManager = new VDiskManager();
|
||||
private _EntryCache: Map<NodePointer, NodeEntry>;
|
||||
private _VDiskManager: VDiskManager;
|
||||
|
||||
private constructor(store: IDataStore[], base: NodeHash[], overlay: ShadowHash[], id: number[]) {
|
||||
for (let i = 0; i < id.length; i++) {
|
||||
if (i === 0) {
|
||||
this._VDiskManager.registerVDisk(store[0]!, base[0], overlay[0], id[0]);
|
||||
this._Mounts.set(0, 0);
|
||||
}
|
||||
this._VDiskManager.registerVDisk(store[i]!, base[i], overlay[i], id[i]);
|
||||
private constructor(
|
||||
config: FileSystemConfig,
|
||||
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
|
||||
) {
|
||||
this._maxNameLength = config.maxNameLength;
|
||||
this._maxPathLength = config.maxPathLength;
|
||||
this._maxSymlinkDepth = config.maxSymlinkDepth;
|
||||
|
||||
this._VDiskManager = new VDiskManager();
|
||||
for (const dev of devices) {
|
||||
this._VDiskManager.registerDisk(dev.config, dev.dstore, dev.mstore);
|
||||
}
|
||||
|
||||
this._EntryCache = this.buildNodeEntryCache();
|
||||
}
|
||||
|
||||
private updateDentryCache(key: NodePointer, entry: NodeEntry): void {}
|
||||
|
||||
private resolveNode(ptr: NodePointer): TreeNode | undefined {
|
||||
private resolveNode(ptr: NodePointer): Node | undefined {
|
||||
const dev = this._VDiskManager.getVDisk(ptr[1]);
|
||||
if (!dev) return undefined;
|
||||
|
||||
@@ -61,24 +53,27 @@ export class FileSystem {
|
||||
return dev.base.get(ptr[0]);
|
||||
}
|
||||
|
||||
public static init(store: IDataStore[], base: NodeHash[], overlay: ShadowHash[], index: number[]): FileSystem {
|
||||
if (store.length === 0 || base.length === 0 || index.length === 0) {
|
||||
public static init(
|
||||
config: FileSystemConfig,
|
||||
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
|
||||
): FileSystem {
|
||||
if (devices.length === 0) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
message: "File System tried to initialized without a base hashmap and store",
|
||||
message: "File System tried to initialized without any devices",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemInitializationError;
|
||||
}
|
||||
|
||||
if (base[0]!.get(0) === undefined) {
|
||||
if (!devices.find(d => d.config.id === 0)) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
message: "File System failed to initialize because root node is undefined or not assigned to inode 0",
|
||||
message: "File System failed to initialize because no device with id 0 was present",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemInitializationError;
|
||||
}
|
||||
|
||||
if (base[0]!.get(0)?.type !== TreeNodeType.Directory) {
|
||||
if (devices[0]!.config.base.get(0)?.type !== NodeType.Directory) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
message: "File System failed to initialize because root node is not a directory",
|
||||
@@ -86,7 +81,7 @@ export class FileSystem {
|
||||
} satisfies FileSystemInitializationError;
|
||||
}
|
||||
|
||||
return new FileSystem(store, base, overlay, index);
|
||||
return new FileSystem(config, devices);
|
||||
}
|
||||
|
||||
public getRoot(): DirectoryNode {
|
||||
|
||||
@@ -6,9 +6,9 @@ export type ShadowHash = ReadonlyMap<number, Node | null>;
|
||||
export type NodePointer = string;
|
||||
|
||||
export interface NodeEntry {
|
||||
pinode: number;
|
||||
pdevice_id: number;
|
||||
name: string;
|
||||
readonly pinode: number;
|
||||
readonly pdevice_id: number;
|
||||
readonly name: string;
|
||||
}
|
||||
|
||||
export interface FileNode {
|
||||
@@ -56,3 +56,18 @@ export enum NodeType {
|
||||
Directory = 1,
|
||||
Symlink = 2,
|
||||
}
|
||||
|
||||
export function toPtr(deviceId: number, inode: number): NodePointer {
|
||||
return `${deviceId}:${inode}`;
|
||||
}
|
||||
|
||||
export function fromPtr(ptr: NodePointer): { device_id: number; inode: number } {
|
||||
if (!ptr.match(/\d+[:]\d+$/g)) {
|
||||
throw new Error("Invalid node pointer format");
|
||||
}
|
||||
const arr = ptr.split(":");
|
||||
return {
|
||||
device_id: Number(arr[0]),
|
||||
inode: Number(arr[1]),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,44 @@
|
||||
import type { ReadableStream } from "@core/util/stream";
|
||||
import { NodeType, type DirectoryNode, type Node, type NodeEntry, type NodeHash, type NodePointer } from "./node";
|
||||
import {
|
||||
NodeType,
|
||||
toPtr,
|
||||
type DirectoryNode,
|
||||
type Node,
|
||||
type NodeEntry,
|
||||
type NodeHash,
|
||||
type NodePointer,
|
||||
} from "./node";
|
||||
|
||||
export function DefaultVDiskConfig(id: number): VDiskConfig {
|
||||
const base = new Map();
|
||||
const root = {
|
||||
type: NodeType.Directory,
|
||||
inode: 0,
|
||||
link_count: 2,
|
||||
mode: 0o0555,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: 0,
|
||||
ctime: 0,
|
||||
atime: 0,
|
||||
children: new Map(),
|
||||
} satisfies DirectoryNode;
|
||||
|
||||
base.set(0, root);
|
||||
|
||||
return {
|
||||
id,
|
||||
label: `disk-${id}`,
|
||||
readonly: false,
|
||||
size: Infinity,
|
||||
base,
|
||||
index: 0,
|
||||
dataC: {
|
||||
chunkSize: 8096,
|
||||
cacheSize: 64 * 1024 * 1024,
|
||||
cacheTTL: 60_000,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export interface VDiskConfig {
|
||||
id: number;
|
||||
@@ -62,7 +101,7 @@ export class VDisk {
|
||||
|
||||
this._overlay = new Map();
|
||||
this._rIndex = new Map();
|
||||
this._rIndexInvalid = true;
|
||||
this._rIndexInvalid = false;
|
||||
this._pending = null;
|
||||
this._flush = Promise.resolve();
|
||||
}
|
||||
@@ -101,6 +140,41 @@ export class VDisk {
|
||||
if (this._readonly) throw Error(`The ${this._label} device under id - ${this._id} is marked readonly`);
|
||||
}
|
||||
|
||||
private _buildReverseIndex(): Map<NodePointer, NodeEntry[]> {
|
||||
const index: Map<NodePointer, NodeEntry[]> = new Map();
|
||||
const stack: [number, number, number, string][] = [];
|
||||
|
||||
const root = this.getNode(0) as DirectoryNode;
|
||||
for (const [name, inode] of root.children) {
|
||||
stack.push([inode, 0, this._id, name]);
|
||||
}
|
||||
|
||||
while (stack.length > 0) {
|
||||
const [inode, pinode, pdevice_id, name] = stack.pop()!;
|
||||
const node = this.getNode(inode);
|
||||
if (node === undefined) continue;
|
||||
|
||||
const key = toPtr(this._id, inode);
|
||||
const entry = {
|
||||
pinode,
|
||||
pdevice_id,
|
||||
name,
|
||||
};
|
||||
|
||||
const arr = index.get(key);
|
||||
if (arr) arr.push(entry);
|
||||
else index.set(key, [entry]);
|
||||
|
||||
if (node.type === NodeType.Directory) {
|
||||
for (const [name, cinode] of node.children) {
|
||||
stack.push([cinode, inode, pdevice_id, name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
private _putNode(inode: number, node: Node | null): void {
|
||||
if (!this._pending)
|
||||
throw new Error(
|
||||
@@ -113,6 +187,8 @@ export class VDisk {
|
||||
public static async open(config: VDiskConfig, data: IDataStore, meta: IMetaStore): Promise<VDisk> {
|
||||
const vdisk = new VDisk(config, data, meta);
|
||||
await vdisk.loadOverlay();
|
||||
vdisk._rIndex = vdisk._buildReverseIndex();
|
||||
|
||||
return vdisk;
|
||||
}
|
||||
|
||||
@@ -138,7 +214,7 @@ export class VDisk {
|
||||
): Promise<number> {
|
||||
return this._batch(() => {
|
||||
this._isWritable();
|
||||
const index = this._iIndex++;
|
||||
const index = ++this._iIndex;
|
||||
const time = Date.now();
|
||||
const link_count = data.type === NodeType.Directory ? 2 : 1;
|
||||
|
||||
@@ -341,7 +417,7 @@ export class VDisk {
|
||||
|
||||
public getIndex(): ReadonlyMap<NodePointer, ReadonlyArray<NodeEntry>> {
|
||||
if (this._rIndexInvalid) {
|
||||
this._rIndex = this._buildRIndex();
|
||||
this._rIndex = this._buildReverseIndex();
|
||||
this._rIndexInvalid = false;
|
||||
}
|
||||
return this._rIndex;
|
||||
|
||||
Reference in New Issue
Block a user