data_store.h
1.71 KB
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
#ifndef DATA_STORE_H
#define DATA_STORE_H
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include "../multithread/buffer_producer.h"
#include "data_basic.h"
#include "../constant.h"
namespace data{
class Data_Store {
protected:
int size=0;
bool write_on_add;
std::string file_name;
std::ofstream output;
virtual void add_internal(data::Data_basic *data)=0;
public:
Data_Store():write_on_add(false){}
Data_Store(std::string file_name):
write_on_add(true),
file_name(file_name)
{
this->output.open(this->file_name,std::ios::trunc);
}
virtual ~Data_Store(){
this->output.close();
}
int getSize(){return this->size;}
void add(data::Data_basic *data){
this->add_internal(data);
if(this->write_on_add){
std::string tmp = data->to_csv();
this->output << tmp << std::endl;
}
}
virtual multithread::Buffer<data::Data_basic*>* get(std::string pattern)=0;
// void write(std::string file_name, std::string pattern){
// multithread::Buffer<data::Data_basic*> *buf;
// std::string towrite;
// std::ofstream output;
// output.open(file_name, std::ios::out | std::ios::trunc);
// buf = this->get(pattern);
// int count_buf(0);
// while(buf->available()){
// if(!buf->isEmpty()){
// data::Data_basic* tmp = buf->pop();
// towrite += tmp->to_csv()+"\n";
// count_buf ++;
// delete tmp;
// }else{
// std::this_thread::sleep_for(THREAD_SLEEP);
// }
// if(count_buf == NB_ELT_BUF){
// output << towrite;
// towrite.clear();
// count_buf=0;
// }
// }
// if(count_buf>0){
// output << towrite;
// }
// output.close();
// }
};
}
#endif // DATA_STORE_H