FANNG1 commented on code in PR #5878:
URL: https://github.com/apache/gravitino/pull/5878#discussion_r1891048215


##########
clients/filesystem-fuse/src/filesystem.rs:
##########
@@ -239,3 +352,406 @@ pub trait FileWriter: Sync + Send {
         Ok(())
     }
 }
+
+/// SimpleFileSystem is a simple implementation for the file system.
+/// it is used to manage the file metadata and file handle.
+/// The operations of the file system are implemented by the PathFileSystem.
+/// Note: This class is not use in the production code, it is used for the 
demo and testing
+pub struct SimpleFileSystem<T: PathFileSystem> {
+    /// file entries
+    file_entry_manager: RwLock<FileEntryManager>,
+    /// opened files
+    opened_file_manager: OpenedFileManager,
+    /// inode id generator
+    file_id_generator: AtomicU64,
+
+    /// real filesystem
+    fs: T,
+}
+
+impl<T: PathFileSystem> SimpleFileSystem<T> {
+    const INITIAL_FILE_ID: u64 = 10000;
+    const ROOT_DIR_PARENT_FILE_ID: u64 = 0;
+    const ROOT_DIR_FILE_ID: u64 = 1;
+    const ROOT_DIR_NAME: &'static str = "";
+
+    pub(crate) fn new(fs: T) -> Self {
+        Self {
+            file_entry_manager: RwLock::new(FileEntryManager::new()),
+            opened_file_manager: OpenedFileManager::new(),
+            file_id_generator: AtomicU64::new(Self::INITIAL_FILE_ID),
+            fs,
+        }
+    }
+
+    fn next_file_id(&self) -> u64 {
+        self.file_id_generator
+            .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
+    }
+
+    async fn get_file_entry(&self, file_id: u64) -> Result<FileEntry> {
+        self.file_entry_manager
+            .read()
+            .await
+            .get_file_by_id(file_id)
+            .ok_or(Errno::from(libc::ENOENT))
+    }
+
+    async fn get_file_entry_by_path(&self, path: &str) -> Option<FileEntry> {
+        self.file_entry_manager.read().await.get_file_by_name(path)
+    }
+
+    async fn fill_file_id(&self, file_stat: &mut FileStat, parent_file_id: 
u64) {
+        let mut file_manager = self.file_entry_manager.write().await;
+        let file = file_manager.get_file_by_name(&file_stat.path);
+        match file {
+            None => {
+                // allocate new file id
+                file_stat.set_file_id(parent_file_id, self.next_file_id());
+                file_manager.insert(file_stat.parent_file_id, 
file_stat.file_id, &file_stat.path);
+            }
+            Some(file) => {
+                // use the exist file id
+                file_stat.set_file_id(file.parent_file_id, file.file_id);
+            }
+        }
+    }
+
+    async fn open_file_internal(
+        &self,
+        file_id: u64,
+        flags: u32,
+        kind: FileType,
+    ) -> Result<FileHandle> {
+        let file_entry = self.get_file_entry(file_id).await?;
+
+        let mut file = {
+            match kind {
+                FileType::Directory => {
+                    self.fs
+                        .open_dir(&file_entry.file_name, OpenFileFlags(flags))
+                        .await?
+                }
+                FileType::RegularFile => {
+                    self.fs
+                        .open_file(&file_entry.file_name, OpenFileFlags(flags))
+                        .await?
+                }
+                _ => return Err(Errno::from(libc::EINVAL)),
+            }
+        };
+        // set the exists file id
+        file.set_file_id(file_entry.parent_file_id, file_id);
+        let file = self.opened_file_manager.put_file(file);
+        let file = file.lock().await;
+        Ok(file.file_handle())
+    }
+}
+
+#[async_trait]
+impl<T: PathFileSystem> RawFileSystem for SimpleFileSystem<T> {
+    async fn init(&self) -> Result<()> {
+        // init root directory
+        self.file_entry_manager.write().await.insert(
+            Self::ROOT_DIR_PARENT_FILE_ID,
+            Self::ROOT_DIR_FILE_ID,
+            Self::ROOT_DIR_NAME,
+        );
+        self.fs.init().await
+    }
+
+    async fn get_file_path(&self, file_id: u64) -> String {
+        let file = self.get_file_entry(file_id).await;
+        file.map(|x| x.file_name).unwrap_or_else(|_| "".to_string())

Review Comment:
   then how do you use it? why not return an error?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to