summarylogtreecommitdiffstats
path: root/leveldb_mcpe.cpp
blob: 7527a131fafaa10a2f61524048f2daec84f5a0b6 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//Rubisks trying to wrap leveldb-mcpe
#include <leveldb/cache.h>
#include <leveldb/comparator.h>
#include <leveldb/db.h>
#include <leveldb/env.h>
#include <leveldb/filter_policy.h>
#include <leveldb/write_batch.h>
#include <leveldb/zlib_compressor.h>
#define BOOST_PYTHON_STATIC_LIB
#define BOOST_SYSTEM_STATIC_LIB
#define BOOST_ALL_STATIC_LIB

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/object.hpp>
#include <boost/python/str.hpp>
#include <boost/python/extract.hpp>
#include <boost/system/config.hpp>
#include <boost/python.hpp>
#include <stdint.h>
#include <stdio.h>

namespace bp = boost::python;

//Exception
class LevelDBException {
public:
	LevelDBException(const std::string msg)
		: message(msg) {}

	std::string getMessage() const {
		return message;
	}
private:
	std::string message;
};

static void ExceptionTranslator(const LevelDBException &err) {
	PyErr_SetString(PyExc_RuntimeError, err.getMessage().c_str());
};

struct IteratorWrapper {
	leveldb::Iterator* _it;
	
	IteratorWrapper(leveldb::Iterator* it){
		this->_it = it;
	}

  ~IteratorWrapper()
  {
    delete _it;
  }

	bool Valid()
	{
		return this->_it->Valid();
	}

	void SeekToFirst()
	{
		this->_it->SeekToFirst();
	}

	void SeekToLast()
	{
		this->_it->SeekToLast();
	}

	void Seek(PyObject* _target)
	{
		const std::string target = bp::extract<const std::string>(_target);
		return this->_it->Seek(target);
	}

	void Next()
	{
		this->_it->Next();
	}

	void Prev()
	{
		this->_it->Prev();
	}
	
	std::string key()
	{
		return this->_it->key().ToString();
	}

	std::string value()
	{
		return this->_it->value().ToString();
	}

	void status()
	{
		leveldb::Status s = this->_it->status();

		if (!s.ok()){
			throw LevelDBException(s.ToString());
		}
	}
};

//write_batch.h
struct WriteBatchWrapper
{
	leveldb::WriteBatch* _wb;

	WriteBatchWrapper(){
		_wb = new leveldb::WriteBatch();
        }

  ~WriteBatchWrapper(){
    delete _wb;
  }

	void Put(PyObject* _key, PyObject* _value)
	{
		const std::string key = bp::extract<const std::string>(_key);
		const std::string value = bp::extract<const std::string>(_value);
		this->_wb->Put(key, value);
	}

	void Delete(PyObject* _key)
	{
		const std::string key = bp::extract<const std::string>(_key);
		this->_wb->Delete(key);
	}
};


//leveldb::DB wrapper
class DBWrap
{
public:
  leveldb::DB * _db;

  DBWrap(PyObject* _options, PyObject* _name) //Open(options, name, db)
    : _db(NULL)
  {
    leveldb::Options options = bp::extract<leveldb::Options&>(_options);
	options.compressors[0] = new leveldb::ZlibCompressor();
    std::string name = bp::extract<std::string>(_name);
    leveldb::Status s = leveldb::DB::Open(options, name, &_db);

    if(!s.ok())
    {
      throw LevelDBException(s.ToString());
    }
  }

  ~DBWrap()
  {
    delete _db;
  }

  void Put(PyObject* _options, PyObject* _key, PyObject* _value) //Put(options, key, value)
  {
	  const leveldb::WriteOptions& options = bp::extract<const leveldb::WriteOptions&>(_options);
	  const std::string key = bp::extract<const std::string>(_key);
	  const std::string value = bp::extract<const std::string>(_value);
	  leveldb::Status s = this->_db->Put(options, key, value);

	  if (!s.ok()){
		  throw LevelDBException(s.ToString());
	  }
  }

  void Delete(PyObject* _options, PyObject* _key) //Delete(options, key)
  {
	  const leveldb::WriteOptions& options = bp::extract<const leveldb::WriteOptions&>(_options);
	  const std::string key = bp::extract<const std::string>(_key);
	  leveldb::Status s = this->_db->Delete(options, key);

	  if (!s.ok()){
		  throw LevelDBException(s.ToString());
	  }
  }

  std::string Get(PyObject* _options, PyObject* _key) //Delete(options, key)
  {
	  const leveldb::ReadOptions& options = bp::extract<const leveldb::ReadOptions&>(_options);
	  const std::string key = bp::extract<const std::string>(_key);
	  std::string value;
	  leveldb::Status s = this->_db->Get(options, key, &value);

	  if (!s.ok()){
		  throw LevelDBException(s.ToString());
	  }
	  return value;
  }


  void Write(PyObject* _options, PyObject* _updates)
  {
	  const leveldb::WriteOptions& options = bp::extract<const leveldb::WriteOptions&>(_options);
	  WriteBatchWrapper& __updates = bp::extract<WriteBatchWrapper&>(_updates);
	  leveldb::Status s = this->_db->Write(options, __updates._wb);

	  if (!s.ok()){
		  throw LevelDBException(s.ToString());
	  }
  }


