parent
efa672661b
commit
41dd937d41
@ -0,0 +1,149 @@
|
|||||||
|
#include "hash_index.h"
|
||||||
|
|
||||||
|
// Submodules
|
||||||
|
#include "collections/array.h"
|
||||||
|
#include "json/json.h"
|
||||||
|
#include "json/layout.h"
|
||||||
|
#include "sha256/sha256.h"
|
||||||
|
|
||||||
|
// Stdlib
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct hash_index_entry
|
||||||
|
{
|
||||||
|
char* key;
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct json_field_type hash_index_entry_type;
|
||||||
|
static struct json_object_field hash_index_entry_layout[] = {
|
||||||
|
{ "key", offsetof( struct hash_index_entry, key ), true, &json_field_string },
|
||||||
|
{ "val", offsetof( struct hash_index_entry, value ), true, &json_field_integer },
|
||||||
|
{ NULL, 0, true, NULL, &hash_index_entry_type },
|
||||||
|
};
|
||||||
|
|
||||||
|
static void* alloc_hash_index_entry()
|
||||||
|
{
|
||||||
|
void* ptr = malloc(sizeof(struct hash_index_entry));
|
||||||
|
memset( ptr, 0, sizeof(struct hash_index_entry));
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
static void free_hash_index_entry( void* ptr )
|
||||||
|
{
|
||||||
|
struct hash_index_entry* rie = ptr;
|
||||||
|
free(rie->key);
|
||||||
|
free(rie);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool hie_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
|
||||||
|
{
|
||||||
|
void* data = *(void**)field_data = malloc(sizeof(struct hash_index_entry));
|
||||||
|
bool res = json_read_object_layout( jpp, hash_index_entry_layout, data );
|
||||||
|
if( !res ) {
|
||||||
|
free( data );
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
static bool hie_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, hash_index_entry_layout, *(void**)field_data );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct json_field_type hash_index_entry_type = {
|
||||||
|
.reader = hie_reader,
|
||||||
|
.writer = hie_writer,
|
||||||
|
.size = sizeof(void*),
|
||||||
|
.layout = hash_index_entry_layout,
|
||||||
|
.alloc = alloc_hash_index_entry,
|
||||||
|
.free = free_hash_index_entry,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hash_index
|
||||||
|
{
|
||||||
|
struct {
|
||||||
|
struct hash_index_entry** items;
|
||||||
|
int count;
|
||||||
|
} entries;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct json_object_field hash_index_layout[] = {
|
||||||
|
{ "entries", offsetof( struct hash_index, entries ), true, &json_field_array_of, &hash_index_entry_type },
|
||||||
|
{ NULL },
|
||||||
|
};
|
||||||
|
bool hash_index_set( const char* index, const char* key, int value )
|
||||||
|
{
|
||||||
|
char hash[65] = "";
|
||||||
|
sha256_easy_hash_hex( key, strlen(key), hash );
|
||||||
|
hash[5] = '\0';
|
||||||
|
|
||||||
|
char filename[512];
|
||||||
|
snprintf( filename, 512, "%s/%s.json", index, hash );
|
||||||
|
|
||||||
|
struct hash_index hi;
|
||||||
|
memset( &hi, 0, sizeof(hi) );
|
||||||
|
json_read_object_layout_from_file( filename, hash_index_layout, &hi );
|
||||||
|
|
||||||
|
struct hash_index_entry new_entry = {
|
||||||
|
.key = (char*)key,
|
||||||
|
.value = value,
|
||||||
|
};
|
||||||
|
struct hash_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( &hi.entries, sizeof(struct hash_index_entry), &ptr );
|
||||||
|
|
||||||
|
json_write_object_layout_to_file( filename, "\t", hash_index_layout, &hi );
|
||||||
|
|
||||||
|
for( int i = 0; i < hi.entries.count-1; ++i ) {
|
||||||
|
struct hash_index_entry* entry = hi.entries.items[i];
|
||||||
|
free(entry->key);
|
||||||
|
free(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hash_index_get( const char* index, const char* key, int* value )
|
||||||
|
{
|
||||||
|
char hash[65] = "";
|
||||||
|
sha256_easy_hash_hex( key, strlen(key), hash );
|
||||||
|
hash[5] = '\0';
|
||||||
|
|
||||||
|
char filename[512];
|
||||||
|
snprintf( filename, 512, "%s/%s.json", index, hash );
|
||||||
|
|
||||||
|
struct hash_index hi;
|
||||||
|
memset( &hi, 0, sizeof(hi) );
|
||||||
|
if( !json_read_object_layout_from_file( filename, hash_index_layout, &hi ) ) {
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
//*/
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
*value = -1;
|
||||||
|
for( int i = 0; i < hi.entries.count; ++i ) {
|
||||||
|
struct hash_index_entry* entry = hi.entries.items[i];
|
||||||
|
if( 0 == strcmp(entry->key,key) ) {
|
||||||
|
*value = entry->value;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
free(entry->key);
|
||||||
|
free(entry);
|
||||||
|
}
|
||||||
|
free(hi.entries.items);
|
||||||
|
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool hash_index_set( const char* index, const char* key, int value );
|
||||||
|
bool hash_index_get( const char* index, const char* key, int* value );
|
||||||
|
|
Loading…
Reference in new issue