Move hash index to ffdb

master
teknomunk 1 year ago
parent d6c5bbcbf9
commit 709d8e415e

@ -1 +1 @@
Subproject commit efa672661b6a4b576706df8ac6739272f26bdf6b
Subproject commit 41dd937d41dd06a74965efdf1ef1423cefbb4617

@ -3,10 +3,9 @@
// Submodules
#include "json/json.h"
#include "json/layout.h"
#include "sha256/sha256.h"
#include "http_client/client.h"
#include "collections/array.h"
#include "ffdb/fs_list.h"
#include "ffdb/hash_index.h"
// Model
#include "model/server.h"
@ -67,140 +66,17 @@ struct account* account_from_id( unsigned int id )
return a;
}
struct reverse_index_entry
static bool index_uri_to_account_id( const char* uri, int account_id )
{
char* uri;
int account_id;
};
extern struct json_field_type reverse_index_entry_type;
static struct json_object_field reverse_index_entry_layout[] = {
{ "uri", offsetof( struct reverse_index_entry, uri ), true, &json_field_string },
{ "id", offsetof( struct reverse_index_entry, account_id ), true, &json_field_integer },
{ NULL, 0, true, NULL, &reverse_index_entry_type },
};
static void* alloc_reverse_index_entry()
{
void* ptr = malloc(sizeof(struct reverse_index_entry));
memset( ptr, 0, sizeof(struct reverse_index_entry));
return ptr;
}
static void free_reverse_index_entry( void* ptr )
{
struct reverse_index_entry* rie = ptr;
free(rie->uri);
free(rie);
}
bool rie_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
void* data = *(void**)field_data = malloc(sizeof(struct reverse_index_entry));
bool res = json_read_object_layout( jpp, reverse_index_entry_layout, data );
if( !res ) {
free( data );
}
return res;
}
bool rie_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_object_field* layout_field_data )
{
json_write_field_name(jw,field_name);
json_write_pretty_object_layout( jw, reverse_index_entry_layout, *(void**)field_data );
return true;
}
struct json_field_type reverse_index_entry_type = {
.reader = rie_reader,
.writer = rie_writer,
.size = sizeof(void*),
.layout = reverse_index_entry_layout,
.alloc = alloc_reverse_index_entry,
.free = free_reverse_index_entry,
};
struct account_reverse_index
{
struct {
struct reverse_index_entry** items;
int count;
} entries;
};
static struct json_object_field account_reverse_index_layout[] = {
{ "entries", offsetof( struct account_reverse_index, entries ), true, &json_field_array_of, &reverse_index_entry_type },
{ NULL },
};
// TODO: move this and the lookup to new model
static int index_uri_to_account_id( const char* uri, int account_id )
{
char hash[65] = "";
sha256_easy_hash_hex( uri, strlen(uri), hash );
hash[5] = '\0';
char filename[512];
snprintf( filename, 512, "data/accounts/uri_index/%s.json", hash );
struct account_reverse_index ari;
memset( &ari, 0, sizeof(ari) );
json_read_object_layout_from_file( filename, account_reverse_index_layout, &ari );
struct reverse_index_entry new_entry = {
.uri = (char*)uri,
.account_id = account_id,
};
struct reverse_index_entry* ptr = &new_entry;
/*
for( int i = 0; i < ari.entries.count; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
printf( "\tentry[%d] = { .uri = %s, .account_id = %d }\n", i, entry->uri, entry->account_id );
}
//*/
array_append( &ari.entries, sizeof(struct reverse_index_entry), &ptr );
json_write_object_layout_to_file( filename, "\t", account_reverse_index_layout, &ari );
for( int i = 0; i < ari.entries.count-1; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
free(entry->uri);
free(entry);
}
return hash_index_set( "data/accounts/uri_index/", uri, account_id );
}
static int lookup_account_id_from_uri( const char* uri )
{
char hash[65] = "";
sha256_easy_hash_hex( uri, strlen(uri), hash );
hash[5] = '\0';
char filename[512];
snprintf( filename, 512, "data/accounts/uri_index/%s.json", hash );
struct account_reverse_index ari;
memset( &ari, 0, sizeof(ari) );
if( !json_read_object_layout_from_file( filename, account_reverse_index_layout, &ari ) ) {
int result = 0;
if( !hash_index_get( "data/accounts/uri_index/", uri, &result ) ) {
return -1;
}
/*
for( int i = 0; i < ari.entries.count; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
printf( "\tentry[%d] = %p{ .uri = %s, .account_id = %d }\n", i, entry, entry->uri, entry->account_id );
}
//*/
int result = -1;
for( int i = 0; i < ari.entries.count; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
if( 0 == strcmp(entry->uri,uri) ) {
result = entry->account_id;
}
free(entry->uri);
free(entry);
}
free(ari.entries.items);
return result;
}

Loading…
Cancel
Save