annotate sparql/librdf/src/rdf_storage_audiodb.c @ 605:d7eb0a7440ad

Tidying and POST enhancement * Ensured that the storage module is closed/freed when errors occur * Now reads in the query via POST if provided (allows for other clients)
author mas01mj
date Tue, 18 Aug 2009 14:23:32 +0000
parents 41665dbab958
children 548bfaa1b0ff
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@599 13 /**
mas01mj@599 14 * NB : This is pulled through from librdf internals. Not ideal, but
mas01mj@599 15 * otherwise we'd need to compile against their source tree!
mas01mj@599 16 **/
mas01mj@599 17
mas01mj@585 18 #define LIBRDF_SIGN_KEY 0x04Ed1A7D
mas01mj@584 19
mas01mj@603 20 #define AF_DIMENSION "http://purl.org/ontology/af/dimension"
mas01mj@603 21 #define AF_VECTORS "http://purl.org/ontology/af/vectors"
mas01mj@603 22 #define MO_SIGNAL "http://purl.org/ontology/mo/Signal"
mas01mj@603 23 #define RDF_TYPE "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
mas01mj@603 24
mas01mj@599 25
mas01mj@584 26 typedef struct {
mas01mj@584 27 librdf_model* model;
mas01mj@584 28 librdf_storage* storage;
mas01mj@599 29 char* name;
mas01mj@584 30 size_t name_len;
mas01mj@584 31 int is_new;
mas01mj@584 32
mas01mj@599 33 adb_t* adb;
mas01mj@584 34
mas01mj@584 35 } librdf_storage_audiodb_instance;
mas01mj@584 36
mas01mj@584 37
mas01mj@584 38 static int librdf_storage_audiodb_init(librdf_storage* storage, const char *name, librdf_hash* options);
mas01mj@584 39 static int librdf_storage_audiodb_open(librdf_storage* storage, librdf_model* model);
mas01mj@584 40 static int librdf_storage_audiodb_close(librdf_storage* storage);
mas01mj@584 41 static int librdf_storage_audiodb_size(librdf_storage* storage);
mas01mj@584 42 static int librdf_storage_audiodb_add_statement(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 43 static int librdf_storage_audiodb_add_statements(librdf_storage* storage, librdf_stream* statement_stream);
mas01mj@584 44 static int librdf_storage_audiodb_remove_statement(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 45 static int librdf_storage_audiodb_contains_statement(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 46 static librdf_stream* librdf_storage_audiodb_serialise(librdf_storage* storage);
mas01mj@584 47 static librdf_stream* librdf_storage_audiodb_find_statements(librdf_storage* storage, librdf_statement* statement);
mas01mj@584 48
mas01mj@584 49 /* find_statements implementing functions */
mas01mj@584 50 static int librdf_storage_audiodb_find_statements_end_of_stream(void* context);
mas01mj@584 51 static int librdf_storage_audiodb_find_statements_next_statement(void* context);
mas01mj@584 52 static void* librdf_storage_audiodb_find_statements_get_statement(void* context, int flags);
mas01mj@584 53 static void librdf_storage_audiodb_find_statements_finished(void* context);
mas01mj@584 54
mas01mj@584 55 static int librdf_storage_audiodb_sync(librdf_storage *storage);
mas01mj@584 56
mas01mj@584 57 static void librdf_storage_audiodb_register_factory(librdf_storage_factory *factory);
mas01mj@584 58 void librdf_storage_module_register_factory(librdf_world *world);
mas01mj@585 59
mas01mj@599 60 /**
mas01mj@599 61 * These 3 are from librdf's rdf_internals - simplify the mallocing/freeing a bit.
mas01mj@599 62 */
mas01mj@599 63
mas01mj@585 64 void librdf_sign_free(void *ptr)
mas01mj@585 65 {
mas01mj@585 66 int *p;
mas01mj@585 67
mas01mj@585 68 if(!ptr)
mas01mj@585 69 return;
mas01mj@585 70
mas01mj@585 71 p=(int*)ptr;
mas01mj@585 72 p--;
mas01mj@585 73
mas01mj@585 74 if(*p != LIBRDF_SIGN_KEY)
mas01mj@585 75 return;
mas01mj@585 76
mas01mj@585 77 free(p);
mas01mj@585 78 }
mas01mj@585 79
mas01mj@585 80
mas01mj@585 81 void* librdf_sign_calloc(size_t nmemb, size_t size)
mas01mj@585 82 {
mas01mj@585 83 int *p;
mas01mj@585 84
mas01mj@585 85 /* turn into bytes */
mas01mj@585 86 size = nmemb*size + sizeof(int);
mas01mj@585 87
mas01mj@585 88 p=(int*)calloc(1, size);
mas01mj@585 89 *p++ = LIBRDF_SIGN_KEY;
mas01mj@585 90 return p;
mas01mj@585 91 }
mas01mj@585 92
mas01mj@585 93 void* librdf_sign_malloc(size_t size)
mas01mj@585 94 {
mas01mj@585 95 int *p;
mas01mj@585 96
mas01mj@585 97 size += sizeof(int);
mas01mj@585 98
mas01mj@585 99 p=(int*)malloc(size);
mas01mj@585 100 *p++ = LIBRDF_SIGN_KEY;
mas01mj@585 101 return p;
mas01mj@585 102 }
mas01mj@584 103
mas01mj@584 104
mas01mj@584 105 static int librdf_storage_audiodb_init(librdf_storage* storage, const char *name, librdf_hash* options) {
mas01mj@584 106
mas01mj@584 107 librdf_storage_audiodb_instance* context;
mas01mj@584 108 char* name_copy;
mas01mj@584 109
mas01mj@585 110 context = (librdf_storage_audiodb_instance*)librdf_sign_calloc(1, sizeof(librdf_storage_audiodb_instance));
mas01mj@584 111
mas01mj@584 112 if(!context)
mas01mj@584 113 {
mas01mj@584 114 if(options)
mas01mj@584 115 librdf_free_hash(options);
mas01mj@584 116 return 1;
mas01mj@584 117 }
mas01mj@584 118
mas01mj@584 119 librdf_storage_set_instance(storage, context);
mas01mj@584 120
mas01mj@584 121 context->storage = storage;
mas01mj@584 122
mas01mj@584 123 // Store the name of the db
mas01mj@584 124 context->name_len=strlen(name);
mas01mj@585 125 name_copy=(char*)librdf_sign_malloc(context->name_len+1);
mas01mj@584 126 if(!name_copy) {
mas01mj@584 127 if(options)
mas01mj@584 128 librdf_free_hash(options);
mas01mj@584 129 return 1;
mas01mj@584 130 }
mas01mj@584 131 strncpy(name_copy, name, context->name_len+1);
mas01mj@584 132 context->name=name_copy;
mas01mj@584 133
mas01mj@584 134 if(librdf_hash_get_as_boolean(options, "new") > 0)
mas01mj@584 135 context->is_new = 1;
mas01mj@584 136
mas01mj@584 137 if(options)
mas01mj@584 138 librdf_free_hash(options);
mas01mj@584 139
mas01mj@585 140 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 141 "Initialised!");
mas01mj@584 142
mas01mj@584 143 return 0;
mas01mj@584 144 }
mas01mj@584 145
mas01mj@584 146 static int librdf_storage_audiodb_open(librdf_storage* storage, librdf_model* model) {
mas01mj@585 147 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@584 148 int db_file_exists = 0;
mas01mj@584 149
mas01mj@585 150 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 151 "open");
mas01mj@584 152
mas01mj@584 153 if(!access((const char*)context->name, F_OK))
mas01mj@584 154 db_file_exists = 1;
mas01mj@584 155 else
mas01mj@584 156 context->is_new = 1;
mas01mj@584 157
mas01mj@584 158 if(context->is_new && db_file_exists)
mas01mj@584 159 unlink(context->name);
mas01mj@584 160
mas01mj@584 161 context->adb = NULL;
mas01mj@584 162
mas01mj@584 163 if(context->is_new) {
mas01mj@584 164 if(!(context->adb = audiodb_create(context->name, 0, 0, 0))) {
mas01mj@585 165 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 166 "Unable to create %s", context->name);
mas01mj@584 167 return 1;
mas01mj@584 168 }
mas01mj@584 169 }
mas01mj@584 170 else
mas01mj@584 171 {
mas01mj@584 172 if(!(context->adb = audiodb_open(context->name, O_RDWR))) {
mas01mj@585 173 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 174 "Unable to open %s", context->name);
mas01mj@584 175 return 1;
mas01mj@584 176 }
mas01mj@584 177 }
mas01mj@584 178
mas01mj@584 179 return 0;
mas01mj@584 180 }
mas01mj@584 181
mas01mj@584 182 static int librdf_storage_audiodb_close(librdf_storage* storage) {
mas01mj@585 183 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@584 184
mas01mj@585 185 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 186 "close");
mas01mj@584 187
mas01mj@584 188 if(context->adb)
mas01mj@584 189 {
mas01mj@584 190 audiodb_close(context->adb);
mas01mj@584 191 context->adb = NULL;
mas01mj@584 192 }
mas01mj@584 193
mas01mj@584 194 return 0;
mas01mj@584 195 }
mas01mj@584 196
mas01mj@584 197 static int librdf_storage_audiodb_size(librdf_storage* storage) {
mas01mj@585 198 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 199 "size");
mas01mj@584 200 return 0;
mas01mj@584 201 }
mas01mj@584 202
mas01mj@584 203 static int librdf_storage_audiodb_add_statement(librdf_storage* storage, librdf_statement* statement) {
mas01mj@585 204 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 205 "add statement");
mas01mj@584 206 return 0;
mas01mj@584 207 }
mas01mj@584 208
mas01mj@584 209 static int librdf_storage_audiodb_add_statements(librdf_storage* storage, librdf_stream* statement_stream) {
mas01mj@585 210 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 211 "add statements");
mas01mj@584 212 return 0;
mas01mj@584 213 }
mas01mj@584 214
mas01mj@584 215 static int librdf_storage_audiodb_remove_statement(librdf_storage* storage, librdf_statement* statement) {
mas01mj@585 216 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 217 "remove statement");
mas01mj@584 218 return 0;
mas01mj@584 219 }
mas01mj@584 220
mas01mj@584 221 static int librdf_storage_audiodb_contains_statement(librdf_storage* storage, librdf_statement* statement) {
mas01mj@599 222
mas01mj@599 223 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@599 224 librdf_world* world = librdf_storage_get_world(storage);
mas01mj@599 225
mas01mj@599 226 librdf_node* subject = librdf_statement_get_subject(statement);
mas01mj@599 227 librdf_node* object = librdf_statement_get_object(statement);
mas01mj@599 228 librdf_node* predicate = librdf_statement_get_predicate(statement);
mas01mj@603 229 librdf_log(world, 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@603 230 "Contains statement %s?", librdf_statement_to_string(statement));
mas01mj@599 231
mas01mj@599 232
mas01mj@599 233 if(subject && object && predicate)
mas01mj@599 234 {
mas01mj@599 235 // audioDBs only contain Signals (tracks are held in a separate store).
mas01mj@599 236
mas01mj@599 237 librdf_uri* type_uri = librdf_new_uri(world, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
mas01mj@599 238 librdf_uri* signal_uri = librdf_new_uri(world, "http://purl.org/ontology/mo/Signal");
mas01mj@599 239 if(librdf_uri_equals(type_uri, librdf_node_get_uri(predicate)))
mas01mj@599 240 {
mas01mj@599 241 librdf_uri* object_uri = librdf_node_get_uri(object);
mas01mj@599 242 if(librdf_uri_equals(object_uri, signal_uri))
mas01mj@599 243 {
mas01mj@599 244 // Grab the track via audioDB
mas01mj@599 245 adb_datum_t datum = {0};
mas01mj@599 246 int result = audiodb_retrieve_datum(
mas01mj@599 247 context->adb,
mas01mj@599 248 librdf_uri_as_string(librdf_node_get_uri(subject)),
mas01mj@599 249 &datum);
mas01mj@599 250 if(result == 0)
mas01mj@599 251 {
mas01mj@599 252 // Found it! Free up the datum.
mas01mj@599 253 audiodb_free_datum(context->adb, &datum);
mas01mj@599 254 return 1;
mas01mj@599 255 }
mas01mj@599 256 }
mas01mj@599 257 }
mas01mj@599 258 librdf_free_uri(type_uri);
mas01mj@599 259 librdf_free_uri(signal_uri);
mas01mj@599 260 }
mas01mj@584 261 return 0;
mas01mj@584 262 }
mas01mj@584 263
mas01mj@584 264 static librdf_stream* librdf_storage_audiodb_serialise(librdf_storage* storage) {
mas01mj@585 265 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 266 "serialise");
mas01mj@584 267 return NULL;
mas01mj@584 268 }
mas01mj@584 269
mas01mj@599 270 /**
mas01mj@599 271 * Linked list bits
mas01mj@599 272 */
mas01mj@599 273
mas01mj@599 274
mas01mj@599 275 struct list_node_s
mas01mj@599 276 {
mas01mj@599 277 struct list_node_s* next;
mas01mj@599 278 struct list_node_s* prev;
mas01mj@599 279 librdf_statement* statement;
mas01mj@599 280 };
mas01mj@599 281
mas01mj@599 282 typedef struct list_node_s list_node;
mas01mj@599 283
mas01mj@599 284 struct list_s {
mas01mj@599 285 list_node* head;
mas01mj@599 286 list_node* tail;
mas01mj@599 287 };
mas01mj@599 288
mas01mj@599 289 typedef struct list_s result_list;
mas01mj@599 290
mas01mj@599 291 result_list* result_data_new()
mas01mj@599 292 {
mas01mj@599 293 result_list* list = (result_list*)calloc(1, sizeof(result_list));
mas01mj@599 294 if(!list)
mas01mj@599 295 return NULL;
mas01mj@599 296 return list;
mas01mj@599 297 }
mas01mj@599 298
mas01mj@599 299 int result_data_add(librdf_world* world, result_list* list) {
mas01mj@599 300
mas01mj@599 301 // First create the node
mas01mj@599 302 list_node* node = (list_node*)calloc(1, sizeof(list_node));
mas01mj@599 303
mas01mj@599 304 if(!node)
mas01mj@599 305 return 1;
mas01mj@599 306
mas01mj@599 307 if(list->tail)
mas01mj@599 308 {
mas01mj@599 309 node->prev = list->tail;
mas01mj@599 310 list->tail->next = node;
mas01mj@599 311 }
mas01mj@599 312
mas01mj@599 313 list->tail = node;
mas01mj@599 314 if(!list->head)
mas01mj@599 315 list->head = node;
mas01mj@599 316 return 0;
mas01mj@599 317 }
mas01mj@599 318
mas01mj@599 319 /**
mas01mj@599 320 * Querying bits.
mas01mj@599 321 **/
mas01mj@599 322
mas01mj@599 323 typedef struct {
mas01mj@599 324 librdf_storage* storage;
mas01mj@599 325 librdf_storage_audiodb_instance* audiodb_context;
mas01mj@599 326 int finished;
mas01mj@599 327 librdf_statement* statement;
mas01mj@599 328 librdf_statement* query_statement;
mas01mj@599 329 librdf_node* context;
mas01mj@599 330 result_list *results;
mas01mj@599 331
mas01mj@599 332 } librdf_storage_audiodb_find_statements_stream_context;
mas01mj@599 333
mas01mj@599 334
mas01mj@599 335
mas01mj@599 336
mas01mj@584 337 static librdf_stream* librdf_storage_audiodb_find_statements(librdf_storage* storage, librdf_statement* statement) {
mas01mj@584 338
mas01mj@585 339 librdf_storage_audiodb_instance* context = (librdf_storage_audiodb_instance*)librdf_storage_get_instance(storage);
mas01mj@584 340 librdf_storage_audiodb_find_statements_stream_context* scontext;
mas01mj@584 341 librdf_stream* stream;
mas01mj@599 342
mas01mj@599 343
mas01mj@599 344 librdf_world* world = librdf_storage_get_world(storage);
mas01mj@599 345
mas01mj@585 346 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@599 347 "find statements %s", librdf_statement_to_string(statement));
mas01mj@584 348
mas01mj@599 349 // Create stream context
mas01mj@585 350 scontext = (librdf_storage_audiodb_find_statements_stream_context*)librdf_sign_calloc(1, sizeof(librdf_storage_audiodb_find_statements_stream_context));
mas01mj@584 351
mas01mj@584 352 if(!scontext)
mas01mj@584 353 return NULL;
mas01mj@584 354
mas01mj@584 355 scontext->storage = storage;
mas01mj@584 356 librdf_storage_add_reference(scontext->storage);
mas01mj@599 357
mas01mj@599 358 // Store a reference to the storage instance.
mas01mj@584 359 scontext->audiodb_context = context;
mas01mj@584 360
mas01mj@599 361 // Clone the query statement and store away.
mas01mj@584 362 scontext->query_statement = librdf_new_statement_from_statement(statement);
mas01mj@584 363 if(!scontext->query_statement) {
mas01mj@584 364 librdf_storage_audiodb_find_statements_finished((void*)scontext);
mas01mj@584 365 return NULL;
mas01mj@584 366 }
mas01mj@599 367 scontext->results = result_data_new();
mas01mj@599 368
mas01mj@599 369 // This will need factoring out
mas01mj@599 370 librdf_node* subject = librdf_statement_get_subject(statement);
mas01mj@599 371 librdf_node* object = librdf_statement_get_object(statement);
mas01mj@599 372 librdf_node* predicate = librdf_statement_get_predicate(statement);
mas01mj@599 373
mas01mj@603 374 bool subjres = (subject && librdf_node_is_resource(subject));
mas01mj@603 375 bool predres = (predicate && librdf_node_is_resource(predicate));
mas01mj@603 376 bool objres = (object && librdf_node_is_resource(object));
mas01mj@599 377
mas01mj@603 378 librdf_uri* dimension = librdf_new_uri(world, AF_DIMENSION);
mas01mj@603 379 librdf_uri* vectors = librdf_new_uri(world, AF_VECTORS);
mas01mj@603 380 librdf_uri* signal = librdf_new_uri(world, MO_SIGNAL);
mas01mj@603 381 librdf_uri* type = librdf_new_uri(world, RDF_TYPE);
mas01mj@603 382
mas01mj@603 383 if(subjres && predres)
mas01mj@599 384 {
mas01mj@603 385 librdf_log(librdf_storage_get_world(storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Got SPX");
mas01mj@599 386 librdf_uri* predicate_uri = librdf_node_get_uri(predicate);
mas01mj@603 387 librdf_uri* subject_uri = librdf_node_get_uri(subject);
mas01mj@599 388 if(librdf_uri_equals(predicate_uri, dimension) || librdf_uri_equals(predicate_uri, vectors))
mas01mj@599 389 {
mas01mj@599 390 // Need dimension or vectors - so get the track datum.
mas01mj@599 391 adb_datum_t datum = {0};
mas01mj@599 392 int result = audiodb_retrieve_datum(
mas01mj@599 393 context->adb,
mas01mj@603 394 librdf_uri_as_string(subject_uri),
mas01mj@599 395 &datum);
mas01mj@599 396 if(result == 0)
mas01mj@599 397 {
mas01mj@599 398 librdf_node* value;
mas01mj@599 399 raptor_stringbuffer* buffer = raptor_new_stringbuffer();
mas01mj@599 400
mas01mj@599 401 if(librdf_uri_equals(predicate_uri, dimension))
mas01mj@599 402 raptor_stringbuffer_append_decimal(buffer, datum.dim);
mas01mj@599 403 else if(librdf_uri_equals(predicate_uri, vectors))
mas01mj@599 404 raptor_stringbuffer_append_decimal(buffer, datum.nvectors);
mas01mj@603 405
mas01mj@604 406 value = librdf_new_node_from_typed_literal(world, raptor_stringbuffer_as_string(buffer), NULL, librdf_new_uri(world, "http://www.w3.org/2001/XMLSchema#integer"));
mas01mj@599 407
mas01mj@599 408 result_data_add(world, scontext->results);
mas01mj@599 409
mas01mj@599 410 result_list* list = scontext->results;
mas01mj@599 411
mas01mj@599 412 list->tail->statement = librdf_new_statement(world);
mas01mj@599 413 librdf_statement_set_subject(list->tail->statement, subject);
mas01mj@599 414 librdf_statement_set_object(list->tail->statement, value);
mas01mj@599 415 librdf_statement_set_predicate(list->tail->statement, predicate);
mas01mj@603 416
mas01mj@603 417 librdf_log(world, 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Add statement %s", librdf_statement_to_string(list->tail->statement));
mas01mj@599 418 }
mas01mj@599 419
mas01mj@599 420 }
mas01mj@599 421 }
mas01mj@603 422 else if(predres && objres)
mas01mj@603 423 {
mas01mj@603 424 // Got XPO
mas01mj@603 425 librdf_log(world, 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "XPO");
mas01mj@603 426 librdf_uri* predicate_uri = librdf_node_get_uri(predicate);
mas01mj@603 427 librdf_uri* object_uri = librdf_node_get_uri(object);
mas01mj@603 428
mas01mj@603 429 if(librdf_uri_equals(predicate_uri, type) && librdf_uri_equals(object_uri, signal))
mas01mj@603 430 {
mas01mj@603 431 adb_liszt_results_t* liszt_results = audiodb_liszt(context->adb);
mas01mj@603 432
mas01mj@603 433 uint32_t k;
mas01mj@603 434 for(k = 0; k < liszt_results->nresults; k++) {
mas01mj@603 435 result_data_add(world, scontext->results);
mas01mj@603 436 result_list* list = scontext->results;
mas01mj@603 437 list->tail->statement = librdf_new_statement(world);
mas01mj@603 438 librdf_statement_set_subject(list->tail->statement, librdf_new_node_from_uri(world, librdf_new_uri(world, liszt_results->entries[k].key)));
mas01mj@603 439
mas01mj@603 440 librdf_statement_set_predicate(list->tail->statement, predicate);
mas01mj@603 441 librdf_statement_set_object(list->tail->statement, object);
mas01mj@603 442 librdf_log(world, 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Add statement %s", librdf_statement_to_string(list->tail->statement));
mas01mj@603 443 }
mas01mj@603 444
mas01mj@603 445 audiodb_liszt_free_results(context->adb, liszt_results);
mas01mj@603 446
mas01mj@603 447 }
mas01mj@603 448 }
mas01mj@603 449 else
mas01mj@603 450 {
mas01mj@603 451 librdf_log(world, 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL, "Halp?");
mas01mj@603 452 }
mas01mj@599 453 librdf_free_uri(dimension);
mas01mj@599 454 librdf_free_uri(vectors);
mas01mj@603 455 librdf_free_uri(signal);
mas01mj@603 456 librdf_free_uri(type);
mas01mj@603 457
mas01mj@599 458 stream = librdf_new_stream(world,
mas01mj@599 459 (void*)scontext,
mas01mj@599 460 &librdf_storage_audiodb_find_statements_end_of_stream,
mas01mj@599 461 &librdf_storage_audiodb_find_statements_next_statement,
mas01mj@599 462 &librdf_storage_audiodb_find_statements_get_statement,
mas01mj@599 463 &librdf_storage_audiodb_find_statements_finished);
mas01mj@584 464
mas01mj@584 465 if(!stream) {
mas01mj@599 466 librdf_log(world, 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@599 467 "Couldn't create stream!");
mas01mj@584 468 librdf_storage_audiodb_find_statements_finished((void*)scontext);
mas01mj@584 469 return NULL;
mas01mj@584 470 }
mas01mj@584 471
mas01mj@584 472 return stream;
mas01mj@584 473 }
mas01mj@584 474
mas01mj@584 475 static void librdf_storage_audiodb_find_statements_finished(void* context) {
mas01mj@584 476 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@599 477
mas01mj@599 478 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@599 479 "Finished!");
mas01mj@584 480
mas01mj@584 481 if(scontext->storage)
mas01mj@584 482 librdf_storage_remove_reference(scontext->storage);
mas01mj@584 483
mas01mj@584 484 if(scontext->query_statement)
mas01mj@584 485 librdf_free_statement(scontext->query_statement);
mas01mj@584 486
mas01mj@584 487 if(scontext->statement)
mas01mj@584 488 librdf_free_statement(scontext->statement);
mas01mj@584 489
mas01mj@584 490 if(scontext->context)
mas01mj@584 491 librdf_free_node(scontext->context);
mas01mj@584 492
mas01mj@585 493 librdf_sign_free(scontext);
mas01mj@584 494 }
mas01mj@584 495
mas01mj@584 496 static int librdf_storage_audiodb_find_statements_end_of_stream(void* context) {
mas01mj@584 497 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@599 498 librdf_world* world = librdf_storage_get_world(scontext->storage);
mas01mj@599 499 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@603 500 "Finished? %d", (scontext->results->head == NULL));
mas01mj@584 501
mas01mj@599 502 return (scontext->results->head == NULL);
mas01mj@584 503 }
mas01mj@584 504
mas01mj@584 505 static int librdf_storage_audiodb_find_statements_next_statement(void* context) {
mas01mj@584 506 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@584 507
mas01mj@599 508 librdf_world* world = librdf_storage_get_world(scontext->storage);
mas01mj@599 509 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@599 510 "to next");
mas01mj@599 511
mas01mj@599 512 if(scontext->results->head->next)
mas01mj@599 513 {
mas01mj@599 514 scontext->results->head = scontext->results->head->next;
mas01mj@599 515 return 0;
mas01mj@599 516 }
mas01mj@599 517 else
mas01mj@599 518 {
mas01mj@584 519 return 1;
mas01mj@584 520 }
mas01mj@584 521 }
mas01mj@584 522
mas01mj@584 523 static void* librdf_storage_audiodb_find_statements_get_statement(void* context, int flags) {
mas01mj@584 524 librdf_storage_audiodb_find_statements_stream_context* scontext=(librdf_storage_audiodb_find_statements_stream_context*)context;
mas01mj@599 525 librdf_world* world = librdf_storage_get_world(scontext->storage);
mas01mj@599 526 librdf_log(librdf_storage_get_world(scontext->storage), 0, LIBRDF_LOG_INFO, LIBRDF_FROM_STORAGE, NULL,
mas01mj@599 527 "Get next");
mas01mj@603 528
mas01mj@584 529 switch(flags) {
mas01mj@584 530 case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:
mas01mj@599 531 return scontext->results->head->statement;
mas01mj@584 532 case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
mas01mj@584 533 return scontext->context;
mas01mj@584 534 default:
mas01mj@599 535 librdf_log(world,
mas01mj@584 536 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
mas01mj@584 537 "Unknown iterator method flag %d", flags);
mas01mj@584 538 return NULL;
mas01mj@584 539 }
mas01mj@584 540 }
mas01mj@584 541
mas01mj@584 542
mas01mj@584 543
mas01mj@584 544 static int librdf_storage_audiodb_sync(librdf_storage *storage) {
mas01mj@584 545 return 0;
mas01mj@584 546 }
mas01mj@584 547
mas01mj@584 548 static void
mas01mj@584 549 librdf_storage_audiodb_register_factory(librdf_storage_factory *factory) {
mas01mj@584 550 factory->version = LIBRDF_STORAGE_INTERFACE_VERSION;
mas01mj@584 551 factory->init = librdf_storage_audiodb_init;
mas01mj@584 552 factory->open = librdf_storage_audiodb_open;
mas01mj@584 553 factory->close = librdf_storage_audiodb_close;
mas01mj@584 554 factory->size = librdf_storage_audiodb_size;
mas01mj@584 555 factory->add_statement = librdf_storage_audiodb_add_statement;
mas01mj@584 556 factory->remove_statement = librdf_storage_audiodb_remove_statement;
mas01mj@584 557 factory->contains_statement = librdf_storage_audiodb_contains_statement;
mas01mj@584 558 factory->serialise = librdf_storage_audiodb_serialise;
mas01mj@584 559 factory->find_statements = librdf_storage_audiodb_find_statements;
mas01mj@584 560 }
mas01mj@584 561
mas01mj@584 562 /** Entry point for dynamically loaded storage module */
mas01mj@584 563 void librdf_storage_module_register_factory(librdf_world *world) {
mas01mj@584 564 librdf_storage_register_factory(world, "audiodb", "AudioDB",
mas01mj@584 565 &librdf_storage_audiodb_register_factory);
mas01mj@584 566 }