-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinetuple.go
169 lines (155 loc) · 3.03 KB
/
linetuple.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ifchanged
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path"
)
// An implementation of sha256 in one file
// with line #0 being the key and line #1 the value, etc.
// 0 key
// 1 value
// 2 key
// 3 value
type LineTuple struct {
fileName string
file *os.File
}
func NewLineTuple(fileName string) (*LineTuple, error) {
var l LineTuple
l.fileName = fileName
var err error
l.file, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, os.ModePerm)
if os.IsNotExist(err) {
err = os.MkdirAll(path.Dir(fileName), os.ModePerm)
if err == nil {
file, err := os.Create(fileName)
if err != nil {
return nil, fmt.Errorf("db create file error: %w", err)
}
l.file = file
//defer file.Close()
} else {
return nil, err
}
}
return &l, nil
}
func (l *LineTuple) Put(key, value []byte) error {
l.file.Seek(0, 0)
lineIndex := 0
foundKeyLine := -1
foundValueLine := -1
newLinesCount := 0
for {
i := 0
line := make([]byte, 0, 64)
var err error
by := make([]byte, 1)
repeat:
by[0] = 0
cnt, err := l.file.Read(by)
if err == io.EOF {
if len(line) > 0 {
goto try
}
break
}
if cnt > 0 {
if by[0] == '\n' {
newLinesCount++
i++
} else {
if by[0] != '\r' { // In case it's not a new line feed, we append
line = append(line, by[0])
}
i++
goto repeat
}
}
try:
if lineIndex == foundValueLine {
if bytes.Equal(value, line) {
// Value equals, we simply break
return nil
} else {
// We take advantage of the fact that values are always the same size
if len(value) != len(line) {
return fmt.Errorf("value is of illegal size: %d", len(value))
} else {
l.file.Seek(-int64(i), 1)
l.file.Write(value)
l.file.Sync()
return nil
}
}
}
if lineIndex%2 == 0 && bytes.Equal(key, line) {
foundKeyLine = lineIndex
foundValueLine = lineIndex + 1
}
lineIndex++
}
if foundKeyLine < 0 {
if newLinesCount%2 != 0 {
// Keeping key beginning at odd lines
l.file.Write([]byte{'\n'})
}
l.file.Write(key)
l.file.Write([]byte{'\n'})
l.file.Write(value)
l.file.Sync()
}
return nil
}
func (l *LineTuple) Has(key []byte) bool {
l.file.Seek(0, 0)
scanner := bufio.NewScanner(l.file)
line := 0
foundLine := -1
for scanner.Scan() {
if foundLine == line {
// The value can be empty, but we don't consider it as a missing value
return true
}
if line%2 == 0 {
if bytes.Equal(scanner.Bytes(), key) {
foundLine = line + 1
}
}
line++
}
return false
}
func (l *LineTuple) Get(key []byte) ([]byte, error) {
l.file.Seek(0, 0)
scanner := bufio.NewScanner(l.file)
line := 0
foundLine := -1
for scanner.Scan() {
if foundLine == line {
return []byte(scanner.Text()), nil
}
if line%2 == 0 {
if bytes.Equal(scanner.Bytes(), key) {
foundLine = line + 1
}
}
line++
}
return []byte{}, nil
}
func (l *LineTuple) Sync() error {
if l.file == nil {
return nil
}
return l.file.Sync()
}
func (l *LineTuple) Close() error {
if l.file == nil {
return nil
}
return l.file.Close()
}