specSFS requires Java.
[bluesky.git] / bluesky / file.c
index 2df979e..66f84e7 100644 (file)
@@ -35,7 +35,7 @@ void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
         block->data = bluesky_string_new(g_malloc0(block_len), block_len);
         break;
     case BLUESKY_BLOCK_REF:
-        bluesky_block_fetch(inode->fs, block);
+        bluesky_block_fetch(inode->fs, block, NULL);
         g_assert(block->type == BLUESKY_BLOCK_CACHED);
         /* Fall through */
     case BLUESKY_BLOCK_CACHED:
@@ -157,6 +157,25 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
     g_return_if_fail(offset < inode->size);
     g_return_if_fail(len <= inode->size - offset);
 
+    /* Start fetches on any data blocks that we will need for this read. */
+    BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
+    barrier->op = STORE_OP_BARRIER;
+    uint64_t start_block, end_block;
+    start_block = offset / BLUESKY_BLOCK_SIZE;
+    end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
+    g_print("Start prefetch on blocks %"PRIi64" .. %"PRIi64"\n",
+            start_block, end_block);
+    for (uint64_t i = start_block; i <= end_block; i++) {
+        BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
+                                         i);
+        if (b->type == BLUESKY_BLOCK_REF)
+            bluesky_block_fetch(inode->fs, b, barrier);
+    }
+    bluesky_store_async_submit(barrier);
+    bluesky_store_async_wait(barrier);
+    bluesky_store_async_unref(barrier);
+    g_print("Prefetch complete.\n");
+
     while (len > 0) {
         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
@@ -169,7 +188,7 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
             memset(buf, 0, bytes);
             break;
         case BLUESKY_BLOCK_REF:
-            bluesky_block_fetch(inode->fs, b);
+            bluesky_block_fetch(inode->fs, b, NULL);
             /* Fall through */
         case BLUESKY_BLOCK_CACHED:
         case BLUESKY_BLOCK_DIRTY:
@@ -185,16 +204,32 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
 
 /* Read the given block from cloud-backed storage if the data is not already
  * cached. */
-void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
+static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
+{
+    BlueSkyBlock *block = (BlueSkyBlock *)data;
+
+    bluesky_string_unref(block->data);
+    block->data = async->data;
+    bluesky_string_ref(block->data);
+    block->type = BLUESKY_BLOCK_CACHED;
+}
+
+void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
+                         BlueSkyStoreAsync *barrier)
 {
     if (block->type != BLUESKY_BLOCK_REF)
         return;
 
-    BlueSkyRCStr *string = bluesky_store_get(fs->store, block->ref);
+    BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
+    async->op = STORE_OP_GET;
+    async->key = g_strdup(block->ref);
+    bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
+    bluesky_store_async_submit(async);
 
-    bluesky_string_unref(block->data);
-    block->data = string;
-    block->type = BLUESKY_BLOCK_CACHED;
+    if (barrier != NULL)
+        bluesky_store_add_barrier(barrier, async);
+    else
+        bluesky_store_async_wait(async);
 }
 
 /* Write the given block to cloud-backed storage and mark it clean. */
@@ -225,13 +260,9 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
     g_free(block->ref);
     block->ref = name;
 
-    /* block->type = BLUESKY_BLOCK_CACHED; */
-    bluesky_string_unref(block->data);
-    block->data = NULL;
-    block->type = BLUESKY_BLOCK_REF;
+    block->type = BLUESKY_BLOCK_CACHED;
 
     g_checksum_free(csum);
-    //bluesky_string_unref(data);
 }
 
 /* Flush all blocks in a file to stable storage. */