Ejemplo n.º 1
0
/**
 * librdf_storage_find_statements_in_context - search the storage for matching statements in a given context
 * @storage: &librdf_storage object
 * @statement: &librdf_statement partial statement to find
 * @context_node: context &librdf_node (or NULL)
 * 
 * Searches the storage for a (partial) statement as described in
 * librdf_statement_match() in the given context and returns a
 * &librdf_stream of matching &librdf_statement objects.  If
 * context is NULL, this is equivalent to librdf_storage_find_statements.
 * 
 * Return value: &librdf_stream of matching statements (may be empty) or NULL on failure
 **/
librdf_stream*
librdf_storage_find_statements_in_context(librdf_storage* storage, librdf_statement* statement, librdf_node* context_node) 
{
  librdf_stream *stream;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(storage, librdf_storage, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, NULL);

  if(storage->factory->find_statements_in_context)
    return storage->factory->find_statements_in_context(storage, statement, context_node);

  stream=librdf_storage_context_as_stream(storage, context_node);
  if(!stream)
    return NULL;

  librdf_stream_add_map(stream, 
                        &librdf_stream_statement_find_map,
                        (librdf_stream_map_free_context_handler)&librdf_free_statement, (void*)statement);

  return stream;
}
Ejemplo n.º 2
0
static librdf_stream*
librdf_storage_trees_serialise_range(librdf_storage* storage, librdf_statement* range)
{
  librdf_storage_trees_instance* context=(librdf_storage_trees_instance*)storage->instance;
  librdf_storage_trees_serialise_stream_context* scontext;
  librdf_stream* stream;
  int filter = 0;
  
  scontext=(librdf_storage_trees_serialise_stream_context*)LIBRDF_CALLOC(librdf_storage_trees_serialise_stream_context, 1, sizeof(librdf_storage_trees_serialise_stream_context));
  if(!scontext)
    return NULL;
    
  scontext->iterator = NULL;

  /* ?s ?p ?o */
  if (!range || (!range->subject && !range->predicate && !range->object)) {
    scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->spo_tree,
        NULL, NULL);
    if (range) {
      librdf_free_statement(range);
      range=NULL;
    }
  /* s ?p o */
  } else if (range->subject && !range->predicate && range->object) {
    if (context->index_sop)
      scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->sop_tree,
        range, librdf_storage_trees_avl_free);
	else
		filter=1;
  /* s _ _ */
  } else if (range->subject) {
    scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->spo_tree,
        range, librdf_storage_trees_avl_free);
  /* ?s _ o */
  } else if (range->object) {
    if (context->index_ops)
      scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->ops_tree,
          range, librdf_storage_trees_avl_free);
	else
		filter=1;
  /* ?s p ?o */
  } else { /* range->predicate != NULL */
    if (context->index_pso)
      scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->pso_tree,
          range, librdf_storage_trees_avl_free);
	else
		filter=1;
  }
    
  /* If filter is set, we're missing the required index.
   * Iterate over the entire model and filter the stream.
   * (With a fully indexed store, this will never happen) */
  if (filter) {
    scontext->iterator=librdf_avltree_get_iterator_start(storage->world, context->graph->spo_tree,
        range, librdf_storage_trees_avl_free);
  }

#ifdef RDF_STORAGE_TREES_WITH_CONTEXTS
  scontext->context_node=NULL;
#endif

  if(!scontext->iterator) {
    LIBRDF_FREE(librdf_storage_trees_serialise_stream_context, scontext);
    return librdf_new_empty_stream(storage->world);
  }
  
  scontext->storage=storage;
  librdf_storage_add_reference(scontext->storage);

  stream=librdf_new_stream(storage->world,
                           (void*)scontext,
                           &librdf_storage_trees_serialise_end_of_stream,
                           &librdf_storage_trees_serialise_next_statement,
                           &librdf_storage_trees_serialise_get_statement,
                           &librdf_storage_trees_serialise_finished);
  
  if(!stream) {
    librdf_storage_trees_serialise_finished((void*)scontext);
    return NULL;
  }

  if(filter) {
    if(librdf_stream_add_map(stream, &librdf_stream_statement_find_map, NULL, (void*)range)) {
      /* error - stream_add_map failed */
      librdf_free_stream(stream);
      stream=NULL;
    }
  }
  
  return stream;  
}