|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|