示例#1
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;
}
示例#2
0
static int test_string_manipulation(librdf_world *world, librdf_hash *h) {

  const char *test_hash_array[]={"shape", "cube",
      "sides", "6", /* for testing get as long */
      "3d", "yes", /* testing bool */
      "colour", "red",
      "colour", "yellow",
      "creator", "rubik",
      NULL};

  const unsigned char* template_string=(const unsigned char*)"the shape is %{shape} and the sides are %{sides} created by %{creator}";
  const unsigned char* template_expected=(const unsigned char*)"the shape is cube and the sides are 6 created by rubik";
  const char * const test_hash_string="field1='value1', field2='\\'value2', field3='\\\\', field4='\\\\\\'', field5 = 'a' ";
  const char * filter_string[] = {"field1", NULL};

  librdf_iterator* iterator;
  librdf_hash_datum *key_hd;
  char *string_result;
  unsigned char *template_result;
  int b;
  long l;

  /*
   *
   * Test librdf_hash_from_array_of_strings
   *
   */
  fprintf(stdout, "Initializing hash from array of strings\n");
  if(librdf_hash_from_array_of_strings(h, test_hash_array)) {
    fprintf(stderr, "Failed to init hash from array of strings\n");
    return(1);
  }

  fprintf(stdout, "librdf_hash_from_array_of_strings success. total values: %d.", librdf_hash_values_count(h));
  fprintf(stdout, "\nresulting hash: ");
  librdf_hash_print(h, stdout);
  fputc('\n', stdout);

  fprintf(stdout, "\nresulting hash keys: ");
  librdf_hash_print_keys(h, stdout);
  fputc('\n', stdout);

  /*
   *
   * Test librdf_hash_get_as_boolean and librdf_hash_get_as_long
   *
   */
  key_hd=librdf_new_hash_datum(world, NULL, 0);

  iterator=librdf_hash_keys(h, key_hd);
  while(!librdf_iterator_end(iterator)) {
    librdf_hash_datum *k=(librdf_hash_datum*)librdf_iterator_get_key(iterator);
    char *key_string;

    key_string = LIBRDF_MALLOC(char*, k->size + 1);
    if(!key_string)
      break;
    strncpy(key_string, (char*)k->data, k->size);
    key_string[k->size]='\0';

    fprintf(stdout, "boolean value of key '%s' is ", key_string);
    b=librdf_hash_get_as_boolean(h, key_string);
    fprintf(stdout, "%d (0 F, -1 Bad, else T)\n", b);

    fprintf(stdout, "long value of key '%s' is ", key_string);
    l=librdf_hash_get_as_long(h, key_string);
    fprintf(stdout, "%ld (decimal, -1 Bad)\n", l);

    LIBRDF_FREE(char*, key_string);
    librdf_iterator_next(iterator);
  }
  if(iterator)
    librdf_free_iterator(iterator);
  librdf_free_hash_datum(key_hd);


  /*
   *
   * Test librdf_hash_from_string
   *
   */
  fprintf(stdout, "Initializing hash from string >>%s<<\n", test_hash_string);
  librdf_hash_from_string (h, test_hash_string);

  fprintf(stdout, "values count %d\n", librdf_hash_values_count(h));
  fprintf(stdout, "resulting: ");
  librdf_hash_print(h, stdout);
  fputc('\n', stdout);


  fprintf(stdout, "Converting hash back to a string");
  string_result=librdf_hash_to_string(h, NULL);

  /* Order is not guaranteed, so sadly we can't just do a full string comparison */
  if(!strstr(string_result, "field1='value1'")) {
    fprintf(stdout, "Did not see field1='value1' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field2='\\'value2'")) {
    fprintf(stdout, "Did not see field2='\\'value2'' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field3='\\\\'")) {
    fprintf(stdout, "Did not see field3='\\\\' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field4='\\\\\\'")) {
    fprintf(stdout, "Did not see field4='\\\\\\' in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else if(!strstr(string_result, "field5='a'")) {
    fprintf(stdout, "Did not see field5='a' in the generated string >>%s<<\n", string_result);
    return 0;
  } else {
    fprintf(stdout, "\nresulting in >>%s<<\n", string_result);
  }
  librdf_free_memory(string_result);

  fprintf(stdout, "Converting hash back to a string with filter");
  string_result=librdf_hash_to_string(h, filter_string);
  if(strstr(string_result, "field1")) {
    fprintf(stdout, "Was not expecting >>field1<< to be in the generated string >>%s<<\n",
        string_result);
    return 0;
  } else {
    fprintf(stdout, "\nresulting in >>%s<<\n", string_result);
  }
  librdf_free_memory(string_result);

  /*
   *
   * Test librdf_hash_interpret_template
   *
   */
  fprintf(stdout, "Substituting into template >>%s",
      template_string);
  template_result=librdf_hash_interpret_template(template_string, h,
      (const unsigned char*)"%{",
      (const unsigned char*)"}");
  if(strcmp((const char*)template_result, (const char*)template_expected)) {
    fprintf(stdout, "Templating failed. Result was >>%s<< but expected >>%s<<\n",
        template_result, template_expected);
    return 0;
  } else
    fprintf(stdout, "\nresulting in >>%s<<\n", template_result);

  LIBRDF_FREE(char*, template_result);

  return 0;
}