@@ -60,9 +60,12 @@ export class SqliteMetadataStore implements FsMetadataStore, FsMetadataStoreVers
6060 private stmtGetSymlink : BetterSqlite3 . Statement ;
6161 private stmtGetChunkKey : BetterSqlite3 . Statement ;
6262 private stmtSetChunkKey : BetterSqlite3 . Statement ;
63+ private stmtLookupFull : BetterSqlite3 . Statement ;
6364 private stmtGetAllChunkKeys : BetterSqlite3 . Statement ;
6465 private stmtDeleteAllChunks : BetterSqlite3 . Statement ;
6566 private stmtDeleteChunksFrom : BetterSqlite3 . Statement ;
67+ private stmtDeleteChunksFromDel : BetterSqlite3 . Statement ;
68+ private stmtRenameDentry : BetterSqlite3 . Statement ;
6669
6770 // Versioning prepared statements (only initialized if versioning is enabled).
6871 private stmtCreateVersion ! : BetterSqlite3 . Statement ;
@@ -97,6 +100,9 @@ export class SqliteMetadataStore implements FsMetadataStore, FsMetadataStoreVers
97100 this . stmtLookup = this . db . prepare (
98101 "SELECT child_ino FROM dentries WHERE parent_ino = ? AND name = ?" ,
99102 ) ;
103+ this . stmtLookupFull = this . db . prepare (
104+ "SELECT child_ino, child_type FROM dentries WHERE parent_ino = ? AND name = ?" ,
105+ ) ;
100106 this . stmtCreateDentry = this . db . prepare (
101107 "INSERT INTO dentries (parent_ino, name, child_ino, child_type) VALUES (?, ?, ?, ?)" ,
102108 ) ;
@@ -133,6 +139,12 @@ export class SqliteMetadataStore implements FsMetadataStore, FsMetadataStoreVers
133139 this . stmtDeleteChunksFrom = this . db . prepare (
134140 "SELECT block_key FROM chunks WHERE ino = ? AND chunk_index >= ?" ,
135141 ) ;
142+ this . stmtDeleteChunksFromDel = this . db . prepare (
143+ "DELETE FROM chunks WHERE ino = ? AND chunk_index >= ?" ,
144+ ) ;
145+ this . stmtRenameDentry = this . db . prepare (
146+ "UPDATE dentries SET parent_ino = ?, name = ? WHERE parent_ino = ? AND name = ?" ,
147+ ) ;
136148
137149 // Versioning prepared statements.
138150 if ( this . versioningEnabled ) {
@@ -434,33 +446,11 @@ export class SqliteMetadataStore implements FsMetadataStore, FsMetadataStoreVers
434446 dstParentIno : number ,
435447 dstName : string ,
436448 ) : Promise < void > {
437- // Look up the source entry.
438- const srcRow = this . stmtLookup . get ( srcParentIno , srcName ) as
439- | { child_ino : number }
440- | undefined ;
441- if ( ! srcRow ) return ;
442-
443- // Get the child type from the source dentry.
444- const srcDentry = this . db
445- . prepare (
446- "SELECT child_type FROM dentries WHERE parent_ino = ? AND name = ?" ,
447- )
448- . get ( srcParentIno , srcName ) as { child_type : string } | undefined ;
449- if ( ! srcDentry ) return ;
450-
451449 // Remove destination if it exists.
452450 this . stmtRemoveDentry . run ( dstParentIno , dstName ) ;
453451
454- // Remove source entry.
455- this . stmtRemoveDentry . run ( srcParentIno , srcName ) ;
456-
457- // Create destination entry.
458- this . stmtCreateDentry . run (
459- dstParentIno ,
460- dstName ,
461- srcRow . child_ino ,
462- srcDentry . child_type ,
463- ) ;
452+ // Move source to destination in a single UPDATE (no lookup needed).
453+ this . stmtRenameDentry . run ( dstParentIno , dstName , srcParentIno , srcName ) ;
464454 }
465455
466456 // -- Path resolution --
@@ -612,9 +602,7 @@ export class SqliteMetadataStore implements FsMetadataStore, FsMetadataStoreVers
612602 block_key : string ;
613603 } > ;
614604 const keys = rows . map ( ( row ) => row . block_key ) ;
615- this . db
616- . prepare ( "DELETE FROM chunks WHERE ino = ? AND chunk_index >= ?" )
617- . run ( ino , startIndex ) ;
605+ this . stmtDeleteChunksFromDel . run ( ino , startIndex ) ;
618606 return keys ;
619607 }
620608
0 commit comments