summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-08-29 14:57:58 +0800
committerchenzizhan <[email protected]>2023-08-29 14:57:58 +0800
commita1a3eb6c43cca9eb8d3ce83b9dd10c5e4b6100bc (patch)
tree5fd2c5f803dce3ebf567705490b036b2655b5ce3
parentc5a7ce66090640f81f7a6f7cbaba26d45d2e4602 (diff)
feat and test: fieldstat_cube_remove
-rw-r--r--include/fieldstat/fieldstat_exporter.h8
-rw-r--r--src/fieldstat.c9
-rw-r--r--src/tags/cell_manager.c23
-rw-r--r--src/tags/cell_manager.h4
-rw-r--r--test/test_register_and_reset.cpp54
5 files changed, 87 insertions, 11 deletions
diff --git a/include/fieldstat/fieldstat_exporter.h b/include/fieldstat/fieldstat_exporter.h
index fa24a68..d18d9b0 100644
--- a/include/fieldstat/fieldstat_exporter.h
+++ b/include/fieldstat/fieldstat_exporter.h
@@ -78,7 +78,13 @@ void fieldstat_json_exporter_free(struct fieldstat_json_exporter *exporter);
Output the fieldstat instance to json string array. User must free the output string.
*/
char *fieldstat_json_exporter_export(const struct fieldstat_json_exporter *exporter);
-
+void fieldstat_json_exporter_export_array(const struct fieldstat_json_exporter *exporter, char **output, size_t *output_size);
+// for (size_t i = 0; i < output_size; i++)
+// {
+// free(output[i]);
+// }
+// free(output);
+// strlen(output[i]);
#ifdef __cplusplus
}
diff --git a/src/fieldstat.c b/src/fieldstat.c
index a9ec70b..4c40fa1 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -336,22 +336,20 @@ int fieldstat_cube_remove(struct fieldstat *instance, int cube_id, const struct
if (cube_id < 0 || cube_id >= instance->valid_cube_arr_length) {
return -2;
}
-
struct fs_cube *cube = instance->cube[cube_id];
if (cube == NULL) {
return -2;
}
-
if (cube->sampling_mode == SAMPLING_MODE_TOPK) {
return -3;
}
-
if (n_tag == 0 || tags == NULL) {
tags = NULL;
n_tag = 0;
}
+
struct tag_hash_key *tag_key = tag_hash_key_construct_with_fieldstat_tag(tags, n_tag);
- int id = cell_manager_find(cube->cell_manager, tag_key);
+ int id = cell_manager_delete_cell(cube->cell_manager, tag_key);
if (id == -1) {
tag_hash_key_free(tag_key);
return -1;
@@ -360,10 +358,9 @@ int fieldstat_cube_remove(struct fieldstat *instance, int cube_id, const struct
for (size_t i = 0; i < cube->n_metric; i++) {
metric_delete_cell(cube->metrics[i], id);
}
- cell_manager_delete_cell(cube->cell_manager, id);
tag_hash_key_free(tag_key);
- return 0;
+ return id;
}
void fieldstat_cube_free(struct fieldstat *instance, int cube_id)
diff --git a/src/tags/cell_manager.c b/src/tags/cell_manager.c
index 1556670..821b87b 100644
--- a/src/tags/cell_manager.c
+++ b/src/tags/cell_manager.c
@@ -203,6 +203,27 @@ The number 8 was determined through experiment, where multiple tests were conduc
return pthis->next_cell_id++;
}
+int cell_manager_delete_cell(struct cell_manager *pthis, const struct tag_hash_key *tag)
+{
+ if (pthis->sampling_mode == SAMPLING_MODE_TOPK) {
+ return -1;
+ }
+
+ struct tag_id_map *node = NULL;
+ HASH_FIND_TAG(pthis->comprehensive_tag_id_map, tag, node);
+ if (node == NULL) {
+ return -1;
+ }
+
+ int cell_id = node->cell_id;
+
+ HASH_DEL(pthis->comprehensive_tag_id_map, node);
+ tag_hash_key_free(node->tag);
+ free(node);
+ pthis->id_tag_array[cell_id] = NULL;
+
+ return cell_id;
+}
/*
mode:uint32 | max_n_cell:uint32 | id_tag_id_array:int_array | id_tag_array:bin_array(tag_hash_key_serialize) |
@@ -355,7 +376,7 @@ const struct tag_hash_key **cell_manager_dump(const struct cell_manager *pthis,
return (const struct tag_hash_key **)pthis->id_tag_array;
}
-int cell_manager_find_cell_id(const struct cell_manager *pthis, const struct tag_hash_key *tag)
+int cell_manager_find(const struct cell_manager *pthis, const struct tag_hash_key *tag)
{
if (pthis->sampling_mode == SAMPLING_MODE_COMPREHENSIVE) {
struct tag_id_map *node = NULL;
diff --git a/src/tags/cell_manager.h b/src/tags/cell_manager.h
index 46c1d36..4738338 100644
--- a/src/tags/cell_manager.h
+++ b/src/tags/cell_manager.h
@@ -16,11 +16,11 @@ void cell_manager_reset(struct cell_manager *pthis);
const struct tag_hash_key *cell_manager_get_tag_by_cell_id(const struct cell_manager *pthis, int cell_id);
int cell_manager_get_count_by_tag(const struct cell_manager *pthis, const struct tag_hash_key *tag);
const struct tag_hash_key **cell_manager_dump(const struct cell_manager *pthis, int *array_len);
-int cell_manager_find_cell_id(const struct cell_manager *pthis, const struct tag_hash_key *tag);
+int cell_manager_find(const struct cell_manager *pthis, const struct tag_hash_key *tag);
int cell_manager_add_cell(struct cell_manager *pthis, const struct tag_hash_key *tag);
int cell_manager_add_cell_topk(struct cell_manager *pthis, const struct tag_hash_key *tag, unsigned int count, int *popped_cell_id, int *existing_cell_id);
-void cell_manager_delete_cell(struct cell_manager *pthis, int cell_id);
+int cell_manager_delete_cell(struct cell_manager *pthis, const struct tag_hash_key *tag);
int cell_manager_serialize(const struct cell_manager *pthis, char **blob, size_t *blob_size);
struct cell_manager *cell_manager_deserialize(const char *blob, size_t blob_size);
diff --git a/test/test_register_and_reset.cpp b/test/test_register_and_reset.cpp
index 9a65e8d..82d20cc 100644
--- a/test/test_register_and_reset.cpp
+++ b/test/test_register_and_reset.cpp
@@ -256,7 +256,59 @@ TEST(test_register, register_many_cells_on_unlimited_sized_cube)
fieldstat_free(instance);
}
-// TEST(test_register, unregister cell)
+
+TEST(test_register, unregister_cell)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_register_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
+ int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM);
+ int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1);
+ int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1);
+ fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id1, 1);
+ fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id2, 1);
+
+ int cell_id_removed = fieldstat_cube_remove(instance, cube_id, &TEST_TAG_INT, 1);
+ EXPECT_EQ(cell_id_removed, cell_id1);
+ EXPECT_EQ(fieldstat_counter_get(instance, cube_id, metric_id, cell_id1), -1);
+
+ int *cell_ids = NULL;
+ struct fieldstat_tag_list *tag_list = NULL;
+ size_t n_cell = 0;
+ fieldstat_cube_read_cell(instance, cube_id, &cell_ids, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 1);
+ EXPECT_EQ(cell_ids[0], cell_id2);
+ EXPECT_EQ(tag_list[0].tag->type, TEST_TAG_DOUBLE.type);
+ EXPECT_STREQ(tag_list[0].tag->key, TEST_TAG_DOUBLE.key);
+ free(cell_ids);
+ fieldstat_tag_list_arr_free(tag_list, n_cell);
+
+ // test merge and serialize to make sure no changes on basic functions
+ struct fieldstat *instance_dst = fieldstat_new();
+ fieldstat_merge(instance_dst, instance);
+ fieldstat_cube_read_cell(instance, cube_id, &cell_ids, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 1);
+ EXPECT_STREQ(tag_list[0].tag->key, TEST_TAG_DOUBLE.key);
+ fieldstat_free(instance_dst);
+ free(cell_ids);
+ fieldstat_tag_list_arr_free(tag_list, n_cell);
+
+ char *blob;
+ size_t blob_size;
+ fieldstat_serialize(instance, &blob, &blob_size);
+ struct fieldstat *instance_ser = fieldstat_deserialize(blob, blob_size);
+ free(blob);
+ fieldstat_cube_read_cell(instance_ser, cube_id, &cell_ids, &tag_list, &n_cell);
+ EXPECT_EQ(n_cell, 1);
+ EXPECT_STREQ(tag_list[0].tag->key, TEST_TAG_DOUBLE.key);
+ fieldstat_free(instance_ser);
+ free(cell_ids);
+ fieldstat_tag_list_arr_free(tag_list, n_cell);
+
+ // completely new tag even with original tag. New posision(not original 0)
+ EXPECT_EQ(fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1), 2);
+
+ fieldstat_free(instance);
+}