Sergey Rogulenko | b0081cf | 2015-05-05 22:39:37 -0700 | [diff] [blame^] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | // |
| 5 | // This file contains helpers to minimize the number of cgo calls, which have |
| 6 | // some overhead. |
| 7 | // Some conventions: |
| 8 | // |
| 9 | // Errors are represented by a null-terminated C string. NULL means no error. |
| 10 | // All operations that can raise an error are passed a "char** errptr" as the |
| 11 | // last argument. *errptr should be NULL. |
| 12 | // On failure, leveldb sets *errptr to a malloc()ed error message. |
| 13 | // |
| 14 | // All of the pointer arguments must be non-NULL. |
| 15 | |
| 16 | #ifndef V_IO_SYNCBASE_X_REF_SERVICES_SYNCBASE_STORE_LEVELDB_SYNCBASE_LEVELDB_H_ |
| 17 | #define V_IO_SYNCBASE_X_REF_SERVICES_SYNCBASE_STORE_LEVELDB_SYNCBASE_LEVELDB_H_ |
| 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
| 23 | #include "leveldb/c.h" |
| 24 | |
| 25 | // Fields of this struct are accessed from go directly without cgo calls. |
| 26 | struct syncbase_leveldb_iterator_t { |
| 27 | leveldb_iterator_t* rep; |
| 28 | unsigned char is_valid; |
| 29 | char const* key; |
| 30 | size_t key_len; |
| 31 | char const* val; |
| 32 | size_t val_len; |
| 33 | }; |
| 34 | |
| 35 | typedef struct syncbase_leveldb_iterator_t syncbase_leveldb_iterator_t; |
| 36 | |
| 37 | // Returns iterator that points to first key that is not less than |start|. |
| 38 | // The returned iterator must be passed to |syncbase_leveldb_iter_destroy| |
| 39 | // when finished. |
| 40 | syncbase_leveldb_iterator_t* syncbase_leveldb_create_iterator( |
| 41 | leveldb_t* db, |
| 42 | const leveldb_readoptions_t* options, |
| 43 | const char* start, size_t start_len); |
| 44 | |
| 45 | // Deallocates iterator returned by |syncbase_leveldb_create_iterator|. |
| 46 | void syncbase_leveldb_iter_destroy(syncbase_leveldb_iterator_t*); |
| 47 | |
| 48 | // Moves to the next entry in the source. After this call, |is_valid| is |
| 49 | // true iff the iterator was not positioned at the last entry in the source. |
| 50 | // REQUIRES: |is_valid| is true. |
| 51 | void syncbase_leveldb_iter_next(syncbase_leveldb_iterator_t* iter); |
| 52 | |
| 53 | // Returns a non-nil error iff the iterator encountered any errors. |
| 54 | void syncbase_leveldb_iter_get_error( |
| 55 | const syncbase_leveldb_iterator_t* iter, char** errptr); |
| 56 | |
| 57 | #ifdef __cplusplus |
| 58 | } // end extern "C" |
| 59 | #endif |
| 60 | |
| 61 | #endif // V_IO_SYNCBASE_X_REF_SERVICES_SYNCBASE_STORE_LEVELDB_SYNCBASE_LEVELDB_H_ |