Skip to content

Instantly share code, notes, and snippets.

@satta
Created July 13, 2018 13:44
Show Gist options
  • Save satta/ca008fd6e2bf84e80953c4427a56d8ca to your computer and use it in GitHub Desktop.
Save satta/ca008fd6e2bf84e80953c4427a56d8ca to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"unsafe"
sophia "github.com/pzhin/go-sophia"
)
func upsertCallback(count int, src []unsafe.Pointer, srcSize uint32,
upsert []unsafe.Pointer, upsertSize uint32, result []unsafe.Pointer,
resultSize uint32, arg unsafe.Pointer) int {
ca := *(*uint32)(src[1])
cb := *(*uint32)(upsert[1])
cret := ca + cb
cresPtr := (*uint32)(result[1])
*cresPtr = cret
tb := *(*uint32)(upsert[2])
tresPtr := (*uint32)(result[2])
*tresPtr = tb
return 0
}
func main() {
env, err := sophia.NewEnvironment()
if err != nil {
log.Fatal(err)
}
env.Set("sophia.path", "/tmp/sophiadb_example")
obsschema := &sophia.Schema{}
obsschema.AddKey("key", sophia.FieldTypeString)
obsschema.AddValue("value", sophia.FieldTypeUInt32)
obsschema.AddValue("value2", sophia.FieldTypeUInt32)
odb, err := env.NewDatabase(sophia.DatabaseConfig{
Name: "test",
Compression: sophia.CompressionTypeLZ4,
Schema: obsschema,
Upsert: upsertCallback,
CompactionCacheSize: 1 * 1024 * 1024 * 1024,
})
if err != nil {
log.Fatal(err)
}
env.Open()
val := int64(0)
for {
d2 := odb.Document()
d2.SetString("key", fmt.Sprintf("val%d", val))
d2.SetInt("value", val)
d2.SetInt("value2", val)
err := odb.Upsert(d2)
if err != nil {
log.Fatal(err)
}
d2.Free()
val++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment