Skip to content

Commit

Permalink
[#463] Migrate JSON, deque, ART & value types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jubilee101 committed Sep 15, 2024
1 parent 8b4b7ed commit f7c5e69
Show file tree
Hide file tree
Showing 15 changed files with 4,048 additions and 263 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Ashutosh Sharma <[email protected]>
Henrique de Carvalho <[email protected]>
Yihe Lu <[email protected]>
Eugenio Gigante <[email protected]>
Haoran Zhang <[email protected]>
164 changes: 164 additions & 0 deletions src/include/art.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright (C) 2024 The pgagroal community
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef PGAGROAL_ART_H
#define PGAGROAL_ART_H

#ifdef __cplusplus
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, struct value* value);

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

/** @struct art
* The ART tree
*/
struct art
{
struct art_node* root; /**< The root node of ART */
uint64_t size; /**< The size of the ART */
};

/** @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 */
struct value* value; /**< The value */
};

/**
* Initializes an adaptive radix tree
* @param tree [out] The tree
* @return 0 on success, 1 if otherwise
*/
int
pgagroal_art_create(struct art** tree);

/**
* inserts a new value into the art tree, note that the key is copied
* @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 successfully inserted, otherwise 1
*/
int
pgagroal_art_insert(struct art* t, unsigned char* key, uint32_t key_len, uintptr_t value, enum value_type type);

/**
* 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
pgagroal_art_contains_key(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
*/
uintptr_t
pgagroal_art_search(struct art* t, unsigned char* key, uint32_t key_len);

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

/**
* Get the next key value pair into iterator
* @param iter The iterator
* @return true if iterator has next, otherwise false
*/
bool
pgagroal_art_iterator_next(struct art_iterator* iter);

/**
* Create an art iterator
* @param t The tree
* @param iter [out] The iterator
* @return 0 if success, otherwise 1
*/
int
pgagroal_art_iterator_create(struct art* t, struct art_iterator** iter);

/**
* Destroy the iterator
* @param iter The iterator
*/
void
pgagroal_art_iterator_destroy(struct art_iterator* iter);

/**
* Convert the ART tree to string
* @param t The ART tree
* @param format The format
* @param tag The optional tag
* @param indent The indent
* @return The string
*/
char*
pgagroal_art_to_string(struct art* t, int32_t format, char* tag, int indent);

/**
* Destroys an ART tree
* @return 0 on success, 1 if otherwise
*/
int
pgagroal_art_destroy(struct art* tree);

#ifdef __cplusplus
}
#endif

#endif
207 changes: 207 additions & 0 deletions src/include/deque.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* Copyright (C) 2024 The pgagroal community
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PGAGROAL_DEQUE_H
#define PGAGROAL_DEQUE_H

#ifdef __cplusplus
extern "C" {
#endif

#include <value.h>

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>

/** @struct deque_node
* Defines a deque node
*/
struct deque_node
{
struct value* data; /**< The value */
char* tag; /**< The tag */
struct deque_node* next; /**< The next pointer */
struct deque_node* prev; /**< The previous pointer */
};

/** @struct deque
* Defines a deque
*/
struct deque
{
uint32_t size; /**< The size of the deque */
bool thread_safe; /**< If the deque is thread safe */
pthread_rwlock_t mutex; /**< The mutex of the deque */
struct deque_node* start; /**< The start node */
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
* @param deque The deque
* @return 0 if success, otherwise 1
*/
int
pgagroal_deque_create(bool thread_safe, struct deque** deque);

/**
* 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
pgagroal_deque_add(struct deque* deque, char* tag, uintptr_t data, enum value_type type);

/**
* Retrieve value and remove the node from deque's head.
* Note that if the value was copied into node,
* this function will return the original value and tag
* rather than making a copy of it.
* 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 data if deque's not empty, otherwise 0
*/
uintptr_t
pgagroal_deque_poll(struct deque* deque, char** tag);

/**
* Retrieve value without removing the node from deque's head.
* Note that if the value was copied into node,
* this function will return the original value and tag
* rather than making a copy of it.
* 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 data if deque's not empty, otherwise 0
*/
uintptr_t
pgagroal_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 0
*/
uintptr_t
pgagroal_deque_get(struct deque* deque, char* tag);

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

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

/**
* Remove the current node iterator points to and place the iterator to the previous node
* @param iter The iterator
*/
void
pgagroal_deque_iterator_remove(struct deque_iterator* iter);

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

/**
* Get the size of the deque
* @param deque The deque
* @return The size
*/
uint32_t
pgagroal_deque_size(struct deque* deque);

/**
* Check if the deque is empty
* @param deque The deque
* @return true if deque size is 0, otherwise false
*/
bool
pgagroal_deque_empty(struct deque* deque);

/**
* List the nodes in the deque
* @param deque The deque
*/
void
pgagroal_deque_list(struct deque* deque);

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

/**
* Destroy the deque and free its and its nodes' memory
* @param deque The deque
*/
void
pgagroal_deque_destroy(struct deque* deque);

#ifdef __cplusplus
}
#endif

#endif
Loading

0 comments on commit f7c5e69

Please sign in to comment.