Skip to content

Commit

Permalink
add a lightweight Comparator example
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhodges committed Oct 27, 2012
1 parent 0b83158 commit 8e9cf7a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://github.com/jmhodges/levigo/blob/master/examples>.
46 changes: 46 additions & 0 deletions examples/comparator_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

/*
#cgo LDFLAGS: -lleveldb
#include <string.h>
#include <leveldb/c.h>
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()
}

0 comments on commit 8e9cf7a

Please sign in to comment.