Last active
January 1, 2016 02:39
-
-
Save tsileo/8080913 to your computer and use it in GitHub Desktop.
Levigo iter range start/end, updated from https://github.com/janelia-flyem/dvid/blob/master/storage/levigo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"fmt" | |
"bytes" | |
// "strconv" | |
"github.com/jmhodges/levigo" | |
) | |
type KeyValue struct { | |
Key string | |
Value string | |
} | |
func GetRange(db *levigo.DB, kStart []byte, kEnd []byte) (values []KeyValue, err error) { | |
values = []KeyValue{} | |
ro := levigo.NewReadOptions() | |
defer ro.Close() | |
it := db.NewIterator(ro) | |
defer func() { | |
it.Close() | |
}() | |
it.Seek(kStart) | |
endBytes := kEnd | |
for { | |
if it.Valid() { | |
if bytes.Compare(it.Key(), endBytes) > 0 { | |
return | |
} | |
value := it.Value() | |
vstr := string(value[:]) | |
key := it.Key() | |
kstr := string(key[:]) | |
values = append(values, KeyValue{kstr, vstr}) | |
it.Next() | |
} else { | |
err = it.GetError() | |
return | |
} | |
} | |
return | |
} | |
func main() { | |
log.Println("Starting") | |
// Setup LevelDB | |
opts := levigo.NewOptions() | |
opts.SetCreateIfMissing(true) | |
filter := levigo.NewBloomFilter(10) | |
opts.SetFilterPolicy(filter) | |
db, err := levigo.Open("/work/opensource/blockchain_playground_golang/tmpdat2", opts) | |
defer db.Close() | |
if err != nil { | |
fmt.Printf("failed to load db: %s\n", err) | |
} | |
wo := levigo.NewWriteOptions() | |
//wo.SetSync(true) | |
defer wo.Close() | |
ro := levigo.NewReadOptions() | |
defer ro.Close() | |
wb := levigo.NewWriteBatch() | |
defer wb.Close() | |
kStart := []byte("last") | |
kEnd := []byte("last\xff") | |
values, err := GetRange(db, kStart, kEnd) | |
if err != nil { | |
fmt.Printf("GetRange error: %s\n", err) | |
} | |
for _, kv := range values { | |
log.Println(kv) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment