feat: Improve OurDBFS and OurDBVFS functionalities

- Handle updates correctly in OurDB `set` function, preventing errors
  when incremental mode is enabled.
- Ensure directories are correctly created with metadata in OurDBFS.
- Add debug print statements to OurDBVFS for improved debugging.
- Simplify OurDBVFS `get_entry` function for better readability and
  correctness.  Fixes potential issues with returning references.
- Update tests to reflect changes and use a temporary directory
  to avoid conflicts.
This commit is contained in:
Mahmoud Emad
2025-02-17 15:37:58 +00:00
parent 73c3c3bdb5
commit 66f29fcb02
4 changed files with 23 additions and 13 deletions

View File

@@ -29,14 +29,16 @@ pub fn (mut db OurDB) set(args OurDBSetArgs) !u32 {
// if id points to an empty location, return an error // if id points to an empty location, return an error
// else, overwrite data // else, overwrite data
if id := args.id { if id := args.id {
// this is an update if id != 0 {
location := db.lookup.get(id)! // this is an update
if location.position == 0 { location := db.lookup.get(id)!
return error('cannot set id for insertions when incremental mode is enabled') if location.position == 0 {
} return error('cannot set id for insertions when incremental mode is enabled')
}
db.set_(id, location, args.data)! db.set_(id, location, args.data)!
return id return id
}
} }
// this is an insert // this is an insert

View File

@@ -26,7 +26,9 @@ pub fn (mut fs OurDBFS) get_root() !&Directory {
} }
// Save new root to DB // Save new root to DB
mut myroot := Directory{ mut myroot := Directory{
metadata: Metadata{} metadata: Metadata{
file_type: .directory
}
parent_id: 0 parent_id: 0
myvfs: &fs myvfs: &fs
} }

View File

@@ -66,8 +66,10 @@ pub fn (mut self OurDBVFS) file_delete(path string) ! {
} }
pub fn (mut self OurDBVFS) dir_create(path string) !vfscore.FSEntry { pub fn (mut self OurDBVFS) dir_create(path string) !vfscore.FSEntry {
println('Debug: Creating directory ${path}')
parent_path := os.dir(path) parent_path := os.dir(path)
dir_name := os.base(path) dir_name := os.base(path)
println('Debug: Creating directory ${dir_name} in ${parent_path}')
mut parent_dir := self.get_directory(parent_path)! mut parent_dir := self.get_directory(parent_path)!
mut new_dir := parent_dir.mkdir(dir_name)! mut new_dir := parent_dir.mkdir(dir_name)!
@@ -157,15 +159,18 @@ pub fn (mut self OurDBVFS) destroy() ! {
// Helper functions // Helper functions
fn (mut self OurDBVFS) get_entry(path string) !ourdb_fs.FSEntry { fn (mut self OurDBVFS) get_entry(path string) !ourdb_fs.FSEntry {
if path == '/' { if path == '/' {
return *self.core.get_root()! // Dereference to return a value return *self.core.get_root()!
} }
mut current := self.core.get_root()! // This is already a reference (&Directory) mut current := self.core.get_root()!
parts := path.trim_left('/').split('/') parts := path.trim_left('/').split('/')
println('parts: ${parts}')
println('current: ${current}')
for i := 0; i < parts.len; i++ { for i := 0; i < parts.len; i++ {
mut found := false mut found := false
mut children := current.children(false)! mut children := current.children(false)!
println('children: ${children}')
for mut child in children { for mut child in children {
if child.metadata.name == parts[i] { if child.metadata.name == parts[i] {
@@ -174,12 +179,13 @@ fn (mut self OurDBVFS) get_entry(path string) !ourdb_fs.FSEntry {
unsafe { unsafe {
current = child current = child
} }
println('Debug: current: ${current}')
found = true found = true
break break
} }
else { else {
if i == parts.len - 1 { if i == parts.len - 1 {
return child // `child` is already a value, so return it directly return child
} else { } else {
return error('Not a directory: ${parts[i]}') return error('Not a directory: ${parts[i]}')
} }
@@ -193,7 +199,7 @@ fn (mut self OurDBVFS) get_entry(path string) !ourdb_fs.FSEntry {
} }
} }
return *current // Dereference to return a value return *current
} }
fn (mut self OurDBVFS) get_directory(path string) !&ourdb_fs.Directory { fn (mut self OurDBVFS) get_directory(path string) !&ourdb_fs.Directory {

View File

@@ -26,7 +26,7 @@ fn test_vfsourdb() ! {
assert root.get_metadata().name == '' assert root.get_metadata().name == ''
// Test directory creation // Test directory creation
mut test_dir := vfs.dir_create('/test_dir')! mut test_dir := vfs.dir_create('/tmp/test_dir')!
assert test_dir.get_metadata().name == 'test_dir' assert test_dir.get_metadata().name == 'test_dir'
assert test_dir.get_metadata().file_type == .directory assert test_dir.get_metadata().file_type == .directory