Exemplo n.º 1
0
/**
 * librdf_node_new_static_node_iterator:
 * @world: world object
 * @nodes: static array of #librdf_node objects
 * @size: size of array
 *
 * Create an iterator over an array of nodes.
 * 
 * This creates an iterator for an existing static array of librdf_node
 * objects.  It is mostly intended for testing iterator code.
 * 
 * Return value: a #librdf_iterator serialization of the nodes or NULL on failure
 **/
librdf_iterator*
librdf_node_new_static_node_iterator(librdf_world* world, librdf_node** nodes,
                                     int size)
{
  librdf_node_static_iterator_context* context;
  librdf_iterator* iterator;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(nodes, librdf_node**, NULL);

  context = (librdf_node_static_iterator_context*)LIBRDF_CALLOC(librdf_node_static_iterator_context, 1, sizeof(*context));
  if(!context)
    return NULL;

  context->nodes = nodes;
  context->size = size;
  context->current = 0;

  iterator = librdf_new_iterator(world,
                                 (void*)context,
                                 librdf_node_static_iterator_is_end,
                                 librdf_node_static_iterator_next_method,
                                 librdf_node_static_iterator_get_method,
                                 librdf_node_static_iterator_finished);
  if(!iterator)
    librdf_node_static_iterator_finished(context);

  return iterator;
}
Exemplo n.º 2
0
/**
 * librdf_list_get_iterator:
 * @list: #librdf_list object
 *
 * Get an iterator for the list.
 * 
 * Return value: a new #librdf_iterator object or NULL on failure
 **/
librdf_iterator*
librdf_list_get_iterator(librdf_list* list)
{
  librdf_list_iterator_context* context;
  librdf_iterator* iterator;

  context = LIBRDF_CALLOC(librdf_list_iterator_context*, 1, sizeof(*context));
  if(!context)
    return NULL;

  context->list=list;
  context->current=list->first;
  context->next=context->current != NULL ? context->current->next : NULL;

  /* librdf_list_iterator_finished() calls librdf_list_remove_iterator_context(),
   * librdf_list_iterator_finished() is called if librdf_new_iterator() fails */
  librdf_list_add_iterator_context(list, context);

  iterator=librdf_new_iterator(list->world, 
                               (void*)context,
                               librdf_list_iterator_is_end,
                               librdf_list_iterator_next_method,
                               librdf_list_iterator_get_method,
                               librdf_list_iterator_finished);
  if(!iterator)
    librdf_list_iterator_finished(context);
  else
    context->iterator=iterator;

  return iterator;
}
Exemplo n.º 3
0
/**
 * librdf_avltree_get_iterator_start:
 * @list: #librdf_avltree object
 *
 * Get an (in-order) iterator for the start of a range, or the entire tree
 * (if range is NULL).  If range specifies a range (i.e. the tree comparison
 * function will 'match' (return 0 for) range and /several/ nodes), the
 * iterator will be placed at the leftmost child matching range, and
 * librdf_avltree_iterator_next will iterate over all nodes
 * (and only nodes) that match range.
 * 
 * Return value: a new #librdf_iterator object or NULL on failure
 **/
librdf_iterator*
librdf_avltree_get_iterator_start(librdf_world* world, librdf_avltree* tree,
                                  void* range,
                                  librdf_avltree_data_free_function range_free_fn)
{
  librdf_avltree_iterator_context* context;
  librdf_iterator* iterator;

  context = (librdf_avltree_iterator_context*)LIBRDF_CALLOC(librdf_avltree_iterator_context, 1, sizeof(*context));
  if(!context)
    return NULL;

  context->tree = tree;
  context->range = range;
  context->range_free_fn = range_free_fn;

  if(range != NULL) {
    /* find the topmost match (range is contained entirely in tree rooted here) */
    context->current = librdf_avltree_search_internal(tree, tree->root, range);
  } else {
    context->current=tree->root;
  }

  context->root = context->current;
  
  /* go down to find start of range (or tree) */
  if(context->current) {
    while(1) {
      librdf_avltree_node* pred;

      context->current = librdf_avltree_node_leftmost(tree, context->current,
                                                      range);
      /* right until we find a match */
      pred = librdf_avltree_node_search_right(tree, context->current->left,
                                              range);

      if(pred && tree->compare_fn(range, pred->data) == 0)
        context->current = pred;
      else
        break;
    }
  }

  iterator=librdf_new_iterator(world, 
                               (void*)context,
                               librdf_avltree_iterator_is_end,
                               librdf_avltree_iterator_next_method,
                               librdf_avltree_iterator_get_method,
                               librdf_avltree_iterator_finished);
  
  if(!iterator) {
    librdf_avltree_iterator_finished(context);
  }

  return iterator;
}
Exemplo n.º 4
0
/**
 * librdf_list_get_iterator - get an iterator for the list
 * @list: &librdf_list object
 * 
 * Return value: a new &librdf_iterator object or NULL on failure
 **/
