Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
File_Hasher.cpp
Go to the documentation of this file.
4
5#include <vector>
6
7namespace joedb
8{
9 ////////////////////////////////////////////////////////////////////////////
11 ////////////////////////////////////////////////////////////////////////////
12 (
13 Buffered_File &file,
14 int64_t start,
15 int64_t size
16 )
17 {
18 SHA_256 sha_256;
19 int64_t old_position = file.get_position();
20 file.set_position(start);
21
22 constexpr uint32_t chunks = 2048;
23 std::vector<char> hashing_buffer(SHA_256::chunk_size * chunks);
24
25 int64_t current_size = 0;
26
27 while (true)
28 {
29 size_t block_size = SHA_256::chunk_size * chunks;
30 if (current_size + int64_t(block_size) > size)
31 block_size = size_t(size - current_size);
32
33 const size_t read_count = file.read_data(&hashing_buffer[0], block_size);
34 current_size += int64_t(read_count);
35 const uint32_t full_chunks = uint32_t(read_count / SHA_256::chunk_size);
36 for (uint32_t i = 0; i < full_chunks; i++)
37 sha_256.process_chunk(&hashing_buffer[i * SHA_256::chunk_size]);
38
39 const uint32_t remainder = uint32_t(read_count % SHA_256::chunk_size);
40 if (remainder || current_size >= size || read_count == 0)
41 {
43 (
44 &hashing_buffer[full_chunks * SHA_256::chunk_size],
45 uint64_t(current_size)
46 );
47 break;
48 }
49 }
50
51 file.set_position(old_position);
52 return sha_256.get_hash();
53 }
54
55 ////////////////////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////////////////////
58 {
59 file.flush(); // necessary, to get correct file.get_size() on next line
60 return File_Hasher::get_hash(file, 0, file.get_size());
61 }
62
63 ////////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////////
66 {
67 Readonly_Memory_File file(s.data(), s.size());
68 return File_Hasher::get_hash(file);
69 }
70
71 ////////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////////
74 (
75 Buffered_File &file,
76 int64_t start,
77 int64_t size
78 )
79 {
80 constexpr int buffer_count = 256;
81
82 if (size < 4 * file.buffer.ssize * buffer_count)
83 return get_hash(file, start, size);
84
85 SHA_256 sha_256;
86 const int64_t old_position = file.get_position();
87 file.flush();
88
89 for (int i = 0; i < buffer_count; i++)
90 {
91 int64_t buffer_position;
92
93 if (i == 0)
94 buffer_position = start;
95 else if (i == buffer_count - 1)
96 buffer_position = start + size - file.buffer.ssize;
97 else
98 {
99 buffer_position = file.buffer.ssize *
100 (
101 (start + i * size) / (file.buffer.ssize * (buffer_count - 1))
102 );
103 }
104
105 file.pread(file.buffer.data, file.buffer.size, buffer_position);
106
107 for (size_t j = 0; j < file.buffer.size; j += SHA_256::chunk_size)
108 sha_256.process_chunk(file.buffer.data + j);
109 }
110
111 file.set_position(old_position);
112 return sha_256.get_hash();
113 }
114
115 ////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////
118 (
119 const Readonly_Journal &journal,
120 int64_t checkpoint
121 )
122 {
124 (
125 journal.file,
126 journal.header_size,
127 checkpoint - journal.header_size
128 );
129 }
130}
virtual int64_t get_size() const
static SHA_256::Hash get_fast_hash(Buffered_File &file, int64_t start, int64_t size)
static SHA_256::Hash get_hash(Buffered_File &file, int64_t start, int64_t size)
static SHA_256::Hash get_hash(const Readonly_Journal &journal, int64_t checkpoint)
const Hash & get_hash() const
Definition SHA_256.h:67
void process_chunk(const char *data)
process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
Definition SHA_256.h:71
std::array< uint32_t, 8 > Hash
Definition SHA_256.h:60
static constexpr size_t chunk_size
Definition SHA_256.h:68
void process_final_chunk(const char *const data, const uint64_t total_length_in_bytes)
process last bytes of the sequence
Definition SHA_256.h:126
Definition Blob.h:7