-
Notifications
You must be signed in to change notification settings - Fork 2
/
Functions_DBVals.go
78 lines (66 loc) · 1.44 KB
/
Functions_DBVals.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
package main
import (
"sort"
"fmt"
)
/*
This is essentially the DBVals class and its functions
They're only separated to make the files smaller and reduce clutter
*/
type dbVal struct {
path []string
k []byte
v []byte
}
// Get the key as a string
func (d dbVal) key() string{
return string(d.k)
}
// Get the value as a string
func (d dbVal) val() string{
return string(d.v)
}
// Test if it is a bucket
func (d dbVal) isBucket() bool{
return d.v==nil
}
// get the path to the bucket this value is in
func (d dbVal) bucketString() string{
s := ""
for i:=0;i<len(d.path);i++{
s=s+d.path[i]+"/"
}
return s
}
// return a bucket made with this dbVal
func (d dbVal) asBucket() bckt{
return bckt{append(d.path, d.key())}
}
func (d dbVal) toString(verbose bool) string{
r := ""
if verbose && d.isBucket() {
bc,vc:=d.asBucket().countBoth()
r = fmt.Sprintf("[Bucket] %s%s\n- Contains %d %s and %d %s\n",d.bucketString(),d.key(),vc,valPlural(vc),bc,bcktPlural(bc))
} else if d.isBucket() {
r = fmt.Sprintf("%s",d.key())
} else if verbose {
r = fmt.Sprintf("[Key] %s%s\n- Value ([]Byte): %v\n",d.bucketString(),d.key(),d.v)
} else {
r = d.key()
}
return r
}
func sortArray(dbvs []dbVal)[]dbVal{
r := []dbVal{}
tmp := make(map[string]dbVal)
keys := []string{}
for i:=0;i<len(dbvs);i++{
keys=append(keys,dbvs[i].key())
tmp[dbvs[i].key()]=dbvs[i]
}
sort.Strings(keys)
for i:=0;i<len(keys);i++{
r=append(r,tmp[keys[i]])
}
return r
}