librdf_iterator*
librdf_list_get_iterator(librdf_list* list)
{
  librdf_list_iterator_context* context;

  context=(librdf_list_iterator_context*)LIBRDF_CALLOC(librdf_list_iterator_context, 1, sizeof(librdf_list_iterator_context));
  if(!context)
    return NULL;

  context->list=list;
  context->current=list->first;
  
  return librdf_new_iterator(list->world, 
                             (void*)context,
                             librdf_list_iterator_is_end,
                             librdf_list_iterator_next_method,
                             librdf_list_iterator_get_method,
                             librdf_list_iterator_finished);
  
}
Exemplo n.º 5
0
/*
 * librdf_storage_node_stream_to_node_create - Create a stream for get sources, targets or arcs methods using find_statements method
 * @storage: the storage object to use
 * @node1: the first node to encode in the key
 * @node2: the second node to encode in the key
 * @want: the field required from the statement
 * 
 * Return value: a new &librdf_iterator or NULL on failure
 **/
static librdf_iterator*
librdf_storage_node_stream_to_node_create(librdf_storage* storage,
                                          librdf_node *node1,
                                          librdf_node *node2,
                                          librdf_statement_part want)
{
  librdf_statement *partial_statement;
  librdf_stream *stream;
  librdf_storage_stream_to_node_iterator_context* context;
  librdf_iterator *iterator;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(storage, librdf_storage, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(node1, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(node2, librdf_node, NULL);

  partial_statement=librdf_new_statement(storage->world);
  if(!partial_statement)
    return NULL;
  
  context=(librdf_storage_stream_to_node_iterator_context*)LIBRDF_CALLOC(librdf_storage_stream_to_node_iterator_context, 1, sizeof(librdf_storage_stream_to_node_iterator_context));
  if(!context) {
    librdf_free_statement(partial_statement);
    return NULL;
  }

  switch(want) {
    case LIBRDF_STATEMENT_SUBJECT:
      librdf_statement_set_predicate(partial_statement, node1);
      librdf_statement_set_object(partial_statement, node2);
      break;
    case LIBRDF_STATEMENT_PREDICATE:
      librdf_statement_set_subject(partial_statement, node1);
      librdf_statement_set_object(partial_statement, node2);
      break;
    case LIBRDF_STATEMENT_OBJECT:
      librdf_statement_set_subject(partial_statement, node1);
      librdf_statement_set_predicate(partial_statement, node2);
      break;
    default:
      librdf_free_statement(partial_statement);
      LIBRDF_ERROR2(storage->world, "Illegal statement part %d seen\n", want);
      return NULL;
  }
  
  stream=storage->factory->find_statements(storage, partial_statement);
  if(!stream) {
    librdf_storage_stream_to_node_iterator_finished(context);
    return NULL;
  }
  
  /* initialise context */
  context->partial_statement=partial_statement;
  context->stream=stream;
  context->want=want;

  context->storage=storage;
  librdf_storage_add_reference(context->storage);

  iterator=librdf_new_iterator(storage->world,
                               (void*)context,
                               librdf_storage_stream_to_node_iterator_is_end,
                               librdf_storage_stream_to_node_iterator_next_method,
                               librdf_storage_stream_to_node_iterator_get_method,
                               librdf_storage_stream_to_node_iterator_finished);
  if(!iterator)
    librdf_storage_stream_to_node_iterator_finished(context);

  return iterator;
}