-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
65 lines (51 loc) · 1.22 KB
/
utils.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
package engine
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
filestore "github.com/lokidb/engine/file_storage"
)
func createFileStores(rootPath string, filesCount int) map[string]*filestore.FileKeyValueStore {
fileStores := make(map[string]*filestore.FileKeyValueStore, filesCount)
for i := 0; i < filesCount; i++ {
filename := filePrefix + strconv.Itoa(i) + fileExtension
filePath := filepath.Join(rootPath, filename)
fileStore := filestore.New(filePath)
fileStores[filename] = fileStore
}
return fileStores
}
func (s *storage) appendToLog(command string, key string, value []byte) {
if !toggleAOL {
return
}
s.aolLock.Lock()
defer s.aolLock.Unlock()
file, err := os.OpenFile(s.aolPath, os.O_WRONLY, os.ModeAppend)
if err != nil {
file, err = os.Create(s.aolPath)
if err != nil {
panic(err)
}
}
endOffset, _ := file.Seek(0, io.SeekEnd)
file.WriteAt([]byte(fmt.Sprintf("%s -:- %s -:- %v\n", command, key, value)), endOffset)
}
func equals(a []byte, b []byte) bool {
if a == nil && b == nil {
return true
} else if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}