Add support for (variable length) integer keys in LSM1.

FossilOrigin-Name: 32f3daec0a8084b258b2513c8025bc4d95a5d757
This commit is contained in:
drh 2016-02-23 01:37:24 +00:00
parent 7dc1918bbf
commit 078b99f806
3 changed files with 166 additions and 11 deletions

View File

@ -199,6 +199,135 @@ static int lsm1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
#define LSM1_TYPE_TEXT 2
#define LSM1_TYPE_BLOB 3
/*
** Write a 32-bit unsigned integer as 4 big-endian bytes.
*/
static void varintWrite32(unsigned char *z, unsigned int y){
z[0] = (unsigned char)(y>>24);
z[1] = (unsigned char)(y>>16);
z[2] = (unsigned char)(y>>8);
z[3] = (unsigned char)(y);
}
/*
** Write a varint into z[]. The buffer z[] must be at least 9 characters
** long to accommodate the largest possible varint. Return the number of
** bytes of z[] used.
*/
static int lsm1PutVarint64(unsigned char *z, sqlite3_uint64 x){
unsigned int w, y;
if( x<=240 ){
z[0] = (unsigned char)x;
return 1;
}
if( x<=2287 ){
y = (unsigned int)(x - 240);
z[0] = (unsigned char)(y/256 + 241);
z[1] = (unsigned char)(y%256);
return 2;
}
if( x<=67823 ){
y = (unsigned int)(x - 2288);
z[0] = 249;
z[1] = (unsigned char)(y/256);
z[2] = (unsigned char)(y%256);
return 3;
}
y = (unsigned int)x;
w = (unsigned int)(x>>32);
if( w==0 ){
if( y<=16777215 ){
z[0] = 250;
z[1] = (unsigned char)(y>>16);
z[2] = (unsigned char)(y>>8);
z[3] = (unsigned char)(y);
return 4;
}
z[0] = 251;
varintWrite32(z+1, y);
return 5;
}
if( w<=255 ){
z[0] = 252;
z[1] = (unsigned char)w;
varintWrite32(z+2, y);
return 6;
}
if( w<=65535 ){
z[0] = 253;
z[1] = (unsigned char)(w>>8);
z[2] = (unsigned char)w;
varintWrite32(z+3, y);
return 7;
}
if( w<=16777215 ){
z[0] = 254;
z[1] = (unsigned char)(w>>16);
z[2] = (unsigned char)(w>>8);
z[3] = (unsigned char)w;
varintWrite32(z+4, y);
return 8;
}
z[0] = 255;
varintWrite32(z+1, w);
varintWrite32(z+5, y);
return 9;
}
/*
** Decode the varint in the first n bytes z[]. Write the integer value
** into *pResult and return the number of bytes in the varint.
**
** If the decode fails because there are not enough bytes in z[] then
** return 0;
*/
static int lsm1GetVarint64(
const unsigned char *z,
int n,
sqlite3_uint64 *pResult
){
unsigned int x;
if( n<1 ) return 0;
if( z[0]<=240 ){
*pResult = z[0];
return 1;
}
if( z[0]<=248 ){
if( n<2 ) return 0;
*pResult = (z[0]-241)*256 + z[1] + 240;
return 2;
}
if( n<z[0]-246 ) return 0;
if( z[0]==249 ){
*pResult = 2288 + 256*z[1] + z[2];
return 3;
}
if( z[0]==250 ){
*pResult = (z[1]<<16) + (z[2]<<8) + z[3];
return 4;
}
x = (z[1]<<24) + (z[2]<<16) + (z[3]<<8) + z[4];
if( z[0]==251 ){
*pResult = x;
return 5;
}
if( z[0]==252 ){
*pResult = (((sqlite3_uint64)x)<<8) + z[5];
return 6;
}
if( z[0]==253 ){
*pResult = (((sqlite3_uint64)x)<<16) + (z[5]<<8) + z[6];
return 7;
}
if( z[0]==254 ){
*pResult = (((sqlite3_uint64)x)<<24) + (z[5]<<16) + (z[6]<<8) + z[7];
return 8;
}
*pResult = (((sqlite3_uint64)x)<<32) +
(0xffffffff & ((z[5]<<24) + (z[6]<<16) + (z[7]<<8) + z[8]));
return 9;
}
/*
** Generate a key encoding for pValue such that all keys compare in
** lexicographical order. Return an SQLite error code or SQLITE_OK.
@ -218,6 +347,7 @@ static int lsm1EncodeKey(
int eType = sqlite3_value_type(pValue);
*ppKey = 0;
*pnKey = 0;
assert( nSpace>=32 );
switch( eType ){
default: {
return SQLITE_ERROR; /* We cannot handle NULL keys */
@ -244,6 +374,21 @@ static int lsm1EncodeKey(
*pnKey = nVal+1;
break;
}
case SQLITE_INTEGER: {
sqlite3_int64 iVal = sqlite3_value_int64(pValue);
sqlite3_uint64 uVal;
if( iVal<0 ){
if( iVal==0xffffffffffffffffLL ) return SQLITE_ERROR;
uVal = -iVal;
eType = LSM1_TYPE_NEGATIVE;
}else{
uVal = iVal;
eType = LSM1_TYPE_POSITIVE;
}
pSpace[0] = eType;
*ppKey = pSpace;
*pnKey = 1 + lsm1PutVarint64(&pSpace[1], uVal);
}
}
return SQLITE_OK;
}
@ -279,7 +424,17 @@ static int lsm1Column(
}else if( pVal[0]==LSM1_TYPE_TEXT ){
sqlite3_result_text(ctx, (const char*)&pVal[1],nVal-1,
SQLITE_TRANSIENT);
}
}else if( nVal>=2 && nVal<=9 &&
(pVal[0]==LSM1_TYPE_POSITIVE || pVal[0]==LSM1_TYPE_NEGATIVE)
){
sqlite3_uint64 uVal = 0;
lsm1GetVarint64(pVal+1, nVal-1, &uVal);
if( pVal[0]==LSM1_TYPE_NEGATIVE ){
sqlite3_result_int64(ctx, -(sqlite3_int64)uVal);
}else{
sqlite3_result_int64(ctx, (sqlite3_int64)uVal);
}
}
}
break;
}

