-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
78 lines (73 loc) · 1.61 KB
/
namespace.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 unikv
import "fmt"
// Namespace is the namespace type
type Namespace struct {
Name string
Prefix string
buckets map[string]*Bucket
}
// NewNamespace creates a new namespace
func NewNamespace(name string) *Namespace {
conf, ok := GetConfigure().Namespaces[name]
prefix := name
if ok {
if conf.Prefix != "" {
prefix = conf.Prefix
}
if conf.Prefix == "$!empty" {
prefix = ""
}
}
return &Namespace{
Name: name,
Prefix: prefix,
buckets: make(map[string]*Bucket),
}
}
// NewBucket creates a new bucket on a namespace
func (ns *Namespace) NewBucket(name string) (*Bucket, error) {
if _, ok := ns.buckets[name]; ok {
return ns.buckets[name], nil
}
bckconf, ok := configure.Namespaces[ns.Name]
var conf *ConfigureBuckets
prefix := concatPrefix(ns.Prefix, name)
if ok {
conf, ok = bckconf.Buckets[name]
}
if !ok {
conf = &ConfigureBuckets{
Prefix: "",
Driver: configure.Default.Driver,
Context: configure.Default.Context,
}
}
if conf.Prefix != "" {
prefix = concatPrefix(ns.Prefix, conf.Prefix)
}
if conf.Prefix == "$!empty" {
prefix = ns.Prefix
}
rslt := &Bucket{
Name: name,
Prefix: prefix,
NamespaceName: ns.Name,
namespace: ns,
}
if _, ok := drivers[conf.Driver]; !ok {
return nil, fmt.Errorf("Unknow driver %s", conf.Driver)
}
drv, err := drivers[conf.Driver].Construct(prefix, conf.Context)
if err != nil {
return nil, err
}
rslt.Driver = drv
ns.buckets[name] = rslt
return rslt, nil
}
// Close closes all the buckets in a namespace
func (ns *Namespace) Close() {
for k := range ns.buckets {
ns.buckets[k].Close()
}
}