Hash :
effa72e0
Author :
Thomas de Grivel
Date :
2024-08-22T02:33:51
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/* kc3
* Copyright 2022,2023,2024 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 "alloc.h"
#include "assert.h"
#include "buf.h"
#include "buf_inspect.h"
#include "env.h"
#include "facts_cursor.h"
#include "facts_spec.h"
#include "facts_with.h"
#include "sym.h"
#include "tag.h"
#include "var.h"
s_facts_with_cursor * facts_with (s_facts *facts,
s_facts_with_cursor *cursor,
p_facts_spec spec)
{
uw facts_count;
s_facts_with_cursor tmp = {0};
assert(facts);
assert(cursor);
assert(spec);
facts_count = facts_spec_count_facts(spec);
tmp.facts = facts;
tmp.facts_count = facts_count;
if (facts_count > 0) {
tmp.levels = alloc(facts_count *
sizeof(s_facts_with_cursor_level));
if (! tmp.levels)
return NULL;
tmp.spec = facts_spec_new_expand(spec);
if (false) {
err_write_1("facts_with: spec = ");
err_inspect_facts_spec(spec);
err_write_1("\nfacts_with: tmp.spec = ");
err_inspect_facts_spec(tmp.spec);
err_write_1("\n");
}
/* facts_spec_sort(tmp.spec); */
}
if (pthread_mutex_init(&tmp.mutex, NULL)) {
err_puts("facts_with: pthread_mutex_init");
assert(! "facts_with: pthread_mutex_init");
return NULL;
}
*cursor = tmp;
return cursor;
}
s_facts_cursor * facts_with_0 (s_facts *facts,
s_facts_cursor *cursor,
s_var *var_subject,
s_var *var_predicate,
s_var *var_object)
{
assert(facts);
assert(cursor);
facts_cursor_init(facts, cursor, facts->index_spo, NULL, NULL);
var_init_copy(&cursor->var_subject, var_subject);
var_init_copy(&cursor->var_predicate, var_predicate);
var_init_copy(&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_var *var_subject,
s_var *var_predicate,
s_var *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);
if (var_subject)
var_init_copy(&cursor->var_subject, var_subject);
if (var_predicate)
var_init_copy(&cursor->var_predicate, var_predicate);
if (var_object)
var_init_copy(&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)
{
bool unbound;
s_var *var_subject = NULL;
s_var *var_predicate = NULL;
s_var *var_object = NULL;
assert(facts);
assert(cursor);
assert(subject);
assert(predicate);
assert(object);
if (! tag_is_unbound_var(subject, &unbound))
return NULL;
if (unbound)
var_subject = &subject->data.var;
if (! tag_is_unbound_var(predicate, &unbound))
return NULL;
if (unbound)
var_predicate = &predicate->data.var;
if (! tag_is_unbound_var(object, &unbound))
return NULL;
if (unbound)
var_object = &object->data.var;
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);
}
s_facts_cursor * facts_with_tuple (s_facts *facts,
s_facts_cursor *cursor,
s_tuple *fact)
{
assert(facts);
assert(cursor);
assert(fact);
if (fact->count != 3) {
err_write_1("facts_with_tuple: Tuple count != 3: ");
err_inspect_tuple(fact);
err_write_1("\n");
assert(! "facts_with_tuple: Tuple count != 3");
return NULL;
}
return facts_with_tags(facts, cursor, fact->tag, fact->tag + 1,
fact->tag + 2);
}