~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to src/leveldb/db/c.cc

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-07-16 09:56:24 UTC
  • mfrom: (0.3.11)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20120716095624-azr2w4hbhei1rxmx
Tags: upstream-0.48
ImportĀ upstreamĀ versionĀ 0.48

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
 
// Use of this source code is governed by a BSD-style license that can be
3
 
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
 
 
5
 
#include "leveldb/c.h"
6
 
 
7
 
#include <stdlib.h>
8
 
#include <unistd.h>
9
 
#include "leveldb/cache.h"
10
 
#include "leveldb/comparator.h"
11
 
#include "leveldb/db.h"
12
 
#include "leveldb/env.h"
13
 
#include "leveldb/iterator.h"
14
 
#include "leveldb/options.h"
15
 
#include "leveldb/status.h"
16
 
#include "leveldb/write_batch.h"
17
 
 
18
 
using leveldb::Cache;
19
 
using leveldb::Comparator;
20
 
using leveldb::CompressionType;
21
 
using leveldb::DB;
22
 
using leveldb::Env;
23
 
using leveldb::FileLock;
24
 
using leveldb::Iterator;
25
 
using leveldb::Logger;
26
 
using leveldb::NewLRUCache;
27
 
using leveldb::Options;
28
 
using leveldb::RandomAccessFile;
29
 
using leveldb::Range;
30
 
using leveldb::ReadOptions;
31
 
using leveldb::SequentialFile;
32
 
using leveldb::Slice;
33
 
using leveldb::Snapshot;
34
 
using leveldb::Status;
35
 
using leveldb::WritableFile;
36
 
using leveldb::WriteBatch;
37
 
using leveldb::WriteOptions;
38
 
 
39
 
extern "C" {
40
 
 
41
 
struct leveldb_t              { DB*               rep; };
42
 
struct leveldb_iterator_t     { Iterator*         rep; };
43
 
struct leveldb_writebatch_t   { WriteBatch        rep; };
44
 
struct leveldb_snapshot_t     { const Snapshot*   rep; };
45
 
struct leveldb_readoptions_t  { ReadOptions       rep; };
46
 
struct leveldb_writeoptions_t { WriteOptions      rep; };
47
 
struct leveldb_options_t      { Options           rep; };
48
 
struct leveldb_cache_t        { Cache*            rep; };
49
 
struct leveldb_seqfile_t      { SequentialFile*   rep; };
50
 
struct leveldb_randomfile_t   { RandomAccessFile* rep; };
51
 
struct leveldb_writablefile_t { WritableFile*     rep; };
52
 
struct leveldb_logger_t       { Logger*           rep; };
53
 
struct leveldb_filelock_t     { FileLock*         rep; };
54
 
 
55
 
struct leveldb_comparator_t : public Comparator {
56
 
  void* state_;
57
 
  void (*destructor_)(void*);
58
 
  int (*compare_)(
59
 
      void*,
60
 
      const char* a, size_t alen,
61
 
      const char* b, size_t blen);
62
 
  const char* (*name_)(void*);
63
 
 
64
 
  virtual ~leveldb_comparator_t() {
65
 
    (*destructor_)(state_);
66
 
  }
67
 
 
68
 
  virtual int Compare(const Slice& a, const Slice& b) const {
69
 
    return (*compare_)(state_, a.data(), a.size(), b.data(), b.size());
70
 
  }
71
 
 
72
 
  virtual const char* Name() const {
73
 
    return (*name_)(state_);
74
 
  }
75
 
 
76
 
  // No-ops since the C binding does not support key shortening methods.
77
 
  virtual void FindShortestSeparator(std::string*, const Slice&) const { }
78
 
  virtual void FindShortSuccessor(std::string* key) const { }
79
 
};
80
 
 
81
 
struct leveldb_env_t {
82
 
  Env* rep;
83
 
  bool is_default;
84
 
};
85
 
 
86
 
static bool SaveError(char** errptr, const Status& s) {
87
 
  assert(errptr != NULL);
88
 
  if (s.ok()) {
89
 
    return false;
90
 
  } else if (*errptr == NULL) {
91
 
    *errptr = strdup(s.ToString().c_str());
92
 
  } else {
93
 
    // TODO(sanjay): Merge with existing error?
94
 
    free(*errptr);
95
 
    *errptr = strdup(s.ToString().c_str());
96
 
  }
97
 
  return true;
98
 
}
99
 
 
100
 
static char* CopyString(const std::string& str) {
101
 
  char* result = reinterpret_cast<char*>(malloc(sizeof(char) * str.size()));
102
 
  memcpy(result, str.data(), sizeof(char) * str.size());
103
 
  return result;
104
 
}
105
 
 
106
 
leveldb_t* leveldb_open(
107
 
    const leveldb_options_t* options,
108
 
    const char* name,
109
 
    char** errptr) {
110
 
  DB* db;
111
 
  if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
112
 
    return NULL;
113
 
  }
114
 
  leveldb_t* result = new leveldb_t;
115
 
  result->rep = db;
116
 
  return result;
117
 
}
118
 
 
119
 
