static int raptor_json_serialize_start(raptor_serializer* serializer) { raptor_json_context* context = (raptor_json_context*)serializer->context; raptor_uri* base_uri; char* value; base_uri = RAPTOR_OPTIONS_GET_NUMERIC(serializer, RAPTOR_OPTION_RELATIVE_URIS) ? serializer->base_uri : NULL; context->json_writer = raptor_new_json_writer(serializer->world, base_uri, serializer->iostream); if(!context->json_writer) return 1; if(context->is_resource) { context->avltree = raptor_new_avltree((raptor_data_compare_handler)raptor_statement_compare, (raptor_data_free_handler)raptor_free_statement, 0); if(!context->avltree) { raptor_free_json_writer(context->json_writer); context->json_writer = NULL; return 1; } } /* start callback */ value = RAPTOR_OPTIONS_GET_STRING(serializer, RAPTOR_OPTION_JSON_CALLBACK); if(value) { raptor_iostream_string_write(value, serializer->iostream); raptor_iostream_write_byte('(', serializer->iostream); } if(!context->is_resource) { /* start outer object */ raptor_json_writer_start_block(context->json_writer, '{'); raptor_json_writer_newline(context->json_writer); /* start triples array */ raptor_iostream_counted_string_write((const unsigned char*)"\"triples\" : ", 12, serializer->iostream); raptor_json_writer_start_block(context->json_writer, '['); raptor_json_writer_newline(context->json_writer); } return 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; }
/* create a new serializer */ static int raptor_turtle_serialize_init(raptor_serializer* serializer, const char *name) { raptor_turtle_context* context=(raptor_turtle_context*)serializer->context; raptor_uri *rdf_type_uri; context->nstack=raptor_new_namespaces_v2(serializer->world, (raptor_simple_message_handler)raptor_serializer_simple_error, serializer, 1); if(!context->nstack) return 1; context->rdf_nspace=raptor_new_namespace(context->nstack, (const unsigned char*)"rdf", (const unsigned char*)raptor_rdf_namespace_uri, 0); context->namespaces=raptor_new_sequence(NULL, NULL); context->subjects = raptor_new_avltree(serializer->world, (raptor_data_compare_function)raptor_abbrev_subject_cmp, (raptor_data_free_function)raptor_free_abbrev_subject, 0); context->blanks = raptor_new_avltree(serializer->world, (raptor_data_compare_function)raptor_abbrev_subject_cmp, (raptor_data_free_function)raptor_free_abbrev_subject, 0); context->nodes = raptor_new_avltree(serializer->world, (raptor_data_compare_function)raptor_abbrev_node_cmp, (raptor_data_free_function)raptor_free_abbrev_node, 0); rdf_type_uri = raptor_new_uri_for_rdf_concept_v2(serializer->world, "type"); if(rdf_type_uri) { context->rdf_type = raptor_new_abbrev_node(serializer->world, RAPTOR_IDENTIFIER_TYPE_RESOURCE, rdf_type_uri, NULL, NULL); raptor_free_uri_v2(serializer->world, rdf_type_uri); } else context->rdf_type = NULL; context->rdf_xml_literal_uri=raptor_new_uri_v2(serializer->world, raptor_xml_literal_datatype_uri_string); context->rdf_first_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#first"); context->rdf_rest_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"); context->rdf_nil_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"); if(!context->rdf_nspace || !context->namespaces || !context->subjects || !context->blanks || !context->nodes || !context->rdf_xml_literal_uri || !context->rdf_first_uri || !context->rdf_rest_uri || !context->rdf_nil_uri || !context->rdf_type) { raptor_turtle_serialize_terminate(serializer); return 1; } /* Note: item 0 in the list is rdf:RDF's namespace */ if(raptor_sequence_push(context->namespaces, context->rdf_nspace)) { raptor_turtle_serialize_terminate(serializer); return 1; } return 0; }
/** * raptor_id_set_add: * @set: #raptor_id_set * @base_uri: base #raptor_uri of identifier * @id: identifier name * @id_len: length of identifier * * INTERNAL - Add an item to the set. * * Return value: <0 on failure, 0 on success, 1 if already present **/ int raptor_id_set_add(raptor_id_set* set, raptor_uri *base_uri, const unsigned char *id, size_t id_len) { raptor_base_id_set *base; char* item; if(!base_uri || !id || !id_len) return -1; base = set->first; while(base) { if(raptor_uri_equals(base->uri, base_uri)) break; base = base->next; } if(!base) { /* a set for this base_uri not found */ base = (raptor_base_id_set*)RAPTOR_CALLOC(raptor_base_id_set, 1, sizeof(*base)); if(!base) return -1; base->world = set->world; base->uri = raptor_uri_copy(base_uri); base->tree = raptor_new_avltree((raptor_data_compare_handler)strcmp, free, 0); /* Add to the start of the list */ if(set->first) set->first->prev = base; /* base->prev = NULL; */ base->next = set->first; set->first = base; } else { /* If not at the start of the list, move there */ if(base != set->first) { /* remove from the list */ base->prev->next = base->next; if(base->next) base->next->prev = base->prev; /* add at the start of the list */ set->first->prev = base; base->prev = NULL; base->next = set->first; } } item = (char*)raptor_avltree_search(base->tree, id); /* if already there, error */ if(item) { #if RAPTOR_DEBUG > 1 set->misses++; #endif return 1; } #if RAPTOR_DEBUG > 1 set->hits++; #endif item = (char*)RAPTOR_MALLOC(cstring, id_len+1); if(!item) return 1; memcpy(item, id, id_len + 1); return raptor_avltree_add(base->tree, item); }
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; }
int main(int argc, char *argv[]) { raptor_world *world; const char *program = raptor_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] = { "jen", "jim", NULL }; #define RESULT_COUNT (ITEM_COUNT-DELETE_COUNT) const char *results[RESULT_COUNT+1] = { "amy", "bij", "daj", "def", "jib", "ron", NULL}; raptor_avltree* tree; raptor_avltree_iterator* iter; visit_state vs; int i; world = raptor_new_world(); if(!world || raptor_world_open(world)) exit(1); tree = raptor_new_avltree(compare_strings, NULL, /* no free as they are static pointers above */ 0); if(!tree) { fprintf(stderr, "%s: Failed to create tree\n", program); exit(1); } for(i = 0; items[i]; i++) { int rc; void* node; #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Adding tree item '%s'\n", program, items[i]); #endif rc = raptor_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 RAPTOR_DEBUG raptor_avltree_check(tree); #endif node = raptor_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 RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Printing tree\n", program); vs.fh = stderr; vs.count = 0; raptor_avltree_visit(tree, print_string, &vs); fprintf(stderr, "%s: Dumping tree\n", program); raptor_avltree_dump(tree, stderr); #endif for(i = 0; delete_items[i]; i++) { int rc; #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Deleting tree item '%s'\n", program, delete_items[i]); #endif rc = raptor_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 RAPTOR_DEBUG raptor_avltree_check(tree); #endif } #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Walking tree forwards via iterator\n", program); #endif iter = raptor_new_avltree_iterator(tree, NULL, NULL, 1); for(i = 0; 1; i++) { const char* data = (const char*)raptor_avltree_iterator_get(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 RAPTOR_DEBUG > 1 fprintf(stderr, "%3d: Got '%s'\n", i, data); #endif if(raptor_avltree_iterator_next(iter)) break; if(i > RESULT_COUNT) { fprintf(stderr, "Forward iterator did not end on result %i as expected\n", i); exit(1); } } raptor_free_avltree_iterator(iter); #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Checking tree\n", program); #endif vs.count = 0; vs.results = results; vs.failed = 0; raptor_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*)raptor_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 RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Freeing tree\n", program); #endif raptor_free_avltree(tree); raptor_free_world(world); /* keep gcc -Wall happy */ return(0); }