bwtree_datastore.cc 3.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Copyright (c) 2017, Los Alamos National Security, LLC.
// All rights reserved.
#include "bwtree_datastore.h"
#include "kv-config.h"
#include <chrono>
#include <iostream>
#include <boost/filesystem.hpp>

using namespace std::chrono;

BwTreeDataStore::BwTreeDataStore() :
  AbstractDataStore(Duplicates::IGNORE, false, false) {
  _tree = NULL;
};

BwTreeDataStore::BwTreeDataStore(Duplicates duplicates, bool eraseOnGet, bool debug) :
  AbstractDataStore(duplicates, eraseOnGet, debug) {
  _tree = NULL;
};

BwTreeDataStore::~BwTreeDataStore() {
  // deleting BwTree can cause core dump
Matthieu Dorier's avatar
Matthieu Dorier committed
23
24
25
#if 0 // letting leak, for now
    delete _tree;
#endif
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
};

void BwTreeDataStore::createDatabase(std::string db_name) {
  _tree = new BwTree<ds_bulk_t, ds_bulk_t, 
		     ds_bulk_less, ds_bulk_equal, ds_bulk_hash,
		     ds_bulk_equal, ds_bulk_hash>();
  if (_debug) {
    _tree->SetDebugLogging(1);
  }
  else {
    _tree->SetDebugLogging(0);
  }
  _tree->UpdateThreadLocal(1);
  _tree->AssignGCID(0);
};

bool BwTreeDataStore::put(const ds_bulk_t &key, const ds_bulk_t &data) {
  std::vector<ds_bulk_t> values;
  bool success = false;

  if(!_tree) return false;
  
  if (_duplicates == Duplicates::ALLOW) {
    success = _tree->Insert(key, data);
  }
  else if (_duplicates == Duplicates::IGNORE) {
    _tree->GetValue(key, values);
    bool duplicate_key = (values.size() != 0);
    if (duplicate_key) {
      // silently ignore
      success = true;
    }
    else {
      success = _tree->Insert(key, data);
    }
  }
  else {
    std::cerr << "BwTreeDataStore::put: Unexpected Duplicates option = " << int32_t(_duplicates) << std::endl;
  }
  
  return success;
};

bool BwTreeDataStore::get(const ds_bulk_t &key, ds_bulk_t &data) {
  std::vector<ds_bulk_t> values;
  bool success = false;

  _tree->GetValue(key, values);
  if (values.size() == 1) {
    data = std::move(values.front());
    success = true;
  }
  else if (values.size() > 1) {
    // this should only happen if duplicates are allowed
    if (_duplicates == Duplicates::ALLOW) {
      data = std::move(values.front()); // caller is asking for just 1
      success = true;
    }
  }

  if (success && _eraseOnGet) {
    bool status = _tree->Delete(key, data);
    if (!status) {
      success = false;
      std::cerr << "BwTreeDataStore::get: BwTree error on delete (eraseOnGet) = " << status << std::endl;
    }
  }

  return success;
};

Matthieu Dorier's avatar
Matthieu Dorier committed
97
98
99
100
101
102
bool BwTreeDataStore::erase(const ds_bulk_t &key) {
    ds_bulk_t data;
    if(!get(key,data)) return false;
    return _tree->Delete(key,data);
}

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
bool BwTreeDataStore::get(const ds_bulk_t &key, std::vector<ds_bulk_t> &data) {
  std::vector<ds_bulk_t> values;
  bool success = false;

  _tree->GetValue(key, values);
  if (values.size() > 1) {
    // this should only happen if duplicates are allowed
    if (_duplicates == Duplicates::ALLOW) {
      data = std::move(values);
      success = true;
    }
  }
  else {
    data = std::move(values);
    success = true;
  }
  
  return success;
};

123
void BwTreeDataStore::set_in_memory(bool enable)
124
125
{};

126
std::vector<ds_bulk_t> BwTreeDataStore::vlist_keys(const ds_bulk_t &start, size_t count, const ds_bulk_t &prefix)
127
128
{
    std::vector<ds_bulk_t> keys;
129
#if 0
130
131
132
133
134
    auto it = _tree->Begin(start);
    while (it.IsEnd() == false) {
	/* BUG: bwtree doesn't support "list keys" or "get a key" */
	//keys.push_back(it.GetLeafNode());
    }
135
#endif
136
137
    return keys;
}
138

139
std::vector<std::pair<ds_bulk_t,ds_bulk_t>> BwTreeDataStore::vlist_keyvals(const ds_bulk_t &start, size_t count, const ds_bulk_t &prefix)
140
141
142
143
144
145
146
147
148
149
150
{
    std::vector<std::pair<ds_bulk_t,ds_bulk_t>> keyvals;
#if 0
    auto it = _tree->Begin(start);
    while (it.IsEnd() == false) {
	/* BUG: bwtree doesn't support "list keys" or "get a key" */
    }
#endif

    return keyvals;
}