From 36c03b3f4e878f2a207cb08e80b06a1503568ed0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 16 Feb 2011 01:11:21 +0000 Subject: Simpler and more typical node creation API. git-svn-id: http://svn.drobilla.net/sord/trunk@33 3d64ff67-21c5-427c-a301-fe4f08042e5a --- sord/sord.h | 12 +++++----- src/sord.c | 71 +++++++++++++++++++++++++++------------------------------ src/sord_test.c | 32 +++++++++++++------------- src/syntax.c | 10 ++++---- 4 files changed, 60 insertions(+), 65 deletions(-) diff --git a/sord/sord.h b/sord/sord.h index a2fbefe..3412afa 100644 --- a/sord/sord.h +++ b/sord/sord.h @@ -118,37 +118,37 @@ sord_free(Sord sord); */ SORD_API SordNode -sord_get_uri(Sord sord, bool create, const uint8_t* str); +sord_new_uri(Sord sord, const uint8_t* str); /** Find a URI, creating a new one if necessary iff @a create is true. */ SORD_API SordNode -sord_get_uri_counted(Sord sord, bool create, const uint8_t* str, int str_len); +sord_new_uri_counted(Sord sord, const uint8_t* str, int str_len); /** Find a blank, creating a new one if necessary iff @a create is true * Use sord_get_blank_counted instead if the length of @a str is known. */ SORD_API SordNode -sord_get_blank(Sord sord, bool create, const uint8_t* str); +sord_new_blank(Sord sord, const uint8_t* str); /** Find a blank, creating a new one if necessary iff @a create is true. */ SORD_API SordNode -sord_get_blank_counted(Sord sord, bool create, const uint8_t* str, int str_len); +sord_new_blank_counted(Sord sord, const uint8_t* str, int str_len); /** Find a literal, creating a new one if necessary iff @a create is true. * Use sord_get_literal_counted instead if the length of @a str is known. */ SORD_API SordNode -sord_get_literal(Sord sord, bool create, SordNode type, +sord_new_literal(Sord sord, SordNode datatype, const uint8_t* str, const char* lang); /** Find a literal, creating a new one if necessary iff @a create is true. */ SORD_API SordNode -sord_get_literal_counted(Sord sord, bool create, SordNode type, +sord_new_literal_counted(Sord sord, SordNode datatype, const uint8_t* str, int str_len, const char* lang, uint8_t lang_len); diff --git a/src/sord.c b/src/sord.c index 1596086..56a7c41 100644 --- a/src/sord.c +++ b/src/sord.c @@ -811,71 +811,66 @@ sord_add_node(Sord sord, SordNode node) } SordNode -sord_get_uri_counted(Sord sord, bool create, const uint8_t* str, int str_len) +sord_new_uri_counted(Sord sord, const uint8_t* str, int str_len) { - SordNode id = sord_lookup_name(sord, str, str_len); - if (id || !create) - return id; - - id = sord_new_node(SORD_URI, (const uint8_t*)str, str_len + 1); + SordNode node = sord_lookup_name(sord, str, str_len); + if (node) { + return node; + } - assert(id); - g_hash_table_insert(sord->names, (void*)g_strdup((const char*)str), (void*)id); - sord_add_node(sord, id); - - return id; + node = sord_new_node(SORD_URI, (const uint8_t*)str, str_len + 1); + g_hash_table_insert(sord->names, g_strdup((const char*)str), node); + sord_add_node(sord, node); + return node; } SordNode -sord_get_uri(Sord sord, bool create, const uint8_t* str) +sord_new_uri(Sord sord, const uint8_t* str) { - return sord_get_uri_counted(sord, create, str, strlen((const char*)str)); + return sord_new_uri_counted(sord, str, strlen((const char*)str)); } SordNode -sord_get_blank_counted(Sord sord, bool create, const uint8_t* str, int str_len) +sord_new_blank_counted(Sord sord, const uint8_t* str, int str_len) { - SordNode id = sord_lookup_name(sord, str, str_len); - if (id || !create) - return id; - - id = sord_new_node(SORD_BLANK, (const uint8_t*)str, str_len + 1); + SordNode node = sord_lookup_name(sord, str, str_len); + if (node) { + return node; + } - assert(id); - g_hash_table_insert(sord->names, (void*)g_strdup((const char*)str), (void*)id); - sord_add_node(sord, id); - - return id; + node = sord_new_node(SORD_BLANK, (const uint8_t*)str, str_len + 1); + g_hash_table_insert(sord->names, g_strdup((const char*)str), node); + sord_add_node(sord, node); + return node; } SordNode -sord_get_blank(Sord sord, bool create, const uint8_t* str) +sord_new_blank(Sord sord, const uint8_t* str) { - return sord_get_blank_counted(sord, create, str, strlen((const char*)str)); + return sord_new_blank_counted(sord, str, strlen((const char*)str)); } SordNode -sord_get_literal_counted(Sord sord, bool create, SordNode type, +sord_new_literal_counted(Sord sord, SordNode type, const uint8_t* str, int str_len, const char* lang, uint8_t lang_len) { - SordNode id = sord_lookup_literal(sord, type, str, str_len, lang, lang_len); - if (id || !create) - return id; - - id = sord_new_literal_node(sord, type, str, str_len, lang, lang_len); + SordNode node = sord_lookup_literal(sord, type, str, str_len, lang, lang_len); + if (node) { + return node; + } - g_hash_table_insert(sord->literals, (void*)id, id); - sord_add_node(sord, id); - - return id; + node = sord_new_literal_node(sord, type, str, str_len, lang, lang_len); + g_hash_table_insert(sord->literals, node, node); // FIXME: correct? + sord_add_node(sord, node); + return node; } SordNode -sord_get_literal(Sord sord, bool create, SordNode type, +sord_new_literal(Sord sord, SordNode type, const uint8_t* str, const char* lang) { - return sord_get_literal_counted(sord, create, type, + return sord_new_literal_counted(sord, type, str, strlen((const char*)str), lang, lang ? strlen(lang) : 0); } diff --git a/src/sord_test.c b/src/sord_test.c index ac6abbe..1172069 100644 --- a/src/sord_test.c +++ b/src/sord_test.c @@ -39,7 +39,7 @@ uri(Sord sord, int num) const size_t uri_len = 3 + DIGITS; char* uri_num = uri + 3; // First `0' snprintf(uri_num, DIGITS + 1, "%0*d", DIGITS, num); - return sord_get_uri_counted(sord, true, (const uint8_t*)uri, uri_len); + return sord_new_uri_counted(sord, (const uint8_t*)uri, uri_len); } void @@ -66,16 +66,16 @@ generate(Sord sord, size_t n_quads, size_t n_objects_per) SordQuad tup; tup[0] = uri(sord, 98); tup[1] = uri(sord, 4); - tup[2] = sord_get_literal(sord, true, 0, (const uint8_t*)"hello", NULL); + tup[2] = sord_new_literal(sord, 0, (const uint8_t*)"hello", NULL); tup[3] = 0; sord_add(sord, tup); - tup[2] = sord_get_literal(sord, true, 0, USTR("hi"), NULL); + tup[2] = sord_new_literal(sord, 0, USTR("hi"), NULL); sord_add(sord, tup); tup[0] = uri(sord, 14); - tup[2] = sord_get_literal(sord, true, 0, USTR("bonjour"), "fr"); + tup[2] = sord_new_literal(sord, 0, USTR("bonjour"), "fr"); sord_add(sord, tup); - tup[2] = sord_get_literal(sord, true, 0, USTR("salut"), "fr"); + tup[2] = sord_new_literal(sord, 0, USTR("salut"), "fr"); sord_add(sord, tup); // Attempt to add some duplicates @@ -83,7 +83,7 @@ generate(Sord sord, size_t n_quads, size_t n_objects_per) sord_add(sord, tup); // Add a blank node subject - tup[0] = sord_get_blank(sord, true, USTR("ablank")); + tup[0] = sord_new_blank(sord, USTR("ablank")); sord_add(sord, tup); tup[1] = uri(sord, 6); @@ -168,7 +168,7 @@ test_read(Sord sord, const size_t n_quads, const int n_objects_per) } // Query blank node subject - SordQuad pat = { sord_get_blank(sord, true, USTR("ablank")), 0, 0 }; + SordQuad pat = { sord_new_blank(sord, USTR("ablank")), 0, 0 }; if (!pat[0]) { fprintf(stderr, "Blank node subject lost\n"); return test_fail(); @@ -274,13 +274,13 @@ main(int argc, char** argv) } // Check interning merges equivalent values - SordNode uri_id = sord_get_uri(sord, true, USTR("http://example.org")); - SordNode blank_id = sord_get_uri(sord, true, USTR("testblank")); - SordNode lit_id = sord_get_literal(sord, true, uri_id, USTR("hello"), NULL); + SordNode uri_id = sord_new_uri(sord, USTR("http://example.org")); + SordNode blank_id = sord_new_uri(sord, USTR("testblank")); + SordNode lit_id = sord_new_literal(sord, uri_id, USTR("hello"), NULL); //sord_clear_cache(write); - SordNode uri_id2 = sord_get_uri(sord, false, USTR("http://example.org")); - SordNode blank_id2 = sord_get_uri(sord, false, USTR("testblank")); - SordNode lit_id2 = sord_get_literal(sord, false, uri_id, USTR("hello"), NULL); + SordNode uri_id2 = sord_new_uri(sord, USTR("http://example.org")); + SordNode blank_id2 = sord_new_uri(sord, USTR("testblank")); + SordNode lit_id2 = sord_new_literal(sord, uri_id, USTR("hello"), NULL); if (uri_id2 != uri_id) { fprintf(stderr, "Fail: URI interning failed (duplicates)\n"); goto fail; @@ -293,9 +293,9 @@ main(int argc, char** argv) } // Check interning doesn't clash non-equivalent values - SordNode uri_id3 = sord_get_uri(sord, false, USTR("http://example.orgX")); - SordNode blank_id3 = sord_get_uri(sord, false, USTR("testblankX")); - SordNode lit_id3 = sord_get_literal(sord, false, uri_id, USTR("helloX"), NULL); + SordNode uri_id3 = sord_new_uri(sord, USTR("http://example.orgX")); + SordNode blank_id3 = sord_new_uri(sord, USTR("testblankX")); + SordNode lit_id3 = sord_new_literal(sord, uri_id, USTR("helloX"), NULL); if (uri_id3 == uri_id) { fprintf(stderr, "Fail: URI interning failed (clash)\n"); goto fail; diff --git a/src/syntax.c b/src/syntax.c index c06882a..1cff77b 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -99,12 +99,12 @@ sord_node_from_serd_node(ReadState* state, const SerdNode* sn) case SERD_NOTHING: return NULL; case SERD_LITERAL: - return sord_get_literal(state->sord, true, NULL, sn->buf, NULL); + return sord_new_literal(state->sord, NULL, sn->buf, NULL); case SERD_URI: { SerdURI abs_uri; SerdNode abs_uri_node = serd_node_new_uri_from_node( sn, &state->base_uri, &abs_uri); - SordNode ret = sord_get_uri(state->sord, true, abs_uri_node.buf); + SordNode ret = sord_new_uri(state->sord, abs_uri_node.buf); serd_node_free(&abs_uri_node); return ret; } @@ -120,15 +120,15 @@ sord_node_from_serd_node(ReadState* state, const SerdNode* sn) memcpy(buf, uri_prefix.buf, uri_prefix.len); memcpy(buf + uri_prefix.len, uri_suffix.buf, uri_suffix.len); buf[uri_len] = '\0'; - SordNode ret = sord_get_uri_counted(state->sord, true, - buf, uri_prefix.len + uri_suffix.len); + SordNode ret = sord_new_uri_counted(state->sord, + buf, uri_prefix.len + uri_suffix.len); free(buf); return ret; } case SERD_BLANK_ID: case SERD_ANON_BEGIN: case SERD_ANON: - return sord_get_blank(state->sord, true, sn->buf); + return sord_new_blank(state->sord, sn->buf); } return NULL; } -- cgit v1.2.1