/* functions implementing storage api */ static int librdf_storage_tstore_init(librdf_storage* storage, const char *name, librdf_hash* options) { librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)LIBRDF_CALLOC( librdf_storage_tstore_instance, 1, sizeof(librdf_storage_tstore_instance)); if(!context) { if(options) librdf_free_hash(options); return 1; } librdf_storage_set_instance(storage, context); context->host=librdf_hash_get_del(options, "host"); context->db=librdf_hash_get_del(options, "database"); context->user=librdf_hash_get_del(options, "user"); context->password=librdf_hash_get_del(options, "password"); context->model=librdf_hash_get_del(options, "model"); /* no more options, might as well free them now */ if(options) librdf_free_hash(options); return 0; }
/* functions implementing storage api */ static int librdf_storage_trees_init(librdf_storage* storage, const char *name, librdf_hash* options) { const int index_spo_option = librdf_hash_get_as_boolean(options, "index-spo") > 0; const int index_sop_option = librdf_hash_get_as_boolean(options, "index-sop") > 0; const int index_ops_option = librdf_hash_get_as_boolean(options, "index-ops") > 0; const int index_pso_option = librdf_hash_get_as_boolean(options, "index-pso") > 0; librdf_storage_trees_instance* context=(librdf_storage_trees_instance*)LIBRDF_CALLOC( librdf_storage_trees_instance, 1, sizeof(librdf_storage_trees_instance)); if(!context) { if(options) librdf_free_hash(options); return 1; } librdf_storage_set_instance(storage, context); #ifdef RDF_STORAGE_TREES_WITH_CONTEXTS /* Support contexts if option given */ if (librdf_hash_get_as_boolean(options, "contexts") > 0) { context->contexts=librdf_new_avltree(librdf_storage_trees_graph_compare, librdf_storage_trees_graph_free); } else { context->contexts=NULL; } #endif /* No indexing options given, index all by default */ if (!index_spo_option && !index_sop_option && !index_ops_option && !index_pso_option) { context->index_sop=1; context->index_ops=1; context->index_pso=1; } else { /* spo is always indexed, option just exists so user can * specifically /only/ index spo */ context->index_sop=index_sop_option; context->index_ops=index_ops_option; context->index_pso=index_pso_option; } context->graph = librdf_storage_trees_graph_new(storage, NULL); /* no more options, might as well free them now */ if(options) librdf_free_hash(options); return 0; }
/* functions implementing storage api */ static int librdf_storage_file_init(librdf_storage* storage, const char *name, librdf_hash* options) { char *name_copy; char *contexts; int rc = 1; int is_uri = !strcmp(storage->factory->name, "uri"); const char *format_name = (is_uri ? "guess" : "rdfxml"); librdf_storage_file_instance* context; context = LIBRDF_CALLOC(librdf_storage_file_instance*, 1, sizeof(*context)); if(!context) goto done; librdf_storage_set_instance(storage, context); /* Cannot save contexts in a file; pass everything else on */ contexts = librdf_hash_get_del(options, "contexts"); if(contexts) LIBRDF_FREE(char*, contexts); context->format_name = librdf_hash_get_del(options, "format"); if(context->format_name) { /* for 'file' and 'uri' storage, check this is a valid parser * for 'file' storage, also check this is a valid serializer */ if(!librdf_parser_check_name(storage->world, context->format_name) || (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) { librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL, "Ignoring storage %s format option '%s' - using default format '%s'", storage->factory->name, context->format_name, format_name); LIBRDF_FREE(char*, context->format_name); context->format_name = NULL; } if(context->format_name) format_name = context->format_name; }
/* functions implementing storage api */ static int librdf_storage_cassandra_init(librdf_storage* storage, const char *name, librdf_hash* options) { char *name_copy; librdf_storage_cassandra_instance* context; if(!name) { if(options) librdf_free_hash(options); return 1; } context = LIBRDF_CALLOC(librdf_storage_cassandra_instance*, 1, sizeof(*context)); if(!context) { if(options) librdf_free_hash(options); return 1; } librdf_storage_set_instance(storage, context); context->storage = storage; context->name_len = strlen(name); // context->transaction = 0; name_copy = LIBRDF_MALLOC(char*, context->name_len + 1); if(!name_copy) { if(options) librdf_free_hash(options); return 1; } strcpy(name_copy, name); context->name = name_copy; // Add options here. /* no more options, might as well free them now */ if(options) librdf_free_hash(options); // FIXME: Hard-coded; context->session = cass_session_new(); context->cluster = cass_cluster_new(); cass_cluster_set_contact_points(context->cluster, name); CassFuture* future = cass_session_connect(context->session, context->cluster); CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { fprintf(stderr, "Cassandra: %s\n", cass_error_desc(rc)); cass_cluster_free(context->cluster); cass_session_free(context->session); free(context->name); free(context); return 1; } cass_future_free(future); return 0; }
/* functions implementing storage api */ static int librdf_storage_file_init(librdf_storage* storage, const char *name, librdf_hash* options) { char *name_copy; char *contexts; int rc = 1; int is_uri = !strcmp(storage->factory->name, "uri"); const char *format_name = (is_uri ? "guess" : "rdfxml"); librdf_storage_file_instance* context; context = (librdf_storage_file_instance*)LIBRDF_CALLOC(librdf_storage_file_instance, 1, sizeof(librdf_storage_file_instance)); if(!context) goto done; librdf_storage_set_instance(storage, context); /* Cannot save contexts in a file; pass everything else on */ contexts = librdf_hash_get_del(options, "contexts"); if(contexts) LIBRDF_FREE(cstring, contexts); context->format_name = librdf_hash_get_del(options, "format"); if(context->format_name) { /* for 'file' and 'uri' storage, check this is a valid parser * for 'file' storage, also check this is a valid serializer */ if(!librdf_parser_check_name(storage->world, context->format_name) || (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) { librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL, "Ignoring storage %s format option '%s' - using default format '%s'", storage->factory->name, context->format_name, format_name); LIBRDF_FREE(cstring, context->format_name); context->format_name = NULL; } if(context->format_name) format_name = context->format_name; } if(is_uri) context->uri = librdf_new_uri(storage->world, (const unsigned char*)name); else { context->name_len = strlen(name); name_copy = (char*)LIBRDF_MALLOC(cstring, context->name_len+1); if(!name_copy) goto done; strcpy(name_copy,name); context->name = name_copy; context->uri = librdf_new_uri_from_filename(storage->world, context->name); } context->storage = librdf_new_storage_with_options(storage->world, NULL, NULL, options); if(!context->storage) goto done; context->model = librdf_new_model(storage->world, context->storage, NULL); if(!context->model) goto done; if(is_uri || !access((const char*)context->name, F_OK)) { librdf_parser *parser; parser = librdf_new_parser(storage->world, format_name, NULL, NULL); if(!parser) { rc = 1; goto done; } librdf_parser_parse_into_model(parser, context->uri, NULL, context->model); librdf_free_parser(parser); } context->changed = 0; rc = 0; done: /* no more options, might as well free them now */ if(options) librdf_free_hash(options); return rc; }