Skip to content

Commit

Permalink
[pgmoneta#350] Universal value types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jubilee101 committed Aug 9, 2024
1 parent 2f476f1 commit 144f8f0
Show file tree
Hide file tree
Showing 26 changed files with 1,816 additions and 2,627 deletions.
73 changes: 37 additions & 36 deletions src/include/art.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ extern "C" {
#endif

#include <deque.h>
#include <value.h>

#include <stdint.h>

#define MAX_PREFIX_LEN 10

typedef int (*art_callback)(void* data, const unsigned char* key, uint32_t key_len, void* value);
typedef int (*art_callback)(void* data, const unsigned char* key, uint32_t key_len, struct value* value);

typedef void (*value_destroy_callback)(void* value);

Expand All @@ -50,31 +51,27 @@ struct art
{
struct art_node* root; /**< The root node of ART */
uint64_t size; /**< The size of the ART */
value_destroy_callback val_destroy_cb; /**< The callback for the value destroy */
};

/** @struct art_iterator
* Defines an art_iterator
*/
struct art_iterator
{
struct deque* que; /**< The deque */
struct art* tree; /**< The ART */
uint32_t count; /**< The count of the iterator */
unsigned char* key; /**< The key */
void* value; /**< The value */
struct deque* que; /**< The deque */
struct art* tree; /**< The ART */
uint32_t count; /**< The count of the iterator */
unsigned char* key; /**< The key */
struct value* value; /**< The value */
};

/**
* Initializes an adaptive radix tree
* @param tree [out] The tree
* @param val_destroy_cb The callback to destroy the val in leaf,
* simple free() is used as default if input is NULL.
* See pgmoneta_art_destroy_value_noop() if you don't need value to be freed.
* @return 0 on success, 1 if otherwise
*/
int
pgmoneta_art_init(struct art** tree, value_destroy_callback val_destroy_cb);
pgmoneta_art_init(struct art** tree);

/**
* Destroys an ART tree
Expand All @@ -85,38 +82,46 @@ pgmoneta_art_destroy(struct art* tree);

/**
* inserts a new value into the art tree, note that the key is copied while the value is not
* @param t the tree
* @param key the key
* @param key_len the length of the key
* @param value opaque value
* @return null if the item was newly inserted, otherwise
* the old value pointer is returned
* @param t The tree
* @param key The key
* @param key_len The length of the key
* @param value The value data
* @param type The value type
* @return 0 if the item was newly inserted, otherwise 1
*/
void*
pgmoneta_art_insert(struct art* t, unsigned char* key, uint32_t key_len, void* value);
int
pgmoneta_art_insert(struct art* t, unsigned char* key, uint32_t key_len, uintptr_t value, enum value_type type);

/**
* Deletes a value from the ART tree
* @param t The tree
* @param key The key
* @param key_len The length of the key
* @return NULL if the item was not found, otherwise
* the value pointer is returned
* @return 0 if success or value not found, 1 if otherwise
*/
void*
int
pgmoneta_art_delete(struct art* t, unsigned char* key, uint32_t key_len);

/**
* Searches for a value in the ART tree
* @param t The tree
* @param key The key
* @param key_len The length of the key
* @return NULL if the item was not found, otherwise
* the value pointer is returned
* @return NULL if the item was not found, otherwise the value pointer is returned
*/
void*
uintptr_t
pgmoneta_art_search(struct art* t, unsigned char* key, uint32_t key_len);

/**
* Check if a key exists in the ART tree
* @param t The tree
* @param key The key
* @param key_len The length of the key
* @return true if the key exists, false if otherwise
*/
bool
pgmoneta_art_contains_key(struct art* t, unsigned char* key, uint32_t key_len);

/**
* Iterates through the entries pairs in the map,
* invoking a callback for each. The call back gets a
Expand Down Expand Up @@ -163,18 +168,14 @@ bool
pgmoneta_art_iterator_has_next(struct art_iterator* iter);

/**
* The noop callback function for destroying value when destroying ART
* @param val The value
*/
void
pgmoneta_art_destroy_value_noop(void* val);

/**
* The default callback function for destroying value when destroying ART
* @param val The value
* Convert the ART tree to string
* @param t The ART tree
* @param tag The optional tag
* @param indent The indent
* @return The string
*/
void
pgmoneta_art_destroy_value_default(void* val);
char*
pgmoneta_art_to_string(struct art* t, char* tag, int indent);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion src/include/csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pgmoneta_csv_reader_init(char* path, struct csv_reader** reader);

/**
* Get the next row in csv file.
* You need to do free(cols) as the structure is allocated by the function
* You need to do free(cols) as the structure is allocated by the function
* @param reader The reader
* @param num_col [out] The number of columns in the row
* @param cols [out] The columns in the row
Expand Down
106 changes: 84 additions & 22 deletions src/include/deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
extern "C" {
#endif

#include <value.h>

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
Expand All @@ -42,9 +44,7 @@ extern "C" {
*/
struct deque_node
{
bool copied; /**< The flag if the value is copied */
void* data; /**< The data */
size_t data_size; /**< The data size */
struct value* data; /**< The value */
char* tag; /**< The tag */
struct deque_node* next; /**< The next pointer */
struct deque_node* prev; /**< The previous pointer */
Expand All @@ -62,6 +62,17 @@ struct deque
struct deque_node* end; /**< The end node */
};

/** @struct deque_iterator
* Defines a deque iterator
*/
struct deque_iterator
{
struct deque* deque; /**< The deque */
struct deque_node* cur; /**< The current deque node */
char* tag; /**< The current tag */
struct value* value; /**< The current value */
};

/**
* Create a deque
* @param thread_safe If the deque needs to be thread safe
Expand All @@ -71,28 +82,17 @@ struct deque
int
pgmoneta_deque_create(bool thread_safe, struct deque** deque);

/**
* Add a node to deque's tail, the data and tag will be copied.
* This function is thread safe
* @param deque The deque
* @param tag The tag, optional
* @param data The data
* @param data_size The size of data
* @return 0 if success, otherwise 1
*/
int
pgmoneta_deque_put(struct deque* deque, char* tag, void* data, size_t data_size);

/**
* Add a node to deque's tail, the tag will be copied, but the data will not
* This function is thread safe
* @param deque The deque
* @param tag The tag,optional
* @param data The data
* @param type The data type
* @return 0 if success, otherwise 1
*/
int
pgmoneta_deque_add(struct deque* deque, char* tag, void* data);
pgmoneta_deque_add(struct deque* deque, char* tag, uintptr_t data, enum value_type type);

/**
* Retrieve value and remove the node from deque's head.
Expand All @@ -102,9 +102,9 @@ pgmoneta_deque_add(struct deque* deque, char* tag, void* data);
* This function is thread safe, but the returned value is not protected
* @param deque The deque
* @param tag [out] Optional, tag will be returned through if not NULL
* @return The value if deque's not empty, otherwise NULL
* @return The value data if deque's not empty, otherwise 0
*/
void*
uintptr_t
pgmoneta_deque_poll(struct deque* deque, char** tag);

/**
Expand All @@ -115,20 +115,40 @@ pgmoneta_deque_poll(struct deque* deque, char** tag);
* This function is thread safe, but the returned value is not protected
* @param deque The deque
* @param tag [out] Optional, tag will be returned through if not NULL
* @return The value if deque's not empty, otherwise NULL
* @return The value data if deque's not empty, otherwise 0
*/
void*
uintptr_t
pgmoneta_deque_peek(struct deque* deque, char** tag);

/**
* Get the data for the specified tag
* @param deque The deque
* @param tag The tag
* @return The data, or NULL
* @return The data, or 0
*/
void*
uintptr_t
pgmoneta_deque_get(struct deque* deque, char* tag);

/**
* Check if deque contains certain tag
* @param deque The deque
* @param tag The tag
* @return true if the tag exists, false if otherwise
*/
bool
pgmoneta_deque_contains_tag(struct deque* deque, char* tag);

/**
* Set the value of a tag if it exists
* @param deque The deque
* @param tag The tag
* @param val The value data
* @param type The value type
* @return 0 if success, 1 if otherwise
*/
int
pgmoneta_deque_set(struct deque* deque, char* tag, uintptr_t val, enum value_type type);

/**
* Get the next deque node
* The function is thread safe for put/add but not for polling,
Expand Down Expand Up @@ -209,6 +229,48 @@ pgmoneta_deque_list(struct deque* deque);
void
pgmoneta_deque_destroy(struct deque* deque);

/**
* Convert what's inside deque to string
* @param deque The deque
* @param tag [Optional] The tag, which will be applied before the content if not null
* @param indent The current indentation
* @return The string
*/
char*
pgmoneta_deque_to_string(struct deque* deque, char* tag, int indent);

/**
* Create a deque iterator
* @param deque The deque
* @param iter [out] The iterator
* @return 0 on success, 1 if otherwise
*/
int
pgmoneta_deque_iterator_init(struct deque* deque, struct deque_iterator** iter);

/**
* Destroy a deque iterator
* @param iter The iterator
*/
void
pgmoneta_deque_iterator_destroy(struct deque_iterator* iter);

/**
* Get the next deque value
* @param iter The iterator
* @return true if has next, false if otherwise
*/
bool
pgmoneta_deque_iterator_next(struct deque_iterator* iter);

/**
* Check if deque has next value
* @param iter The iterator
* @return true if has next, false if otherwise
*/
bool
pgmoneta_deque_iterator_has_next(struct deque_iterator* iter);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 144f8f0

Please sign in to comment.