View File

@ -1,5 +1,5 @@
C Merge\sup\sto\strunk.
D 2016-02-22T13:01:22.781
C Add\ssupport\sfor\s(variable\slength)\sinteger\skeys\sin\sLSM1.
D 2016-02-23T01:37:24.930
F Makefile.in 4e90dc1521879022aa9479268a4cd141d1771142
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc 28fc4ee02333996d31b3602b39eeb8e609a89ce4
@ -13,7 +13,7 @@ F autoconf/INSTALL 83e4a25da9fd053c7b3665eaaaf7919707915903
F autoconf/Makefile.am 29e2a6e8d0c5e32723a48b4faf6b168854dde5f4
F autoconf/Makefile.msc d5ab32cf30d6ba8b5ce20835b4042f6cc2a4cb39
F autoconf/README.first 6c4f34fe115ff55d4e8dbfa3cecf04a0188292f7
F autoconf/README.txt 7c31da66232f7590bb987cfcd4e2381744b25d24 w autoconf/README
F autoconf/README.txt 7c31da66232f7590bb987cfcd4e2381744b25d24
F autoconf/configure.ac 72a5e42beb090b32bca580285dc0ab3c4670adb8
F autoconf/tea/Makefile.in b438a7020446c8a8156e8d97c8914a04833da6fd
F autoconf/tea/README 3e9a3c060f29a44344ab50aec506f4db903fb873
@ -215,7 +215,7 @@ F ext/lsm1/lsm_str.c 77ebdd5040ddf267a6f724d4c83132d2dce8a226
F ext/lsm1/lsm_tree.c 5d9fb2bc58a1a70c75126bd8d7198f7b627e165b
F ext/lsm1/lsm_unix.c fcaf5b6738713f1229dc0e1a90393ecf24f787f2
F ext/lsm1/lsm_varint.c b19ae9bd26b5a1e8402fb8a564b25d9542338a41
F ext/lsm1/lsm_vtab.c 37be4d5928de455f752fdd8108ea1e6cd06ce6e0
F ext/lsm1/lsm_vtab.c f7fb6c185ebd5dcf70f676886e28799afa72424e
F ext/misc/amatch.c a1a8f66c29d40bd71b075546ddeddb477b17a2bb
F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704
F ext/misc/compress.c 122faa92d25033d6c3f07c39231de074ab3d2e83
@ -998,7 +998,7 @@ F test/savepoint4.test c8f8159ade6d2acd9128be61e1230f1c1edc6cc0
F test/savepoint5.test 0735db177e0ebbaedc39812c8d065075d563c4fd
F test/savepoint6.test f41279c5e137139fa5c21485773332c7adb98cd7
F test/savepoint7.test db3db281486c925095f305aad09fe806e5188ff3
F test/savepointfault.test f044eac64b59f09746c7020ee261734de82bf9b2 w test/savepoint3.test
F test/savepointfault.test f044eac64b59f09746c7020ee261734de82bf9b2
F test/scanstatus.test 5253c219e331318a437f436268e0e82345700285
F test/schema.test 8f7999be894260f151adf15c2c7540f1c6d6a481
F test/schema2.test 906408621ea881fdb496d878b1822572a34e32c5
@ -1363,7 +1363,7 @@ F test/whereH.test e4b07f7a3c2f5d31195cd33710054c78667573b2
F test/whereI.test eab5b226bbc344ac70d7dc09b963a064860ae6d7
F test/whereJ.test 55a3221706a7ab706293f17cc8f96da563bf0767
F test/whereK.test f8e3cf26a8513ecc7f514f54df9f0572c046c42b
F test/wherefault.test 1374c3aa198388925246475f84ad4cd5f9528864 w test/where8m.test
F test/wherefault.test 1374c3aa198388925246475f84ad4cd5f9528864
F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31
F test/wild001.test bca33f499866f04c24510d74baf1e578d4e44b1c
F test/win32heap.test ea19770974795cff26e11575e12d422dbd16893c
@ -1445,7 +1445,7 @@ F tool/vdbe_profile.tcl 246d0da094856d72d2c12efec03250d71639d19f
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh a98af506df552f3b3c0d904f94e4cdc4e1a6d598
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 8aede091c4740511d11ea14da253fe39bbfe75a6 a48ac4c347813bd2b416b1cb06c3cbf1f4b3781a
R 580b69c1534293700c3568d992e6ddd2
P f9e5fb88a5a14b26e9ea6997f449d0913f27a494
R cb91bcf5f852d7d369f68158e9ec3ba2
U drh
Z e274944a16c507edac9643474a14a944
Z 5c76f2e4fcca8d24f17a7236982c41a2

View File

@ -1 +1 @@
f9e5fb88a5a14b26e9ea6997f449d0913f27a494
32f3daec0a8084b258b2513c8025bc4d95a5d757