forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.cpp
256 lines (242 loc) · 7.33 KB
/
HashTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "gb-include.h"
#include "HashTable.h"
HashTable::HashTable () {
m_keys = NULL;
m_vals = NULL;
m_numSlots = 0;
m_numSlotsUsed = 0;
m_doFree = true;
}
// returns false and sets errno on error
bool HashTable::set ( long initialNumTerms , char *buf , long bufSize ) {
reset();
return setTableSize ( initialNumTerms , buf , bufSize );
}
HashTable::~HashTable ( ) { reset ( ); }
// . call clean() to do a more careful reset
// . clean will rehash
void HashTable::reset ( ) {
if ( m_doFree ) {
if (m_keys) mfree(m_keys,m_numSlots*sizeof(long),"HashTablek");
if (m_vals) mfree(m_vals,m_numSlots*sizeof(long),"HashTablev");
}
m_keys = NULL;
m_vals = NULL;
m_numSlots = 0;
m_numSlotsUsed = 0;
}
void HashTable::clear ( ) {
// vacate all slots
if ( m_keys ) memset ( m_keys , 0 , sizeof(long) * m_numSlots );
m_numSlotsUsed = 0;
}
// . returns the slot number for "key"
// . returns -1 if key not in hash table
long HashTable::getOccupiedSlotNum ( long key ) {
if ( m_numSlots <= 0 ) return -1;
//long n = ((unsigned long)key) % ((unsigned long)m_numSlots);
long n = ((unsigned long)key) & m_mask;
long count = 0;
while ( count++ < m_numSlots ) {
if ( m_keys [ n ] == 0 ) return -1;
if ( m_keys [ n ] == key ) return n;
if ( ++n == m_numSlots ) n = 0;
}
log("hashtable: Could not get key. Table is full.");
return -1;
}
// return 0 if key not in hash table
long HashTable::getValue ( long key ) {
// returns -1 if key not in hash table
long n = getOccupiedSlotNum ( key );
if ( n < 0 ) return 0;
return m_vals[n];
}
// . returns false and sets errno on error, returns true otherwise
// . adds scores if termId already exists in table
bool HashTable::addKey ( long key , long value , long *slot ) {
// keys of 0 mean empty! they are reserved... fix that!
if ( key == 0 ) { char *xx=NULL; *xx=0; }
// check to see if we should grow the table
if ( 100 * m_numSlotsUsed >= m_numSlots * 90 ) {
long growTo = (m_numSlots * 120 ) / 100 + 20;
if ( ! setTableSize ( growTo , NULL , 0 ) ) return false;
}
//long n = ((unsigned long)key) % ((unsigned long)m_numSlots);
long n = ((unsigned long)key) & m_mask;
long count = 0;
while ( count++ < m_numSlots ) {
if ( m_keys [ n ] == 0 ) break;
if ( m_keys [ n ] == key ) break;
if ( ++n == m_numSlots ) n = 0;
}
// bail if not found
if ( count >= m_numSlots ) {
g_errno = ENOMEM;
return log("hashtable: Could not add key. Table is full.");
}
if ( m_keys [ n ] == 0 ) {
// inc count if we're the first
m_numSlotsUsed++;
// and store the ky
m_keys [ n ] = key;
}
// insert the value for this key
m_vals [ n ] = value;
if ( slot ) *slot = n;
return true;
}
// patch the hole so chaining still works
bool HashTable::removeKey ( long key ) {
// returns -1 if key not in hash table
long n = getOccupiedSlotNum(key);
if ( n < 0 ) return true;
m_keys[n] = 0;
m_numSlotsUsed--;
if ( ++n >= m_numSlots ) n = 0;
// keep looping until we hit an empty slot
long val;
while ( m_keys[n] ) {
key = m_keys[n];
val = m_vals[n];
m_keys[n] = 0;
m_numSlotsUsed--;
addKey ( key , val );
if ( ++n >= m_numSlots ) n = 0;
}
return true;
}
// patch the hole so chaining still works
void HashTable::removeSlot ( long n ) {
// returns -1 if key not in hash table
//long n = getOccupiedSlotNum(key);
//if ( n < 0 ) return true;
long key = m_keys[n];
// sanity check, must not be empty
if ( key == 0 ) { char *xx = NULL; *xx = 0; }
// delete it
m_keys[n] = 0;
m_numSlotsUsed--;
if ( ++n >= m_numSlots ) n = 0;
// keep looping until we hit an empty slot
long val;
while ( m_keys[n] ) {
key = m_keys[n];
val = m_vals[n];
m_keys[n] = 0;
m_numSlotsUsed--;
addKey ( key , val );
if ( ++n >= m_numSlots ) n = 0;
}
}
// . set table size to "n" slots
// . rehashes the termId/score pairs into new table
// . returns false and sets errno on error
bool HashTable::setTableSize ( long oldn , char *buf , long bufSize ) {
// don't change size if we do not need to
if ( oldn == m_numSlots ) return true;
// make it a power of 2
long n = getHighestLitBitValue ( (unsigned long)oldn * 2 - 1 );
// do not go negative on me
if ( oldn == 0 ) n = 0;
// sanity check
if ( n < oldn ) { char *xx = NULL; *xx = 0; }
// do we have a buf?
long need = 2 * n * sizeof(long);
// sanity check, buf should also meet what we need
if ( buf && bufSize < need ) { char *xx = NULL; *xx = 0; }
// set the buf
long *newKeys ;
long *newVals ;
// if we should not free note that
bool savedDoFree = m_doFree ;
// use our buf if we can
if ( buf ) {
m_doFree = false;
bzero ( buf , need );
newKeys = (long *)buf;
buf += n * sizeof(long);
newVals = (long *)buf;
buf += n * sizeof(long);
}
else {
m_doFree = true;
newKeys = (long *)mcalloc ( n * sizeof(long) , "HashTablek");
if ( ! newKeys ) return false;
newVals = (long *)mmalloc ( n * sizeof(long) , "HashTablev");
if ( ! newVals ) {
mfree ( newKeys , n * sizeof(long) , "HashTablek" );
return false;
}
}
// rehash the slots if we had some
if ( m_keys ) {
for ( long i = 0 ; i < m_numSlots ; i++ ) {
// skip the empty slots
if ( m_keys [ i ] == 0 ) continue;
// get the new slot # for this slot (might be the same)
//longnum=((unsigned long)m_keys[i])%((unsigned long)n)
long num=((unsigned long)m_keys[i])&
((unsigned long)(n-1));
// if that is occupied, go down
while ( newKeys[num] ) if ( ++num >= n ) num = 0;
// move the slotPtr/key/size to this new slot
newKeys [ num ] = m_keys [ i ];
newVals [ num ] = m_vals [ i ];
}
}
// free the old guys
if ( m_keys && savedDoFree ) {
mfree ( m_keys , m_numSlots * sizeof(long) , "HashTablek" );
mfree ( m_vals , m_numSlots * sizeof(long) , "HashTablev" );
}
// assign the new slots, m_numSlotsUsed should be the same
m_keys = newKeys;
m_vals = newVals;
m_numSlots = n;
m_mask = n - 1;
return true;
}
// both return false and set g_errno on error, true otherwise
bool HashTable::load ( char *dir , char *filename ) {
reset();
File f;
f.set ( dir , filename );
if ( ! f.doesExist() ) return true;
log(LOG_INFO,"admin: Loading hashtable from %s%s",dir,filename);
if ( ! f.open ( O_RDONLY) ) return false;
long numSlots;
long numSlotsUsed;
long off = 0;
if ( ! f.read ( &numSlots , 4 , off ) ) return false;
off += 4;
if ( ! f.read ( &numSlotsUsed , 4 , off ) ) return false;
off += 4;
if ( ! setTableSize ( numSlots , NULL , 0 ) ) return false;
if ( ! f.read ( m_keys , numSlots * 4 , off ) ) return false;
off += numSlots * 4;
if ( ! f.read ( m_vals , numSlots * 4 , off ) ) return false;
off += numSlots * 4;
m_numSlotsUsed = numSlotsUsed;
f.close();
return true;
}
bool HashTable::save ( char *dir , char *filename ) {
File f;
f.set ( dir , filename );
log(LOG_INFO,"admin: Saving hashtable from %s%s",dir,filename);
if ( ! f.open ( O_RDWR | O_CREAT ) ) return false;
long numSlots = m_numSlots;
long numSlotsUsed = m_numSlotsUsed;
long off = 0;
if ( ! f.write ( &numSlots , 4 , off ) ) return false;
off += 4;
if ( ! f.write ( &numSlotsUsed , 4 , off ) ) return false;
off += 4;
if ( ! f.write ( m_keys , numSlots * 4 , off ) ) return false;
off += numSlots * 4;
if ( ! f.write ( m_vals , numSlots * 4 , off ) ) return false;
off += numSlots * 4;
f.close();
return true;
}