-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcatalog.go
67 lines (53 loc) · 1.45 KB
/
catalog.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
package lungo
import (
"fmt"
"strings"
"github.com/256dpi/lungo/mongokit"
)
// Handle is a two component identifier for namespaces where the first part is
// the database and the second the collection.
type Handle [2]string
// String will return the string form of the handle.
func (h Handle) String() string {
return strings.Join(h[:], ".")
}
// Validate will validate the handle.
func (h Handle) Validate(needCollection bool) error {
// check database
if h[0] == "" {
return fmt.Errorf("missing database in handle")
}
// check collection
if needCollection && h[1] == "" {
return fmt.Errorf("missing collection in handle")
}
return nil
}
// Local is the local database.
const Local = "local"
// Oplog is the handle for the local oplog namespace.
var Oplog = Handle{Local, "oplog"}
// Catalog is the top level object per database that contains all data.
type Catalog struct {
Namespaces map[Handle]*mongokit.Collection
}
// NewCatalog creates and returns a new catalog.
func NewCatalog() *Catalog {
return &Catalog{
Namespaces: map[Handle]*mongokit.Collection{
Oplog: mongokit.NewCollection(false),
},
}
}
// Clone will clone the catalog. Namespaces need to be cloned separately.
func (d *Catalog) Clone() *Catalog {
// create clone
clone := &Catalog{
Namespaces: make(map[Handle]*mongokit.Collection, len(d.Namespaces)),
}
// copy namespaces
for name, namespace := range d.Namespaces {
clone.Namespaces[name] = namespace
}
return clone
}