forked from vasedb/vasedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
109 lines (91 loc) · 2.63 KB
/
option.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
// Open Source: MIT License
// Author: Leon Ding <ding@ibyte.me>
// Date: 2022/2/26 - 10:47 下午 - UTC/GMT+08:00
package bottle
import (
"fmt"
"os"
"strings"
)
// Option bottle setting option
type Option struct {
Directory string `yaml:"Directory"` // data directory
DataFileMaxSize int64 `yaml:"DataFileMaxSize"` // data file max size
Enable bool `yaml:"Enable"` // data whether to enable encryption
Secret string `yaml:"Secret"` // data encryption key
}
var (
// DefaultOption default initialization option
DefaultOption = Option{
Directory: "./data",
DataFileMaxSize: 10240,
}
)
// Validation verifying configuration Items
func (o *Option) Validation() {
if o.Directory == "" {
panic("The data file directory cannot be empty")
}
// The first one does not determine whether there is a backslash
o.Directory = pathBackslashes(o.Directory)
// Record the location of the data file
o.Directory = strings.TrimSpace(o.Directory)
Root = o.Directory
if o.DataFileMaxSize != 0 {
defaultMaxFileSize = o.DataFileMaxSize
}
if o.Enable {
if len(o.Secret) < 16 && len(o.Secret) > 16 {
panic("The encryption key contains less than 16 characters")
}
Secret = []byte(o.Secret)
encoder = AES()
}
dataDirectory = fmt.Sprintf("%sdata/", Root)
indexDirectory = fmt.Sprintf("%sindex/", Root)
if o.DataFileMaxSize != 0 {
defaultMaxFileSize = o.DataFileMaxSize
}
}
// SetEncryptor Set up a custom encryption implementation
func SetEncryptor(encryptor Encryptor, secret []byte) {
if len(secret) == 0 {
panic("The key used by the encryptor cannot be empty!!!")
}
encoder.enable = true
encoder.Encryptor = encryptor
Secret = secret
}
// SetIndexSize set the expected index size to prevent secondary
// memory allocation and data migration during running
func SetIndexSize(size int32) {
if size == 0 {
return
}
index = make(map[uint64]*record, size)
}
// SetHashFunc sets the specified hash function
func SetHashFunc(hash Hashed) {
HashedFunc = hash
}
// pathBackslashes Check directory ending backslashes
func pathBackslashes(path string) string {
if !strings.HasSuffix(path, "/") {
return fmt.Sprintf("%s/", path)
}
return path
}
// Checks whether the target path exists
// 如果返回的错误为nil,说明文件或文件夹存在
// 如果返回的错误类型使用os.IsNotExist()判断为true,说明文件或文件夹不存在
// 如果返回的错误为其它类型,则不确定是否在存在
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}