aboutsummaryrefslogtreecommitdiff
path: root/db/memtable.cu
blob: 241a35b9c9968b3f226e28d609e421509b1b34b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "db/memtable.cuh"
#include "db/dbformat.h"
#include "leveldb/comparator.h"
#include "leveldb/env.h"
#include "leveldb/iterator.h"
#include "util/coding.cuh"

namespace leveldb {

static Slice GetLengthPrefixedSlice(const char* data) {
  uint32_t len;
  const char* p = data;
  p = GetVarint32Ptr(p, p + 5, &len);  // +5: we assume "p" is not corrupted
  return Slice(p, len);
}

MemTable::MemTable(const InternalKeyComparator& comparator)
    : comparator_(comparator), refs_(0) {

}

MemTable::~MemTable() { assert(refs_ == 0); }

size_t MemTable::ApproximateMemoryUsage() { return arena_.MemoryUsage(); }

int MemTable::KeyComparator::operator()(const char* aptr,
                                        const char* bptr) const {
  // Internal keys are encoded as length-prefixed strings.
  Slice a = GetLengthPrefixedSlice(aptr);
  Slice b = GetLengthPrefixedSlice(bptr);
  return comparator.Compare(a, b);
}

// Encode a suitable internal key target for "target" and return it.
// Uses *scratch as scratch space, and the returned pointer will point
// into this scratch space.
static const char* EncodeKey(std::string* scratch, const Slice& target) {
  scratch->clear();
  PutVarint32(scratch, target.size());
  scratch->append(target.data(), target.size());
  return scratch->data();
}

class MemTableIterator : public Iterator {
 public:
  explicit MemTableIterator(MemTable::Table* table) : iter_(table) {}

  MemTableIterator(const MemTableIterator&) = delete;
  MemTableIterator& operator=(const MemTableIterator&) = delete;

  ~MemTableIterator() override = default;

  bool Valid() const override { return iter_.Valid(); }
  void Seek(const Slice& k) override { iter_.Seek(EncodeKey(&tmp_, k)); }
  void SeekToFirst() override { iter_.SeekToFirst(); }
  void SeekToLast() override { iter_.SeekToLast(); }
  void Next() override { iter_.Next(); }
  void Prev() override { iter_.Prev(); }
  Slice key() const override { return GetLengthPrefixedSlice(iter_.key()); }
  Slice value() const override {
    Slice key_slice = GetLengthPrefixedSlice(iter_.key());
    return GetLengthPrefixedSlice(key_slice.data() + key_slice.size());
  }

  Status status() const override { return Status::OK(); }

 private:
  MemTable::Table::Iterator iter_;
  std::string tmp_;  // For passing to EncodeKey
};

Iterator* MemTable::NewIterator() { return new MemTableIterator(&table_); }


__global__ void Add_(MemTable * mtb, size_t encoded_len, char * encode_data) {
  char* buf = mtb->arena_.Allocate(encoded_len);
  memcpy(buf, encode_data, encoded_len);
  mtb->table_.Insert(buf);
}


void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
                   const Slice& value) {
  // Format of an entry is concatenation of:
  //  key_size     : varint32 of internal_key.size()
  //  key bytes    : char[internal_key.size()]
  //  tag          : uint64((sequence << 8) | type)
  //  value_size   : varint32 of value.size()
  //  value point  : point to host memory
  size_t key_size = key.size();
  size_t val_size = value.size();
  size_t internal_key_size = key_size + 8;
  //const size_t encoded_len = VarintLength(internal_key_size) +
  //                           internal_key_size + VarintLength(val_size) +
  //                           val_size;
  const size_t encoded_len = VarintLength(internal_key_size) + internal_key_size +
                             VarintLength(val_size) + 8;


    char * key_mem = this->arena_.Allocate( key_size);
    std::memcpy(key_mem, key.data(), key_size);

    //char * tag_mem = key_mem + key_size;
    //EncodeFixed64(tag_mem, (s << 8) | type);

    char * val_mem = this->arena_.Allocate(val_size);
    std::memcpy(val_mem, value.data(), val_size);

  char * insert_val = new char[encoded_len], *cuda_insert = nullptr;
  //  EncodeVarint32(insert_val, encoded_len);
  char * p = EncodeVarint32(insert_val + 8, internal_key_size);
  //EncodeFixed64(p, reinterpret_cast<uint64_t>(key_mem));
  memcpy(p, key_mem, key_size);
  p += key_size;
  EncodeFixed64(p, (s << 8) | type );
  p += 8;
  //EncodeFixed64(p, reinterpret_cast<uint64_t>(val_size));
  p = EncodeVarint32(p, val_size);
  EncodeFixed64(p, reinterpret_cast<uint64_t>(val_mem));

  assert(p + 8 == insert_val + encoded_len);

  cudaMalloc((void**)&cuda_insert, encoded_len);
  cudaMemcpy(cuda_insert, insert_val, encoded_len, cudaMemcpyHostToDevice);

  Add_<<<1, 1>>>(this, encoded_len, cuda_insert);
  cudaDeviceSynchronize();

  cudaFree(cuda_insert);
  delete[] insert_val;
}

