Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pseudo batch-less operations #178

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion include/core/jbatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ G_BEGIN_DECLS

struct JBatch;

/**
* Simulate batch-less operations.
*
* J_BATCH_SINGLE can be passed to operations instead of an actual batch.
* The operation will then be executed immediately and is finished after the call returns.
* Default semantic settings will be used for execution.
*
*/
#define J_BATCH_SINGLE NULL

typedef struct JBatch JBatch;

typedef void (*JBatchAsyncCallback)(JBatch*, gboolean, gpointer);
Expand Down Expand Up @@ -102,6 +112,8 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(JBatch, j_batch_unref)
/**
* Returns a batch's semantics.
*
* The returned object must be freed using j_semantic_unref.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this affect any existing code? It could lead to memory leaks.

*
* \code
* \endcode
*
Expand All @@ -114,6 +126,8 @@ JSemantics* j_batch_get_semantics(JBatch* batch);
/**
* Adds a new operation to the batch.
*
* Passing J_BATCH_SINGLE creates a temporary batch and calls j_batch_execute.
*
* \private
*
* \code
Expand All @@ -122,7 +136,7 @@ JSemantics* j_batch_get_semantics(JBatch* batch);
* \param batch A batch.
* \param operation An operation.
**/
void j_batch_add(JBatch* batch, JOperation* operation);
gboolean j_batch_add(JBatch* batch, JOperation* operation);

