summaryrefslogtreecommitdiff
path: root/src/runtime/c/sg/sg.c
diff options
context:
space:
mode:
authorkrasimir <krasimir@chalmers.se>2016-04-28 14:14:04 +0000
committerkrasimir <krasimir@chalmers.se>2016-04-28 14:14:04 +0000
commit6fb17dc6fc914c24718689b7c57c7dd17b4b7022 (patch)
tree7f265126828a337ab79ea55f6f373367ffecc28f /src/runtime/c/sg/sg.c
parent41171be5a6473eeb5de42e3f2c0e20c854d1481a (diff)
a version of sqlite3.c which is prunned to one third of the original size and now it contains only the database storage engine withough the SQL compiler and interpreter
Diffstat (limited to 'src/runtime/c/sg/sg.c')
-rw-r--r--src/runtime/c/sg/sg.c947
1 files changed, 462 insertions, 485 deletions
diff --git a/src/runtime/c/sg/sg.c b/src/runtime/c/sg/sg.c
index f13026d68..76bff8d57 100644
--- a/src/runtime/c/sg/sg.c
+++ b/src/runtime/c/sg/sg.c
@@ -1,23 +1,35 @@
-#define SQLITE_API static
-#include "sqlite3.c"
+#include <stdlib.h>
+#include "sqlite3Btree.h"
#include "sg/sg.h"
#include "pgf/data.h"
-#define SG_EXPRS "sg_exprs"
-#define SG_PAIRS "sg_pairs"
-#define SG_IDENTS "sg_idents"
-#define SG_LITERALS "sg_literals"
-#define SG_TOKENS "sg_tokens"
-#define SG_TRIPLES "sg_triples"
-#define SG_TRIPLES_SPO "sg_triples_spo"
-#define SG_TRIPLES_PO "sg_triples_po"
-#define SG_TRIPLES_O "sg_triples_o"
+#define SG_EXPRS 1
+#define SG_PAIRS 2
+#define SG_IDENTS 3
+#define SG_LITERALS 4
+#define SG_TOKENS 5
+#define SG_TRIPLES 6
+#define SG_TRIPLES_SPO 7
+#define SG_TRIPLES_PO 8
+#define SG_TRIPLES_O 9
+
+struct SgSG {
+ Btree *pBtree;
+ int exprsTNum; /* The page number for the table of expressions */
+ int identsTNum; /* The page number for the index on identifiers */
+ int literalsTNum; /* The page number for the index on literals */
+ int pairsTNum; /* The page number for the index on application pairs */
+ int tokensTNum; /* The page number for the index on linearization tokens */
+ int triplesTNum; /* The page number for the table of triples */
+ int triplesIdxTNum[3]; /* The page number for the three indexes on triples */
+ int autoCommit;
+};
void
-sg_raise_sqlite(sqlite3* db, GuExn* err)
+sg_raise_sqlite(int rc, GuExn* err)
{
- const char *msg = sqlite3_errmsg(db);
+ const char *msg = sqlite3BtreeErrName(rc);
GuExnData* err_data = gu_raise(err, SgError);
if (err_data) {
@@ -35,168 +47,239 @@ sg_raise_err(GuString msg, GuExn* err)
}
}
+static int
+sg_create_table(Btree* pBtree, BtCursor* crsSchema, int tblKey, int* pTNum, int flags)
+{
+ int rc;
+ int file_format = sqlite3BtreeFileFormat(pBtree);
+
+ int res = 0;
+ rc = sqlite3BtreeMovetoUnpacked(crsSchema, 0, tblKey, 0, &res);
+ if (rc != SQLITE_OK) {
+ return rc;
+ }
+
+ Mem mem;
+ int serial_type;
+
+ if (res == 0) {
+ u32 payloadSize;
+ rc = sqlite3BtreeDataSize(crsSchema, &payloadSize);
+ if (rc != SQLITE_OK)
+ return rc;
+
+ u32 avail = 0;
+ const unsigned char* row = sqlite3BtreeDataFetch(crsSchema, &avail);
+ row++;
+
+ row += getVarint32(row, serial_type);
+ row += sqlite3BtreeSerialGet(row, serial_type, &mem);
+ assert(mem.flags & MEM_Int);
+
+ *pTNum = mem.u.i;
+ } else {
+ rc = sqlite3BtreeCreateTable(pBtree, pTNum, flags);
+ if (rc != SQLITE_OK) {
+ return rc;
+ }
+
+ mem.flags = MEM_Int;
+ mem.u.i = *pTNum;
+
+ unsigned char buf[32];
+ unsigned char *p;
+
+ buf[0] = 2;
+ serial_type = sqlite3BtreeSerialType(&mem, file_format);
+ buf[1] = serial_type;
+
+ p = buf+2;
+ p += sqlite3BtreeSerialPut(buf+2, &mem, serial_type);
+
+ rc = sqlite3BtreeInsert(crsSchema, 0, tblKey,
+ buf, p-buf, 0,
+ 0, 0);
+ if (rc != SQLITE_OK)
+ return rc;
+ }
+
+ return SQLITE_OK;
+}
+
SgSG*
sg_open(const char *filename,
GuExn* err)
{
int rc;
-
- sqlite3* db = NULL;
- rc = sqlite3_open(filename, &db);
+
+ Btree* pBtree;
+ rc = sqlite3BtreeOpen(0, filename, &pBtree,
+ 0, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return NULL;
}
- rc = sqlite3_exec(db, "create table if not exists " SG_EXPRS "(fun not null, arg integer);"
- "create unique index if not exists " SG_IDENTS " on " SG_EXPRS "(fun) where arg is null;"
- "create index if not exists " SG_TOKENS " on " SG_EXPRS "(fun) where arg <> arg;" // actually used for full text search
- "create unique index if not exists " SG_LITERALS " on " SG_EXPRS "(fun) where arg = 0;"
- "create unique index if not exists " SG_PAIRS " on " SG_EXPRS "(fun,arg) where arg > 0;"
- "create table if not exists " SG_TRIPLES "(subj integer, pred integer, obj integer, state integer);"
- "create unique index if not exists " SG_TRIPLES_SPO " on " SG_TRIPLES "(subj,pred,obj);"
- "create index if not exists " SG_TRIPLES_PO " on " SG_TRIPLES "(pred,obj);"
- "create index if not exists " SG_TRIPLES_O " on " SG_TRIPLES "(obj);",
- NULL, NULL, NULL);
+ rc = sqlite3BtreeBeginTrans(pBtree, 1);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
+ sqlite3BtreeClose(pBtree);
return NULL;
}
- return (SgSG*) db;
+ BtCursor* crsSchema = NULL;
+ rc = sqlite3BtreeCursor(pBtree, 1, 1, 0, 0, &crsSchema);
+ if (rc != SQLITE_OK) {
+ sg_raise_sqlite(rc, err);
+ sqlite3BtreeClose(pBtree);
+ return NULL;
+ }
+
+ SgSG* sg = malloc(sizeof(SgSG));
+ sg->pBtree = pBtree;
+ sg->autoCommit = 1;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_EXPRS, &sg->exprsTNum, BTREE_INTKEY);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_IDENTS, &sg->identsTNum, 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_LITERALS, &sg->literalsTNum, 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_PAIRS, &sg->pairsTNum, 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_TOKENS, &sg->tokensTNum, 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_TRIPLES, &sg->triplesTNum, BTREE_INTKEY);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_TRIPLES_SPO, &sg->triplesIdxTNum[0], 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_TRIPLES_PO, &sg->triplesIdxTNum[1], 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ rc = sg_create_table(pBtree, crsSchema, SG_TRIPLES_O, &sg->triplesIdxTNum[2], 0);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ sqlite3BtreeCloseCursor(crsSchema);
+
+ rc = sqlite3BtreeCommit(pBtree);
+ if (rc != SQLITE_OK)
+ goto fail;
+
+ return sg;
+
+fail:
+ sg_raise_sqlite(rc, err);
+ if (crsSchema != NULL)
+ sqlite3BtreeCloseCursor(crsSchema);
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
+ sqlite3BtreeClose(pBtree);
+ free(sg);
+ return NULL;
}
void
sg_close(SgSG* sg, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
- int rc;
- rc = sqlite3_close((sqlite3*) db);
- if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
- }
+ sqlite3BtreeClose(sg->pBtree);
}
void
sg_shutdown()
{
- sqlite3_shutdown();
+ sqlite3BtreeShutdown();
}
void
sg_begin_trans(SgSG* sg, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
- int rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 1);
+ int rc = sqlite3BtreeBeginTrans(sg->pBtree, 1);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return;
}
- db->autoCommit = 0;
+ sg->autoCommit = 0;
}
void
sg_commit(SgSG* sg, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
- int rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ int rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return;
}
- db->autoCommit = 1;
+ sg->autoCommit = 1;
}
void
sg_rollback(SgSG* sg, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
- int rc = sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ int rc = sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return;
}
- db->autoCommit = 1;
+ sg->autoCommit = 1;
}
typedef struct {
int n_cursors;
- BtCursor crsExprs;
- BtCursor crsIdents;
- BtCursor crsLiterals;
- BtCursor crsPairs;
+ BtCursor* crsExprs;
+ BtCursor* crsIdents;
+ BtCursor* crsLiterals;
+ BtCursor* crsPairs;
SgId key_seed;
} ExprContext;
static int
-open_exprs(sqlite3 *db, int wrFlag, bool identsOnly,
+open_exprs(SgSG *sg, int wrFlag, bool identsOnly,
ExprContext* ctxt, GuExn* err)
{
ctxt->n_cursors = 0;
-
- Table *exprsTbl =
- sqlite3HashFind(&db->aDb[0].pSchema->tblHash, SG_EXPRS);
- if (!exprsTbl) {
- sg_raise_err("Table " SG_EXPRS " is missing", err);
- return SQLITE_ERROR;
- }
-
- Index *identsIdx = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_IDENTS);
- if (!identsIdx) {
- sg_raise_err("Index " SG_IDENTS " is missing", err);
- return SQLITE_ERROR;
- }
-
- Index *literalsIdx = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_LITERALS);
- if (!literalsIdx) {
- sg_raise_err("Index " SG_LITERALS " is missing", err);
- return SQLITE_ERROR;
- }
-
- Index *pairsIdx = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_PAIRS);
- if (!pairsIdx) {
- sg_raise_err("Index " SG_PAIRS " is missing", err);
- return SQLITE_ERROR;
- }
int rc;
- memset(&ctxt->crsExprs, 0, sizeof(ctxt->crsExprs));
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, exprsTbl->tnum, wrFlag, NULL, &ctxt->crsExprs);
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->exprsTNum, wrFlag, 0, 0, &ctxt->crsExprs);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
ctxt->n_cursors++;
- memset(&ctxt->crsIdents, 0, sizeof(ctxt->crsIdents));
- KeyInfo *infIdents = sqlite3KeyInfoAlloc(db, 1, 1);
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, identsIdx->tnum, wrFlag, infIdents, &ctxt->crsIdents);
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->identsTNum, wrFlag, 1, 1, &ctxt->crsIdents);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
ctxt->n_cursors++;
if (!identsOnly) {
- memset(&ctxt->crsLiterals, 0, sizeof(ctxt->crsLiterals));
- KeyInfo *infLiterals = sqlite3KeyInfoAlloc(db, 1, 1);
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, literalsIdx->tnum, wrFlag, infLiterals, &ctxt->crsLiterals);
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->literalsTNum, wrFlag, 1, 1, &ctxt->crsLiterals);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
ctxt->n_cursors++;
- memset(&ctxt->crsPairs, 0, sizeof(ctxt->crsPairs));
- KeyInfo *infPairs = sqlite3KeyInfoAlloc(db, 2, 0);
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, pairsIdx->tnum, wrFlag, infPairs, &ctxt->crsPairs);
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->pairsTNum, wrFlag, 2, 0, &ctxt->crsPairs);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
ctxt->n_cursors++;
@@ -204,15 +287,15 @@ open_exprs(sqlite3 *db, int wrFlag, bool identsOnly,
if (wrFlag) {
int res;
- rc = sqlite3BtreeLast(&ctxt->crsExprs, &res);
+ rc = sqlite3BtreeLast(ctxt->crsExprs, &res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
- rc = sqlite3BtreeKeySize(&ctxt->crsExprs, &ctxt->key_seed);
+ rc = sqlite3BtreeKeySize(ctxt->crsExprs, &ctxt->key_seed);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
} else {
@@ -226,33 +309,30 @@ static void
close_exprs(ExprContext* ctxt)
{
if (ctxt->n_cursors >= 4) {
- sqlite3KeyInfoUnref(ctxt->crsPairs.pKeyInfo);
- sqlite3BtreeCloseCursor(&ctxt->crsPairs);
+ sqlite3BtreeCloseCursor(ctxt->crsPairs);
}
if (ctxt->n_cursors >= 3) {
- sqlite3KeyInfoUnref(ctxt->crsLiterals.pKeyInfo);
- sqlite3BtreeCloseCursor(&ctxt->crsLiterals);
+ sqlite3BtreeCloseCursor(ctxt->crsLiterals);
}
if (ctxt->n_cursors >= 2) {
- sqlite3KeyInfoUnref(ctxt->crsIdents.pKeyInfo);
- sqlite3BtreeCloseCursor(&ctxt->crsIdents);
+ sqlite3BtreeCloseCursor(ctxt->crsIdents);
}
if (ctxt->n_cursors >= 1) {
- sqlite3BtreeCloseCursor(&ctxt->crsExprs);
+ sqlite3BtreeCloseCursor(ctxt->crsExprs);
}
ctxt->n_cursors = 0;
}
static int
-find_function_rowid(sqlite3* db, ExprContext* ctxt,
+find_function_rowid(SgSG* sg, ExprContext* ctxt,
GuString fun, SgId* pKey, int wrFlag)
{
int rc = SQLITE_OK;
- int file_format = db->aDb[0].pSchema->file_format;
+ int file_format = sqlite3BtreeFileFormat(sg->pBtree);
Mem mem[2];
mem[0].flags = MEM_Str;
@@ -260,20 +340,17 @@ find_function_rowid(sqlite3* db, ExprContext* ctxt,
mem[0].z = (void*) fun;
UnpackedRecord idxKey;
- idxKey.pKeyInfo = ctxt->crsIdents.pKeyInfo;
- idxKey.nField = 1;
- idxKey.default_rc = 0;
- idxKey.aMem = mem;
+ sqlite3BtreeInitUnpackedRecord(&idxKey, ctxt->crsIdents, 1, 0, mem);
int res = 0;
- rc = sqlite3BtreeMovetoUnpacked(&ctxt->crsIdents,
+ rc = sqlite3BtreeMovetoUnpacked(ctxt->crsIdents,
&idxKey, 0, 0, &res);
if (rc != SQLITE_OK) {
return rc;
}
if (res == 0) {
- rc = sqlite3VdbeIdxRowid(db, &ctxt->crsIdents, pKey);
+ rc = sqlite3BtreeIdxRowid(sg->pBtree, ctxt->crsIdents, pKey);
} else {
if (wrFlag == 0) {
*pKey = 0;
@@ -282,16 +359,16 @@ find_function_rowid(sqlite3* db, ExprContext* ctxt,
*pKey = ++ctxt->key_seed;
- int serial_type_fun = sqlite3VdbeSerialType(&mem[0], file_format);
- int serial_type_fun_hdr_len = sqlite3VarintLen(serial_type_fun);
+ int serial_type_fun = sqlite3BtreeSerialType(&mem[0], file_format);
+ int serial_type_fun_hdr_len = sqlite3BtreeVarintLen(serial_type_fun);
mem[1].flags = MEM_Int;
mem[1].u.i = *pKey;
- int serial_type_key = sqlite3VdbeSerialType(&mem[1], file_format);
- int serial_type_key_hdr_len = sqlite3VarintLen(serial_type_key);
+ int serial_type_key = sqlite3BtreeSerialType(&mem[1], file_format);
+ int serial_type_key_hdr_len = sqlite3BtreeVarintLen(serial_type_key);
- unsigned char* buf = malloc(1+serial_type_fun_hdr_len+MAX(1,serial_type_key_hdr_len)+mem[0].n+8);
+ unsigned char* buf = malloc(1+serial_type_fun_hdr_len+(serial_type_key_hdr_len > 1 ? serial_type_key_hdr_len : 1)+mem[0].n+8);
unsigned char* p = buf;
*p++ = 1+serial_type_fun_hdr_len+1;
p += putVarint32(p, serial_type_fun);
@@ -299,7 +376,7 @@ find_function_rowid(sqlite3* db, ExprContext* ctxt,
memcpy(p, fun, mem[0].n);
p += mem[0].n;
- rc = sqlite3BtreeInsert(&ctxt->crsExprs, 0, *pKey,
+ rc = sqlite3BtreeInsert(ctxt->crsExprs, 0, *pKey,
buf, p-buf, 0,
0, 0);
if (rc != SQLITE_OK) {
@@ -312,8 +389,8 @@ find_function_rowid(sqlite3* db, ExprContext* ctxt,
p += putVarint32(p, serial_type_key);
memcpy(p, fun, mem[0].n);
p += mem[0].n;
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type_key);
- rc = sqlite3BtreeInsert(&ctxt->crsIdents, buf, p-buf,
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type_key);
+ rc = sqlite3BtreeInsert(ctxt->crsIdents, buf, p-buf,
0, *pKey, 0,
0, 0);
@@ -325,11 +402,11 @@ free:
}
static int
-store_expr(sqlite3* db,
+store_expr(SgSG* sg,
ExprContext* ctxt, PgfExpr expr, SgId* pKey, int wrFlag)
{
int rc = SQLITE_OK;
- int file_format = db->aDb[0].pSchema->file_format;
+ int file_format = sqlite3BtreeFileFormat(sg->pBtree);
GuVariantInfo ei = gu_variant_open(expr);
switch (ei.tag) {
@@ -343,7 +420,7 @@ store_expr(sqlite3* db,
Mem mem[3];
mem[0].flags = MEM_Int;
- rc = store_expr(db, ctxt, app->fun, &mem[0].u.i, wrFlag);
+ rc = store_expr(sg, ctxt, app->fun, &mem[0].u.i, wrFlag);
if (rc != SQLITE_OK)
return rc;
if (wrFlag == 0 && mem[0].u.i == 0) {
@@ -352,7 +429,7 @@ store_expr(sqlite3* db,
}
mem[1].flags = MEM_Int;
- rc = store_expr(db, ctxt, app->arg, &mem[1].u.i, wrFlag);
+ rc = store_expr(sg, ctxt, app->arg, &mem[1].u.i, wrFlag);
if (rc != SQLITE_OK)
return rc;
if (wrFlag == 0 && mem[1].u.i == 0) {
@@ -361,20 +438,17 @@ store_expr(sqlite3* db,
}
UnpackedRecord idxKey;
- idxKey.pKeyInfo = ctxt->crsPairs.pKeyInfo;
- idxKey.nField = 2;
- idxKey.default_rc = 0;
- idxKey.aMem = mem;
+ sqlite3BtreeInitUnpackedRecord(&idxKey, ctxt->crsPairs, 2, 0, mem);
int res = 0;
- rc = sqlite3BtreeMovetoUnpacked(&ctxt->crsPairs,
+ rc = sqlite3BtreeMovetoUnpacked(ctxt->crsPairs,
&idxKey, 0, 0, &res);
if (rc != SQLITE_OK) {
return rc;
}
if (res == 0) {
- rc = sqlite3VdbeIdxRowid(db, &ctxt->crsPairs, pKey);
+ rc = sqlite3BtreeIdxRowid(sg->pBtree, ctxt->crsPairs, pKey);
} else {
if (wrFlag == 0) {
*pKey = 0;
@@ -389,15 +463,15 @@ store_expr(sqlite3* db,
u32 serial_type;
unsigned char* p = buf+4;
- serial_type = sqlite3VdbeSerialType(&mem[0], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[0], file_format);
buf[2] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[0], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[0], serial_type);
- serial_type = sqlite3VdbeSerialType(&mem[1], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[1], file_format);
buf[3] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type);
- rc = sqlite3BtreeInsert(&ctxt->crsExprs, 0, *pKey,
+ rc = sqlite3BtreeInsert(ctxt->crsExprs, 0, *pKey,
buf+1, p-(buf+1), 0,
0, 0);
if (rc != SQLITE_OK) {
@@ -410,11 +484,11 @@ store_expr(sqlite3* db,
mem[2].flags = MEM_Int;
mem[2].u.i = *pKey;
- serial_type = sqlite3VdbeSerialType(&mem[2], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[2], file_format);
buf[3] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[2], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[2], serial_type);
- rc = sqlite3BtreeInsert(&ctxt->crsPairs, buf, p-buf,
+ rc = sqlite3BtreeInsert(ctxt->crsPairs, buf, p-buf,
0, *pKey, 0,
0, 0);
}
@@ -454,20 +528,17 @@ store_expr(sqlite3* db,
}
UnpackedRecord idxKey;
- idxKey.pKeyInfo = ctxt->crsIdents.pKeyInfo;
- idxKey.nField = 1;
- idxKey.default_rc = 0;
- idxKey.aMem = mem;
+ sqlite3BtreeInitUnpackedRecord(&idxKey, ctxt->crsIdents, 1, 0, mem);
int res = 0;
- rc = sqlite3BtreeMovetoUnpacked(&ctxt->crsLiterals,
+ rc = sqlite3BtreeMovetoUnpacked(ctxt->crsLiterals,
&idxKey, 0, 0, &res);
if (rc != SQLITE_OK) {
return rc;
}
if (res == 0) {
- rc = sqlite3VdbeIdxRowid(db, &ctxt->crsLiterals, pKey);
+ rc = sqlite3BtreeIdxRowid(sg->pBtree, ctxt->crsLiterals, pKey);
} else {
if (wrFlag == 0) {
*pKey = 0;
@@ -479,36 +550,36 @@ store_expr(sqlite3* db,
mem[1].flags = MEM_Int;
mem[1].u.i = 0;
- int serial_type_lit = sqlite3VdbeSerialType(&mem[0], file_format);
- int serial_type_lit_hdr_len = sqlite3VarintLen(serial_type_lit);
- int serial_type_arg = sqlite3VdbeSerialType(&mem[1], file_format);
- int serial_type_arg_hdr_len = sqlite3VarintLen(serial_type_arg);
+ int serial_type_lit = sqlite3BtreeSerialType(&mem[0], file_format);
+ int serial_type_lit_hdr_len = sqlite3BtreeVarintLen(serial_type_lit);
+ int serial_type_arg = sqlite3BtreeSerialType(&mem[1], file_format);
+ int serial_type_arg_hdr_len = sqlite3BtreeVarintLen(serial_type_arg);
- unsigned char* buf = malloc(1+serial_type_lit_hdr_len+MAX(1,serial_type_arg_hdr_len)+mem[0].n+8);
+ unsigned char* buf = malloc(1+serial_type_lit_hdr_len+(serial_type_arg_hdr_len > 1 ? serial_type_arg_hdr_len : 1)+mem[0].n+8);
unsigned char* p = buf;
*p++ = 1+serial_type_lit_hdr_len+serial_type_arg_hdr_len;
p += putVarint32(p, serial_type_lit);
p += putVarint32(p, serial_type_arg);
- p += sqlite3VdbeSerialPut(p, &mem[0], serial_type_lit);
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type_arg);
+ p += sqlite3BtreeSerialPut(p, &mem[0], serial_type_lit);
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type_arg);
- rc = sqlite3BtreeInsert(&ctxt->crsExprs, 0, *pKey,
+ rc = sqlite3BtreeInsert(ctxt->crsExprs, 0, *pKey,
buf, p-buf, 0,
0, 0);
if (rc == SQLITE_OK) {
mem[1].flags = MEM_Int;
mem[1].u.i = *pKey;
- int serial_type_key = sqlite3VdbeSerialType(&mem[1], file_format);
- int serial_type_key_hdr_len = sqlite3VarintLen(serial_type_key);
+ int serial_type_key = sqlite3BtreeSerialType(&mem[1], file_format);
+ int serial_type_key_hdr_len = sqlite3BtreeVarintLen(serial_type_key);
p = buf;
*p++ = 1+serial_type_lit_hdr_len+serial_type_key_hdr_len;
p += putVarint32(p, serial_type_lit);
p += putVarint32(p, serial_type_key);
- p += sqlite3VdbeSerialPut(p, &mem[0], serial_type_lit);
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type_key);
- rc = sqlite3BtreeInsert(&ctxt->crsLiterals, buf, p-buf,
+ p += sqlite3BtreeSerialPut(p, &mem[0], serial_type_lit);
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type_key);
+ rc = sqlite3BtreeInsert(ctxt->crsLiterals, buf, p-buf,
0, *pKey, 0,
0, 0);
}
@@ -522,7 +593,7 @@ store_expr(sqlite3* db,
}
case PGF_EXPR_FUN: {
PgfExprFun* fun = ei.data;
- rc = find_function_rowid(db, ctxt, fun->fun, pKey, wrFlag);
+ rc = find_function_rowid(sg, ctxt, fun->fun, pKey, wrFlag);
break;
}
case PGF_EXPR_VAR: {
@@ -531,12 +602,12 @@ store_expr(sqlite3* db,
}
case PGF_EXPR_TYPED: {
PgfExprTyped* etyped = ei.data;
- rc = store_expr(db, ctxt, etyped->expr, pKey, wrFlag);
+ rc = store_expr(sg, ctxt, etyped->expr, pKey, wrFlag);
break;
}
case PGF_EXPR_IMPL_ARG: {
PgfExprImplArg* eimpl = ei.data;
- rc = store_expr(db, ctxt, eimpl->expr, pKey, wrFlag);
+ rc = store_expr(sg, ctxt, eimpl->expr, pKey, wrFlag);
break;
}
default:
@@ -549,36 +620,34 @@ store_expr(sqlite3* db,
SgId
sg_insert_expr(SgSG *sg, PgfExpr expr, int wrFlag, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, wrFlag);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, wrFlag);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return 0;
}
}
ExprContext ctxt;
- rc = open_exprs(db, wrFlag, false, &ctxt, err);
+ rc = open_exprs(sg, wrFlag, false, &ctxt, err);
if (rc != SQLITE_OK)
goto close;
SgId key;
- rc = store_expr(db, &ctxt, expr, &key, wrFlag);
+ rc = store_expr(sg, &ctxt, expr, &key, wrFlag);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
close_exprs(&ctxt);
- if (db->autoCommit) {
- rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return 0;
}
}
@@ -588,8 +657,8 @@ sg_insert_expr(SgSG *sg, PgfExpr expr, int wrFlag, GuExn* err)
close:
close_exprs(&ctxt);
- if (db->autoCommit) {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ if (sg->autoCommit) {
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
return 0;
}
@@ -621,11 +690,11 @@ load_expr(BtCursor* crsExprs, SgId key, PgfExpr *pExpr, GuPool* out_pool)
row += getVarint32(row, serial_type_arg);
Mem mem[2];
- row += sqlite3VdbeSerialGet(row, serial_type_fun, &mem[0]);
- row += sqlite3VdbeSerialGet(row, serial_type_arg, &mem[1]);
+ row += sqlite3BtreeSerialGet(row, serial_type_fun, &mem[0]);
+ row += sqlite3BtreeSerialGet(row, serial_type_arg, &mem[1]);
if (mem[1].flags & MEM_Null) {
- u32 len = sqlite3VdbeSerialTypeLen(serial_type_fun);
+ u32 len = sqlite3BtreeSerialTypeLen(serial_type_fun);
PgfExprFun *efun =
gu_new_flex_variant(PGF_EXPR_FUN,
@@ -641,7 +710,7 @@ load_expr(BtCursor* crsExprs, SgId key, PgfExpr *pExpr, GuPool* out_pool)
pExpr, out_pool);
if (mem[0].flags & MEM_Str) {
- u32 len = sqlite3VdbeSerialTypeLen(serial_type_fun);
+ u32 len = sqlite3BtreeSerialTypeLen(serial_type_fun);
PgfLiteralStr *lstr =
gu_new_flex_variant(PGF_LITERAL_STR,
@@ -684,49 +753,39 @@ load_expr(BtCursor* crsExprs, SgId key, PgfExpr *pExpr, GuPool* out_pool)
PgfExpr
sg_get_expr(SgSG *sg, SgId key, GuPool* out_pool, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
- Table *exprsTbl =
- sqlite3HashFind(&db->aDb[0].pSchema->tblHash, SG_EXPRS);
- if (!exprsTbl) {
- sg_raise_err("Table " SG_EXPRS " is missing", err);
- return gu_null_variant;
- }
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 0);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return gu_null_variant;
}
}
- BtCursor crsExprs;
- memset(&crsExprs, 0, sizeof(crsExprs));
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, exprsTbl->tnum, 0, NULL, &crsExprs);
+ BtCursor* crsExprs;
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->exprsTNum, 0, 0, 0, &crsExprs);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto rollback;
}
PgfExpr expr = gu_null_variant;
- rc = load_expr(&crsExprs, key, &expr, out_pool);
+ rc = load_expr(crsExprs, key, &expr, out_pool);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
- rc = sqlite3BtreeCloseCursor(&crsExprs);
+ rc = sqlite3BtreeCloseCursor(crsExprs);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto rollback;
}
- if (db->autoCommit) {
- rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto rollback;
}
}
@@ -734,34 +793,34 @@ sg_get_expr(SgSG *sg, SgId key, GuPool* out_pool, GuExn* err)
return expr;
close:
- sqlite3BtreeCloseCursor(&crsExprs);
+ sqlite3BtreeCloseCursor(crsExprs);
rollback:
- if (db->autoCommit) {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ if (sg->autoCommit) {
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
return gu_null_variant;
}
static int
-insert_token(sqlite3 *db, BtCursor* crsTokens, GuString tok, SgId key)
+insert_token(SgSG *sg, BtCursor* crsTokens, GuString tok, SgId key)
{
int rc = SQLITE_OK;
- int file_format = db->aDb[0].pSchema->file_format;
+ int file_format = sqlite3BtreeFileFormat(sg->pBtree);
Mem mem[2];
mem[0].flags = MEM_Str;
mem[0].n = strlen(tok);
mem[0].z = (void*) tok;
- int serial_type_tok = sqlite3VdbeSerialType(&mem[0], file_format);
- int serial_type_tok_hdr_len = sqlite3VarintLen(serial_type_tok);
+ int serial_type_tok = sqlite3BtreeSerialType(&mem[0], file_format);
+ int serial_type_tok_hdr_len = sqlite3BtreeVarintLen(serial_type_tok);
mem[1].flags = MEM_Int;
mem[1].u.i = key;
- int serial_type_key = sqlite3VdbeSerialType(&mem[1], file_format);
- int serial_type_key_hdr_len = sqlite3VarintLen(serial_type_key);
+ int serial_type_key = sqlite3BtreeSerialType(&mem[1], file_format);
+ int serial_type_key_hdr_len = sqlite3BtreeVarintLen(serial_type_key);
unsigned char* buf = malloc(1+serial_type_tok_hdr_len+serial_type_key_hdr_len+mem[0].n+8);
unsigned char* p = buf;
@@ -770,7 +829,7 @@ insert_token(sqlite3 *db, BtCursor* crsTokens, GuString tok, SgId key)
p += putVarint32(p, serial_type_key);
memcpy(p, tok, mem[0].n);
p += mem[0].n;
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type_key);
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type_key);
rc = sqlite3BtreeInsert(crsTokens, buf, p-buf,
0, key, 0,
0, 0);
@@ -780,7 +839,7 @@ insert_token(sqlite3 *db, BtCursor* crsTokens, GuString tok, SgId key)
}
static int
-insert_syms(sqlite3 *db, BtCursor* crsTokens, PgfSymbols* syms, SgId key)
+insert_syms(SgSG *sg, BtCursor* crsTokens, PgfSymbols* syms, SgId key)
{
int rc;
size_t n_syms = gu_seq_length(syms);
@@ -790,19 +849,19 @@ insert_syms(sqlite3 *db, BtCursor* crsTokens, PgfSymbols* syms, SgId key)
switch (sym_i.tag) {
case PGF_SYMBOL_KS: {
PgfSymbolKS* ks = sym_i.data;
- rc = insert_token(db, crsTokens, ks->token, key);
+ rc = insert_token(sg, crsTokens, ks->token, key);
if (rc != SQLITE_OK)
return rc;
break;
}
case PGF_SYMBOL_KP: {
PgfSymbolKP* kp = sym_i.data;
- rc = insert_syms(db, crsTokens, kp->default_form, key);
+ rc = insert_syms(sg, crsTokens, kp->default_form, key);
if (rc != SQLITE_OK)
return rc;
for (size_t i = 0; i < kp->n_forms; i++) {
- rc = insert_syms(db, crsTokens, kp->forms[i].form, key);
+ rc = insert_syms(sg, crsTokens, kp->forms[i].form, key);
if (rc != SQLITE_OK)
return rc;
}
@@ -818,41 +877,32 @@ void
sg_update_fts_index(SgSG* sg, PgfPGF* pgf, GuExn* err)
{
int rc = SQLITE_OK;
- sqlite3 *db = (sqlite3 *) sg;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 1);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 1);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return;
}
}
ExprContext ctxt;
- rc = open_exprs(db, 1, true, &ctxt, err);
+ rc = open_exprs(sg, 1, true, &ctxt, err);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
-
- Index *tokensIdx = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_TOKENS);
- if (!tokensIdx) {
- sg_raise_err("Index " SG_TOKENS " is missing", err);
- return;
- }
- rc = sqlite3BtreeClearTable(db->aDb[0].pBt, tokensIdx->tnum, NULL);
+ rc = sqlite3BtreeClearTable(sg->pBtree, sg->tokensTNum, NULL);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return;
}
- BtCursor crsTokens;
- memset(&crsTokens, 0, sizeof(crsTokens));
- KeyInfo *infTokens = sqlite3KeyInfoAlloc(db, 1, 1);
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, tokensIdx->tnum, 1, infTokens, &crsTokens);
+ BtCursor* crsTokens;
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->tokensTNum, 1, 1, 1, &crsTokens);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
}
ctxt.n_cursors++;
@@ -865,17 +915,17 @@ sg_update_fts_index(SgSG* sg, PgfPGF* pgf, GuExn* err)
PgfCncFun* cncfun = gu_seq_get(concr->cncfuns, PgfCncFun*, funid);
SgId key = 0;
- rc = find_function_rowid(db, &ctxt, cncfun->absfun->name, &key, 1);
+ rc = find_function_rowid(sg, &ctxt, cncfun->absfun->name, &key, 1);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
for (size_t lin_idx = 0; lin_idx < cncfun->n_lins; lin_idx++) {
PgfSequence* seq = cncfun->lins[lin_idx];
- rc = insert_syms(db, &crsTokens, seq->syms, key);
+ rc = insert_syms(sg, crsTokens, seq->syms, key);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
}
@@ -883,17 +933,16 @@ sg_update_fts_index(SgSG* sg, PgfPGF* pgf, GuExn* err)
}
if (ctxt.n_cursors >= 3) {
- sqlite3KeyInfoUnref(crsTokens.pKeyInfo);
- sqlite3BtreeCloseCursor(&crsTokens);
+ sqlite3BtreeCloseCursor(crsTokens);
ctxt.n_cursors--;
}
close_exprs(&ctxt);
- if (db->autoCommit) {
- rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
}
}
@@ -901,15 +950,14 @@ sg_update_fts_index(SgSG* sg, PgfPGF* pgf, GuExn* err)
close:
if (ctxt.n_cursors >= 3) {
- sqlite3KeyInfoUnref(crsTokens.pKeyInfo);
- sqlite3BtreeCloseCursor(&crsTokens);
+ sqlite3BtreeCloseCursor(crsTokens);
ctxt.n_cursors--;
}
close_exprs(&ctxt);
- if (db->autoCommit) {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ if (sg->autoCommit) {
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
}
@@ -917,20 +965,11 @@ GuSeq*
sg_query_linearization(SgSG *sg, GuString tok, GuPool *pool, GuExn* err)
{
int rc;
- sqlite3 *db = (sqlite3 *) sg;
- Index *tokensIdx = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_TOKENS);
- if (!tokensIdx) {
- sg_raise_err("Index " SG_TOKENS " is missing", err);
- return NULL;
- }
-
- BtCursor crsTokens;
- memset(&crsTokens, 0, sizeof(crsTokens));
- KeyInfo *infTokens = sqlite3KeyInfoAlloc(db, 1, 1);
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, tokensIdx->tnum, 1, infTokens, &crsTokens);
+ BtCursor* crsTokens;
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->tokensTNum, 1, 1, 1, &crsTokens);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return NULL;
}
@@ -940,16 +979,13 @@ sg_query_linearization(SgSG *sg, GuString tok, GuPool *pool, GuExn* err)
mem[0].z = (void*) tok;
UnpackedRecord idxKey;
- idxKey.pKeyInfo = crsTokens.pKeyInfo;
- idxKey.nField = 1;
- idxKey.default_rc = 0;
- idxKey.aMem = mem;
+ sqlite3BtreeInitUnpackedRecord(&idxKey, crsTokens, 1, 0, mem);
int res = 0;
- rc = sqlite3BtreeMovetoUnpacked(&crsTokens,
+ rc = sqlite3BtreeMovetoUnpacked(crsTokens,
&idxKey, 0, 0, &res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return NULL;
}
@@ -957,93 +993,68 @@ sg_query_linearization(SgSG *sg, GuString tok, GuPool *pool, GuExn* err)
while (res == 0) {
SgId key;
- rc = sqlite3VdbeIdxRowid(db, &crsTokens, &key);
+ rc = sqlite3BtreeIdxRowid(sg->pBtree, crsTokens, &key);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
+ sqlite3BtreeClearCursor(crsTokens);
return NULL;
}
gu_buf_push(ids, SgId, key);
- sqlite3BtreeNext(&crsTokens, &res);
+ sqlite3BtreeNext(crsTokens, &res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
+ sqlite3BtreeClearCursor(crsTokens);
return NULL;
}
i64 szData;
const unsigned char *zData;
- rc = sqlite3BtreeKeySize(&crsTokens, &szData);
+ rc = sqlite3BtreeKeySize(crsTokens, &szData);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
+ sqlite3BtreeClearCursor(crsTokens);
return NULL;
}
u32 available = 0;
- zData = sqlite3BtreeKeyFetch(&crsTokens, &available);
+ zData = sqlite3BtreeKeyFetch(crsTokens, &available);
if (szData > available)
gu_impossible();
- res = sqlite3VdbeRecordCompare(available, zData, &idxKey);
+ res = sqlite3BtreeRecordCompare(available, zData, &idxKey);
if (res != 0)
break;
}
+ sqlite3BtreeClearCursor(crsTokens);
return gu_buf_data_seq(ids);
}
typedef struct {
int n_cursors;
- BtCursor cursor[4];
+ BtCursor* cursor[4];
} TripleContext;
static int
-open_triples(sqlite3 *db, int wrFlag, TripleContext* ctxt, GuExn* err)
+open_triples(SgSG *sg, int wrFlag, TripleContext* ctxt, GuExn* err)
{
- Index *idx[3];
- idx[0] = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_TRIPLES_SPO);
- if (!idx[0]) {
- sg_raise_err("Index " SG_TRIPLES_SPO " is missing", err);
- return SQLITE_ERROR;
- }
-
- idx[1] = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_TRIPLES_PO);
- if (!idx[1]) {
- sg_raise_err("Index " SG_TRIPLES_PO " is missing", err);
- return SQLITE_ERROR;
- }
-
- idx[2] = sqlite3HashFind(&db->aDb[0].pSchema->idxHash, SG_TRIPLES_O);
- if (!idx[2]) {
- sg_raise_err("Index " SG_TRIPLES_O " is missing", err);
- return SQLITE_ERROR;
- }
-
- Table *triplesTbl =
- sqlite3HashFind(&db->aDb[0].pSchema->tblHash, SG_TRIPLES);
- if (!triplesTbl) {
- sg_raise_err("Table " SG_TRIPLES " is missing", err);
- return SQLITE_ERROR;
- }
-
int rc;
- memset(ctxt->cursor, 0, sizeof(ctxt->cursor));
-
ctxt->n_cursors = 0;
while (ctxt->n_cursors < 3) {
- KeyInfo *inf = sqlite3KeyInfoAlloc(db, 3-ctxt->n_cursors, ctxt->n_cursors);
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, idx[ctxt->n_cursors]->tnum, wrFlag, inf, &ctxt->cursor[ctxt->n_cursors]);
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->triplesIdxTNum[ctxt->n_cursors], wrFlag, 3-ctxt->n_cursors, ctxt->n_cursors, &ctxt->cursor[ctxt->n_cursors]);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
ctxt->n_cursors++;
}
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, triplesTbl->tnum, wrFlag, NULL, &ctxt->cursor[ctxt->n_cursors]);
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->triplesTNum, wrFlag, 0, 0, &ctxt->cursor[ctxt->n_cursors]);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return rc;
}
ctxt->n_cursors++;
@@ -1056,113 +1067,105 @@ close_triples(TripleContext* ctxt)
{
while (ctxt->n_cursors > 0) {
ctxt->n_cursors--;
- if (ctxt->cursor[ctxt->n_cursors].pKeyInfo != NULL) {
- sqlite3KeyInfoUnref(ctxt->cursor[ctxt->n_cursors].pKeyInfo);
- }
- sqlite3BtreeCloseCursor(&ctxt->cursor[ctxt->n_cursors]);
+ sqlite3BtreeCloseCursor(ctxt->cursor[ctxt->n_cursors]);
}
}
SgId
sg_insert_triple(SgSG *sg, SgTriple triple, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 1);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 1);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return 0;
}
}
TripleContext tctxt;
- rc = open_triples(db, 1, &tctxt, err);
+ rc = open_triples(sg, 1, &tctxt, err);
if (rc != SQLITE_OK)
goto close;
Mem mem[4];
ExprContext ectxt;
- rc = open_exprs(db, 1, false, &ectxt, err);
+ rc = open_exprs(sg, 1, false, &ectxt, err);
if (rc != SQLITE_OK)
goto close;
for (size_t i = 0; i < 3; i++) {
mem[i].flags = MEM_Int;
- rc = store_expr(db, &ectxt, triple[i], &mem[i].u.i, 1);
+ rc = store_expr(sg, &ectxt, triple[i], &mem[i].u.i, 1);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
}
UnpackedRecord idxKey;
- idxKey.pKeyInfo = tctxt.cursor[0].pKeyInfo;
- idxKey.nField = 3;
- idxKey.default_rc = 0;
- idxKey.aMem = mem;
+ sqlite3BtreeInitUnpackedRecord(&idxKey, tctxt.cursor[0], 3, 0, mem);
int res = 0;
- rc = sqlite3BtreeMovetoUnpacked(&tctxt.cursor[0],
+ rc = sqlite3BtreeMovetoUnpacked(tctxt.cursor[0],
&idxKey, 0, 0, &res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
SgId key = 0;
if (res == 0) {
- rc = sqlite3VdbeIdxRowid(db, &tctxt.cursor[0], &key);
+ rc = sqlite3BtreeIdxRowid(sg->pBtree, tctxt.cursor[0], &key);
} else {
- rc = sqlite3BtreeLast(&tctxt.cursor[3], &res);
+ rc = sqlite3BtreeLast(tctxt.cursor[3], &res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
- rc = sqlite3BtreeKeySize(&tctxt.cursor[3], &key);
+ rc = sqlite3BtreeKeySize(tctxt.cursor[3], &key);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
key++;
u32 serial_type;
unsigned char buf[41]; // enough for a record with three integers
- int file_format = db->aDb[0].pSchema->file_format;
+ int file_format = sqlite3BtreeFileFormat(sg->pBtree);
unsigned char* p = buf+(buf[0] = 5);
- serial_type = sqlite3VdbeSerialType(&mem[0], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[0], file_format);
buf[1] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[0], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[0], serial_type);
- serial_type = sqlite3VdbeSerialType(&mem[1], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[1], file_format);
buf[2] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type);
- serial_type = sqlite3VdbeSerialType(&mem[2], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[2], file_format);
buf[3] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[2], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[2], serial_type);
unsigned char* tmp = p;
mem[3].flags = MEM_Int;
mem[3].u.i = 1;
- serial_type = sqlite3VdbeSerialType(&mem[3], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[3], file_format);
buf[4] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[3], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[3], serial_type);
- rc = sqlite3BtreeInsert(&tctxt.cursor[3], 0, key,
+ rc = sqlite3BtreeInsert(tctxt.cursor[3], 0, key,
buf, p-buf, 0,
0, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
@@ -1170,55 +1173,55 @@ sg_insert_triple(SgSG *sg, SgTriple triple, GuExn* err)
mem[3].u.i = key;
p = tmp;
- serial_type = sqlite3VdbeSerialType(&mem[3], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[3], file_format);
buf[4] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[3], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[3], serial_type);
- rc = sqlite3BtreeInsert(&tctxt.cursor[0], buf, p-buf,
+ rc = sqlite3BtreeInsert(tctxt.cursor[0], buf, p-buf,
0, key, 0,
0, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
p = buf+(buf[0] = 4);
- serial_type = sqlite3VdbeSerialType(&mem[1], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[1], file_format);
buf[1] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[1], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[1], serial_type);
- serial_type = sqlite3VdbeSerialType(&mem[2], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[2], file_format);
buf[2] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[2], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[2], serial_type);
- serial_type = sqlite3VdbeSerialType(&mem[3], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[3], file_format);
buf[3] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[3], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[3], serial_type);
- rc = sqlite3BtreeInsert(&tctxt.cursor[1], buf, p-buf,
+ rc = sqlite3BtreeInsert(tctxt.cursor[1], buf, p-buf,
0, key, 0,
0, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
p = buf+(buf[0] = 3);
- serial_type = sqlite3VdbeSerialType(&mem[2], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[2], file_format);
buf[1] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[2], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[2], serial_type);
- serial_type = sqlite3VdbeSerialType(&mem[3], file_format);
+ serial_type = sqlite3BtreeSerialType(&mem[3], file_format);
buf[2] = serial_type;
- p += sqlite3VdbeSerialPut(p, &mem[3], serial_type);
+ p += sqlite3BtreeSerialPut(p, &mem[3], serial_type);
- rc = sqlite3BtreeInsert(&tctxt.cursor[2], buf, p-buf,
+ rc = sqlite3BtreeInsert(tctxt.cursor[2], buf, p-buf,
0, key, 0,
0, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
}
@@ -1227,15 +1230,15 @@ close:
close_exprs(&ectxt);
close_triples(&tctxt);
- if (db->autoCommit) {
+ if (sg->autoCommit) {
if (rc == SQLITE_OK || rc == SQLITE_DONE) {
- rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return 0;
}
} else {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
}
@@ -1264,9 +1267,9 @@ load_triple(BtCursor* crsTriples, BtCursor* crsExprs, SgTriple triple,
row++;
Mem mem[3];
- row += sqlite3VdbeSerialGet(row, serial_type_subj, &mem[0]);
- row += sqlite3VdbeSerialGet(row, serial_type_pred, &mem[1]);
- row += sqlite3VdbeSerialGet(row, serial_type_obj, &mem[2]);
+ row += sqlite3BtreeSerialGet(row, serial_type_subj, &mem[0]);
+ row += sqlite3BtreeSerialGet(row, serial_type_pred, &mem[1]);
+ row += sqlite3BtreeSerialGet(row, serial_type_obj, &mem[2]);
for (int i = 0; i < 3; i++) {
if (gu_variant_is_null(triple[i])) {
@@ -1283,82 +1286,64 @@ int
sg_get_triple(SgSG *sg, SgId key, SgTriple triple,
GuPool* out_pool, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
triple[0] = gu_null_variant;
triple[1] = gu_null_variant;
triple[2] = gu_null_variant;
- Table *triplesTbl =
- sqlite3HashFind(&db->aDb[0].pSchema->tblHash, SG_TRIPLES);
- if (!triplesTbl) {
- sg_raise_err("Table " SG_TRIPLES " is missing", err);
- return false;
- }
-
- Table *exprsTbl =
- sqlite3HashFind(&db->aDb[0].pSchema->tblHash, SG_EXPRS);
- if (!exprsTbl) {
- sg_raise_err("Table " SG_EXPRS " is missing", err);
- return false;
- }
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 0);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
}
- BtCursor crsTriples;
- memset(&crsTriples, 0, sizeof(crsTriples));
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, triplesTbl->tnum, 0, NULL, &crsTriples);
+ BtCursor* crsTriples;
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->triplesTNum, 0, 0, 0, &crsTriples);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto rollback;
}
- BtCursor crsExprs;
- memset(&crsExprs, 0, sizeof(crsExprs));
- rc = sqlite3BtreeCursor(db->aDb[0].pBt, exprsTbl->tnum, 0, NULL, &crsExprs);
+ BtCursor* crsExprs;
+ rc = sqlite3BtreeCursor(sg->pBtree, sg->exprsTNum, 0, 0, 0, &crsExprs);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close1;
}
int res;
- rc = sqlite3BtreeMovetoUnpacked(&crsTriples, 0, key, 0, &res);
+ rc = sqlite3BtreeMovetoUnpacked(crsTriples, 0, key, 0, &res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
if (res == 0) {
- rc = load_triple(&crsTriples, &crsExprs, triple, out_pool);
+ rc = load_triple(crsTriples, crsExprs, triple, out_pool);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
}
- rc = sqlite3BtreeCloseCursor(&crsExprs);
+ rc = sqlite3BtreeCloseCursor(crsExprs);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close1;
}
- rc = sqlite3BtreeCloseCursor(&crsTriples);
+ rc = sqlite3BtreeCloseCursor(crsTriples);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto rollback;
}
- if (db->autoCommit) {
- rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
}
@@ -1366,14 +1351,14 @@ sg_get_triple(SgSG *sg, SgId key, SgTriple triple,
return (res == 0);
close:
- sqlite3BtreeCloseCursor(&crsExprs);
+ sqlite3BtreeCloseCursor(crsExprs);
close1:
- sqlite3BtreeCloseCursor(&crsTriples);
+ sqlite3BtreeCloseCursor(crsTriples);
rollback:
- if (db->autoCommit) {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ if (sg->autoCommit) {
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
return false;
}
@@ -1389,7 +1374,7 @@ typedef struct {
} SgTripleResultInt;
struct SgTripleResult {
- sqlite3 *db;
+ SgSG *sg;
SgTriple triple;
ExprContext ectxt;
@@ -1397,11 +1382,11 @@ struct SgTripleResult {
};
static int
-init_triple_result_int(sqlite3 *db, SgTripleResultInt* tresi, GuExn* err)
+init_triple_result_int(SgSG *sg, SgTripleResultInt* tresi, GuExn* err)
{
int rc;
- rc = open_triples(db, 0, &tresi->tctxt, err);
+ rc = open_triples(sg, 0, &tresi->tctxt, err);
if (rc != SQLITE_OK)
return rc;
@@ -1412,10 +1397,8 @@ init_triple_result_int(sqlite3 *db, SgTripleResultInt* tresi, GuExn* err)
i++;
}
- tresi->cursor = &tresi->tctxt.cursor[i];
- tresi->idxKey.pKeyInfo = tresi->cursor->pKeyInfo;
- tresi->idxKey.nField = 0;
- tresi->idxKey.aMem = &tresi->mem[i];
+ tresi->cursor = tresi->tctxt.cursor[i];
+ sqlite3BtreeInitUnpackedRecord(&tresi->idxKey, tresi->cursor, 0, 0, &tresi->mem[i]);
tresi->res = 0;
while (i+tresi->idxKey.nField < 3) {
@@ -1440,7 +1423,7 @@ init_triple_result_int(sqlite3 *db, SgTripleResultInt* tresi, GuExn* err)
}
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
}
return rc;
@@ -1449,25 +1432,23 @@ init_triple_result_int(sqlite3 *db, SgTripleResultInt* tresi, GuExn* err)
SgTripleResult*
sg_query_triple(SgSG *sg, SgTriple triple, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 0);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return NULL;
}
}
SgTripleResult* tres = malloc(sizeof(SgTripleResult));
- tres->db = db;
+ tres->sg = sg;
tres->triple[0] = triple[0];
tres->triple[1] = triple[1];
tres->triple[2] = triple[2];
- rc = open_exprs(db, 0, false, &tres->ectxt, err);
+ rc = open_exprs(sg, 0, false, &tres->ectxt, err);
if (rc != SQLITE_OK)
goto close;
@@ -1476,7 +1457,7 @@ sg_query_triple(SgSG *sg, SgTriple triple, GuExn* err)
tres->i.mem[i].flags = MEM_Null;
else {
tres->i.mem[i].flags = MEM_Int;
- rc = store_expr(db, &tres->ectxt, triple[i], &tres->i.mem[i].u.i, 0);
+ rc = store_expr(sg, &tres->ectxt, triple[i], &tres->i.mem[i].u.i, 0);
if (rc != SQLITE_OK)
goto close;
if (tres->i.mem[i].u.i == 0) {
@@ -1486,7 +1467,7 @@ sg_query_triple(SgSG *sg, SgTriple triple, GuExn* err)
}
}
- rc = init_triple_result_int(db, &tres->i, err);
+ rc = init_triple_result_int(sg, &tres->i, err);
if (rc != SQLITE_OK) {
return NULL;
}
@@ -1497,8 +1478,8 @@ close:
close_exprs(&tres->ectxt);
close_triples(&tres->i.tctxt);
- if (db->autoCommit) {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ if (sg->autoCommit) {
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
free(tres);
@@ -1506,7 +1487,7 @@ close:
}
static bool
-triple_result_fetch_int(sqlite3* db, SgTripleResultInt* tresi,
+triple_result_fetch_int(SgSG* sg, SgTripleResultInt* tresi,
SgId* pKey, GuExn* err)
{
while (tresi->res == 0) {
@@ -1517,7 +1498,7 @@ triple_result_fetch_int(sqlite3* db, SgTripleResultInt* tresi,
const unsigned char *zData;
rc = sqlite3BtreeKeySize(tresi->cursor, &szData);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
@@ -1527,7 +1508,7 @@ triple_result_fetch_int(sqlite3* db, SgTripleResultInt* tresi,
gu_impossible();
tresi->idxKey.default_rc = 0;
- tresi->res = sqlite3VdbeRecordCompare(available, zData, &tresi->idxKey);
+ tresi->res = sqlite3BtreeRecordCompare(available, zData, &tresi->idxKey);
if (tresi->res != 0)
return false;
@@ -1536,15 +1517,15 @@ triple_result_fetch_int(sqlite3* db, SgTripleResultInt* tresi,
tresi->mem[2].flags != MEM_Null) {
int offset =
zData[0] +
- sqlite3VdbeSerialTypeLen(zData[1]) +
- sqlite3VdbeSerialTypeLen(zData[2]);
+ sqlite3BtreeSerialTypeLen(zData[1]) +
+ sqlite3BtreeSerialTypeLen(zData[2]);
zData+offset;
Mem mem;
- sqlite3VdbeSerialGet(zData+offset, zData[3], &mem);
+ sqlite3BtreeSerialGet(zData+offset, zData[3], &mem);
if (mem.u.i != tresi->mem[2].u.i) {
sqlite3BtreeNext(tresi->cursor, &tresi->res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
continue;
@@ -1553,24 +1534,24 @@ triple_result_fetch_int(sqlite3* db, SgTripleResultInt* tresi,
}
if (tresi->idxKey.nField > 0) {
- rc = sqlite3VdbeIdxRowid(db, tresi->cursor, pKey);
+ rc = sqlite3BtreeIdxRowid(sg->pBtree, tresi->cursor, pKey);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
- rc = sqlite3BtreeMovetoUnpacked(&tresi->tctxt.cursor[3], 0, *pKey, 0, &tresi->res);
+ rc = sqlite3BtreeMovetoUnpacked(tresi->tctxt.cursor[3], 0, *pKey, 0, &tresi->res);
} else {
rc = sqlite3BtreeKeySize(tresi->cursor, pKey);
}
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
sqlite3BtreeNext(tresi->cursor, &tresi->res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
@@ -1596,7 +1577,7 @@ sg_triple_result_fetch(SgTripleResult* tres, SgId* pKey, SgTriple triple,
const unsigned char *zData;
rc = sqlite3BtreeKeySize(tres->i.cursor, &szData);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
@@ -1606,7 +1587,7 @@ sg_triple_result_fetch(SgTripleResult* tres, SgId* pKey, SgTriple triple,
gu_impossible();
tres->i.idxKey.default_rc = 0;
- tres->i.res = sqlite3VdbeRecordCompare(available, zData, &tres->i.idxKey);
+ tres->i.res = sqlite3BtreeRecordCompare(available, zData, &tres->i.idxKey);
if (tres->i.res != 0)
return false;
@@ -1615,15 +1596,15 @@ sg_triple_result_fetch(SgTripleResult* tres, SgId* pKey, SgTriple triple,
tres->i.mem[2].flags != MEM_Null) {
int offset =
zData[0] +
- sqlite3VdbeSerialTypeLen(zData[1]) +
- sqlite3VdbeSerialTypeLen(zData[2]);
+ sqlite3BtreeSerialTypeLen(zData[1]) +
+ sqlite3BtreeSerialTypeLen(zData[2]);
zData+offset;
Mem mem;
- sqlite3VdbeSerialGet(zData+offset, zData[3], &mem);
+ sqlite3BtreeSerialGet(zData+offset, zData[3], &mem);
if (mem.u.i != tres->i.mem[2].u.i) {
sqlite3BtreeNext(tres->i.cursor, &tres->i.res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
continue;
@@ -1632,31 +1613,31 @@ sg_triple_result_fetch(SgTripleResult* tres, SgId* pKey, SgTriple triple,
}
if (tres->i.idxKey.nField > 0) {
- rc = sqlite3VdbeIdxRowid(tres->db, tres->i.cursor, pKey);
+ rc = sqlite3BtreeIdxRowid(tres->sg->pBtree, tres->i.cursor, pKey);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
- rc = sqlite3BtreeMovetoUnpacked(&tres->i.tctxt.cursor[3], 0, *pKey, 0, &tres->i.res);
+ rc = sqlite3BtreeMovetoUnpacked(tres->i.tctxt.cursor[3], 0, *pKey, 0, &tres->i.res);
} else {
rc = sqlite3BtreeKeySize(tres->i.cursor, pKey);
}
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
- rc = load_triple(&tres->i.tctxt.cursor[3], &tres->ectxt.crsExprs,
+ rc = load_triple(tres->i.tctxt.cursor[3], tres->ectxt.crsExprs,
triple, out_pool);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
sqlite3BtreeNext(tres->i.cursor, &tres->i.res);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return false;
}
@@ -1680,10 +1661,10 @@ sg_triple_result_close(SgTripleResult* tres, GuExn* err)
close_exprs(&tres->ectxt);
close_triples(&tres->i.tctxt);
- if (tres->db->autoCommit) {
- int rc = sqlite3BtreeCommit(tres->db->aDb[0].pBt);
+ if (tres->sg->autoCommit) {
+ int rc = sqlite3BtreeCommit(tres->sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(tres->db, err);
+ sg_raise_sqlite(rc, err);
return;
}
}
@@ -1708,7 +1689,7 @@ struct SgQuery {
};
struct SgQueryResult {
- sqlite3* db;
+ SgSG* sg;
SgQuery* query;
ExprContext ectxt;
@@ -1722,20 +1703,18 @@ SgQuery*
sg_prepare_query(SgSG *sg, size_t n_triples, SgTriple* triples,
GuPool* pool, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 0);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return NULL;
}
}
ExprContext ectxt;
- rc = open_exprs(db, 0, false, &ectxt, err);
+ rc = open_exprs(sg, 0, false, &ectxt, err);
if (rc != SQLITE_OK)
goto close;
@@ -1760,9 +1739,9 @@ sg_prepare_query(SgSG *sg, size_t n_triples, SgTriple* triples,
if (gu_variant_tag(expr) == PGF_EXPR_META)
query->vars[j].id = 0;
else {
- rc = store_expr(db, &ectxt, expr, &query->vars[j].id, 0);
+ rc = store_expr(sg, &ectxt, expr, &query->vars[j].id, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}
@@ -1779,10 +1758,10 @@ sg_prepare_query(SgSG *sg, size_t n_triples, SgTriple* triples,
close_exprs(&ectxt);
- if (db->autoCommit) {
- rc = sqlite3BtreeCommit(db->aDb[0].pBt);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeCommit(sg->pBtree);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return 0;
}
}
@@ -1792,8 +1771,8 @@ sg_prepare_query(SgSG *sg, size_t n_triples, SgTriple* triples,
close:
close_exprs(&ectxt);
- if (db->autoCommit) {
- sqlite3BtreeRollback(db->aDb[0].pBt, SQLITE_ABORT_ROLLBACK, 0);
+ if (sg->autoCommit) {
+ sqlite3BtreeRollback(sg->pBtree, SQLITE_ABORT_ROLLBACK, 0);
}
return NULL;
@@ -1802,14 +1781,12 @@ close:
SgQueryResult*
sg_query(SgSG *sg, SgQuery* query, GuExn* err)
{
- sqlite3 *db = (sqlite3 *) sg;
-
int rc;
- if (db->autoCommit) {
- rc = sqlite3BtreeBeginTrans(db->aDb[0].pBt, 0);
+ if (sg->autoCommit) {
+ rc = sqlite3BtreeBeginTrans(sg->pBtree, 0);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(db, err);
+ sg_raise_sqlite(rc, err);
return NULL;
}
}
@@ -1817,12 +1794,12 @@ sg_query(SgSG *sg, SgQuery* query, GuExn* err)
SgQueryResult* qres =
malloc(sizeof(SgQueryResult)+
sizeof(SgTripleResultInt)*query->n_patterns);
- qres->db = db;
+ qres->sg = sg;
qres->is_empty = false;
qres->query = query;
qres->n_results = 0;
- rc = open_exprs(db, 0, false, &qres->ectxt, err);
+ rc = open_exprs(sg, 0, false, &qres->ectxt, err);
if (rc != SQLITE_OK)
goto close;
@@ -1851,7 +1828,7 @@ sg_query_result_fetch(SgQueryResult* qres, PgfExpr* res, GuPool* out_pool, GuExn
}
}
- rc = init_triple_result_int(qres->db, &qres->results[i], err);
+ rc = init_triple_result_int(qres->sg, &qres->results[i], err);
if (rc != SQLITE_OK)
goto close;
@@ -1860,7 +1837,7 @@ sg_query_result_fetch(SgQueryResult* qres, PgfExpr* res, GuPool* out_pool, GuExn
SgId key;
for (;;) {
bool found =
- triple_result_fetch_int(qres->db, &qres->results[i],
+ triple_result_fetch_int(qres->sg, &qres->results[i],
&key, err);
if (gu_exn_is_raised(err)) {
return false;
@@ -1887,11 +1864,11 @@ sg_query_result_fetch(SgQueryResult* qres, PgfExpr* res, GuPool* out_pool, GuExn
for (size_t i = 0; i < qres->query->n_sels; i++) {
res[i] = qres->query->vars[qres->query->sel[i]].expr;
if (gu_variant_is_null(res[i])) {
- rc = load_expr(&qres->ectxt.crsExprs,
+ rc = load_expr(qres->ectxt.crsExprs,
qres->query->vars[qres->query->sel[i]].id,
&res[i], out_pool);
if (rc != SQLITE_OK) {
- sg_raise_sqlite(qres->db, err);
+ sg_raise_sqlite(rc, err);
goto close;
}