Directory deserialization and loading of inodes from stable store.
[bluesky.git] / bluesky / inode.c
index 8fdd882..3430b19 100644 (file)
@@ -6,6 +6,7 @@
  * TODO: Licensing
  */
 
+#include <stdio.h>
 #include <stdint.h>
 #include <glib.h>
 #include <string.h>
@@ -140,6 +141,13 @@ BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
     inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
     g_mutex_unlock(fs->lock);
 
+    if (inode == NULL) {
+        bluesky_inode_fetch(fs, inum);
+        g_mutex_lock(fs->lock);
+        inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
+        g_mutex_unlock(fs->lock);
+    }
+
     return inode;
 }
 
@@ -150,3 +158,32 @@ void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
     g_hash_table_insert(fs->inodes, &inode->inum, inode);
     g_mutex_unlock(fs->lock);
 }
+
+/* Synchronize an inode to stable storage. */
+void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode)
+{
+    GString *buf = g_string_new("");
+    bluesky_serialize_inode(buf, inode);
+
+    gsize len = buf->len;
+    BlueSkyRCStr *data = bluesky_string_new(g_string_free(buf, FALSE), len);
+
+    char key[64];
+    sprintf(key, "inode-%016llx", inode->inum);
+
+    bluesky_store_put(fs->store, key, data);
+}
+
+/* Fetch an inode from stable storage. */
+void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum)
+{
+    char key[64];
+    sprintf(key, "inode-%016llx", inum);
+    BlueSkyRCStr *data = bluesky_store_get(fs->store, key);
+
+    BlueSkyInode *inode = bluesky_deserialize_inode(fs, data->data);
+    if (inode != NULL) {
+        bluesky_insert_inode(fs, inode);
+        g_print("Loaded inode %lld\n", (long long)inum);
+    }
+}