[gvdb/mcatanzaro/write-contents-async] Add gvdb_table_write_contents_async()



commit 030f5385b6699db31643f0a14e7e4864b18bcaf9
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Thu Jun 20 18:15:54 2019 -0500

    Add gvdb_table_write_contents_async()
    
    This is just an async version of gvdb_table_write_contents().
    
    Future work: someone could write an async version of gvdb_table_new(),
    then sync I/O would no longer be required to construct a GvdbTable.

 gvdb-builder.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gvdb-builder.h | 45 +++++++++++++++++-----------
 2 files changed, 121 insertions(+), 17 deletions(-)
---
diff --git a/gvdb-builder.c b/gvdb-builder.c
index 2383e60..40e6c6a 100644
--- a/gvdb-builder.c
+++ b/gvdb-builder.c
@@ -511,6 +511,9 @@ gvdb_table_write_contents (GHashTable   *table,
   FileBuilder *fb;
   GString *str;
 
+  g_return_if_fail (table != NULL);
+  g_return_if_fail (filename != NULL);
+
   fb = file_builder_new (byteswap);
   file_builder_add_hash (fb, table, &root);
   str = file_builder_serialise (fb, root);
@@ -520,3 +523,93 @@ gvdb_table_write_contents (GHashTable   *table,
 
   return status;
 }
+
+typedef struct {
+  GString *contents;
+  GFile *file;
+} WriteContentsData;
+
+static WriteContentsData *
+write_contents_data_new (GString *contents,
+                         GFile   *file)
+{
+  WriteContentsData *data;
+
+  data = g_slice_new (WriteContentsData);
+  data->contents = contents;
+  data->file = file;
+
+  return data;
+}
+
+static void
+write_contents_data_free (WriteContentsData *data)
+{
+  g_string_free (data->contents, TRUE);
+  g_object_unref (data->file);
+  g_slice_free (WriteContentsData, data);
+}
+
+static void
+replace_contents_cb (GObject      *source_object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
+{
+  GTask *task = user_data;
+  WriteContentsData *data = g_task_get_task_data (task);
+  GError *error = NULL;
+
+  if (!g_file_replace_contents_finish (data->file, result, NULL, &error))
+    {
+      g_task_return_error (task, error);
+      g_object_unref (task);
+      return;
+    }
+
+  g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
+}
+
+void
+gvdb_table_write_contents_async (GHashTable          *table,
+                                 const gchar         *filename,
+                                 gboolean             byteswap,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
+{
+  struct gvdb_pointer root;
+  FileBuilder *fb;
+  WriteContentsData *data;
+  GString *str;
+  GFile *file;
+  GTask *task;
+
+  g_return_if_fail (table != NULL);
+  g_return_if_fail (filename != NULL);
+
+  fb = file_builder_new (byteswap);
+  file_builder_add_hash (fb, table, &root);
+  str = file_builder_serialise (fb, root);
+
+  file = g_file_new_for_path (filename);
+  data = write_contents_data_new (str, file);
+
+  task = g_task_new (NULL, cancellable, callback, user_data);
+  g_task_set_task_data (task, data, (GDestroyNotify)write_contents_data_free);
+
+  g_file_replace_contents_async (file, str->str, str->len,
+                                 NULL, FALSE,
+                                 G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION,
+                                 cancellable, replace_contents_cb, task);
+}
+
+gboolean
+gvdb_table_write_contents_finish (GHashTable    *table,
+                                  GAsyncResult  *result,
+                                  GError       **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
diff --git a/gvdb-builder.h b/gvdb-builder.h
index b4815f0..92adc20 100644
--- a/gvdb-builder.h
+++ b/gvdb-builder.h
@@ -25,31 +25,42 @@
 typedef struct _GvdbItem GvdbItem;
 
 G_GNUC_INTERNAL
-GHashTable *            gvdb_hash_table_new                             (GHashTable    *parent,
-                                                                         const gchar   *key);
+GHashTable *            gvdb_hash_table_new                             (GHashTable        *parent,
+                                                                         const gchar       *key);
 
 G_GNUC_INTERNAL
-GvdbItem *              gvdb_hash_table_insert                          (GHashTable    *table,
-                                                                         const gchar   *key);
+GvdbItem *              gvdb_hash_table_insert                          (GHashTable        *table,
+                                                                         const gchar       *key);
 G_GNUC_INTERNAL
-void                    gvdb_hash_table_insert_string                   (GHashTable    *table,
-                                                                         const gchar   *key,
-                                                                         const gchar   *value);
+void                    gvdb_hash_table_insert_string                   (GHashTable        *table,
+                                                                         const gchar       *key,
+                                                                         const gchar       *value);
 
 G_GNUC_INTERNAL
-void                    gvdb_item_set_value                             (GvdbItem      *item,
-                                                                         GVariant      *value);
+void                    gvdb_item_set_value                             (GvdbItem          *item,
+                                                                         GVariant          *value);
 G_GNUC_INTERNAL
-void                    gvdb_item_set_hash_table                        (GvdbItem      *item,
-                                                                         GHashTable    *table);
+void                    gvdb_item_set_hash_table                        (GvdbItem          *item,
+                                                                         GHashTable        *table);
 G_GNUC_INTERNAL
-void                    gvdb_item_set_parent                            (GvdbItem      *item,
-                                                                         GvdbItem      *parent);
+void                    gvdb_item_set_parent                            (GvdbItem          *item,
+                                                                         GvdbItem          *parent);
 
 G_GNUC_INTERNAL
-gboolean                gvdb_table_write_contents                       (GHashTable     *table,
-                                                                         const gchar    *filename,
-                                                                         gboolean        byteswap,
-                                                                         GError        **error);
+gboolean                gvdb_table_write_contents                       (GHashTable         *table,
+                                                                         const gchar        *filename,
+                                                                         gboolean            byteswap,
+                                                                         GError            **error);
+
+void                    gvdb_table_write_contents_async                 (GHashTable          *table,
+                                                                         const gchar         *filename,
+                                                                         gboolean             byteswap,
+                                                                         GCancellable        *cancellable,
+                                                                         GAsyncReadyCallback  callback,
+                                                                         gpointer             user_data);
+
+gboolean                gvdb_table_write_contents_finish                (GHashTable          *table,
+                                                                         GAsyncResult        *result,
+                                                                         GError             **error);
 
 #endif /* __gvdb_builder_h__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]