Skip to content

Commit

Permalink
[#463] Migrate JSON, deque, ART & value types(round 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jubilee101 committed Oct 25, 2024
1 parent 8801878 commit fe1693d
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Henrique de Carvalho <decarv.henrique@gmail.com>
Yihe Lu <t1t4m1un@gmail.com>
Eugenio Gigante <giganteeugenio2@gmail.com>
Mohanad Khaled <mohanadkhaled87@gmail.com>
Haoran Zhang <andrewzhr9911@gmail.com>
```

## Committers
Expand All @@ -45,8 +46,4 @@ community.

Consider giving the project a [star][star] on
[GitHub][pagmoneta] if you find it useful. And, feel free to follow
the project on [Twitter][twitter] as well.

## License

[BSD-3-Clause][license]
the project on [Twitter][twitter] as well.
78 changes: 78 additions & 0 deletions doc/manual/98-licenses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
\newpage

# License

```
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.
```

[BSD-3-Clause][license]


## libart
Our adaptive radix tree (ART) implementation is based on
[The Adaptive Radix Tree: ARTful Indexing for Main-Memory Databases][ART_paper]
and [libart][libart] which has a [3-BSD license][license] as

```
Copyright (c) 2012, Armon Dadgar
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the organization 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 ARMON DADGAR
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.
```
2 changes: 2 additions & 0 deletions doc/manual/99-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
[prometheus]: https://prometheus.io/
[wireshark]: https://www.wireshark.org/
[pgprtdbg]: https://github.com/jesperpedersen/pgprtdbg
[ART_paper]: http://www-db.in.tum.de/~leis/papers/ART.pdf
[libart]: https://github.com/armon/libart

<!-- doc/ -->
[rpm]: https://github.com/agroal/pgagroal/blob/main/doc/RPM.md
Expand Down
11 changes: 10 additions & 1 deletion src/include/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ pgagroal_json_put(struct json* item, char* key, uintptr_t val, enum value_type t
uintptr_t
pgagroal_json_get(struct json* item, char* tag);

/**
* Check if the json item contains the given key
* @param item The json item
* @param key The key
* @return True if the key exists, otherwise false
*/
bool
pgagroal_json_contains_key(struct json* item, char* key);

/**
* Append an entry into the json array
* If the entry is put into an empty json object, it will be treated as json array,
Expand Down Expand Up @@ -192,4 +201,4 @@ pgagroal_json_destroy(struct json* object);
}
#endif

#endif
#endif
1 change: 1 addition & 0 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ extern "C" {
#define INDENT_PER_LEVEL 2
#define FORMAT_JSON 0
#define FORMAT_TEXT 1
#define FORMAT_JSON_COMPACT 2
#define BULLET_POINT "- "

#define likely(x) __builtin_expect (!!(x), 1)
Expand Down
8 changes: 8 additions & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ pgagroal_indent(char* str, char* tag, int indent);
bool
pgagroal_compare_string(const char* str1, const char* str2);

/**
* Escape a string
* @param str The original string
* @return The escaped string
*/
char*
pgagroal_escape_string(char* str);

#ifdef DEBUG

/**
Expand Down
3 changes: 1 addition & 2 deletions src/include/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef PGAGROAL_VALUE_H
#define PGAGROAL_VALUE_H

Expand Down Expand Up @@ -59,7 +58,7 @@ enum value_type {
ValueDeque,
ValueART,
ValueRef,
ValueVerifyEntry,
ValueMem,
};

/**
Expand Down
75 changes: 72 additions & 3 deletions src/libpgagroal/art.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

#include <art.h>
#include <json.h>
#include <utils.h>

#include <stdbool.h>
Expand Down Expand Up @@ -292,9 +293,15 @@ art_to_json_string_cb(void* param, const unsigned char* key, uint32_t key_len, s
static int
art_to_text_string_cb(void* param, const unsigned char* key, uint32_t key_len, struct value* value);

static int
art_to_compact_json_string_cb(void* param, const unsigned char* key, uint32_t key_len, struct value* value);

static char*
to_json_string(struct art* t, char* tag, int indent);

static char*
to_compact_json_string(struct art* t, char* tag, int indent);

static char*
to_text_string(struct art* t, char* tag, int indent);

Expand Down Expand Up @@ -382,6 +389,10 @@ pgagroal_art_to_string(struct art* t, int32_t format, char* tag, int indent)
{
return to_text_string(t, tag, indent);
}
else if (format == FORMAT_JSON_COMPACT)
{
return to_compact_json_string(t, tag, indent);
}
return NULL;
}

Expand Down Expand Up @@ -1459,10 +1470,13 @@ art_to_json_string_cb(void* param, const unsigned char* key, uint32_t key_len, s
struct to_string_param* p = (struct to_string_param*) param;
char* str = NULL;
char* tag = NULL;
char* translated_key = NULL;
p->cnt++;
bool has_next = p->cnt < p->t->size;
tag = pgagroal_append_char(tag, '"');
tag = pgagroal_append(tag, (char*)key);
translated_key = pgagroal_escape_string((char*)key);
tag = pgagroal_append(tag, translated_key);
free(translated_key);
tag = pgagroal_append_char(tag, '"');
tag = pgagroal_append(tag, ": ");
str = pgagroal_value_to_string(value, FORMAT_JSON, tag, p->indent);
Expand All @@ -1474,6 +1488,30 @@ art_to_json_string_cb(void* param, const unsigned char* key, uint32_t key_len, s
return 0;
}

static int
art_to_compact_json_string_cb(void* param, const unsigned char* key, uint32_t key_len, struct value* value)
{
struct to_string_param* p = (struct to_string_param*) param;
char* str = NULL;
char* tag = NULL;
char* translated_key = NULL;
p->cnt++;
bool has_next = p->cnt < p->t->size;
tag = pgagroal_append_char(tag, '"');
translated_key = pgagroal_escape_string((char*)key);
tag = pgagroal_append(tag, (char*)translated_key);
free(translated_key);
tag = pgagroal_append_char(tag, '"');
tag = pgagroal_append(tag, ":");
str = pgagroal_value_to_string(value, FORMAT_JSON_COMPACT, tag, p->indent);
free(tag);
p->str = pgagroal_append(p->str, str);
p->str = pgagroal_append(p->str, has_next ? "," : "");

free(str);
return 0;
}

static int
art_to_text_string_cb(void* param, const unsigned char* key, uint32_t key_len, struct value* value)
{
Expand All @@ -1484,15 +1522,23 @@ art_to_text_string_cb(void* param, const unsigned char* key, uint32_t key_len, s
bool has_next = p->cnt < p->t->size;
tag = pgagroal_append(tag, (char*)key);
tag = pgagroal_append(tag, ": ");
if (value->type == ValueJSON)
if (value->type == ValueJSON && ((struct json*) value->data)->type != JSONUnknown)
{
tag = pgagroal_append(tag, "\n");
}
if (pgagroal_compare_string(p->tag, BULLET_POINT))
{
if (p->cnt == 1)
{
str = pgagroal_value_to_string(value, FORMAT_TEXT, tag, 0);
if (value->type != ValueJSON || ((struct json*) value->data)->type == JSONUnknown)
{
str = pgagroal_value_to_string(value, FORMAT_TEXT, tag, 0);
}
else
{
p->str = pgagroal_indent(p->str, tag, 0);
str = pgagroal_value_to_string(value, FORMAT_TEXT, NULL, p->indent + INDENT_PER_LEVEL);
}
}
else
{
Expand Down Expand Up @@ -1535,6 +1581,29 @@ to_json_string(struct art* t, char* tag, int indent)
return ret;
}

static char*
to_compact_json_string(struct art* t, char* tag, int indent)
{
char* ret = NULL;
ret = pgagroal_indent(ret, tag, indent);
if (t == NULL || t->size == 0)
{
ret = pgagroal_append(ret, "{}");
return ret;
}
ret = pgagroal_append(ret, "{");
struct to_string_param param = {
.indent = indent,
.str = ret,
.t = t,
.cnt = 0,
};
art_iterate(t, art_to_compact_json_string_cb, &param);
ret = param.str;
ret = pgagroal_append(ret, "}");
return ret;
}

static char*
to_text_string(struct art* t, char* tag, int indent)
{
Expand Down
43 changes: 43 additions & 0 deletions src/libpgagroal/deque.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ deque_find(struct deque* deque, char* tag);
static char*
to_json_string(struct deque* deque, char* tag, int indent);

static char*
to_compact_json_string(struct deque* deque, char* tag, int indent);

static char*
to_text_string(struct deque* deque, char* tag, int indent);

Expand Down Expand Up @@ -229,6 +232,10 @@ pgagroal_deque_to_string(struct deque* deque, int32_t format, char* tag, int ind
{
return to_text_string(deque, tag, indent);
}
else if (format == FORMAT_JSON_COMPACT)
{
return to_compact_json_string(deque, tag, indent);
}
return NULL;
}

Expand Down Expand Up @@ -462,6 +469,42 @@ to_json_string(struct deque* deque, char* tag, int indent)
return ret;
}

static char*
to_compact_json_string(struct deque* deque, char* tag, int indent)
{
char* ret = NULL;
ret = pgagroal_indent(ret, tag, indent);
struct deque_node* cur = NULL;
if (deque == NULL || pgagroal_deque_empty(deque))
{
ret = pgagroal_append(ret, "[]");
return ret;
}
deque_read_lock(deque);
ret = pgagroal_append(ret, "[");
cur = deque_next(deque, deque->start);
while (cur != NULL)
{
bool has_next = cur->next != deque->end;
char* str = NULL;
char* t = NULL;
if (cur->tag != NULL)
{
t = pgagroal_append(t, cur->tag);
t = pgagroal_append(t, ":");
}
str = pgagroal_value_to_string(cur->data, FORMAT_JSON_COMPACT, t, indent);
free(t);
ret = pgagroal_append(ret, str);
ret = pgagroal_append(ret, has_next ? "," : "");
free(str);
cur = deque_next(deque, cur);
}
ret = pgagroal_append(ret, "]");
deque_unlock(deque);
return ret;
}

static char*
to_text_string(struct deque* deque, char* tag, int indent)
{
Expand Down
Loading

0 comments on commit fe1693d

Please sign in to comment.