This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
192 lines (162 loc) · 3.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"reflect"
"errors"
"strconv"
)
type Storage struct {
Segments map[rune]*Segment
IdCounter int64
}
func (db *Storage) Init() {
db.IdCounter = 0
db.Segments = make(map[rune]*Segment)
}
func (db *Storage) Size() (size int) {
size = 0
for _, s := range db.Segments {
size += s.Size
}
return size
}
func (db *Storage) Write(id string, data interface{}) error {
if id == "" {
return errors.New("Passed ID is invalid!")
}
if data == nil {
return errors.New("Passed data is nil!")
}
var segID rune
var seg *Segment
var sub *Segment
ok := false
for i := 0; i < len(id); i++ {
segID = rune(id[i])
if seg == nil {
if seg, ok = db.Segments[segID]; !ok {
seg = &Segment{nil, 0, id[:i + 1], make([]*Record, 0), make(map[rune]*Segment)}
db.Segments[segID] = seg
}
} else {
if sub, ok = seg.Sub[segID]; !ok {
sub = &Segment{seg, 0, id[:i + 1], make([]*Record, 0), make(map[rune]*Segment)}
seg.Sub[segID] = sub
seg = sub
} else {
seg = sub
}
}
}
if seg == nil {
return errors.New("Segments were not evaluated properly for ID " + id)
}
db.IdCounter++
seg.Records = append(seg.Records, &Record{db.IdCounter, id, data})
// recursive incremental
seg.Size++
for seg.Root != nil {
seg.Root.Size++
seg = seg.Root
}
return nil
}
func (db *Storage) FindSegment(id string) (*Segment, error) {
if id == "" {
return nil, errors.New("Passed ID is invalid!")
}
var segmentID rune
var seg *Segment
ok := false
for i := 0; i < len(id); i++ {
segmentID = rune(id[i])
if seg == nil {
if seg, ok = db.Segments[segmentID]; !ok {
return nil, errors.New("Segment not found!")
}
} else {
if seg, ok = seg.Sub[segmentID]; !ok {
return nil, errors.New("SubSegment not found!")
}
}
}
return seg, nil
}
// primaryFields must be type of string or int
func GetIdentifier(x interface{}, primaryFields []string) string {
v := reflect.Indirect(reflect.ValueOf(x))
t := v.Type()
l := v.NumField()
kinds := make([]reflect.Kind, l)
values := make([]interface{}, l)
keys := make([]reflect.StructField, l)
identifier := ""
for i := 0; i < l; i++ {
kinds[i] = v.Field(i).Kind()
values[i] = v.Field(i).Interface()
keys[i] = t.Field(i)
}
temp := ""
if primaryFields != nil {
for _, field := range primaryFields {
for i := 0; i < l; i++ {
if field == keys[i].Name {
if kinds[i] == reflect.String {
temp = values[i].(string)
if len(temp) > 0 {
identifier += string(temp[0])
break
}
} else if kinds[i] == reflect.Int {
identifier += string(strconv.Itoa(values[i].(int))[0])
}
}
}
}
} else {
for i := 0; i < l; i++ {
if kinds[i] == reflect.String {
temp = values[i].(string)
if len(temp) > 0 {
identifier = string(temp[0])
break
}
} else if kinds[i] == reflect.Int {
identifier += string(strconv.Itoa(values[i].(int))[0])
}
}
}
return identifier
}
// primaryFields must be type of string or int
func GetIdentifierFromMap(m map[string]interface{}, primaryFields []string) string {
identifier := ""
temp := ""
var k reflect.Kind
if primaryFields != nil {
for _, field := range primaryFields {
v, ok := m[field]
if ok {
k = reflect.TypeOf(v).Kind()
if k == reflect.String {
temp = v.(string)
if len(temp) > 0 {
identifier += string(temp[0])
}
} else if k == reflect.Int {
identifier += string(strconv.Itoa(v.(int))[0])
}
}
}
} else {
for _, value := range m {
if reflect.TypeOf(value).Kind() == reflect.String {
temp = value.(string)
if len(temp) > 0 {
identifier = string(temp[0])
break
}
}
}
}
return identifier
}