-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_store.go
117 lines (108 loc) · 2.22 KB
/
db_store.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
110
111
112
113
114
115
116
117
package lsm3
import (
"encoding/binary"
"fmt"
"io"
"os"
"path"
)
const (
metaFile = "db.meta"
)
func (c *Contains) SaveToDB() error {
length, data, err := c.Bytes()
if err != nil {
return err
}
fp, err := os.OpenFile(path.Join(c.dirPath, metaFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fp.Close()
if err := binary.Write(fp, binary.BigEndian, uint32(length)); err != nil {
return err
}
if _, err := fp.Write(data); err != nil {
return err
}
// 写入TableInfo
for _, table := range c.table {
length, data, err := table.Bytes()
if err != nil {
return err
}
if err := binary.Write(fp, binary.BigEndian, uint32(length)); err != nil {
return err
}
if _, err := fp.Write(data); err != nil {
return err
}
}
if err := fp.Sync(); err != nil {
return err
}
return nil
}
func LoadFromDB(dirPath string) (*Contains, error) {
metaFile := path.Join(dirPath, metaFile)
if _, err := os.Stat(metaFile); os.IsNotExist(err) {
return &Contains{
table: make(map[string]*TableInfo),
ts: make(map[string]uint32),
}, nil
}
fp, err := os.OpenFile(metaFile, os.O_RDONLY, 0644)
if err != nil {
return nil, err
}
defer fp.Close()
// 进行数据读取
var length uint32
err = binary.Read(fp, binary.BigEndian, &length)
if err != nil {
return nil, err
}
data := make([]byte, length)
count, err := io.ReadFull(fp, data)
if err != nil {
return nil, err
}
if count != int(length) {
return nil, fmt.Errorf("read length error")
}
contain, err := RestoreContains(data)
if err != nil {
return nil, err
}
contain.table = make(map[string]*TableInfo)
for {
err := binary.Read(fp, binary.BigEndian, &length)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if cap(data) < int(length) {
data = make([]byte, length)
} else {
data = data[:length]
}
count, err := io.ReadFull(fp, data)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if count != int(length) {
return nil, fmt.Errorf("read length error")
}
tableInfo := &TableInfo{}
if err := tableInfo.FromBytes(data); err != nil {
return nil, err
}
contain.table[tableInfo.TableName] = tableInfo
}
return contain, nil
}