Exemplo n.º 1
0
static int
librdf_storage_trees_contains_statement(librdf_storage* storage, librdf_statement* statement)
{
  librdf_storage_trees_instance* context=(librdf_storage_trees_instance*)storage->instance;

  return (librdf_avltree_search(context->graph->spo_tree, statement) != NULL);
}
Exemplo n.º 2
0
/**
 * librdf_storage_trees_context_remove_statement:
 * @storage: #librdf_storage object
 * @context_node: #librdf_node object
 * @statement: #librdf_statement statement to remove
 *
 * Remove a statement from a storage context.
 * 
 * Return value: non 0 on failure
 **/
static int
librdf_storage_trees_context_remove_statement(librdf_storage* storage, 
                                              librdf_node* context_node,
                                              librdf_statement* statement) 
{
  librdf_storage_trees_instance* context=(librdf_storage_trees_instance*)storage->instance;
  librdf_storage_trees_graph* key=librdf_storage_trees_graph_new(storage, context_node);
  librdf_storage_trees_graph* graph=(librdf_storage_trees_graph*)
    librdf_avltree_search(context->contexts, &key);
  librdf_storage_trees_graph_free(key);
  if (graph) {
    return librdf_storage_trees_remove_statement_internal(graph, statement);
  } else {
    return -1;
  }
}
Exemplo n.º 3
0
/**
 * librdf_storage_trees_context_add_statement:
 * @storage: #librdf_storage object
 * @context_node: #librdf_node object
 * @statement: #librdf_statement statement to add
 *
 * Add a statement to a storage context.
 * 
 * Return value: non 0 on failure
 **/
static int
librdf_storage_trees_context_add_statement(librdf_storage* storage,
                                           librdf_node* context_node,
                                           librdf_statement* statement) 
{
  librdf_storage_trees_context* context=(librdf_storage_trees_context*)storage->context;
  
  librdf_storage_trees_graph* key=librdf_storage_trees_graph_new(storage, context_node);
  librdf_storage_trees_graph* graph=(librdf_storage_trees_graph*)
    librdf_avltree_search(context->contexts, key);
  
  if(graph) {
    librdf_storage_trees_graph_free(key);
  } else {
    librdf_avltree_add(context->contexts, key);
    graph=key;
  }
    
  return librdf_storage_trees_add_statement_internal(storage, graph, statement);
}
Exemplo n.º 4
0
int
main(int argc, char *argv[])
{
  const char *program = librdf_basename(argv[0]);
#define ITEM_COUNT 8
  const char *items[ITEM_COUNT+1] = { "ron", "amy", "jen", "bij", "jib", "daj", "jim", "def", NULL };
#define DELETE_COUNT 2
  const char *delete_items[DELETE_COUNT+1] = { "daj", "jim", NULL };
#define RESULT_COUNT (ITEM_COUNT-DELETE_COUNT)
  const char *results[RESULT_COUNT+1] = { "amy", "bij", /*"daj",*/ "def", "jen", "jib", /*"jim",*/"ron", NULL};

  librdf_world* world;
  librdf_avltree* tree;
  librdf_iterator* iter;
  visit_state vs;
  int i;
  
  world = librdf_new_world();
  tree = librdf_new_avltree(compare_strings,
                            NULL); /* no free as they are static pointers above */
  
  if(!tree) {
    fprintf(stderr, "%s: Failed to create tree\n", program);
    exit(1);
  }
  for(i = 0; items[i]; i++) {
    int rc;
    void* node;

#if LIBRDF_DEBUG > 1
    fprintf(stderr, "%s: Adding tree item '%s'\n", program, items[i]);
#endif
  
    rc = librdf_avltree_add(tree, (void*)items[i]);
    if(rc) {
      fprintf(stderr,
              "%s: Adding tree item %d '%s' failed, returning error %d\n",
              program, i, items[i], rc);
      exit(1);
    }

#ifdef LIBRDF_DEBUG
    librdf_avltree_check(tree);
#endif

    node = librdf_avltree_search(tree, (void*)items[i]);
    if(!node) {
      fprintf(stderr,
              "%s: Tree did NOT contain item %d '%s' as expected\n",
              program, i, items[i]);
      exit(1);
    }
  }

#if LIBRDF_DEBUG > 1
  fprintf(stderr, "%s: Printing tree\n", program);
  vs.fh = stderr;
  vs.count = 0;
  librdf_avltree_visit(tree, print_string, &vs);

  //fprintf(stderr, "%s: Dumping tree\n", program);
  //librdf_avltree_dump(tree, stderr, print_string);
#endif


  for(i = 0; delete_items[i]; i++) {
    int rc;

#if LIBRDF_DEBUG > 1
    fprintf(stderr, "%s: Deleting tree item '%s'\n", program, delete_items[i]);
#endif
  
    rc = librdf_avltree_delete(tree, (void*)delete_items[i]);
    if(!rc) {
      fprintf(stderr,
              "%s: Deleting tree item %d '%s' failed, returning error %d\n",
              program, i, delete_items[i], rc);
      exit(1);
    }

#ifdef LIBRDF_DEBUG
    librdf_avltree_check(tree);
#endif
  }


#if LIBRDF_DEBUG > 1
  fprintf(stderr, "%s: Walking tree forwards via iterator\n", program);
#endif
  iter = librdf_avltree_get_iterator_start(world, tree, NULL, NULL);
  for(i = 0; 1; i++) {
    const char* data = (const char*)librdf_iterator_get_object(iter);
    const char* result = results[i];
    if((!data && data != result) || (data && strcmp(data, result))) {
      fprintf(stderr, "%3d: Forwards iterator expected '%s' but found '%s'\n",
              i, result, data);
      exit(1);
    }
#if LIBRDF_DEBUG > 1
    fprintf(stderr, "%3d: Got '%s'\n", i, data);
#endif
    if(librdf_iterator_next(iter))
      break;

    if(i > RESULT_COUNT) {
      fprintf(stderr, "Forward iterator did not end on result %i as expected\n", i);
      exit(1);
    }
  }


#if LIBRDF_DEBUG > 1
  fprintf(stderr, "%s: Checking tree\n", program);
#endif
  vs.count = 0;
  vs.results=results;
  vs.failed = 0;
  librdf_avltree_visit(tree, check_string, &vs);
  if(vs.failed) {
    fprintf(stderr, "%s: Checking tree failed\n", program);
    exit(1);
  }

  
  for(i = 0; results[i]; i++) {
    const char* result = results[i];
    char* data = (char*)librdf_avltree_remove(tree, (void*)result);
    if(!data) {
      fprintf(stderr, "%s: remove %i failed at item '%s'\n", program, i,
              result);
      exit(1);
    }
    if(strcmp(data, result)) {
      fprintf(stderr, "%s: remove %i returned %s not %s as expected\n", program,
              i, data, result);
      exit(1);
    }
  }
  

#if LIBRDF_DEBUG > 1
  fprintf(stderr, "%s: Freeing tree\n", program);
#endif
  librdf_free_avltree(tree);
  librdf_free_world(world);

  /* keep gcc -Wall happy */
  return(0);
}