vfs_basics working
This commit is contained in:
3
examples/vfs/vfs_db/.gitignore
vendored
3
examples/vfs/vfs_db/.gitignore
vendored
@@ -1 +1,2 @@
|
||||
example_db
|
||||
dedupestor_vfs
|
||||
ourdb_vfs
|
||||
@@ -23,6 +23,8 @@ pub fn (mut db VFSDedupeDB) delete(id u32) ! {
|
||||
example_data_dir := os.join_path(os.dir(@FILE), 'example_db')
|
||||
os.mkdir_all(example_data_dir)!
|
||||
|
||||
data_path := os.join_path(example_data_dir, 'data')
|
||||
|
||||
// Create separate databases for data and metadata
|
||||
mut db_data := VFSDedupeDB{
|
||||
DedupeStore: dedupestor.new(
|
||||
@@ -36,39 +38,85 @@ mut db_metadata := ourdb.new(
|
||||
)!
|
||||
|
||||
// Create VFS with separate databases for data and metadata
|
||||
mut vfs := vfs_db.new_with_separate_dbs(
|
||||
mut db_data,
|
||||
mut db_metadata,
|
||||
data_dir: os.join_path(example_data_dir, 'data'),
|
||||
metadata_dir: os.join_path(example_data_dir, 'metadata')
|
||||
)!
|
||||
mut vfs := vfs_db.new(mut db_data, mut db_metadata) or {
|
||||
panic('Failed to create VFS: ${err}')
|
||||
}
|
||||
|
||||
println('\n---------BEGIN EXAMPLE')
|
||||
println('---------WRITING FILES')
|
||||
vfs.file_create('some_file.txt')!
|
||||
vfs.file_create('another_file.txt')!
|
||||
vfs.file_create('/some_file.txt') or {
|
||||
panic('Failed to create file: ${err}')
|
||||
}
|
||||
vfs.file_create('/another_file.txt') or {
|
||||
panic('Failed to create file: ${err}')
|
||||
}
|
||||
|
||||
vfs.file_write('some_file.txt', 'gibberish'.bytes())!
|
||||
vfs.file_write('another_file.txt', 'abcdefg'.bytes())!
|
||||
vfs.file_write('/some_file.txt', 'gibberish'.bytes()) or {
|
||||
panic('Failed to write file: ${err}')
|
||||
}
|
||||
vfs.file_write('/another_file.txt', 'abcdefg'.bytes()) or {
|
||||
panic('Failed to write file: ${err}')
|
||||
}
|
||||
|
||||
println('\n---------READING FILES')
|
||||
println(vfs.file_read('some_file.txt')!.bytestr())
|
||||
println(vfs.file_read('another_file.txt')!.bytestr())
|
||||
some_file_content := vfs.file_read('/some_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(some_file_content.bytestr())
|
||||
|
||||
another_file_content := vfs.file_read('/another_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(another_file_content.bytestr())
|
||||
|
||||
println("\n---------WRITING DUPLICATE FILE (DB SIZE: ${os.file_size(os.join_path(example_data_dir, 'data/0.db'))})")
|
||||
vfs.file_create('duplicate.txt')!
|
||||
vfs.file_write('duplicate.txt', 'gibberish'.bytes())!
|
||||
vfs.file_create('/duplicate.txt') or {
|
||||
panic('Failed to create file: ${err}')
|
||||
}
|
||||
vfs.file_write('/duplicate.txt', 'gibberish'.bytes()) or {
|
||||
panic('Failed to write file: ${err}')
|
||||
}
|
||||
|
||||
println("\n---------WROTE DUPLICATE FILE (DB SIZE: ${os.file_size(os.join_path(example_data_dir, 'data/0.db'))})")
|
||||
println('---------READING FILES')
|
||||
println(vfs.file_read('some_file.txt')!.bytestr())
|
||||
println(vfs.file_read('another_file.txt')!.bytestr())
|
||||
println(vfs.file_read('duplicate.txt')!.bytestr())
|
||||
some_file_content3 := vfs.file_read('/some_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(some_file_content3.bytestr())
|
||||
|
||||
another_file_content3 := vfs.file_read('/another_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(another_file_content3.bytestr())
|
||||
|
||||
duplicate_content := vfs.file_read('/duplicate.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(duplicate_content.bytestr())
|
||||
|
||||
println("\n---------DELETING DUPLICATE FILE (DB SIZE: ${os.file_size(os.join_path(example_data_dir, 'data/0.db'))})")
|
||||
vfs.file_delete('duplicate.txt')!
|
||||
println("---------READING FILES (DB SIZE: ${os.file_size(os.join_path(example_data_dir, 'data/0.db'))})")
|
||||
println(vfs.file_read('some_file.txt')!.bytestr())
|
||||
println(vfs.file_read('another_file.txt')!.bytestr())
|
||||
vfs.file_delete('/duplicate.txt') or {
|
||||
panic('Failed to delete file: ${err}')
|
||||
}
|
||||
|
||||
data_path := os.join_path(example_data_dir, 'data/0.db')
|
||||
db_file_path := os.join_path(data_path, '0.db')
|
||||
println("---------READING FILES (DB SIZE: ${if os.exists(db_file_path) { os.file_size(db_file_path) } else { 0 }})")
|
||||
some_file_content2 := vfs.file_read('/some_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(some_file_content2.bytestr())
|
||||
|
||||
another_file_content2 := vfs.file_read('/another_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println(another_file_content2.bytestr())
|
||||
|
||||
// FAILS SUCCESSFULLY
|
||||
// println(vfs.file_read('duplicate.txt')!.bytestr())
|
||||
// duplicate_content := vfs.file_read('duplicate.txt') or {
|
||||
// println('Expected error: ${err}')
|
||||
// []u8{}
|
||||
// }
|
||||
// if duplicate_content.len > 0 {
|
||||
// println(duplicate_content.bytestr())
|
||||
// }
|
||||
180
examples/vfs/vfs_db/vfs_basics.vsh
Executable file
180
examples/vfs/vfs_db/vfs_basics.vsh
Executable file
@@ -0,0 +1,180 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import os
|
||||
import freeflowuniverse.herolib.vfs.vfs_db
|
||||
import freeflowuniverse.herolib.data.ourdb
|
||||
|
||||
// This example demonstrates directory operations in the VFS
|
||||
// - Creating directories with subdirectories
|
||||
// - Listing directories
|
||||
// - Reading and writing files in subdirectories
|
||||
// - Deleting files and verifying they're gone
|
||||
|
||||
// Set up the example data directory
|
||||
example_data_dir := '/tmp/example_dir_ops'
|
||||
os.mkdir_all(example_data_dir)!
|
||||
|
||||
// Create separate databases for data and metadata
|
||||
mut db_data := ourdb.new(
|
||||
path: os.join_path(example_data_dir, 'data')
|
||||
incremental_mode: false
|
||||
)!
|
||||
|
||||
mut db_metadata := ourdb.new(
|
||||
path: os.join_path(example_data_dir, 'metadata')
|
||||
incremental_mode: false
|
||||
)!
|
||||
|
||||
// Create VFS with separate databases for data and metadata
|
||||
mut vfs := vfs_db.new(mut db_data, mut db_metadata) or {
|
||||
panic('Failed to create VFS: ${err}')
|
||||
}
|
||||
|
||||
println('\n---------BEGIN DIRECTORY OPERATIONS EXAMPLE')
|
||||
|
||||
// Create directories with subdirectories
|
||||
println('\n---------CREATING DIRECTORIES')
|
||||
vfs.dir_create('/dir1') or {
|
||||
panic('Failed to create directory: ${err}')
|
||||
}
|
||||
println('Created directory: /dir1')
|
||||
|
||||
vfs.dir_create('/dir1/subdir1') or {
|
||||
panic('Failed to create directory: ${err}')
|
||||
}
|
||||
println('Created directory: /dir1/subdir1')
|
||||
|
||||
vfs.dir_create('/dir1/subdir2') or {
|
||||
panic('Failed to create directory: ${err}')
|
||||
}
|
||||
println('Created directory: /dir1/subdir2')
|
||||
|
||||
vfs.dir_create('/dir2') or {
|
||||
panic('Failed to create directory: ${err}')
|
||||
}
|
||||
println('Created directory: /dir2')
|
||||
|
||||
vfs.dir_create('/dir2/subdir1') or {
|
||||
panic('Failed to create directory: ${err}')
|
||||
}
|
||||
println('Created directory: /dir2/subdir1')
|
||||
|
||||
vfs.dir_create('/dir2/subdir1/subsubdir1') or {
|
||||
panic('Failed to create directory: ${err}')
|
||||
}
|
||||
println('Created directory: /dir2/subdir1/subsubdir1')
|
||||
|
||||
// List directories
|
||||
println('\n---------LISTING ROOT DIRECTORY')
|
||||
root_entries := vfs.dir_list('/') or {
|
||||
panic('Failed to list directory: ${err}')
|
||||
}
|
||||
println('Root directory contains:')
|
||||
for entry in root_entries {
|
||||
entry_type := if entry.get_metadata().file_type == .directory { 'directory' } else { 'file' }
|
||||
println('- ${entry.get_metadata().name} (${entry_type})')
|
||||
}
|
||||
|
||||
println('\n---------LISTING /dir1 DIRECTORY')
|
||||
dir1_entries := vfs.dir_list('/dir1') or {
|
||||
panic('Failed to list directory: ${err}')
|
||||
}
|
||||
println('/dir1 directory contains:')
|
||||
for entry in dir1_entries {
|
||||
entry_type := if entry.get_metadata().file_type == .directory { 'directory' } else { 'file' }
|
||||
println('- ${entry.get_metadata().name} (${entry_type})')
|
||||
}
|
||||
|
||||
// Write a file in a subdirectory
|
||||
println('\n---------WRITING FILE IN SUBDIRECTORY')
|
||||
vfs.file_create('/dir1/subdir1/test_file.txt') or {
|
||||
panic('Failed to create file: ${err}')
|
||||
}
|
||||
println('Created file: /dir1/subdir1/test_file.txt')
|
||||
|
||||
test_content := 'This is a test file in a subdirectory'
|
||||
vfs.file_write('/dir1/subdir1/test_file.txt', test_content.bytes()) or {
|
||||
panic('Failed to write file: ${err}')
|
||||
}
|
||||
println('Wrote content to file: /dir1/subdir1/test_file.txt')
|
||||
|
||||
// Read the file and verify content
|
||||
println('\n---------READING FILE FROM SUBDIRECTORY')
|
||||
file_content := vfs.file_read('/dir1/subdir1/test_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println('File content: ${file_content.bytestr()}')
|
||||
println('Content verification: ${if file_content.bytestr() == test_content { 'SUCCESS' } else { 'FAILED' }}')
|
||||
|
||||
// List the subdirectory to see the file
|
||||
println('\n---------LISTING /dir1/subdir1 DIRECTORY')
|
||||
subdir1_entries := vfs.dir_list('/dir1/subdir1') or {
|
||||
panic('Failed to list directory: ${err}')
|
||||
}
|
||||
println('/dir1/subdir1 directory contains:')
|
||||
for entry in subdir1_entries {
|
||||
entry_type := if entry.get_metadata().file_type == .directory { 'directory' } else { 'file' }
|
||||
println('- ${entry.get_metadata().name} (${entry_type})')
|
||||
}
|
||||
|
||||
// Delete the file
|
||||
println('\n---------DELETING FILE')
|
||||
vfs.file_delete('/dir1/subdir1/test_file.txt') or {
|
||||
panic('Failed to delete file: ${err}')
|
||||
}
|
||||
println('Deleted file: /dir1/subdir1/test_file.txt')
|
||||
|
||||
// List the subdirectory again to verify the file is gone
|
||||
println('\n---------LISTING /dir1/subdir1 DIRECTORY AFTER DELETION')
|
||||
subdir1_entries_after := vfs.dir_list('/dir1/subdir1') or {
|
||||
panic('Failed to list directory: ${err}')
|
||||
}
|
||||
println('/dir1/subdir1 directory contains:')
|
||||
if subdir1_entries_after.len == 0 {
|
||||
println('- (empty directory)')
|
||||
} else {
|
||||
for entry in subdir1_entries_after {
|
||||
entry_type := if entry.get_metadata().file_type == .directory { 'directory' } else { 'file' }
|
||||
println('- ${entry.get_metadata().name} (${entry_type})')
|
||||
}
|
||||
}
|
||||
|
||||
// Create a file in a deep subdirectory
|
||||
println('\n---------CREATING FILE IN DEEP SUBDIRECTORY')
|
||||
vfs.file_create('/dir2/subdir1/subsubdir1/deep_file.txt') or {
|
||||
panic('Failed to create file: ${err}')
|
||||
}
|
||||
println('Created file: /dir2/subdir1/subsubdir1/deep_file.txt')
|
||||
|
||||
deep_content := 'This file is in a deep subdirectory'
|
||||
vfs.file_write('/dir2/subdir1/subsubdir1/deep_file.txt', deep_content.bytes()) or {
|
||||
panic('Failed to write file: ${err}')
|
||||
}
|
||||
println('Wrote content to file: /dir2/subdir1/subsubdir1/deep_file.txt')
|
||||
|
||||
// Read the deep file and verify content
|
||||
println('\n---------READING FILE FROM DEEP SUBDIRECTORY')
|
||||
deep_file_content := vfs.file_read('/dir2/subdir1/subsubdir1/deep_file.txt') or {
|
||||
panic('Failed to read file: ${err}')
|
||||
}
|
||||
println('File content: ${deep_file_content.bytestr()}')
|
||||
println('Content verification: ${if deep_file_content.bytestr() == deep_content { 'SUCCESS' } else { 'FAILED' }}')
|
||||
|
||||
// Clean up by deleting directories (optional)
|
||||
println('\n---------CLEANING UP')
|
||||
vfs.file_delete('/dir2/subdir1/subsubdir1/deep_file.txt') or {
|
||||
panic('Failed to delete file: ${err}')
|
||||
}
|
||||
println('Deleted file: /dir2/subdir1/subsubdir1/deep_file.txt')
|
||||
|
||||
// Try to verify the file is gone by attempting to read it
|
||||
println('\n---------VERIFYING FILE IS GONE')
|
||||
deep_file_exists := vfs.file_read('/dir2/subdir1/subsubdir1/deep_file.txt') or {
|
||||
println('File is gone as expected: ${err}')
|
||||
[]u8{}
|
||||
}
|
||||
if deep_file_exists.len > 0 {
|
||||
panic('ERROR: File still exists!')
|
||||
}
|
||||
|
||||
println('\n---------END DIRECTORY OPERATIONS EXAMPLE')
|
||||
Reference in New Issue
Block a user