-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdocument.c
More file actions
92 lines (81 loc) · 2.45 KB
/
document.c
File metadata and controls
92 lines (81 loc) · 2.45 KB
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
#include "document.h"
#include "memory.h"
#include <assert.h>
ts_field_t *ts_field_new(uint8_t *name, enum ts_field_type type, void *value) {
assert(name != NULL);
assert(value != NULL);
ts_field_t *field = ts_calloc(1, sizeof(ts_field_t));
assert(field != NULL);
field->name = ts_strdup(name);
field->value_type = type;
size_t cpylen;
switch(type) {
case FIELD_STRING:
cpylen = strlen(value) + 1;
break;
case FIELD_INT32:
cpylen = sizeof(int32_t);
}
field->value = ts_calloc(1, cpylen);
memcpy(field->value, value, cpylen);
return field;
}
void ts_field_free(ts_field_t *field) {
ts_free(field->name);
ts_free(field->value);
ts_free(field);
}
ts_document_t *ts_document_new(ts_docid_generator_t *generator) {
ts_document_t *doc = ts_calloc(1, sizeof(ts_document_t));
assert(doc != NULL);
if (generator != NULL) {
doc->doc_id = generator->incr(generator);
}
return doc;
}
int ts_document_field_add(ts_document_t *doc, ts_field_t *field, uint8_t force) {
assert(doc != NULL);
assert(field != NULL);
ts_field_t *tmp= NULL;
HASH_FIND_STR(doc->fields, field->name, tmp);
if (tmp != NULL) {
if (!force) {
return -1;
}
ts_document_field_del(doc, field->name);
}
HASH_ADD_STR(doc->fields, name, field);
return 0;
}
int ts_document_field_del(ts_document_t *document, uint8_t *name) {
assert(document != NULL);
assert(name != NULL);
ts_field_t *field = NULL;
HASH_FIND_STR(document->fields, name, field);
if (field == NULL) {
return -1;
}
HASH_DEL(document->fields, field);
return 0;
}
void ts_document_free(ts_document_t *document, uint8_t free_fields) {
ts_field_t *field, *tmp;
HASH_ITER(hh, document->fields, field, tmp) {
HASH_DEL(document->fields, field);
if (free_fields) ts_field_free(field);
}
ts_free(document);
}
ts_document_t *ts_document_clone(ts_document_t *document) {
ts_document_t *new = ts_document_new(NULL);
new->doc_id = document->doc_id;
new->pk = document->pk;
ts_field_t *field, *tmp, *newfield;
HASH_ITER(hh, document->fields, field, tmp) {
newfield = ts_field_new(field->name, field->value_type, field->value);
if (ts_field_indexable(field))
ts_field_set_indexable(newfield);
ts_document_field_add(new, newfield, 1);
}
return new;
}