2
2
3
3
#include " Exceptions.hpp"
4
4
#include " Units.hpp"
5
- // -------------------------------------------------------------------------------------
6
- // -------------------------------------------------------------------------------------
5
+
7
6
#include < fcntl.h>
8
7
#include < sys/stat.h>
9
8
#include < unistd.h>
13
12
#include < cmath>
14
13
#include < sstream>
15
14
#include < thread>
16
- // -------------------------------------------------------------------------------------
15
+
17
16
using namespace std ;
18
- // -------------------------------------------------------------------------------------
17
+
19
18
namespace leanstore {
20
19
namespace utils {
21
- // -------------------------------------------------------------------------------------
20
+
21
+ // WARNING: this only works with 4 byte types
22
22
template <class T >
23
- bool createTestFileImpl (
24
- const string& file_name, uint64_t count,
25
- function<T(int )> factory) // WARNING: this only works with 4 byte types
26
- {
23
+ bool createTestFileImpl (const string& file_name, uint64_t count,
24
+ function<T(int )> factory) {
27
25
// Open file
28
26
ofstream of (file_name, ios::binary);
29
27
if (!of.is_open () || !of.good ())
@@ -46,7 +44,7 @@ bool createTestFileImpl(
46
44
of.close ();
47
45
return of.good ();
48
46
}
49
- // -------------------------------------------------------------------------------------
47
+
50
48
template <class T >
51
49
bool foreachInFileImpl (const string& file_name, function<void (T)> callback) {
52
50
// Open file
@@ -64,20 +62,20 @@ bool foreachInFileImpl(const string& file_name, function<void(T)> callback) {
64
62
}
65
63
return true ;
66
64
}
67
- // -------------------------------------------------------------------------------------
65
+
68
66
bool CreateTestFile (const string& file_name, uint64_t count,
69
67
function<int32_t (int32_t )> factory) {
70
68
return createTestFileImpl<int32_t >(file_name, count, factory);
71
69
}
72
- // -------------------------------------------------------------------------------------
70
+
73
71
bool ForeachInFile (const string& file_name, function<void (uint32_t )> callback) {
74
72
return foreachInFileImpl<uint32_t >(file_name, callback);
75
73
}
76
- // -------------------------------------------------------------------------------------
74
+
77
75
bool CreateDirectory (const string& directory_name) {
78
76
return mkdir (directory_name.c_str (), 0666 ) == 0 ;
79
77
}
80
- // -------------------------------------------------------------------------------------
78
+
81
79
bool CreateFile (const string& file_name, const uint64_t bytes) {
82
80
int file_fd = open (file_name.c_str (), O_CREAT | O_WRONLY, 0666 );
83
81
if (file_fd < 0 ) {
@@ -94,7 +92,7 @@ bool CreateFile(const string& file_name, const uint64_t bytes) {
94
92
95
93
return true ;
96
94
}
97
- // -------------------------------------------------------------------------------------
95
+
98
96
bool CreateFile (const string& file_name, const string& content) {
99
97
int file_fd = open (file_name.c_str (), O_CREAT | O_TRUNC | O_WRONLY, 0666 );
100
98
if (file_fd < 0 ) {
@@ -104,9 +102,11 @@ bool CreateFile(const string& file_name, const string& content) {
104
102
size_t written_bytes = write (file_fd, content.data (), content.size ());
105
103
return written_bytes == content.size ();
106
104
}
107
- // -------------------------------------------------------------------------------------
108
- void DeleteFile (const std::string& file_name) { remove (file_name.c_str ()); }
109
- // -------------------------------------------------------------------------------------
105
+
106
+ void DeleteFile (const std::string& file_name) {
107
+ remove (file_name.c_str ());
108
+ }
109
+
110
110
uint64_t GetFileLength (const string& file_name) {
111
111
int fileFD = open (file_name.c_str (), O_RDWR);
112
112
if (fileFD < 0 ) {
@@ -124,45 +124,45 @@ uint64_t GetFileLength(const string& file_name) {
124
124
close (fileFD);
125
125
return st.st_size ;
126
126
}
127
- // -------------------------------------------------------------------------------------
127
+
128
128
bool fileExists (const string& file_name) {
129
129
struct stat buffer;
130
130
bool exists = (stat (file_name.c_str (), &buffer) == 0 );
131
131
return exists && (buffer.st_mode & S_IFREG);
132
132
}
133
- // -------------------------------------------------------------------------------------
133
+
134
134
bool directoryExists (const string& file_name) {
135
135
struct stat buffer;
136
136
bool exists = (stat (file_name.c_str (), &buffer) == 0 );
137
137
return exists && (buffer.st_mode & S_IFDIR);
138
138
}
139
- // -------------------------------------------------------------------------------------
139
+
140
140
bool pathExists (const string& file_name) {
141
141
struct stat buffer;
142
142
bool exists = (stat (file_name.c_str (), &buffer) == 0 );
143
143
return exists;
144
144
}
145
- // -------------------------------------------------------------------------------------
145
+
146
146
string LoadFileToMemory (const string& file_name) {
147
147
uint64_t length = GetFileLength (file_name);
148
148
string data (length, ' a' );
149
149
ifstream in (file_name);
150
150
in.read (&data[0 ], length);
151
151
return data;
152
152
}
153
- // -------------------------------------------------------------------------------------
153
+
154
154
namespace {
155
- // -------------------------------------------------------------------------------------
155
+
156
156
uint64_t applyPrecision (uint64_t input, uint32_t precision) {
157
157
uint32_t digits = log10 (input) + 1 ;
158
158
if (digits <= precision)
159
159
return input;
160
160
uint32_t invalidDigits = pow (10 , digits - precision);
161
161
return (uint64_t )((double )input / invalidDigits + .5f ) * invalidDigits;
162
162
}
163
- // -------------------------------------------------------------------------------------
163
+
164
164
} // namespace
165
- // -------------------------------------------------------------------------------------
165
+
166
166
string FormatTime (chrono::nanoseconds ns, uint32_t precision) {
167
167
ostringstream os;
168
168
@@ -184,7 +184,7 @@ string FormatTime(chrono::nanoseconds ns, uint32_t precision) {
184
184
185
185
return os.str ();
186
186
}
187
- // -------------------------------------------------------------------------------------
187
+
188
188
void PinThread (int socket) {
189
189
#ifdef __linux__
190
190
// Doesn't work on OS X right now
@@ -202,7 +202,7 @@ void PinThread(int socket) {
202
202
#endif
203
203
(void )socket;
204
204
}
205
- // -------------------------------------------------------------------------------------
205
+
206
206
void RunMultithreaded (uint32_t thread_count, function<void (uint32_t )> foo) {
207
207
atomic<bool > start (false );
208
208
vector<unique_ptr<thread>> threads (thread_count);
@@ -218,7 +218,7 @@ void RunMultithreaded(uint32_t thread_count, function<void(uint32_t)> foo) {
218
218
iter->join ();
219
219
}
220
220
}
221
- // -------------------------------------------------------------------------------------
221
+
222
222
uint8_t * AlignedAlloc (uint64_t alignment, uint64_t size) {
223
223
void * result = nullptr ;
224
224
int error = posix_memalign (&result, alignment, size);
@@ -227,8 +227,9 @@ uint8_t* AlignedAlloc(uint64_t alignment, uint64_t size) {
227
227
}
228
228
return reinterpret_cast <uint8_t *>(result);
229
229
}
230
- // -------------------------------------------------------------------------------------
230
+
231
231
namespace {
232
+
232
233
array<char , 16 > NUM_TO_HEX{{' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' ,
233
234
' a' , ' b' , ' c' , ' d' , ' e' , ' f' }};
234
235
uint8_t HexToNum (char c) {
@@ -240,8 +241,9 @@ uint8_t HexToNum(char c) {
240
241
}
241
242
UNREACHABLE ();
242
243
}
244
+
243
245
} // namespace
244
- // -------------------------------------------------------------------------------------
246
+
245
247
const string DataToHex (const uint8_t * data, uint32_t len, bool spaces) {
246
248
string result;
247
249
for (uint32_t i = 0 ; i < len; i++) {
@@ -252,11 +254,11 @@ const string DataToHex(const uint8_t* data, uint32_t len, bool spaces) {
252
254
}
253
255
return result;
254
256
}
255
- // -------------------------------------------------------------------------------------
257
+
256
258
const string StringToHex (const string& str, bool spaces) {
257
259
return DataToHex ((const uint8_t *)str.data (), str.size (), spaces);
258
260
}
259
- // -------------------------------------------------------------------------------------
261
+
260
262
const vector<uint8_t > HexToData (const string& str, bool spaces) {
261
263
assert (spaces || str.size () % 2 == 0 );
262
264
@@ -269,7 +271,7 @@ const vector<uint8_t> HexToData(const string& str, bool spaces) {
269
271
270
272
return result;
271
273
}
272
- // -------------------------------------------------------------------------------------
274
+
273
275
const string HexToString (const string& str, bool spaces) {
274
276
assert (spaces || str.size () % 2 == 0 );
275
277
@@ -282,7 +284,6 @@ const string HexToString(const string& str, bool spaces) {
282
284
283
285
return result;
284
286
}
285
- // -------------------------------------------------------------------------------------
287
+
286
288
} // namespace utils
287
289
} // namespace leanstore
288
- // -------------------------------------------------------------------------------------
0 commit comments