forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexedStorage.cpp
169 lines (140 loc) · 3.58 KB
/
IndexedStorage.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
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Repository at: https://github.com/djeedjay/DebugViewPP/
#include "stdafx.h"
#include <vector>
#include "IndexedStorageLib/IndexedStorage.h"
#include "snappy.h"
namespace fusion {
namespace indexedstorage {
const int blockSize = 400;
bool VectorStorage::Empty() const
{
return m_storage.empty();
}
void VectorStorage::Clear()
{
m_storage.clear();
m_storage.shrink_to_fit();
}
size_t VectorStorage::Add(const std::string& value)
{
m_storage.push_back(value);
return m_storage.size() - 1;
}
size_t VectorStorage::Count() const
{
return m_storage.size();
}
std::string VectorStorage::operator[](size_t i) const
{
return m_storage[i];
}
void VectorStorage::shrink_to_fit()
{
m_storage.shrink_to_fit();
}
SnappyStorage::SnappyStorage() :
m_writeBlockIndex(0),
m_readBlockIndex(-1)
{
}
bool SnappyStorage::Empty() const
{
return m_storage.empty();
}
void SnappyStorage::Clear()
{
m_storage.clear();
m_storage.shrink_to_fit();
m_readList.clear();
m_readList.shrink_to_fit();
m_readBlockIndex = -1;
m_writeList.clear();
m_writeList.shrink_to_fit();
m_writeBlockIndex = 0;
}
size_t SnappyStorage::Add(const std::string& value)
{
auto id = m_writeList.size();
m_writeList.push_back(value);
auto result = m_writeBlockIndex * blockSize + id;
if (id == blockSize - 1)
{
std::string test = Compress(m_writeList);
m_storage.push_back(test);
m_writeList.clear();
++m_writeBlockIndex;
}
return result;
}
size_t SnappyStorage::Count() const
{
return m_storage.size();
}
std::string SnappyStorage::operator[](size_t i)
{
return GetString(i);
}
size_t SnappyStorage::GetBlockIndex(size_t index)
{
return index / blockSize;
}
size_t SnappyStorage::GetRelativeIndex(size_t index)
{
return index % blockSize;
}
std::string SnappyStorage::GetString(size_t index)
{
auto blockId = GetBlockIndex(index);
auto id = GetRelativeIndex(index);
if (blockId == m_writeBlockIndex)
{
return m_writeList[id];
}
if (blockId != m_readBlockIndex)
{
m_readList = Decompress(m_storage[blockId]);
m_readBlockIndex = blockId;
}
return m_readList[id];
}
std::string SnappyStorage::Compress(const std::vector<std::string>& value) const
{
std::vector<char> raw;
for (auto& s : value)
{
for (auto t : s)
{
raw.push_back(t);
}
raw.push_back('\0');
}
std::string data;
snappy::Compress(&raw[0], raw.size(), &data);
return data;
}
std::vector<std::string> SnappyStorage::Decompress(const std::string& value)
{
std::vector<std::string> vec;
std::string data;
snappy::Uncompress(value.c_str(), value.size(), &data);
for (auto it = data.begin(); it != data.end(); ++it)
{
auto begin = it;
while (*it)
++it; // cppcheck : Message: The iterator incrementing is suspicious - it is incremented at line 150 and then at line 146. The loop might unintentionally skip an element in the container. There is no comparison between these increments to prevent that the iterator is incremented beyond the end.
vec.emplace_back(begin, it);
}
return vec;
}
void SnappyStorage::shrink_to_fit()
{
m_readList.shrink_to_fit();
m_writeList.shrink_to_fit();
m_storage.shrink_to_fit();
}
} // namespace indexedstorage
} // namespace fusion