Hash :
33ae4589
Author :
Thomas de Grivel
Date :
2023-03-27T20:20:27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <stdlib.h>
#include "facts_cursor.h"
#include "facts_spec.h"
#include "facts_with.h"
#include "tag.h"
s_facts_with_cursor * facts_with (s_facts *facts,
s_facts_with_cursor *cursor,
p_facts_spec spec)
{
uw facts_count;
assert(facts);
assert(cursor);
assert(spec);
facts_count = facts_spec_count_facts(spec);
cursor->facts = facts;
cursor->facts_count = facts_count;
if (facts_count > 0) {
cursor->levels = calloc(facts_count,
sizeof(s_facts_with_cursor_level));
cursor->spec = facts_spec_new_expand(spec);
/* facts_spec_sort(cursor->spec); */
}
else {
cursor->levels = NULL;
cursor->spec = NULL;
}
cursor->level = 0;
if (pthread_mutex_init(&cursor->mutex, NULL))
errx(1, "facts_with: pthread_mutex_init");
return cursor;
}
s_facts_cursor * facts_with_0 (s_facts *facts,
s_facts_cursor *cursor,
s_tag *var_subject,
s_tag *var_predicate,
s_tag *var_object)
{
assert(facts);
assert(cursor);
facts_cursor_init(facts, cursor, facts->index_spo, NULL, NULL);
cursor->var_subject = var_subject;
cursor->var_predicate = var_predicate;
cursor->var_object = var_object;
return cursor;
}
s_facts_cursor * facts_with_1_2 (s_facts *facts,
s_facts_cursor *cursor,
const s_tag *subject,
const s_tag *predicate,
const s_tag *object,
s_tag *var_subject,
s_tag *var_predicate,
s_tag *var_object)
{
s_fact start;
s_fact end;
s_skiplist__fact *tree;
assert(facts);
assert(cursor);
assert(subject);
assert(predicate);
assert(object);
assert(var_subject || var_predicate || var_object);
start.subject = var_subject ? TAG_FIRST : subject;
start.predicate = var_predicate ? TAG_FIRST : predicate;
start.object = var_object ? TAG_FIRST : object;
end.subject = var_subject ? TAG_LAST : subject;
end.predicate = var_predicate ? TAG_LAST : predicate;
end.object = var_object ? TAG_LAST : object;
if (! var_subject && var_object)
tree = facts->index_spo;
else if (! var_predicate)
tree = facts->index_pos;
else
tree = facts->index_osp;
facts_cursor_init(facts, cursor, tree, &start, &end);
cursor->var_subject = var_subject;
cursor->var_predicate = var_predicate;
cursor->var_object = var_object;
return cursor;
}
s_facts_cursor * facts_with_3 (s_facts *facts,
s_facts_cursor *cursor,
const s_tag *subject,
const s_tag *predicate,
const s_tag *object)
{
s_fact fact;
assert(facts);
assert(cursor);
assert(subject);
assert(predicate);
assert(object);
fact.subject = subject;
fact.predicate = predicate;
fact.object = object;
return facts_cursor_init(facts, cursor, facts->index_spo, &fact, &fact);
}
s_facts_cursor * facts_with_tags (s_facts *facts,
s_facts_cursor *cursor,
s_tag *subject,
s_tag *predicate,
s_tag *object)
{
s_tag *var_subject = NULL;
s_tag *var_predicate = NULL;
s_tag *var_object = NULL;
assert(facts);
assert(cursor);
assert(subject);
assert(predicate);
assert(object);
if (tag_is_unbound_var(subject))
var_subject = subject;
if (tag_is_unbound_var(predicate))
var_predicate = predicate;
if (tag_is_unbound_var(object))
var_object = object;
if (var_subject && var_predicate && var_object)
return facts_with_0(facts, cursor, var_subject, var_predicate,
var_object);
if (! (var_subject || var_predicate || var_object))
return facts_with_3(facts, cursor, subject, predicate, object);
return facts_with_1_2(facts, cursor, subject, predicate, object,
var_subject, var_predicate, var_object);
}