forked from membase/ep-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.hh
More file actions
179 lines (141 loc) · 3.48 KB
/
callbacks.hh
File metadata and controls
179 lines (141 loc) · 3.48 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
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef CALLBACKS_H
#define CALLBACKS_H 1
#include <cassert>
#include "locks.hh"
class Item;
/**
* Value for callback for GET operations.
*/
class GetValue {
public:
GetValue() : value(NULL), id(-1),
status(ENGINE_KEY_ENOENT),
partial(false), nru(false) { }
explicit GetValue(Item *v, ENGINE_ERROR_CODE s=ENGINE_SUCCESS,
uint64_t i = -1,
bool incomplete = false, bool reference = false) :
value(v), id(i), status(s),
partial(incomplete), nru(reference) { }
/**
* The value retrieved for the key.
*/
Item* getValue() { return value; }
/**
* Engine code describing what happened.
*/
ENGINE_ERROR_CODE getStatus() const { return status; }
/**
* Set the status code
*/
void setStatus(ENGINE_ERROR_CODE s) { status = s; }
/**
* Get the item's underlying ID (if applicable).
*/
uint64_t getId() { return id; }
/**
* Set the item's underlying ID.
*/
void setId(uint64_t newId) { id = newId; }
bool isPartial() const { return partial; }
void setPartial() { partial = true; }
bool isReferenced() const { return nru; }
void setValue(Item *i) { value = i; }
private:
Item* value;
uint64_t id;
ENGINE_ERROR_CODE status;
bool partial;
bool nru;
};
/**
* Interface for callbacks from storage APIs.
*/
template <typename RV>
class Callback {
public:
virtual ~Callback() {}
/**
* Method called on callback.
*/
virtual void callback(RV &value) = 0;
virtual void setStatus(int status) {
myStatus = status;
}
virtual int getStatus() {
return myStatus;
}
private:
int myStatus;
};
/**
* Threadsafe callback implementation that just captures the value.
*/
template <typename T>
class RememberingCallback : public Callback<T> {
public:
/**
* Construct a remembering callback.
*/
RememberingCallback() : fired(false), so() { }
/**
* Clean up (including lock resources).
*/
~RememberingCallback() {
}
/**
* The callback implementation -- just store a value.
*/
void callback(T &value) {
LockHolder lh(so);
val = value;
fired = true;
so.notify();
}
/**
* Wait for a value to be available.
*
* This method will return immediately if a value is currently
* available, otherwise it will wait indefinitely for a value
* to arrive.
*/
void waitForValue() {
LockHolder lh(so);
if (!fired) {
so.wait();
}
assert(fired);
}
/**
* The value that was captured from the callback.
*/
T val;
/**
* True if the callback has fired.
*/
bool fired;
private:
SyncObject so;
DISALLOW_COPY_AND_ASSIGN(RememberingCallback);
};
template <typename T>
class TimedRememberingCallback : public RememberingCallback<T> {
public:
TimedRememberingCallback() :
RememberingCallback<T>(), start(gethrtime()), stop(0)
{ }
~TimedRememberingCallback() {
}
void callback(T &value) {
stop = gethrtime();
RememberingCallback<T>::callback(value);
}
hrtime_t getDelta() const {
return stop - start;
}
private:
hrtime_t start;
hrtime_t stop;
DISALLOW_COPY_AND_ASSIGN(TimedRememberingCallback);
};
#endif /* CALLBACKS_H */