void leveldb_close(leveldb_t* db) {
120
 
  delete db->rep;
121
 
  delete db;
122
 
}
123
 
 
124
 
void leveldb_put(
125
 
    leveldb_t* db,
126
 
    const leveldb_writeoptions_t* options,
127
 
    const char* key, size_t keylen,
128
 
    const char* val, size_t vallen,
129
 
    char** errptr) {
130
 
  SaveError(errptr,
131
 
            db->rep->Put(options->rep, Slice(key, keylen), Slice(val, vallen)));
132
 
}
133
 
 
134
 
void leveldb_delete(
135
 
    leveldb_t* db,
136
 
    const leveldb_writeoptions_t* options,
137
 
    const char* key, size_t keylen,
138
 
    char** errptr) {
139
 
  SaveError(errptr, db->rep->Delete(options->rep, Slice(key, keylen)));
140
 
}
141
 
 
142
 
 
143
 
void leveldb_write(
144
 
    leveldb_t* db,
145
 
    const leveldb_writeoptions_t* options,
146
 
    leveldb_writebatch_t* batch,
147
 
    char** errptr) {
148
 
  SaveError(errptr, db->rep->Write(options->rep, &batch->rep));
149
 
}
150
 
 
151
 
char* leveldb_get(
152
 
    leveldb_t* db,
153
 
    const leveldb_readoptions_t* options,
154
 
    const char* key, size_t keylen,
155
 
    size_t* vallen,
156
 
    char** errptr) {
157
 
  char* result = NULL;
158
 
  std::string tmp;
159
 
  Status s = db->rep->Get(options->rep, Slice(key, keylen), &tmp);
160
 
  if (s.ok()) {
161
 
    *vallen = tmp.size();
162
 
    result = CopyString(tmp);
163
 
  } else {
164
 
    *vallen = 0;
165
 
    if (!s.IsNotFound()) {
166
 
      SaveError(errptr, s);
167
 
    }
168
 
  }
169
 
  return result;
170
 
}
171
 
 
172
 
leveldb_iterator_t* leveldb_create_iterator(
173
 
    leveldb_t* db,
174
 
    const leveldb_readoptions_t* options) {
175
 
  leveldb_iterator_t* result = new leveldb_iterator_t;
176
 
  result->rep = db->rep->NewIterator(options->rep);
177
 
  return result;
178
 
}
179
 
 
180
 
const leveldb_snapshot_t* leveldb_create_snapshot(
181
 
    leveldb_t* db) {
182
 
  leveldb_snapshot_t* result = new leveldb_snapshot_t;
183
 
  result->rep = db->rep->GetSnapshot();
184
 
  return result;
185
 
}
186
 
 
187
 
void leveldb_release_snapshot(
188
 
    leveldb_t* db,
189
 
    const leveldb_snapshot_t* snapshot) {
190
 
  db->rep->ReleaseSnapshot(snapshot->rep);
191
 
  delete snapshot;
192
 
}
193
 
 
194
 
char* leveldb_property_value(
195
 
    leveldb_t* db,
196
 
    const char* propname) {
197
 
  std::string tmp;
198
 
  if (db->rep->GetProperty(Slice(propname), &tmp)) {
199
 
    // We use strdup() since we expect human readable output.
200
 
    return strdup(tmp.c_str());
201
 
  } else {
202
 
    return NULL;
203
 
  }
204
 
}
205
 
 
206
 
void leveldb_approximate_sizes(
207
 
    leveldb_t* db,
208
 
    int num_ranges,
209
 
    const char* const* range_start_key, const size_t* range_start_key_len,
210
 
    const char* const* range_limit_key, const size_t* range_limit_key_len,
211
 
    uint64_t* sizes) {
212
 
  Range* ranges = new Range[num_ranges];
213
 
  for (int i = 0; i < num_ranges; i++) {
214
 
    ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
215
 
    ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
216
 
  }
217
 
  db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
218
 
  delete[] ranges;
219
 
}
220
 
 
221
 
void leveldb_destroy_db(
222
 
    const leveldb_options_t* options,
223
 
    const char* name,
224
 
    char** errptr) {
225
 
  SaveError(errptr, DestroyDB(name, options->rep));
226
 
}
227
 
 
228
 
void leveldb_repair_db(
229
 
    const leveldb_options_t* options,
230
 
    const char* name,
231
 
    char** errptr) {
232
 
  SaveError(errptr, RepairDB(name, options->rep));
233
 
}
234
 
 
235
 