  IteratorWrapper* NewIterator(const leveldb::ReadOptions& options)
  {
	  leveldb::Iterator* it = this->_db->NewIterator(options);
	  return new IteratorWrapper(it);
  }

  const leveldb::Snapshot* GetSnapshot()
  {
	  return _db->GetSnapshot();
  }

  void GetApproximateSizes(const leveldb::Range* range, int n, uint64_t* sizes)
  {
    this->_db->GetApproximateSizes(range, n, sizes);
  }


  void ReleaseSnapshot(const leveldb::Snapshot* snapshot)
  {
    this->_db->ReleaseSnapshot(snapshot);
  }

  bool GetProperty(const leveldb::Slice& property, std::string* value)
  {
    return this->_db->GetProperty(property, value);
  }

  void CompactRange(const leveldb::Slice* begin, const leveldb::Slice* end)
  {
    this->_db->CompactRange(begin, end);
  }
};

class RepairWrapper{
public:
	~RepairWrapper(){};

	RepairWrapper(PyObject* _path, PyObject* _options)
	{
		const std::string path = bp::extract<const std::string>(_path);
		const leveldb::Options& options = bp::extract<const leveldb::Options&>(_options);
		leveldb::Status s = leveldb::RepairDB(path, options);

		if (!s.ok()){
			throw LevelDBException(s.ToString());
		}
	}
};

BOOST_PYTHON_MODULE(leveldb_mcpe)
{
  //Exceptions
  bp::register_exception_translator<LevelDBException>(&ExceptionTranslator);

  //leveldb/db.h
  bp::class_<DBWrap, boost::noncopyable>("DB", bp::init<PyObject*, PyObject*>())
    .def("Put", &DBWrap::Put)
    .def("Delete", &DBWrap::Delete)
    .def("Write", &DBWrap::Write)
    .def("Get", &DBWrap::Get)
    .def("NewIterator", &DBWrap::NewIterator, 
		bp::return_value_policy<bp::manage_new_object>())
    .def("GetSnapshot", &DBWrap::GetSnapshot,
		bp::return_value_policy<bp::reference_existing_object>())
    .def("ReleaseSnapshot", &DBWrap::ReleaseSnapshot)
    .def("GetProperty", &DBWrap::GetProperty)
	.def("GetApproximateSizes", &DBWrap::GetApproximateSizes)
	.def("CompactRange", &DBWrap::CompactRange);

  //leveldb/options.h
  bp::class_<leveldb::Options>("Options", bp::init<>())
	  //.def_readonly("comparator", &leveldb::Options::comparator) //Pointer, maybe needs better wrapper? Untested
	  .def_readwrite("create_if_missing", &leveldb::Options::create_if_missing)
	  .def_readwrite("error_if_exists", &leveldb::Options::error_if_exists)
	  //.def_readwrite("env", &leveldb::Options::env) //Pointer, maybe needs better wrapper? Untested
	  //.def_readwrite("info_log", &leveldb::Options::info_log) //Pointer, maybe needs better wrapper? Untested
	  .def_readwrite("write_buffer_size", &leveldb::Options::write_buffer_size)
	  .def_readwrite("max_open_files", &leveldb::Options::max_open_files)
	  //.def_readwrite("block_cache", &leveldb::Options::block_cache) //Pointer, maybe needs better wrapper? Untested
	  .def_readwrite("block_size", &leveldb::Options::block_size)
	  .def_readwrite("block_restart_interval", &leveldb::Options::block_restart_interval)
	  //TODO setup a way to include compressor array //Pointer, maybe needs better wrapper? Untested
	  //.def_readonly("filter_policy", &leveldb::Options::filter_policy); //Pointer, maybe needs better wrapper? Untested
	;
  
  bp::class_<leveldb::ReadOptions>("ReadOptions", bp::init<>())
	  .def_readwrite("verify_checksums", &leveldb::ReadOptions::verify_checksums)
	  .def_readwrite("fill_cache", &leveldb::ReadOptions::fill_cache)
	  .add_property("snapshot",
		bp::make_getter(&leveldb::ReadOptions::snapshot),
		bp::make_setter(&leveldb::ReadOptions::snapshot))
	;

  bp::class_<leveldb::WriteOptions>("WriteOptions", bp::init<>())
	  .def_readwrite("sync", &leveldb::WriteOptions::sync)
	;

  //leveldb/iterator.h
  bp::class_<IteratorWrapper, boost::noncopyable>("Iterator", bp::no_init)
	  .def("Valid", &IteratorWrapper::Valid)
	  .def("SeekToFirst", &IteratorWrapper::SeekToFirst)
	  .def("SeekToLast", &IteratorWrapper::SeekToLast)
	  .def("Seek", &IteratorWrapper::Seek)
	  .def("Next", &IteratorWrapper::Next)
	  .def("Prev", &IteratorWrapper::Prev)
	  .def("key", &IteratorWrapper::key)
	  .def("value", &IteratorWrapper::value)
	  .def("status", &IteratorWrapper::status)
	  ;

  //leveldb/write_batch.h
  bp::class_<WriteBatchWrapper>("WriteBatch", bp::init<>())
	  .def("Put", &WriteBatchWrapper::Put)
	  .def("Delete", &WriteBatchWrapper::Delete)
	  ;

  bp::class_<leveldb::Snapshot, boost::noncopyable>("Snapshot", bp::no_init);

  bp::class_<RepairWrapper>("RepairWrapper", bp::init<PyObject*, PyObject*>());
}