Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the start of the "zonefile" extension. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | zonefile |
Files: | files | file ages | folders |
SHA3-256: |
c125b4c380d1a20c7d71b413e96183ec |
User & Date: | dan 2018-02-10 17:41:01.882 |
Context
2018-02-10
| ||
21:04 | Add start of "zonefile" virtual table. (check-in: 0b7bd1694b user: dan tags: zonefile) | |
17:41 | Add the start of the "zonefile" extension. (check-in: c125b4c380 user: dan tags: zonefile) | |
02:31 | Fix misplaced testcase() macros from the previous check-in. (check-in: 3aed949a18 user: drh tags: trunk) | |
Changes
Added ext/zonefile/README.md.
> > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Notes: * Using 32-bit frame numbers (not 16-bit). * The ZonefileHeader object is 26 bytes in size. Which means that the ZoneFileIndex will not be 8-byte aligned. Problem? * The offsets in the ZoneFileIndex.byteOffsetZoneFrame[] array are relative to the offset in ZoneFileHeader.byteOffsetFrames. This is necessary as we may not know the offset of the start of the frame data until after the ZoneFileIndex structure is compressed. |
Added ext/zonefile/zonefile.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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | /* ** 2018-02-10 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ****************************************************************************** */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #ifndef SQLITE_OMIT_VIRTUALTABLE #ifndef SQLITE_AMALGAMATION typedef sqlite3_int64 i64; typedef sqlite3_uint64 u64; typedef unsigned char u8; typedef unsigned short u16; typedef unsigned long u32; #define MIN(a,b) ((a)<(b) ? (a) : (b)) #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) # define ALWAYS(X) (1) # define NEVER(X) (0) #elif !defined(NDEBUG) # define ALWAYS(X) ((X)?1:(assert(0),0)) # define NEVER(X) ((X)?(assert(0),1):0) #else # define ALWAYS(X) (X) # define NEVER(X) (X) #endif #endif /* SQLITE_AMALGAMATION */ #define ZONEFILE_MAGIC_NUMBER 0x464B3138 #define ZONEFILE_SZ_HEADER 26 #define ZONEFILE_DEFAULT_MAXAUTOFRAMESIZE (64*1024) #define ZONEFILE_DEFAULT_ENCRYPTION 0 #define ZONEFILE_DEFAULT_COMPRESSION 0 #include <stdio.h> #include <string.h> #include <assert.h> typedef struct ZonefileWrite ZonefileWrite; struct ZonefileWrite { int compressionTypeIndexData; int compressionTypeContent; int encryptionType; int maxAutoFrameSize; }; typedef struct ZonefileBuffer ZonefileBuffer; struct ZonefileBuffer { u8 *a; int n; int nAlloc; }; /* ** Set the error message contained in context ctx to the results of ** vprintf(zFmt, ...). */ static void zonefileCtxError(sqlite3_context *ctx, const char *zFmt, ...){ char *zMsg = 0; va_list ap; va_start(ap, zFmt); zMsg = sqlite3_vmprintf(zFmt, ap); sqlite3_result_error(ctx, zMsg, -1); sqlite3_free(zMsg); va_end(ap); } static void zonefileTransferError(sqlite3_context *pCtx){ sqlite3 *db = sqlite3_context_db_handle(pCtx); const char *zErr = sqlite3_errmsg(db); sqlite3_result_error(pCtx, zErr, -1); } static sqlite3_stmt *zonefilePrepare( sqlite3_context *pCtx, const char *zFmt, ... ){ sqlite3_stmt *pRet = 0; va_list ap; char *zSql; va_start(ap, zFmt); zSql = sqlite3_vmprintf(zFmt, ap); if( zSql ){ sqlite3 *db = sqlite3_context_db_handle(pCtx); int rc = sqlite3_prepare(db, zSql, -1, &pRet, 0); if( rc!=SQLITE_OK ){ zonefileTransferError(pCtx); } }else{ sqlite3_result_error_nomem(pCtx); } return pRet; } /* ** Return zero if the two SQL values passed as arguments are equal, or ** non-zero otherwise. Values with different types are considered unequal, ** even if they both contain the same numeric value (e.g. 2 and 2.0). */ static int zonefileCompareValue(sqlite3_value *p1, sqlite3_value *p2){ int eType; assert( p1 ); if( p2==0 ) return 1; eType = sqlite3_value_type(p1); if( sqlite3_value_type(p2)!=eType ) return 1; switch( eType ){ case SQLITE_INTEGER: return sqlite3_value_int64(p1)==sqlite3_value_int64(p2); case SQLITE_FLOAT: return sqlite3_value_double(p1)==sqlite3_value_double(p2); case SQLITE_TEXT: case SQLITE_BLOB: { int n1 = sqlite3_value_bytes(p1); int n2 = sqlite3_value_bytes(p2); if( n1!=n2 ) return 1; return memcmp(sqlite3_value_blob(p1), sqlite3_value_blob(p2), n1); } default: assert( eType==SQLITE_NULL); } return 0; } int zonefileIsAutoFrame(sqlite3_value *pFrame){ return ( sqlite3_value_type(pFrame)==SQLITE_INTEGER && sqlite3_value_int64(pFrame)==-1 ); } static int zonefileGetParams( sqlite3_context *pCtx, /* Leave any error message here */ const char *zJson, /* JSON configuration parameter (or NULL) */ ZonefileWrite *p /* Populate this object before returning */ ){ memset(p, 0, sizeof(ZonefileWrite)); p->maxAutoFrameSize = ZONEFILE_DEFAULT_MAXAUTOFRAMESIZE; return SQLITE_OK; } /* ** Check that there is room in buffer pBuf for at least nByte bytes more ** data. If not, attempt to allocate more space. If the allocation attempt ** fails, leave an error message in context pCtx and return SQLITE_ERROR. ** ** If no error occurs, SQLITE_OK is returned. */ static int zonefileBufferGrow( sqlite3_context *pCtx, ZonefileBuffer *pBuf, int nByte ){ int nReq = pBuf->n + nByte; if( nReq>pBuf->nAlloc ){ u8 *aNew; int nNew = pBuf->nAlloc ? pBuf->nAlloc*2 : 128; while( nNew<nReq ) nNew = nNew*2; aNew = sqlite3_realloc(pBuf->a, nNew); if( aNew==0 ){ sqlite3_result_error_nomem(pCtx); return SQLITE_ERROR; } pBuf->a = aNew; pBuf->nAlloc = nNew; } return SQLITE_OK; } static void zonefileBufferFree(ZonefileBuffer *pBuf){ sqlite3_free(pBuf->a); memset(pBuf, 0, sizeof(ZonefileBuffer)); } static void zonefilePut32(u8 *aBuf, u32 v){ aBuf[0] = (v >> 24) & 0xFF; aBuf[1] = (v >> 16) & 0xFF; aBuf[2] = (v >> 8) & 0xFF; aBuf[3] = v & 0xFF; } static void zonefileAppend32(ZonefileBuffer *pBuf, u32 v){ zonefilePut32(&pBuf->a[pBuf->n], v); pBuf->n += 4; } static void zonefileAppend64(ZonefileBuffer *pBuf, u64 v){ zonefileAppend32(pBuf, v>>32); zonefileAppend32(pBuf, v & 0xFFFFFFFF); } static void zonefileAppendBlob(ZonefileBuffer *pBuf, const u8 *p, int n){ memcpy(&pBuf->a[pBuf->n], p, n); pBuf->n += n; } static int zonefileWrite(FILE *pFd, const u8 *aBuf, int nBuf){ size_t res = fwrite(aBuf, 1, nBuf, pFd); return res!=nBuf ? SQLITE_ERROR : SQLITE_OK; } /* ** Function: zonefile_write(F,T[,J]) */ static void zonefileWriteFunc( sqlite3_context *pCtx, /* Context object */ int objc, /* Number of SQL arguments */ sqlite3_value **objv /* Array of SQL arguments */ ){ const char *zFile = 0; /* File to write to */ const char *zTbl = 0; /* Database object to read from */ const char *zJson = 0; /* JSON configuration parameters */ ZonefileWrite sWrite; /* Decoded JSON parameters */ int nKey = 0; /* Number of keys in new zonefile */ int nFrame = 0; /* Number of frames in new zonefile */ int szFrame = 0; /* Size of current frame */ sqlite3_stmt *pStmt = 0; /* SQL used to read data from source table */ FILE *pFd = 0; int rc; sqlite3_value *pPrev = 0; ZonefileBuffer sFrameIdx = {0, 0, 0}; ZonefileBuffer sKeyIdx = {0, 0, 0}; ZonefileBuffer sFrames = {0, 0, 0}; u8 aHdr[ZONEFILE_SZ_HEADER]; /* Space to assemble zonefile header */ assert( objc==2 || objc==3 ); zFile = (const char*)sqlite3_value_text(objv[0]); zTbl = (const char*)sqlite3_value_text(objv[1]); if( objc==3 ){ zJson = (const char*)sqlite3_value_text(objv[2]); } if( zonefileGetParams(pCtx, zJson, &sWrite) ) return; /* Prepare the SQL statement used to read data from the source table. This ** also serves to verify the suitability of the source table schema. */ pStmt = zonefilePrepare(pCtx, "SELECT k, frame, v FROM %Q ORDER BY frame, idx, k", zTbl ); if( pStmt==0 ) return; /* Open a file-handle used to write out the zonefile */ pFd = fopen(zFile, "w"); if( pFd==0 ){ zonefileCtxError(pCtx, "error opening file \"%s\" (fopen())", zFile); sqlite3_finalize(pStmt); return; } while( SQLITE_ROW==sqlite3_step(pStmt) ){ sqlite3_int64 k = sqlite3_column_int64(pStmt, 0); sqlite3_value *pFrame = sqlite3_column_value(pStmt, 1); int nBlob = sqlite3_column_bytes(pStmt, 2); const u8 *pBlob = (const u8*)sqlite3_column_blob(pStmt, 2); int bAuto = zonefileIsAutoFrame(pFrame); if( zonefileCompareValue(pFrame, pPrev) || (bAuto && szFrame && (szFrame+nBlob)>sWrite.maxAutoFrameSize) ){ /* Add new entry to sFrameIdx */ szFrame = 0; if( zonefileBufferGrow(pCtx, &sFrameIdx, 4) ) goto zone_write_out; zonefileAppend32(&sFrameIdx, sFrames.n); sqlite3_value_free(pPrev); pPrev = sqlite3_value_dup(pFrame); if( pPrev==0 ){ sqlite3_result_error_nomem(pCtx); goto zone_write_out; } nFrame++; } /* Add new entry to sKeyIdx */ if( zonefileBufferGrow(pCtx, &sKeyIdx, 20) ) goto zone_write_out; zonefileAppend64(&sKeyIdx, k); zonefileAppend32(&sKeyIdx, nFrame-1); zonefileAppend32(&sKeyIdx, szFrame); zonefileAppend32(&sKeyIdx, nBlob); /* Add data for new entry to sFrames */ if( zonefileBufferGrow(pCtx, &sFrames, nBlob) ) goto zone_write_out; zonefileAppendBlob(&sFrames, pBlob, nBlob); szFrame += nBlob; nKey++; } /* Create the zonefile header in the in-memory buffer */ zonefilePut32(&aHdr[0], ZONEFILE_MAGIC_NUMBER); aHdr[4] = sWrite.compressionTypeIndexData; aHdr[5] = sWrite.compressionTypeContent; zonefilePut32(&aHdr[6], 0); /* Compression dictionary byte offset */ zonefilePut32(&aHdr[10], ZONEFILE_SZ_HEADER + sFrameIdx.n + sKeyIdx.n); zonefilePut32(&aHdr[14], nFrame); zonefilePut32(&aHdr[18], nKey); aHdr[22] = sWrite.encryptionType; aHdr[23] = 0; /* Encryption key index */ aHdr[24] = 0; /* extended header version */ aHdr[25] = 0; /* extended header size */ assert( ZONEFILE_SZ_HEADER==26 ); rc = zonefileWrite(pFd, aHdr, ZONEFILE_SZ_HEADER); if( rc==SQLITE_OK ) rc = zonefileWrite(pFd, sFrameIdx.a, sFrameIdx.n); if( rc==SQLITE_OK ) rc = zonefileWrite(pFd, sKeyIdx.a, sKeyIdx.n); if( rc==SQLITE_OK ) rc = zonefileWrite(pFd, sFrames.a, sFrames.n); if( rc ){ zonefileCtxError(pCtx, "error writing file \"%s\" (fwrite())", zFile); goto zone_write_out; } if( fclose(pFd) ){ zonefileCtxError(pCtx, "error writing file \"%s\" (fclose())", zFile); } pFd = 0; zone_write_out: if( pFd ) fclose(pFd); sqlite3_finalize(pStmt); zonefileBufferFree(&sFrameIdx); zonefileBufferFree(&sKeyIdx); zonefileBufferFree(&sFrames); } /* ** Register the "zonefile" extensions. */ static int zonefileRegister(sqlite3 *db){ struct Func { const char *z; int n; void (*x)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { { "zonefile_write", 2, zonefileWriteFunc }, { "zonefile_write", 3, zonefileWriteFunc }, }; int rc = SQLITE_OK; int i; for(i=0; rc==SQLITE_OK && i<sizeof(aFunc)/sizeof(aFunc[0]); i++){ struct Func *p = &aFunc[i]; rc = sqlite3_create_function(db, p->z, p->n, SQLITE_ANY, 0, p->x, 0, 0); } return rc; } #else /* SQLITE_OMIT_VIRTUALTABLE */ # define zonefileRegister(x) SQLITE_OK #endif #ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_zonefile_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ return zonefileRegister(db); } |
Added ext/zonefile/zonefile1.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | # 2018 Feb 11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # The focus of this file is testing the zonefile extension. # if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join $testdir tester.tcl] set testprefix zonefile1 load_static_extension db zonefile do_execsql_test 1.0 { CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO zz VALUES(1, -1, -1, randomblob(100)); INSERT INTO zz VALUES(2, -1, -1, randomblob(100)); INSERT INTO zz VALUES(3, -1, -1, randomblob(100)); } do_execsql_test 1.1 { SELECT zonefile_write('test.zonefile', 'zz'); } finish_test |
Changes to main.mk.
︙ | ︙ | |||
368 369 370 371 372 373 374 375 376 377 378 379 380 381 | $(TOP)/ext/misc/series.c \ $(TOP)/ext/misc/spellfix.c \ $(TOP)/ext/misc/totype.c \ $(TOP)/ext/misc/unionvtab.c \ $(TOP)/ext/misc/wholenumber.c \ $(TOP)/ext/misc/vfslog.c \ $(TOP)/ext/misc/zipfile.c \ $(TOP)/ext/fts5/fts5_tcl.c \ $(TOP)/ext/fts5/fts5_test_mi.c \ $(TOP)/ext/fts5/fts5_test_tok.c #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c | > | 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | $(TOP)/ext/misc/series.c \ $(TOP)/ext/misc/spellfix.c \ $(TOP)/ext/misc/totype.c \ $(TOP)/ext/misc/unionvtab.c \ $(TOP)/ext/misc/wholenumber.c \ $(TOP)/ext/misc/vfslog.c \ $(TOP)/ext/misc/zipfile.c \ $(TOP)/ext/zonefile/zonefile.c \ $(TOP)/ext/fts5/fts5_tcl.c \ $(TOP)/ext/fts5/fts5_test_mi.c \ $(TOP)/ext/fts5/fts5_test_tok.c #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c |
︙ | ︙ |
Changes to src/test1.c.
︙ | ︙ | |||
6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 | extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*); #ifdef SQLITE_HAVE_ZLIB extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*); #endif static const struct { const char *zExtName; int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); } aExtension[] = { { "amatch", sqlite3_amatch_init }, { "carray", sqlite3_carray_init }, { "closure", sqlite3_closure_init }, | > | 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 | extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*); #ifdef SQLITE_HAVE_ZLIB extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*); #endif extern int sqlite3_zonefile_init(sqlite3*,char**,const sqlite3_api_routines*); static const struct { const char *zExtName; int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); } aExtension[] = { { "amatch", sqlite3_amatch_init }, { "carray", sqlite3_carray_init }, { "closure", sqlite3_closure_init }, |
︙ | ︙ | |||
7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 | { "spellfix", sqlite3_spellfix_init }, { "totype", sqlite3_totype_init }, { "unionvtab", sqlite3_unionvtab_init }, { "wholenumber", sqlite3_wholenumber_init }, #ifdef SQLITE_HAVE_ZLIB { "zipfile", sqlite3_zipfile_init }, #endif }; sqlite3 *db; const char *zName; int i, j, rc; char *zErrMsg = 0; if( objc<3 ){ Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); | > | 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 | { "spellfix", sqlite3_spellfix_init }, { "totype", sqlite3_totype_init }, { "unionvtab", sqlite3_unionvtab_init }, { "wholenumber", sqlite3_wholenumber_init }, #ifdef SQLITE_HAVE_ZLIB { "zipfile", sqlite3_zipfile_init }, #endif { "zonefile", sqlite3_zonefile_init }, }; sqlite3 *db; const char *zName; int i, j, rc; char *zErrMsg = 0; if( objc<3 ){ Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); |
︙ | ︙ |