void leveldb_iter_destroy(leveldb_iterator_t* iter) {
236
 
  delete iter->rep;
237
 
  delete iter;
238
 
}
239
 
 
240
 
unsigned char leveldb_iter_valid(const leveldb_iterator_t* iter) {
241
 
  return iter->rep->Valid();
242
 
}
243
 
 
244
 
void leveldb_iter_seek_to_first(leveldb_iterator_t* iter) {
245
 
  iter->rep->SeekToFirst();
246
 
}
247
 
 
248
 
void leveldb_iter_seek_to_last(leveldb_iterator_t* iter) {
249
 
  iter->rep->SeekToLast();
250
 
}
251
 
 
252
 
void leveldb_iter_seek(leveldb_iterator_t* iter, const char* k, size_t klen) {
253
 
  iter->rep->Seek(Slice(k, klen));
254
 
}
255
 
 
256
 
void leveldb_iter_next(leveldb_iterator_t* iter) {
257
 
  iter->rep->Next();
258
 
}
259
 
 
260
 
void leveldb_iter_prev(leveldb_iterator_t* iter) {
261
 
  iter->rep->Prev();
262
 
}
263
 
 
264
 
const char* leveldb_iter_key(const leveldb_iterator_t* iter, size_t* klen) {
265
 
  Slice s = iter->rep->key();
266
 
  *klen = s.size();
267
 
  return s.data();
268
 
}
269
 
 
270
 
const char* leveldb_iter_value(const leveldb_iterator_t* iter, size_t* vlen) {
271
 
  Slice s = iter->rep->value();
272
 
  *vlen = s.size();
273
 
  return s.data();
274
 
}
275
 
 
276
 
void leveldb_iter_get_error(const leveldb_iterator_t* iter, char** errptr) {
277
 
  SaveError(errptr, iter->rep->status());
278
 
}
279
 
 
280
 
leveldb_writebatch_t* leveldb_writebatch_create() {
281
 
  return new leveldb_writebatch_t;
282
 
}
283
 
 
284
 
void leveldb_writebatch_destroy(leveldb_writebatch_t* b) {
285
 
  delete b;
286
 
}
287
 
 
288
 
void leveldb_writebatch_clear(leveldb_writebatch_t* b) {
289
 
  b->rep.Clear();
290
 
}
291
 
 
292
 
void leveldb_writebatch_put(
293
 
    leveldb_writebatch_t* b,
294
 
    const char* key, size_t klen,
295
 
    const char* val, size_t vlen) {
296
 
  b->rep.Put(Slice(key, klen), Slice(val, vlen));
297
 
}
298
 
 
299
 
void leveldb_writebatch_delete(
300
 
    leveldb_writebatch_t* b,
301
 
    const char* key, size_t klen) {
302
 
  b->rep.Delete(Slice(key, klen));
303
 
}
304
 
 
305
 
void leveldb_writebatch_iterate(
306
 
    leveldb_writebatch_t* b,
307
 
    void* state,
308
 
    void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
309
 
    void (*deleted)(void*, const char* k, size_t klen)) {
310
 
  class H : public WriteBatch::Handler {
311
 
   public:
312
 
    void* state_;
313
 
    void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
314
 
    void (*deleted_)(void*, const char* k, size_t klen);
315
 
    virtual void Put(const Slice& key, const Slice& value) {
316
 
      (*put_)(state_, key.data(), key.size(), value.data(), value.size());
317
 
    }
318
 
    virtual void Delete(const Slice& key) {
319
 
      (*deleted_)(state_, key.data(), key.size());
320
 
    }
321
 
  };
322
 
  H handler;
323
 
  handler.state_ = state;
324
 
  handler.put_ = put;
325
 
  handler.deleted_ = deleted;
326
 
  b->rep.Iterate(&handler);
327
 
}
328
 
 
329
 
leveldb_options_t* leveldb_options_create() {
330
 
  return new leveldb_options_t;
331
 
}
332
 
 
333
 
void leveldb_options_destroy(leveldb_options_t* options) {
334
 
  delete options;
335
 
}
336
 
 
337
 
void leveldb_options_set_comparator(
338
 
    leveldb_options_t* opt,
339
 
    leveldb_comparator_t* cmp) {
340
 
  opt->rep.comparator = cmp;
341
 
}
342
 
 
343
 
void leveldb_options_set_create_if_missing(
344
 
    leveldb_options_t* opt, unsigned char v) {
345
 
  opt->rep.create_if_missing = v;
346
 
}
347
 
 
348
 
void leveldb_options_set_error_if_exists(
349
 
    leveldb_options_t* opt, unsigned char v) {
350
 
  opt->rep.error_if_exists = v;
351
 
}
352
 
 
353
 
void leveldb_options_set_paranoid_checks(
354
 
    leveldb_options_t* opt, unsigned char v) {
355
 
  opt->rep.paranoid_checks = v;
356
 
}
357
 
 
358
 
