+ item->location_flags &= ~CLOUDLOG_JOURNAL;
+ if (!(item->location_flags & CLOUDLOG_CLOUD))
+ return;
+
+ map = bluesky_cachefile_lookup(fs,
+ item->location.directory,
+ item->location.sequence,
+ FALSE);
+ if (map == NULL)
+ return;
+
+ /* At this point, we have information about the log segment containing the
+ * item we need. If our item is already fetched, we have nothing to do
+ * except update the atime. If not, queue up a fetch of our object. */
+ const BlueSkyRangesetItem *rangeitem;
+ rangeitem = bluesky_rangeset_lookup(map->items,
+ item->location.offset);
+ if (rangeitem == NULL) {
+ if (map->prefetches == NULL)
+ map->prefetches = bluesky_rangeset_new();
+
+ gchar *id = bluesky_cloudlog_id_to_string(item->id);
+ if (bluesky_verbose)
+ g_print("Need to prefetch %s\n", id);
+ g_free(id);
+
+ bluesky_rangeset_insert(map->prefetches,
+ item->location.offset,
+ item->location.size, NULL);
+
+ uint64_t start, length;
+ bluesky_rangeset_get_extents(map->prefetches, &start, &length);
+ if (bluesky_verbose)
+ g_print("Range to prefetch: %"PRIu64" + %"PRIu64"\n",
+ start, length);
+ }
+
+ bluesky_cachefile_unref(map);
+ g_mutex_unlock(map->lock);
+}
+
+/* Ensure that a cloud log item is loaded in memory, and if not read it in.
+ * TODO: Make asynchronous, and make this also fetch from the cloud. Right now
+ * we only read from the log. Log item must be locked. */
+void bluesky_cloudlog_fetch(BlueSkyCloudLog *log)
+{
+ if (log->data != NULL)
+ return;
+
+ BlueSkyProfile *profile = bluesky_profile_get();
+ if (profile != NULL)
+ bluesky_profile_add_event(profile, g_strdup_printf("Fetch log entry"));
+
+ /* There are actually two cases: a full deserialization if we have not ever
+ * read the object before, and a partial deserialization where the metadata
+ * is already in memory and we just need to remap the data. If the object
+ * type has not yet been set, we'll need to read and parse the metadata.
+ * Once that is done, we can fall through the case of remapping the data
+ * itself. */
+ if (log->type == LOGTYPE_UNKNOWN) {
+ BlueSkyRCStr *raw = bluesky_log_map_object(log, FALSE);
+ g_assert(raw != NULL);
+ bluesky_deserialize_cloudlog(log, raw->data, raw->len);
+ bluesky_string_unref(raw);
+ }
+
+ /* At this point all metadata should be available and we need only remap
+ * the object data. */
+ log->data = bluesky_log_map_object(log, TRUE);
+
+ if (log->data == NULL) {
+ g_error("Unable to fetch cloudlog entry!");
+ }
+
+ if (profile != NULL)
+ bluesky_profile_add_event(profile, g_strdup_printf("Fetch complete"));
+ g_cond_broadcast(log->cond);
+}