Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions extensions/puterfs/PuterFSProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const svc_acl = extension.import('service:acl');

// TODO: these services ought to be part of this extension
const svc_size = extension.import('service:sizeService');
const svc_fsEntryFetcher = extension.import('service:fsEntryFetcher');
const svc_resource = extension.import('service:resourceService');

// Not sure where these really belong yet
Expand Down Expand Up @@ -98,8 +97,9 @@ const {
} = extension.import('fs').util;

export default class PuterFSProvider {
constructor ({ fsEntryController }) {
constructor ({ fsEntryController, storageController }) {
this.fsEntryController = fsEntryController;
this.storageController = storageController;
this.name = 'puterfs';
}

Expand Down Expand Up @@ -219,25 +219,25 @@ export default class PuterFSProvider {
}) {
// shortcut: has full path
if ( selector?.path ) {
const entry = await svc_fsEntryFetcher.findByPath(selector.path);
const entry = await this.fsEntryController.findByPath(selector.path);
return Boolean(entry);
}

// shortcut: has uid
if ( selector?.uid ) {
const entry = await svc_fsEntryFetcher.findByUID(selector.uid);
const entry = await this.fsEntryController.findByUID(selector.uid);
return Boolean(entry);
}

// shortcut: parent uid + child name
if ( selector instanceof NodeChildSelector && selector.parent instanceof NodeUIDSelector ) {
return await svc_fsEntryFetcher.nameExistsUnderParent(selector.parent.uid,
return await this.fsEntryController.nameExistsUnderParent(selector.parent.uid,
selector.name);
}

// shortcut: parent id + child name
if ( selector instanceof NodeChildSelector && selector.parent instanceof NodeInternalIDSelector ) {
return await svc_fsEntryFetcher.nameExistsUnderParentID(selector.parent.id,
return await this.fsEntryController.nameExistsUnderParentID(selector.parent.id,
selector.name);
}

Expand Down Expand Up @@ -358,7 +358,7 @@ export default class PuterFSProvider {
node,
}) {
// For Puter FS nodes, we assume we will obtain all properties from
// fsEntryService/fsEntryFetcher, except for 'thumbnail' unless it's
// fsEntryController, except for 'thumbnail' unless it's
// explicitly requested.

if ( options.tracer == null ) {
Expand Down Expand Up @@ -409,7 +409,7 @@ export default class PuterFSProvider {
entry = await this.fsEntryController.get(maybe_uid, options);
controls.log.debug('got an entry from the future');
} else {
entry = await svc_fsEntryFetcher.find(selector, options);
entry = await this.fsEntryController.find(selector, options);
}

if ( ! entry ) {
Expand Down Expand Up @@ -944,7 +944,7 @@ export default class PuterFSProvider {
const state_upload = storage.create_upload();

try {
await state_upload.run({
await this.storageController.upload({
uid: uuid,
file,
storage_meta: { bucket, bucket_region },
Expand Down
239 changes: 237 additions & 2 deletions extensions/puterfs/fsentries/FSEntryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const { PuterPath } = extension.import('fs');
const { Context } = extension.import('core');

const {
RootNodeSelector,
NodeChildSelector,
NodeUIDSelector,
NodePathSelector,
NodeInternalIDSelector,
} = extension.import('core').fs.selectors;

export default class {
Expand All @@ -36,6 +40,37 @@ export default class {
this.entryListeners_ = {};

this.mkPromiseForQueueSize_();

// this list of properties is for read operations
// (originally in FSEntryFetcher)
this.defaultProperties = [
'id',
'associated_app_id',
'uuid',
'public_token',
'bucket',
'bucket_region',
'file_request_token',
'user_id',
'parent_uid',
'is_dir',
'is_public',
'is_shortcut',
'is_symlink',
'symlink_path',
'shortcut_to',
'sort_by',
'sort_order',
'immutable',
'name',
'metadata',
'modified',
'created',
'accessed',
'size',
'layout',
'path',
];
}

init () {
Expand Down Expand Up @@ -67,6 +102,7 @@ export default class {
});
}

// #region write operations
async insert (entry) {
const op = new Insert(entry);
await this.enqueue_(op);
Expand All @@ -84,7 +120,9 @@ export default class {
await this.enqueue_(op);
return op;
}
// #endregion

// #region read operations
async fast_get_descendants (uuid) {
return (await db.read(`
WITH RECURSIVE descendant_cte AS (
Expand Down Expand Up @@ -160,8 +198,7 @@ export default class {
op.apply(answer);
}
if ( answer.is_diff ) {
const fsEntryFetcher = Context.get('services').get('fsEntryFetcher');
const base_entry = await fsEntryFetcher.find(new NodeUIDSelector(uuid),
const base_entry = await this.find(new NodeUIDSelector(uuid),
fetch_entry_options);
answer.entry = { ...base_entry, ...answer.entry };
}
Expand Down Expand Up @@ -197,6 +234,203 @@ export default class {
return rows[0].total_size;
}

/**
* Finds a filesystem entry using the provided selector.
* @param {Object} selector - The selector object specifying how to find the entry
* @param {Object} fetch_entry_options - Options for fetching the entry
* @returns {Promise<Object|null>} The filesystem entry or null if not found
*/
async find (selector, fetch_entry_options) {
if ( selector instanceof RootNodeSelector ) {
return selector.entry;
}
if ( selector instanceof NodePathSelector ) {
return await this.findByPath(selector.value, fetch_entry_options);
}
if ( selector instanceof NodeUIDSelector ) {
return await this.findByUID(selector.value, fetch_entry_options);
}
if ( selector instanceof NodeInternalIDSelector ) {
return await this.findByID(selector.id, fetch_entry_options);
}
if ( selector instanceof NodeChildSelector ) {
let id;

if ( selector.parent instanceof RootNodeSelector ) {
id = await this.findNameInRoot(selector.name);
} else {
const parentEntry = await this.find(selector.parent);
if ( ! parentEntry ) return null;
id = await this.findNameInParent(parentEntry.uuid, selector.name);
}

if ( id === undefined ) return null;
if ( typeof id !== 'number' ) {
throw new Error('unexpected type for id value',
typeof id,
id);
}
return this.find(new NodeInternalIDSelector('mysql', id));
}
}

/**
* Finds a filesystem entry by its UUID.
* @param {string} uuid - The UUID of the entry to find
* @param {Object} fetch_entry_options - Options including thumbnail flag
* @returns {Promise<Object|undefined>} The filesystem entry or undefined if not found
*/
async findByUID (uuid, fetch_entry_options = {}) {
const { thumbnail } = fetch_entry_options;

let fsentry = await db.tryHardRead(`SELECT ${
this.defaultProperties.join(', ')
}${thumbnail ? ', thumbnail' : ''
} FROM fsentries WHERE uuid = ? LIMIT 1`,
[uuid]);

return fsentry[0];
}

/**
* Finds a filesystem entry by its internal database ID.
* @param {number} id - The internal ID of the entry to find
* @param {Object} fetch_entry_options - Options including thumbnail flag
* @returns {Promise<Object|undefined>} The filesystem entry or undefined if not found
*/
async findByID (id, fetch_entry_options = {}) {
const { thumbnail } = fetch_entry_options;

let fsentry = await db.tryHardRead(`SELECT ${
this.defaultProperties.join(', ')
}${thumbnail ? ', thumbnail' : ''
} FROM fsentries WHERE id = ? LIMIT 1`,
[id]);

return fsentry[0];
}

/**
* Finds a filesystem entry by its full path.
* @param {string} path - The full path of the entry to find
* @param {Object} fetch_entry_options - Options including thumbnail flag and tracer
* @returns {Promise<Object|false>} The filesystem entry or false if not found
*/
async findByPath (path, fetch_entry_options = {}) {
const { thumbnail } = fetch_entry_options;

if ( path === '/' ) {
return this.find(new RootNodeSelector());
}

const parts = path.split('/').filter(path => path !== '');
if ( parts.length === 0 ) {
// TODO: invalid path; this should be an error
return false;
}

// TODO: use a closure table for more efficient path resolving
let parent_uid = null;
let result;

const resultColsSql = this.defaultProperties.join(', ') +
(thumbnail ? ', thumbnail' : '');

result = await db.read(`SELECT ${ resultColsSql
} FROM fsentries WHERE path=? LIMIT 1`,
[path]);

// using knex instead

if ( result[0] ) return result[0];

const loop = async () => {
for ( let i = 0 ; i < parts.length ; i++ ) {
const part = parts[i];
const isLast = i == parts.length - 1;
const colsSql = isLast ? resultColsSql : 'uuid';
if ( parent_uid === null ) {
result = await db.read(`SELECT ${ colsSql
} FROM fsentries WHERE parent_uid IS NULL AND name=? LIMIT 1`,
[part]);
} else {
result = await db.read(`SELECT ${ colsSql
} FROM fsentries WHERE parent_uid=? AND name=? LIMIT 1`,
[parent_uid, part]);
}

if ( ! result[0] ) return false;
parent_uid = result[0].uuid;
}
};

if ( fetch_entry_options.tracer ) {
const tracer = fetch_entry_options.tracer;
const options = fetch_entry_options.trace_options;
await tracer.startActiveSpan('fs:sql:findByPath',
...(options ? [options] : []),
async span => {
await loop();
span.end();
});
} else {
await loop();
}

return result[0];
}

/**
* Finds the ID of a child entry with the given name in the root directory.
* @param {string} name - The name of the child entry to find
* @returns {Promise<number|undefined>} The ID of the child entry or undefined if not found
*/
async findNameInRoot (name) {
let child_id = await db.read('SELECT `id` FROM `fsentries` WHERE `parent_uid` IS NULL AND name = ? LIMIT 1',
[name]);
return child_id[0]?.id;
}

/**
* Finds the ID of a child entry with the given name under a specific parent.
* @param {string} parent_uid - The UUID of the parent directory
* @param {string} name - The name of the child entry to find
* @returns {Promise<number|undefined>} The ID of the child entry or undefined if not found
*/
async findNameInParent (parent_uid, name) {
let child_id = await db.read('SELECT `id` FROM `fsentries` WHERE `parent_uid` = ? AND name = ? LIMIT 1',
[parent_uid, name]);
return child_id[0]?.id;
}

/**
* Checks if an entry with the given name exists under a specific parent.
* @param {string} parent_uid - The UUID of the parent directory
* @param {string} name - The name to check for
* @returns {Promise<boolean>} True if the name exists under the parent, false otherwise
*/
async nameExistsUnderParent (parent_uid, name) {
let check_dupe = await db.read('SELECT `id` FROM `fsentries` WHERE `parent_uid` = ? AND name = ? LIMIT 1',
[parent_uid, name]);
return !!check_dupe[0];
}

/**
* Checks if an entry with the given name exists under a parent specified by ID.
* @param {number} parent_id - The internal ID of the parent directory
* @param {string} name - The name to check for
* @returns {Promise<boolean>} True if the name exists under the parent, false otherwise
*/
async nameExistsUnderParentID (parent_id, name) {
const parent = await this.findByID(parent_id);
if ( ! parent ) {
return false;
}
return this.nameExistsUnderParent(parent.uuid, name);
}
// #endregion

// #region queue logic
async enqueue_ (op) {
while (
this.currentState.queue.length > this.max_queue ||
Expand Down Expand Up @@ -296,4 +530,5 @@ export default class {
this.mkPromiseForQueueSize_();
queueSizeResolve();
}
// #endregion
}
Loading
Loading