Skip to content

Commit 9b3d1a3

Browse files
committed
improve load/store
1 parent 33ee78d commit 9b3d1a3

File tree

3 files changed

+171
-89
lines changed

3 files changed

+171
-89
lines changed

Diff for: DynamicDataStore/include/backing_store.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ along with this program. If not, see http://www.gnu.org/licenses.
2828

2929
class BackingStore {
3030
public:
31-
typedef std::function<int(std::fstream&)> fileio_func_ty;
31+
typedef std::function<std::pair<long long, long long>(std::fstream&)>
32+
fileio_func_ty;
33+
3234

3335
BackingStore( const std::string& directory_path );
3436

@@ -41,12 +43,14 @@ class BackingStore {
4143
bool
4244
add_symbol_store( const std::string& symbol );
4345

44-
bool
46+
// {success, front elems pushed, back elems pushed}
47+
std::tuple<bool, unsigned long long, unsigned long long>
4548
read_from_symbol_store( const std::string& symbol,
4649
fileio_func_ty read_func_front,
4750
fileio_func_ty read_func_back );
4851

49-
bool
52+
// {success, front elems pulled, back elems pulled}
53+
std::tuple<bool, unsigned long long, unsigned long long>
5054
write_to_symbol_store( const std::string& symbol,
5155
fileio_func_ty write_func_front,
5256
fileio_func_ty write_func_back );
@@ -85,10 +89,10 @@ class BackingStore {
8589
bool
8690
_add_store( const std::string& symbol );
8791

88-
int
92+
std::tuple<bool, long long, long long>
8993
_read_store( SymbolStore::Side& side, fileio_func_ty read_func );
9094

91-
int
95+
std::tuple<bool, long long, long long>
9296
_write_store( SymbolStore::Side& side, fileio_func_ty write_func );
9397
};
9498

Diff for: DynamicDataStore/src/backing_store.cpp

+92-49
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,38 @@ along with this program. If not, see http://www.gnu.org/licenses.
2020
#include <sys/stat.h>
2121
#include <iostream>
2222
#include <cstdio>
23+
#include <sstream>
2324

2425
#include "common.h"
2526
#include "backing_store.h"
2627

2728
using FFLAG = std::ios_base;
2829
using std::string;
2930

31+
namespace {
32+
33+
template<bool IsWrite, bool IsFront>
34+
void
35+
log_read_write( bool success,
36+
unsigned long long nlines,
37+
unsigned long long nelems,
38+
const std::string& symbol )
39+
{
40+
static const std::string TAG("BACKING-STORE");
41+
42+
std::stringstream ss;
43+
ss << (success ? "SUCCESS" : "FAILURE")
44+
<< (IsWrite ? " writing" : " reading")
45+
<< (IsFront ? " front" : " back")
46+
<< "; lines=" << nlines << ", elements=" << nelems;
47+
48+
success ? log_info(TAG, ss.str(), symbol)
49+
: log_error(TAG, ss.str() ,symbol);
50+
}
51+
52+
} /* namespace */
53+
54+
3055
bool
3156
BackingStore::directory_exists( const string& dir_path )
3257
{
@@ -159,65 +184,80 @@ BackingStore::delete_symbol_store( const string& symbol )
159184
}
160185

161186

162-
bool
187+
// {success, front elems pushed, back elems pushed}
188+
std::tuple<bool, unsigned long long, unsigned long long>
163189
BackingStore::read_from_symbol_store( const string& symbol,
164190
fileio_func_ty read_func_front,
165191
fileio_func_ty read_func_back )
166192
{
167193
auto f = _stores.find(symbol);
168194
if( f == _stores.end() ){
169195
log_error("BACKING-STORE", "symbol store doesn't exist", symbol);
170-
return false;
196+
return std::make_tuple(false, 0, 0);
171197
}
172198

173-
int result = _read_store( f->second.back, read_func_back );
174-
if( result ){
175-
string err = "failed to read (back) data for " + symbol + ", line";
176-
log_error( "BACKING-STORE", err, std::to_string(result) );
177-
return false;
178-
}
199+
long long nlines, nelems_front, nelems_back;
200+
bool result_front, result_back;
179201

180-
if( f->second.back.file->eof() )
181-
f->second.back.file->clear();
182-
183-
result = _read_store( f->second.front, read_func_front );
184-
if( result ){
185-
string err = "failed to read (front) data for " + symbol + ", line";
186-
log_error( "BACKING-STORE", err, std::to_string(result) );
187-
return false;
188-
}
202+
// FRONT
203+
std::tie(result_front, nlines, nelems_front) =
204+
_read_store( f->second.front, read_func_front );
189205

190206
if( f->second.front.file->eof() )
191207
f->second.front.file->clear();
192208

193-
return true;
209+
log_read_write<false, true>(result_front, nlines, nelems_front, symbol);
210+
211+
// BACK
212+
std::tie(result_back, nlines, nelems_back) =
213+
_read_store( f->second.back, read_func_back );
214+
215+
if( f->second.back.file->eof() )
216+
f->second.back.file->clear();
217+
218+
log_read_write<false, false>(result_back, nlines, nelems_back, symbol);
219+
220+
return std::make_tuple(
221+
result_front && result_back,
222+
static_cast<unsigned long long>(std::max(0LL, nelems_front)),
223+
static_cast<unsigned long long>(std::max(0LL, nelems_back))
224+
);
194225
}
195226

196227

197-
bool
228+
229+
// {success, front elems pulled, back elems pulled}
230+
std::tuple<bool, unsigned long long, unsigned long long>
198231
BackingStore::write_to_symbol_store( const string& symbol,
199232
fileio_func_ty write_func_front,
200233
fileio_func_ty write_func_back )
201234
{
202235
auto f = _stores.find(symbol);
203236
if( f == _stores.end() ){
204237
log_error("BACKING-STORE", "symbol store doesn't exist", symbol);
205-
return false;
238+
return std::make_tuple(false, 0, 0);
206239
}
207240

208-
int result1 = _write_store( f->second.back, write_func_back );
209-
if( result1 ){
210-
string err = "failed to write (back) data for " + symbol + ", line";
211-
log_error( "BACKING-STORE", err, std::to_string(result1) );
212-
}
241+
long long nlines, nelems_front, nelems_back;
242+
bool result_front, result_back;
213243

214-
int result2 = _write_store( f->second.front, write_func_front );
215-
if( result2 ){
216-
string err = "failed to write (front) data for " + symbol + ", line";
217-
log_error( "BACKING-STORE", err, std::to_string(result2) );
218-
}
244+
// FRONT
245+
std::tie(result_front, nlines, nelems_front)
246+
= _write_store(f->second.front, write_func_front);
219247

220-
return (result1 == 0 and result2 == 0);
248+
log_read_write<true, true>(result_front, nlines, nelems_front, symbol);
249+
250+
// BACK
251+
std::tie(result_back, nlines, nelems_back)
252+
= _write_store(f->second.back, write_func_back);
253+
254+
log_read_write<true, false>(result_back, nlines, nelems_back, symbol);
255+
256+
return std::make_tuple(
257+
result_front && result_back,
258+
static_cast<unsigned long long>(std::max(0LL, nelems_front)),
259+
static_cast<unsigned long long>(std::max(0LL, nelems_back))
260+
);
221261
}
222262

223263

@@ -286,49 +326,52 @@ BackingStore::_write_index( const string& symbol )
286326
return true;
287327
}
288328

289-
int
329+
330+
// {success, lines read, elems pushed}
331+
std::tuple<bool, long long, long long>
290332
BackingStore::_read_store( SymbolStore::Side& side, fileio_func_ty read_func )
291333
{
292334
side.file->seekp(0, FFLAG::end);
293335
if( side.file->fail() ){
294336
log_error("FILE", "seekp to end of file failed", side.path);
295-
return -1;
337+
return std::make_tuple(false, -1, -1);
296338
}
297339

298340
if( side.file->tellp() == std::fstream::pos_type(0) )
299-
return 0;
341+
return std::make_tuple(true, 0, 0);
300342

301343
side.file->seekg(0, FFLAG::beg);
302344
if( side.file->fail() ){
303345
log_error("FILE", "seek to beginning of file failed", side.path);
304-
return -1;
346+
return std::make_tuple(false, -1, -1);
305347
}
306348

307-
int i = read_func( *(side.file) );
349+
auto p = read_func( *(side.file) );
350+
bool success = false;
308351

309-
if( side.file->bad() ){
310-
log_error("FILE", "I/O error reading from .store file", side.path);
311-
return i;
312-
}else if( !side.file->eof() ){
313-
log_error("FILE", "failed to reach EOF reading from .store file",
314-
side.path);
315-
return i;
316-
}
352+
if( side.file->bad() )
353+
log_error("FILE", "I/O error reading from .store file", side.path);
354+
else if( !side.file->eof() )
355+
log_error("FILE", ".store file didn't reach EOF", side.path);
356+
else
357+
success = true;
317358

318-
return 0;
359+
return std::make_tuple(success, p.first, p.second);
319360
}
320361

321-
int
362+
363+
// {success, lines written, elems pulled}
364+
std::tuple<bool, long long, long long>
322365
BackingStore::_write_store( SymbolStore::Side& side, fileio_func_ty write_func )
323366
{
324-
int i = write_func( *(side.file) );
367+
auto p = write_func( *(side.file) );
325368
if( !(*side.file) ){
326369
log_error("FILE", "symbol store write failed", side.path);
327-
return i;
370+
return std::make_tuple(false, p.first, p.second);
328371
}
329372

330373
log_info("FILE", "symbol store write succeeded", side.path);
331-
return 0;
374+
return std::make_tuple(true, p.first, p.second);
332375
}
333376

334377

0 commit comments

Comments
 (0)