Hash :
f0401f0c
Author :
Thomas de Grivel
Date :
2024-03-04T14:19:12
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
/* c3
* Copyright 2022-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 "assert.h"
#include "facts.h"
#include "facts_cursor.h"
#include "skiplist__fact.h"
#include "skiplist_node__fact.h"
#include "tag.h"
void facts_cursor_clean (s_facts_cursor *cursor)
{
assert(cursor);
facts_cursor_lock_clean(cursor);
}
s_facts_cursor * facts_cursor_init (s_facts *facts,
s_facts_cursor *cursor,
s_skiplist__fact *index,
s_fact *start,
s_fact *end)
{
s_skiplist_node__fact *pred;
assert(cursor);
assert(index);
pred = skiplist_pred__fact(index, start);
assert(pred);
cursor->index = index;
cursor->node = SKIPLIST_NODE_NEXT__fact(pred, 0);
skiplist_node_delete__fact(pred);
if (start)
cursor->start = *start;
else {
cursor->start.subject = TAG_FIRST;
cursor->start.predicate = TAG_FIRST;
cursor->start.object = TAG_FIRST;
}
if (end)
cursor->end = *end;
else {
cursor->end.subject = TAG_LAST;
cursor->end.predicate = TAG_LAST;
cursor->end.object = TAG_LAST;
}
cursor->var_subject = NULL;
cursor->var_predicate = NULL;
cursor->var_object = NULL;
cursor->facts = facts;
facts_cursor_lock_init(cursor);
return cursor;
}
s_facts_cursor * facts_cursor_lock (s_facts_cursor *cursor)
{
assert(cursor);
if (pthread_mutex_lock(&cursor->mutex)) {
err_puts("facts_cursor_lock: pthread_mutex_lock");
assert(! "facts_cursor_lock: pthread_mutex_lock");
return NULL;
}
facts_lock_r(cursor->facts);
return cursor;
}
s_facts_cursor * facts_cursor_lock_clean (s_facts_cursor *cursor)
{
assert(cursor);
if (pthread_mutex_destroy(&cursor->mutex)) {
err_puts("facts_cursor_lock_clean: pthread_mutex_destroy");
assert(! "facts_cursor_lock_clean: pthread_mutex_destroy");
return NULL;
}
return cursor;
}
s_facts_cursor * facts_cursor_lock_init (s_facts_cursor *cursor)
{
assert(cursor);
if (pthread_mutex_init(&cursor->mutex, NULL)) {
err_puts("facts_cursor_lock_init: pthread_mutex_init");
assert(! "facts_cursor_lock_init: pthread_mutex_init");
return NULL;
}
return cursor;
}
s_facts_cursor * facts_cursor_lock_unlock (s_facts_cursor *cursor)
{
assert(cursor);
facts_lock_unlock_r(cursor->facts);
if (pthread_mutex_unlock(&cursor->mutex)) {
err_puts("facts_cursor_lock_unlock: pthread_mutex_unlock");
assert(! "facts_cursor_lock_unlock: pthread_mutex_unlock");
return NULL;
}
return cursor;
}
s_fact * facts_cursor_next (s_facts_cursor *cursor)
{
assert(cursor);
facts_cursor_lock(cursor);
if (cursor->node) {
cursor->node = SKIPLIST_NODE_NEXT__fact(cursor->node, 0);
if (cursor->node &&
cursor->index->compare(&cursor->end, cursor->node->fact) < 0)
cursor->node = NULL;
}
if (cursor->node) {
s_fact *fact = cursor->node->fact;
if (cursor->var_subject)
*cursor->var_subject = *fact->subject;
if (cursor->var_predicate)
*cursor->var_predicate = *fact->predicate;
if (cursor->var_object)
*cursor->var_object = *fact->object;
facts_cursor_lock_unlock(cursor);
return fact;
}
if (cursor->var_subject)
tag_init_var(cursor->var_subject);
if (cursor->var_predicate)
tag_init_var(cursor->var_predicate);
if (cursor->var_object)
tag_init_var(cursor->var_object);
facts_cursor_lock_unlock(cursor);
return NULL;
}