forked from pranjalv123/mexos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levigo-ex.go
60 lines (54 loc) · 1.47 KB
/
levigo-ex.go
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
package main
import "github.com/jmhodges/levigo"
import "fmt"
import "math/rand"
import "time"
func trace(s string) (string, time.Time) {
fmt.Println("START:", s)
return s, time.Now()
}
func un(s string, startTime time.Time) {
endTime := time.Now()
fmt.Println(" END:", s, "ElapsedTime in seconds:", endTime.Sub(startTime))
}
func db_create() (*levigo.DB) {
defer un(trace("db create"))
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(3<<30))
opts.SetCreateIfMissing(true)
db, _ := levigo.Open("/tmp/leveldb", opts)
ro := levigo.NewReadOptions()
wo := levigo.NewWriteOptions()
db.Put(wo, []byte("key1"), []byte("val1"))
data, _ := db.Get(ro, []byte("key1"))
fmt.Println(string(data));
return db
}
func gen_data(size uint64) ([]byte) {
defer un(trace("gen data"))
data := make([]byte, size)
// src := new(rand.Rand)
for i := range data {
data[i] = byte(rand.Int31() & 0xff)
}
return data
}
func write_data(data []byte, db *levigo.DB, nitems int, keysize int, valsize int) {
defer un(trace("write data"))
wo := levigo.NewWriteOptions()
keystart := 0
valstart := 0
for i:=0; i < nitems; i++ {
db.Put(wo,
data[keystart:keystart + keysize],
data[valstart:valstart + valsize])
keystart %= (len(data) - keysize)
valstart %= (len(data) - valsize)
}
fmt.Println("Wrote ", nitems, "items with key size", keysize, "and value size", valsize)
}
func main() {
db := db_create()
data := gen_data(1 << 24)
write_data(data, db, 1000000, 4, 4)
}