-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_log.go
148 lines (131 loc) · 2.69 KB
/
json_log.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package storage
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
)
// TODO set json flag to output single line json
type JsonLog struct {
}
// 写失败将破坏文件数据
func (l *JsonLog) WriteHeader(f File, header *FileHeader) error {
const available = HeaderSize - 1
_, err := f.Seek(0, io.SeekStart)
if err != nil {
return err
}
jDoc, err := json.Marshal(header)
if err != nil {
return err
}
if len(jDoc) > available {
return fmt.Errorf("header too large")
}
padding := available - len(jDoc)
if padding > 0 {
spaces := bytes.Repeat([]byte{' '}, padding)
jDoc = append(jDoc, spaces...)
}
// now its length equal to HeaderSize
jDoc = append(jDoc, '\n')
n, err := f.Write(jDoc)
if err != nil {
return err
}
if n != len(jDoc) {
return fmt.Errorf("write size unexpected")
}
return nil
}
// TODO: 更多校验
func (l *JsonLog) IsValidFile(f File) (bool, error) {
fSize, err := f.Seek(0, io.SeekEnd)
if err != nil {
return false, err
}
if fSize < HeaderSize {
return false, nil
}
return true, nil
}
func (l *JsonLog) ReadHeader(f File) (*FileHeader, error) {
const available = HeaderSize - 1
_, err := f.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
buffer := [available]byte{}
n, err := f.Read(buffer[:])
if err != nil {
return nil, err
}
if n != len(buffer) {
return nil, fmt.Errorf("read size unexpected")
}
header := FileHeader{}
err = json.Unmarshal(buffer[:], &header)
if err != nil {
return nil, err
}
return &header, nil
}
// 写失败将破坏文件数据
func (l *JsonLog) AppendEntry(f File, pos int64, entry *LogEntry) (int64, error) {
jDoc, err := json.Marshal(entry)
if err != nil {
return 0, err
}
jDoc = append(jDoc, '\n')
if pos != -1 {
if pos < HeaderSize {
return 0, fmt.Errorf("invalid pos")
}
_, err = f.Seek(pos, io.SeekStart)
if err != nil {
return 0, err
}
} else {
sz, err := f.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
if sz < HeaderSize {
return 0, fmt.Errorf("invalid file")
}
}
n, err := f.Write(jDoc)
if err != nil {
return 0, err
}
if n != len(jDoc) {
return 0, fmt.Errorf("write size unexpected")
}
return int64(n), nil
}
func (l *JsonLog) ReadEntry(f File, pos int64, entry *LogEntry) (int64, error) {
_, err := f.Seek(pos, io.SeekStart)
if err != nil {
return 0, err
}
reader := bufio.NewReader(f)
jDoc, err := reader.ReadBytes('\n')
if err != nil {
return 0, err
}
readSz := len(jDoc)
// won't be 0
if len(jDoc) <= 1 {
return 0, fmt.Errorf("read size unexpected")
}
jDoc = jDoc[:len(jDoc)-1]
err = json.Unmarshal(jDoc, entry)
if err != nil {
return 0, err
}
return int64(readSz), nil
}
func _() {
var _ LogFormat = &JsonLog{}
}