uint64_t FNVHashInt64(uint64_t hashme) { unsigned char buf[8]; int i; for (i = 0; i < 8; i++) { buf[i] = (unsigned char) (hashme & 0x00000000000000FFULL); hashme >>= 8; } return FNVHash64(buf, 8); }
HTKey_t FNVHashInt64(HTValue_t hashval) { unsigned char buf[8]; int i; uint64_t hashme = (uint64_t)hashval; for (i = 0; i < 8; i++) { buf[i] = (unsigned char) (hashme & 0x00000000000000FFULL); hashme >>= 8; } return FNVHash64(buf, 8); }
static void AddToHashtable(HashTable tab, char *word, DocPositionOffset_t pos) { HTKey_t hashKey; int retval; HTKeyValue kv; // Hash the string. hashKey = FNVHash64((unsigned char *) word, strlen(word)); // Have we already encountered this word within this file? // If so, it's already in the hashtable. retval = LookupHashTable(tab, hashKey, &kv); if (retval == 1) { // Yes; we just need to add a position in using AppendLinkedList(). Note // how we're casting the DocPositionOffset_t position // variable to an LLPayload_t to store // it in the linked list payload without needing to malloc space for it. // Ugly, but it works! WordPositions *wp = (WordPositions *) kv.value; retval = AppendLinkedList(wp->positions, (LLPayload_t) ((intptr_t) pos)); Verify333(retval != 0); } else { // STEP 8. // No; this is the first time we've seen this word. Allocate and prepare // a new WordPositions structure, and append the new position to its list // using a similar ugly hack as right above. WordPositions *wp; char *newstr; HTKeyValue oldkv; bool retbool; // Allocate space for a new WordPositions structure. wp = (WordPositions *)malloc(sizeof(WordPositions)); Verify333(wp != NULL); // Allocate space for the word and copy the word content. newstr = (char *)malloc(strlen(word) + 1); // +1 is for "\0" Verify333(newstr != NULL); snprintf(newstr, strlen(word) +1, "%s", word); wp->word = newstr; // Set linkedlist from positions and append linked list wp->positions = AllocateLinkedList(); Verify333(wp->positions != NULL); retbool = AppendLinkedList(wp->positions, (LLPayload_t) ((intptr_t) pos)); Verify333(retbool); // Set the key value pair and add it to hashtable. kv.key = hashKey; kv.value = wp; retval = InsertHashTable(tab, kv, &oldkv); Verify333(retval == 1); } }
DocID_t DTRegisterDocumentName(DocTable table, char *docname) { // Allocate space for the docid. char *doccopy; DocID_t *docid = (DocID_t *) malloc(sizeof(DocID_t)); DocID_t res; HTKeyValue kv, oldkv; int retval; // Make a copy of the docname. doccopy = (char *) malloc(1+strlen(docname)); if (doccopy != NULL) { strncpy(doccopy, docname, 1+strlen(docname)); } Verify333(table != NULL); Verify333(doccopy != NULL); Verify333(docid != NULL); // Check to see if the document already exists; if so, // free up the malloc'ed space and return the existing docid // STEP 2. res = DTLookupDocumentName(table, docname); if (res != 0) { free(doccopy); free(docid); return res; } // allocate the next docID table->max_id += 1; *docid = table->max_id; // STEP 3. // Set up the key/value for the docid_to_docname mapping, and // do the insert. kv.key = (HTKey_t)*docid; kv.value = (HTValue_t)doccopy; retval = InsertHashTable(table->docid_to_docname, kv, &oldkv); Verify333(retval != 0); // STEP 4. // Set up the key/value for the docname_to_docid mapping, and // do the insert. kv.key = (HTKey_t)FNVHash64((unsigned char*)docname, strlen(docname)); kv.value = (HTValue_t)docid; retval = InsertHashTable(table->docname_to_docid, kv, &oldkv); Verify333(retval != 0); return *docid; }
DocID_t DTLookupDocumentName(DocTable table, char *docname) { HTKey_t key; HTKeyValue kv; int res; Verify333(table != NULL); Verify333(docname != NULL); // STEP 5. // Lookup the FNVHash64() or the docname in the // docname_to_docid table within dt, and return // either "0" if the docname isn't found or the // docID if it is. key = FNVHash64((unsigned char*)docname, strlen(docname)); res = LookupHashTable(table->docname_to_docid, key, &kv); if (res != 1) { return 0; } else { return *(DocID_t*)kv.value; } }