Skip to content

Instantly share code, notes, and snippets.

@prateek
Last active May 14, 2018 18:31
Show Gist options
  • Save prateek/fb2cb1a197d4dcfeaf39640a40af3787 to your computer and use it in GitHub Desktop.
Save prateek/fb2cb1a197d4dcfeaf39640a40af3787 to your computer and use it in GitHub Desktop.
vellum example usage
package main
import (
"bytes"
"github.com/couchbaselabs/vellum"
vregex "github.com/couchbaselabs/vellum/regexp"
)
func main() {
var fstBuffer bytes.Buffer
builder, err := vellum.New(&fstBuffer, nil)
if err != nil {
panic(err)
}
// 5 is len(field) ...
insert := []byte("5field")
insert = append(insert, []byte("value")...)
if err := builder.Insert(insert, 1); err != nil {
panic(err)
}
if err := builder.Close(); err != nil {
panic(err)
}
fst, err := vellum.Load(fstBuffer.Bytes())
if err != nil {
panic(err)
}
regex := []byte("5field")
regex = append(regex, []byte("val.*")...)
re, err := vregex.New(string(regex))
if err != nil {
panic(err)
}
minKey := append([]byte("5field"))
maxKey := append([]byte("5fiele"))
iter, err := fst.Search(re, minKey, maxKey)
if err != nil {
panic(err)
}
for {
_, offset := iter.Current()
println("offset", offset)
err = iter.Next()
if err == vellum.ErrIteratorDone {
break
}
if err != nil {
panic(err)
}
}
if err := iter.Close(); err != nil {
panic(err)
}
println("all done!")
}
func not_working_main() {
var fstBuffer bytes.Buffer
builder, err := vellum.New(&fstBuffer, nil)
if err != nil {
panic(err)
}
insert := []byte("field")
insert = append(insert, byte(0xff))
insert = append(insert, []byte("value")...)
if err := builder.Insert(insert, 1); err != nil {
panic(err)
}
if err := builder.Close(); err != nil {
panic(err)
}
fst, err := vellum.Load(fstBuffer.Bytes())
if err != nil {
panic(err)
}
regex := []byte("field")
regex = append(regex, byte(0xff))
regex = append(regex, []byte("val.*")...)
re, err := vregex.New(string(regex))
if err != nil {
panic(err)
}
minKey := append([]byte("field"), byte(0xff), byte(0))
maxKey := append([]byte("field"), byte(0xff), byte(0xff))
iter, err := fst.Search(re, minKey, maxKey)
if err != nil {
panic(err)
}
for {
_, offset := iter.Current()
println("offset", offset)
err = iter.Next()
if err == vellum.ErrIteratorDone {
break
}
if err != nil {
panic(err)
}
}
if err := iter.Close(); err != nil {
panic(err)
}
println("all done!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment