fts3_hash.c 11 KB

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