libdballe  7.7
serializer.h
1 /*
2  * memdb/serializer - Read/write memdb contents to disk
3  *
4  * Copyright (C) 2013--2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_MEMDB_SERIALIZER_H
23 #define DBA_MEMDB_SERIALIZER_H
24 
25 #include <dballe/memdb/station.h>
26 #include <dballe/memdb/stationvalue.h>
27 #include <dballe/memdb/levtr.h>
28 #include <dballe/memdb/value.h>
29 #include <dballe/core/csv.h>
30 #include <map>
31 #include <cstdio>
32 
33 namespace dballe {
34 struct Memdb;
35 
36 namespace memdb {
37 namespace serialize {
38 
43 {
44  std::string pathname;
45  std::string pathname_tmp;
46  FILE* fd;
47 
48  CSVOutfile();
49  CSVOutfile(const std::string& pathname);
50  ~CSVOutfile();
51 
52  void open(const std::string& pathname);
53 
58  void commit();
59 
64  void rollback();
65 
66  virtual void flush_row();
67 
68 private:
69  CSVOutfile(const CSVOutfile&);
70  CSVOutfile& operator=(const CSVOutfile&);
71 };
72 
76 struct CSVWriter
77 {
78  CSVOutfile out_station;
79  CSVOutfile out_stationvalue;
80  CSVOutfile out_stationvalue_attr;
81  CSVOutfile out_value;
82  CSVOutfile out_value_attr;
83 
84  CSVWriter(const std::string& dir);
85 
86  void write(const Memdb& memdb);
87 
88  void commit();
89 };
90 
96 {
97 public:
98  std::string pathname;
99 
100 public:
101  CSVInfile();
102  CSVInfile(const std::string& pathname);
103 
105  void open(const std::string& pathname);
106 
107  template<typename INFILE>
108  void read_attrs(const INFILE& values);
109 
110 private:
111  CSVInfile(const CSVInfile&);
112  CSVInfile& operator=(const CSVInfile&);
113 };
114 
120 template<typename VALUES>
122 {
123  typedef typename VALUES::value_type value_type;
124 
125  VALUES& values;
126  // Map 0-based line numbers to IDs in values
127  std::map<size_t, size_t> id_map;
128 
129  CSVValueStorageInfile(VALUES& values) : values(values) {}
130  const typename VALUES::value_type& by_lineno(size_t lineno) const;
131 };
132 
133 struct CSVStationsInfile : public CSVValueStorageInfile<memdb::Stations>
134 {
136 
137  void read();
138 };
139 
140 struct CSVStationValuesInfile : public CSVValueStorageInfile<memdb::StationValues>
141 {
143 
144  void read(const CSVStationsInfile& stations);
145 };
146 
147 struct CSVValuesInfile : public CSVValueStorageInfile<memdb::Values>
148 {
149  Memdb& memdb;
150  CSVValuesInfile(Memdb& memdb);
151 
152  void read(const CSVStationsInfile& stations);
153 };
154 
158 struct CSVReader
159 {
160  Memdb& memdb;
161 
162  CSVStationsInfile in_station;
163  CSVStationValuesInfile in_stationvalue;
164  CSVInfile in_stationvalue_attr;
165  CSVValuesInfile in_value;
166  CSVInfile in_value_attr;
167 
168  CSVReader(const std::string& dir, Memdb& memdb);
169 
170  void read();
171 };
172 
173 }
174 }
175 }
176 
177 #endif
178 
179 
Definition: csv.h:135
void rollback()
Roll back the write, deleting the output temporary file and leaving the original untouched.
In-memory database backend.
Definition: memdb.h:78
void commit()
Commit the write, closing the output temporary file and renaming it to its final name.
Storage and index for station values.
Definition: stationvalue.h:41
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: serializer.h:147
Deserializer for Memdb.
Definition: serializer.h:158
Storage and index for station information.
Definition: memdb/station.h:62
Routines to parse data in CSV format.
virtual void flush_row()
Write the current line to the output file, and start a new one.
CSVReader implementation that reads using stdio and has helper functions to deserialize Memdb...
Definition: serializer.h:95
CSVWriter implementation that writes its output file atomically.
Definition: serializer.h:42
Serializer for Memdb contents.
Definition: serializer.h:76
Definition: csv.h:51
Common implementation bits of ValueStorage deserializers.
Definition: serializer.h:121