Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 1 | #include <typeinfo> |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 2 | #define EXPORT __attribute__((visibility("visible"))) |
| 3 | #define IMPORT |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 4 | #include <assert.h> |
| 5 | #include <memory> |
| 6 | #include <iostream> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 7 | #include <algorithm> |
| 8 | #include <vector> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 10 | #include <string> |
| 11 | #include <sstream> // for ostringstream |
| 12 | #include <math.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 13 | #include "rocksdb/cache.h" |
| 14 | #include "rocksdb/comparator.h" |
| 15 | #include "rocksdb/db.h" |
| 16 | #include "rocksdb/env.h" |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 17 | #include "rocksdb/table.h" |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 18 | #include <rocksdb/merge_operator.h> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 19 | #include <rocksdb/slice_transform.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 20 | #include "rocksdb/utilities/db_ttl.h" |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 21 | #include "rocksdb/filter_policy.h" |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 22 | #include "merge_operators.h" |
| 23 | |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 24 | #define AVG_WINDOW_SIZE 7 |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 25 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 26 | #define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100) |
| 27 | #define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1) |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 28 | #define W1(key) (uint64_t)(key & 0xffffff) |
| 29 | #define W2(key) (uint64_t)((key >> 24) & 0xffffff) |
| 30 | #define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff)) |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 31 | |
| 32 | typedef struct { |
| 33 | uint64_t freq; |
| 34 | char *word; |
| 35 | } vocab_entry; |
| 36 | |
| 37 | // typedef struct Collocator { |
| 38 | // uint64_t w2; |
| 39 | // uint64_t sum; |
| 40 | // }; |
| 41 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 42 | using namespace rocksdb; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 43 | using namespace std; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 44 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 45 | namespace rocksdb { |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 46 | class Collocator { |
| 47 | public: |
| 48 | uint64_t w2; |
| 49 | uint64_t sum; |
| 50 | double pmi; |
| 51 | double npmi; |
| 52 | double llr; |
| 53 | double md; |
| 54 | double lfmd; |
| 55 | double fpmi; |
| 56 | }; |
| 57 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 58 | size_t num_merge_operator_calls; |
| 59 | void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 60 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 61 | size_t num_partial_merge_calls; |
| 62 | void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 63 | |
| 64 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 65 | inline void EncodeFixed64(char* buf, uint64_t value) { |
| 66 | if (! IS_BIG_ENDIAN) { |
| 67 | memcpy(buf, &value, sizeof(value)); |
| 68 | } else { |
| 69 | buf[0] = value & 0xff; |
| 70 | buf[1] = (value >> 8) & 0xff; |
| 71 | buf[2] = (value >> 16) & 0xff; |
| 72 | buf[3] = (value >> 24) & 0xff; |
| 73 | buf[4] = (value >> 32) & 0xff; |
| 74 | buf[5] = (value >> 40) & 0xff; |
| 75 | buf[6] = (value >> 48) & 0xff; |
| 76 | buf[7] = (value >> 56) & 0xff; |
| 77 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 80 | inline uint32_t DecodeFixed32(const char* ptr) { |
| 81 | if (! IS_BIG_ENDIAN) { |
| 82 | // Load the raw bytes |
| 83 | uint32_t result; |
| 84 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load |
| 85 | return result; |
| 86 | } else { |
| 87 | return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0]))) |
| 88 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8) |
| 89 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16) |
| 90 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | inline uint64_t DecodeFixed64(const char* ptr) { |
| 95 | if (! IS_BIG_ENDIAN) { |
| 96 | // Load the raw bytes |
| 97 | uint64_t result; |
| 98 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load |
| 99 | return result; |
| 100 | } else { |
| 101 | uint64_t lo = DecodeFixed32(ptr); |
| 102 | uint64_t hi = DecodeFixed32(ptr + 4); |
| 103 | return (hi << 32) | lo; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | class CountMergeOperator : public AssociativeMergeOperator { |
| 109 | public: |
| 110 | CountMergeOperator() { |
| 111 | mergeOperator_ = MergeOperators::CreateUInt64AddOperator(); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 114 | virtual bool Merge(const Slice& key, |
| 115 | const Slice* existing_value, |
| 116 | const Slice& value, |
| 117 | std::string* new_value, |
| 118 | Logger* logger) const override { |
| 119 | assert(new_value->empty()); |
| 120 | ++num_merge_operator_calls; |
| 121 | if (existing_value == nullptr) { |
| 122 | new_value->assign(value.data(), value.size()); |
| 123 | return true; |
| 124 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 125 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 126 | return mergeOperator_->PartialMerge( |
| 127 | key, |
| 128 | *existing_value, |
| 129 | value, |
| 130 | new_value, |
| 131 | logger); |
| 132 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 133 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 134 | virtual const char* Name() const override { |
| 135 | return "UInt64AddOperator"; |
| 136 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 137 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 138 | private: |
| 139 | std::shared_ptr<MergeOperator> mergeOperator_; |
| 140 | }; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 141 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 142 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 143 | class CollocatorIterator : public Iterator { |
| 144 | private: |
| 145 | char prefixc[sizeof(uint64_t)]; |
| 146 | Iterator *base_iterator_; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 147 | |
| 148 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 149 | public: |
| 150 | CollocatorIterator(Iterator* base_iterator) |
| 151 | : base_iterator_(base_iterator) |
| 152 | {} |
| 153 | |
| 154 | ~CollocatorIterator(); |
| 155 | |
| 156 | void setPrefix(char *prefix) { |
| 157 | memcpy(prefixc, prefix, sizeof(uint64_t)); |
| 158 | } |
| 159 | |
| 160 | virtual void SeekToFirst() { base_iterator_->SeekToFirst(); } |
| 161 | virtual void SeekToLast() { base_iterator_->SeekToLast(); } |
| 162 | virtual void Seek(const rocksdb::Slice& s) { base_iterator_->Seek(s); } |
| 163 | virtual void Prev() { base_iterator_->Prev(); } |
| 164 | virtual void Next() { base_iterator_->Next(); } |
| 165 | virtual Slice key() const; |
| 166 | virtual Slice value() const; |
| 167 | virtual Status status() const; |
| 168 | virtual bool Valid() const; |
| 169 | bool isValid(); |
| 170 | uint64_t intValue(); |
| 171 | uint64_t intKey(); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 172 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 173 | }; |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 174 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 175 | // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {} |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 176 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 177 | bool rocksdb::CollocatorIterator::Valid() const { |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 178 | return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3)); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 179 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 180 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 181 | bool rocksdb::CollocatorIterator::isValid() { |
| 182 | return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3)); |
| 183 | } |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 184 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 185 | uint64_t rocksdb::CollocatorIterator::intKey() { |
| 186 | return DecodeFixed64(base_iterator_->key().data()); |
| 187 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 188 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 189 | uint64_t rocksdb::CollocatorIterator::intValue() { |
| 190 | return DecodeFixed64(base_iterator_->value().data()); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 193 | class Collocators { |
| 194 | private: |
| 195 | WriteOptions merge_option_; // for merge |
| 196 | char _one[sizeof(uint64_t)]; |
| 197 | Slice _one_slice; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 198 | vocab_entry *_vocab = NULL; |
| 199 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 200 | protected: |
| 201 | std::shared_ptr<DB> db_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 202 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 203 | WriteOptions put_option_; |
| 204 | ReadOptions get_option_; |
| 205 | WriteOptions delete_option_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 206 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 207 | uint64_t default_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 208 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 209 | std::shared_ptr<DB> OpenDb(const char *dbname); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 210 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 211 | public: |
| 212 | Collocators(const char *db_name); |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 213 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 214 | ~Collocators(); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 215 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 216 | // public interface of Collocators. |
| 217 | // All four functions return false |
| 218 | // if the underlying level db operation failed. |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 219 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 220 | // mapped to a levedb Put |
| 221 | bool set(const std::string& key, uint64_t value) { |
| 222 | // just treat the internal rep of int64 as the string |
| 223 | char buf[sizeof(value)]; |
| 224 | EncodeFixed64(buf, value); |
| 225 | Slice slice(buf, sizeof(value)); |
| 226 | auto s = db_->Put(put_option_, key, slice); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 227 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 228 | if (s.ok()) { |
| 229 | return true; |
| 230 | } else { |
| 231 | std::cerr << s.ToString() << std::endl; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 232 | return false; |
| 233 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 234 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 235 | |
| 236 | DB *getDb() { |
| 237 | return db_.get(); |
| 238 | } |
| 239 | |
| 240 | // mapped to a rocksdb Delete |
| 241 | bool remove(const std::string& key) { |
| 242 | auto s = db_->Delete(delete_option_, key); |
| 243 | |
| 244 | if (s.ok()) { |
| 245 | return true; |
| 246 | } else { |
| 247 | std::cerr << s.ToString() << std::endl; |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // mapped to a rocksdb Get |
| 253 | bool get(const std::string& key, uint64_t* value) { |
| 254 | std::string str; |
| 255 | auto s = db_->Get(get_option_, key, &str); |
| 256 | |
| 257 | if (s.IsNotFound()) { |
| 258 | // return default value if not found; |
| 259 | *value = default_; |
| 260 | return true; |
| 261 | } else if (s.ok()) { |
| 262 | // deserialization |
| 263 | if (str.size() != sizeof(uint64_t)) { |
| 264 | std::cerr << "value corruption\n"; |
| 265 | return false; |
| 266 | } |
| 267 | *value = DecodeFixed64(&str[0]); |
| 268 | return true; |
| 269 | } else { |
| 270 | std::cerr << s.ToString() << std::endl; |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | uint64_t get(const uint32_t w1, const uint32_t w2, const int8_t dist) { |
| 277 | char encoded_key[sizeof(uint64_t)]; |
| 278 | EncodeFixed64(encoded_key, encodeCollocation(w1,w2,dist)); |
| 279 | uint64_t value = default_; |
| 280 | get(std::string(encoded_key, 8), &value); |
| 281 | return value; |
| 282 | } |
| 283 | |
| 284 | virtual void inc(const std::string& key) { |
| 285 | db_->Merge(merge_option_, key, _one_slice); |
| 286 | } |
| 287 | |
| 288 | void inc(const uint64_t key) { |
| 289 | char encoded_key[sizeof(uint64_t)]; |
| 290 | EncodeFixed64(encoded_key, key); |
| 291 | db_->Merge(merge_option_, std::string(encoded_key, 8), _one_slice); |
| 292 | } |
| 293 | |
| 294 | virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist); |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 295 | void dump(uint32_t w1, uint32_t w2, int8_t dist); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 296 | vector<Collocator> get_collocators(uint32_t w1, vocab_entry *vocab, uint64_t total); |
| 297 | string collocators2json(vector<Collocator> collocators); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 298 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 299 | // mapped to a rocksdb Merge operation |
| 300 | virtual bool add(const std::string& key, uint64_t value) { |
| 301 | char encoded[sizeof(uint64_t)]; |
| 302 | EncodeFixed64(encoded, value); |
| 303 | Slice slice(encoded, sizeof(uint64_t)); |
| 304 | auto s = db_->Merge(merge_option_, key, slice); |
| 305 | |
| 306 | if (s.ok()) { |
| 307 | return true; |
| 308 | } else { |
| 309 | std::cerr << s.ToString() << std::endl; |
| 310 | return false; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist); |
| 315 | }; |
| 316 | |
| 317 | rocksdb::Collocators::Collocators(const char *db_name) { |
| 318 | std::cout << "Test merge-based counters... " << db_name << "\n"; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 319 | // merge_option_.sync = true; |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 320 | db_ = OpenDb(db_name); |
| 321 | assert(db_); |
| 322 | uint64_t one = 1; |
| 323 | EncodeFixed64(_one, one); |
| 324 | _one_slice = Slice(_one, sizeof(uint64_t)); |
| 325 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 326 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 327 | rocksdb::CollocatorIterator::~CollocatorIterator() { |
| 328 | std::cout << "destroying itera\n"; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 331 | rocksdb::Collocators::~Collocators() { |
| 332 | std::cout << "destroying coll\n"; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 335 | void rocksdb::Collocators::inc(const uint32_t w1, const uint32_t w2, const uint8_t dist) { |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 336 | inc(encodeCollocation(w1, w2, dist)); |
| 337 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 338 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 339 | std::shared_ptr<DB> rocksdb::Collocators::OpenDb(const char *dbname) { |
| 340 | std::cout << "Test merge-based counters... " << dbname << "\n"; |
| 341 | DB* db; |
| 342 | Options options; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 343 | |
| 344 | |
| 345 | options.env->SetBackgroundThreads(4); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 346 | options.create_if_missing = true; |
| 347 | options.merge_operator = std::make_shared<CountMergeOperator>(); |
| 348 | options.max_successive_merges = 0; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 349 | // options.prefix_extractor.reset(NewFixedPrefixTransform(8)); |
| 350 | options.IncreaseParallelism(70); |
| 351 | // options.OptimizeLevelStyleCompaction(); |
| 352 | options.max_write_buffer_number = 48; |
| 353 | options.max_background_jobs = 48; |
| 354 | options.allow_concurrent_memtable_write=true; |
| 355 | // options.memtable_factory.reset(rocksdb::NewHashLinkListRepFactory(200000)); |
| 356 | // options.enable_write_thread_adaptive_yield = 1; |
| 357 | // options.allow_concurrent_memtable_write = 1; |
| 358 | // options.memtable_factory.reset(new rocksdb::SkipListFactory); |
| 359 | // options.write_buffer_size = 1 << 22; |
| 360 | // options.allow_mmap_reads = true; |
| 361 | // options.allow_mmap_writes = true; |
| 362 | options.max_background_compactions = 40; |
| 363 | // BlockBasedTableOptions table_options; |
| 364 | // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false)); |
| 365 | // options.bloom_locality = 1; |
| 366 | // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024); |
| 367 | // table_options.block_cache = cache; |
| 368 | // options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 369 | Status s; |
| 370 | // DestroyDB(dbname, Options()); |
| 371 | s = DB::Open(options, dbname, &db); |
| 372 | if (!s.ok()) { |
| 373 | std::cerr << s.ToString() << std::endl; |
| 374 | assert(false); |
| 375 | } |
| 376 | return std::shared_ptr<DB>(db); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 379 | CollocatorIterator* rocksdb::Collocators::SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) { |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 380 | ReadOptions options; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 381 | options.prefix_same_as_start = true; |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 382 | char prefixc[sizeof(uint64_t)]; |
| 383 | EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist)); |
| 384 | Iterator *it = db_->NewIterator(options); |
| 385 | CollocatorIterator *cit = new CollocatorIterator(it); |
| 386 | cit->Seek(std::string(prefixc,3));// it->Valid() && it->key().starts_with(std::string(prefixc,3)); it->Next()) { |
| 387 | cit->setPrefix(prefixc); |
| 388 | return cit; |
| 389 | } |
| 390 | |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 391 | void rocksdb::Collocators::dump(uint32_t w1, uint32_t w2, int8_t dist) { |
| 392 | auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist)); |
| 393 | for (; it->isValid(); it->Next()) { |
| 394 | uint64_t value = it->intValue(); |
| 395 | uint64_t key = it->intKey(); |
| 396 | std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value << std::endl; |
| 397 | } |
| 398 | std::cout << "ready dumping\n"; |
| 399 | } |
| 400 | |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 401 | double calculateLLR(uint64_t f_X_, uint64_t uintN, uint64_t f_X_Y_, uint64_t f_Y_) { |
| 402 | double f_e_, f_o_; |
| 403 | double A=0.0, B=0.0, C=0.0, D=0.0, N=0.0; |
| 404 | double LLR=0.0, statVal=0.0, minusDiffCoeff=0.0; |
| 405 | double BlogB=0.0, ClogC=0.0; |
| 406 | |
| 407 | N = (double)uintN; |
| 408 | A = (double)f_X_Y_; |
| 409 | B = (double)f_X_ -A; |
| 410 | C = (double)f_Y_ -A; |
| 411 | D = (double)N -A-B-C;; |
| 412 | |
| 413 | if (B > 0.) BlogB = B*log(B); |
| 414 | if (C > 0.) ClogC = C*log(C); |
| 415 | |
| 416 | if ((A>0.) && (D>0.) && (N>0.)) { |
| 417 | f_e_ = (double)f_X_ /(double)N; |
| 418 | f_o_ = (double)f_X_Y_/(double)f_Y_; |
| 419 | |
| 420 | minusDiffCoeff = |
| 421 | ( f_X_==0 ? (double)((-1)*f_X_Y_) : |
| 422 | ( f_X_Y_==0 ? (double)((+1)*f_X_) : |
| 423 | (f_e_-f_o_)/(f_e_+f_o_) |
| 424 | ) |
| 425 | ); |
| 426 | |
| 427 | /* log likelihood ratio */ |
| 428 | LLR = 2*( A*log(A) |
| 429 | +BlogB |
| 430 | +ClogC |
| 431 | +D*log(D) |
| 432 | -(A+B)*log(A+B) |
| 433 | -(A+C)*log(A+C) |
| 434 | -(B+D)*log(B+D) |
| 435 | -(C+D)*log(C+D) |
| 436 | +N*log(N) |
| 437 | ); |
| 438 | } |
| 439 | return(minusDiffCoeff > 0 ? 0 : (statVal=LLR)); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | bool sortByNpmi(const Collocator &lhs, const Collocator &rhs) { return lhs.npmi > rhs.npmi; } |
| 444 | bool sortByLfmd(const Collocator &lhs, const Collocator &rhs) { return lhs.lfmd > rhs.lfmd; } |
| 445 | |
| 446 | std::vector<Collocator> rocksdb::Collocators::get_collocators(uint32_t w1, vocab_entry *vocab, uint64_t total) { |
| 447 | _vocab = vocab; |
| 448 | std::vector<Collocator> collocators; |
| 449 | uint64_t w2, last_w2 = 0xffffffffffffffff; |
| 450 | uint64_t sum = 0, total_w1 = 0; |
| 451 | for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) { |
| 452 | uint64_t value = it->intValue(), |
| 453 | key = it->intKey(); |
| 454 | w2 = W2(key); |
| 455 | total_w1 += value; |
| 456 | if(last_w2 == 0xffffffffffffffff) last_w2 = w2; |
| 457 | if (w2 != last_w2) { |
| 458 | double pmi = log2( total * ((double) sum) / |
| 459 | (AVG_WINDOW_SIZE * ((double)vocab[w1].freq) * ((double)vocab[last_w2].freq) )); |
| 460 | // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics. In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625 |
| 461 | // double md = log2(pow((double)sum * AVG_WINDOW_SIZE / total, 2) / (AVG_WINDOW_SIZE * ((double)vocab[w1].freq/total) * ((double)vocab[last_w2].freq/total))); |
| 462 | double md = log2((double)sum * sum / ((double) total * AVG_WINDOW_SIZE * AVG_WINDOW_SIZE * vocab[w1].freq * vocab[last_w2].freq)); |
| 463 | collocators.push_back ( {last_w2, sum, pmi, pmi / (-log2(((double) sum / AVG_WINDOW_SIZE / total))), /* normalize to [-1,1] */ |
| 464 | calculateLLR(vocab[w1].freq, total, sum, vocab[last_w2].freq), md, md + log2((double)sum / AVG_WINDOW_SIZE / total), pmi*sum/total/AVG_WINDOW_SIZE} ); |
| 465 | last_w2 = w2; |
| 466 | sum = value; |
| 467 | } else { |
| 468 | sum += value; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | sort(collocators.begin(), collocators.end(), sortByLfmd); |
| 473 | |
| 474 | int i=0; |
| 475 | for (Collocator c : collocators) { |
| 476 | if(i++>10) break; |
| 477 | std::cout << "w1:" << vocab[w1].word << ", w2:" << vocab[c.w2].word |
| 478 | << "\t f(w1):" << vocab[w1].freq |
| 479 | << "\t f(w2):" << vocab[c.w2].freq |
| 480 | << "\t f(w1, x):" << total_w1 |
| 481 | << "\t f(w1, w2):" << c.sum |
| 482 | << "\t pmi:" << c.pmi |
| 483 | << "\t npmi:" << c.npmi |
| 484 | << "\t llr:" << c.llr |
| 485 | << "\t md:" << c.md |
| 486 | << "\t lfmd:" << c.lfmd |
| 487 | << "\t fpmi:" << c.fpmi |
| 488 | << "\t total:" << total |
| 489 | << std::endl; |
| 490 | } |
| 491 | return collocators; |
| 492 | } |
| 493 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 494 | rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); } |
| 495 | rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); } |
| 496 | rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); } |
| 497 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 498 | }; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 499 | |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 500 | string rocksdb::Collocators::collocators2json(vector<Collocator> collocators) { |
| 501 | ostringstream s; |
| 502 | s << "["; |
| 503 | bool first = true; |
| 504 | for (Collocator c : collocators) { |
| 505 | if(!first) |
| 506 | s << ",\n"; |
| 507 | else |
| 508 | first = false; |
| 509 | s << "{" |
| 510 | "\"word\":\"" << string(_vocab[c.w2].word) << "\"," << |
| 511 | "\"rank\":" << c.w2 << "," << |
| 512 | "\"npmi\":" << c.npmi << "," << |
| 513 | "\"llr\":" << c.llr << "," << |
| 514 | "\"lfmd\":" << c.lfmd << "," << |
| 515 | "\"mi\":" << c.fpmi << |
| 516 | "}"; |
| 517 | } |
| 518 | s << "]\n"; |
| 519 | return s.str(); |
| 520 | } |
| 521 | |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 522 | typedef rocksdb::Collocators COLLOCATORS; |
| 523 | |
| 524 | extern "C" { |
| 525 | COLLOCATORS *open_collocators(char *dbname) { |
| 526 | return new rocksdb::Collocators(dbname); |
| 527 | } |
| 528 | |
| 529 | void inc_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) { |
| 530 | db->inc(w1, w2, dist); |
| 531 | } |
| 532 | |
| 533 | void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) { |
| 534 | db->dump(w1, w2, dist); |
| 535 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame^] | 536 | |
| 537 | void get_collocators(COLLOCATORS *db, uint32_t w1, vocab_entry *vocab, uint64_t total) { |
| 538 | db->get_collocators(w1, vocab, total); |
| 539 | } |
| 540 | |
| 541 | const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1, vocab_entry *vocab, uint64_t total) { |
| 542 | return strdup(db->collocators2json(db->get_collocators(w1, vocab, total)).c_str()); |
| 543 | } |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 544 | } |