fts1_hash.c 11 KB

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