|
| 1 | +#ifndef TENSORFLOW_CORE_FRAMEWORK_EMBEDDING_LEVELDB_KV_H_ |
| 2 | +#define TENSORFLOW_CORE_FRAMEWORK_EMBEDDING_LEVELDB_KV_H_ |
| 3 | + |
| 4 | +#include "tensorflow/core/lib/io/path.h" |
| 5 | + |
| 6 | +#include "tensorflow/core/framework/embedding/kv_interface.h" |
| 7 | +#include "tensorflow/core/lib/core/status.h" |
| 8 | + |
| 9 | +#include "leveldb/db.h" |
| 10 | +#include "leveldb/comparator.h" |
| 11 | +#include "leveldb/write_batch.h" |
| 12 | + |
| 13 | +#include <sstream> |
| 14 | + |
| 15 | +using leveldb::DB; |
| 16 | +using leveldb::Options; |
| 17 | +using leveldb::ReadOptions; |
| 18 | +using leveldb::WriteBatch; |
| 19 | +using leveldb::WriteOptions; |
| 20 | +using leveldb::Iterator; |
| 21 | + |
| 22 | +namespace tensorflow { |
| 23 | +template <class V> |
| 24 | +class ValuePtr; |
| 25 | + |
| 26 | +template <class K> |
| 27 | +class SizeCounter { |
| 28 | + public: |
| 29 | + SizeCounter(int num_parts) { |
| 30 | + num_parts_ = num_parts; |
| 31 | + for (int i = 0; i < num_parts_; i++) { |
| 32 | + counter_.emplace_back(0); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + void add(K key, int64 count) { |
| 37 | + int part = key % num_parts_; |
| 38 | + __sync_fetch_and_add(&counter_[part], count); |
| 39 | + } |
| 40 | + |
| 41 | + void sub(K key, int64 count) { |
| 42 | + int part = key % num_parts_; |
| 43 | + __sync_fetch_and_sub(&counter_[part], count); |
| 44 | + } |
| 45 | + |
| 46 | + int64 size() { |
| 47 | + int64 total = 0; |
| 48 | + for (int i = 0; i < num_parts_; i++) { |
| 49 | + total += counter_[i]; |
| 50 | + } |
| 51 | + return total; |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + std::vector<int64> counter_; |
| 56 | + int num_parts_; |
| 57 | +}; |
| 58 | + |
| 59 | +template <class K, class V> |
| 60 | +class LevelDBKV : public KVInterface<K, V> { |
| 61 | + public: |
| 62 | + LevelDBKV(std::string path) { |
| 63 | + path_ = io::JoinPath(path, "level_db_" + std::to_string(Env::Default()->NowMicros()));; |
| 64 | + options_.create_if_missing = true; |
| 65 | + leveldb::Status s = leveldb::DB::Open(options_, path_, &db_); |
| 66 | + KVInterface<K, V>::total_dims_ = 0; |
| 67 | + counter_ = new SizeCounter<K>(8); |
| 68 | + CHECK(s.ok()); |
| 69 | + } |
| 70 | + |
| 71 | + void SetNewValuePtrFunc(std::function<ValuePtr<V>*(size_t)> new_value_ptr_fn) { |
| 72 | + new_value_ptr_fn_ = new_value_ptr_fn; |
| 73 | + } |
| 74 | + |
| 75 | + ~LevelDBKV() { |
| 76 | + delete db_; |
| 77 | + } |
| 78 | + |
| 79 | + Status Lookup(K key, ValuePtr<V>** value_ptr) { |
| 80 | + std::string val_str; |
| 81 | + leveldb::Slice db_key((char*)(&key), sizeof(void*)); |
| 82 | + ValuePtr<V>* val = new_value_ptr_fn_(KVInterface<K, V>::total_dims_); |
| 83 | + leveldb::ReadOptions options; |
| 84 | + leveldb::Status s = db_->Get(options, db_key, &val_str); |
| 85 | + if (s.IsNotFound()) { |
| 86 | + delete val; |
| 87 | + return errors::NotFound( |
| 88 | + "Unable to find Key: ", key, " in LevelDB."); |
| 89 | + } else { |
| 90 | + memcpy((int64 *)(val->GetPtr()), &val_str[0], val_str.length()); |
| 91 | + *value_ptr = val; |
| 92 | + return Status::OK(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + Status Insert(K key, const ValuePtr<V>* value_ptr) { |
| 97 | + counter_->add(key, 1); |
| 98 | + return Status::OK(); |
| 99 | + } |
| 100 | + |
| 101 | + Status BatchInsert(std::vector<K> keys, std::vector<ValuePtr<V>*> value_ptrs) { |
| 102 | + return BatchCommit(keys, value_ptrs); |
| 103 | + } |
| 104 | + |
| 105 | + Status BatchCommit(std::vector<K> keys, std::vector<ValuePtr<V>*> value_ptrs) { |
| 106 | + WriteBatch batch; |
| 107 | + for (int i = 0; i < keys.size(); i++) { |
| 108 | + std::string value_res((char*)value_ptrs[i]->GetPtr(), sizeof(FixedLengthHeader) + KVInterface<K, V>::total_dims_ * sizeof(V)); |
| 109 | + leveldb::Slice db_key((char*)(&keys[i]), sizeof(void*)); |
| 110 | + batch.Put(db_key, value_res); |
| 111 | + delete value_ptrs[i]; |
| 112 | + } |
| 113 | + db_->Write(WriteOptions(),&batch); |
| 114 | + return Status::OK(); |
| 115 | + } |
| 116 | + |
| 117 | + Status Commit(K key, const ValuePtr<V>* value_ptr) { |
| 118 | + std::string value_res((char*)value_ptr->GetPtr(), sizeof(FixedLengthHeader) + KVInterface<K, V>::total_dims_ * sizeof(V)); |
| 119 | + leveldb::Slice db_key((char*)(&key), sizeof(void*)); |
| 120 | + leveldb::Status s = db_->Put(WriteOptions(), db_key, value_res); |
| 121 | + delete value_ptr; |
| 122 | + if (!s.ok()){ |
| 123 | + return errors::AlreadyExists( |
| 124 | + "already exists Key: ", key, " in RocksDB."); |
| 125 | + } else { |
| 126 | + return Status::OK(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Status Remove(K key) { |
| 131 | + counter_->sub(key, 1); |
| 132 | + leveldb::Slice db_key((char*)(&key), sizeof(void*)); |
| 133 | + leveldb::Status s = db_->Delete(WriteOptions(), db_key); |
| 134 | + if (s.ok()) { |
| 135 | + return Status::OK(); |
| 136 | + } else { |
| 137 | + return errors::NotFound( |
| 138 | + "Unable to find Key: ", key, " in RocksDB."); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + Status GetSnapshot(std::vector<K>* key_list, std::vector<ValuePtr<V>* >* value_ptr_list) { |
| 143 | + ReadOptions options; |
| 144 | + options.snapshot = db_->GetSnapshot(); |
| 145 | + Iterator* it = db_->NewIterator(options); |
| 146 | + for (it->SeekToFirst(); it->Valid(); it->Next()) { |
| 147 | + std::string key_str, value_str; |
| 148 | + ValuePtr<V>* value_ptr = new_value_ptr_fn_(KVInterface<K, V>::total_dims_); |
| 149 | + key_str = it->key().ToString(); |
| 150 | + value_str = it->value().ToString(); |
| 151 | + key_list->emplace_back(*((long*)&key_str[0])); |
| 152 | + void* ptr = value_ptr->GetPtr(); |
| 153 | + memcpy(ptr, &value_str[0], KVInterface<K, V>::total_dims_ * sizeof(V) + sizeof(FixedLengthHeader)); |
| 154 | + value_ptr_list->emplace_back(value_ptr); |
| 155 | + } |
| 156 | + assert(it->status().ok()); |
| 157 | + delete it; |
| 158 | + db_->ReleaseSnapshot(options.snapshot); |
| 159 | + return Status::OK(); |
| 160 | + } |
| 161 | + |
| 162 | + int64 Size() const { |
| 163 | + return counter_->size(); |
| 164 | + } |
| 165 | + |
| 166 | + void FreeValuePtr(ValuePtr<V>* value_ptr) { |
| 167 | + delete value_ptr; |
| 168 | + } |
| 169 | + |
| 170 | + std::string DebugString() const { |
| 171 | + return ""; |
| 172 | + } |
| 173 | + private: |
| 174 | + DB* db_; |
| 175 | + SizeCounter<K>* counter_; |
| 176 | + Options options_; |
| 177 | + std::string path_; |
| 178 | + std::function<ValuePtr<V>*(size_t)> new_value_ptr_fn_; |
| 179 | +}; |
| 180 | + |
| 181 | +} //namespace tensorflow |
| 182 | + |
| 183 | +#endif TENSORFLOW_CORE_FRAMEWORK_EMBEDDING_LEVELDB_KV_H_ |
0 commit comments