void leveldb_options_set_env(leveldb_options_t* opt, leveldb_env_t* env) {
359
 
  opt->rep.env = (env ? env->rep : NULL);
360
 
}
361
 
 
362
 
void leveldb_options_set_info_log(leveldb_options_t* opt, leveldb_logger_t* l) {
363
 
  opt->rep.info_log = (l ? l->rep : NULL);
364
 
}
365
 
 
366
 
void leveldb_options_set_write_buffer_size(leveldb_options_t* opt, size_t s) {
367
 
  opt->rep.write_buffer_size = s;
368
 
}
369
 
 
370
 
void leveldb_options_set_max_open_files(leveldb_options_t* opt, int n) {
371
 
  opt->rep.max_open_files = n;
372
 
}
373
 
 
374
 
void leveldb_options_set_cache(leveldb_options_t* opt, leveldb_cache_t* c) {
375
 
  opt->rep.block_cache = c->rep;
376
 
}
377
 
 
378
 
void leveldb_options_set_block_size(leveldb_options_t* opt, size_t s) {
379
 
  opt->rep.block_size = s;
380
 
}
381
 
 
382
 
void leveldb_options_set_block_restart_interval(leveldb_options_t* opt, int n) {
383
 
  opt->rep.block_restart_interval = n;
384
 
}
385
 
 
386
 
void leveldb_options_set_compression(leveldb_options_t* opt, int t) {
387
 
  opt->rep.compression = static_cast<CompressionType>(t);
388
 
}
389
 
 
390
 
leveldb_comparator_t* leveldb_comparator_create(
391
 
    void* state,
392
 
    void (*destructor)(void*),
393
 
    int (*compare)(
394
 
        void*,
395
 
        const char* a, size_t alen,
396
 
        const char* b, size_t blen),
397
 
    const char* (*name)(void*)) {
398
 
  leveldb_comparator_t* result = new leveldb_comparator_t;
399
 
  result->state_ = state;
400
 
  result->destructor_ = destructor;
401
 
  result->compare_ = compare;
402
 
  result->name_ = name;
403
 
  return result;
404
 
}
405
 
 
406
 
void leveldb_comparator_destroy(leveldb_comparator_t* cmp) {
407
 
  delete cmp;
408
 
}
409
 
 
410
 
leveldb_readoptions_t* leveldb_readoptions_create() {
411
 
  return new leveldb_readoptions_t;
412
 
}
413
 
 
414
 
void leveldb_readoptions_destroy(leveldb_readoptions_t* opt) {
415
 
  delete opt;
416
 
}
417
 
 
418
 
void leveldb_readoptions_set_verify_checksums(
419
 
    leveldb_readoptions_t* opt,
420
 
    unsigned char v) {
421
 
  opt->rep.verify_checksums = v;
422
 
}
423
 
 
424
 
void leveldb_readoptions_set_fill_cache(
425
 
    leveldb_readoptions_t* opt, unsigned char v) {
426
 
  opt->rep.fill_cache = v;
427
 
}
428
 
 
429
 
void leveldb_readoptions_set_snapshot(
430
 
    leveldb_readoptions_t* opt,
431
 
    const leveldb_snapshot_t* snap) {
432
 
  opt->rep.snapshot = (snap ? snap->rep : NULL);
433
 
}
434
 
 
435
 
leveldb_writeoptions_t* leveldb_writeoptions_create() {
436
 
  return new leveldb_writeoptions_t;
437
 
}
438
 
 
439
 
void leveldb_writeoptions_destroy(leveldb_writeoptions_t* opt) {
440
 
  delete opt;
441
 
}
442
 
 
443
 
void leveldb_writeoptions_set_sync(
444
 
    leveldb_writeoptions_t* opt, unsigned char v) {
445
 
  opt->rep.sync = v;
446
 
}
447
 
 
448
 
leveldb_cache_t* leveldb_cache_create_lru(size_t capacity) {
449
 
  leveldb_cache_t* c = new leveldb_cache_t;
450
 
  c->rep = NewLRUCache(capacity);
451
 
  return c;
452
 
}
453
 
 
454
 
void leveldb_cache_destroy(leveldb_cache_t* cache) {
455
 
  delete cache->rep;
456
 
  delete cache;
457
 
}
458
 
 
459
 
leveldb_env_t* leveldb_create_default_env() {
460
 
  leveldb_env_t* result = new leveldb_env_t;
461
 
  result->rep = Env::Default();
462
 
  result->is_default = true;
463
 
  return result;
464
 
}
465
 
 
466
 
void leveldb_env_destroy(leveldb_env_t* env) {
467
 
  if (!env->is_default) delete env->rep;
468
 
  delete env;
469
 
}
470
 
 
471
 
}  // end extern "C"