-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_dbinsert.c
115 lines (79 loc) · 2.81 KB
/
js_dbinsert.c
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
#include "js.h"
#include "database/db.h"
#include "database/db_api.h"
extern uint32_t calcSize (value_t doc);
extern void marshal_doc(value_t document, uint8_t *doc, uint32_t docSize);
// insertDocs (docStore, docArray, &docIdArray, &docCount, dbTxn)
value_t js_insertDocs(uint32_t args, environment_t *env) {
value_t v, slot, slot2, docs, docStore, dbTxn;
value_t array;
uint32_t size;
int i, count;
value_t s;
Doc *doc;
s.bits = vt_status;
if (debug) fprintf(stderr, "funcall : InsertDocs\n");
docStore = eval_arg (&args, env);
if (vt_handle != docStore.type || Hndl_docStore != docStore.subType) {
fprintf(stderr, "Error: createIndex => expecting docStore:handle => %s\n", strtype(docStore.type));
return s.status = ERROR_script_internal, s;
}
// insert an array of documents
array = eval_arg(&args, env);
if (vt_array != array.type && vt_object != array.type && vt_document != array.type) {
fprintf(stderr, "Error: insertDocs => expecting docs:Array => %s\n", strtype(array.type));
return s.status = ERROR_script_internal, s;
}
if (array.type == vt_array)
count = vec_count(array.aval->values);
else
count = 1;
// return an array of DocId
slot = eval_arg(&args, env);
if (vt_lval != slot.type) {
fprintf(stderr, "Error: insertDocs => expecting DocId:Symbol => %s\n", strtype(slot.type));
return s.status = ERROR_script_internal, s;
}
// return the size of the array
slot2 = eval_arg(&args, env);
if (vt_lval != slot2.type) {
fprintf(stderr, "Error: insertDocs => expecting Count:Symbol => %s\n", strtype(slot2.type));
return s.status = ERROR_script_internal, s;
}
// insert txn
dbTxn = eval_arg(&args, env);
if (vt_txnId != dbTxn.type && vt_undef != dbTxn.type) {
fprintf(stderr, "Error: insertDocs => expecting Txn:docId => %s\n", strtype(dbTxn.type));
return s.status = ERROR_script_internal, s;
}
docs = newArray(array_value);
// insert the documents
for( i = 0; i < count; i++ ) {
value_t nxtDoc;
if (array.type == vt_array)
nxtDoc = array.aval->values[i];
else
nxtDoc = array;
// marshall the document
size = calcSize(nxtDoc);
if ((s.status = (int)allocDoc((DbHandle *)docStore.handle, &doc, size)))
return fprintf(stderr, "Error: insertDocs => %s\n", strstatus(s.status)), s;
marshal_doc(nxtDoc, (uint8_t*)(doc + 1), size);
// add the document and index keys to the documentStore
s.status = (int)assignDoc((DbHandle *)docStore.handle, doc, dbTxn.txnBits);
if (s.status) {
fprintf(stderr, "Error: insertDocs => %s\n", strstatus(s.status));
return s;
}
// add the docId to the result Array
v.bits = vt_docId;
v.docBits = doc->docId.bits;
vec_push(docs.aval->values, v);
}
replaceValue(slot, docs);
v.bits = vt_int;
v.nval = count;
replaceValue(slot2, v);
abandonValue(array);
return s.status = OK, s;
}