-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsstables.go
109 lines (91 loc) · 2.42 KB
/
sstables.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"errors"
"github.com/thomasjungblut/go-sstables/skiplist"
"github.com/thomasjungblut/go-sstables/sstables"
"log"
"os"
)
func main() {
path := "/tmp/sstable_example/"
os.MkdirAll(path, 0777)
defer os.RemoveAll(path)
mainWriteSimple(path)
mainSimpleRead(path)
}
func mainSimpleRead(path string) {
reader, err := sstables.NewSSTableReader(
sstables.ReadBasePath("/tmp/sstable_example/"),
sstables.ReadWithKeyComparator(skiplist.BytesComparator{}))
if err != nil {
log.Fatalf("error: %v", err)
}
defer reader.Close()
metadata := reader.MetaData()
log.Printf("reading table with %d records, minKey %d and maxKey %d", metadata.NumRecords, metadata.MinKey, metadata.MaxKey)
contains := reader.Contains([]byte{1})
val, err := reader.Get([]byte{1})
if err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("table contains value for key? %t = %d", contains, val)
it, err := reader.ScanRange([]byte{1}, []byte{2})
for {
k, v, err := it.Next()
// io.EOF signals that no records are left to be read
if errors.Is(err, sstables.Done) {
break
}
if err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("%d = %d", k, v)
}
}
func mainWriteSimple(path string) {
writer, err := sstables.NewSSTableSimpleWriter(
sstables.WriteBasePath(path),
sstables.WithKeyComparator(skiplist.BytesComparator{}))
if err != nil {
log.Fatalf("error: %v", err)
}
skipListMap := skiplist.NewSkipListMap[[]byte, []byte](skiplist.BytesComparator{})
skipListMap.Insert([]byte{1}, []byte{1})
skipListMap.Insert([]byte{2}, []byte{2})
skipListMap.Insert([]byte{3}, []byte{3})
err = writer.WriteSkipListMap(skipListMap)
if err != nil {
log.Fatalf("error: %v", err)
}
}
func mainWriteStreaming() {
path := "/tmp/sstable_example/"
os.MkdirAll(path, 0777)
defer os.RemoveAll(path)
writer, err := sstables.NewSSTableStreamWriter(
sstables.WriteBasePath(path),
sstables.WithKeyComparator(skiplist.BytesComparator{}))
if err != nil {
log.Fatalf("error: %v", err)
}
err = writer.Open()
if err != nil {
log.Fatalf("error: %v", err)
}
err = writer.WriteNext([]byte{1}, []byte{1})
if err != nil {
log.Fatalf("error: %v", err)
}
err = writer.WriteNext([]byte{2}, []byte{2})
if err != nil {
log.Fatalf("error: %v", err)
}
err = writer.WriteNext([]byte{3}, []byte{3})
if err != nil {
log.Fatalf("error: %v", err)
}
err = writer.Close()
if err != nil {
log.Fatalf("error: %v", err)
}
}