Skip to content

Commit

Permalink
Cmd sorted array basics (#239)
Browse files Browse the repository at this point in the history
* renaming intarray to sarray (sorted array)

* header changes done

* things compile now

* fix bug in request.c

* update func signature

* Squashed 'deps/ccommon/' changes from f5efe29..2a62281

2a62281 add atoi64 to bstring (#206)
f71c657 cc_option simplify _allowed_in_name (#205)
24e3131 Add ITT instrumentation option (#204)
236c98d Fix docs (#200)
e58f6a8 cc_array and cc_ring_array NULL fixes (#201)
1c8df42 Add basic support of build type (#199)
7107988 Fix now_ns() (#198)
da240e5 cc: extend cc_util module (#196)
4846b15 Fix TAILQ_REINIT (#195)
4f5dbb0 Update Cmake version to 2.8 (#197)
2e6f78a cc_mm use OS_DARWIN macro to detect OS (#194)
57acaf6 cc: extend queue module (#193)
a64ada2 cc: extend duration module (#192)
b117632 reverting CMake file changes (#191)
dea5bee backport changes made to ccommon in pelikan (#190)
a4c0334 add linebreak to stats_log() (#188)
05eb03e fix inconsistent naming and bump version (#187)
4acc53a Stats to file (#186)
2168fec minimize osx build config (#185)
42b24de Simplify rust options, specify fewer output targets (#183)
c9fa905 update CMakeRust used to latest version, tweaks to make build work (#184)
2ef0163 Reorder dependency includes in cmake, don't parallel build (#182)
a6a54d9 remove endian-specific logic from str*cmp (#177)
4c0668b epoll_create* ignores size hint in newer kernels, switch to new API (#179)
c9c5ee5 improve cc_bstring string literal and cstring names (#176)
0184d73 Add unit tests for buffer, fix buf/dbuf bugs and refactor (#174)
d7dab43 create a .cargo/config so intellij uses the same target dir as cmake (#173)
e710712 use accept4 for tcp_accept when available (#171)
21ba10e Remove cargo lock for shared lib, closes #169 (#172)
24660f1 update style guide (#170)
17baf1e Per thread logging (#168)

git-subtree-dir: deps/ccommon
git-subtree-split: 2a62281

* fix sarray bugs
  • Loading branch information
Yao Yue authored Aug 4, 2019
1 parent d5849d3 commit d279cbe
Show file tree
Hide file tree
Showing 36 changed files with 1,201 additions and 374 deletions.
10 changes: 5 additions & 5 deletions deps/ccommon/docs/modules/cc_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Synopsis
void log_flush(struct logger *logger);
rstatus_i log_reopen(struct logger *logger);
rstatus_i log_reopen(struct logger *logger, char *target);
Description
-----------
Expand Down Expand Up @@ -95,18 +95,18 @@ Flush to file
^^^^^^^^^^^^^
.. code-block:: C
void log_flush(struct logger *logger);
size_t log_flush(struct logger *logger);
``log_flush`` writes as much data to the log file as possible, and updates the (read) marker in the ring buffer. Data that cannot be written to the file will be kept until next call. If the ring buffer or the file was never setup, no action is taken.
``log_flush`` writes as much data to the log file as possible, and updates the (read) marker in the ring buffer. Data that cannot be written to the file will be kept until next call. If the ring buffer or the file was never setup, no action is taken. Return the number of bytes flushed.


Log reopen
^^^^^^^^^^
.. code-block:: C
rstatus_i log_reopen(struct logger *logger);
rstatus_i log_reopen(struct logger *logger, char *target);
``log_reopen`` reopens the log file according to ``name``, and does nothing if standard outputs are used. It returns ``CC_OK`` for success or ``CC_ERROR`` if reopen failed (at which point ``logger`` will no longer have a valid ``fd``).
``log_reopen`` reopens the log file according to ``name``, and does nothing if standard outputs are used. It returns ``CC_OK`` for success or ``CC_ERROR`` if reopen failed (at which point ``logger`` will no longer have a valid ``fd``). If ``target`` is specified function will rename original log file to the provided target filename and reopen the log file.

This function can be used to reopen the log file when an exception has happened, or another party such as ``logrotate`` instructs the application to do so. Log rotation in a ``nocopytruncate`` manner- i.e. the content in the file is not copied, but the file is simply renamed- is more efficient in high-load systems. But doing so requires signaling the application to reopen the log file after renaming. This function makes it possible to achieve that when used with proper signal handling.

Expand Down
4 changes: 2 additions & 2 deletions deps/ccommon/docs/modules/cc_metric.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ Helper functions
.. code-block:: C
void metric_reset(struct metric sarr[], unsigned int nmetric);
size_t metric_print(char *buf, size_t nbuf, struct metric *m);
size_t metric_print(char *buf, size_t nbuf, char *fmt, struct metric *m);
``metric_reset`` resets the values of an array of metrics.
``metric_print`` prints the name and value of a metric, in human readable format, to buffer ``buf``, with a single space separating the two fields. This simple style is compatible with how Memcached currently reports metrics ([Memcached]_). Helper functions for other formats (e.g. Redis [Redis]_, StatsD [StatsD]_) may be introduced in the future.
``metric_print`` prints the name and value of a metric, in human readable format specified by ``fmt``, to buffer ``buf``.


Update
Expand Down
12 changes: 7 additions & 5 deletions deps/ccommon/docs/modules/cc_option.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ Data Structure
typedef enum option_type {
OPTION_TYPE_BOOL,
OPTION_TYPE_UINT,
OPTION_TYPE_FPN,
OPTION_TYPE_STR,
OPTION_TYPE_SENTINEL
} option_type_e;
typedef union option_val {
bool vbool;
uintmax_t vuint;
double vfpn;
char *vstr;
} option_val_u;
Expand All @@ -59,7 +61,7 @@ Data Structure
char *description;
};
The core data structure ``struct option`` has six members. ``name`` and ``description`` help identify and explain the purpose of the option. ``type`` decides how input should be interpreted, which currently can be boolean, unsigned integer or C string. Both the default value and current value are kept around, with values matching the type. Keeping the default separately will make it easy to reset the option to original. Finally, boolean ``set`` tells if an option has been set, and thus usable.
The core data structure ``struct option`` has six members. ``name`` and ``description`` help identify and explain the purpose of the option. ``type`` decides how input should be interpreted, which currently can be boolean, unsigned integer, double or C string. Both the default value and current value are kept around, with values matching the type. Keeping the default separately will make it easy to reset the option to original. Finally, boolean ``set`` tells if an option has been set, and thus usable.

Synopsis
--------
Expand All @@ -71,8 +73,8 @@ Synopsis
rstatus_i option_load_file(FILE *fp, struct option options[], unsigned int nopt);
void option_print(struct option *opt);
void option_printall(struct option options[], unsigned int nopt);
void option_printall_default(struct option options[], unsigned int nopt);
void option_print_all(struct option options[], unsigned int nopt);
void option_describe_all(struct option options[], unsigned int nopt);
void option_free(struct option options[], unsigned int nopt);
Expand Down Expand Up @@ -118,8 +120,8 @@ Print option info
.. code-block:: C
void option_print(struct option *opt);
void option_printall(struct option options[], unsigned int nopt);
void option_printall_default(struct option options[], unsigned int nopt);
void option_print_all(struct option options[], unsigned int nopt);
void option_describe_all(struct option options[], unsigned int nopt);
Examples
Expand Down
1 change: 1 addition & 0 deletions deps/ccommon/include/cc_bstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void bstring_free(struct bstring **bstring);

/* bstring to uint conversion */
rstatus_i bstring_atou64(uint64_t *u64, struct bstring *str);
rstatus_i bstring_atoi64(int64_t *i64, struct bstring *str);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion deps/ccommon/src/cc_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ array_create(struct array **arr, uint32_t nalloc, size_t size)
ASSERT(nalloc != 0 && size != 0);

*arr = (struct array *)cc_alloc(sizeof(**arr));
if (arr == NULL) {
if (*arr == NULL) {
log_info("array creation failed due to OOM");

return CC_ENOMEM;
Expand Down
40 changes: 40 additions & 0 deletions deps/ccommon/src/cc_bstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,46 @@ bstring_compare(const struct bstring *s1, const struct bstring *s2)
return cc_bcmp(s1->data, s2->data, s1->len);
}

rstatus_i
bstring_atoi64(int64_t *i64, struct bstring *str)
{
uint32_t offset = 0;
uint8_t c;
int64_t sign = 1;

if (str->len == 0 || str->len >= CC_INT64_MAXLEN) {
return CC_ERROR;
}

if (*str->data == '-') {
offset = 1;
sign = -1;
}

for (*i64 = 0LL; offset < str->len; offset++) {
c = *(str->data + offset);
if (c < '0' || c > '9') {
return CC_ERROR;
}

// overflow check
if (offset == CC_INT64_MAXLEN - 2) {
if (sign < 0 && *i64 == INT64_MIN / 10 &&
c - '0' > (uint64_t)(-INT64_MIN) % 10) {
return CC_ERROR;
}
if (sign > 0 && *i64 == INT64_MAX / 10 &&
c - '0' > INT64_MAX % 10) {
return CC_ERROR;
}
}

*i64 = *i64 * 10LL + sign * (int64_t)(c - '0');
}

return CC_OK;
}

rstatus_i
bstring_atou64(uint64_t *u64, struct bstring *str)
{
Expand Down
3 changes: 1 addition & 2 deletions deps/ccommon/src/cc_option.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,7 @@ static inline bool
_allowed_in_name(char c)
{
/* the criteria is C's rules on variable names since we use it as such */
if ((c >= 'a' && c <= 'z') || c == '_' || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')) {
if (isalnum(c) || c == '_') {
return true;
} else {
return false;
Expand Down
4 changes: 2 additions & 2 deletions deps/ccommon/src/cc_ring_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ ring_array_create(size_t elem_size, uint32_t cap)
void
ring_array_destroy(struct ring_array **arr)
{
log_verb("destroying ring array %p and freeing memory", *arr);

if ((arr == NULL) || (*arr == NULL)) {
log_warn("destroying NULL ring_array pointer");
return;
}

log_verb("destroying ring array %p and freeing memory", *arr);

cc_free(*arr);
*arr = NULL;
}
32 changes: 32 additions & 0 deletions deps/ccommon/test/bstring/check_bstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@ START_TEST(test_strcmp)
END_TEST


START_TEST(test_atoi64)
{
int64_t val;
struct bstring bstr;
char int64[CC_INT64_MAXLEN];

test_reset();

ck_assert_int_eq(bstring_atoi64(&val, &str2bstr("foo")), CC_ERROR);

ck_assert_int_eq(bstring_atoi64(&val, &str2bstr("123")), CC_OK);
ck_assert_uint_eq(val, 123);
ck_assert_int_eq(bstring_atoi64(&val, &str2bstr("-123")), CC_OK);
ck_assert_uint_eq(val, -123);

sprintf(int64, "%"PRIi64, INT64_MAX);
bstring_init(&bstr);
ck_assert_int_eq(bstring_copy(&bstr, int64, strlen(int64)), CC_OK);
ck_assert_int_eq(bstring_atoi64(&val, &bstr), CC_OK);
ck_assert_int_eq(val, INT64_MAX);
bstring_deinit(&bstr);

sprintf(int64, "%"PRIi64, INT64_MIN);
bstring_init(&bstr);
ck_assert_int_eq(bstring_copy(&bstr, int64, strlen(int64)), CC_OK);
ck_assert_int_eq(bstring_atoi64(&val, &bstr), CC_OK);
ck_assert_int_eq(val, INT64_MIN);
bstring_deinit(&bstr);
}
END_TEST

START_TEST(test_atou64)
{
uint64_t val;
Expand Down Expand Up @@ -189,6 +220,7 @@ bstring_suite(void)
tcase_add_test(tc_bstring, test_copy);
tcase_add_test(tc_bstring, test_compare);
tcase_add_test(tc_bstring, test_strcmp);
tcase_add_test(tc_bstring, test_atoi64);
tcase_add_test(tc_bstring, test_atou64);
tcase_add_test(tc_bstring, test_bstring_alloc_and_free);

Expand Down
2 changes: 1 addition & 1 deletion src/data_structure/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_subdirectory(bitmap)
add_subdirectory(intarray)
add_subdirectory(sarray)
add_subdirectory(ziplist)
1 change: 0 additions & 1 deletion src/data_structure/intarray/CMakeLists.txt

This file was deleted.

103 changes: 0 additions & 103 deletions src/data_structure/intarray/intarray.h

This file was deleted.

1 change: 1 addition & 0 deletions src/data_structure/sarray/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_library(ds_sarray sarray.c)
Loading

0 comments on commit d279cbe

Please sign in to comment.