fts2_hash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ** 2001 September 22
  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. ** This is the implementation of generic hash-tables used in SQLite.
  13. ** We've modified it slightly to serve as a standalone hash table
  14. ** implementation for the full-text indexing module.
  15. */
  16. /*
  17. ** The code in this file is only compiled if:
  18. **
  19. ** * The FTS2 module is being built as an extension
  20. ** (in which case SQLITE_CORE is not defined), or
  21. **
  22. ** * The FTS2 module is being built into the core of
  23. ** SQLite (in which case SQLITE_ENABLE_FTS2 is defined).
  24. */
  25. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)
  26. #include <assert.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "sqlite3.h"
  30. #include "sqlite3ext.h"
  31. SQLITE_EXTENSION_INIT3
  32. #include "fts2_hash.h"
  33. /*
  34. ** Malloc and Free functions
  35. */
  36. static void *fts2HashMalloc(int n){
  37. void *p = sqlite3_malloc(n);
  38. if( p ){
  39. memset(p, 0, n);
  40. }
  41. return p;
  42. }
  43. static void fts2HashFree(void *p){
  44. sqlite3_free(p);
  45. }
  46. /* Turn bulk memory into a hash table object by initializing the
  47. ** fields of the Hash structure.
  48. **
  49. ** "pNew" is a pointer to the hash table that is to be initialized.
  50. ** keyClass is one of the constants
  51. ** FTS2_HASH_BINARY or FTS2_HASH_STRING. The value of keyClass
  52. ** determines what kind of key the hash table will use. "copyKey" is
  53. ** true if the hash table should make its own private copy of keys and
  54. ** false if it should just use the supplied pointer.
  55. */
  56. void sqlite3Fts2HashInit(fts2Hash *pNew, int keyClass, int copyKey){
  57. assert( pNew!=0 );
  58. assert( keyClass>=FTS2_HASH_STRING && keyClass<=FTS2_HASH_BINARY );
  59. pNew->keyClass = keyClass;
  60. pNew->copyKey = copyKey;
  61. pNew->first = 0;
  62. pNew->count = 0;
  63. pNew->htsize = 0;
  64. pNew->ht = 0;
  65. }
  66. /* Remove all entries from a hash table. Reclaim all memory.
  67. ** Call this routine to delete a hash table or to reset a hash table
  68. ** to the empty state.
  69. */
  70. void sqlite3Fts2HashClear(fts2Hash *pH){
  71. fts2HashElem *elem; /* For looping over all elements of the table */
  72. assert( pH!=0 );
  73. elem = pH->first;
  74. pH->first = 0;
  75. fts2HashFree(pH->ht);
  76. pH->ht = 0;
  77. pH->htsize = 0;
  78. while( elem ){
  79. fts2HashElem *next_elem = elem->next;
  80. if( pH->copyKey && elem->pKey ){
  81. fts2HashFree(elem->pKey);
  82. }
  83. fts2HashFree(elem);
  84. elem = next_elem;
  85. }
  86. pH->count = 0;
  87. }
  88. /*
  89. ** Hash and comparison functions when the mode is FTS2_HASH_STRING
  90. */
  91. static int strHash(const void *pKey, int nKey){
  92. const char *z = (const char *)pKey;
  93. int h = 0;
  94. if( nKey<=0 ) nKey = (int) strlen(z);
  95. while( nKey > 0 ){
  96. h = (h<<3) ^ h ^ *z++;
  97. nKey--;
  98. }
  99. return h & 0x7fffffff;
  100. }
  101. static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  102. if( n1!=n2 ) return 1;
  103. return strncmp((const char*)pKey1,(const char*)pKey2,n1);
  104. }
  105. /*
  106. ** Hash and comparison functions when the mode is FTS2_HASH_BINARY
  107. */
  108. static int binHash(const void *pKey, int nKey){
  109. int h = 0;
  110. const char *z = (const char *)pKey;
  111. while( nKey-- > 0 ){
  112. h = (h<<3) ^ h ^ *(z++);
  113. }
  114. return h & 0x7fffffff;
  115. }
  116. static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  117. if( n1!=n2 ) return 1;
  118. return memcmp(pKey1,pKey2,n1);
  119. }
  120. /*
  121. ** Return a pointer to the appropriate hash function given the key class.
  122. **
  123. ** The C syntax in this function definition may be unfamilar to some
  124. ** programmers, so we provide the following additional explanation:
  125. **
  126. ** The name of the function is "hashFunction". The function takes a
  127. ** single parameter "keyClass". The return value of hashFunction()
  128. ** is a pointer to another function. Specifically, the return value
  129. ** of hashFunction() is a pointer to a function that takes two parameters
  130. ** with types "const void*" and "int" and returns an "int".
  131. */
  132. static int (*hashFunction(int keyClass))(const void*,int){
  133. if( keyClass==FTS2_HASH_STRING ){
  134. return &strHash;
  135. }else{
  136. assert( keyClass==FTS2_HASH_BINARY );
  137. return &binHash;
  138. }
  139. }
  140. /*
  141. ** Return a pointer to the appropriate hash function given the key class.
  142. **
  143. ** For help in interpreted the obscure C code in the function definition,
  144. ** see the header comment on the previous function.
  145. */
  146. static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
  147. if( keyClass==FTS2_HASH_STRING ){
  148. return &strCompare;
  149. }else{
  150. assert( keyClass==FTS2_HASH_BINARY );
  151. return &binCompare;
  152. }
  153. }
  154. /* Link an element into the hash table
  155. */
  156. static void insertElement(
  157. fts2Hash *pH, /* The complete hash table */
  158. struct _fts2ht *pEntry, /* The entry into which pNew is inserted */
  159. fts2HashElem *pNew /* The element to be inserted */
  160. ){
  161. fts2HashElem *pHead; /* First element already in pEntry */
  162. pHead = pEntry->chain;
  163. if( pHead ){
  164. pNew->next = pHead;
  165. pNew->prev = pHead->prev;
  166. if( pHead->prev ){ pHead->prev->next = pNew; }
  167. else { pH->first = pNew; }
  168. pHead->prev = pNew;
  169. }else{
  170. pNew->next = pH->first;
  171. if( pH->first ){ pH->first->prev = pNew; }
  172. pNew->prev = 0;
  173. pH->first = pNew;
  174. }
  175. pEntry->count++;
  176. pEntry->chain = pNew;
  177. }
  178. /* Resize the hash table so that it cantains "new_size" buckets.
  179. ** "new_size" must be a power of 2. The hash table might fail
  180. ** to resize if sqliteMalloc() fails.
  181. */
  182. static void rehash(fts2Hash *pH, int new_size){
  183. struct _fts2ht *new_ht; /* The new hash table */
  184. fts2HashElem *elem, *next_elem; /* For looping over existing elements */
  185. int (*xHash)(const void*,int); /* The hash function */
  186. assert( (new_size & (new_size-1))==0 );
  187. new_ht = (struct _fts2ht *)fts2HashMalloc( new_size*sizeof(struct _fts2ht) );
  188. if( new_ht==0 ) return;
  189. fts2HashFree(pH->ht);
  190. pH->ht = new_ht;
  191. pH->htsize = new_size;
  192. xHash = hashFunction(pH->keyClass);
  193. for(elem=pH->first, pH->first=0; elem; elem = next_elem){
  194. int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
  195. next_elem = elem->next;
  196. insertElement(pH, &new_ht[h], elem);
  197. }
  198. }
  199. /* This function (for internal use only) locates an element in an
  200. ** hash table that matches the given key. The hash for this key has
  201. ** already been computed and is passed as the 4th parameter.
  202. */
  203. static fts2HashElem *findElementGivenHash(
  204. const fts2Hash *pH, /* The pH to be searched */
  205. const void *pKey, /* The key we are searching for */
  206. int nKey,
  207. int h /* The hash for this key. */
  208. ){
  209. fts2HashElem *elem; /* Used to loop thru the element list */
  210. int count; /* Number of elements left to test */
  211. int (*xCompare)(const void*,int,const void*,int); /* comparison function */
  212. if( pH->ht ){
  213. struct _fts2ht *pEntry = &pH->ht[h];
  214. elem = pEntry->chain;
  215. count = pEntry->count;
  216. xCompare = compareFunction(pH->keyClass);
  217. while( count-- && elem ){
  218. if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
  219. return elem;
  220. }
  221. elem = elem->next;
  222. }
  223. }
  224. return 0;
  225. }
  226. /* Remove a single entry from the hash table given a pointer to that
  227. ** element and a hash on the element's key.
  228. */
  229. static void removeElementGivenHash(
  230. fts2Hash *pH, /* The pH containing "elem" */
  231. fts2HashElem* elem, /* The element to be removed from the pH */
  232. int h /* Hash value for the element */
  233. ){
  234. struct _fts2ht *pEntry;
  235. if( elem->prev ){
  236. elem->prev->next = elem->next;
  237. }else{
  238. pH->first = elem->next;
  239. }
  240. if( elem->next ){
  241. elem->next->prev = elem->prev;
  242. }
  243. pEntry = &pH->ht[h];
  244. if( pEntry->chain==elem ){
  245. pEntry->chain = elem->next;
  246. }
  247. pEntry->count--;
  248. if( pEntry->count<=0 ){
  249. pEntry->chain = 0;
  250. }
  251. if( pH->copyKey && elem->pKey ){
  252. fts2HashFree(elem->pKey);
  253. }
  254. fts2HashFree( elem );
  255. pH->count--;
  256. if( pH->count<=0 ){
  257. assert( pH->first==0 );
  258. assert( pH->count==0 );
  259. fts2HashClear(pH);
  260. }
  261. }
  262. /* Attempt to locate an element of the hash table pH with a key
  263. ** that matches pKey,nKey. Return the data for this element if it is
  264. ** found, or NULL if there is no match.
  265. */
  266. void *sqlite3Fts2HashFind(const fts2Hash *pH, const void *pKey, int nKey){
  267. int h; /* A hash on key */
  268. fts2HashElem *elem; /* The element that matches key */
  269. int (*xHash)(const void*,int); /* The hash function */
  270. if( pH==0 || pH->ht==0 ) return 0;
  271. xHash = hashFunction(pH->keyClass);
  272. assert( xHash!=0 );
  273. h = (*xHash)(pKey,nKey);
  274. assert( (pH->htsize & (pH->htsize-1))==0 );
  275. elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
  276. return elem ? elem->data : 0;
  277. }
  278. /* Insert an element into the hash table pH. The key is pKey,nKey
  279. ** and the data is "data".
  280. **
  281. ** If no element exists with a matching key, then a new
  282. ** element is created. A copy of the key is made if the copyKey
  283. ** flag is set. NULL is returned.
  284. **
  285. ** If another element already exists with the same key, then the
  286. ** new data replaces the old data and the old data is returned.
  287. ** The key is not copied in this instance. If a malloc fails, then
  288. ** the new data is returned and the hash table is unchanged.
  289. **
  290. ** If the "data" parameter to this function is NULL, then the
  291. ** element corresponding to "key" is removed from the hash table.
  292. */
  293. void *sqlite3Fts2HashInsert(
  294. fts2Hash *pH, /* The hash table to insert into */
  295. const void *pKey, /* The key */
  296. int nKey, /* Number of bytes in the key */
  297. void *data /* The data */
  298. ){
  299. int hraw; /* Raw hash value of the key */
  300. int h; /* the hash of the key modulo hash table size */
  301. fts2HashElem *elem; /* Used to loop thru the element list */
  302. fts2HashElem *new_elem; /* New element added to the pH */
  303. int (*xHash)(const void*,int); /* The hash function */
  304. assert( pH!=0 );
  305. xHash = hashFunction(pH->keyClass);
  306. assert( xHash!=0 );
  307. hraw = (*xHash)(pKey, nKey);
  308. assert( (pH->htsize & (pH->htsize-1))==0 );
  309. h = hraw & (pH->htsize-1);
  310. elem = findElementGivenHash(pH,pKey,nKey,h);
  311. if( elem ){
  312. void *old_data = elem->data;
  313. if( data==0 ){
  314. removeElementGivenHash(pH,elem,h);
  315. }else{
  316. elem->data = data;
  317. }
  318. return old_data;
  319. }
  320. if( data==0 ) return 0;
  321. new_elem = (fts2HashElem*)fts2HashMalloc( sizeof(fts2HashElem) );
  322. if( new_elem==0 ) return data;
  323. if( pH->copyKey && pKey!=0 ){
  324. new_elem->pKey = fts2HashMalloc( nKey );
  325. if( new_elem->pKey==0 ){
  326. fts2HashFree(new_elem);
  327. return data;
  328. }
  329. memcpy((void*)new_elem->pKey, pKey, nKey);
  330. }else{
  331. new_elem->pKey = (void*)pKey;
  332. }
  333. new_elem->nKey = nKey;
  334. pH->count++;
  335. if( pH->htsize==0 ){
  336. rehash(pH,8);
  337. if( pH->htsize==0 ){
  338. pH->count = 0;
  339. fts2HashFree(new_elem);
  340. return data;
  341. }
  342. }
  343. if( pH->count > pH->htsize ){
  344. rehash(pH,pH->htsize*2);
  345. }
  346. assert( pH->htsize>0 );
  347. assert( (pH->htsize & (pH->htsize-1))==0 );
  348. h = hraw & (pH->htsize-1);
  349. insertElement(pH, &pH->ht[h], new_elem);
  350. new_elem->data = data;
  351. return 0;
  352. }
  353. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */