File: main.go

package info (click to toggle)
golang-github-pzhin-go-sophia 0.0~git20191015.afcd224-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: ansic: 23,001; makefile: 4
file content (90 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"unsafe"

	"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 {

	if src == nil {
		return 0
	}
	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)
	}

	tmpDir, err := ioutil.TempDir("", "sophia_test")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	env.Set(sophia.EnvironmentPath, tmpDir)

	schema := &sophia.Schema{}
	schema.AddKey("key", sophia.FieldTypeString)
	schema.AddValue("value", sophia.FieldTypeUInt32)
	schema.AddValue("value2", sophia.FieldTypeUInt32)

	odb, err := env.NewDatabase(sophia.DatabaseConfig{
		Name:                "test",
		Compression:         sophia.CompressionTypeLZ4,
		Schema:              schema,
		Upsert:              upsertCallback,
		CompactionCacheSize: 1 * 1024 * 1024 * 1024,
	})
	if err != nil {
		log.Fatal(err)
	}

	err = env.Open()
	if err != nil {
		log.Fatal(err)
	}

	var value int64
	for i := 0; i < 1e6; i++ {
		doc := odb.Document()
		doc.SetString("key", fmt.Sprintf("val%d", value))
		doc.SetInt("value", value)
		doc.SetInt("value2", value)
		err := odb.Upsert(doc)
		if err != nil {
			log.Fatal(err)
		}
		doc.Free()
		value++
	}

	dc := odb.Document()
	cursor, err := odb.Cursor(dc)
	if err != nil {
		log.Fatal(err)
	}

	for d := cursor.Next(); !d.IsEmpty(); d = cursor.Next() {
		var size int
		fmt.Println(d.GetString("key", &size), ":", d.GetInt("value"), ":", d.GetInt("value2"), ":", size)
	}
}