Ton

Форк
0
/
MemoryKeyValue.cpp 
83 строки · 2.3 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

4
    TON Blockchain Library is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8

9
    TON Blockchain Library is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13

14
    You should have received a copy of the GNU Lesser General Public License
15
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19
#include "td/db/MemoryKeyValue.h"
20

21
#include "td/utils/format.h"
22

23
namespace td {
24
Result<MemoryKeyValue::GetStatus> MemoryKeyValue::get(Slice key, std::string &value) {
25
  auto it = map_.find(key);
26
  if (it == map_.end()) {
27
    return GetStatus::NotFound;
28
  }
29
  value = it->second;
30
  return GetStatus::Ok;
31
}
32
Status MemoryKeyValue::set(Slice key, Slice value) {
33
  map_[key.str()] = value.str();
34
  return Status::OK();
35
}
36
Status MemoryKeyValue::erase(Slice key) {
37
  auto it = map_.find(key);
38
  if (it != map_.end()) {
39
    map_.erase(it);
40
  }
41
  return Status::OK();
42
}
43

44
Result<size_t> MemoryKeyValue::count(Slice prefix) {
45
  size_t res = 0;
46
  for (auto it = map_.lower_bound(prefix); it != map_.end(); it++) {
47
    if (Slice(it->first).truncate(prefix.size()) != prefix) {
48
      break;
49
    }
50
    res++;
51
  }
52
  return res;
53
}
54

55
std::unique_ptr<KeyValueReader> MemoryKeyValue::snapshot() {
56
  auto res = std::make_unique<MemoryKeyValue>();
57
  res->map_ = map_;
58
  return std::move(res);
59
}
60

61
std::string MemoryKeyValue::stats() const {
62
  return PSTRING() << "MemoryKeyValueStats{" << tag("get_count", get_count_) << "}";
63
}
64
Status MemoryKeyValue::begin_write_batch() {
65
  UNREACHABLE();
66
}
67
Status MemoryKeyValue::commit_write_batch() {
68
  UNREACHABLE();
69
}
70
Status MemoryKeyValue::abort_write_batch() {
71
  UNREACHABLE();
72
}
73

74
Status MemoryKeyValue::begin_transaction() {
75
  UNREACHABLE();
76
}
77
Status MemoryKeyValue::commit_transaction() {
78
  UNREACHABLE();
79
}
80
Status MemoryKeyValue::abort_transaction() {
81
  UNREACHABLE();
82
}
83
}  // namespace td
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.