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:
@@ -29,6 +29,7 @@ pub fn (mut db OurDB) set(args OurDBSetArgs) !u32 {
|
||||
// if id points to an empty location, return an error
|
||||
// else, overwrite data
|
||||
if id := args.id {
|
||||
if id != 0 {
|
||||
// this is an update
|
||||
location := db.lookup.get(id)!
|
||||
if location.position == 0 {
|
||||
@@ -38,6 +39,7 @@ pub fn (mut db OurDB) set(args OurDBSetArgs) !u32 {
|
||||
db.set_(id, location, args.data)!
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
// this is an insert
|
||||
id := db.lookup.get_next_id()!
|
||||
|
||||
@@ -26,7 +26,9 @@ pub fn (mut fs OurDBFS) get_root() !&Directory {
|
||||
}
|
||||
// Save new root to DB
|
||||
mut myroot := Directory{
|
||||
metadata: Metadata{}
|
||||
metadata: Metadata{
|
||||
file_type: .directory
|
||||
}
|
||||
parent_id: 0
|
||||
myvfs: &fs
|
||||
}
|
||||
|
||||
@@ -66,8 +66,10 @@ pub fn (mut self OurDBVFS) file_delete(path string) ! {
|
||||
}
|
||||
|
||||
pub fn (mut self OurDBVFS) dir_create(path string) !vfscore.FSEntry {
|
||||
println('Debug: Creating directory ${path}')
|
||||
parent_path := os.dir(path)
|
||||
dir_name := os.base(path)
|
||||
println('Debug: Creating directory ${dir_name} in ${parent_path}')
|
||||
|
||||
mut parent_dir := self.get_directory(parent_path)!
|
||||
mut new_dir := parent_dir.mkdir(dir_name)!
|
||||
@@ -157,15 +159,18 @@ pub fn (mut self OurDBVFS) destroy() ! {
|
||||
// Helper functions
|
||||
fn (mut self OurDBVFS) get_entry(path string) !ourdb_fs.FSEntry {
|
||||
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('/')
|
||||
println('parts: ${parts}')
|
||||
println('current: ${current}')
|
||||
|
||||
for i := 0; i < parts.len; i++ {
|
||||
mut found := false
|
||||
mut children := current.children(false)!
|
||||
println('children: ${children}')
|
||||
|
||||
for mut child in children {
|
||||
if child.metadata.name == parts[i] {
|
||||
@@ -174,12 +179,13 @@ fn (mut self OurDBVFS) get_entry(path string) !ourdb_fs.FSEntry {
|
||||
unsafe {
|
||||
current = child
|
||||
}
|
||||
println('Debug: current: ${current}')
|
||||
found = true
|
||||
break
|
||||
}
|
||||
else {
|
||||
if i == parts.len - 1 {
|
||||
return child // `child` is already a value, so return it directly
|
||||
return child
|
||||
} else {
|
||||
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 {
|
||||
|
||||
@@ -26,7 +26,7 @@ fn test_vfsourdb() ! {
|
||||
assert root.get_metadata().name == ''
|
||||
|
||||
// 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().file_type == .directory
|
||||
|
||||
|
||||
Reference in New Issue
Block a user