forked from 3dyne/3dyne_legacy_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_hash.cpp
397 lines (298 loc) · 11 KB
/
disk_hash.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (C) 2013 Simon A. Berger
*
* This program is free software; you may redistribute it and/or modify its
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <unistd.h>
//#include <sys/mman.h>
#include <cassert>
#include <cstring>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <stdexcept>
#include "minimal_external_hash.h"
#if 0
class mapped_file {
public:
mapped_file( const char *filename, bool read_write ) {
int mode = O_RDONLY;
int prot = PROT_READ;
if( read_write ) {
mode = O_RDWR;
prot = PROT_READ | PROT_WRITE;
}
fd_ = open( filename, mode );
assert( fd_ != -1 );
size_ = lseek( fd_, 0, SEEK_END );
lseek( fd_, 0, SEEK_SET );
base_ = mmap( 0, size_, prot, MAP_SHARED, fd_, 0 );
assert( base_ != 0 );
if( read_write ) {
madvise( base_, size_, MADV_SEQUENTIAL );
}
}
size_t size() const {
return size_;
}
~mapped_file() {
munmap( base_, size_ );
close( fd_ );
}
void *base() {
return base_;
}
private:
void *base_;
// bool read_write_;
int fd_;
off_t size_;
};
template<typename K>
uint64_t hash_value( const K &k ) {
return k.hash();
}
uint64_t hash_value( const std::string &s ) {
uint64_t hash = 5381;
for( size_t i = 0; i < s.size(); ++i ) {
hash = ((hash << 5) + hash) ^ s[i]; /* hash * 33 + c */
}
return hash;
}
uint64_t hash_value( const char *s ) {
uint64_t hash = 5381;
while( *s != 0 ) {
hash = ((hash << 5) + hash) ^ *s; /* hash * 33 + c */
++s;
}
return hash;
}
class hash_builder {
typedef int64_t int_type;
const static size_t int_size = sizeof(int_type);
public:
hash_builder( size_t table_size ) : table_size_(table_size), link_offsets_(table_size), num_extra_hops_(0) {
// link_offsets_ stores the 'next pointers' per bucket. Initially they point to
// entries in the hash table.
for( size_t i = 0; i < table_size_; ++i ) {
link_offsets_[i] = i * int_size;
}
// start appending after the hash table
append_pos_ = table_size_ * int_size;
}
void write( const char *filename ) {
{
std::ofstream os( filename );
os.seekp( append_pos_ );
int_type x = table_size_;
// write the table size at the end of the file
// this will also grow the file to the necessary size
os.write( (char*)(&x), sizeof(int_type));
}
//std::cout << "append: " << append_pos_ << "\n";
mapped_file mf( filename, true );
char *base = (char*) mf.base();
// write file headers (name, size) and contents
for( std::vector< std::pair< std::string, size_t > >::iterator it = file_dest_.begin(); it != file_dest_.end(); ++it ) {
// write name (0 terminated)
const std::string &name = it->first;
char *append_pos = base + it->second;
// std::cout << "append_pos " << it->second << "\n";
std::copy( name.begin(), name.end(), append_pos );
append_pos += name.size();
*append_pos = 0; ++append_pos;
// write file size
std::ifstream is( it->first );
assert( is.good() );
is.seekg( 0, std::ios::end );
size_t size = is.tellg();
is.seekg( 0, std::ios::beg );
{
int_type x = size;
char *xcp = (char*)&x;
std::copy( xcp, xcp + int_size, append_pos );
}
append_pos += int_size;
// write file content
append_pos = align( append_pos );
is.read( append_pos, size );
}
// write bucket chains (i.e., the 'next pointers')
for( std::vector< std::pair< size_t, size_t > >::iterator it = ptrs_.begin(); it != ptrs_.end(); ++it ) {
int_type x = it->second;
char *xcp = (char*)&x;
std::copy( xcp, xcp + int_size, base + it->first );
}
std::cout << "fill factor: " << ptrs_.size() / float(table_size_) << "\n";
std::cout << "avg hops: " << (ptrs_.size() + num_extra_hops_) / float( ptrs_.size()) << "\n";
}
bool add( const char *filename ) {
std::streampos size = file_size( filename );
if( size > 0xffffffff ) { // WTF? tellg should return -1, instead it returns '-1 with sign bit set to zero...'
std::cout << "ignore " << filename << "\n";
return false;
}
uint64_t name_hash = hash_value( filename );
size_t bucket = name_hash % table_size_;
// remember current 'next pointer' for writing later
ptrs_.push_back( std::make_pair( link_offsets_[bucket], append_pos_ ) );
// statistics: count as 'extra hop' if link_offset_[bucket] does not point into hash table (i.e., the bucket is non-empty)
if( link_offsets_[bucket] >= table_size_ * int_size ) {
++num_extra_hops_;
}
// update 'next pointer'
link_offsets_[bucket] = append_pos_;
// remember file content location (starts after the chaining link)
size_t file_pos = append_pos_ + int_size;
file_dest_.push_back( std::make_pair( std::string(filename), file_pos ));
file_pos += strlen(filename) + 1 + 8;
file_pos = align( file_pos );
// std::cout << "file pos: " << file_pos << "\n";
append_pos_ = file_pos + size;
return true;
}
template<typename T>
static T align( T v ) {
const size_t a = 32;
return v + (a - (size_t(v) % a)) % a;
}
private:
std::streampos file_size( const char *filename ) {
std::ifstream is( filename );
assert( is.good() );
is.seekg( 0, std::ios_base::end );
return is.tellg();
}
const size_t table_size_;
std::vector<size_t> link_offsets_;
std::vector<std::pair<std::string,size_t>> file_dest_;
std::vector<std::pair<size_t,size_t>> ptrs_;
size_t append_pos_;
size_t num_extra_hops_;
};
class disk_hash {
public:
typedef int64_t int_type;
const static size_t int_size = sizeof(int_type);
disk_hash( const char *filename ) : mf_(filename, false) {
base_ = (char *)mf_.base();
size_t s = mf_.size();
assert( s > int_size);
int_type ts = get_int( base_ + s - int_size );
std::cout << "table size: " << ts << "\n";
table_size_ = ts;
assert( base_ == hash_builder::align(base_));
}
char *find( const char *filename, size_t &out_size ) {
size_t name_hash = hash_value( filename );
size_t bucket = name_hash % table_size_;
// start offset of hash chain
size_t offset = get_int( base_ + bucket * int_size );
size_t chain_len = 0;
// traverse current hash chain
while( offset != 0 ) {
validate_offset( offset );
const size_t next_offset = get_int( base_ + offset );
offset += int_size;
if( strncmp( filename, base_ + offset, remaining_size( offset ) ) == 0 ) {
offset += strlen( filename ) + 1;
size_t size = get_int( base_ + offset );
offset += int_size;
offset = hash_builder::align(offset);
// std::cout << "found: " << filename << " chunk size: " << size << " at " << std::hex << offset << std::dec << " chain: " << chain_len << "\n";
if( remaining_size( offset ) < size ) {
throw std::runtime_error( "internal error. premature end of hash file." );
}
out_size = size;
return base_ + offset;
}
++chain_len;
offset = next_offset;
}
// fell through: name not found
out_size = size_t(-1);
return 0;
}
private:
void validate_offset( size_t ofs ) {
if( ofs > mf_.size() - int_size ) {
throw std::runtime_error( "internal error. offset out of range." );
}
}
size_t remaining_size( size_t offset ) {
if( offset > mf_.size() ) {
throw std::runtime_error( "internal error. offset out of range 2." );
}
return mf_.size() - offset;
}
int_type get_int( char *ptr ) {
int_type x;
char *xcp = (char*)&x;
std::copy( ptr, ptr + int_size, xcp );
return x;
}
size_t table_size_;
mapped_file mf_;
char *base_;
};
#endif
int main() {
//const size_t ht_size = 1024 * 32;
const size_t ht_size = 1024 * 8;
if( !false )
{
meh::file_based_builder hb( ht_size );
{
std::ifstream is( "files.txt" );
while( is.good() ) {
std::string name;
is >> name;
// std::cout << "name: " << name << "\n";
if( !name.empty() ) {
std::ifstream ts( name.c_str() );
if( ts.good() ) {
hb.add( name.c_str() );
}
}
}
}
hb.write( "hash.bin" );
hb.print_stats();
}
size_t nfound = 0;
meh::hash dh( "hash.bin" );
// while(true)
{
std::ifstream is( "files.txt" );
while( is.good() ) {
std::string name;
is >> name;
// std::cout << "name: " << name << "\n";
if( !name.empty() ) {
size_t size;
char *data = dh.find( name.c_str(), size );
if( data != 0 ) {
nfound++;
}
}
}
std::cout << "found: " << nfound << "\n";
// dh.mf().unmap();
// getchar();
// dh.mf().map();
}
// mapped_file mf( "test.bin", false );
}