foundationdb/fdbclient/ThreadSafeTransaction.h

139 lines
5.5 KiB
C
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* ThreadSafeTransaction.h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
2017-05-26 04:48:44 +08:00
* 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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2017-05-26 04:48:44 +08:00
* 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 FDBCLIENT_THREADSAFETRANSACTION_H
#define FDBCLIENT_THREADSAFETRANSACTION_H
#pragma once
#include "fdbclient/ReadYourWrites.h"
2017-05-26 04:48:44 +08:00
#include "flow/ThreadHelper.actor.h"
#include "fdbclient/ClusterInterface.h"
#include "fdbclient/IClientApi.h"
2017-05-26 04:48:44 +08:00
class ThreadSafeDatabase : public IDatabase, public ThreadSafeReferenceCounted<ThreadSafeDatabase> {
public:
~ThreadSafeDatabase();
static ThreadFuture<Reference<IDatabase>> createFromExistingDatabase(Database cx);
Reference<ITransaction> createTransaction();
void setOption( FDBDatabaseOptions::Option option, Optional<StringRef> value = Optional<StringRef>() );
ThreadFuture<Void> onConnected(); // Returns after a majority of coordination servers are available and have reported a leader. The cluster file therefore is valid, but the database might be unavailable.
2017-05-26 04:48:44 +08:00
void addref() { ThreadSafeReferenceCounted<ThreadSafeDatabase>::addref(); }
void delref() { ThreadSafeReferenceCounted<ThreadSafeDatabase>::delref(); }
private:
friend class ThreadSafeTransaction;
DatabaseContext* db;
public: // Internal use only
ThreadSafeDatabase( std::string connFilename, int apiVersion );
2017-05-26 04:48:44 +08:00
ThreadSafeDatabase( DatabaseContext* db ) : db(db) {}
DatabaseContext* unsafeGetPtr() const { return db; }
};
class ThreadSafeTransaction : public ITransaction, ThreadSafeReferenceCounted<ThreadSafeTransaction>, NonCopyable {
public:
explicit ThreadSafeTransaction(DatabaseContext* cx);
2017-05-26 04:48:44 +08:00
~ThreadSafeTransaction();
void cancel() override;
void setVersion( Version v ) override;
ThreadFuture<Version> getReadVersion() override;
ThreadFuture< Optional<Value> > get( const KeyRef& key, bool snapshot = false ) override;
ThreadFuture< Key > getKey( const KeySelectorRef& key, bool snapshot = false ) override;
ThreadFuture< Standalone<RangeResultRef> > getRange( const KeySelectorRef& begin, const KeySelectorRef& end, int limit, bool snapshot = false, bool reverse = false ) override;
ThreadFuture< Standalone<RangeResultRef> > getRange( const KeySelectorRef& begin, const KeySelectorRef& end, GetRangeLimits limits, bool snapshot = false, bool reverse = false ) override;
ThreadFuture< Standalone<RangeResultRef> > getRange( const KeyRangeRef& keys, int limit, bool snapshot = false, bool reverse = false ) override {
2019-07-03 05:42:50 +08:00
return getRange( firstGreaterOrEqual(keys.begin), firstGreaterOrEqual(keys.end), limit, snapshot, reverse );
2017-05-26 04:48:44 +08:00
}
ThreadFuture< Standalone<RangeResultRef> > getRange( const KeyRangeRef& keys, GetRangeLimits limits, bool snapshot = false, bool reverse = false ) override {
2017-05-26 04:48:44 +08:00
return getRange( firstGreaterOrEqual(keys.begin), firstGreaterOrEqual(keys.end), limits, snapshot, reverse );
}
ThreadFuture<Standalone<VectorRef<const char*>>> getAddressesForKey(const KeyRef& key) override;
ThreadFuture<Standalone<StringRef>> getVersionstamp() override;
2017-05-26 04:48:44 +08:00
void addReadConflictRange( const KeyRangeRef& keys ) override;
2017-05-26 04:48:44 +08:00
void makeSelfConflicting();
void atomicOp( const KeyRef& key, const ValueRef& value, uint32_t operationType ) override;
void set( const KeyRef& key, const ValueRef& value ) override;
void clear( const KeyRef& begin, const KeyRef& end) override;
void clear( const KeyRangeRef& range ) override;
void clear( const KeyRef& key ) override;
2017-05-26 04:48:44 +08:00
ThreadFuture< Void > watch( const KeyRef& key ) override;
2017-05-26 04:48:44 +08:00
void addWriteConflictRange( const KeyRangeRef& keys ) override;
2017-05-26 04:48:44 +08:00
ThreadFuture<Void> commit() override;
Version getCommittedVersion() override;
ThreadFuture<int64_t> getApproximateSize() override;
2017-05-26 04:48:44 +08:00
void setOption( FDBTransactionOptions::Option option, Optional<StringRef> value = Optional<StringRef>() ) override;
2017-05-26 04:48:44 +08:00
ThreadFuture<Void> checkDeferredError();
ThreadFuture<Void> onError( Error const& e ) override;
2017-05-26 04:48:44 +08:00
// These are to permit use as state variables in actors:
ThreadSafeTransaction() : tr(NULL) {}
void operator=(ThreadSafeTransaction&& r) BOOST_NOEXCEPT;
ThreadSafeTransaction(ThreadSafeTransaction&& r) BOOST_NOEXCEPT;
2017-05-26 04:48:44 +08:00
void reset() override;
2017-05-26 04:48:44 +08:00
void addref() override { ThreadSafeReferenceCounted<ThreadSafeTransaction>::addref(); }
void delref() override { ThreadSafeReferenceCounted<ThreadSafeTransaction>::delref(); }
2017-05-26 04:48:44 +08:00
private:
ReadYourWritesTransaction *tr;
};
class ThreadSafeApi : public IClientApi, ThreadSafeReferenceCounted<ThreadSafeApi> {
public:
void selectApiVersion(int apiVersion);
const char* getClientVersion();
void setNetworkOption(FDBNetworkOptions::Option option, Optional<StringRef> value = Optional<StringRef>());
void setupNetwork();
void runNetwork();
void stopNetwork();
Reference<IDatabase> createDatabase(const char *clusterFilePath);
2017-05-26 04:48:44 +08:00
void addNetworkThreadCompletionHook(void (*hook)(void*), void *hookParameter);
2017-05-26 04:48:44 +08:00
static IClientApi* api;
private:
ThreadSafeApi();
int apiVersion;
const std::string clientVersion;
uint64_t transportId;
Mutex lock;
std::vector<std::pair<void (*)(void*), void*>> threadCompletionHooks;
2017-05-26 04:48:44 +08:00
};
#endif