1
0

fts2_tokenizer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** 2006 July 10
  3. **
  4. ** The author disclaims copyright to this source code.
  5. **
  6. *************************************************************************
  7. ** Defines the interface to tokenizers used by fulltext-search. There
  8. ** are three basic components:
  9. **
  10. ** sqlite3_tokenizer_module is a singleton defining the tokenizer
  11. ** interface functions. This is essentially the class structure for
  12. ** tokenizers.
  13. **
  14. ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
  15. ** including customization information defined at creation time.
  16. **
  17. ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
  18. ** tokens from a particular input.
  19. */
  20. #ifndef _FTS2_TOKENIZER_H_
  21. #define _FTS2_TOKENIZER_H_
  22. /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
  23. ** If tokenizers are to be allowed to call sqlite3_*() functions, then
  24. ** we will need a way to register the API consistently.
  25. */
  26. #include "sqlite3.h"
  27. /*
  28. ** Structures used by the tokenizer interface. When a new tokenizer
  29. ** implementation is registered, the caller provides a pointer to
  30. ** an sqlite3_tokenizer_module containing pointers to the callback
  31. ** functions that make up an implementation.
  32. **
  33. ** When an fts2 table is created, it passes any arguments passed to
  34. ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
  35. ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
  36. ** implementation. The xCreate() function in turn returns an
  37. ** sqlite3_tokenizer structure representing the specific tokenizer to
  38. ** be used for the fts2 table (customized by the tokenizer clause arguments).
  39. **
  40. ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
  41. ** method is called. It returns an sqlite3_tokenizer_cursor object
  42. ** that may be used to tokenize a specific input buffer based on
  43. ** the tokenization rules supplied by a specific sqlite3_tokenizer
  44. ** object.
  45. */
  46. typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
  47. typedef struct sqlite3_tokenizer sqlite3_tokenizer;
  48. typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
  49. struct sqlite3_tokenizer_module {
  50. /*
  51. ** Structure version. Should always be set to 0.
  52. */
  53. int iVersion;
  54. /*
  55. ** Create a new tokenizer. The values in the argv[] array are the
  56. ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
  57. ** TABLE statement that created the fts2 table. For example, if
  58. ** the following SQL is executed:
  59. **
  60. ** CREATE .. USING fts2( ... , tokenizer <tokenizer-name> arg1 arg2)
  61. **
  62. ** then argc is set to 2, and the argv[] array contains pointers
  63. ** to the strings "arg1" and "arg2".
  64. **
  65. ** This method should return either SQLITE_OK (0), or an SQLite error
  66. ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
  67. ** to point at the newly created tokenizer structure. The generic
  68. ** sqlite3_tokenizer.pModule variable should not be initialized by
  69. ** this callback. The caller will do so.
  70. */
  71. int (*xCreate)(
  72. int argc, /* Size of argv array */
  73. const char *const*argv, /* Tokenizer argument strings */
  74. sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
  75. );
  76. /*
  77. ** Destroy an existing tokenizer. The fts2 module calls this method
  78. ** exactly once for each successful call to xCreate().
  79. */
  80. int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
  81. /*
  82. ** Create a tokenizer cursor to tokenize an input buffer. The caller
  83. ** is responsible for ensuring that the input buffer remains valid
  84. ** until the cursor is closed (using the xClose() method).
  85. */
  86. int (*xOpen)(
  87. sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
  88. const char *pInput, int nBytes, /* Input buffer */
  89. sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
  90. );
  91. /*
  92. ** Destroy an existing tokenizer cursor. The fts2 module calls this
  93. ** method exactly once for each successful call to xOpen().
  94. */
  95. int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
  96. /*
  97. ** Retrieve the next token from the tokenizer cursor pCursor. This
  98. ** method should either return SQLITE_OK and set the values of the
  99. ** "OUT" variables identified below, or SQLITE_DONE to indicate that
  100. ** the end of the buffer has been reached, or an SQLite error code.
  101. **
  102. ** *ppToken should be set to point at a buffer containing the
  103. ** normalized version of the token (i.e. after any case-folding and/or
  104. ** stemming has been performed). *pnBytes should be set to the length
  105. ** of this buffer in bytes. The input text that generated the token is
  106. ** identified by the byte offsets returned in *piStartOffset and
  107. ** *piEndOffset.
  108. **
  109. ** The buffer *ppToken is set to point at is managed by the tokenizer
  110. ** implementation. It is only required to be valid until the next call
  111. ** to xNext() or xClose().
  112. */
  113. /* TODO(shess) current implementation requires pInput to be
  114. ** nul-terminated. This should either be fixed, or pInput/nBytes
  115. ** should be converted to zInput.
  116. */
  117. int (*xNext)(
  118. sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
  119. const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
  120. int *piStartOffset, /* OUT: Byte offset of token in input buffer */
  121. int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
  122. int *piPosition /* OUT: Number of tokens returned before this one */
  123. );
  124. };
  125. struct sqlite3_tokenizer {
  126. const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
  127. /* Tokenizer implementations will typically add additional fields */
  128. };
  129. struct sqlite3_tokenizer_cursor {
  130. sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
  131. /* Tokenizer implementations will typically add additional fields */
  132. };
  133. #endif /* _FTS2_TOKENIZER_H_ */