Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 152 lines (122 sloc) 3.092 kB
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #ifndef CALLBACKS_H
3 #define CALLBACKS_H 1
4
b5f2a78 @seanlynch Include assert.h in callbacks.hh since we use asserts.
seanlynch authored
5 #include <cassert>
6
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
7 #include "locks.hh"
8
b3262a2 @dustin Removed kvstore.hh
dustin authored
9 class Item;
78fdf39 @fdmanana Implemented SYNC on replication
fdmanana authored
10 class StoredValue;
b3262a2 @dustin Removed kvstore.hh
dustin authored
11
12 /**
13 * Value for callback for GET operations.
14 */
15 class GetValue {
16 public:
78fdf39 @fdmanana Implemented SYNC on replication
fdmanana authored
17 GetValue() : value(NULL), storedValue(NULL), id(-1),
18 vb_version(-1), status(ENGINE_KEY_ENOENT) { }
b3262a2 @dustin Removed kvstore.hh
dustin authored
19
64a57dc @dustin Return the rowid with GetValue
dustin authored
20 explicit GetValue(Item *v, ENGINE_ERROR_CODE s=ENGINE_SUCCESS,
78fdf39 @fdmanana Implemented SYNC on replication
fdmanana authored
21 uint64_t i = -1, uint16_t vbucket_version = -1,
22 StoredValue *sv = NULL) :
23 value(v), storedValue(sv), id(i), vb_version(vbucket_version), status(s) { }
b3262a2 @dustin Removed kvstore.hh
dustin authored
24
25 /**
26 * The value retrieved for the key.
27 */
28 Item* getValue() { return value; }
29
30 /**
31 * Engine code describing what happened.
32 */
33 ENGINE_ERROR_CODE getStatus() const { return status; }
34
64a57dc @dustin Return the rowid with GetValue
dustin authored
35 /**
ec4f59c @trondn Add support for "decremental" restore
trondn authored
36 * Set the status code
37 */
38 void setStatus(ENGINE_ERROR_CODE s) { status = s; }
39
40 /**
64a57dc @dustin Return the rowid with GetValue
dustin authored
41 * Get the item's underlying ID (if applicable).
42 */
43 uint64_t getId() { return id; }
44
e39cf80 @chiyoung Preliminary implementation for supporting vbucket versioning
chiyoung authored
45 /**
46 * Get the item's vbucket version (if applicable).
47 */
48 uint16_t getVBucketVersion() { return vb_version; }
49
78fdf39 @fdmanana Implemented SYNC on replication
fdmanana authored
50 /**
51 * Get the StoredValue instance associated with the item (if applicable).
52 */
53 StoredValue* getStoredValue() const {
54 return storedValue;
55 }
56
b3262a2 @dustin Removed kvstore.hh
dustin authored
57 private:
58
59 Item* value;
78fdf39 @fdmanana Implemented SYNC on replication
fdmanana authored
60 StoredValue* storedValue;
64a57dc @dustin Return the rowid with GetValue
dustin authored
61 uint64_t id;
e39cf80 @chiyoung Preliminary implementation for supporting vbucket versioning
chiyoung authored
62 uint16_t vb_version;
b3262a2 @dustin Removed kvstore.hh
dustin authored
63 ENGINE_ERROR_CODE status;
64 };
65
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
66 /**
67 * Interface for callbacks from storage APIs.
68 */
69 template <typename RV>
70 class Callback {
71 public:
72
73 virtual ~Callback() {}
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
74
75 /**
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
76 * Method called on callback.
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
77 */
954e754 @dustin Convert callback to pure virtual.
dustin authored
78 virtual void callback(RV &value) = 0;
ee21db8 @maniktaneja implementation of getl command (lock and get)
maniktaneja authored
79
80 virtual void setStatus(int status) {
81 myStatus = status;
82 }
83
84 virtual int getStatus() {
85 return myStatus;
86 }
87
88 private:
89
90 int myStatus;
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
91 };
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
92
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
93 /**
94 * Threadsafe callback implementation that just captures the value.
95 */
96 template <typename T>
97 class RememberingCallback : public Callback<T> {
98 public:
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
99
100 /**
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
101 * Construct a remembering callback.
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
102 */
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
103 RememberingCallback() : fired(false), so() { }
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
104
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
105 /**
106 * Clean up (including lock resources).
107 */
108 ~RememberingCallback() {
109 }
110
111 /**
112 * The callback implementation -- just store a value.
113 */
114 void callback(T &value) {
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
115 LockHolder lh(so);
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
116 val = value;
117 fired = true;
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
118 so.notify();
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
119 }
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
120
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
121 /**
122 * Wait for a value to be available.
123 *
124 * This method will return immediately if a value is currently
125 * available, otherwise it will wait indefinitely for a value
126 * to arrive.
127 */
128 void waitForValue() {
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
129 LockHolder lh(so);
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
130 if (!fired) {
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
131 so.wait();
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
132 }
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
133 assert(fired);
134 }
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
135
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
136 /**
137 * The value that was captured from the callback.
138 */
139 T val;
140 /**
141 * True if the callback has fired.
142 */
143 bool fired;
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
144
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
145 private:
279b986 @trondn Wrap the pthreads logic into classes
trondn authored
146 SyncObject so;
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
147
38b0b7c @trondn Refactor: Remove kvtest namespace
trondn authored
148 DISALLOW_COPY_AND_ASSIGN(RememberingCallback);
149 };
54e72c4 @trondn Add files from Dustin's kvtest
trondn authored
150
151 #endif /* CALLBACKS_H */
Something went wrong with that request. Please try again.