1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
|
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
res = 0;
}
return res;
}
}
/*
** Values that may be used as the first parameter to fts3DoclistMerge().
*/
#define MERGE_NOT 2 /* D + D -> D */
#define MERGE_AND 3 /* D + D -> D */
#define MERGE_OR 4 /* D + D -> D */
#define MERGE_PHRASE 6 /* P + P -> D */
#define MERGE_NEAR 8 /* P + P -> D */
#define MERGE_POS_OR 5 /* P + P -> P */
#define MERGE_POS_PHRASE 7 /* P + P -> P */
#define MERGE_POS_NEAR 9 /* P + P -> P */
/*
** Merge the two doclists passed in buffer a1 (size n1 bytes) and a2
** (size n2 bytes). The output is written to pre-allocated buffer aBuffer,
** which is guaranteed to be large enough to hold the results. The number
** of bytes written to aBuffer is stored in *pnBuffer before returning.
**
** If successful, SQLITE_OK is returned. Otherwise, if a malloc error
** occurs while allocating a temporary buffer as part of the merge operation,
** SQLITE_NOMEM is returned.
*/
static int fts3DoclistMerge(
int mergetype, /* One of the MERGE_XXX constants */
int nParam1, /* Used by MERGE_NEAR and MERGE_POS_NEAR */
int nParam2, /* Used by MERGE_NEAR and MERGE_POS_NEAR */
char *aBuffer, /* Pre-allocated output buffer */
int *pnBuffer, /* OUT: Bytes written to aBuffer */
char *a1, /* Buffer containing first doclist */
int n1, /* Size of buffer a1 */
char *a2, /* Buffer containing second doclist */
int n2, /* Size of buffer a2 */
int *pnDoc /* OUT: Number of docids in output */
){
sqlite3_int64 i1 = 0;
sqlite3_int64 i2 = 0;
sqlite3_int64 iPrev = 0;
char *p = aBuffer;
char *p1 = a1;
char *p2 = a2;
char *pEnd1 = &a1[n1];
char *pEnd2 = &a2[n2];
int nDoc = 0;
assert( mergetype==MERGE_OR || mergetype==MERGE_POS_OR
|| mergetype==MERGE_AND || mergetype==MERGE_NOT
|| mergetype==MERGE_PHRASE || mergetype==MERGE_POS_PHRASE
|| mergetype==MERGE_NEAR || mergetype==MERGE_POS_NEAR
);
assert( mergetype==MERGE_POS_PHRASE || mergetype==MERGE_POS_NEAR );
if( !aBuffer ){
*pnBuffer = 0;
return SQLITE_NOMEM;
}
/* Read the first docid from each doclist */
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
switch( mergetype ){
case MERGE_OR:
case MERGE_POS_OR:
while( p1 || p2 ){
if( p2 && p1 && i1==i2 ){
fts3PutDeltaVarint(&p, &iPrev, i1);
if( mergetype==MERGE_POS_OR ) fts3PoslistMerge(&p, &p1, &p2);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( !p2 || (p1 && i1<i2) ){
fts3PutDeltaVarint(&p, &iPrev, i1);
if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3PutDeltaVarint(&p, &iPrev, i2);
if( mergetype==MERGE_POS_OR ) fts3PoslistCopy(&p, &p2);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
case MERGE_AND:
while( p1 && p2 ){
if( i1==i2 ){
fts3PutDeltaVarint(&p, &iPrev, i1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
nDoc++;
}else if( i1<i2 ){
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
case MERGE_NOT:
while( p1 ){
if( p2 && i1==i2 ){
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( !p2 || i1<i2 ){
fts3PutDeltaVarint(&p, &iPrev, i1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
case MERGE_POS_PHRASE:
case MERGE_PHRASE: {
char **ppPos = (mergetype==MERGE_PHRASE ? 0 : &p);
while( p1 && p2 ){
if( i1==i2 ){
char *pSave = p;
sqlite3_int64 iPrevSave = iPrev;
fts3PutDeltaVarint(&p, &iPrev, i1);
if( 0==fts3PoslistPhraseMerge(ppPos, nParam1, 0, 1, &p1, &p2) ){
p = pSave;
iPrev = iPrevSave;
}else{
nDoc++;
}
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( i1<i2 ){
fts3PoslistCopy(0, &p1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3PoslistCopy(0, &p2);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
break;
}
default: assert( mergetype==MERGE_POS_NEAR || mergetype==MERGE_NEAR ); {
char *aTmp = 0;
char **ppPos = 0;
if( mergetype==MERGE_POS_NEAR ){
ppPos = &p;
aTmp = sqlite3_malloc(2*(n1+n2+1));
if( !aTmp ){
return SQLITE_NOMEM;
}
}
while( p1 && p2 ){
if( i1==i2 ){
char *pSave = p;
sqlite3_int64 iPrevSave = iPrev;
fts3PutDeltaVarint(&p, &iPrev, i1);
if( !fts3PoslistNearMerge(ppPos, aTmp, nParam1, nParam2, &p1, &p2) ){
iPrev = iPrevSave;
p = pSave;
}
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}else if( i1<i2 ){
fts3PoslistCopy(0, &p1);
fts3GetDeltaVarint2(&p1, pEnd1, &i1);
}else{
fts3PoslistCopy(0, &p2);
fts3GetDeltaVarint2(&p2, pEnd2, &i2);
}
}
sqlite3_free(aTmp);
break;
}
}
if( pnDoc ) *pnDoc = nDoc;
*pnBuffer = (int)(p-aBuffer);
return SQLITE_OK;
}
/*
** A pointer to an instance of this structure is used as the context
** argument to sqlite3Fts3SegReaderIterate()
*/
typedef struct TermSelect TermSelect;
struct TermSelect {
int isReqPos;
|
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
|
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
|
-
+
|
sqlite3_free(pRight->doclist.aAll);
pRight->doclist.aAll = 0;
pRight->doclist.nAll = 0;
}else if( pRight->doclist.aAll ){
char *aOut; /* Buffer in which to assemble new doclist */
int nOut; /* Size of buffer aOut in bytes */
*pRc = fts3DoclistNearMerge(bDescIdx, MERGE_POS_NEAR, nNear,
*pRc = fts3DoclistNearMerge(bDescIdx, nNear,
pLeft->nToken, pLeft->doclist.aAll, pLeft->doclist.nAll,
pRight->nToken, pRight->doclist.aAll, pRight->doclist.nAll,
&aOut, &nOut
);
sqlite3_free(pRight->doclist.aAll);
pRight->doclist.aAll = aOut;
pRight->doclist.nAll = nOut;
|