__global__ void Get_(MemTable * met, char * memkey, char ** data, size_t* malloc_size) {
  *data = nullptr;
  auto iter = met->getIter();
  iter.Seek(memkey);
  if (iter.Valid()) {
    // entry format is:
    //    klength  varint32
    //    userkey  char[klength]
    //    tag      uint64
    //    vlength  varint32
    //    value    char[vlength]
    // Check that it belongs to same user key.  We do not check the
    // sequence number since the Seek() call above should have skipped
    // all entries with overly large sequence numbers.
    size_t key_size;
    const char *entry = iter.key();
    const char * p = GetVarint32PtrCuda(entry, entry + 5,
                       reinterpret_cast<uint32_t*>(&key_size));
    p = GetVarint32PtrCuda(p + key_size, p + key_size + 5, nullptr);
    *malloc_size = (p - entry + 8);
    cudaMalloc((void**)*data, *malloc_size);
    memcpy(*data, entry, *malloc_size);
  }
}

/*
__global__ void Get_Phase2() {
    // Correct user key
    const uint64_t tag = DecodeFixed64Cuda(key_ptr + key_length - 8);
    switch (static_cast<ValueType>(tag & 0xff)) {
      case kTypeValue: {
        SizedString v = GetLengthPrefixedSliceCuda(key_ptr + key_length);
        *value=v;
        *ret = true;
        break;
      }
      case kTypeDeletion:
        //*s = Status::NotFound(Slice());
        *is_not_found = true;
        *ret = true;
        break;
    }
}
*/

bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {

  Slice memkey = key.memtable_key();
  char * cuda_mem_key = nullptr;
  cudaMalloc((void**)&cuda_mem_key, memkey.size());
  cudaMemcpy(cuda_mem_key, memkey.data(), memkey.size(), cudaMemcpyHostToDevice);

  char ** cuda_skiplist_key = nullptr;
  cudaMalloc((void**)&cuda_skiplist_key, sizeof(char *));

  size_t * cuda_malloc_size = nullptr;
  cudaMalloc((void**)&cuda_malloc_size, sizeof(cuda_malloc_size));

  Get_<<<1,1>>>(this, cuda_mem_key, cuda_skiplist_key, cuda_malloc_size);
  cudaDeviceSynchronize();
  cudaFree(cuda_mem_key);

  if (*cuda_skiplist_key == nullptr) {
    cudaFree(cuda_skiplist_key);
    cudaFree(cuda_malloc_size);
    return false;
  }
  auto * malloc_size = new size_t;
  cudaMemcpy(malloc_size, cuda_malloc_size, sizeof(size_t), cudaMemcpyDeviceToHost);

  char * entry = this->host_arena_.Allocate(*malloc_size);
  cudaMemcpy(entry, cuda_skiplist_key, *malloc_size, cudaMemcpyDeviceToHost);

  cudaFree(cuda_skiplist_key);
  cudaFree(cuda_malloc_size);
  delete malloc_size;

    uint32_t key_length;
    const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length);
    if (comparator_.comparator.user_comparator()->Compare(
            Slice(key_ptr, key_length - 8), key.user_key()) == 0) {
      // Correct user key
      const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8);
      switch (static_cast<ValueType>(tag & 0xff)) {
        case kTypeValue: {
          // HOW CAN WE GET IT?
          Slice v = GetLengthPrefixedSlice(key_ptr + key_length);
          value->assign(v.data(), v.size());
          return true;
        }
        case kTypeDeletion:
          *s = Status::NotFound(Slice());
          return true;
      }
    }


/*  bool * cuda_return_value = nullptr, *return_value = new bool, *is_not_found = nullptr;
  SizedString * return_string_value = nullptr;
  cudaMalloc((void**)&cuda_return_value, sizeof(bool));
  cudaMalloc((void**)&is_not_found, sizeof(bool));
  cudaMallocManaged((void**)&return_string_value, sizeof(SizedString));

  Get_<<<1, 1>>>(this, cuda_return_value, cuda_mem_key, nullptr, is_not_found);
  cudaDeviceSynchronize();

  cudaMemcpy(return_value, cuda_return_value, sizeof(bool), cudaMemcpyDeviceToHost);

  if (*return_value) {
    if (*is_not_found) {
      *s = Status::NotFound(Slice());
    } else {
      auto * local_sized_string = new SizedString();
      cudaMemcpy(local_sized_string, return_string_value, sizeof(SizedString), cudaMemcpyDeviceToHost);

      char * local_string = new char[local_sized_string->length];
      cudaMemcpy(local_string, local_sized_string->data, local_sized_string->length, cudaMemcpyDeviceToHost);
      value->assign(local_string, local_sized_string->length);
      delete local_sized_string;
      delete [] local_string;
    }
  }

  bool rvalue = *return_value;
  delete return_value;
  cudaFree(is_not_found);
  cudaFree(cuda_return_value);
  cudaFree(cuda_mem_key);
  cudaFree(return_string_value);*/
    return false;
}

}  // namespace leveldb