Exemple #1
0
/*
 * raptor_abbrev_subject implementation
 *
 * The subject of triples, with all predicates and values
 * linked from them.
 *
 **/
raptor_abbrev_subject*
raptor_new_abbrev_subject(raptor_abbrev_node* node)
{
  raptor_abbrev_subject* subject;
  
  if(!(node->type == RAPTOR_IDENTIFIER_TYPE_RESOURCE ||
        node->type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS ||
        node->type == RAPTOR_IDENTIFIER_TYPE_ORDINAL)) {
    RAPTOR_FATAL1("Subject node must be a resource, blank, or ordinal\n");
    return NULL;
  }  
  
  subject = (raptor_abbrev_subject*)RAPTOR_CALLOC(raptor_subject, 1,
                                                  sizeof(raptor_abbrev_subject));

  if(subject) {
    subject->node = node;
    subject->node->ref_count++;
    subject->node->count_as_subject++;
    
    subject->node_type = NULL;

    subject->properties =
      raptor_new_avltree((raptor_data_compare_function)raptor_compare_abbrev_po,
                         (raptor_data_free_function)raptor_free_abbrev_po,
                         0);
#ifdef RAPTOR_DEBUG
    raptor_avltree_set_print_handler(subject->properties,
                                     (raptor_data_print_function)raptor_print_abbrev_po);
#endif

    subject->list_items =
      raptor_new_sequence((raptor_sequence_free_handler *)raptor_free_abbrev_node, NULL);

    if(!subject->node || !subject->properties || !subject->list_items) {
      raptor_free_abbrev_subject(subject);
      subject = NULL;
    }
  
  }

  return subject;
}
static int
rasqal_groupby_rowsource_process(rasqal_rowsource* rowsource,
                                 rasqal_groupby_rowsource_context* con)
{
  /* already processed */
  if(con->processed)
    return 0;

  con->processed = 1;

  /* Empty expression list - no need to read rows */
  if(!con->expr_seq || !con->expr_seq_size) {
    con->group_id++;
    return 0;
  }


  con->tree = raptor_new_avltree(rasqal_rowsource_groupby_literal_sequence_compare,
                                 (raptor_data_free_handler)rasqal_free_groupby_tree_node,
                                 /* flags */ 0);

  if(!con->tree)
    return 1;

  raptor_avltree_set_print_handler(con->tree,
                                   rasqal_rowsource_groupby_tree_print_node);
  

  while(1) {
    rasqal_row* row;
    
    row = rasqal_rowsource_read_row(con->rowsource);
    if(!row)
      break;

    rasqal_row_bind_variables(row, rowsource->query->vars_table);
    
    if(con->expr_seq) {
      raptor_sequence* literal_seq;
      rasqal_groupby_tree_node key;
      rasqal_groupby_tree_node* node;
      
      literal_seq = rasqal_expression_sequence_evaluate(rowsource->query,
                                                        con->expr_seq,
                                                        /* ignore_errors */ 0,
                                                        /* literal_seq */ NULL,
                                                        /* error_p */ NULL);
      
      if(!literal_seq) {
        /* FIXME - what to do on errors? */
        continue;
      }
      
      memset(&key, '\0', sizeof(key));
      key.con = con;
      key.literals = literal_seq;
      
      node = (rasqal_groupby_tree_node*)raptor_avltree_search(con->tree, &key);
      if(!node) {
        /* New Group */
        node = (rasqal_groupby_tree_node*)RASQAL_CALLOC(rasqal_groupby_tree_node, sizeof(*node), 1);
        if(!node) {
          raptor_free_sequence(literal_seq);
          return 1;
        }

        node->con = con;
        node->group_id = ++con->group_id;

        /* node now owns literal_seq */
        node->literals = literal_seq;

#ifdef HAVE_RAPTOR2_API
        node->rows = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print);
#else
        node->rows = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_row, (raptor_sequence_print_handler*)rasqal_row_print);
#endif
        if(!node->rows) {
          rasqal_free_groupby_tree_node(node);
          return 1;
        }

        /* after this, node is owned by con->tree */
        raptor_avltree_add(con->tree, node);
      } else
        raptor_free_sequence(literal_seq);
      
      row->group_id = node->group_id;

      /* after this, node owns the row */
      raptor_sequence_push(node->rows, row);

    }
  }

#ifdef RASQAL_DEBUG
  if(con->tree) {
    fputs("Grouping ", DEBUG_FH);
    raptor_avltree_print(con->tree, DEBUG_FH);
    fputs("\n", DEBUG_FH);
  }
#endif
  
  con->group_iterator = raptor_new_avltree_iterator(con->tree,
                                                    NULL, NULL,
                                                    1);
  con->group_row_index = 0;

  con->offset = 0;

  return 0;
}