From 8e9cf7afb015e714eccd85d6a3c22f4d72e8a34c Mon Sep 17 00:00:00 2001 From: Jeff Hodges Date: Sat, 27 Oct 2012 13:50:05 -0700 Subject: [PATCH] add a lightweight Comparator example --- README.md | 3 +++ examples/comparator_example.go | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 examples/comparator_example.go diff --git a/README.md b/README.md index 833572f..d1ec3aa 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,6 @@ Of course, these same rules apply when doing `go build`, as well. Comparators and WriteBatch iterators must be written in C in your own library. This seems like a pain in the ass, but remember that you'll have the LevelDB C API available to your in your client package when you import levigo. + +An example of writing your own Comparator can be found in +. diff --git a/examples/comparator_example.go b/examples/comparator_example.go new file mode 100644 index 0000000..02f8a7a --- /dev/null +++ b/examples/comparator_example.go @@ -0,0 +1,46 @@ +package main + +/* +#cgo LDFLAGS: -lleveldb +#include +#include + +static void CmpDestroy(void* arg) { } + +static int CmpCompare(void* arg, const char* a, size_t alen, + const char* b, size_t blen) { + int n = (alen < blen) ? alen : blen; + int r = memcmp(a, b, n); + if (r == 0) { + if (alen < blen) r = -1; + else if (alen > blen) r = +1; + } + return r; +} + +static const char* CmpName(void* arg) { + return "foo"; +} + +static leveldb_comparator_t* CmpFooNew() { + return leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName); +} + +*/ +import "C" + +type Comparator struct { + Comparator *C.leveldb_comparator_t +} + +func NewFooComparator() *Comparator { + return &Comparator{C.CmpFooNew()} +} + +func (cmp *Comparator) Close() { + C.leveldb_comparator_destroy(cmp.Comparator) +} + +func main() { + NewFooComparator().Close() +}