/**
* Executes the batch.
Expand Down
3 changes: 0 additions & 3 deletions include/db/jdb-entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ gboolean j_db_entry_set_field(JDBEntry* entry, gchar const* name, gconstpointer
* \param[out] error A GError pointer. Will point to a GError object in case of failure.
* \pre entry != NULL
* \pre entry has a least 1 value set to not NULL
* \pre batch != NULL
*
* \return TRUE on success, FALSE otherwise
**/
Expand All @@ -134,7 +133,6 @@ gboolean j_db_entry_insert(JDBEntry* entry, JBatch* batch, GError** error);
* \pre entry has a least 1 value set to not NULL
* \pre selector != NULL
* \pre selector matches at least 1 entry
* \pre batch != NULL
*
* \return TRUE on success, FALSE otherwise
**/
Expand All @@ -151,7 +149,6 @@ gboolean j_db_entry_update(JDBEntry* entry, JDBSelector* selector, JBatch* batch
* \param[in] batch the batch to append this operation to
* \param[out] error A GError pointer. Will point to a GError object in case of failure.
* \pre entry != NULL
* \pre batch != NULL
*
* \return TRUE on success, FALSE otherwise
**/
Expand Down
3 changes: 0 additions & 3 deletions include/db/jdb-schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ gboolean j_db_schema_add_index(JDBSchema* schema, gchar const** names, GError**
*
* \pre schema != NULL
* \pre schema contains at least 1 variable
* \pre batch != NULL
*
* \return TRUE on success, FALSE otherwise
**/
Expand All @@ -174,7 +173,6 @@ gboolean j_db_schema_create(JDBSchema* schema, JBatch* batch, GError** error);
*
* \pre schema != NULL
* \pre schema exists in the backend
* \pre batch != NULL
*
* \return TRUE on success, FALSE otherwise
**/
Expand All @@ -189,7 +187,6 @@ gboolean j_db_schema_get(JDBSchema* schema, JBatch* batch, GError** error);
*
* \pre schema != NULL
* \pre schema exists in the backend
* \pre batch != NULL
*
* \return TRUE on success, FALSE otherwise
**/
Expand Down
4 changes: 2 additions & 2 deletions include/item/jcollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ JCollection* j_collection_create(gchar const* name, JBatch* batch);
* \param name A name.
* \param batch A batch.
**/
void j_collection_get(JCollection** collection, gchar const* name, JBatch* batch);
gboolean j_collection_get(JCollection** collection, gchar const* name, JBatch* batch);

/**
* Deletes a collection.
Expand All @@ -123,7 +123,7 @@ void j_collection_get(JCollection** collection, gchar const* name, JBatch* batch
* \param collection A collection.
* \param batch A batch.
**/
void j_collection_delete(JCollection* collection, JBatch* batch);
gboolean j_collection_delete(JCollection* collection, JBatch* batch);

/**
* @}
Expand Down
10 changes: 5 additions & 5 deletions include/item/jitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ JItem* j_item_create(JCollection* collection, gchar const* name, JDistribution*
* \param item An item.
* \param batch A batch.
**/
void j_item_delete(JItem* item, JBatch* batch);
gboolean j_item_delete(JItem* item, JBatch* batch);

/**
* Gets an item from a collection.
Expand All @@ -142,7 +142,7 @@ void j_item_delete(JItem* item, JBatch* batch);
* \param name A name.
* \param batch A batch.
**/
void j_item_get(JCollection* collection, JItem** item, gchar const* name, JBatch* batch);
gboolean j_item_get(JCollection* collection, JItem** item, gchar const* name, JBatch* batch);

/**
* Reads an item.
Expand All @@ -157,7 +157,7 @@ void j_item_get(JCollection* collection, JItem** item, gchar const* name, JBatch
* \param bytes_read Number of bytes read.
* \param batch A batch.
**/
void j_item_read(JItem* item, gpointer data, guint64 length, guint64 offset, guint64* bytes_read, JBatch* batch);
gboolean j_item_read(JItem* item, gpointer data, guint64 length, guint64 offset, guint64* bytes_read, JBatch* batch);

/**
* Writes an item.
Expand All @@ -175,7 +175,7 @@ void j_item_read(JItem* item, gpointer data, guint64 length, guint64 offset, gui
* \param bytes_written Number of bytes written.
* \param batch A batch.
**/
void j_item_write(JItem* item, gconstpointer data, guint64 length, guint64 offset, guint64* bytes_written, JBatch* batch);
gboolean j_item_write(JItem* item, gconstpointer data, guint64 length, guint64 offset, guint64* bytes_written, JBatch* batch);

/**
* Get the status of an item.
Expand All @@ -186,7 +186,7 @@ void j_item_write(JItem* item, gconstpointer data, guint64 length, guint64 offse
* \param item An item.
* \param batch A batch.
**/
void j_item_get_status(JItem* item, JBatch* batch);
gboolean j_item_get_status(JItem* item, JBatch* batch);

/**
* Returns an item's size.
Expand Down
8 changes: 4 additions & 4 deletions include/kv/jkv.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(JKV, j_kv_unref)
* \param value_destroy A function to correctly free the stored data.
* \param batch A batch.
**/
void j_kv_put(JKV* kv, gpointer value, guint32 value_len, GDestroyNotify value_destroy, JBatch* batch);
gboolean j_kv_put(JKV* kv, gpointer value, guint32 value_len, GDestroyNotify value_destroy, JBatch* batch);

/**
* Deletes a key-value pair.
Expand All @@ -136,7 +136,7 @@ void j_kv_put(JKV* kv, gpointer value, guint32 value_len, GDestroyNotify value_d
* \param kv A JKV.
* \param batch A batch.
**/
void j_kv_delete(JKV* kv, JBatch* batch);
gboolean j_kv_delete(JKV* kv, JBatch* batch);

/**
* Get a key-value pair.
Expand All @@ -149,7 +149,7 @@ void j_kv_delete(JKV* kv, JBatch* batch);
* \param value_len A pointer to the length of the returned value buffer.
* \param batch A batch.
**/
void j_kv_get(JKV* kv, gpointer* value, guint32* value_len, JBatch* batch);
gboolean j_kv_get(JKV* kv, gpointer* value, guint32* value_len, JBatch* batch);

/**
* Get a key-value pair and execute a callback function once the pair is received.
Expand All @@ -162,7 +162,7 @@ void j_kv_get(JKV* kv, gpointer* value, guint32* value_len, JBatch* batch);
* \param data User defined data which will be passed to the callback function.
* \param batch A batch.
**/
void j_kv_get_callback(JKV* kv, JKVGetFunc func, gpointer data, JBatch* batch);
gboolean j_kv_get_callback(JKV* kv, JKVGetFunc func, gpointer data, JBatch* batch);

/**
* @}
Expand Down
12 changes: 6 additions & 6 deletions include/object/jdistributed-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(JDistributedObject, j_distributed_object_unref)
*
* \return A new object. Should be freed with j_distributed_object_unref().
**/
void j_distributed_object_create(JDistributedObject* object, JBatch* batch);
gboolean j_distributed_object_create(JDistributedObject* object, JBatch* batch);

/**
* Deletes an object.
Expand All @@ -113,7 +113,7 @@ void j_distributed_object_create(JDistributedObject* object, JBatch* batch);
* \param object An object.
* \param batch A batch.
**/
void j_distributed_object_delete(JDistributedObject* object, JBatch* batch);
gboolean j_distributed_object_delete(JDistributedObject* object, JBatch* batch);

/**
* Reads an object.
Expand All @@ -128,7 +128,7 @@ void j_distributed_object_delete(JDistributedObject* object, JBatch* batch);
* \param bytes_read Number of bytes read.
* \param batch A batch.
**/
void j_distributed_object_read(JDistributedObject* object, gpointer data, guint64 length, guint64 offset, guint64* bytes_read, JBatch* batch);
gboolean j_distributed_object_read(JDistributedObject* object, gpointer data, guint64 length, guint64 offset, guint64* bytes_read, JBatch* batch);

/**
* Writes an object.
Expand All @@ -146,7 +146,7 @@ void j_distributed_object_read(JDistributedObject* object, gpointer data, guint6
* \param bytes_written Number of bytes written.
* \param batch A batch.
**/
void j_distributed_object_write(JDistributedObject* object, gconstpointer data, guint64 length, guint64 offset, guint64* bytes_written, JBatch* batch);
gboolean j_distributed_object_write(JDistributedObject* object, gconstpointer data, guint64 length, guint64 offset, guint64* bytes_written, JBatch* batch);

/**
* Get the status of an object.
Expand All @@ -159,7 +159,7 @@ void j_distributed_object_write(JDistributedObject* object, gconstpointer data,
* \param size The size of object.
* \param batch A batch.
**/
void j_distributed_object_status(JDistributedObject* object, gint64* modification_time, guint64* size, JBatch* batch);
gboolean j_distributed_object_status(JDistributedObject* object, gint64* modification_time, guint64* size, JBatch* batch);

/**
* Sync an object.
Expand All @@ -170,7 +170,7 @@ void j_distributed_object_status(JDistributedObject* object, gint64* modificatio
* \param object An object.
* \param batch A batch.
**/
void j_distributed_object_sync(JDistributedObject* object, JBatch* batch);
gboolean j_distributed_object_sync(JDistributedObject* object, JBatch* batch);

/**
* @}
Expand Down
12 changes: 6 additions & 6 deletions include/object/jobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(JObject, j_object_unref)
* \param batch A batch.
*
**/
void j_object_create(JObject* object, JBatch* batch);
gboolean j_object_create(JObject* object, JBatch* batch);

/**
* Deletes an object.
Expand All @@ -127,7 +127,7 @@ void j_object_create(JObject* object, JBatch* batch);
* \param object An object.
* \param batch A batch.
**/
void j_object_delete(JObject* object, JBatch* batch);
gboolean j_object_delete(JObject* object, JBatch* batch);

/**
* Reads an object.
Expand All @@ -142,7 +142,7 @@ void j_object_delete(JObject* object, JBatch* batch);
* \param bytes_read Number of bytes read.
* \param batch A batch.
**/
void j_object_read(JObject* object, gpointer data, guint64 length, guint64 offset, guint64* bytes_read, JBatch* batch);
gboolean j_object_read(JObject* object, gpointer data, guint64 length, guint64 offset, guint64* bytes_read, JBatch* batch);

/**
* Writes an object.
Expand All @@ -160,7 +160,7 @@ void j_object_read(JObject* object, gpointer data, guint64 length, guint64 offse
* \param bytes_written Number of bytes written.
* \param batch A batch.
**/
void j_object_write(JObject* object, gconstpointer data, guint64 length, guint64 offset, guint64* bytes_written, JBatch* batch);
gboolean j_object_write(JObject* object, gconstpointer data, guint64 length, guint64 offset, guint64* bytes_written, JBatch* batch);

/**
* Get the status of an object.
Expand All @@ -173,7 +173,7 @@ void j_object_write(JObject* object, gconstpointer data, guint64 length, guint64
* \param size The size of object.
* \param batch A batch.
**/
void j_object_status(JObject* object, gint64* modification_time, guint64* size, JBatch* batch);
gboolean j_object_status(JObject* object, gint64* modification_time, guint64* size, JBatch* batch);

/**
* Sync an object.
Expand All @@ -184,7 +184,7 @@ void j_object_status(JObject* object, gint64* modification_time, guint64* size,
* \param object An object.
* \param batch A batch.
**/
void j_object_sync(JObject* object, JBatch* batch);
gboolean j_object_sync(JObject* object, JBatch* batch);

/**
* @}
Expand Down
22 changes: 17 additions & 5 deletions lib/core/jbatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,32 @@ j_batch_get_semantics(JBatch* batch)
{
J_TRACE_FUNCTION(NULL);

g_return_val_if_fail(batch != NULL, NULL);
if (batch == J_BATCH_SINGLE)
{
return j_semantics_new(J_SEMANTICS_TEMPLATE_DEFAULT);
}

return batch->semantics;
return j_semantics_ref(batch->semantics);
}

void
gboolean
j_batch_add(JBatch* batch, JOperation* operation)
{
J_TRACE_FUNCTION(NULL);

g_return_if_fail(batch != NULL);
g_return_if_fail(operation != NULL);
g_return_val_if_fail(operation != NULL, FALSE);

// pseudo batch-less ops by passing J_BATCH_SINGLE
if (batch == J_BATCH_SINGLE)
{
g_autoptr(JBatch) tmp_batch = NULL;
tmp_batch = j_batch_new_for_template(J_SEMANTICS_TEMPLATE_DEFAULT);
j_list_append(tmp_batch->list, operation);
return j_batch_execute(tmp_batch);
}

j_list_append(batch->list, operation);
return TRUE;
}

/* Internal */
Expand Down
3 changes: 0 additions & 3 deletions lib/db/jdb-entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ j_db_entry_insert(JDBEntry* entry, JBatch* batch, GError** error)
J_TRACE_FUNCTION(NULL);

g_return_val_if_fail(entry != NULL, FALSE);
g_return_val_if_fail(batch != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

if (G_UNLIKELY(!j_db_internal_insert(entry, batch, error)))
Expand All @@ -199,7 +198,6 @@ j_db_entry_update(JDBEntry* entry, JDBSelector* selector, JBatch* batch, GError*
bson_t* bson;

g_return_val_if_fail(entry != NULL, FALSE);
g_return_val_if_fail(batch != NULL, FALSE);
g_return_val_if_fail(selector != NULL, FALSE);
g_return_val_if_fail(selector->schema == entry->schema, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
Expand Down Expand Up @@ -229,7 +227,6 @@ j_db_entry_delete(JDBEntry* entry, JDBSelector* selector, JBatch* batch, GError*
J_TRACE_FUNCTION(NULL);

g_return_val_if_fail(entry != NULL, FALSE);
g_return_val_if_fail(batch != NULL, FALSE);
g_return_val_if_fail((selector == NULL) || (selector->schema == entry->schema), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

Expand Down
Loading