Refactor to create hash_index_free_composite, start work to have multiple trie levels in same file

master
teknomunk 9 months ago
parent 06066e9de2
commit 87d5d24625

@ -43,6 +43,7 @@ static void hash_index_entry_free( struct hash_index_entry* e )
free(e->key);
free(e);
}
JSON_FIELD_TYPE_OBJECT_LAYOUT_WITH_DEFAULTS( hash_index_entry );
struct hash_index
@ -53,6 +54,15 @@ struct hash_index
} entries;
};
static void hash_index_free_composite( struct hash_index* hi )
{
for( int i = 0; i < hi->entries.count; ++i ) {
struct hash_index_entry* entry = hi->entries.items[i];
hash_index_entry_free(entry);
}
free(hi->entries.items);
}
#define OBJ_TYPE struct hash_index
static struct json_object_field hash_index_layout[] = {
JSON_FIELD_ARRAY_OF_TYPE( entries, true, hash_index_entry_type ),
@ -94,11 +104,7 @@ bool hash_index_set( const char* index, const char* key, int value )
exists:
json_write_object_layout_to_file( filename, "\t", hash_index_layout, &hi );
for( int i = 0; i < hi.entries.count; ++i ) {
struct hash_index_entry* entry = hi.entries.items[i];
hash_index_entry_free(entry);
}
free(hi.entries.items);
hash_index_free_composite( &hi );
return true;
}
bool hash_index_remove( const char* index, const char* key )
@ -136,11 +142,7 @@ exists:
json_write_object_layout_to_file( filename, "\t", hash_index_layout, &hi );
for( int i = 0; i < hi.entries.count; ++i ) {
struct hash_index_entry* entry = hi.entries.items[i];
hash_index_entry_free(entry);
}
free(hi.entries.items);
hash_index_free_composite( &hi );
return true;
}
@ -175,11 +177,34 @@ bool hash_index_get( const char* index, const char* key, int* value )
*value = entry->val;
found = true;
}
free(entry->key);
free(entry);
}
free(hi.entries.items);
hash_index_free_composite(&hi);
return found;
}
char* ffdb_hash_get_key( const char* index, int slot, int offset )
{
char* result = NULL;
char filename[512];
snprintf( filename,512, "%s/%05x.json", index, slot );
struct hash_index hi;
memset( &hi, 0, sizeof(hi) );
if( !json_read_object_layout_from_file( filename, hash_index_layout, &hi ) ) {
goto failed;
}
if( offset >= hi.entries.count ) { goto failed; }
result = strdup( hi.entries.items[offset]->key );
cleanup:
hash_index_free_composite( &hi );
return result;
failed:
free(result);
result = NULL;
goto cleanup;
}

@ -10,4 +10,5 @@ bool hash_index_remove( const char* index, const char* key );
bool ffdb_hash_set( const char* index, const char* key, const char* value );
char* ffdb_hash_get( const char* index, const char* key );
bool ffdb_hash_remove( const char* index, const char* key );
char* ffdb_hash_get_key( const char* index, int slot, int offset );

@ -84,13 +84,32 @@ static bool trie_entry_load( FILE* f, struct trie_entry* e )
memset(ed,0,sizeof(*ed));
ed->label = edge_label;
ed->count = 1;
if( !json_pull_parser_read_int( &jpp, &ed->count ) ) {
char* value = json_pull_parser_read_string(&jpp);
if( !value ) { goto failed; }
if( json_pull_parser_read_int( &jpp, &ed->count ) ) {
continue;
}
char* value = json_pull_parser_read_string(&jpp);
if( value ) {
ed->value = value;
ed->count = 1;
continue;
}
struct trie_entry* child = NULL;
child = malloc(sizeof(*child));
memset(child,0,sizeof(*child));
if( trie_entry_load( f, child ) ) {
ed->child_trie = child;
ed->count = 0;
for( int i = 0; i < child->edges.count; ++i ) {
ed->count += child->edges.items[i].count;
}
continue;
}
free(child);
goto failed;
}
if( !json_pull_parser_end_object(&jpp,&save) ) { goto failed; }

Loading…
Cancel
Save