forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestore.hpp
318 lines (264 loc) · 7.52 KB
/
filestore.hpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// solid/frame/file/filestore.hpp
//
// Copyright (c) 2013 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef SOLID_FRAME_FILE_STORE_HPP
#define SOLID_FRAME_FILE_STORE_HPP
#include "solid/frame/sharedstore.hpp"
#include "solid/frame/file/tempbase.hpp"
#include "solid/system/filedevice.hpp"
namespace solid{
namespace frame{
class Manager;
namespace file{
uint32_t dbgid();
enum{
MemoryLevelFlag = 1,
VeryFastLevelFlag = 2,
FastLevelFlag = 4,
NormalLevelFlag = 8,
SlowLevelFlag = 16,
AllLevelsFlag = 1 + 2 + 4 + 8 + 16,
};
enum TempRemoveMode{
RemoveAfterCreateE,
RemoveAfterCloseE,
RemoveNeverE
};
struct TempConfiguration{
struct Storage{
Storage():level(0), capacity(0), minsize(0), maxsize(0), removemode(RemoveAfterCreateE){}
std::string path;
uint32_t level;
uint64_t capacity;
uint64_t minsize;
uint64_t maxsize;
TempRemoveMode removemode;
};
typedef std::vector<Storage> StorageVectorT;
StorageVectorT storagevec;
};
struct Utf8Controller;
struct File{
File():ptmp(nullptr){}
void clear(){
fd.close();
delete ptmp;
ptmp = nullptr;
}
bool open(const char *_path, const size_t _openflags){
return fd.open(_path, _openflags);
}
//We only offer offset based io because in case of shared use
//the file offset will be kept by streams
int read(char *_pb, uint32_t _bl, int64_t _off){
if(!ptmp){
return fd.read(_pb, _bl, _off);
}else{
return ptmp->read(_pb, _bl, _off);
}
}
int write(const char *_pb, uint32_t _bl, int64_t _off){
if(!ptmp){
return fd.write(_pb, _bl, _off);
}else{
return ptmp->write(_pb, _bl, _off);
}
}
int64_t size()const{
if(!ptmp){
return fd.size();
}else{
return ptmp->size();
}
}
bool truncate(int64_t _len = 0){
if(!ptmp){
return fd.truncate(_len);
}else{
return ptmp->truncate(_len);
}
}
int64_t capacity()const{
if(!ptmp){
return -1;
}else{
return ptmp->tempsize;
}
}
void flush(){
if(!ptmp){
if(fd.ok()){
fd.flush();
}
}else{
ptmp->flush();
}
}
bool isTemp()const{
return ptmp != nullptr;
}
TempBase* temp()const{
return ptmp;
}
private:
friend struct Utf8Controller;
FileDevice fd;
TempBase *ptmp;
};
typedef shared::Pointer<File> FilePointerT;
template <class S, class Cmd, class Base>
struct OpenCommand: Base{
Cmd cmd;
OpenCommand(
Cmd const &_cmd, typename Base::PathT _path, size_t _openflags
):Base(_path, _openflags), cmd(_cmd){}
void operator()(S &_rstore, FilePointerT &_rptr, ErrorCodeT _err){
if(!_err){
Base::openFile(_rstore, _rptr, _err);
}
cmd(_rstore, _rptr, _err);
}
};
struct CreateTempCommandBase{
size_t openflags;
uint64_t size;
size_t storageid;
protected:
CreateTempCommandBase(
uint64_t _sz, size_t _openflags
):openflags(_openflags), size(_sz), storageid(InvalidIndex()){}
void openTemp(Utf8Controller &_rstore, FilePointerT &_rptr, ErrorCodeT &_rerr);
};
template <class S, class Cmd>
struct CreateTempCommand: CreateTempCommandBase{
Cmd cmd;
CreateTempCommand(
Cmd const &_cmd, uint64_t _sz, size_t _createflags
):CreateTempCommandBase(_sz, _createflags), cmd(_cmd){}
void operator()(S &_rstore, FilePointerT &_rptr, ErrorCodeT _err){
CreateTempCommandBase::openTemp(_rstore, _rptr, _err);
cmd(_rstore, _rptr, _err);
}
};
struct Utf8Configuration{
struct Storage{
Storage(){}
Storage(
std::string const &_globalprefix,
std::string const &_localprefix
):globalprefix(_globalprefix), localprefix(_localprefix){}
std::string globalprefix;
std::string localprefix;
};
typedef std::vector<Storage> StorageVectorT;
StorageVectorT storagevec;
};
struct Utf8OpenCommandBase;
struct Utf8Controller{
typedef Utf8OpenCommandBase OpenCommandBaseT;
Utf8Controller(const Utf8Configuration &_rfilecfg, const TempConfiguration &_rtempcfg);
~Utf8Controller();
//NOTE: it would have been nice to have all the below methods as protected
bool clear(shared::StoreBase::Accessor &_rsbacc, File &_rf, const size_t _idx);
bool executeBeforeErase(shared::StoreBase::Accessor &_rsbacc);
void executeOnSignal(shared::StoreBase::Accessor &_rsbacc, ulong _sm);
bool prepareIndex(
shared::StoreBase::Accessor &_rsbacc, Utf8OpenCommandBase &_rcmd,
size_t &_ridx, size_t &_rflags, ErrorCodeT &_rerr
);
bool preparePointer(
shared::StoreBase::Accessor &_rsbacc, Utf8OpenCommandBase &_rcmd,
FilePointerT &_rptr, size_t &_rflags, ErrorCodeT &_rerr
);
bool prepareIndex(
shared::StoreBase::Accessor &_rsbacc, CreateTempCommandBase &_rcmd,
size_t &_ridx, size_t &_rflags, ErrorCodeT &_rerr
);
bool preparePointer(
shared::StoreBase::Accessor &_rsbacc, CreateTempCommandBase &_rcmd,
FilePointerT &_rptr, size_t &_rflags, ErrorCodeT &_rerr
);
private:
friend struct CreateTempCommandBase;
friend struct Utf8OpenCommandBase;
void openFile(Utf8OpenCommandBase &_rcmd, FilePointerT &_rptr, ErrorCodeT &_rerr);
void openTemp(CreateTempCommandBase &_rcmd, FilePointerT &_rptr, ErrorCodeT &_rerr);
void doPrepareOpenTemp(File &_rf, uint64_t _sz, const size_t _storeid);
void doCloseTemp(TempBase &_rtemp);
void doDeliverTemp(shared::StoreBase::Accessor &_rsbacc, const size_t _storeid);
private:
struct Data;
Data &d;
};
struct Utf8PathStub{
size_t storeidx;
std::string path;
size_t idx;
};
struct Utf8OpenCommandBase{
typedef std::string const& PathT;
Utf8PathStub outpath;
std::string const &inpath;
size_t openflags;
Utf8OpenCommandBase(
std::string const &_inpath, size_t _openflags
):inpath(_inpath), openflags(_openflags){}
void openFile(Utf8Controller &_rstore, FilePointerT &_rptr, ErrorCodeT &_rerr);
};
template <class Base = Utf8Controller>
class Store: public shared::Store<File, Store<Base> >, public Base{
typedef Base BaseT;
typedef shared::Store<File, Store<Base> > StoreT;
typedef Store<Base> ThisT;
public:
template <typename C>
Store(
Manager &_rm,
C _c
): StoreT(_rm), BaseT(_c){}
template <typename C1, typename C2>
Store(
Manager &_rm,
C1 _c1, C2 _c2
): StoreT(_rm), BaseT(_c1, _c2){}
//If a file with _path already exists in the store, the call will be similar with open with truncate openflag
template <class Cmd>
bool requestCreateFile(Cmd const& _cmd, std::string const &_path, const size_t _openflags = 0, const size_t _flags = 0){
OpenCommand<
ThisT, Cmd,
typename BaseT::OpenCommandBaseT
> cmd(_cmd, _path, _openflags | FileDevice::CreateE | FileDevice::TruncateE);
return StoreT::requestReinit(cmd, _flags);
}
template <class Cmd>
bool requestOpenFile(Cmd const& _cmd, std::string const &_path, const size_t _openflags = 0, const size_t _flags = 0){
OpenCommand<
ThisT, Cmd,
typename BaseT::OpenCommandBaseT
> cmd(_cmd, _path, _openflags);
return StoreT::requestReinit(cmd, _flags);
}
template <class Cmd>
bool requestCreateTemp(
Cmd const &_cmd, uint64_t _sz, const size_t _createflags = AllLevelsFlag, const size_t _flags = 0
){
CreateTempCommand<ThisT, Cmd> cmd(_cmd, _sz, _createflags);
return StoreT::requestReinit(cmd, _flags);
}
};
inline void Utf8OpenCommandBase::openFile(Utf8Controller &_rstore, FilePointerT &_rptr, ErrorCodeT &_rerr){
_rstore.openFile(*this, _rptr, _rerr);
}
inline void CreateTempCommandBase::openTemp(Utf8Controller &_rstore, FilePointerT &_rptr, ErrorCodeT &_rerr){
_rstore.openTemp(*this, _rptr, _rerr);
}
}//namespace file
}//namespace frame
}//namespace solid
#endif