foundationdb/fdbserver/OnDemandStore.actor.cpp

79 lines
2.0 KiB
C++
Raw Normal View History

/*
* OnDemandStore.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
* Licensed 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.
*/
#include "fdbserver/OnDemandStore.h"
#include "flow/actorcompiler.h" // must be last include
ACTOR static Future<Void> onErr(Future<Future<Void>> e) {
Future<Void> f = wait(e);
wait(f);
return Void();
}
void OnDemandStore::open() {
platform::createDirectory(folder);
store = keyValueStoreMemory(joinPath(folder, prefix), myID, 500e6);
err.send(store->getError());
}
OnDemandStore::OnDemandStore(std::string const& folder, UID myID, std::string const& prefix)
2021-07-23 13:48:27 +08:00
: folder(folder), myID(myID), store(nullptr), prefix(prefix) {}
OnDemandStore::~OnDemandStore() {
2021-09-10 04:50:07 +08:00
close();
}
IKeyValueStore* OnDemandStore::get() {
if (!store) {
open();
}
return store;
}
bool OnDemandStore::exists() const {
return store || fileExists(joinPath(folder, prefix + "0.fdq")) || fileExists(joinPath(folder, prefix + "1.fdq")) ||
fileExists(joinPath(folder, prefix + ".fdb"));
}
IKeyValueStore* OnDemandStore::operator->() {
return get();
}
2021-10-18 11:40:38 +08:00
Future<Void> OnDemandStore::getError() const {
return onErr(err.getFuture());
}
2021-09-10 04:50:07 +08:00
2021-10-18 11:40:38 +08:00
Future<Void> OnDemandStore::onClosed() const {
2021-09-10 04:50:07 +08:00
return store->onClosed();
}
void OnDemandStore::dispose() {
if (store) {
store->dispose();
store = nullptr;
}
}
void OnDemandStore::close() {
if (store) {
store->close();
store = nullptr;
}
}