~ubuntu-branches/ubuntu/precise/sqlite3/precise-updates

« back to all changes in this revision

Viewing changes to ext/fts3/fts3Int.h

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2009-12-11 14:34:09 UTC
  • mfrom: (9.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091211143409-o29fahwmcmyd0vq1
Tags: 3.6.21-2
Run autoreconf to prevent FTBFS with new libtool (closes: #560660).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** 2009 Nov 12
 
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
*/
 
14
 
 
15
#ifndef _FTSINT_H
 
16
#define _FTSINT_H
 
17
 
 
18
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 
19
# define NDEBUG 1
 
20
#endif
 
21
 
 
22
#include "sqlite3.h"
 
23
#include "fts3_tokenizer.h"
 
24
#include "fts3_hash.h"
 
25
 
 
26
/*
 
27
** This constant controls how often segments are merged. Once there are
 
28
** FTS3_MERGE_COUNT segments of level N, they are merged into a single
 
29
** segment of level N+1.
 
30
*/
 
31
#define FTS3_MERGE_COUNT 16
 
32
 
 
33
/*
 
34
** This is the maximum amount of data (in bytes) to store in the 
 
35
** Fts3Table.pendingTerms hash table. Normally, the hash table is
 
36
** populated as documents are inserted/updated/deleted in a transaction
 
37
** and used to create a new segment when the transaction is committed.
 
38
** However if this limit is reached midway through a transaction, a new 
 
39
** segment is created and the hash table cleared immediately.
 
40
*/
 
41
#define FTS3_MAX_PENDING_DATA (1*1024*1024)
 
42
 
 
43
/*
 
44
** Macro to return the number of elements in an array. SQLite has a
 
45
** similar macro called ArraySize(). Use a different name to avoid
 
46
** a collision when building an amalgamation with built-in FTS3.
 
47
*/
 
48
#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
 
49
 
 
50
/*
 
51
** Maximum length of a varint encoded integer. The varint format is different
 
52
** from that used by SQLite, so the maximum length is 10, not 9.
 
53
*/
 
54
#define FTS3_VARINT_MAX 10
 
55
 
 
56
/*
 
57
** This section provides definitions to allow the
 
58
** FTS3 extension to be compiled outside of the 
 
59
** amalgamation.
 
60
*/
 
61
#ifndef SQLITE_AMALGAMATION
 
62
/*
 
63
** Macros indicating that conditional expressions are always true or
 
64
** false.
 
65
*/
 
66
# define ALWAYS(x) (x)
 
67
# define NEVER(X)  (x)
 
68
/*
 
69
** Internal types used by SQLite.
 
70
*/
 
71
typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
 
72
typedef short int i16;            /* 2-byte (or larger) signed integer */
 
73
/*
 
74
** Macro used to suppress compiler warnings for unused parameters.
 
75
*/
 
76
#define UNUSED_PARAMETER(x) (void)(x)
 
77
#endif
 
78
 
 
79
typedef struct Fts3Table Fts3Table;
 
80
typedef struct Fts3Cursor Fts3Cursor;
 
81
typedef struct Fts3Expr Fts3Expr;
 
82
typedef struct Fts3Phrase Fts3Phrase;
 
83
typedef struct Fts3SegReader Fts3SegReader;
 
84
typedef struct Fts3SegFilter Fts3SegFilter;
 
85
 
 
86
/*
 
87
** A connection to a fulltext index is an instance of the following
 
88
** structure. The xCreate and xConnect methods create an instance
 
89
** of this structure and xDestroy and xDisconnect free that instance.
 
90
** All other methods receive a pointer to the structure as one of their
 
91
** arguments.
 
92
*/
 
93
struct Fts3Table {
 
94
  sqlite3_vtab base;              /* Base class used by SQLite core */
 
95
  sqlite3 *db;                    /* The database connection */
 
96
  const char *zDb;                /* logical database name */
 
97
  const char *zName;              /* virtual table name */
 
98
  int nColumn;                    /* number of named columns in virtual table */
 
99
  char **azColumn;                /* column names.  malloced */
 
100
  sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
 
101
 
 
102
  /* Precompiled statements used by the implementation. Each of these 
 
103
  ** statements is run and reset within a single virtual table API call. 
 
104
  */
 
105
  sqlite3_stmt *aStmt[18];
 
106
 
 
107
  /* Pointer to string containing the SQL:
 
108
  **
 
109
  ** "SELECT block FROM %_segments WHERE blockid BETWEEN ? AND ? 
 
110
  **    ORDER BY blockid"
 
111
  */
 
112
  char *zSelectLeaves;
 
113
  int nLeavesStmt;                /* Valid statements in aLeavesStmt */
 
114
  int nLeavesTotal;               /* Total number of prepared leaves stmts */
 
115
  int nLeavesAlloc;               /* Allocated size of aLeavesStmt */
 
116
  sqlite3_stmt **aLeavesStmt;     /* Array of prepared zSelectLeaves stmts */
 
117
 
 
118
  int nNodeSize;                  /* Soft limit for node size */
 
119
 
 
120
  /* The following hash table is used to buffer pending index updates during
 
121
  ** transactions. Variable nPendingData estimates the memory size of the 
 
122
  ** pending data, including hash table overhead, but not malloc overhead. 
 
123
  ** When nPendingData exceeds FTS3_MAX_PENDING_DATA, the buffer is flushed 
 
124
  ** automatically. Variable iPrevDocid is the docid of the most recently
 
125
  ** inserted record.
 
126
  */
 
127
  int nPendingData;
 
128
  sqlite_int64 iPrevDocid;
 
129
  Fts3Hash pendingTerms;
 
130
};
 
131
 
 
132
/*
 
133
** When the core wants to read from the virtual table, it creates a
 
134
** virtual table cursor (an instance of the following structure) using
 
135
** the xOpen method. Cursors are destroyed using the xClose method.
 
136
*/
 
137
struct Fts3Cursor {
 
138
  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
 
139
  i16 eSearch;                    /* Search strategy (see below) */
 
140
  u8 isEof;                       /* True if at End Of Results */
 
141
  u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
 
142
  sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
 
143
  Fts3Expr *pExpr;                /* Parsed MATCH query string */
 
144
  sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
 
145
  char *pNextId;                  /* Pointer into the body of aDoclist */
 
146
  char *aDoclist;                 /* List of docids for full-text queries */
 
147
  int nDoclist;                   /* Size of buffer at aDoclist */
 
148
};
 
149
 
 
150
/*
 
151
** The Fts3Cursor.eSearch member is always set to one of the following.
 
152
** Actualy, Fts3Cursor.eSearch can be greater than or equal to
 
153
** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
 
154
** of the column to be searched.  For example, in
 
155
**
 
156
**     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
 
157
**     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
 
158
** 
 
159
** Because the LHS of the MATCH operator is 2nd column "b",
 
160
** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
 
161
** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1" 
 
162
** indicating that all columns should be searched,
 
163
** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
 
164
*/
 
165
#define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
 
166
#define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
 
167
#define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
 
168
 
 
169
/*
 
170
** A "phrase" is a sequence of one or more tokens that must match in
 
171
** sequence.  A single token is the base case and the most common case.
 
172
** For a sequence of tokens contained in "...", nToken will be the number
 
173
** of tokens in the string.
 
174
*/
 
175
struct Fts3Phrase {
 
176
  int nToken;                /* Number of tokens in the phrase */
 
177
  int iColumn;               /* Index of column this phrase must match */
 
178
  int isNot;                 /* Phrase prefixed by unary not (-) operator */
 
179
  struct PhraseToken {
 
180
    char *z;                 /* Text of the token */
 
181
    int n;                   /* Number of bytes in buffer pointed to by z */
 
182
    int isPrefix;            /* True if token ends in with a "*" character */
 
183
  } aToken[1];               /* One entry for each token in the phrase */
 
184
};
 
185
 
 
186
/*
 
187
** A tree of these objects forms the RHS of a MATCH operator.
 
188
*/
 
189
struct Fts3Expr {
 
190
  int eType;                 /* One of the FTSQUERY_XXX values defined below */
 
191
  int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
 
192
  Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
 
193
  Fts3Expr *pLeft;           /* Left operand */
 
194
  Fts3Expr *pRight;          /* Right operand */
 
195
  Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
 
196
};
 
197
 
 
198
/*
 
199
** Candidate values for Fts3Query.eType. Note that the order of the first
 
200
** four values is in order of precedence when parsing expressions. For 
 
201
** example, the following:
 
202
**
 
203
**   "a OR b AND c NOT d NEAR e"
 
204
**
 
205
** is equivalent to:
 
206
**
 
207
**   "a OR (b AND (c NOT (d NEAR e)))"
 
208
*/
 
209
#define FTSQUERY_NEAR   1
 
210
#define FTSQUERY_NOT    2
 
211
#define FTSQUERY_AND    3
 
212
#define FTSQUERY_OR     4
 
213
#define FTSQUERY_PHRASE 5
 
214
 
 
215
 
 
216
/* fts3_init.c */
 
217
int sqlite3Fts3DeleteVtab(int, sqlite3_vtab *);
 
218
int sqlite3Fts3InitVtab(int, sqlite3*, void*, int, const char*const*, 
 
219
                        sqlite3_vtab **, char **);
 
220
 
 
221
/* fts3_write.c */
 
222
int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
 
223
int sqlite3Fts3PendingTermsFlush(Fts3Table *);
 
224
void sqlite3Fts3PendingTermsClear(Fts3Table *);
 
225
int sqlite3Fts3Optimize(Fts3Table *);
 
226
int sqlite3Fts3SegReaderNew(Fts3Table *,int, sqlite3_int64,
 
227
  sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
 
228
void sqlite3Fts3SegReaderFree(Fts3Table *, Fts3SegReader *);
 
229
int sqlite3Fts3SegReaderIterate(
 
230
  Fts3Table *, Fts3SegReader **, int, Fts3SegFilter *,
 
231
  int (*)(Fts3Table *, void *, char *, int, char *, int),  void *
 
232
);
 
233
int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char const**, int*);
 
234
int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **);
 
235
 
 
236
/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
 
237
#define FTS3_SEGMENT_REQUIRE_POS   0x00000001
 
238
#define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
 
239
#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
 
240
#define FTS3_SEGMENT_PREFIX        0x00000008
 
241
 
 
242
/* Type passed as 4th argument to SegmentReaderIterate() */
 
243
struct Fts3SegFilter {
 
244
  const char *zTerm;
 
245
  int nTerm;
 
246
  int iCol;
 
247
  int flags;
 
248
};
 
249
 
 
250
/* fts3.c */
 
251
int sqlite3Fts3PutVarint(char *, sqlite3_int64);
 
252
int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
 
253
int sqlite3Fts3GetVarint32(const char *, int *);
 
254
int sqlite3Fts3VarintLen(sqlite3_uint64);
 
255
void sqlite3Fts3Dequote(char *);
 
256
 
 
257
/* fts3_tokenizer.c */
 
258
const char *sqlite3Fts3NextToken(const char *, int *);
 
259
int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
 
260
int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, 
 
261
  const char *, sqlite3_tokenizer **, const char **, char **
 
262
);
 
263
 
 
264
/* fts3_snippet.c */
 
265
void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
 
266
void sqlite3Fts3Snippet(sqlite3_context*, Fts3Cursor*, 
 
267
  const char *, const char *, const char *
 
268
);
 
269
 
 
270
/* fts3_expr.c */
 
271
int sqlite3Fts3ExprParse(sqlite3_tokenizer *, 
 
272
  char **, int, int, const char *, int, Fts3Expr **
 
273
);
 
274
void sqlite3Fts3ExprFree(Fts3Expr *);
 
275
#ifdef SQLITE_TEST
 
276
void sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
 
277
#endif
 
278
 
 
279
#endif /* _FTSINT_H */