⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fts3.c

📁 sqlite最新源码
💻 C
📖 第 1 页 / 共 5 页
字号:
    }  }  dlrDestroy(&left);  dlrDestroy(&right);  dlwDestroy(&writer);}/* We have two DL_DOCIDS doclists:  pLeft and pRight.** Write the intersection of these two doclists into pOut as a** DL_DOCIDS doclist.*/static void docListAndMerge(  const char *pLeft, int nLeft,  const char *pRight, int nRight,  DataBuffer *pOut      /* Write the combined doclist here */){  DLReader left, right;  DLWriter writer;  if( nLeft==0 || nRight==0 ) return;  dlrInit(&left, DL_DOCIDS, pLeft, nLeft);  dlrInit(&right, DL_DOCIDS, pRight, nRight);  dlwInit(&writer, DL_DOCIDS, pOut);  while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){    if( dlrDocid(&left)<dlrDocid(&right) ){      dlrStep(&left);    }else if( dlrDocid(&right)<dlrDocid(&left) ){      dlrStep(&right);    }else{      dlwAdd(&writer, dlrDocid(&left));      dlrStep(&left);      dlrStep(&right);    }  }  dlrDestroy(&left);  dlrDestroy(&right);  dlwDestroy(&writer);}/* We have two DL_DOCIDS doclists:  pLeft and pRight.** Write the union of these two doclists into pOut as a** DL_DOCIDS doclist.*/static void docListOrMerge(  const char *pLeft, int nLeft,  const char *pRight, int nRight,  DataBuffer *pOut      /* Write the combined doclist here */){  DLReader left, right;  DLWriter writer;  if( nLeft==0 ){    if( nRight!=0 ) dataBufferAppend(pOut, pRight, nRight);    return;  }  if( nRight==0 ){    dataBufferAppend(pOut, pLeft, nLeft);    return;  }  dlrInit(&left, DL_DOCIDS, pLeft, nLeft);  dlrInit(&right, DL_DOCIDS, pRight, nRight);  dlwInit(&writer, DL_DOCIDS, pOut);  while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){    if( dlrAtEnd(&right) ){      dlwAdd(&writer, dlrDocid(&left));      dlrStep(&left);    }else if( dlrAtEnd(&left) ){      dlwAdd(&writer, dlrDocid(&right));      dlrStep(&right);    }else if( dlrDocid(&left)<dlrDocid(&right) ){      dlwAdd(&writer, dlrDocid(&left));      dlrStep(&left);    }else if( dlrDocid(&right)<dlrDocid(&left) ){      dlwAdd(&writer, dlrDocid(&right));      dlrStep(&right);    }else{      dlwAdd(&writer, dlrDocid(&left));      dlrStep(&left);      dlrStep(&right);    }  }  dlrDestroy(&left);  dlrDestroy(&right);  dlwDestroy(&writer);}/* We have two DL_DOCIDS doclists:  pLeft and pRight.** Write into pOut as DL_DOCIDS doclist containing all documents that** occur in pLeft but not in pRight.*/static void docListExceptMerge(  const char *pLeft, int nLeft,  const char *pRight, int nRight,  DataBuffer *pOut      /* Write the combined doclist here */){  DLReader left, right;  DLWriter writer;  if( nLeft==0 ) return;  if( nRight==0 ){    dataBufferAppend(pOut, pLeft, nLeft);    return;  }  dlrInit(&left, DL_DOCIDS, pLeft, nLeft);  dlrInit(&right, DL_DOCIDS, pRight, nRight);  dlwInit(&writer, DL_DOCIDS, pOut);  while( !dlrAtEnd(&left) ){    while( !dlrAtEnd(&right) && dlrDocid(&right)<dlrDocid(&left) ){      dlrStep(&right);    }    if( dlrAtEnd(&right) || dlrDocid(&left)<dlrDocid(&right) ){      dlwAdd(&writer, dlrDocid(&left));    }    dlrStep(&left);  }  dlrDestroy(&left);  dlrDestroy(&right);  dlwDestroy(&writer);}static char *string_dup_n(const char *s, int n){  char *str = sqlite3_malloc(n + 1);  memcpy(str, s, n);  str[n] = '\0';  return str;}/* Duplicate a string; the caller must free() the returned string. * (We don't use strdup() since it is not part of the standard C library and * may not be available everywhere.) */static char *string_dup(const char *s){  return string_dup_n(s, strlen(s));}/* Format a string, replacing each occurrence of the % character with * zDb.zName.  This may be more convenient than sqlite_mprintf() * when one string is used repeatedly in a format string. * The caller must free() the returned string. */static char *string_format(const char *zFormat,                           const char *zDb, const char *zName){  const char *p;  size_t len = 0;  size_t nDb = strlen(zDb);  size_t nName = strlen(zName);  size_t nFullTableName = nDb+1+nName;  char *result;  char *r;  /* first compute length needed */  for(p = zFormat ; *p ; ++p){    len += (*p=='%' ? nFullTableName : 1);  }  len += 1;  /* for null terminator */  r = result = sqlite3_malloc(len);  for(p = zFormat; *p; ++p){    if( *p=='%' ){      memcpy(r, zDb, nDb);      r += nDb;      *r++ = '.';      memcpy(r, zName, nName);      r += nName;    } else {      *r++ = *p;    }  }  *r++ = '\0';  assert( r == result + len );  return result;}static int sql_exec(sqlite3 *db, const char *zDb, const char *zName,                    const char *zFormat){  char *zCommand = string_format(zFormat, zDb, zName);  int rc;  FTSTRACE(("FTS3 sql: %s\n", zCommand));  rc = sqlite3_exec(db, zCommand, NULL, 0, NULL);  sqlite3_free(zCommand);  return rc;}static int sql_prepare(sqlite3 *db, const char *zDb, const char *zName,                       sqlite3_stmt **ppStmt, const char *zFormat){  char *zCommand = string_format(zFormat, zDb, zName);  int rc;  FTSTRACE(("FTS3 prepare: %s\n", zCommand));  rc = sqlite3_prepare_v2(db, zCommand, -1, ppStmt, NULL);  sqlite3_free(zCommand);  return rc;}/* end utility functions *//* Forward reference */typedef struct fulltext_vtab fulltext_vtab;/*** An instance of the following structure keeps track of generated** matching-word offset information and snippets.*/typedef struct Snippet {  int nMatch;     /* Total number of matches */  int nAlloc;     /* Space allocated for aMatch[] */  struct snippetMatch { /* One entry for each matching term */    char snStatus;       /* Status flag for use while constructing snippets */    short int iCol;      /* The column that contains the match */    short int iTerm;     /* The index in Query.pTerms[] of the matching term */    int iToken;          /* The index of the matching document token */    short int nByte;     /* Number of bytes in the term */    int iStart;          /* The offset to the first character of the term */  } *aMatch;      /* Points to space obtained from malloc */  char *zOffset;  /* Text rendering of aMatch[] */  int nOffset;    /* strlen(zOffset) */  char *zSnippet; /* Snippet text */  int nSnippet;   /* strlen(zSnippet) */} Snippet;typedef enum QueryType {  QUERY_GENERIC,   /* table scan */  QUERY_DOCID,     /* lookup by docid */  QUERY_FULLTEXT   /* QUERY_FULLTEXT + [i] is a full-text search for column i*/} QueryType;typedef enum fulltext_statement {  CONTENT_INSERT_STMT,  CONTENT_SELECT_STMT,  CONTENT_UPDATE_STMT,  CONTENT_DELETE_STMT,  CONTENT_EXISTS_STMT,  BLOCK_INSERT_STMT,  BLOCK_SELECT_STMT,  BLOCK_DELETE_STMT,  BLOCK_DELETE_ALL_STMT,  SEGDIR_MAX_INDEX_STMT,  SEGDIR_SET_STMT,  SEGDIR_SELECT_LEVEL_STMT,  SEGDIR_SPAN_STMT,  SEGDIR_DELETE_STMT,  SEGDIR_SELECT_SEGMENT_STMT,  SEGDIR_SELECT_ALL_STMT,  SEGDIR_DELETE_ALL_STMT,  SEGDIR_COUNT_STMT,  MAX_STMT                     /* Always at end! */} fulltext_statement;/* These must exactly match the enum above. *//* TODO(shess): Is there some risk that a statement will be used in two** cursors at once, e.g.  if a query joins a virtual table to itself?** If so perhaps we should move some of these to the cursor object.*/static const char *const fulltext_zStatement[MAX_STMT] = {  /* CONTENT_INSERT */ NULL,  /* generated in contentInsertStatement() */  /* CONTENT_SELECT */ NULL,  /* generated in contentSelectStatement() */  /* CONTENT_UPDATE */ NULL,  /* generated in contentUpdateStatement() */  /* CONTENT_DELETE */ "delete from %_content where docid = ?",  /* CONTENT_EXISTS */ "select docid from %_content limit 1",  /* BLOCK_INSERT */  "insert into %_segments (blockid, block) values (null, ?)",  /* BLOCK_SELECT */ "select block from %_segments where blockid = ?",  /* BLOCK_DELETE */ "delete from %_segments where blockid between ? and ?",  /* BLOCK_DELETE_ALL */ "delete from %_segments",  /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?",  /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)",  /* SEGDIR_SELECT_LEVEL */  "select start_block, leaves_end_block, root from %_segdir "  " where level = ? order by idx",  /* SEGDIR_SPAN */  "select min(start_block), max(end_block) from %_segdir "  " where level = ? and start_block <> 0",  /* SEGDIR_DELETE */ "delete from %_segdir where level = ?",  /* NOTE(shess): The first three results of the following two  ** statements must match.  */  /* SEGDIR_SELECT_SEGMENT */  "select start_block, leaves_end_block, root from %_segdir "  " where level = ? and idx = ?",  /* SEGDIR_SELECT_ALL */  "select start_block, leaves_end_block, root from %_segdir "  " order by level desc, idx asc",  /* SEGDIR_DELETE_ALL */ "delete from %_segdir",  /* SEGDIR_COUNT */ "select count(*), ifnull(max(level),0) from %_segdir",};/*** A connection to a fulltext index is an instance of the following** structure.  The xCreate and xConnect methods create an instance** of this structure and xDestroy and xDisconnect free that instance.** All other methods receive a pointer to the structure as one of their** arguments.*/struct fulltext_vtab {  sqlite3_vtab base;               /* Base class used by SQLite core */  sqlite3 *db;                     /* The database connection */  const char *zDb;                 /* logical database name */  const char *zName;               /* virtual table name */  int nColumn;                     /* number of columns in virtual table */  char **azColumn;                 /* column names.  malloced */  char **azContentColumn;          /* column names in content table; malloced */  sqlite3_tokenizer *pTokenizer;   /* tokenizer for inserts and queries */  /* Precompiled statements which we keep as long as the table is  ** open.  */  sqlite3_stmt *pFulltextStatements[MAX_STMT];  /* Precompiled statements used for segment merges.  We run a  ** separate select across the leaf level of each tree being merged.  */  sqlite3_stmt *pLeafSelectStmts[MERGE_COUNT];  /* The statement used to prepare pLeafSelectStmts. */#define LEAF_SELECT \  "select block from %_segments where blockid between ? and ? order by blockid"  /* These buffer pending index updates during transactions.  ** nPendingData estimates the memory size of the pending data.  It  ** doesn't include the hash-bucket overhead, nor any malloc  ** overhead.  When nPendingData exceeds kPendingThreshold, the  ** buffer is flushed even before the transaction closes.  ** pendingTerms stores the data, and is only valid when nPendingData  ** is >=0 (nPendingData<0 means pendingTerms has not been  ** initialized).  iPrevDocid is the last docid written, used to make  ** certain we're inserting in sorted order.  */  int nPendingData;#define kPendingThreshold (1*1024*1024)  sqlite_int64 iPrevDocid;  fts3Hash pendingTerms;};/*** When the core wants to do a query, it create a cursor using a** call to xOpen.  This structure is an instance of a cursor.  It** is destroyed by xClose.*/typedef struct fulltext_cursor {  sqlite3_vtab_cursor base;        /* Base class used by SQLite core */  QueryType iCursorType;           /* Copy of sqlite3_index_info.idxNum */  sqlite3_stmt *pStmt;             /* Prepared statement in use by the cursor */  int eof;                         /* True if at End Of Results */  Fts3Expr *pExpr;                 /* Parsed MATCH query string */  Snippet snippet;                 /* Cached snippet for the current row */  int iColumn;                     /* Column being searched */  DataBuffer result;               /* Doclist results from fulltextQuery */  DLReader reader;                 /* Result reader if result not empty */} fulltext_cursor;static fulltext_vtab *cursor_vtab(fulltext_cursor *c){  return (fulltext_vtab *) c->base.pVtab;}static const sqlite3_module fts3Module;   /* forward declaration *//* Return a dynamically generated statement of the form *   insert into %_content (docid, ...) values (?, ...) */static const char *contentInsertStatement(fulltext_vtab *v){  StringBuffer sb;  int i;  initStringBuffer(&sb);  append(&sb, "insert into %_content (docid, ");  appendList(&sb, v->nColumn, v->azContentColumn);  append(&sb, ") values (?");  for(i=0; i<v->nColumn; ++i)    append(&sb, ", ?");  append(&sb, ")");  return stringBufferData(&sb);}/* Return a dynamically generated statement of the form *   select <content columns> from %_content where docid = ? */static const char *contentSelectStatement(fulltext_vtab *v){  StringBuffer sb;  initStringBuffer(&sb);  append(&sb, "S

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -