|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "fts5Int.h" |
|
|
|
|
|
int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){ |
|
|
if( (u32)pBuf->nSpace<nByte ){ |
|
|
u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64; |
|
|
u8 *pNew; |
|
|
while( nNew<nByte ){ |
|
|
nNew = nNew * 2; |
|
|
} |
|
|
pNew = sqlite3_realloc64(pBuf->p, nNew); |
|
|
if( pNew==0 ){ |
|
|
*pRc = SQLITE_NOMEM; |
|
|
return 1; |
|
|
}else{ |
|
|
pBuf->nSpace = (int)nNew; |
|
|
pBuf->p = pNew; |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){ |
|
|
if( fts5BufferGrow(pRc, pBuf, 9) ) return; |
|
|
pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal); |
|
|
} |
|
|
|
|
|
void sqlite3Fts5Put32(u8 *aBuf, int iVal){ |
|
|
aBuf[0] = (iVal>>24) & 0x00FF; |
|
|
aBuf[1] = (iVal>>16) & 0x00FF; |
|
|
aBuf[2] = (iVal>> 8) & 0x00FF; |
|
|
aBuf[3] = (iVal>> 0) & 0x00FF; |
|
|
} |
|
|
|
|
|
int sqlite3Fts5Get32(const u8 *aBuf){ |
|
|
return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferAppendBlob( |
|
|
int *pRc, |
|
|
Fts5Buffer *pBuf, |
|
|
u32 nData, |
|
|
const u8 *pData |
|
|
){ |
|
|
if( nData ){ |
|
|
if( fts5BufferGrow(pRc, pBuf, nData) ) return; |
|
|
assert( pBuf->p!=0 ); |
|
|
memcpy(&pBuf->p[pBuf->n], pData, nData); |
|
|
pBuf->n += nData; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferAppendString( |
|
|
int *pRc, |
|
|
Fts5Buffer *pBuf, |
|
|
const char *zStr |
|
|
){ |
|
|
int nStr = (int)strlen(zStr); |
|
|
sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr); |
|
|
pBuf->n--; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferAppendPrintf( |
|
|
int *pRc, |
|
|
Fts5Buffer *pBuf, |
|
|
char *zFmt, ... |
|
|
){ |
|
|
if( *pRc==SQLITE_OK ){ |
|
|
char *zTmp; |
|
|
va_list ap; |
|
|
va_start(ap, zFmt); |
|
|
zTmp = sqlite3_vmprintf(zFmt, ap); |
|
|
va_end(ap); |
|
|
|
|
|
if( zTmp==0 ){ |
|
|
*pRc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp); |
|
|
sqlite3_free(zTmp); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){ |
|
|
char *zRet = 0; |
|
|
if( *pRc==SQLITE_OK ){ |
|
|
va_list ap; |
|
|
va_start(ap, zFmt); |
|
|
zRet = sqlite3_vmprintf(zFmt, ap); |
|
|
va_end(ap); |
|
|
if( zRet==0 ){ |
|
|
*pRc = SQLITE_NOMEM; |
|
|
} |
|
|
} |
|
|
return zRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){ |
|
|
sqlite3_free(pBuf->p); |
|
|
memset(pBuf, 0, sizeof(Fts5Buffer)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){ |
|
|
pBuf->n = 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5BufferSet( |
|
|
int *pRc, |
|
|
Fts5Buffer *pBuf, |
|
|
int nData, |
|
|
const u8 *pData |
|
|
){ |
|
|
pBuf->n = 0; |
|
|
sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData); |
|
|
} |
|
|
|
|
|
int sqlite3Fts5PoslistNext64( |
|
|
const u8 *a, int n, |
|
|
int *pi, |
|
|
i64 *piOff |
|
|
){ |
|
|
int i = *pi; |
|
|
assert( a!=0 || i==0 ); |
|
|
if( i>=n ){ |
|
|
|
|
|
*piOff = -1; |
|
|
return 1; |
|
|
}else{ |
|
|
i64 iOff = *piOff; |
|
|
u32 iVal; |
|
|
assert( a!=0 ); |
|
|
fts5FastGetVarint32(a, i, iVal); |
|
|
if( iVal<=1 ){ |
|
|
if( iVal==0 ){ |
|
|
*pi = i; |
|
|
return 0; |
|
|
} |
|
|
fts5FastGetVarint32(a, i, iVal); |
|
|
iOff = ((i64)iVal) << 32; |
|
|
assert( iOff>=0 ); |
|
|
fts5FastGetVarint32(a, i, iVal); |
|
|
if( iVal<2 ){ |
|
|
|
|
|
*piOff = -1; |
|
|
return 1; |
|
|
} |
|
|
*piOff = iOff + ((iVal-2) & 0x7FFFFFFF); |
|
|
}else{ |
|
|
*piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF); |
|
|
} |
|
|
*pi = i; |
|
|
assert_nc( *piOff>=iOff ); |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){ |
|
|
if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){ |
|
|
pIter->bEof = 1; |
|
|
} |
|
|
return pIter->bEof; |
|
|
} |
|
|
|
|
|
int sqlite3Fts5PoslistReaderInit( |
|
|
const u8 *a, int n, |
|
|
Fts5PoslistReader *pIter |
|
|
){ |
|
|
memset(pIter, 0, sizeof(*pIter)); |
|
|
pIter->a = a; |
|
|
pIter->n = n; |
|
|
sqlite3Fts5PoslistReaderNext(pIter); |
|
|
return pIter->bEof; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5PoslistSafeAppend( |
|
|
Fts5Buffer *pBuf, |
|
|
i64 *piPrev, |
|
|
i64 iPos |
|
|
){ |
|
|
if( iPos>=*piPrev ){ |
|
|
static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32; |
|
|
if( (iPos & colmask) != (*piPrev & colmask) ){ |
|
|
pBuf->p[pBuf->n++] = 1; |
|
|
pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32)); |
|
|
*piPrev = (iPos & colmask); |
|
|
} |
|
|
pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2); |
|
|
*piPrev = iPos; |
|
|
} |
|
|
} |
|
|
|
|
|
int sqlite3Fts5PoslistWriterAppend( |
|
|
Fts5Buffer *pBuf, |
|
|
Fts5PoslistWriter *pWriter, |
|
|
i64 iPos |
|
|
){ |
|
|
int rc = 0; |
|
|
if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc; |
|
|
sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){ |
|
|
void *pRet = 0; |
|
|
if( *pRc==SQLITE_OK ){ |
|
|
pRet = sqlite3_malloc64(nByte); |
|
|
if( pRet==0 ){ |
|
|
if( nByte>0 ) *pRc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
memset(pRet, 0, (size_t)nByte); |
|
|
} |
|
|
} |
|
|
return pRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){ |
|
|
char *zRet = 0; |
|
|
if( *pRc==SQLITE_OK ){ |
|
|
if( nIn<0 ){ |
|
|
nIn = (int)strlen(pIn); |
|
|
} |
|
|
zRet = (char*)sqlite3_malloc(nIn+1); |
|
|
if( zRet ){ |
|
|
memcpy(zRet, pIn, nIn); |
|
|
zRet[nIn] = '\0'; |
|
|
}else{ |
|
|
*pRc = SQLITE_NOMEM; |
|
|
} |
|
|
} |
|
|
return zRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5IsBareword(char t){ |
|
|
u8 aBareword[128] = { |
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, |
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 |
|
|
}; |
|
|
|
|
|
return (t & 0x80) || aBareword[(int)t]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct Fts5TermsetEntry Fts5TermsetEntry; |
|
|
struct Fts5TermsetEntry { |
|
|
char *pTerm; |
|
|
int nTerm; |
|
|
int iIdx; |
|
|
Fts5TermsetEntry *pNext; |
|
|
}; |
|
|
|
|
|
struct Fts5Termset { |
|
|
Fts5TermsetEntry *apHash[512]; |
|
|
}; |
|
|
|
|
|
int sqlite3Fts5TermsetNew(Fts5Termset **pp){ |
|
|
int rc = SQLITE_OK; |
|
|
*pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset)); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
int sqlite3Fts5TermsetAdd( |
|
|
Fts5Termset *p, |
|
|
int iIdx, |
|
|
const char *pTerm, int nTerm, |
|
|
int *pbPresent |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
*pbPresent = 0; |
|
|
if( p ){ |
|
|
int i; |
|
|
u32 hash = 13; |
|
|
Fts5TermsetEntry *pEntry; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=nTerm-1; i>=0; i--){ |
|
|
hash = (hash << 3) ^ hash ^ pTerm[i]; |
|
|
} |
|
|
hash = (hash << 3) ^ hash ^ iIdx; |
|
|
hash = hash % ArraySize(p->apHash); |
|
|
|
|
|
for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){ |
|
|
if( pEntry->iIdx==iIdx |
|
|
&& pEntry->nTerm==nTerm |
|
|
&& memcmp(pEntry->pTerm, pTerm, nTerm)==0 |
|
|
){ |
|
|
*pbPresent = 1; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if( pEntry==0 ){ |
|
|
pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm); |
|
|
if( pEntry ){ |
|
|
pEntry->pTerm = (char*)&pEntry[1]; |
|
|
pEntry->nTerm = nTerm; |
|
|
pEntry->iIdx = iIdx; |
|
|
memcpy(pEntry->pTerm, pTerm, nTerm); |
|
|
pEntry->pNext = p->apHash[hash]; |
|
|
p->apHash[hash] = pEntry; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
void sqlite3Fts5TermsetFree(Fts5Termset *p){ |
|
|
if( p ){ |
|
|
u32 i; |
|
|
for(i=0; i<ArraySize(p->apHash); i++){ |
|
|
Fts5TermsetEntry *pEntry = p->apHash[i]; |
|
|
while( pEntry ){ |
|
|
Fts5TermsetEntry *pDel = pEntry; |
|
|
pEntry = pEntry->pNext; |
|
|
sqlite3_free(pDel); |
|
|
} |
|
|
} |
|
|
sqlite3_free(p); |
|
|
} |
|
|
} |
|
|
|