Mercurial > hg > audiodb
view sparql/librdf/src/rdf_storage_audiodb.c @ 596:6ad0a6e67d4c
Take advantage of those new handy _or_goto_error macros
Use them in various places where previously we either elided the error
checking (various lseek() calls) or used a combination of calls
(replaced by malloc_and_fill_or_goto_error()).
In the process, fix what is probably a bug (or else introduce one, but I
don't think so): audiodb_track_id_datum() computed the offset into the
timesTable wrongly, forgetting to multiply by 2. (TODO: this should be
easily testable using the API).
Now all of LIBOBJS can be produced by my (Debian's) mingw32
cross-compiler, except for lshlib.o.
author | mas01cr |
---|---|
date | Tue, 11 Aug 2009 21:42:49 +0000 |
parents | 5098bd155c39 |
children | c6debbac3216 |
line wrap: on
line source
#include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #ifdef HAVE_STDLIB_H #include <stdlib.h> /* for abort() as used in errors */ #endif #include <sys/types.h> #include <librdf.h> #include <audioDB_API.h> #define LIBRDF_SIGN_KEY 0x04Ed1A7D typedef struct { librdf_model* model; librdf_storage* storage; char *name; size_t name_len; int is_new; adb_t *adb; } librdf_storage_audiodb_instance; typedef struct { librdf_storage* storage; librdf_storage_audiodb_instance* audiodb_context; int finished; librdf_statement* statement; librdf_statement* query_statement; librdf_node* context; } librdf_storage_audiodb_find_statements_stream_context; static int librdf_storage_audiodb_init(librdf_storage* storage, const char *name, librdf_hash* options); static int librdf_storage_audiodb_open(librdf_storage* storage, librdf_model* model); static int librdf_storage_audiodb_close(librdf_storage* storage); static int librdf_storage_audiodb_size(librdf_storage* storage); static int librdf_storage_audiodb_add_statement(librdf_storage* storage, librdf_statement* statement); static int librdf_storage_audiodb_add_statements(librdf_storage* storage, librdf_stream* statement_stream); static int librdf_storage_audiodb_remove_statement(librdf_storage* storage, librdf_statement* statement); static int librdf_storage_audiodb_contains_statement(librdf_storage* storage, librdf_statement* statement); static librdf_stream* librdf_storage_audiodb_serialise(librdf_storage* storage); static librdf_stream* librdf_storage_audiodb_find_statements(librdf_storage* storage, librdf_statement* statement); /* find_statements implementing functions */ static int librdf_storage_audiodb_find_statements_end_of_stream(void* context); static int librdf_storage_audiodb_find_statements_next_statement(void* context); static void* librdf_storage_audiodb_find_statements_get_statement(void* context, int flags); static void librdf_storage_audiodb_find_statements_finished(void* context); static int librdf_storage_audiodb_sync(librdf_storage *storage); static void librdf_storage_audiodb_register_factory(librdf_storage_factory *factory); void librdf_storage_module_register_factory(librdf_world *world); void librdf_sign_free(void *ptr) { int *p; if(!ptr) return; p=(int*)ptr; p--; if(*p != LIBRDF_SIGN_KEY) return; free(p); } void* librdf_sign_calloc(size_t nmemb, size_t size) { int *p; /* turn into bytes */ size = nmemb*size + sizeof(int); p=(int*)calloc(1, size); *p++ = LIBRDF_SIGN_KEY; return p; } void* librdf_sign_malloc(size_t size) { int *p; size += sizeof(int); p=(int*)malloc(size); *p++ = LIBRDF_SIGN_KEY; return p; } static int librdf_storage_audiodb_init(librdf_storage* storage, const char *name, librdf_hash* options) { librdf_storage_audiodb_instance* context; char* name_copy; context = (librdf_storage_audiodb_instance*)librdf_sign_calloc(1, sizeof(librdf_storage_audiodb_instance)); if(!context) { if(options) librdf_free_hash(options); return 1; } librdf_storage_set_instance(storage, context); context->storage = storage; // Store the name of the db context->name_len=strlen(name); name_copy=(char*)librdf_sign_malloc(context->name_len+1); if(!name_copy) { if(options) librdf_free_hash(options); return 1; } strncpy(name_copy, name, context->name_len+1); context->name=name_copy; if(librdf_hash_get_as_boolean(options, "new") > 0) context->is_new = 1; if(options) librdf_free_hash(options); librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Initialised!"); return 0; } static int librdf_storage_audiodb_open(librdf_storage* storage, librdf_model* model) { librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage); int db_file_exists = 0; librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "open"); if(!access((const char*)context->name, F_OK)) db_file_exists = 1; else context->is_new = 1; if(context->is_new && db_file_exists) unlink(context->name); context->adb = NULL; if(context->is_new) { if(!(context->adb = audiodb_create(context->name, 0, 0, 0))) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "Unable to create %s", context->name); return 1; } } else { if(!(context->adb = audiodb_open(context->name, O_RDWR))) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "Unable to open %s", context->name); return 1; } } return 0; } static int librdf_storage_audiodb_close(librdf_storage* storage) { librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage); librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "close"); if(context->adb) { audiodb_close(context->adb); context->adb = NULL; } return 0; } static int librdf_storage_audiodb_size(librdf_storage* storage) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "size"); return 0; } static int librdf_storage_audiodb_add_statement(librdf_storage* storage, librdf_statement* statement) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "add statement"); return 0; } static int librdf_storage_audiodb_add_statements(librdf_storage* storage, librdf_stream* statement_stream) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "add statements"); return 0; } static int librdf_storage_audiodb_remove_statement(librdf_storage* storage, librdf_statement* statement) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "remove statement"); return 0; } static int librdf_storage_audiodb_contains_statement(librdf_storage* storage, librdf_statement* statement) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Contains statement"); return 0; } static librdf_stream* librdf_storage_audiodb_serialise(librdf_storage* storage) { librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "serialise"); return NULL; } static librdf_stream* librdf_storage_audiodb_find_statements(librdf_storage* storage, librdf_statement* statement) { librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage); librdf_storage_audiodb_find_statements_stream_context* scontext; librdf_stream* stream; librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "find statements %s", librdf_statement_to_string(statement)); scontext = (librdf_storage_audiodb_find_statements_stream_context*)librdf_sign_calloc(1, sizeof(librdf_storage_audiodb_find_statements_stream_context)); if(!scontext) return NULL; scontext->storage = storage; librdf_storage_add_reference(scontext->storage); scontext->audiodb_context = context; scontext->query_statement = librdf_new_statement_from_statement(statement); if(!scontext->query_statement) { librdf_storage_audiodb_find_statements_finished((void*)scontext); return NULL; } stream = librdf_new_stream(librdf_storage_get_world(storage), (void*)scontext, &librdf_storage_audiodb_find_statements_end_of_stream, &librdf_storage_audiodb_find_statements_next_statement, &librdf_storage_audiodb_find_statements_get_statement, &librdf_storage_audiodb_find_statements_finished); if(!stream) { librdf_storage_audiodb_find_statements_finished((void*)scontext); return NULL; } return stream; } static void librdf_storage_audiodb_find_statements_finished(void* context) { librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context; if(scontext->storage) librdf_storage_remove_reference(scontext->storage); if(scontext->query_statement) librdf_free_statement(scontext->query_statement); if(scontext->statement) librdf_free_statement(scontext->statement); if(scontext->context) librdf_free_node(scontext->context); librdf_sign_free(scontext); } static int librdf_storage_audiodb_get_next_common(librdf_storage_audiodb_instance* scontext, librdf_statement **statement, librdf_node **context_node) { librdf_node* node; if(!*statement) { if(!(*statement = librdf_new_statement(librdf_storage_get_world(scontext->storage)))) return 1; } librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Handle statement %s", librdf_statement_to_string(*statement)); librdf_statement_clear(*statement); node = librdf_new_node_from_uri_string(librdf_storage_get_world(scontext->storage), "testing"); if(!node) return 1; librdf_statement_set_subject(*statement, node); node = librdf_new_node_from_uri_string(librdf_storage_get_world(scontext->storage), "foootle"); if(!node) return 1; librdf_statement_set_predicate(*statement, node); node = librdf_new_node_from_uri_string(librdf_storage_get_world(scontext->storage), "barble"); if(!node) return 1; librdf_statement_set_object(*statement, node); return -1; } static int librdf_storage_audiodb_find_statements_end_of_stream(void* context) { librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context; if(scontext->finished) return 1; if(scontext->statement == NULL) { int result; result = librdf_storage_audiodb_get_next_common(scontext->audiodb_context, &scontext->statement, &scontext->context); librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Handle eos statement %s %d", librdf_statement_to_string(scontext->query_statement), result); if(result) { scontext->finished = 1; } } return scontext->finished; } static int librdf_storage_audiodb_find_statements_next_statement(void* context) { librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context; int result; if(scontext->finished) return 1; result = librdf_storage_audiodb_get_next_common(scontext->audiodb_context, &scontext->statement, &scontext->context); librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Handle next statement %s %d", librdf_statement_to_string(scontext->query_statement), result); if(result) { scontext->finished = 1; } return result; } static void* librdf_storage_audiodb_find_statements_get_statement(void* context, int flags) { librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context; switch(flags) { case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT: return scontext->statement; case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT: return scontext->context; default: librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "Unknown iterator method flag %d", flags); return NULL; } } static int librdf_storage_audiodb_sync(librdf_storage *storage) { return 0; } static void librdf_storage_audiodb_register_factory(librdf_storage_factory *factory) { factory->version = LIBRDF_STORAGE_INTERFACE_VERSION; factory->init = librdf_storage_audiodb_init; factory->open = librdf_storage_audiodb_open; factory->close = librdf_storage_audiodb_close; factory->size = librdf_storage_audiodb_size; factory->add_statement = librdf_storage_audiodb_add_statement; factory->remove_statement = librdf_storage_audiodb_remove_statement; factory->contains_statement = librdf_storage_audiodb_contains_statement; factory->serialise = librdf_storage_audiodb_serialise; factory->find_statements = librdf_storage_audiodb_find_statements; } /** Entry point for dynamically loaded storage module */ void librdf_storage_module_register_factory(librdf_world *world) { librdf_storage_register_factory(world, "audiodb", "AudioDB", &librdf_storage_audiodb_register_factory); }