annotate sparql/librdf/src/rdf_storage_audiodb.c @ 598:b2a941a372fb

Test for audiodb_retrieve_datum() The good news: my fix for audiodb_track_id_datum()'s timesTable handling looks correct. The bad news: we need to NULL out fields we don't fill with data. Do so.
author mas01cr
date Wed, 12 Aug 2009 14:57:11 +0000
parents 5098bd155c39
children c6debbac3216
rev   line source
mas01mj@585 1 #include <stdlib.h>
mas01mj@584 2 #include <unistd.h>
mas01mj@584 3 #include <fcntl.h>
mas01mj@584 4 #include <stdio.h>
mas01mj@584 5 #include <string.h>
mas01mj@584 6 #ifdef HAVE_STDLIB_H
mas01mj@584 7 #include <stdlib.h> /* for abort() as used in errors */
mas01mj@584 8 #endif
mas01mj@584 9 #include <sys/types.h>
mas01mj@585 10 #include <librdf.h>
mas01mj@584 11 #include <audioDB_API.h>
mas01mj@584 12
mas01mj@585 13 #define LIBRDF_SIGN_KEY 0x04Ed1A7D
mas01mj@584 14
mas01mj@584 15 typedef struct {
mas01mj@584 16 librdf_model* model;
mas01mj@584 17 librdf_storage* storage;
mas01mj@584 18 char *name;
mas01mj@584 19 size_t name_len;
mas01mj@584 20 int is_new;
mas01mj@584 21
mas01mj@584 22 adb_t *adb;
mas01mj@584 23
mas01mj@584 24 } librdf_storage_audiodb_instance;
mas01mj@584 25
mas01mj@584 26 typedef struct {
mas01mj@584 27 librdf_storage* storage;
mas01mj@584 28 librdf_storage_audiodb_instance* audiodb_context;
mas01mj@584 29 int finished;
mas01mj@584 30 librdf_statement* statement;
mas01mj@584 31 librdf_statement* query_statement;
mas01mj@584 32 librdf_node* context;
mas01mj@584 33
mas01mj@584 34 } librdf_storage_audiodb_find_statements_stream_context;
mas01mj@584 35
mas01mj@584 36 static int librdf_storage_audiodb_init(librdf_storage* storage, const char *name, librdf_hash* options);
mas01mj@584 37 static int librdf_storage_audiodb_open(librdf_storage* storage, librdf_model* model);
mas01mj@584 38 static int librdf_storage_audiodb_close(librdf_storage* storage);
mas01mj@584 39 static int librdf_storage_audiodb_size(librdf_storage* storage);
mas01mj@584 40 static int librdf_storage_audiodb_add_statement(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 41 static int librdf_storage_audiodb_add_statements(librdf_storage* storage, librdf_stream* statement_stream);
mas01mj@584 42 static int librdf_storage_audiodb_remove_statement(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 43 static int librdf_storage_audiodb_contains_statement(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 44 static librdf_stream* librdf_storage_audiodb_serialise(librdf_storage* storage);
mas01mj@584 45 static librdf_stream* librdf_storage_audiodb_find_statements(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 46
mas01mj@584 47 /* find_statements implementing functions */
mas01mj@584 48 static int librdf_storage_audiodb_find_statements_end_of_stream(void* context);
mas01mj@584 49 static int librdf_storage_audiodb_find_statements_next_statement(void* context);
mas01mj@584 50 static void* librdf_storage_audiodb_find_statements_get_statement(void* context, int flags);
mas01mj@584 51 static void librdf_storage_audiodb_find_statements_finished(void* context);
mas01mj@584 52
mas01mj@584 53 static int librdf_storage_audiodb_sync(librdf_storage *storage);
mas01mj@584 54
mas01mj@584 55 static void librdf_storage_audiodb_register_factory(librdf_storage_factory *factory);
mas01mj@584 56 void librdf_storage_module_register_factory(librdf_world *world);
mas01mj@585 57
mas01mj@585 58 void librdf_sign_free(void *ptr)
mas01mj@585 59 {
mas01mj@585 60 int *p;
mas01mj@585 61
mas01mj@585 62 if(!ptr)
mas01mj@585 63 return;
mas01mj@585 64
mas01mj@585 65 p=(int*)ptr;
mas01mj@585 66 p--;
mas01mj@585 67
mas01mj@585 68 if(*p != LIBRDF_SIGN_KEY)
mas01mj@585 69 return;
mas01mj@585 70
mas01mj@585 71 free(p);
mas01mj@585 72 }
mas01mj@585 73
mas01mj@585 74
mas01mj@585 75 void* librdf_sign_calloc(size_t nmemb, size_t size)
mas01mj@585 76 {
mas01mj@585 77 int *p;
mas01mj@585 78
mas01mj@585 79 /* turn into bytes */
mas01mj@585 80 size = nmemb*size + sizeof(int);
mas01mj@585 81
mas01mj@585 82 p=(int*)calloc(1, size);
mas01mj@585 83 *p++ = LIBRDF_SIGN_KEY;
mas01mj@585 84 return p;
mas01mj@585 85 }
mas01mj@585 86
mas01mj@585 87 void* librdf_sign_malloc(size_t size)
mas01mj@585 88 {
mas01mj@585 89 int *p;
mas01mj@585 90
mas01mj@585 91 size += sizeof(int);
mas01mj@585 92
mas01mj@585 93 p=(int*)malloc(size);
mas01mj@585 94 *p++ = LIBRDF_SIGN_KEY;
mas01mj@585 95 return p;
mas01mj@585 96 }
mas01mj@584 97
mas01mj@584 98
mas01mj@584 99 static int librdf_storage_audiodb_init(librdf_storage* storage, const char *name, librdf_hash* options) {
mas01mj@584 100
mas01mj@584 101 librdf_storage_audiodb_instance* context;
mas01mj@584 102 char* name_copy;
mas01mj@584 103
mas01mj@585 104 context = (librdf_storage_audiodb_instance*)librdf_sign_calloc(1, sizeof(librdf_storage_audiodb_instance));
mas01mj@584 105
mas01mj@584 106 if(!context)
mas01mj@584 107 {
mas01mj@584 108 if(options)
mas01mj@584 109 librdf_free_hash(options);
mas01mj@584 110 return 1;
mas01mj@584 111 }
mas01mj@584 112
mas01mj@584 113 librdf_storage_set_instance(storage, context);
mas01mj@584 114
mas01mj@584 115 context->storage = storage;
mas01mj@584 116
mas01mj@584 117 // Store the name of the db
mas01mj@584 118 context->name_len=strlen(name);
mas01mj@585 119 name_copy=(char*)librdf_sign_malloc(context->name_len+1);
mas01mj@584 120 if(!name_copy) {
mas01mj@584 121 if(options)
mas01mj@584 122 librdf_free_hash(options);
mas01mj@584 123 return 1;
mas01mj@584 124 }
mas01mj@584 125 strncpy(name_copy, name, context->name_len+1);
mas01mj@584 126 context->name=name_copy;
mas01mj@584 127
mas01mj@584 128 if(librdf_hash_get_as_boolean(options, "new") > 0)
mas01mj@584 129 context->is_new = 1;
mas01mj@584 130
mas01mj@584 131 if(options)
mas01mj@584 132 librdf_free_hash(options);
mas01mj@584 133
mas01mj@585 134 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 135 "Initialised!");
mas01mj@584 136
mas01mj@584 137 return 0;
mas01mj@584 138 }
mas01mj@584 139
mas01mj@584 140 static int librdf_storage_audiodb_open(librdf_storage* storage, librdf_model* model) {
mas01mj@584 141
mas01mj@585 142 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@584 143 int db_file_exists = 0;
mas01mj@584 144
mas01mj@585 145 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 146 "open");
mas01mj@584 147
mas01mj@584 148 if(!access((const char*)context->name, F_OK))
mas01mj@584 149 db_file_exists = 1;
mas01mj@584 150 else
mas01mj@584 151 context->is_new = 1;
mas01mj@584 152
mas01mj@584 153 if(context->is_new && db_file_exists)
mas01mj@584 154 unlink(context->name);
mas01mj@584 155
mas01mj@584 156 context->adb = NULL;
mas01mj@584 157
mas01mj@584 158 if(context->is_new) {
mas01mj@584 159 if(!(context->adb = audiodb_create(context->name, 0, 0, 0))) {
mas01mj@585 160 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 161 "Unable to create %s", context->name);
mas01mj@584 162 return 1;
mas01mj@584 163 }
mas01mj@584 164 }
mas01mj@584 165 else
mas01mj@584 166 {
mas01mj@584 167 if(!(context->adb = audiodb_open(context->name, O_RDWR))) {
mas01mj@585 168 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 169 "Unable to open %s", context->name);
mas01mj@584 170 return 1;
mas01mj@584 171 }
mas01mj@584 172 }
mas01mj@584 173
mas01mj@584 174 return 0;
mas01mj@584 175 }
mas01mj@584 176
mas01mj@584 177 static int librdf_storage_audiodb_close(librdf_storage* storage) {
mas01mj@585 178 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@584 179
mas01mj@585 180 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 181 "close");
mas01mj@584 182
mas01mj@584 183 if(context->adb)
mas01mj@584 184 {
mas01mj@584 185 audiodb_close(context->adb);
mas01mj@584 186 context->adb = NULL;
mas01mj@584 187 }
mas01mj@584 188
mas01mj@584 189 return 0;
mas01mj@584 190 }
mas01mj@584 191
mas01mj@584 192 static int librdf_storage_audiodb_size(librdf_storage* storage) {
mas01mj@585 193 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 194 "size");
mas01mj@584 195 return 0;
mas01mj@584 196 }
mas01mj@584 197
mas01mj@584 198 static int librdf_storage_audiodb_add_statement(librdf_storage* storage, librdf_statement* statement) {
mas01mj@585 199 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 200 "add statement");
mas01mj@584 201 return 0;
mas01mj@584 202 }
mas01mj@584 203
mas01mj@584 204 static int librdf_storage_audiodb_add_statements(librdf_storage* storage, librdf_stream* statement_stream) {
mas01mj@585 205 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 206 "add statements");
mas01mj@584 207 return 0;
mas01mj@584 208 }
mas01mj@584 209
mas01mj@584 210 static int librdf_storage_audiodb_remove_statement(librdf_storage* storage, librdf_statement* statement) {
mas01mj@585 211 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 212 "remove statement");
mas01mj@584 213 return 0;
mas01mj@584 214 }
mas01mj@584 215
mas01mj@584 216 static int librdf_storage_audiodb_contains_statement(librdf_storage* storage, librdf_statement* statement) {
mas01mj@585 217 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 218 "Contains statement");
mas01mj@584 219 return 0;
mas01mj@584 220 }
mas01mj@584 221
mas01mj@584 222 static librdf_stream* librdf_storage_audiodb_serialise(librdf_storage* storage) {
mas01mj@585 223 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 224 "serialise");
mas01mj@584 225 return NULL;
mas01mj@584 226 }
mas01mj@584 227
mas01mj@584 228 static librdf_stream* librdf_storage_audiodb_find_statements(librdf_storage* storage, librdf_statement* statement) {
mas01mj@584 229
mas01mj@585 230 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@584 231 librdf_storage_audiodb_find_statements_stream_context* scontext;
mas01mj@584 232 librdf_stream* stream;
mas01mj@584 233
mas01mj@585 234 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 235 "find statements %s", librdf_statement_to_string(statement));
mas01mj@584 236
mas01mj@585 237 scontext = (librdf_storage_audiodb_find_statements_stream_context*)librdf_sign_calloc(1, sizeof(librdf_storage_audiodb_find_statements_stream_context));
mas01mj@584 238
mas01mj@584 239 if(!scontext)
mas01mj@584 240 return NULL;
mas01mj@584 241
mas01mj@584 242 scontext->storage = storage;
mas01mj@584 243 librdf_storage_add_reference(scontext->storage);
mas01mj@584 244
mas01mj@584 245 scontext->audiodb_context = context;
mas01mj@584 246
mas01mj@584 247 scontext->query_statement = librdf_new_statement_from_statement(statement);
mas01mj@584 248 if(!scontext->query_statement) {
mas01mj@584 249 librdf_storage_audiodb_find_statements_finished((void*)scontext);
mas01mj@584 250 return NULL;
mas01mj@584 251 }
mas01mj@584 252
mas01mj@585 253 stream = librdf_new_stream(librdf_storage_get_world(storage),
mas01mj@584 254 (void*)scontext,
mas01mj@584 255 &librdf_storage_audiodb_find_statements_end_of_stream,
mas01mj@584 256 &librdf_storage_audiodb_find_statements_next_statement,
mas01mj@584 257 &librdf_storage_audiodb_find_statements_get_statement,
mas01mj@584 258 &librdf_storage_audiodb_find_statements_finished);
mas01mj@584 259
mas01mj@584 260 if(!stream) {
mas01mj@584 261 librdf_storage_audiodb_find_statements_finished((void*)scontext);
mas01mj@584 262 return NULL;
mas01mj@584 263 }
mas01mj@584 264
mas01mj@584 265 return stream;
mas01mj@584 266 }
mas01mj@584 267
mas01mj@584 268 static void librdf_storage_audiodb_find_statements_finished(void* context) {
mas01mj@584 269 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@584 270
mas01mj@584 271 if(scontext->storage)
mas01mj@584 272 librdf_storage_remove_reference(scontext->storage);
mas01mj@584 273
mas01mj@584 274 if(scontext->query_statement)
mas01mj@584 275 librdf_free_statement(scontext->query_statement);
mas01mj@584 276
mas01mj@584 277 if(scontext->statement)
mas01mj@584 278 librdf_free_statement(scontext->statement);
mas01mj@584 279
mas01mj@584 280 if(scontext->context)
mas01mj@584 281 librdf_free_node(scontext->context);
mas01mj@584 282
mas01mj@585 283 librdf_sign_free(scontext);
mas01mj@584 284 }
mas01mj@584 285
mas01mj@584 286 static int librdf_storage_audiodb_get_next_common(librdf_storage_audiodb_instance* scontext,
mas01mj@584 287 librdf_statement **statement,
mas01mj@584 288 librdf_node **context_node) {
mas01mj@584 289
mas01mj@584 290 librdf_node* node;
mas01mj@584 291
mas01mj@584 292
mas01mj@584 293 if(!*statement) {
mas01mj@585 294 if(!(*statement = librdf_new_statement(librdf_storage_get_world(scontext->storage))))
mas01mj@584 295 return 1;
mas01mj@584 296 }
mas01mj@584 297
mas01mj@585 298 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@585 299 "Handle statement %s", librdf_statement_to_string(*statement));
mas01mj@584 300
mas01mj@584 301 librdf_statement_clear(*statement);
mas01mj@584 302
mas01mj@585 303 node = librdf_new_node_from_uri_string(librdf_storage_get_world(scontext->storage), "testing");
mas01mj@584 304
mas01mj@584 305 if(!node)
mas01mj@584 306 return 1;
mas01mj@584 307
mas01mj@584 308 librdf_statement_set_subject(*statement, node);
mas01mj@584 309
mas01mj@585 310 node = librdf_new_node_from_uri_string(librdf_storage_get_world(scontext->storage), "foootle");
mas01mj@584 311
mas01mj@584 312 if(!node)
mas01mj@584 313 return 1;
mas01mj@584 314
mas01mj@584 315 librdf_statement_set_predicate(*statement, node);
mas01mj@584 316
mas01mj@585 317 node = librdf_new_node_from_uri_string(librdf_storage_get_world(scontext->storage), "barble");
mas01mj@584 318
mas01mj@584 319 if(!node)
mas01mj@584 320 return 1;
mas01mj@584 321
mas01mj@584 322 librdf_statement_set_object(*statement, node);
mas01mj@584 323
mas01mj@584 324 return -1;
mas01mj@584 325 }
mas01mj@584 326
mas01mj@584 327 static int librdf_storage_audiodb_find_statements_end_of_stream(void* context) {
mas01mj@584 328 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@584 329
mas01mj@584 330 if(scontext->finished)
mas01mj@584 331 return 1;
mas01mj@584 332
mas01mj@584 333 if(scontext->statement == NULL) {
mas01mj@584 334 int result;
mas01mj@584 335 result = librdf_storage_audiodb_get_next_common(scontext->audiodb_context,
mas01mj@584 336 &scontext->statement,
mas01mj@584 337 &scontext->context);
mas01mj@584 338
mas01mj@585 339 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 340 "Handle eos statement %s %d", librdf_statement_to_string(scontext->query_statement), result);
mas01mj@584 341
mas01mj@584 342 if(result) {
mas01mj@584 343 scontext->finished = 1;
mas01mj@584 344 }
mas01mj@584 345 }
mas01mj@584 346 return scontext->finished;
mas01mj@584 347
mas01mj@584 348 }
mas01mj@584 349
mas01mj@584 350 static int librdf_storage_audiodb_find_statements_next_statement(void* context) {
mas01mj@584 351 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@584 352 int result;
mas01mj@584 353
mas01mj@584 354
mas01mj@584 355 if(scontext->finished)
mas01mj@584 356 return 1;
mas01mj@584 357
mas01mj@584 358 result = librdf_storage_audiodb_get_next_common(scontext->audiodb_context,
mas01mj@584 359 &scontext->statement,
mas01mj@584 360 &scontext->context);
mas01mj@584 361
mas01mj@585 362 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 363 "Handle next statement %s %d", librdf_statement_to_string(scontext->query_statement), result);
mas01mj@584 364
mas01mj@584 365 if(result) {
mas01mj@584 366 scontext->finished = 1;
mas01mj@584 367 }
mas01mj@584 368
mas01mj@584 369 return result;
mas01mj@584 370 }
mas01mj@584 371
mas01mj@584 372 static void* librdf_storage_audiodb_find_statements_get_statement(void* context, int flags) {
mas01mj@584 373 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@584 374
mas01mj@584 375 switch(flags) {
mas01mj@584 376 case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:
mas01mj@584 377 return scontext->statement;
mas01mj@584 378 case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
mas01mj@584 379 return scontext->context;
mas01mj@584 380 default:
mas01mj@585 381 librdf_log(librdf_storage_get_world(scontext->storage),
mas01mj@584 382 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 383 "Unknown iterator method flag %d", flags);
mas01mj@584 384 return NULL;
mas01mj@584 385 }
mas01mj@584 386
mas01mj@584 387 }
mas01mj@584 388
mas01mj@584 389
mas01mj@584 390
mas01mj@584 391 static int librdf_storage_audiodb_sync(librdf_storage *storage) {
mas01mj@584 392 return 0;
mas01mj@584 393 }
mas01mj@584 394
mas01mj@584 395 static void
mas01mj@584 396 librdf_storage_audiodb_register_factory(librdf_storage_factory *factory) {
mas01mj@584 397 factory->version = LIBRDF_STORAGE_INTERFACE_VERSION;
mas01mj@584 398 factory->init = librdf_storage_audiodb_init;
mas01mj@584 399 factory->open = librdf_storage_audiodb_open;
mas01mj@584 400 factory->close = librdf_storage_audiodb_close;
mas01mj@584 401 factory->size = librdf_storage_audiodb_size;
mas01mj@584 402 factory->add_statement = librdf_storage_audiodb_add_statement;
mas01mj@584 403 factory->remove_statement = librdf_storage_audiodb_remove_statement;
mas01mj@584 404 factory->contains_statement = librdf_storage_audiodb_contains_statement;
mas01mj@584 405 factory->serialise = librdf_storage_audiodb_serialise;
mas01mj@584 406 factory->find_statements = librdf_storage_audiodb_find_statements;
mas01mj@584 407 }
mas01mj@584 408
mas01mj@584 409 /** Entry point for dynamically loaded storage module */
mas01mj@584 410 void librdf_storage_module_register_factory(librdf_world *world) {
mas01mj@584 411 librdf_storage_register_factory(world, "audiodb", "AudioDB",
mas01mj@584 412 &librdf_storage_audiodb_register_factory);
mas01mj@584 413 }