/
storage.go
115 lines (90 loc) · 1.67 KB
/
storage.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
package storage
import (
"errors"
"net/url"
"boscoin.io/sebak/lib/common"
)
var SupportedStorageType []string = []string{
"memory",
"file",
}
type IterItem struct {
N uint64
Key []byte
Value []byte
}
func (i IterItem) Clone() IterItem {
k := make([]byte, len(i.Key))
copy(k, i.Key)
v := make([]byte, len(i.Value))
copy(v, i.Value)
return IterItem{
N: i.N,
Key: k,
Value: v,
}
}
type Item struct {
Key string
Value interface{}
}
type Model struct {
}
func NewStorage(config *Config) (st *LevelDBBackend, err error) {
st = &LevelDBBackend{}
if err = st.Init(config); err != nil {
return
}
return
}
type Config url.URL
func NewConfigFromURL(u *url.URL) *Config {
return (*Config)(u)
}
func NewConfigFromString(s string) (e *Config, err error) {
var u *url.URL
if u, err = url.Parse(s); err != nil {
return
}
if _, found := common.InStringArray(SupportedStorageType, u.Scheme); !found {
err = errors.New("unsupported storage type")
return
}
e = NewConfigFromURL(u)
return
}
func (e *Config) String() string {
return (&url.URL{
Scheme: e.Scheme,
Host: e.Host,
Path: e.Path,
}).String()
}
func (e *Config) Query() url.Values {
return (*url.URL)(e).Query()
}
func (e *Config) UnmarshalJSON(b []byte) error {
p, err := ParseConfig(string(b)[1 : len(string(b))-1])
if err != nil {
return err
}
*e = *p
return nil
}
func ParseConfig(s string) (u *Config, err error) {
var parsed *url.URL
parsed, err = url.Parse(s)
if err != nil {
return
}
if len(parsed.Scheme) < 1 {
err = errors.New("missing scheme")
return
}
u = (*Config)(parsed)
return
}
type Committable interface {
Discard()
Commit() error
}