1 module rocksdb.comparator; 2 3 extern (C) { 4 struct rocksdb_comparator_t {}; 5 6 rocksdb_comparator_t* rocksdb_comparator_create( 7 void* state, 8 void function(void*), 9 int function(void*, const char* a, size_t alen, const char* b, size_t blen), 10 immutable(char*) function(void*)); 11 12 void rocksdb_comparator_destroy(rocksdb_comparator_t*); 13 14 alias CmpDestroyF = void function(void*); 15 alias CmpCompareF = static int function(void* arg, const char* a, size_t alen, const char* b, size_t blen); 16 alias CmpNameF = static immutable(char*) function(void *arg); 17 } 18 19 20 class Comparator { 21 rocksdb_comparator_t* cmp; 22 23 this(CmpDestroyF des, CmpCompareF cmp, CmpNameF name) { 24 this.cmp = rocksdb_comparator_create(null, des, cmp, name); 25 } 26 27 ~this() { 28 rocksdb_comparator_destroy(this.cmp); 29 } 30 } 31 32 unittest { 33 import std.algorithm.comparison : max; 34 import core.stdc.string : cmemcmp = memcmp; 35 import std.string : toStringz; 36 37 extern (C) void destroyComparator(void *arg) { 38 39 } 40 41 extern (C) int compareComparator(void *arg, const char* a, size_t aSize, const char* b, size_t bSize) { 42 size_t n = max(aSize, bSize); 43 int r = cmemcmp(a, b, n); 44 if (r == 0) { 45 r = (aSize < bSize) ? -1 : 1; 46 } 47 return r; 48 } 49 50 extern (C) static immutable(char)* nameComparator(void *arg) { 51 return toStringz("test"); 52 } 53 54 auto cmp = new Comparator(&destroyComparator, &compareComparator, &nameComparator); 55 destroy(cmp); 56 }