Hash :
865d1524
Author :
Thomas de Grivel
Date :
2022-10-31T00:50:45
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/* c3
* Copyright 2022 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 "buf.h"
#include "buf_file.h"
#include "buf_inspect.h"
#include "buf_parse.h"
#include "fact.h"
#include "facts.h"
#include "facts_cursor.h"
#include "facts_with.h"
#include "set__fact.h"
#include "set__tag.h"
#include "skiplist__fact.h"
#include "tag.h"
s_fact * facts_add_fact (s_facts *facts, const s_fact *fact)
{
s_fact tmp;
s_fact *f = NULL;
s_set_item__fact *item;
assert(facts);
assert(fact);
tmp.subject = facts_ref_tag(facts, fact->subject);
tmp.predicate = facts_ref_tag(facts, fact->predicate);
tmp.object = facts_ref_tag(facts, fact->object);
if ((item = set_get__fact(&facts->facts, &tmp)))
return &item->data;
if (facts->log)
facts_log_add(facts->log, &tmp);
item = set_add__fact(&facts->facts, &tmp);
f = &item->data;
skiplist_insert__fact(facts->index_spo, f);
skiplist_insert__fact(facts->index_pos, f);
skiplist_insert__fact(facts->index_osp, f);
return f;
}
s_fact * facts_add_tags (s_facts *facts, const s_tag *subject,
const s_tag *predicate,
const s_tag *object)
{
s_fact fact;
fact_init(&fact, subject, predicate, object);
return facts_add_fact(facts, &fact);
}
void facts_clean (s_facts *facts)
{
if (facts->log)
buf_file_close(facts->log);
skiplist_delete__fact(facts->index_osp);
skiplist_delete__fact(facts->index_pos);
skiplist_delete__fact(facts->index_spo);
set_clean__fact(&facts->facts);
set_clean__tag(&facts->tags);
}
void facts_delete (s_facts *facts)
{
assert(facts);
facts_clean(facts);
free(facts);
}
sw facts_dump (const s_facts *facts, const s8 *path)
{
s_buf buf;
s_facts_cursor cursor;
s_fact *fact;
FILE *fp;
s_tag predicate;
s_tag object;
sw r;
sw result = 0;
s_tag subject;
assert(facts);
assert(path);
BUF_INIT_ALLOCA(&buf, 1024);
fp = fopen(path, "w");
buf_file_open_w(&buf, fp);
tag_init_var(&subject);
tag_init_var(&predicate);
tag_init_var(&object);
facts_with_0(facts, &cursor, &subject, &predicate, &object);
while ((fact = facts_cursor_next(&cursor))) {
if ((r = facts_log_add(&buf, fact)) < 0)
goto clean;
result += r;
}
clean:
buf_file_close(&buf);
fclose(fp);
return result;
}
s_fact * facts_find_fact (const s_facts *facts, const s_fact *fact)
{
s_fact f;
s_set_item__fact *item;
assert(facts);
assert(fact);
if ((f.subject = facts_find_tag(facts, fact->subject)) &&
(f.predicate = facts_find_tag(facts, fact->predicate)) &&
(f.object = facts_find_tag(facts, fact->object)) &&
(item = set_get__fact((const s_set__fact *) &facts->facts, &f)))
return &item->data;
return NULL;
}
s_tag * facts_find_tag (const s_facts *facts, const s_tag *tag)
{
s_set_item__tag *item;
assert(facts);
assert(tag);
if ((item = set_get__tag(&facts->tags, tag)))
return &item->data;
return NULL;
}
s_facts * facts_init (s_facts *facts, s_buf *log)
{
const u8 max_height = 10;
const double spacing = 2.7;
assert(facts);
set_init__tag(&facts->tags, 1024);
set_init__fact(&facts->facts, 1024);
facts->index_spo = skiplist_new__fact(max_height, spacing);
assert(facts->index_spo);
facts->index_spo->compare = fact_compare;
facts->index_pos = skiplist_new__fact(max_height, spacing);
assert(facts->index_pos);
facts->index_pos->compare = fact_compare_pos;
facts->index_osp = skiplist_new__fact(max_height, spacing);
assert(facts->index_osp);
facts->index_osp->compare = fact_compare_osp;
facts->log = log;
return facts;
}
/* XXX nested databases should be used with care */
sw facts_load (s_facts *facts, const s8 *path)
{
s_buf buf;
s_fact fact;
FILE *fp;
sw r;
sw result = 0;
assert(facts);
assert(path);
BUF_INIT_ALLOCA(&buf, 1024);
fp = fopen(path, "r");
buf_file_open_r(&buf, fp);
while (1) {
if ((r = buf_read_1(&buf, "add ")) < 0)
break;
result += r;
if (r) {
if ((r = buf_parse_fact(&buf, &fact)) <= 0)
break;
result += r;
facts_add_fact(facts, &fact);
goto ok;
}
if ((r = buf_read_1(&buf, "remove ")) <= 0)
break;
result += r;
if ((r = buf_parse_fact(&buf, &fact)) <= 0)
break;
result += r;
facts_remove_fact(facts, &fact);
ok:
buf_read_1(&buf, "\n");
}
buf_file_close(&buf);
fclose(fp);
return result;
}
sw facts_log_add (s_buf *log, const s_fact *fact)
{
sw r;
sw result = 0;
if ((r = buf_write_1(log, "add ")) < 0)
return r;
result += r;
if ((r = buf_inspect_fact(log, fact)) < 0)
return r;
result += r;
if ((r = buf_write_1(log, "\n")) < 0)
return r;
result += r;
return result;
}
sw facts_log_remove (s_buf *log, const s_fact *fact)
{
sw r;
sw result = 0;
if ((r = buf_write_1(log, "remove ")) < 0)
return r;
result += r;
if ((r = buf_inspect_fact(log, fact)) < 0)
return r;
result += r;
if ((r = buf_write_1(log, "\n")) < 0)
return r;
result += r;
return result;
}
s_facts * facts_new (s_buf *log)
{
s_facts *n;
if (! (n = malloc(sizeof(s_facts))))
errx(1, "facts_new: out of memory");
return facts_init(n, log);
}
s_tag * facts_ref_tag (s_facts *facts, const s_tag *tag)
{
s_set_item__tag *item;
assert(facts);
assert(tag);
item = set_add__tag(&facts->tags, tag);
item->usage++;
return &item->data;
}
e_bool facts_remove_fact (s_facts *facts, const s_fact *fact)
{
s_fact f;
s_fact *found;
assert(facts);
assert(fact);
found = facts_find_fact(facts, fact);
if (found) {
skiplist_remove__fact(facts->index_spo, found);
skiplist_remove__fact(facts->index_pos, found);
skiplist_remove__fact(facts->index_osp, found);
f = *found;
set_remove__fact(&facts->facts, found);
facts_unref_tag(facts, f.subject);
facts_unref_tag(facts, f.predicate);
facts_unref_tag(facts, f.object);
return true;
}
return false;
}
e_bool facts_unref_tag (s_facts *facts, const s_tag *tag)
{
s_set_item__tag *item;
assert(facts);
assert(tag);
item = set_get__tag(&facts->tags, tag);
if (item) {
item->usage--;
if (! item->usage)
set_remove_item__tag(&facts->tags, item);
return true;
}
return false;
}