Skip to content

Commit

Permalink
array subobject: implement overwriting keys and reusing deleted space
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerhards committed Apr 12, 2016
1 parent 20fc9f3 commit 6e3f511
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
30 changes: 22 additions & 8 deletions json_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,12 @@ static void fjson_object_generic_delete(struct fjson_object* jso)
static struct fjson_object* fjson_object_new(const enum fjson_type o_type)
{
struct fjson_object *const jso = (struct fjson_object*)calloc(sizeof(struct fjson_object), 1);
//fprintf(stderr, "new 1, jso %p\n", jso);
if (!jso)
return NULL;
jso->o_type = o_type;
jso->_ref_count = 1;
jso->_delete = &fjson_object_generic_delete;
INIT_ATOMIC_HELPER_MUT(jso->_mut_ref_count);
//fprintf(stderr, "new ret, jso %p\n", jso);
return jso;
}

Expand Down Expand Up @@ -422,7 +420,6 @@ _fjson_find_child(struct fjson_object *const __restrict__ jso,
struct fjson_object_iterator it = fjson_object_iter_begin(jso);
struct fjson_object_iterator itEnd = fjson_object_iter_end(jso);
while (!fjson_object_iter_equal(&it, &itEnd)) {
fprintf(stderr, "base pg: %p --> pg: %p, curr_idx %d, remain %d\n", &jso->o.c_obj.pg, it.pg, it.curr_idx, it.objs_remain);
if (!strcmp (key, fjson_object_iter_peek_name(&it)))
return _fjson_object_iter_peek_child(&it);
fjson_object_iter_next(&it);
Expand All @@ -438,8 +435,26 @@ fjson_child_get_empty_etry(struct fjson_object *const __restrict__ jso)
{
struct _fjson_child *chld = NULL;
struct _fjson_child_pg *pg;

if (jso->o.c_obj.ndeleted > 0) {
/* we first fill deleted spots */
pg = &jso->o.c_obj.pg;
while (chld == NULL) {
for (int i = 0 ; i < FJSON_OBJECT_CHLD_PG_SIZE ; ++i) {
if(pg->children[i].k == NULL) {
chld = &(pg->children[i]);
--jso->o.c_obj.ndeleted;
goto done;
}
}
pg = pg->next;
}
/* if we reach this point, we have a program error */
assert(0);
goto done;
}

const int pg_idx = jso->o.c_obj.nelem % FJSON_OBJECT_CHLD_PG_SIZE;
//fprintf(stderr, "get_etry: pg_idx %d, nelem %d\n", pg_idx, jso->o.c_obj.nelem );
if (jso->o.c_obj.nelem > 0 && pg_idx == 0) {
if((pg = calloc(1, sizeof(struct _fjson_child_pg))) == NULL) {
errno = ENOMEM;
Expand All @@ -449,13 +464,12 @@ fjson_child_get_empty_etry(struct fjson_object *const __restrict__ jso)
jso->o.c_obj.lastpg = pg;
}
pg = jso->o.c_obj.lastpg;
//fprintf(stderr, "get_etry: existing chld, k %p\n", pg->children[pg_idx].k);
if (pg->children[pg_idx].k == NULL) {
/* we can use this spot and save us search time */
chld = &(pg->children[pg_idx]);
//fprintf(stderr, "get_etry: existing chld pg %p, idx: %d\n", pg, pg_idx);
goto done;
}

done: return chld;
}

Expand All @@ -464,7 +478,6 @@ void fjson_object_object_add_ex(struct fjson_object *const __restrict__ jso,
struct fjson_object *const val,
const unsigned opts)
{
fprintf(stderr, "_add_ex, key %s\n", key);
// We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid.
struct _fjson_child *chld;
Expand All @@ -483,7 +496,6 @@ fprintf(stderr, "_add_ex, key %s\n", key);
chld->flags.k_is_constant = (opts & FJSON_OBJECT_KEY_IS_CONSTANT) != 0;
chld->v = val;
++jso->o.c_obj.nelem;
//fprintf(stderr, "_add_ex, key %s added, nelem %d\n", key, jso->o.c_obj.nelem);

done:
return;
Expand Down Expand Up @@ -538,9 +550,11 @@ void fjson_object_object_del(struct fjson_object* jso, const char *key)
if (chld != NULL) {
free((void*)chld->k);
fjson_object_put(chld->v);
chld->flags.k_is_constant = 0;
chld->k = NULL;
chld->v = NULL;
--jso->o.c_obj.nelem;
++jso->o.c_obj.ndeleted;
}
}

Expand Down
1 change: 1 addition & 0 deletions json_object_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct fjson_object
int64_t c_int64;
struct {
int nelem;
int ndeleted;
struct _fjson_child_pg pg;
struct _fjson_child_pg *lastpg;
} c_obj;
Expand Down

0 comments on commit 6e3f511

Please sign in to comment.