Skip to content

Commit 5cdb3e1

Browse files
authored
Minor cleanup to FDBPlugin and IndexPlugin (#166)
* Minor cleanup to FDBPlugin and IndexPLugin * Fix review comments
1 parent f494f17 commit 5cdb3e1

2 files changed

Lines changed: 53 additions & 54 deletions

File tree

src/QLContext.actor.cpp

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ Future<DataValue> IReadContext::toDataValue() {
3535
}
3636

3737
ACTOR Future<Void> DocTransaction::commitChanges(Reference<DocTransaction> self, std::string docPrefix) {
38-
auto info = self->infos.find(docPrefix);
39-
if (info == self->infos.end())
38+
auto deferredDocument = self->deferredDocuments.find(docPrefix);
39+
if (deferredDocument == self->deferredDocuments.end())
4040
return Void();
41-
Void _ = wait(info->second->commitChanges(self));
41+
Void _ = wait(deferredDocument->second->commitChanges(self));
4242
return Void();
4343
}
4444

@@ -60,13 +60,13 @@ ACTOR Future<Void> DocumentDeferred::commitChanges(Reference<DocTransaction> tr,
6060
*/
6161
Future<Void> DocTransaction::onError(Error const& e) {
6262
cancel_ongoing_index_reads();
63-
infos.clear();
63+
deferredDocuments.clear();
6464
return tr->onError(e);
6565
}
6666

6767
void DocTransaction::cancel_ongoing_index_reads() {
68-
for (auto it : infos) {
69-
for (auto actor : it.second->index_update_actors)
68+
for (auto deferredDocument : deferredDocuments) {
69+
for (auto actor : deferredDocument.second->index_update_actors)
7070
actor.cancel();
7171
}
7272
}
@@ -185,22 +185,22 @@ struct FDBPlugin : ITDoc, ReferenceCounted<FDBPlugin>, FastAllocated<FDBPlugin>
185185
void addref() override { ReferenceCounted<FDBPlugin>::addref(); }
186186
void delref() override { ReferenceCounted<FDBPlugin>::delref(); }
187187

188-
std::pair<bool, Reference<DocumentDeferred>> findOrCreate(Reference<DocTransaction> tr, DataKey const& key) {
189-
if (key.size() > 1) {
190-
std::string documentPrefix = key.keyPrefix(2).toString();
191-
auto info = tr->infos.find(documentPrefix);
192-
if (info == tr->infos.end())
193-
info = tr->infos
194-
.insert(std::make_pair(documentPrefix, Reference<DocumentDeferred>(new DocumentDeferred())))
195-
.first;
196-
return std::make_pair(true, (*info).second);
197-
}
198-
return std::make_pair(false, Reference<DocumentDeferred>());
188+
Reference<DocumentDeferred> findOrCreate(Reference<DocTransaction> tr, DataKey const& key) {
189+
ASSERT(key.size() > 1);
190+
std::string documentPrefix = key.keyPrefix(2).toString();
191+
auto deferredDocument = tr->deferredDocuments.find(documentPrefix);
192+
if (deferredDocument == tr->deferredDocuments.end())
193+
deferredDocument =
194+
tr->deferredDocuments
195+
.insert(std::make_pair(documentPrefix, Reference<DocumentDeferred>(new DocumentDeferred())))
196+
.first;
197+
return (*deferredDocument).second;
199198
}
200199

201200
Future<Optional<DataValue>> get(Reference<DocTransaction> tr, DataKey key) override {
202201
return FDBPlugin_get(key, tr);
203202
}
203+
204204
GenFutureStream<KeyValue> getDescendants(Reference<DocTransaction> tr,
205205
DataKey key,
206206
StringRef begin,
@@ -211,33 +211,34 @@ struct FDBPlugin : ITDoc, ReferenceCounted<FDBPlugin>, FastAllocated<FDBPlugin>
211211
r.actor = FDBPlugin_getDescendants(key, tr, begin, end, p, flowControlLock);
212212
return r;
213213
}
214+
214215
void set(Reference<DocTransaction> tr, DataKey key, ValueRef value) override {
215216
std::string k = getFDBKey(key);
216217
Value v = value;
217-
auto pair = findOrCreate(tr, key);
218-
if (pair.first)
219-
pair.second->deferred.emplace_back([k, v](Reference<DocTransaction> tr) {
220-
if (k.size() > DocLayerConstants::FDB_KEY_LENGTH_LIMIT)
221-
throw key_too_large();
222-
if (v.size() > DocLayerConstants::FDB_VALUE_LENGTH_LIMIT)
223-
throw value_too_large();
224-
tr->tr->set(k, v);
225-
});
218+
auto docDeferred = findOrCreate(tr, key);
219+
docDeferred->deferred.emplace_back([k, v](Reference<DocTransaction> tr) {
220+
if (k.size() > DocLayerConstants::FDB_KEY_LENGTH_LIMIT)
221+
throw key_too_large();
222+
if (v.size() > DocLayerConstants::FDB_VALUE_LENGTH_LIMIT)
223+
throw value_too_large();
224+
tr->tr->set(k, v);
225+
});
226226
}
227+
227228
void clearDescendants(Reference<DocTransaction> tr, DataKey key) override {
228229
std::string _key = getFDBKey(key);
229230

230231
KeyRange kr = KeyRangeRef(_key + '\x00', _key + '\xFF');
231-
auto pair = findOrCreate(tr, key);
232-
if (pair.first)
233-
pair.second->deferred.emplace_back([kr](Reference<DocTransaction> tr) { tr->tr->clear(kr); });
232+
auto docDeferred = findOrCreate(tr, key);
233+
docDeferred->deferred.emplace_back([kr](Reference<DocTransaction> tr) { tr->tr->clear(kr); });
234234
}
235+
235236
void clear(Reference<DocTransaction> tr, DataKey key) override {
236237
std::string k = getFDBKey(key);
237-
auto pair = findOrCreate(tr, key);
238-
if (pair.first)
239-
pair.second->deferred.emplace_back([k](Reference<DocTransaction> tr) { tr->tr->clear(k); });
238+
auto docDeferred = findOrCreate(tr, key);
239+
docDeferred->deferred.emplace_back([k](Reference<DocTransaction> tr) { tr->tr->clear(k); });
240240
}
241+
241242
std::string toString() override { return "FDBPlugin"; }
242243
};
243244

@@ -246,45 +247,43 @@ struct IndexPlugin : ITDoc {
246247
Reference<DocumentDeferred> dd,
247248
DataKey documentPath) = 0;
248249

249-
std::pair<bool, Reference<DocumentDeferred>> shouldDoUpdate(Reference<DocTransaction> tr,
250-
DataKey const& documentKey) {
251-
if (documentKey.startsWith(collectionPath) && documentKey.size() > collectionPath.size()) {
252-
std::string documentPrefix = documentKey.toString();
253-
auto info = tr->infos.find(documentPrefix);
254-
if (info == tr->infos.end())
255-
info = tr->infos
256-
.insert(std::make_pair(documentPrefix, Reference<DocumentDeferred>(new DocumentDeferred())))
257-
.first;
258-
return std::make_pair(true, (*info).second);
259-
}
260-
return std::make_pair(false, Reference<DocumentDeferred>());
250+
Reference<DocumentDeferred> shouldDoUpdate(Reference<DocTransaction> tr, DataKey const& documentKey) {
251+
ASSERT(documentKey.startsWith(collectionPath) && documentKey.size() > collectionPath.size());
252+
std::string documentPrefix = documentKey.toString();
253+
auto deferredDocument = tr->deferredDocuments.find(documentPrefix);
254+
if (deferredDocument == tr->deferredDocuments.end())
255+
deferredDocument =
256+
tr->deferredDocuments
257+
.insert(std::make_pair(documentPrefix, Reference<DocumentDeferred>(new DocumentDeferred())))
258+
.first;
259+
return (*deferredDocument).second;
261260
}
262261

263262
void set(Reference<DocTransaction> tr, DataKey key, ValueRef value) override {
264263
DataKey documentPrefix = key.keyPrefix(collectionPath.size() + 1);
265-
auto pair = shouldDoUpdate(tr, documentPrefix);
266-
if (pair.first && pair.second->dirty.insert(this).second) {
267-
pair.second->index_update_actors.push_back(doIndexUpdate(tr, pair.second, documentPrefix));
264+
auto docDeferred = shouldDoUpdate(tr, documentPrefix);
265+
if (docDeferred->dirty.insert(this).second) {
266+
docDeferred->index_update_actors.push_back(doIndexUpdate(tr, docDeferred, documentPrefix));
268267
}
269268

270269
next->set(tr, key, value);
271270
}
272271

273272
void clear(Reference<DocTransaction> tr, DataKey key) override {
274273
DataKey documentPrefix = key.keyPrefix(collectionPath.size() + 1);
275-
auto pair = shouldDoUpdate(tr, documentPrefix);
276-
if (pair.first && pair.second->dirty.insert(this).second) {
277-
pair.second->index_update_actors.push_back(doIndexUpdate(tr, pair.second, documentPrefix));
274+
auto docDeferred = shouldDoUpdate(tr, documentPrefix);
275+
if (docDeferred->dirty.insert(this).second) {
276+
docDeferred->index_update_actors.push_back(doIndexUpdate(tr, docDeferred, documentPrefix));
278277
}
279278

280279
next->clear(tr, key);
281280
}
282281

283282
void clearDescendants(Reference<DocTransaction> tr, DataKey key) override {
284283
DataKey documentPrefix = key.keyPrefix(collectionPath.size() + 1);
285-
auto pair = shouldDoUpdate(tr, documentPrefix);
286-
if (pair.first && pair.second->dirty.insert(this).second) {
287-
pair.second->index_update_actors.push_back(doIndexUpdate(tr, pair.second, documentPrefix));
284+
auto docDeferred = shouldDoUpdate(tr, documentPrefix);
285+
if (docDeferred->dirty.insert(this).second) {
286+
docDeferred->index_update_actors.push_back(doIndexUpdate(tr, docDeferred, documentPrefix));
288287
}
289288
next->clearDescendants(tr, key);
290289
}

src/QLContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct DocTransaction : ReferenceCounted<DocTransaction> {
8787
// in general. Look at the comments in doNonIsolatedRW() for more on what it's for.
8888
void cancel_ongoing_index_reads();
8989

90-
std::map<std::string, Reference<DocumentDeferred>> infos;
90+
std::map<std::string, Reference<DocumentDeferred>> deferredDocuments;
9191
};
9292

9393
template <class T>

0 commit comments

Comments
 (0)