|
effa893…
|
drh
|
1 |
/* |
|
effa893…
|
drh
|
2 |
** Copyright (c) 2019 D. Richard Hipp |
|
effa893…
|
drh
|
3 |
** |
|
effa893…
|
drh
|
4 |
** This program is free software; you can redistribute it and/or |
|
effa893…
|
drh
|
5 |
** modify it under the terms of the Simplified BSD License (also |
|
effa893…
|
drh
|
6 |
** known as the "2-Clause License" or "FreeBSD License".) |
|
effa893…
|
drh
|
7 |
** |
|
effa893…
|
drh
|
8 |
** This program is distributed in the hope that it will be useful, |
|
effa893…
|
drh
|
9 |
** but without any warranty; without even the implied warranty of |
|
effa893…
|
drh
|
10 |
** merchantability or fitness for a particular purpose. |
|
effa893…
|
drh
|
11 |
** |
|
effa893…
|
drh
|
12 |
** Author contact information: |
|
effa893…
|
drh
|
13 |
** [email protected] |
|
effa893…
|
drh
|
14 |
** http://www.hwaci.com/drh/ |
|
effa893…
|
drh
|
15 |
** |
|
effa893…
|
drh
|
16 |
******************************************************************************* |
|
effa893…
|
drh
|
17 |
** |
|
effa893…
|
drh
|
18 |
** This module implements SQL interfaces to the delta logic. The code |
|
effa893…
|
drh
|
19 |
** here is adapted from the ext/misc/fossildelta.c extension in SQLite. |
|
effa893…
|
drh
|
20 |
*/ |
|
effa893…
|
drh
|
21 |
#include "config.h" |
|
effa893…
|
drh
|
22 |
#include "deltafunc.h" |
|
effa893…
|
drh
|
23 |
|
|
effa893…
|
drh
|
24 |
/* |
|
effa893…
|
drh
|
25 |
** SQL functions: delta_create(X,Y) |
|
effa893…
|
drh
|
26 |
** |
|
effa893…
|
drh
|
27 |
** Return a delta that will transform X into Y. |
|
effa893…
|
drh
|
28 |
*/ |
|
effa893…
|
drh
|
29 |
static void deltaCreateFunc( |
|
effa893…
|
drh
|
30 |
sqlite3_context *context, |
|
effa893…
|
drh
|
31 |
int argc, |
|
effa893…
|
drh
|
32 |
sqlite3_value **argv |
|
effa893…
|
drh
|
33 |
){ |
|
effa893…
|
drh
|
34 |
const char *aOrig; int nOrig; /* old blob */ |
|
effa893…
|
drh
|
35 |
const char *aNew; int nNew; /* new blob */ |
|
effa893…
|
drh
|
36 |
char *aOut; int nOut; /* output delta */ |
|
effa893…
|
drh
|
37 |
|
|
effa893…
|
drh
|
38 |
assert( argc==2 ); |
|
effa893…
|
drh
|
39 |
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
|
effa893…
|
drh
|
40 |
if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return; |
|
effa893…
|
drh
|
41 |
nOrig = sqlite3_value_bytes(argv[0]); |
|
effa893…
|
drh
|
42 |
aOrig = (const char*)sqlite3_value_blob(argv[0]); |
|
effa893…
|
drh
|
43 |
nNew = sqlite3_value_bytes(argv[1]); |
|
effa893…
|
drh
|
44 |
aNew = (const char*)sqlite3_value_blob(argv[1]); |
|
effa893…
|
drh
|
45 |
aOut = sqlite3_malloc64(nNew+70); |
|
effa893…
|
drh
|
46 |
if( aOut==0 ){ |
|
effa893…
|
drh
|
47 |
sqlite3_result_error_nomem(context); |
|
effa893…
|
drh
|
48 |
}else{ |
|
effa893…
|
drh
|
49 |
nOut = delta_create(aOrig, nOrig, aNew, nNew, aOut); |
|
effa893…
|
drh
|
50 |
if( nOut<0 ){ |
|
effa893…
|
drh
|
51 |
sqlite3_free(aOut); |
|
effa893…
|
drh
|
52 |
sqlite3_result_error(context, "cannot create fossil delta", -1); |
|
effa893…
|
drh
|
53 |
}else{ |
|
effa893…
|
drh
|
54 |
sqlite3_result_blob(context, aOut, nOut, sqlite3_free); |
|
effa893…
|
drh
|
55 |
} |
|
effa893…
|
drh
|
56 |
} |
|
effa893…
|
drh
|
57 |
} |
|
effa893…
|
drh
|
58 |
|
|
effa893…
|
drh
|
59 |
/* |
|
effa893…
|
drh
|
60 |
** SQL functions: delta_apply(X,D) |
|
effa893…
|
drh
|
61 |
** |
|
effa893…
|
drh
|
62 |
** Return the result of applying delta D to input X. |
|
effa893…
|
drh
|
63 |
*/ |
|
effa893…
|
drh
|
64 |
static void deltaApplyFunc( |
|
effa893…
|
drh
|
65 |
sqlite3_context *context, |
|
effa893…
|
drh
|
66 |
int argc, |
|
effa893…
|
drh
|
67 |
sqlite3_value **argv |
|
effa893…
|
drh
|
68 |
){ |
|
7692260…
|
stephan
|
69 |
const char *aOrig; int nOrig; /* The X input */ |
|
7692260…
|
stephan
|
70 |
const char *aDelta; int nDelta; /* The input delta (D) */ |
|
7692260…
|
stephan
|
71 |
char *aOut; sqlite3_int64 nOut; /* The output */ |
|
effa893…
|
drh
|
72 |
|
|
effa893…
|
drh
|
73 |
assert( argc==2 ); |
|
effa893…
|
drh
|
74 |
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
|
effa893…
|
drh
|
75 |
if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return; |
|
effa893…
|
drh
|
76 |
nOrig = sqlite3_value_bytes(argv[0]); |
|
effa893…
|
drh
|
77 |
aOrig = (const char*)sqlite3_value_blob(argv[0]); |
|
effa893…
|
drh
|
78 |
nDelta = sqlite3_value_bytes(argv[1]); |
|
effa893…
|
drh
|
79 |
aDelta = (const char*)sqlite3_value_blob(argv[1]); |
|
effa893…
|
drh
|
80 |
|
|
effa893…
|
drh
|
81 |
/* Figure out the size of the output */ |
|
effa893…
|
drh
|
82 |
nOut = delta_output_size(aDelta, nDelta); |
|
effa893…
|
drh
|
83 |
if( nOut<0 ){ |
|
effa893…
|
drh
|
84 |
sqlite3_result_error(context, "corrupt fossil delta", -1); |
|
effa893…
|
drh
|
85 |
return; |
|
effa893…
|
drh
|
86 |
} |
|
7692260…
|
stephan
|
87 |
if( nOut>0x7fffffff ){ |
|
7692260…
|
stephan
|
88 |
sqlite3_result_error_toobig(context); |
|
7692260…
|
stephan
|
89 |
return; |
|
7692260…
|
stephan
|
90 |
} |
|
7692260…
|
stephan
|
91 |
aOut = sqlite3_malloc64(nOut+1); |
|
effa893…
|
drh
|
92 |
if( aOut==0 ){ |
|
effa893…
|
drh
|
93 |
sqlite3_result_error_nomem(context); |
|
effa893…
|
drh
|
94 |
}else{ |
|
7692260…
|
stephan
|
95 |
const int nOut2 = delta_apply(aOrig, nOrig, aDelta, nDelta, aOut); |
|
effa893…
|
drh
|
96 |
if( nOut2!=nOut ){ |
|
effa893…
|
drh
|
97 |
sqlite3_free(aOut); |
|
effa893…
|
drh
|
98 |
sqlite3_result_error(context, "corrupt fossil delta", -1); |
|
effa893…
|
drh
|
99 |
}else{ |
|
7692260…
|
stephan
|
100 |
sqlite3_result_blob(context, aOut, nOut2, sqlite3_free); |
|
effa893…
|
drh
|
101 |
} |
|
effa893…
|
drh
|
102 |
} |
|
effa893…
|
drh
|
103 |
} |
|
effa893…
|
drh
|
104 |
|
|
effa893…
|
drh
|
105 |
|
|
effa893…
|
drh
|
106 |
/* |
|
effa893…
|
drh
|
107 |
** SQL functions: delta_output_size(D) |
|
effa893…
|
drh
|
108 |
** |
|
effa893…
|
drh
|
109 |
** Return the size of the output that results from applying delta D. |
|
effa893…
|
drh
|
110 |
*/ |
|
effa893…
|
drh
|
111 |
static void deltaOutputSizeFunc( |
|
effa893…
|
drh
|
112 |
sqlite3_context *context, |
|
effa893…
|
drh
|
113 |
int argc, |
|
effa893…
|
drh
|
114 |
sqlite3_value **argv |
|
effa893…
|
drh
|
115 |
){ |
|
effa893…
|
drh
|
116 |
const char *aDelta; int nDelta; /* The input delta (D) */ |
|
7692260…
|
stephan
|
117 |
sqlite3_int64 nOut; /* Size of output */ |
|
effa893…
|
drh
|
118 |
assert( argc==1 ); |
|
effa893…
|
drh
|
119 |
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
|
effa893…
|
drh
|
120 |
nDelta = sqlite3_value_bytes(argv[0]); |
|
effa893…
|
drh
|
121 |
aDelta = (const char*)sqlite3_value_blob(argv[0]); |
|
effa893…
|
drh
|
122 |
|
|
effa893…
|
drh
|
123 |
/* Figure out the size of the output */ |
|
effa893…
|
drh
|
124 |
nOut = delta_output_size(aDelta, nDelta); |
|
effa893…
|
drh
|
125 |
if( nOut<0 ){ |
|
effa893…
|
drh
|
126 |
sqlite3_result_error(context, "corrupt fossil delta", -1); |
|
effa893…
|
drh
|
127 |
return; |
|
effa893…
|
drh
|
128 |
}else{ |
|
7692260…
|
stephan
|
129 |
sqlite3_result_int64(context, nOut); |
|
effa893…
|
drh
|
130 |
} |
|
effa893…
|
drh
|
131 |
} |
|
effa893…
|
drh
|
132 |
|
|
effa893…
|
drh
|
133 |
/***************************************************************************** |
|
effa893…
|
drh
|
134 |
** Table-valued SQL function: delta_parse(DELTA) |
|
effa893…
|
drh
|
135 |
** |
|
effa893…
|
drh
|
136 |
** Schema: |
|
effa893…
|
drh
|
137 |
** |
|
effa893…
|
drh
|
138 |
** CREATE TABLE delta_parse( |
|
effa893…
|
drh
|
139 |
** op TEXT, |
|
effa893…
|
drh
|
140 |
** a1 INT, |
|
effa893…
|
drh
|
141 |
** a2 ANY, |
|
effa893…
|
drh
|
142 |
** delta HIDDEN BLOB |
|
effa893…
|
drh
|
143 |
** ); |
|
effa893…
|
drh
|
144 |
** |
|
effa893…
|
drh
|
145 |
** Given an input DELTA, this function parses the delta and returns |
|
effa893…
|
drh
|
146 |
** rows for each entry in the delta. The op column has one of the |
|
effa893…
|
drh
|
147 |
** values SIZE, COPY, INSERT, CHECKSUM, ERROR. |
|
effa893…
|
drh
|
148 |
** |
|
effa893…
|
drh
|
149 |
** Assuming no errors, the first row has op='SIZE'. a1 is the size of |
|
effa893…
|
drh
|
150 |
** the output in bytes and a2 is NULL. |
|
effa893…
|
drh
|
151 |
** |
|
effa893…
|
drh
|
152 |
** After the initial SIZE row, there are zero or more 'COPY' and/or 'INSERT' |
|
effa893…
|
drh
|
153 |
** rows. A COPY row means content is copied from the source into the |
|
effa893…
|
drh
|
154 |
** output. Column a1 is the number of bytes to copy and a2 is the offset |
|
effa893…
|
drh
|
155 |
** into source from which to begin copying. An INSERT row means to |
|
effa893…
|
drh
|
156 |
** insert text into the output stream. Column a1 is the number of bytes |
|
effa893…
|
drh
|
157 |
** to insert and column is a BLOB that contains the text to be inserted. |
|
effa893…
|
drh
|
158 |
** |
|
effa893…
|
drh
|
159 |
** The last row of a well-formed delta will have an op value of 'CHECKSUM'. |
|
effa893…
|
drh
|
160 |
** The a1 column will be the value of the checksum and a2 will be NULL. |
|
effa893…
|
drh
|
161 |
** |
|
effa893…
|
drh
|
162 |
** If the input delta is not well-formed, then a row with an op value |
|
effa893…
|
drh
|
163 |
** of 'ERROR' is returned. The a1 value of the ERROR row is the offset |
|
effa893…
|
drh
|
164 |
** into the delta where the error was encountered and a2 is NULL. |
|
effa893…
|
drh
|
165 |
*/ |
|
effa893…
|
drh
|
166 |
typedef struct deltaparsevtab_vtab deltaparsevtab_vtab; |
|
effa893…
|
drh
|
167 |
typedef struct deltaparsevtab_cursor deltaparsevtab_cursor; |
|
effa893…
|
drh
|
168 |
struct deltaparsevtab_vtab { |
|
effa893…
|
drh
|
169 |
sqlite3_vtab base; /* Base class - must be first */ |
|
effa893…
|
drh
|
170 |
/* No additional information needed */ |
|
effa893…
|
drh
|
171 |
}; |
|
effa893…
|
drh
|
172 |
struct deltaparsevtab_cursor { |
|
effa893…
|
drh
|
173 |
sqlite3_vtab_cursor base; /* Base class - must be first */ |
|
effa893…
|
drh
|
174 |
char *aDelta; /* The delta being parsed */ |
|
921e7c1…
|
drh
|
175 |
i64 iCursor; /* Current cursor location */ |
|
921e7c1…
|
drh
|
176 |
i64 iNext; /* Next cursor value */ |
|
921e7c1…
|
drh
|
177 |
i64 nDelta; /* Number of bytes in the delta */ |
|
effa893…
|
drh
|
178 |
int eOp; /* Name of current operator */ |
|
effa893…
|
drh
|
179 |
unsigned int a1, a2; /* Arguments to current operator */ |
|
effa893…
|
drh
|
180 |
}; |
|
effa893…
|
drh
|
181 |
|
|
effa893…
|
drh
|
182 |
/* Operator names: |
|
effa893…
|
drh
|
183 |
*/ |
|
70a94d0…
|
jan.nijtmans
|
184 |
static const char *const azOp[] = { |
|
effa893…
|
drh
|
185 |
"SIZE", "COPY", "INSERT", "CHECKSUM", "ERROR", "EOF" |
|
effa893…
|
drh
|
186 |
}; |
|
effa893…
|
drh
|
187 |
#define DELTAPARSE_OP_SIZE 0 |
|
effa893…
|
drh
|
188 |
#define DELTAPARSE_OP_COPY 1 |
|
effa893…
|
drh
|
189 |
#define DELTAPARSE_OP_INSERT 2 |
|
effa893…
|
drh
|
190 |
#define DELTAPARSE_OP_CHECKSUM 3 |
|
effa893…
|
drh
|
191 |
#define DELTAPARSE_OP_ERROR 4 |
|
effa893…
|
drh
|
192 |
#define DELTAPARSE_OP_EOF 5 |
|
effa893…
|
drh
|
193 |
|
|
effa893…
|
drh
|
194 |
/* |
|
effa893…
|
drh
|
195 |
** Read bytes from *pz and convert them into a positive integer. When |
|
effa893…
|
drh
|
196 |
** finished, leave *pz pointing to the first character past the end of |
|
effa893…
|
drh
|
197 |
** the integer. The *pLen parameter holds the length of the string |
|
effa893…
|
drh
|
198 |
** in *pz and is decremented once for each character in the integer. |
|
effa893…
|
drh
|
199 |
*/ |
|
effa893…
|
drh
|
200 |
static unsigned int deltaGetInt(const char **pz, int *pLen){ |
|
effa893…
|
drh
|
201 |
static const signed char zValue[] = { |
|
effa893…
|
drh
|
202 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
effa893…
|
drh
|
203 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
effa893…
|
drh
|
204 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
effa893…
|
drh
|
205 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
|
effa893…
|
drh
|
206 |
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
|
effa893…
|
drh
|
207 |
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36, |
|
effa893…
|
drh
|
208 |
-1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, |
|
effa893…
|
drh
|
209 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1, |
|
21d8fad…
|
stephan
|
210 |
|
|
21d8fad…
|
stephan
|
211 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
212 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
213 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
214 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
215 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
216 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
217 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21d8fad…
|
stephan
|
218 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
effa893…
|
drh
|
219 |
}; |
|
effa893…
|
drh
|
220 |
unsigned int v = 0; |
|
effa893…
|
drh
|
221 |
int c; |
|
effa893…
|
drh
|
222 |
unsigned char *z = (unsigned char*)*pz; |
|
21d8fad…
|
stephan
|
223 |
unsigned char *zEnd = z + (*pLen); |
|
21d8fad…
|
stephan
|
224 |
while( z<zEnd && (c = zValue[*z])>=0 ){ |
|
21d8fad…
|
stephan
|
225 |
v = (v<<6) + c; |
|
21d8fad…
|
stephan
|
226 |
z++; |
|
effa893…
|
drh
|
227 |
} |
|
21d8fad…
|
stephan
|
228 |
|
|
21d8fad…
|
stephan
|
229 |
*pLen -= (int)(z - (unsigned char*)*pz); |
|
effa893…
|
drh
|
230 |
*pz = (char*)z; |
|
effa893…
|
drh
|
231 |
return v; |
|
effa893…
|
drh
|
232 |
} |
|
effa893…
|
drh
|
233 |
|
|
effa893…
|
drh
|
234 |
/* |
|
effa893…
|
drh
|
235 |
** The deltaparsevtabConnect() method is invoked to create a new |
|
effa893…
|
drh
|
236 |
** deltaparse virtual table. |
|
effa893…
|
drh
|
237 |
** |
|
effa893…
|
drh
|
238 |
** Think of this routine as the constructor for deltaparsevtab_vtab objects. |
|
effa893…
|
drh
|
239 |
** |
|
effa893…
|
drh
|
240 |
** All this routine needs to do is: |
|
effa893…
|
drh
|
241 |
** |
|
effa893…
|
drh
|
242 |
** (1) Allocate the deltaparsevtab_vtab object and initialize all fields. |
|
effa893…
|
drh
|
243 |
** |
|
effa893…
|
drh
|
244 |
** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
|
effa893…
|
drh
|
245 |
** result set of queries against the virtual table will look like. |
|
effa893…
|
drh
|
246 |
*/ |
|
effa893…
|
drh
|
247 |
static int deltaparsevtabConnect( |
|
effa893…
|
drh
|
248 |
sqlite3 *db, |
|
effa893…
|
drh
|
249 |
void *pAux, |
|
effa893…
|
drh
|
250 |
int argc, const char *const*argv, |
|
effa893…
|
drh
|
251 |
sqlite3_vtab **ppVtab, |
|
effa893…
|
drh
|
252 |
char **pzErr |
|
effa893…
|
drh
|
253 |
){ |
|
effa893…
|
drh
|
254 |
deltaparsevtab_vtab *pNew; |
|
effa893…
|
drh
|
255 |
int rc; |
|
effa893…
|
drh
|
256 |
|
|
effa893…
|
drh
|
257 |
rc = sqlite3_declare_vtab(db, |
|
effa893…
|
drh
|
258 |
"CREATE TABLE x(op,a1,a2,delta HIDDEN)" |
|
effa893…
|
drh
|
259 |
); |
|
effa893…
|
drh
|
260 |
/* For convenience, define symbolic names for the index to each column. */ |
|
effa893…
|
drh
|
261 |
#define DELTAPARSEVTAB_OP 0 |
|
effa893…
|
drh
|
262 |
#define DELTAPARSEVTAB_A1 1 |
|
effa893…
|
drh
|
263 |
#define DELTAPARSEVTAB_A2 2 |
|
effa893…
|
drh
|
264 |
#define DELTAPARSEVTAB_DELTA 3 |
|
effa893…
|
drh
|
265 |
if( rc==SQLITE_OK ){ |
|
effa893…
|
drh
|
266 |
pNew = sqlite3_malloc64( sizeof(*pNew) ); |
|
effa893…
|
drh
|
267 |
*ppVtab = (sqlite3_vtab*)pNew; |
|
effa893…
|
drh
|
268 |
if( pNew==0 ) return SQLITE_NOMEM; |
|
effa893…
|
drh
|
269 |
memset(pNew, 0, sizeof(*pNew)); |
|
a8dbbac…
|
drh
|
270 |
sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
|
effa893…
|
drh
|
271 |
} |
|
effa893…
|
drh
|
272 |
return rc; |
|
effa893…
|
drh
|
273 |
} |
|
effa893…
|
drh
|
274 |
|
|
effa893…
|
drh
|
275 |
/* |
|
effa893…
|
drh
|
276 |
** This method is the destructor for deltaparsevtab_vtab objects. |
|
effa893…
|
drh
|
277 |
*/ |
|
effa893…
|
drh
|
278 |
static int deltaparsevtabDisconnect(sqlite3_vtab *pVtab){ |
|
effa893…
|
drh
|
279 |
deltaparsevtab_vtab *p = (deltaparsevtab_vtab*)pVtab; |
|
effa893…
|
drh
|
280 |
sqlite3_free(p); |
|
effa893…
|
drh
|
281 |
return SQLITE_OK; |
|
effa893…
|
drh
|
282 |
} |
|
effa893…
|
drh
|
283 |
|
|
effa893…
|
drh
|
284 |
/* |
|
effa893…
|
drh
|
285 |
** Constructor for a new deltaparsevtab_cursor object. |
|
effa893…
|
drh
|
286 |
*/ |
|
effa893…
|
drh
|
287 |
static int deltaparsevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
|
effa893…
|
drh
|
288 |
deltaparsevtab_cursor *pCur; |
|
921e7c1…
|
drh
|
289 |
pCur = sqlite3_malloc64( sizeof(*pCur) ); |
|
effa893…
|
drh
|
290 |
if( pCur==0 ) return SQLITE_NOMEM; |
|
effa893…
|
drh
|
291 |
memset(pCur, 0, sizeof(*pCur)); |
|
effa893…
|
drh
|
292 |
*ppCursor = &pCur->base; |
|
effa893…
|
drh
|
293 |
return SQLITE_OK; |
|
effa893…
|
drh
|
294 |
} |
|
effa893…
|
drh
|
295 |
|
|
effa893…
|
drh
|
296 |
/* |
|
effa893…
|
drh
|
297 |
** Destructor for a deltaparsevtab_cursor. |
|
effa893…
|
drh
|
298 |
*/ |
|
effa893…
|
drh
|
299 |
static int deltaparsevtabClose(sqlite3_vtab_cursor *cur){ |
|
effa893…
|
drh
|
300 |
deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; |
|
9002a80…
|
drh
|
301 |
sqlite3_free(pCur->aDelta); |
|
effa893…
|
drh
|
302 |
sqlite3_free(pCur); |
|
effa893…
|
drh
|
303 |
return SQLITE_OK; |
|
effa893…
|
drh
|
304 |
} |
|
effa893…
|
drh
|
305 |
|
|
effa893…
|
drh
|
306 |
|
|
effa893…
|
drh
|
307 |
/* |
|
effa893…
|
drh
|
308 |
** Advance a deltaparsevtab_cursor to its next row of output. |
|
effa893…
|
drh
|
309 |
*/ |
|
effa893…
|
drh
|
310 |
static int deltaparsevtabNext(sqlite3_vtab_cursor *cur){ |
|
effa893…
|
drh
|
311 |
deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; |
|
effa893…
|
drh
|
312 |
const char *z; |
|
effa893…
|
drh
|
313 |
int i = 0; |
|
effa893…
|
drh
|
314 |
|
|
effa893…
|
drh
|
315 |
pCur->iCursor = pCur->iNext; |
|
a8dbbac…
|
drh
|
316 |
if( pCur->iCursor >= pCur->nDelta ){ |
|
a8dbbac…
|
drh
|
317 |
pCur->eOp = DELTAPARSE_OP_ERROR; |
|
a8dbbac…
|
drh
|
318 |
pCur->iNext = pCur->nDelta; |
|
a8dbbac…
|
drh
|
319 |
return SQLITE_OK; |
|
a8dbbac…
|
drh
|
320 |
} |
|
effa893…
|
drh
|
321 |
z = pCur->aDelta + pCur->iCursor; |
|
effa893…
|
drh
|
322 |
pCur->a1 = deltaGetInt(&z, &i); |
|
effa893…
|
drh
|
323 |
switch( z[0] ){ |
|
effa893…
|
drh
|
324 |
case '@': { |
|
effa893…
|
drh
|
325 |
z++; |
|
a8dbbac…
|
drh
|
326 |
if( pCur->iNext>=pCur->nDelta ){ |
|
a8dbbac…
|
drh
|
327 |
pCur->eOp = DELTAPARSE_OP_ERROR; |
|
a8dbbac…
|
drh
|
328 |
pCur->iNext = pCur->nDelta; |
|
a8dbbac…
|
drh
|
329 |
break; |
|
a8dbbac…
|
drh
|
330 |
} |
|
effa893…
|
drh
|
331 |
pCur->a2 = deltaGetInt(&z, &i); |
|
effa893…
|
drh
|
332 |
pCur->eOp = DELTAPARSE_OP_COPY; |
|
921e7c1…
|
drh
|
333 |
pCur->iNext = (i64)(&z[1] - pCur->aDelta); |
|
effa893…
|
drh
|
334 |
break; |
|
effa893…
|
drh
|
335 |
} |
|
effa893…
|
drh
|
336 |
case ':': { |
|
effa893…
|
drh
|
337 |
z++; |
|
effa893…
|
drh
|
338 |
pCur->a2 = (unsigned int)(z - pCur->aDelta); |
|
effa893…
|
drh
|
339 |
pCur->eOp = DELTAPARSE_OP_INSERT; |
|
921e7c1…
|
drh
|
340 |
pCur->iNext = (i64)(&z[pCur->a1] - pCur->aDelta); |
|
effa893…
|
drh
|
341 |
break; |
|
effa893…
|
drh
|
342 |
} |
|
effa893…
|
drh
|
343 |
case ';': { |
|
effa893…
|
drh
|
344 |
pCur->eOp = DELTAPARSE_OP_CHECKSUM; |
|
effa893…
|
drh
|
345 |
pCur->iNext = pCur->nDelta; |
|
effa893…
|
drh
|
346 |
break; |
|
effa893…
|
drh
|
347 |
} |
|
effa893…
|
drh
|
348 |
default: { |
|
effa893…
|
drh
|
349 |
if( pCur->iNext==pCur->nDelta ){ |
|
effa893…
|
drh
|
350 |
pCur->eOp = DELTAPARSE_OP_EOF; |
|
effa893…
|
drh
|
351 |
}else{ |
|
effa893…
|
drh
|
352 |
pCur->eOp = DELTAPARSE_OP_ERROR; |
|
effa893…
|
drh
|
353 |
pCur->iNext = pCur->nDelta; |
|
effa893…
|
drh
|
354 |
} |
|
effa893…
|
drh
|
355 |
break; |
|
effa893…
|
drh
|
356 |
} |
|
effa893…
|
drh
|
357 |
} |
|
effa893…
|
drh
|
358 |
return SQLITE_OK; |
|
effa893…
|
drh
|
359 |
} |
|
effa893…
|
drh
|
360 |
|
|
effa893…
|
drh
|
361 |
/* |
|
effa893…
|
drh
|
362 |
** Return values of columns for the row at which the deltaparsevtab_cursor |
|
effa893…
|
drh
|
363 |
** is currently pointing. |
|
effa893…
|
drh
|
364 |
*/ |
|
effa893…
|
drh
|
365 |
static int deltaparsevtabColumn( |
|
effa893…
|
drh
|
366 |
sqlite3_vtab_cursor *cur, /* The cursor */ |
|
effa893…
|
drh
|
367 |
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
|
effa893…
|
drh
|
368 |
int i /* Which column to return */ |
|
effa893…
|
drh
|
369 |
){ |
|
effa893…
|
drh
|
370 |
deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; |
|
effa893…
|
drh
|
371 |
switch( i ){ |
|
effa893…
|
drh
|
372 |
case DELTAPARSEVTAB_OP: { |
|
effa893…
|
drh
|
373 |
sqlite3_result_text(ctx, azOp[pCur->eOp], -1, SQLITE_STATIC); |
|
effa893…
|
drh
|
374 |
break; |
|
effa893…
|
drh
|
375 |
} |
|
effa893…
|
drh
|
376 |
case DELTAPARSEVTAB_A1: { |
|
7692260…
|
stephan
|
377 |
sqlite3_result_int64(ctx, pCur->a1); |
|
effa893…
|
drh
|
378 |
break; |
|
effa893…
|
drh
|
379 |
} |
|
effa893…
|
drh
|
380 |
case DELTAPARSEVTAB_A2: { |
|
effa893…
|
drh
|
381 |
if( pCur->eOp==DELTAPARSE_OP_COPY ){ |
|
7692260…
|
stephan
|
382 |
sqlite3_result_int64(ctx, pCur->a2); |
|
effa893…
|
drh
|
383 |
}else if( pCur->eOp==DELTAPARSE_OP_INSERT ){ |
|
921e7c1…
|
drh
|
384 |
if( (i64)pCur->a2 + (i64)pCur->a1 > pCur->nDelta ){ |
|
a8dbbac…
|
drh
|
385 |
sqlite3_result_zeroblob(ctx, pCur->a1); |
|
a8dbbac…
|
drh
|
386 |
}else{ |
|
a8dbbac…
|
drh
|
387 |
sqlite3_result_blob(ctx, pCur->aDelta+pCur->a2, pCur->a1, |
|
a8dbbac…
|
drh
|
388 |
SQLITE_TRANSIENT); |
|
a8dbbac…
|
drh
|
389 |
} |
|
effa893…
|
drh
|
390 |
} |
|
effa893…
|
drh
|
391 |
break; |
|
effa893…
|
drh
|
392 |
} |
|
effa893…
|
drh
|
393 |
case DELTAPARSEVTAB_DELTA: { |
|
effa893…
|
drh
|
394 |
sqlite3_result_blob(ctx, pCur->aDelta, pCur->nDelta, SQLITE_TRANSIENT); |
|
effa893…
|
drh
|
395 |
break; |
|
effa893…
|
drh
|
396 |
} |
|
effa893…
|
drh
|
397 |
} |
|
effa893…
|
drh
|
398 |
return SQLITE_OK; |
|
effa893…
|
drh
|
399 |
} |
|
effa893…
|
drh
|
400 |
|
|
effa893…
|
drh
|
401 |
/* |
|
effa893…
|
drh
|
402 |
** Return the rowid for the current row. In this implementation, the |
|
effa893…
|
drh
|
403 |
** rowid is the same as the output value. |
|
effa893…
|
drh
|
404 |
*/ |
|
effa893…
|
drh
|
405 |
static int deltaparsevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
effa893…
|
drh
|
406 |
deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; |
|
effa893…
|
drh
|
407 |
*pRowid = pCur->iCursor; |
|
effa893…
|
drh
|
408 |
return SQLITE_OK; |
|
effa893…
|
drh
|
409 |
} |
|
effa893…
|
drh
|
410 |
|
|
effa893…
|
drh
|
411 |
/* |
|
effa893…
|
drh
|
412 |
** Return TRUE if the cursor has been moved off of the last |
|
effa893…
|
drh
|
413 |
** row of output. |
|
effa893…
|
drh
|
414 |
*/ |
|
effa893…
|
drh
|
415 |
static int deltaparsevtabEof(sqlite3_vtab_cursor *cur){ |
|
effa893…
|
drh
|
416 |
deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; |
|
a8dbbac…
|
drh
|
417 |
return pCur->eOp==DELTAPARSE_OP_EOF || pCur->iCursor>=pCur->nDelta; |
|
effa893…
|
drh
|
418 |
} |
|
effa893…
|
drh
|
419 |
|
|
effa893…
|
drh
|
420 |
/* |
|
effa893…
|
drh
|
421 |
** This method is called to "rewind" the deltaparsevtab_cursor object back |
|
effa893…
|
drh
|
422 |
** to the first row of output. This method is always called at least |
|
70a94d0…
|
jan.nijtmans
|
423 |
** once prior to any call to deltaparsevtabColumn() or deltaparsevtabRowid() or |
|
effa893…
|
drh
|
424 |
** deltaparsevtabEof(). |
|
effa893…
|
drh
|
425 |
*/ |
|
effa893…
|
drh
|
426 |
static int deltaparsevtabFilter( |
|
70a94d0…
|
jan.nijtmans
|
427 |
sqlite3_vtab_cursor *pVtabCursor, |
|
effa893…
|
drh
|
428 |
int idxNum, const char *idxStr, |
|
effa893…
|
drh
|
429 |
int argc, sqlite3_value **argv |
|
effa893…
|
drh
|
430 |
){ |
|
effa893…
|
drh
|
431 |
deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor *)pVtabCursor; |
|
effa893…
|
drh
|
432 |
const char *a; |
|
effa893…
|
drh
|
433 |
int i = 0; |
|
effa893…
|
drh
|
434 |
pCur->eOp = DELTAPARSE_OP_ERROR; |
|
effa893…
|
drh
|
435 |
if( idxNum!=1 ){ |
|
effa893…
|
drh
|
436 |
return SQLITE_OK; |
|
effa893…
|
drh
|
437 |
} |
|
effa893…
|
drh
|
438 |
pCur->nDelta = sqlite3_value_bytes(argv[0]); |
|
effa893…
|
drh
|
439 |
a = (const char*)sqlite3_value_blob(argv[0]); |
|
effa893…
|
drh
|
440 |
if( pCur->nDelta==0 || a==0 ){ |
|
effa893…
|
drh
|
441 |
return SQLITE_OK; |
|
effa893…
|
drh
|
442 |
} |
|
effa893…
|
drh
|
443 |
pCur->aDelta = sqlite3_malloc64( pCur->nDelta+1 ); |
|
effa893…
|
drh
|
444 |
if( pCur->aDelta==0 ){ |
|
effa893…
|
drh
|
445 |
pCur->nDelta = 0; |
|
effa893…
|
drh
|
446 |
return SQLITE_NOMEM; |
|
effa893…
|
drh
|
447 |
} |
|
effa893…
|
drh
|
448 |
memcpy(pCur->aDelta, a, pCur->nDelta); |
|
effa893…
|
drh
|
449 |
pCur->aDelta[pCur->nDelta] = 0; |
|
effa893…
|
drh
|
450 |
a = pCur->aDelta; |
|
effa893…
|
drh
|
451 |
pCur->eOp = DELTAPARSE_OP_SIZE; |
|
effa893…
|
drh
|
452 |
pCur->a1 = deltaGetInt(&a, &i); |
|
effa893…
|
drh
|
453 |
if( a[0]!='\n' ){ |
|
effa893…
|
drh
|
454 |
pCur->eOp = DELTAPARSE_OP_ERROR; |
|
effa893…
|
drh
|
455 |
pCur->a1 = pCur->a2 = 0; |
|
effa893…
|
drh
|
456 |
pCur->iNext = pCur->nDelta; |
|
effa893…
|
drh
|
457 |
return SQLITE_OK; |
|
effa893…
|
drh
|
458 |
} |
|
effa893…
|
drh
|
459 |
a++; |
|
921e7c1…
|
drh
|
460 |
pCur->iNext = (i64)(a - pCur->aDelta); |
|
effa893…
|
drh
|
461 |
return SQLITE_OK; |
|
effa893…
|
drh
|
462 |
} |
|
effa893…
|
drh
|
463 |
|
|
effa893…
|
drh
|
464 |
/* |
|
effa893…
|
drh
|
465 |
** SQLite will invoke this method one or more times while planning a query |
|
effa893…
|
drh
|
466 |
** that uses the virtual table. This routine needs to create |
|
effa893…
|
drh
|
467 |
** a query plan for each invocation and compute an estimated cost for that |
|
effa893…
|
drh
|
468 |
** plan. |
|
effa893…
|
drh
|
469 |
*/ |
|
effa893…
|
drh
|
470 |
static int deltaparsevtabBestIndex( |
|
effa893…
|
drh
|
471 |
sqlite3_vtab *tab, |
|
effa893…
|
drh
|
472 |
sqlite3_index_info *pIdxInfo |
|
effa893…
|
drh
|
473 |
){ |
|
effa893…
|
drh
|
474 |
int i; |
|
effa893…
|
drh
|
475 |
for(i=0; i<pIdxInfo->nConstraint; i++){ |
|
effa893…
|
drh
|
476 |
if( pIdxInfo->aConstraint[i].iColumn != DELTAPARSEVTAB_DELTA ) continue; |
|
effa893…
|
drh
|
477 |
if( pIdxInfo->aConstraint[i].usable==0 ) continue; |
|
effa893…
|
drh
|
478 |
if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
|
effa893…
|
drh
|
479 |
pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
|
effa893…
|
drh
|
480 |
pIdxInfo->aConstraintUsage[i].omit = 1; |
|
effa893…
|
drh
|
481 |
pIdxInfo->estimatedCost = (double)1; |
|
effa893…
|
drh
|
482 |
pIdxInfo->estimatedRows = 10; |
|
effa893…
|
drh
|
483 |
pIdxInfo->idxNum = 1; |
|
effa893…
|
drh
|
484 |
return SQLITE_OK; |
|
effa893…
|
drh
|
485 |
} |
|
effa893…
|
drh
|
486 |
pIdxInfo->idxNum = 0; |
|
effa893…
|
drh
|
487 |
pIdxInfo->estimatedCost = (double)0x7fffffff; |
|
effa893…
|
drh
|
488 |
pIdxInfo->estimatedRows = 0x7fffffff; |
|
effa893…
|
drh
|
489 |
return SQLITE_CONSTRAINT; |
|
effa893…
|
drh
|
490 |
} |
|
effa893…
|
drh
|
491 |
|
|
effa893…
|
drh
|
492 |
/* |
|
70a94d0…
|
jan.nijtmans
|
493 |
** This following structure defines all the methods for the |
|
effa893…
|
drh
|
494 |
** virtual table. |
|
effa893…
|
drh
|
495 |
*/ |
|
effa893…
|
drh
|
496 |
static sqlite3_module deltaparsevtabModule = { |
|
effa893…
|
drh
|
497 |
/* iVersion */ 0, |
|
effa893…
|
drh
|
498 |
/* xCreate */ 0, |
|
effa893…
|
drh
|
499 |
/* xConnect */ deltaparsevtabConnect, |
|
effa893…
|
drh
|
500 |
/* xBestIndex */ deltaparsevtabBestIndex, |
|
effa893…
|
drh
|
501 |
/* xDisconnect */ deltaparsevtabDisconnect, |
|
effa893…
|
drh
|
502 |
/* xDestroy */ 0, |
|
effa893…
|
drh
|
503 |
/* xOpen */ deltaparsevtabOpen, |
|
effa893…
|
drh
|
504 |
/* xClose */ deltaparsevtabClose, |
|
effa893…
|
drh
|
505 |
/* xFilter */ deltaparsevtabFilter, |
|
effa893…
|
drh
|
506 |
/* xNext */ deltaparsevtabNext, |
|
effa893…
|
drh
|
507 |
/* xEof */ deltaparsevtabEof, |
|
effa893…
|
drh
|
508 |
/* xColumn */ deltaparsevtabColumn, |
|
effa893…
|
drh
|
509 |
/* xRowid */ deltaparsevtabRowid, |
|
effa893…
|
drh
|
510 |
/* xUpdate */ 0, |
|
effa893…
|
drh
|
511 |
/* xBegin */ 0, |
|
effa893…
|
drh
|
512 |
/* xSync */ 0, |
|
effa893…
|
drh
|
513 |
/* xCommit */ 0, |
|
effa893…
|
drh
|
514 |
/* xRollback */ 0, |
|
effa893…
|
drh
|
515 |
/* xFindMethod */ 0, |
|
effa893…
|
drh
|
516 |
/* xRename */ 0, |
|
effa893…
|
drh
|
517 |
/* xSavepoint */ 0, |
|
effa893…
|
drh
|
518 |
/* xRelease */ 0, |
|
effa893…
|
drh
|
519 |
/* xRollbackTo */ 0, |
|
ab05475…
|
danield
|
520 |
/* xShadowName */ 0, |
|
ab05475…
|
danield
|
521 |
/* xIntegrity */ 0 |
|
effa893…
|
drh
|
522 |
}; |
|
effa893…
|
drh
|
523 |
|
|
effa893…
|
drh
|
524 |
/* |
|
effa893…
|
drh
|
525 |
** Invoke this routine to register the various delta functions. |
|
effa893…
|
drh
|
526 |
*/ |
|
effa893…
|
drh
|
527 |
int deltafunc_init(sqlite3 *db){ |
|
921e7c1…
|
drh
|
528 |
static const int enc = SQLITE_UTF8|SQLITE_INNOCUOUS; |
|
effa893…
|
drh
|
529 |
int rc = SQLITE_OK; |
|
921e7c1…
|
drh
|
530 |
rc = sqlite3_create_function(db, "delta_create", 2, enc, 0, |
|
effa893…
|
drh
|
531 |
deltaCreateFunc, 0, 0); |
|
effa893…
|
drh
|
532 |
if( rc==SQLITE_OK ){ |
|
921e7c1…
|
drh
|
533 |
rc = sqlite3_create_function(db, "delta_apply", 2, enc, 0, |
|
effa893…
|
drh
|
534 |
deltaApplyFunc, 0, 0); |
|
effa893…
|
drh
|
535 |
} |
|
effa893…
|
drh
|
536 |
if( rc==SQLITE_OK ){ |
|
921e7c1…
|
drh
|
537 |
rc = sqlite3_create_function(db, "delta_output_size", 1, enc, 0, |
|
effa893…
|
drh
|
538 |
deltaOutputSizeFunc, 0, 0); |
|
effa893…
|
drh
|
539 |
} |
|
effa893…
|
drh
|
540 |
if( rc==SQLITE_OK ){ |
|
effa893…
|
drh
|
541 |
rc = sqlite3_create_module(db, "delta_parse", &deltaparsevtabModule, 0); |
|
effa893…
|
drh
|
542 |
} |
|
effa893…
|
drh
|
543 |
return rc; |
|
effa893…
|
drh
|
544 |
} |