lsm_vtab.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. ** 2015-11-16
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. **
  13. ** This file implements a virtual table for SQLite3 around the LSM
  14. ** storage engine from SQLite4.
  15. **
  16. ** USAGE
  17. **
  18. ** CREATE VIRTUAL TABLE demo USING lsm1(filename,key,keytype,value1,...);
  19. **
  20. ** The filename parameter is the name of the LSM database file, which is
  21. ** separate and distinct from the SQLite3 database file.
  22. **
  23. ** The keytype must be one of: UINT, TEXT, BLOB. All keys must be of that
  24. ** one type. "UINT" means unsigned integer. The values may be of any
  25. ** SQLite datatype: BLOB, TEXT, INTEGER, FLOAT, or NULL.
  26. **
  27. ** The virtual table contains read-only hidden columns:
  28. **
  29. ** lsm1_key A BLOB which is the raw LSM key. If the "keytype"
  30. ** is BLOB or TEXT then this column is exactly the
  31. ** same as the key. For the UINT keytype, this column
  32. ** will be a variable-length integer encoding of the key.
  33. **
  34. ** lsm1_value A BLOB which is the raw LSM value. All of the value
  35. ** columns are packed into this BLOB using the encoding
  36. ** described below.
  37. **
  38. ** Attempts to write values into the lsm1_key and lsm1_value columns are
  39. ** silently ignored.
  40. **
  41. ** EXAMPLE
  42. **
  43. ** The virtual table declared this way:
  44. **
  45. ** CREATE VIRTUAL TABLE demo2 USING lsm1('x.lsm',id,UINT,a,b,c,d);
  46. **
  47. ** Results in a new virtual table named "demo2" that acts as if it has
  48. ** the following schema:
  49. **
  50. ** CREATE TABLE demo2(
  51. ** id UINT PRIMARY KEY ON CONFLICT REPLACE,
  52. ** a ANY,
  53. ** b ANY,
  54. ** c ANY,
  55. ** d ANY,
  56. ** lsm1_key BLOB HIDDEN,
  57. ** lsm1_value BLOB HIDDEN
  58. ** ) WITHOUT ROWID;
  59. **
  60. **
  61. **
  62. ** INTERNALS
  63. **
  64. ** The key encoding for BLOB and TEXT is just a copy of the blob or text.
  65. ** UTF-8 is used for text. The key encoding for UINT is the variable-length
  66. ** integer format at https://sqlite.org/src4/doc/trunk/www/varint.wiki.
  67. **
  68. ** The values are encoded as a single blob (since that is what lsm stores as
  69. ** its content). There is a "type integer" followed by "content" for each
  70. ** value, alternating back and forth. The content might be empty.
  71. **
  72. ** TYPE1 CONTENT1 TYPE2 CONTENT2 TYPE3 CONTENT3 ....
  73. **
  74. ** Each "type integer" is encoded as a variable-length integer in the
  75. ** format of the link above. Let the type integer be T. The actual
  76. ** datatype is an integer 0-5 equal to T%6. Values 1 through 5 correspond
  77. ** to SQLITE_INTEGER through SQLITE_NULL. The size of the content in bytes
  78. ** is T/6. Type value 0 means that the value is an integer whose actual
  79. ** values is T/6 and there is no content. The type-value-0 integer format
  80. ** only works for integers in the range of 0 through 40.
  81. **
  82. ** There is no content for NULL or type-0 integers. For BLOB and TEXT
  83. ** values, the content is the blob data or the UTF-8 text data. For
  84. ** non-negative integers X, the content is a variable-length integer X*2.
  85. ** For negative integers Y, the content is varaible-length integer (1-Y)*2+1.
  86. ** For FLOAT values, the content is the IEEE754 floating point value in
  87. ** native byte-order. This means that FLOAT values will be corrupted when
  88. ** database file is moved between big-endian and little-endian machines.
  89. */
  90. #include "sqlite3ext.h"
  91. SQLITE_EXTENSION_INIT1
  92. #include "lsm.h"
  93. #include <assert.h>
  94. #include <string.h>
  95. /* Forward declaration of subclasses of virtual table objects */
  96. typedef struct lsm1_vtab lsm1_vtab;
  97. typedef struct lsm1_cursor lsm1_cursor;
  98. typedef struct lsm1_vblob lsm1_vblob;
  99. /* Primitive types */
  100. typedef unsigned char u8;
  101. typedef unsigned int u32;
  102. typedef sqlite3_uint64 u64;
  103. /* An open connection to an LSM table */
  104. struct lsm1_vtab {
  105. sqlite3_vtab base; /* Base class - must be first */
  106. lsm_db *pDb; /* Open connection to the LSM table */
  107. u8 keyType; /* SQLITE_BLOB, _TEXT, or _INTEGER */
  108. u32 nVal; /* Number of value columns */
  109. };
  110. /* lsm1_cursor is a subclass of sqlite3_vtab_cursor which will
  111. ** serve as the underlying representation of a cursor that scans
  112. ** over rows of the result
  113. */
  114. struct lsm1_cursor {
  115. sqlite3_vtab_cursor base; /* Base class - must be first */
  116. lsm_cursor *pLsmCur; /* The LSM cursor */
  117. u8 isDesc; /* 0: scan forward. 1: scan reverse */
  118. u8 atEof; /* True if the scan is complete */
  119. u8 bUnique; /* True if no more than one row of output */
  120. u8 *zData; /* Content of the current row */
  121. u32 nData; /* Number of bytes in the current row */
  122. u8 *aeType; /* Types for all column values */
  123. u32 *aiOfst; /* Offsets to the various fields */
  124. u32 *aiLen; /* Length of each field */
  125. u8 *pKey2; /* Loop termination key, or NULL */
  126. u32 nKey2; /* Length of the loop termination key */
  127. };
  128. /* An extensible buffer object.
  129. **
  130. ** Content can be appended. Space to hold new content is automatically
  131. ** allocated.
  132. */
  133. struct lsm1_vblob {
  134. u8 *a; /* Space to hold content, from sqlite3_malloc64() */
  135. u64 n; /* Bytes of space used */
  136. u64 nAlloc; /* Bytes of space allocated */
  137. u8 errNoMem; /* True if a memory allocation error has been seen */
  138. };
  139. #if defined(__GNUC__)
  140. # define LSM1_NOINLINE __attribute__((noinline))
  141. #elif defined(_MSC_VER) && _MSC_VER>=1310
  142. # define LSM1_NOINLINE __declspec(noinline)
  143. #else
  144. # define LSM1_NOINLINE
  145. #endif
  146. /* Increase the available space in the vblob object so that it can hold
  147. ** at least N more bytes. Return the number of errors.
  148. */
  149. static int lsm1VblobEnlarge(lsm1_vblob *p, u32 N){
  150. if( p->n+N>p->nAlloc ){
  151. if( p->errNoMem ) return 1;
  152. p->nAlloc += N + (p->nAlloc ? p->nAlloc : N);
  153. p->a = sqlite3_realloc64(p->a, p->nAlloc);
  154. if( p->a==0 ){
  155. p->n = 0;
  156. p->nAlloc = 0;
  157. p->errNoMem = 1;
  158. return 1;
  159. }
  160. p->nAlloc = sqlite3_msize(p->a);
  161. }
  162. return 0;
  163. }
  164. /* Append N bytes to a vblob after first enlarging it */
  165. static LSM1_NOINLINE void lsm1VblobEnlargeAndAppend(
  166. lsm1_vblob *p,
  167. const u8 *pData,
  168. u32 N
  169. ){
  170. if( p->n+N>p->nAlloc && lsm1VblobEnlarge(p, N) ) return;
  171. memcpy(p->a+p->n, pData, N);
  172. p->n += N;
  173. }
  174. /* Append N bytes to a vblob */
  175. static void lsm1VblobAppend(lsm1_vblob *p, const u8 *pData, u32 N){
  176. sqlite3_int64 n = p->n;
  177. if( n+N>p->nAlloc ){
  178. lsm1VblobEnlargeAndAppend(p, pData, N);
  179. }else{
  180. p->n += N;
  181. memcpy(p->a+n, pData, N);
  182. }
  183. }
  184. /* append text to a vblob */
  185. static void lsm1VblobAppendText(lsm1_vblob *p, const char *z){
  186. lsm1VblobAppend(p, (u8*)z, (u32)strlen(z));
  187. }
  188. /* Dequote the string */
  189. static void lsm1Dequote(char *z){
  190. int j;
  191. char cQuote = z[0];
  192. size_t i, n;
  193. if( cQuote!='\'' && cQuote!='"' ) return;
  194. n = strlen(z);
  195. if( n<2 || z[n-1]!=z[0] ) return;
  196. for(i=1, j=0; i<n-1; i++){
  197. if( z[i]==cQuote && z[i+1]==cQuote ) i++;
  198. z[j++] = z[i];
  199. }
  200. z[j] = 0;
  201. }
  202. /*
  203. ** The lsm1Connect() method is invoked to create a new
  204. ** lsm1_vtab that describes the virtual table.
  205. */
  206. static int lsm1Connect(
  207. sqlite3 *db,
  208. void *pAux,
  209. int argc, const char *const*argv,
  210. sqlite3_vtab **ppVtab,
  211. char **pzErr
  212. ){
  213. lsm1_vtab *pNew;
  214. int rc;
  215. char *zFilename;
  216. u8 keyType = 0;
  217. int i;
  218. lsm1_vblob sql;
  219. static const char *azTypes[] = { "UINT", "TEXT", "BLOB" };
  220. static const u8 aeTypes[] = { SQLITE_INTEGER, SQLITE_TEXT, SQLITE_BLOB };
  221. static const char *azArgName[] = {"filename", "key", "key type", "value1" };
  222. for(i=0; i<sizeof(azArgName)/sizeof(azArgName[0]); i++){
  223. if( argc<i+4 || argv[i+3]==0 || argv[i+3][0]==0 ){
  224. *pzErr = sqlite3_mprintf("%s (%r) argument missing",
  225. azArgName[i], i+1);
  226. return SQLITE_ERROR;
  227. }
  228. }
  229. for(i=0; i<sizeof(azTypes)/sizeof(azTypes[0]); i++){
  230. if( sqlite3_stricmp(azTypes[i],argv[5])==0 ){
  231. keyType = aeTypes[i];
  232. break;
  233. }
  234. }
  235. if( keyType==0 ){
  236. *pzErr = sqlite3_mprintf("key type should be INT, TEXT, or BLOB");
  237. return SQLITE_ERROR;
  238. }
  239. *ppVtab = sqlite3_malloc( sizeof(*pNew) );
  240. pNew = (lsm1_vtab*)*ppVtab;
  241. if( pNew==0 ){
  242. return SQLITE_NOMEM;
  243. }
  244. memset(pNew, 0, sizeof(*pNew));
  245. pNew->keyType = keyType;
  246. rc = lsm_new(0, &pNew->pDb);
  247. if( rc ){
  248. *pzErr = sqlite3_mprintf("lsm_new failed with error code %d", rc);
  249. rc = SQLITE_ERROR;
  250. goto connect_failed;
  251. }
  252. zFilename = sqlite3_mprintf("%s", argv[3]);
  253. lsm1Dequote(zFilename);
  254. rc = lsm_open(pNew->pDb, zFilename);
  255. sqlite3_free(zFilename);
  256. if( rc ){
  257. *pzErr = sqlite3_mprintf("lsm_open failed with %d", rc);
  258. rc = SQLITE_ERROR;
  259. goto connect_failed;
  260. }
  261. memset(&sql, 0, sizeof(sql));
  262. lsm1VblobAppendText(&sql, "CREATE TABLE x(");
  263. lsm1VblobAppendText(&sql, argv[4]);
  264. lsm1VblobAppendText(&sql, " ");
  265. lsm1VblobAppendText(&sql, argv[5]);
  266. lsm1VblobAppendText(&sql, " PRIMARY KEY");
  267. for(i=6; i<argc; i++){
  268. lsm1VblobAppendText(&sql, ", ");
  269. lsm1VblobAppendText(&sql, argv[i]);
  270. pNew->nVal++;
  271. }
  272. lsm1VblobAppendText(&sql,
  273. ", lsm1_command HIDDEN"
  274. ", lsm1_key HIDDEN"
  275. ", lsm1_value HIDDEN) WITHOUT ROWID");
  276. lsm1VblobAppend(&sql, (u8*)"", 1);
  277. if( sql.errNoMem ){
  278. rc = SQLITE_NOMEM;
  279. goto connect_failed;
  280. }
  281. rc = sqlite3_declare_vtab(db, (const char*)sql.a);
  282. sqlite3_free(sql.a);
  283. connect_failed:
  284. if( rc!=SQLITE_OK ){
  285. if( pNew ){
  286. if( pNew->pDb ) lsm_close(pNew->pDb);
  287. sqlite3_free(pNew);
  288. }
  289. *ppVtab = 0;
  290. }
  291. return rc;
  292. }
  293. /*
  294. ** This method is the destructor for lsm1_cursor objects.
  295. */
  296. static int lsm1Disconnect(sqlite3_vtab *pVtab){
  297. lsm1_vtab *p = (lsm1_vtab*)pVtab;
  298. lsm_close(p->pDb);
  299. sqlite3_free(p);
  300. return SQLITE_OK;
  301. }
  302. /*
  303. ** Constructor for a new lsm1_cursor object.
  304. */
  305. static int lsm1Open(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
  306. lsm1_vtab *p = (lsm1_vtab*)pVtab;
  307. lsm1_cursor *pCur;
  308. int rc;
  309. pCur = sqlite3_malloc64( sizeof(*pCur)
  310. + p->nVal*(sizeof(pCur->aiOfst)+sizeof(pCur->aiLen)+1) );
  311. if( pCur==0 ) return SQLITE_NOMEM;
  312. memset(pCur, 0, sizeof(*pCur));
  313. pCur->aiOfst = (u32*)&pCur[1];
  314. pCur->aiLen = &pCur->aiOfst[p->nVal];
  315. pCur->aeType = (u8*)&pCur->aiLen[p->nVal];
  316. *ppCursor = &pCur->base;
  317. rc = lsm_csr_open(p->pDb, &pCur->pLsmCur);
  318. if( rc==LSM_OK ){
  319. rc = SQLITE_OK;
  320. }else{
  321. sqlite3_free(pCur);
  322. *ppCursor = 0;
  323. rc = SQLITE_ERROR;
  324. }
  325. return rc;
  326. }
  327. /*
  328. ** Destructor for a lsm1_cursor.
  329. */
  330. static int lsm1Close(sqlite3_vtab_cursor *cur){
  331. lsm1_cursor *pCur = (lsm1_cursor*)cur;
  332. sqlite3_free(pCur->pKey2);
  333. lsm_csr_close(pCur->pLsmCur);
  334. sqlite3_free(pCur);
  335. return SQLITE_OK;
  336. }
  337. /*
  338. ** Advance a lsm1_cursor to its next row of output.
  339. */
  340. static int lsm1Next(sqlite3_vtab_cursor *cur){
  341. lsm1_cursor *pCur = (lsm1_cursor*)cur;
  342. int rc = LSM_OK;
  343. if( pCur->bUnique ){
  344. pCur->atEof = 1;
  345. }else{
  346. if( pCur->isDesc ){
  347. rc = lsm_csr_prev(pCur->pLsmCur);
  348. }else{
  349. rc = lsm_csr_next(pCur->pLsmCur);
  350. }
  351. if( rc==LSM_OK && lsm_csr_valid(pCur->pLsmCur)==0 ){
  352. pCur->atEof = 1;
  353. }
  354. if( pCur->pKey2 && pCur->atEof==0 ){
  355. const u8 *pVal;
  356. u32 nVal;
  357. assert( pCur->isDesc==0 );
  358. rc = lsm_csr_key(pCur->pLsmCur, (const void**)&pVal, (int*)&nVal);
  359. if( rc==LSM_OK ){
  360. u32 len = pCur->nKey2;
  361. int c;
  362. if( len>nVal ) len = nVal;
  363. c = memcmp(pVal, pCur->pKey2, len);
  364. if( c==0 ) c = nVal - pCur->nKey2;
  365. if( c>0 ) pCur->atEof = 1;
  366. }
  367. }
  368. pCur->zData = 0;
  369. }
  370. return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
  371. }
  372. /*
  373. ** Return TRUE if the cursor has been moved off of the last
  374. ** row of output.
  375. */
  376. static int lsm1Eof(sqlite3_vtab_cursor *cur){
  377. lsm1_cursor *pCur = (lsm1_cursor*)cur;
  378. return pCur->atEof;
  379. }
  380. /*
  381. ** Rowids are not supported by the underlying virtual table. So always
  382. ** return 0 for the rowid.
  383. */
  384. static int lsm1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  385. *pRowid = 0;
  386. return SQLITE_OK;
  387. }
  388. /*
  389. ** Type prefixes on LSM keys
  390. */
  391. #define LSM1_TYPE_NEGATIVE 0
  392. #define LSM1_TYPE_POSITIVE 1
  393. #define LSM1_TYPE_TEXT 2
  394. #define LSM1_TYPE_BLOB 3
  395. /*
  396. ** Write a 32-bit unsigned integer as 4 big-endian bytes.
  397. */
  398. static void varintWrite32(unsigned char *z, unsigned int y){
  399. z[0] = (unsigned char)(y>>24);
  400. z[1] = (unsigned char)(y>>16);
  401. z[2] = (unsigned char)(y>>8);
  402. z[3] = (unsigned char)(y);
  403. }
  404. /*
  405. ** Write a varint into z[]. The buffer z[] must be at least 9 characters
  406. ** long to accommodate the largest possible varint. Return the number of
  407. ** bytes of z[] used.
  408. */
  409. static int lsm1PutVarint64(unsigned char *z, sqlite3_uint64 x){
  410. unsigned int w, y;
  411. if( x<=240 ){
  412. z[0] = (unsigned char)x;
  413. return 1;
  414. }
  415. if( x<=2287 ){
  416. y = (unsigned int)(x - 240);
  417. z[0] = (unsigned char)(y/256 + 241);
  418. z[1] = (unsigned char)(y%256);
  419. return 2;
  420. }
  421. if( x<=67823 ){
  422. y = (unsigned int)(x - 2288);
  423. z[0] = 249;
  424. z[1] = (unsigned char)(y/256);
  425. z[2] = (unsigned char)(y%256);
  426. return 3;
  427. }
  428. y = (unsigned int)x;
  429. w = (unsigned int)(x>>32);
  430. if( w==0 ){
  431. if( y<=16777215 ){
  432. z[0] = 250;
  433. z[1] = (unsigned char)(y>>16);
  434. z[2] = (unsigned char)(y>>8);
  435. z[3] = (unsigned char)(y);
  436. return 4;
  437. }
  438. z[0] = 251;
  439. varintWrite32(z+1, y);
  440. return 5;
  441. }
  442. if( w<=255 ){
  443. z[0] = 252;
  444. z[1] = (unsigned char)w;
  445. varintWrite32(z+2, y);
  446. return 6;
  447. }
  448. if( w<=65535 ){
  449. z[0] = 253;
  450. z[1] = (unsigned char)(w>>8);
  451. z[2] = (unsigned char)w;
  452. varintWrite32(z+3, y);
  453. return 7;
  454. }
  455. if( w<=16777215 ){
  456. z[0] = 254;
  457. z[1] = (unsigned char)(w>>16);
  458. z[2] = (unsigned char)(w>>8);
  459. z[3] = (unsigned char)w;
  460. varintWrite32(z+4, y);
  461. return 8;
  462. }
  463. z[0] = 255;
  464. varintWrite32(z+1, w);
  465. varintWrite32(z+5, y);
  466. return 9;
  467. }
  468. /* Append non-negative integer x as a variable-length integer.
  469. */
  470. static void lsm1VblobAppendVarint(lsm1_vblob *p, sqlite3_uint64 x){
  471. sqlite3_int64 n = p->n;
  472. if( n+9>p->nAlloc && lsm1VblobEnlarge(p, 9) ) return;
  473. p->n += lsm1PutVarint64(p->a+p->n, x);
  474. }
  475. /*
  476. ** Decode the varint in the first n bytes z[]. Write the integer value
  477. ** into *pResult and return the number of bytes in the varint.
  478. **
  479. ** If the decode fails because there are not enough bytes in z[] then
  480. ** return 0;
  481. */
  482. static int lsm1GetVarint64(
  483. const unsigned char *z,
  484. int n,
  485. sqlite3_uint64 *pResult
  486. ){
  487. unsigned int x;
  488. if( n<1 ) return 0;
  489. if( z[0]<=240 ){
  490. *pResult = z[0];
  491. return 1;
  492. }
  493. if( z[0]<=248 ){
  494. if( n<2 ) return 0;
  495. *pResult = (z[0]-241)*256 + z[1] + 240;
  496. return 2;
  497. }
  498. if( n<z[0]-246 ) return 0;
  499. if( z[0]==249 ){
  500. *pResult = 2288 + 256*z[1] + z[2];
  501. return 3;
  502. }
  503. if( z[0]==250 ){
  504. *pResult = (z[1]<<16) + (z[2]<<8) + z[3];
  505. return 4;
  506. }
  507. x = (z[1]<<24) + (z[2]<<16) + (z[3]<<8) + z[4];
  508. if( z[0]==251 ){
  509. *pResult = x;
  510. return 5;
  511. }
  512. if( z[0]==252 ){
  513. *pResult = (((sqlite3_uint64)x)<<8) + z[5];
  514. return 6;
  515. }
  516. if( z[0]==253 ){
  517. *pResult = (((sqlite3_uint64)x)<<16) + (z[5]<<8) + z[6];
  518. return 7;
  519. }
  520. if( z[0]==254 ){
  521. *pResult = (((sqlite3_uint64)x)<<24) + (z[5]<<16) + (z[6]<<8) + z[7];
  522. return 8;
  523. }
  524. *pResult = (((sqlite3_uint64)x)<<32) +
  525. (0xffffffff & ((z[5]<<24) + (z[6]<<16) + (z[7]<<8) + z[8]));
  526. return 9;
  527. }
  528. /* Encoded a signed integer as a varint. Numbers close to zero uses fewer
  529. ** bytes than numbers far away from zero. However, the result is not in
  530. ** lexicographical order.
  531. **
  532. ** Encoding: Non-negative integer X is encoding as an unsigned
  533. ** varint X*2. Negative integer Y is encoding as an unsigned
  534. ** varint (1-Y)*2 + 1.
  535. */
  536. static int lsm1PutSignedVarint64(u8 *z, sqlite3_int64 v){
  537. sqlite3_uint64 u;
  538. if( v>=0 ){
  539. u = (sqlite3_uint64)v;
  540. return lsm1PutVarint64(z, u*2);
  541. }else{
  542. u = (sqlite3_uint64)(-1-v);
  543. return lsm1PutVarint64(z, u*2+1);
  544. }
  545. }
  546. /* Decoded a signed varint. */
  547. static int lsm1GetSignedVarint64(
  548. const unsigned char *z,
  549. int n,
  550. sqlite3_int64 *pResult
  551. ){
  552. sqlite3_uint64 u = 0;
  553. n = lsm1GetVarint64(z, n, &u);
  554. if( u&1 ){
  555. *pResult = -1 - (sqlite3_int64)(u>>1);
  556. }else{
  557. *pResult = (sqlite3_int64)(u>>1);
  558. }
  559. return n;
  560. }
  561. /*
  562. ** Read the value part of the key-value pair and decode it into columns.
  563. */
  564. static int lsm1DecodeValues(lsm1_cursor *pCur){
  565. lsm1_vtab *pTab = (lsm1_vtab*)(pCur->base.pVtab);
  566. int i, n;
  567. int rc;
  568. u8 eType;
  569. sqlite3_uint64 v;
  570. if( pCur->zData ) return 1;
  571. rc = lsm_csr_value(pCur->pLsmCur, (const void**)&pCur->zData,
  572. (int*)&pCur->nData);
  573. if( rc ) return 0;
  574. for(i=n=0; i<pTab->nVal; i++){
  575. v = 0;
  576. n += lsm1GetVarint64(pCur->zData+n, pCur->nData-n, &v);
  577. pCur->aeType[i] = eType = (u8)(v%6);
  578. if( eType==0 ){
  579. pCur->aiOfst[i] = (u32)(v/6);
  580. pCur->aiLen[i] = 0;
  581. }else{
  582. pCur->aiOfst[i] = n;
  583. n += (pCur->aiLen[i] = (u32)(v/6));
  584. }
  585. if( n>pCur->nData ) break;
  586. }
  587. if( i<pTab->nVal ){
  588. pCur->zData = 0;
  589. return 0;
  590. }
  591. return 1;
  592. }
  593. /*
  594. ** Return values of columns for the row at which the lsm1_cursor
  595. ** is currently pointing.
  596. */
  597. static int lsm1Column(
  598. sqlite3_vtab_cursor *cur, /* The cursor */
  599. sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
  600. int i /* Which column to return */
  601. ){
  602. lsm1_cursor *pCur = (lsm1_cursor*)cur;
  603. lsm1_vtab *pTab = (lsm1_vtab*)(cur->pVtab);
  604. if( i==0 ){
  605. /* The key column */
  606. const void *pVal;
  607. int nVal;
  608. if( lsm_csr_key(pCur->pLsmCur, &pVal, &nVal)==LSM_OK ){
  609. if( pTab->keyType==SQLITE_BLOB ){
  610. sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
  611. }else if( pTab->keyType==SQLITE_TEXT ){
  612. sqlite3_result_text(ctx,(const char*)pVal, nVal, SQLITE_TRANSIENT);
  613. }else{
  614. const unsigned char *z = (const unsigned char*)pVal;
  615. sqlite3_uint64 v1;
  616. lsm1GetVarint64(z, nVal, &v1);
  617. sqlite3_result_int64(ctx, (sqlite3_int64)v1);
  618. }
  619. }
  620. }else if( i>pTab->nVal ){
  621. if( i==pTab->nVal+2 ){ /* lsm1_key */
  622. const void *pVal;
  623. int nVal;
  624. if( lsm_csr_key(pCur->pLsmCur, &pVal, &nVal)==LSM_OK ){
  625. sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
  626. }
  627. }else if( i==pTab->nVal+3 ){ /* lsm1_value */
  628. const void *pVal;
  629. int nVal;
  630. if( lsm_csr_value(pCur->pLsmCur, &pVal, &nVal)==LSM_OK ){
  631. sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
  632. }
  633. }
  634. }else if( lsm1DecodeValues(pCur) ){
  635. /* The i-th value column (where leftmost is 1) */
  636. const u8 *zData;
  637. u32 nData;
  638. i--;
  639. zData = pCur->zData + pCur->aiOfst[i];
  640. nData = pCur->aiLen[i];
  641. switch( pCur->aeType[i] ){
  642. case 0: { /* in-line integer */
  643. sqlite3_result_int(ctx, pCur->aiOfst[i]);
  644. break;
  645. }
  646. case SQLITE_INTEGER: {
  647. sqlite3_int64 v;
  648. lsm1GetSignedVarint64(zData, nData, &v);
  649. sqlite3_result_int64(ctx, v);
  650. break;
  651. }
  652. case SQLITE_FLOAT: {
  653. double v;
  654. if( nData==sizeof(v) ){
  655. memcpy(&v, zData, sizeof(v));
  656. sqlite3_result_double(ctx, v);
  657. }
  658. break;
  659. }
  660. case SQLITE_TEXT: {
  661. sqlite3_result_text(ctx, (const char*)zData, nData, SQLITE_TRANSIENT);
  662. break;
  663. }
  664. case SQLITE_BLOB: {
  665. sqlite3_result_blob(ctx, zData, nData, SQLITE_TRANSIENT);
  666. break;
  667. }
  668. default: {
  669. /* A NULL. Do nothing */
  670. }
  671. }
  672. }
  673. return SQLITE_OK;
  674. }
  675. /* Parameter "pValue" contains an SQL value that is to be used as
  676. ** a key in an LSM table. The type of the key is determined by
  677. ** "keyType". Extract the raw bytes used for the key in LSM1.
  678. */
  679. static void lsm1KeyFromValue(
  680. int keyType, /* The key type */
  681. sqlite3_value *pValue, /* The key value */
  682. u8 *pBuf, /* Storage space for a generated key */
  683. const u8 **ppKey, /* OUT: the bytes of the key */
  684. int *pnKey /* OUT: size of the key */
  685. ){
  686. if( keyType==SQLITE_BLOB ){
  687. *ppKey = (const u8*)sqlite3_value_blob(pValue);
  688. *pnKey = sqlite3_value_bytes(pValue);
  689. }else if( keyType==SQLITE_TEXT ){
  690. *ppKey = (const u8*)sqlite3_value_text(pValue);
  691. *pnKey = sqlite3_value_bytes(pValue);
  692. }else{
  693. sqlite3_int64 v = sqlite3_value_int64(pValue);
  694. if( v<0 ) v = 0;
  695. *pnKey = lsm1PutVarint64(pBuf, v);
  696. *ppKey = pBuf;
  697. }
  698. }
  699. /* Move to the first row to return.
  700. */
  701. static int lsm1Filter(
  702. sqlite3_vtab_cursor *pVtabCursor,
  703. int idxNum, const char *idxStr,
  704. int argc, sqlite3_value **argv
  705. ){
  706. lsm1_cursor *pCur = (lsm1_cursor *)pVtabCursor;
  707. lsm1_vtab *pTab = (lsm1_vtab*)(pCur->base.pVtab);
  708. int rc = LSM_OK;
  709. int seekType = -1;
  710. const u8 *pVal = 0;
  711. int nVal;
  712. u8 keyType = pTab->keyType;
  713. u8 aKey1[16];
  714. pCur->atEof = 1;
  715. sqlite3_free(pCur->pKey2);
  716. pCur->pKey2 = 0;
  717. if( idxNum<99 ){
  718. lsm1KeyFromValue(keyType, argv[0], aKey1, &pVal, &nVal);
  719. }
  720. switch( idxNum ){
  721. case 0: { /* key==argv[0] */
  722. assert( argc==1 );
  723. seekType = LSM_SEEK_EQ;
  724. pCur->isDesc = 0;
  725. pCur->bUnique = 1;
  726. break;
  727. }
  728. case 1: { /* key>=argv[0] AND key<=argv[1] */
  729. u8 aKey[12];
  730. seekType = LSM_SEEK_GE;
  731. pCur->isDesc = 0;
  732. pCur->bUnique = 0;
  733. if( keyType==SQLITE_INTEGER ){
  734. sqlite3_int64 v = sqlite3_value_int64(argv[1]);
  735. if( v<0 ) v = 0;
  736. pCur->nKey2 = lsm1PutVarint64(aKey, (sqlite3_uint64)v);
  737. pCur->pKey2 = sqlite3_malloc( pCur->nKey2 );
  738. if( pCur->pKey2==0 ) return SQLITE_NOMEM;
  739. memcpy(pCur->pKey2, aKey, pCur->nKey2);
  740. }else{
  741. pCur->nKey2 = sqlite3_value_bytes(argv[1]);
  742. pCur->pKey2 = sqlite3_malloc( pCur->nKey2 );
  743. if( pCur->pKey2==0 ) return SQLITE_NOMEM;
  744. if( keyType==SQLITE_BLOB ){
  745. memcpy(pCur->pKey2, sqlite3_value_blob(argv[1]), pCur->nKey2);
  746. }else{
  747. memcpy(pCur->pKey2, sqlite3_value_text(argv[1]), pCur->nKey2);
  748. }
  749. }
  750. break;
  751. }
  752. case 2: { /* key>=argv[0] */
  753. seekType = LSM_SEEK_GE;
  754. pCur->isDesc = 0;
  755. pCur->bUnique = 0;
  756. break;
  757. }
  758. case 3: { /* key<=argv[0] */
  759. seekType = LSM_SEEK_LE;
  760. pCur->isDesc = 1;
  761. pCur->bUnique = 0;
  762. break;
  763. }
  764. default: { /* full table scan */
  765. pCur->isDesc = 0;
  766. pCur->bUnique = 0;
  767. break;
  768. }
  769. }
  770. if( pVal ){
  771. rc = lsm_csr_seek(pCur->pLsmCur, pVal, nVal, seekType);
  772. }else{
  773. rc = lsm_csr_first(pCur->pLsmCur);
  774. }
  775. if( rc==LSM_OK && lsm_csr_valid(pCur->pLsmCur)!=0 ){
  776. pCur->atEof = 0;
  777. }
  778. return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
  779. }
  780. /*
  781. ** Only comparisons against the key are allowed. The idxNum defines
  782. ** which comparisons are available:
  783. **
  784. ** 0 key==?1
  785. ** 1 key>=?1 AND key<=?2
  786. ** 2 key>?1 or key>=?1
  787. ** 3 key<?1 or key<=?1
  788. ** 99 Full table scan only
  789. */
  790. static int lsm1BestIndex(
  791. sqlite3_vtab *tab,
  792. sqlite3_index_info *pIdxInfo
  793. ){
  794. int i; /* Loop over constraints */
  795. int idxNum = 99; /* The query plan */
  796. int nArg = 0; /* Number of arguments to xFilter */
  797. int argIdx = -1; /* Index of the key== constraint, or -1 if none */
  798. int iIdx2 = -1; /* The index of the second key */
  799. int omit1 = 0;
  800. int omit2 = 0;
  801. const struct sqlite3_index_constraint *pConstraint;
  802. pConstraint = pIdxInfo->aConstraint;
  803. for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  804. if( pConstraint->usable==0 ) continue;
  805. if( pConstraint->iColumn!=0 ) continue;
  806. switch( pConstraint->op ){
  807. case SQLITE_INDEX_CONSTRAINT_EQ: {
  808. if( idxNum>0 ){
  809. argIdx = i;
  810. iIdx2 = -1;
  811. idxNum = 0;
  812. omit1 = 1;
  813. }
  814. break;
  815. }
  816. case SQLITE_INDEX_CONSTRAINT_GE:
  817. case SQLITE_INDEX_CONSTRAINT_GT: {
  818. if( idxNum==99 ){
  819. argIdx = i;
  820. idxNum = 2;
  821. omit1 = pConstraint->op==SQLITE_INDEX_CONSTRAINT_GE;
  822. }else if( idxNum==3 ){
  823. iIdx2 = idxNum;
  824. omit2 = omit1;
  825. argIdx = i;
  826. idxNum = 1;
  827. omit1 = pConstraint->op==SQLITE_INDEX_CONSTRAINT_GE;
  828. }
  829. break;
  830. }
  831. case SQLITE_INDEX_CONSTRAINT_LE:
  832. case SQLITE_INDEX_CONSTRAINT_LT: {
  833. if( idxNum==99 ){
  834. argIdx = i;
  835. idxNum = 3;
  836. omit1 = pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE;
  837. }else if( idxNum==2 ){
  838. iIdx2 = i;
  839. idxNum = 1;
  840. omit1 = pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE;
  841. }
  842. break;
  843. }
  844. }
  845. }
  846. if( argIdx>=0 ){
  847. pIdxInfo->aConstraintUsage[argIdx].argvIndex = ++nArg;
  848. pIdxInfo->aConstraintUsage[argIdx].omit = omit1;
  849. }
  850. if( iIdx2>=0 ){
  851. pIdxInfo->aConstraintUsage[iIdx2].argvIndex = ++nArg;
  852. pIdxInfo->aConstraintUsage[iIdx2].omit = omit2;
  853. }
  854. if( idxNum==0 ){
  855. pIdxInfo->estimatedCost = (double)1;
  856. pIdxInfo->estimatedRows = 1;
  857. pIdxInfo->orderByConsumed = 1;
  858. }else if( idxNum==1 ){
  859. pIdxInfo->estimatedCost = (double)100;
  860. pIdxInfo->estimatedRows = 100;
  861. }else if( idxNum<99 ){
  862. pIdxInfo->estimatedCost = (double)5000;
  863. pIdxInfo->estimatedRows = 5000;
  864. }else{
  865. /* Full table scan */
  866. pIdxInfo->estimatedCost = (double)2147483647;
  867. pIdxInfo->estimatedRows = 2147483647;
  868. }
  869. pIdxInfo->idxNum = idxNum;
  870. return SQLITE_OK;
  871. }
  872. /*
  873. ** The xUpdate method is normally used for INSERT, REPLACE, UPDATE, and
  874. ** DELETE. But this virtual table only supports INSERT and REPLACE.
  875. ** DELETE is accomplished by inserting a record with a value of NULL.
  876. ** UPDATE is achieved by using REPLACE.
  877. */
  878. int lsm1Update(
  879. sqlite3_vtab *pVTab,
  880. int argc,
  881. sqlite3_value **argv,
  882. sqlite_int64 *pRowid
  883. ){
  884. lsm1_vtab *p = (lsm1_vtab*)pVTab;
  885. int nKey, nKey2;
  886. int i;
  887. int rc = LSM_OK;
  888. const u8 *pKey, *pKey2;
  889. unsigned char aKey[16];
  890. unsigned char pSpace[16];
  891. lsm1_vblob val;
  892. if( argc==1 ){
  893. /* DELETE the record whose key is argv[0] */
  894. lsm1KeyFromValue(p->keyType, argv[0], aKey, &pKey, &nKey);
  895. lsm_delete(p->pDb, pKey, nKey);
  896. return SQLITE_OK;
  897. }
  898. if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
  899. /* An UPDATE */
  900. lsm1KeyFromValue(p->keyType, argv[0], aKey, &pKey, &nKey);
  901. lsm1KeyFromValue(p->keyType, argv[1], pSpace, &pKey2, &nKey2);
  902. if( nKey!=nKey2 || memcmp(pKey, pKey2, nKey)!=0 ){
  903. /* The UPDATE changes the PRIMARY KEY value. DELETE the old key */
  904. lsm_delete(p->pDb, pKey, nKey);
  905. }
  906. /* Fall through into the INSERT case to complete the UPDATE */
  907. }
  908. /* "INSERT INTO tab(lsm1_command) VALUES('....')" is used to implement
  909. ** special commands.
  910. */
  911. if( sqlite3_value_type(argv[3+p->nVal])!=SQLITE_NULL ){
  912. return SQLITE_OK;
  913. }
  914. lsm1KeyFromValue(p->keyType, argv[2], aKey, &pKey, &nKey);
  915. memset(&val, 0, sizeof(val));
  916. for(i=0; i<p->nVal; i++){
  917. sqlite3_value *pArg = argv[3+i];
  918. u8 eType = sqlite3_value_type(pArg);
  919. switch( eType ){
  920. case SQLITE_NULL: {
  921. lsm1VblobAppendVarint(&val, SQLITE_NULL);
  922. break;
  923. }
  924. case SQLITE_INTEGER: {
  925. sqlite3_int64 v = sqlite3_value_int64(pArg);
  926. if( v>=0 && v<=240/6 ){
  927. lsm1VblobAppendVarint(&val, v*6);
  928. }else{
  929. int n = lsm1PutSignedVarint64(pSpace, v);
  930. lsm1VblobAppendVarint(&val, SQLITE_INTEGER + n*6);
  931. lsm1VblobAppend(&val, pSpace, n);
  932. }
  933. break;
  934. }
  935. case SQLITE_FLOAT: {
  936. double r = sqlite3_value_double(pArg);
  937. lsm1VblobAppendVarint(&val, SQLITE_FLOAT + 8*6);
  938. lsm1VblobAppend(&val, (u8*)&r, sizeof(r));
  939. break;
  940. }
  941. case SQLITE_BLOB: {
  942. int n = sqlite3_value_bytes(pArg);
  943. lsm1VblobAppendVarint(&val, n*6 + SQLITE_BLOB);
  944. lsm1VblobAppend(&val, sqlite3_value_blob(pArg), n);
  945. break;
  946. }
  947. case SQLITE_TEXT: {
  948. int n = sqlite3_value_bytes(pArg);
  949. lsm1VblobAppendVarint(&val, n*6 + SQLITE_TEXT);
  950. lsm1VblobAppend(&val, sqlite3_value_text(pArg), n);
  951. break;
  952. }
  953. }
  954. }
  955. if( val.errNoMem ){
  956. return SQLITE_NOMEM;
  957. }
  958. rc = lsm_insert(p->pDb, pKey, nKey, val.a, val.n);
  959. sqlite3_free(val.a);
  960. return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
  961. }
  962. /* Begin a transaction
  963. */
  964. static int lsm1Begin(sqlite3_vtab *pVtab){
  965. lsm1_vtab *p = (lsm1_vtab*)pVtab;
  966. int rc = lsm_begin(p->pDb, 1);
  967. return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
  968. }
  969. /* Phase 1 of a transaction commit.
  970. */
  971. static int lsm1Sync(sqlite3_vtab *pVtab){
  972. return SQLITE_OK;
  973. }
  974. /* Commit a transaction
  975. */
  976. static int lsm1Commit(sqlite3_vtab *pVtab){
  977. lsm1_vtab *p = (lsm1_vtab*)pVtab;
  978. int rc = lsm_commit(p->pDb, 0);
  979. return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
  980. }
  981. /* Rollback a transaction
  982. */
  983. static int lsm1Rollback(sqlite3_vtab *pVtab){
  984. lsm1_vtab *p = (lsm1_vtab*)pVtab;
  985. int rc = lsm_rollback(p->pDb, 0);
  986. return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
  987. }
  988. /*
  989. ** This following structure defines all the methods for the
  990. ** generate_lsm1 virtual table.
  991. */
  992. static sqlite3_module lsm1Module = {
  993. 0, /* iVersion */
  994. lsm1Connect, /* xCreate */
  995. lsm1Connect, /* xConnect */
  996. lsm1BestIndex, /* xBestIndex */
  997. lsm1Disconnect, /* xDisconnect */
  998. lsm1Disconnect, /* xDestroy */
  999. lsm1Open, /* xOpen - open a cursor */
  1000. lsm1Close, /* xClose - close a cursor */
  1001. lsm1Filter, /* xFilter - configure scan constraints */
  1002. lsm1Next, /* xNext - advance a cursor */
  1003. lsm1Eof, /* xEof - check for end of scan */
  1004. lsm1Column, /* xColumn - read data */
  1005. lsm1Rowid, /* xRowid - read data */
  1006. lsm1Update, /* xUpdate */
  1007. lsm1Begin, /* xBegin */
  1008. lsm1Sync, /* xSync */
  1009. lsm1Commit, /* xCommit */
  1010. lsm1Rollback, /* xRollback */
  1011. 0, /* xFindMethod */
  1012. 0, /* xRename */
  1013. 0, /* xSavepoint */
  1014. 0, /* xRelease */
  1015. 0, /* xRollbackTo */
  1016. 0, /* xShadowName */
  1017. 0 /* xIntegrity */
  1018. };
  1019. #ifdef _WIN32
  1020. __declspec(dllexport)
  1021. #endif
  1022. int sqlite3_lsm_init(
  1023. sqlite3 *db,
  1024. char **pzErrMsg,
  1025. const sqlite3_api_routines *pApi
  1026. ){
  1027. int rc = SQLITE_OK;
  1028. SQLITE_EXTENSION_INIT2(pApi);
  1029. rc = sqlite3_create_module(db, "lsm1", &lsm1Module, 0);
  1030. return rc;
  1031. }