@@ -20,13 +20,38 @@ along with this program. If not, see http://www.gnu.org/licenses.
20
20
#include < sys/stat.h>
21
21
#include < iostream>
22
22
#include < cstdio>
23
+ #include < sstream>
23
24
24
25
#include " common.h"
25
26
#include " backing_store.h"
26
27
27
28
using FFLAG = std::ios_base;
28
29
using std::string;
29
30
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
+
30
55
bool
31
56
BackingStore::directory_exists ( const string& dir_path )
32
57
{
@@ -159,65 +184,80 @@ BackingStore::delete_symbol_store( const string& symbol )
159
184
}
160
185
161
186
162
- bool
187
+ // {success, front elems pushed, back elems pushed}
188
+ std::tuple<bool , unsigned long long , unsigned long long >
163
189
BackingStore::read_from_symbol_store ( const string& symbol,
164
190
fileio_func_ty read_func_front,
165
191
fileio_func_ty read_func_back )
166
192
{
167
193
auto f = _stores.find (symbol);
168
194
if ( f == _stores.end () ){
169
195
log_error (" BACKING-STORE" , " symbol store doesn't exist" , symbol);
170
- return false ;
196
+ return std::make_tuple ( false , 0 , 0 ) ;
171
197
}
172
198
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;
179
201
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 );
189
205
190
206
if ( f->second .front .file ->eof () )
191
207
f->second .front .file ->clear ();
192
208
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
+ );
194
225
}
195
226
196
227
197
- bool
228
+
229
+ // {success, front elems pulled, back elems pulled}
230
+ std::tuple<bool , unsigned long long , unsigned long long >
198
231
BackingStore::write_to_symbol_store ( const string& symbol,
199
232
fileio_func_ty write_func_front,
200
233
fileio_func_ty write_func_back )
201
234
{
202
235
auto f = _stores.find (symbol);
203
236
if ( f == _stores.end () ){
204
237
log_error (" BACKING-STORE" , " symbol store doesn't exist" , symbol);
205
- return false ;
238
+ return std::make_tuple ( false , 0 , 0 ) ;
206
239
}
207
240
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;
213
243
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);
219
247
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
+ );
221
261
}
222
262
223
263
@@ -286,49 +326,52 @@ BackingStore::_write_index( const string& symbol )
286
326
return true ;
287
327
}
288
328
289
- int
329
+
330
+ // {success, lines read, elems pushed}
331
+ std::tuple<bool , long long , long long >
290
332
BackingStore::_read_store ( SymbolStore::Side& side, fileio_func_ty read_func )
291
333
{
292
334
side.file ->seekp (0 , FFLAG::end);
293
335
if ( side.file ->fail () ){
294
336
log_error (" FILE" , " seekp to end of file failed" , side.path );
295
- return - 1 ;
337
+ return std::make_tuple ( false , - 1 , - 1 ) ;
296
338
}
297
339
298
340
if ( side.file ->tellp () == std::fstream::pos_type (0 ) )
299
- return 0 ;
341
+ return std::make_tuple ( true , 0 , 0 ) ;
300
342
301
343
side.file ->seekg (0 , FFLAG::beg);
302
344
if ( side.file ->fail () ){
303
345
log_error (" FILE" , " seek to beginning of file failed" , side.path );
304
- return - 1 ;
346
+ return std::make_tuple ( false , - 1 , - 1 ) ;
305
347
}
306
348
307
- int i = read_func ( *(side.file ) );
349
+ auto p = read_func ( *(side.file ) );
350
+ bool success = false ;
308
351
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 ;
317
358
318
- return 0 ;
359
+ return std::make_tuple (success, p. first , p. second ) ;
319
360
}
320
361
321
- int
362
+
363
+ // {success, lines written, elems pulled}
364
+ std::tuple<bool , long long , long long >
322
365
BackingStore::_write_store ( SymbolStore::Side& side, fileio_func_ty write_func )
323
366
{
324
- int i = write_func ( *(side.file ) );
367
+ auto p = write_func ( *(side.file ) );
325
368
if ( !(*side.file ) ){
326
369
log_error (" FILE" , " symbol store write failed" , side.path );
327
- return i ;
370
+ return std::make_tuple ( false , p. first , p. second ) ;
328
371
}
329
372
330
373
log_info (" FILE" , " symbol store write succeeded" , side.path );
331
- return 0 ;
374
+ return std::make_tuple ( true , p. first , p. second ) ;
332
375
}
333
376
334
377
0 commit comments