1 /* 2 * fdb_flow.h 3 * 4 * This source file is part of the FoundationDB open source project 5 * 6 * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 #ifndef FDB_FLOW_FDB_FLOW_H 22 #define FDB_FLOW_FDB_FLOW_H 23 24 #include <flow/flow.h> 25 26 #define FDB_API_VERSION 610 27 #include <bindings/c/foundationdb/fdb_c.h> 28 #undef DLLEXPORT 29 30 #include "FDBLoanerTypes.h" 31 32 namespace FDB { 33 struct CFuture : NonCopyable, ReferenceCounted<CFuture>, FastAllocated<CFuture> { CFutureCFuture34 CFuture() : f(NULL) {} CFutureCFuture35 explicit CFuture(FDBFuture* f) : f(f) {} ~CFutureCFuture36 ~CFuture() { 37 if (f) { 38 fdb_future_destroy(f); 39 } 40 } 41 42 void blockUntilReady(); 43 44 FDBFuture* f; 45 }; 46 47 template <class T> 48 class FDBStandalone : public T { 49 public: FDBStandalone()50 FDBStandalone() {} FDBStandalone(Reference<CFuture> f,T const & t)51 FDBStandalone(Reference<CFuture> f, T const& t) : T(t), f(f) {} FDBStandalone(FDBStandalone const & o)52 FDBStandalone(FDBStandalone const& o) : T((T const&)o), f(o.f) {} 53 54 private: 55 Reference<CFuture> f; 56 }; 57 58 class ReadTransaction : public ReferenceCounted<ReadTransaction> { 59 public: ~ReadTransaction()60 virtual ~ReadTransaction(){}; 61 virtual void setReadVersion(Version v) = 0; 62 virtual Future<Version> getReadVersion() = 0; 63 64 virtual Future<Optional<FDBStandalone<ValueRef>>> get(const Key& key, bool snapshot = false) = 0; 65 virtual Future<FDBStandalone<KeyRef>> getKey(const KeySelector& key, bool snapshot = false) = 0; 66 virtual Future<Void> watch(const Key& key) = 0; 67 68 virtual Future<FDBStandalone<RangeResultRef>> getRange( 69 const KeySelector& begin, const KeySelector& end, GetRangeLimits limits = GetRangeLimits(), 70 bool snapshot = false, bool reverse = false, 71 FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL) = 0; 72 virtual Future<FDBStandalone<RangeResultRef>> getRange( 73 const KeySelector& begin, const KeySelector& end, int limit, bool snapshot = false, bool reverse = false, 74 FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL) { 75 return getRange(begin, end, GetRangeLimits(limit), snapshot, reverse, streamingMode); 76 } 77 virtual Future<FDBStandalone<RangeResultRef>> getRange( 78 const KeyRange& keys, int limit, bool snapshot = false, bool reverse = false, 79 FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL) { 80 return getRange(KeySelector(firstGreaterOrEqual(keys.begin), keys.arena()), 81 KeySelector(firstGreaterOrEqual(keys.end), keys.arena()), limit, snapshot, reverse, 82 streamingMode); 83 } 84 virtual Future<FDBStandalone<RangeResultRef>> getRange( 85 const KeyRange& keys, GetRangeLimits limits = GetRangeLimits(), bool snapshot = false, bool reverse = false, 86 FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL) { 87 return getRange(KeySelector(firstGreaterOrEqual(keys.begin), keys.arena()), 88 KeySelector(firstGreaterOrEqual(keys.end), keys.arena()), limits, snapshot, reverse, 89 streamingMode); 90 } 91 92 virtual void addReadConflictRange(KeyRangeRef const& keys) = 0; 93 virtual void addReadConflictKey(KeyRef const& key) = 0; 94 95 virtual void setOption(FDBTransactionOption option, Optional<StringRef> value = Optional<StringRef>()) = 0; 96 97 virtual Future<Void> onError(Error const& e) = 0; 98 99 virtual void cancel() = 0; 100 virtual void reset() = 0; 101 }; 102 103 class Transaction : public ReadTransaction { 104 public: 105 virtual void addWriteConflictRange(KeyRangeRef const& keys) = 0; 106 virtual void addWriteConflictKey(KeyRef const& key) = 0; 107 108 virtual void atomicOp(const KeyRef& key, const ValueRef& operand, FDBMutationType operationType) = 0; 109 virtual void set(const KeyRef& key, const ValueRef& value) = 0; 110 virtual void clear(const KeyRangeRef& range) = 0; 111 virtual void clear(const KeyRef& key) = 0; 112 113 virtual Future<Void> commit() = 0; 114 virtual Version getCommittedVersion() = 0; 115 virtual Future<FDBStandalone<StringRef>> getVersionstamp() = 0; 116 }; 117 118 class Database : public ReferenceCounted<Database> { 119 public: ~Database()120 virtual ~Database(){}; 121 virtual Reference<Transaction> createTransaction() = 0; 122 virtual void setDatabaseOption(FDBDatabaseOption option, Optional<StringRef> value = Optional<StringRef>()) = 0; 123 }; 124 125 class API { 126 public: 127 static API* selectAPIVersion(int apiVersion); 128 static API* getInstance(); 129 static bool isAPIVersionSelected(); 130 131 void setNetworkOption(FDBNetworkOption option, Optional<StringRef> value = Optional<StringRef>()); 132 133 void setupNetwork(); 134 void runNetwork(); 135 void stopNetwork(); 136 137 Reference<Database> createDatabase(std::string const& connFilename = ""); 138 139 bool evaluatePredicate(FDBErrorPredicate pred, Error const& e); 140 int getAPIVersion() const; 141 142 private: 143 static API* instance; 144 145 API(int version); 146 int version; 147 }; 148 } // namespace FDB 149 #endif // FDB_FLOW_FDB_FLOW_H 150