-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathrefobjects.h
372 lines (273 loc) · 9.98 KB
/
refobjects.h
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// eleveldb: Erlang Wrapper for LevelDB (http://code.google.com/p/leveldb/)
//
// Copyright (c) 2011-2015 Basho Technologies, Inc. All Rights Reserved.
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
// -------------------------------------------------------------------
#ifndef INCL_REFOBJECTS_H
#define INCL_REFOBJECTS_H
#include <stdint.h>
#include <sys/time.h>
#include <list>
#include "leveldb/db.h"
#include "leveldb/write_batch.h"
#include "leveldb/perf_count.h"
#include "util/refobject_base.h"
#define LEVELDB_PLATFORM_POSIX
#include "port/port.h"
#ifndef __WORK_RESULT_HPP
#include "work_result.hpp"
#endif
#ifndef ATOMS_H
#include "atoms.h"
#endif
namespace eleveldb {
/**
* Base class for any object that offers RefInc / RefDec interface
* Today the class' sole purpose is to provide eleveldb specific
* performance counters.
*/
class RefObject : public leveldb::RefObjectBase
{
public:
RefObject();
virtual ~RefObject();
private:
RefObject(const RefObject&); // nocopy
RefObject& operator=(const RefObject&); // nocopyassign
}; // class RefObject
/**
* Base class for any object that is managed as an Erlang reference
*/
class ErlRefObject : public RefObject
{
public:
// Pointer to "this" from within Erlang heap.
// First thread to clear "this" in Erlang heap
// owns the shutdown (Erlang or async C)
void * volatile * m_ErlangThisPtr;
leveldb::port::Mutex m_CloseMutex; //!< for condition wait
leveldb::port::CondVar m_CloseCond; //!< for notification of user's finish
private: // private to force use of GetCloseRequest()
// m_CloseRequested assumes m_CloseMutex held for writes
// 1 once InitiateCloseRequest starts,
// 2 other pointers to "this" released
// 3 final RefDec and destructor executing
volatile uint32_t m_CloseRequested;
public:
ErlRefObject();
virtual ~ErlRefObject();
virtual uint32_t RefDec();
virtual void Shutdown()=0;
bool ClaimCloseFromCThread();
void InitiateCloseRequest();
// memory fencing for reads
uint32_t GetCloseRequested()
{return(leveldb::add_and_fetch(&m_CloseRequested, (uint32_t)0));};
private:
ErlRefObject(const ErlRefObject&); // nocopy
ErlRefObject& operator=(const ErlRefObject&); // nocopyassign
}; // class RefObject
/**
* Class to manage access and counting of references
* to a reference object.
*/
template <class TargetT>
class ReferencePtr
{
TargetT * t;
public:
ReferencePtr()
: t(NULL)
{};
explicit ReferencePtr(TargetT *_t)
: t(_t)
{
if (NULL!=t)
t->RefInc();
}
ReferencePtr(const ReferencePtr &rhs)
{t=rhs.t; if (NULL!=t) t->RefInc();};
~ReferencePtr()
{
TargetT * temp_ptr(t);
t=NULL;
if (NULL!=temp_ptr)
temp_ptr->RefDec();
}
void assign(TargetT * _t)
{
if (_t!=t)
{
if (NULL!=t)
t->RefDec();
t=_t;
if (NULL!=t)
t->RefInc();
} // if
};
TargetT * get() {return(t);};
TargetT * operator->() {return(t);};
private:
ReferencePtr & operator=(const ReferencePtr & rhs); // no assignment
}; // ReferencePtr
/**
* Per database object. Created as erlang reference.
*
* Extra reference count created upon initialization, released on close.
*/
class DbObject : public ErlRefObject
{
public:
leveldb::DB* m_Db; // NULL or leveldb database object
leveldb::Options * m_DbOptions;
leveldb::port::Mutex m_ItrMutex; //!< mutex protecting m_ItrList
std::list<class ItrObject *> m_ItrList; //!< ItrObjects holding ref count to this
protected:
static ErlNifResourceType* m_Db_RESOURCE;
public:
DbObject(leveldb::DB * DbPtr, leveldb::Options * Options);
virtual ~DbObject();
virtual void Shutdown();
// manual back link to ItrObjects holding reference to this
bool AddReference(class ItrObject *);
void RemoveReference(class ItrObject *);
static void CreateDbObjectType(ErlNifEnv * Env);
static void * CreateDbObject(leveldb::DB * Db, leveldb::Options * DbOptions);
static DbObject * RetrieveDbObject(ErlNifEnv * Env, const ERL_NIF_TERM & DbTerm, bool * term_ok=NULL);
static void DbObjectResourceCleanup(ErlNifEnv *Env, void * Arg);
private:
DbObject();
DbObject(const DbObject&); // nocopy
DbObject& operator=(const DbObject&); // nocopyassign
}; // class DbObject
typedef ReferencePtr<class DbObject> DbObjectPtr_t;
/**
* A self deleting wrapper to contain leveldb iterator.
* Used when an ItrObject needs to skip around and might
* have a background MoveItem performing a prefetch on existing
* iterator.
*
* Oct 17, 2016: new usage model does not require the Wrapper
* be replaced for reuse after Seeks. Converting to static object
*/
class LevelIteratorWrapper
{
public:
DbObjectPtr_t m_DbPtr; //!< access to db for iterator rebuild
leveldb::ReadOptions & m_Options; //!< ItrObject's ReadOptions struct
// (updates "snapshot" member
const leveldb::Snapshot * m_Snapshot;
leveldb::Iterator * m_Iterator;
volatile uint32_t m_HandoffAtomic; //!< matthew's atomic foreground/background prefetch flag.
// m_PrefetchStarted must use uint32_t instead of bool for Solaris CAS operations
volatile uint32_t m_PrefetchStarted; //!< true after first prefetch command
// only used if m_Options.iterator_refresh == true
std::string m_RecentKey; //!< Most recent key returned
time_t m_IteratorStale; //!< time iterator should refresh
bool m_StillUse; //!< true if no error or key end seen
// read by Erlang thread, maintained by eleveldb MoveItem::DoWork
volatile bool m_IsValid; //!< iterator state after last operation
LevelIteratorWrapper(DbObjectPtr_t & DbPtr, leveldb::ReadOptions & Options);
virtual ~LevelIteratorWrapper()
{
PurgeIterator();
} // ~LevelIteratorWrapper
leveldb::Iterator * get() {return(m_Iterator);};
volatile bool Valid() {return(m_IsValid);};
void SetValid(bool flag) {m_IsValid=flag;};
// warning, only valid with Valid() otherwise potential
// segfault if iterator purged to fix AAE'ism
leveldb::Slice key() {return(m_Iterator->key());};
leveldb::Slice value() {return(m_Iterator->value());};
// iterator_refresh related routines
void PurgeIterator()
{
if (NULL!=m_Snapshot)
{
const leveldb::Snapshot * temp_snap(m_Snapshot);
m_Snapshot=NULL;
// leveldb performs actual "delete" call on m_Shapshot's pointer
m_DbPtr->m_Db->ReleaseSnapshot(temp_snap);
} // if
if (NULL!=m_Iterator)
{
leveldb::Iterator * temp_iter(m_Iterator);
m_Iterator=NULL;
delete temp_iter;
} // if
} // PurgeIterator
void RebuildIterator()
{
struct timeval tv;
gettimeofday(&tv, NULL);
m_IteratorStale=tv.tv_sec + 300; // +5min
PurgeIterator();
m_Snapshot = m_DbPtr->m_Db->GetSnapshot();
m_Options.snapshot = m_Snapshot;
m_Iterator = m_DbPtr->m_Db->NewIterator(m_Options);
} // RebuildIterator
private:
LevelIteratorWrapper(const LevelIteratorWrapper &); // no copy
LevelIteratorWrapper& operator=(const LevelIteratorWrapper &); // no assignment
}; // LevelIteratorWrapper
typedef ReferencePtr<LevelIteratorWrapper> LevelIteratorWrapperPtr_t;
/**
* Per Iterator object. Created as erlang reference.
*/
class ItrObject : public ErlRefObject
{
public:
bool keys_only;
leveldb::ReadOptions m_ReadOptions; //!< local copy, pass to LevelIteratorWrapper only
LevelIteratorWrapper m_Wrap;
volatile class MoveTask * reuse_move; //!< iterator work object that is reused instead of lots malloc/free
ReferencePtr<DbObject> m_DbPtr;
ERL_NIF_TERM itr_ref; //!< what was caller ref to async_iterator
ErlNifEnv *itr_ref_env; //!< Erlang Env to hold itr_ref
protected:
static ErlNifResourceType* m_Itr_RESOURCE;
public:
ItrObject(DbObjectPtr_t &, bool, leveldb::ReadOptions &);
virtual ~ItrObject(); // needs to perform free_itr
virtual uint32_t RefDec();
virtual void Shutdown();
static void CreateItrObjectType(ErlNifEnv * Env);
static void * CreateItrObject(DbObjectPtr_t & Db, bool KeysOnly, leveldb::ReadOptions & Options);
static ItrObject * RetrieveItrObject(ErlNifEnv * Env, const ERL_NIF_TERM & DbTerm,
bool ItrClosing,
ReferencePtr<class ItrObject> & CountedPtr);
static void ItrObjectResourceCleanup(ErlNifEnv *Env, void * Arg);
bool ReleaseReuseMove();
private:
ItrObject();
ItrObject(const ItrObject &); // no copy
ItrObject & operator=(const ItrObject &); // no assignment
}; // class ItrObject
typedef ReferencePtr<class ItrObject> ItrObjectPtr_t;
/**
* Container stored in Erlang heap. Used
* to allow erlang heap to destroy iterator if process(s) holding
* iterator go away.
*/
struct ItrObjErlang
{
ItrObject * m_ItrPtr;
volatile uint32_t m_SpinLock;
};
} // namespace eleveldb
#endif // INCL_REFOBJECTS_H