1//  Copyright (c) 2013 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package system
11
12import (
13	"github.com/couchbase/query/datastore"
14	"github.com/couchbase/query/errors"
15)
16
17type namespace struct {
18	store     *store
19	id        string
20	name      string
21	keyspaces map[string]datastore.Keyspace
22}
23
24func (p *namespace) DatastoreId() string {
25	return p.store.Id()
26}
27
28func (p *namespace) Id() string {
29	return p.id
30}
31
32func (p *namespace) Name() string {
33	return p.name
34}
35
36func (p *namespace) KeyspaceIds() ([]string, errors.Error) {
37	return p.KeyspaceNames()
38}
39
40func (p *namespace) KeyspaceNames() ([]string, errors.Error) {
41	rv := make([]string, len(p.keyspaces))
42	i := 0
43	for k, _ := range p.keyspaces {
44		rv[i] = k
45		i = i + 1
46	}
47	return rv, nil
48}
49
50func (p *namespace) KeyspaceById(id string) (datastore.Keyspace, errors.Error) {
51	return p.KeyspaceByName(id)
52}
53
54func (p *namespace) KeyspaceByName(name string) (datastore.Keyspace, errors.Error) {
55
56	b, ok := p.keyspaces[name]
57	if !ok {
58		return nil, errors.NewSystemKeyspaceNotFoundError(nil, name)
59	}
60
61	return b, nil
62}
63
64func (p *namespace) MetadataVersion() uint64 {
65	return 0
66}
67
68// newNamespace creates a new namespace.
69func newNamespace(s *store) (*namespace, errors.Error) {
70	p := new(namespace)
71	p.store = s
72	p.id = NAMESPACE_ID
73	p.name = NAMESPACE_NAME
74	p.keyspaces = make(map[string]datastore.Keyspace)
75
76	e := p.loadKeyspaces()
77	if e != nil {
78		return nil, e
79	}
80	return p, nil
81}
82
83func (p *namespace) loadKeyspaces() (e errors.Error) {
84
85	sb, e := newStoresKeyspace(p)
86	if e != nil {
87		return e
88	}
89	p.keyspaces[sb.Name()] = sb
90
91	pb, e := newNamespacesKeyspace(p)
92	if e != nil {
93		return e
94	}
95	p.keyspaces[pb.Name()] = pb
96
97	bb, e := newKeyspacesKeyspace(p)
98	if e != nil {
99		return e
100	}
101	p.keyspaces[bb.Name()] = bb
102
103	db, e := newDualKeyspace(p)
104	if e != nil {
105		return e
106	}
107	p.keyspaces[db.Name()] = db
108
109	ib, e := newIndexesKeyspace(p)
110	if e != nil {
111		return e
112	}
113	p.keyspaces[ib.Name()] = ib
114
115	preps, e := newPreparedsKeyspace(p)
116	if e != nil {
117		return e
118	}
119	p.keyspaces[preps.Name()] = preps
120
121	reqs, e := newRequestsKeyspace(p)
122	if e != nil {
123		return e
124	}
125	p.keyspaces[reqs.Name()] = reqs
126
127	actives, e := newActiveRequestsKeyspace(p)
128	if e != nil {
129		return e
130	}
131	p.keyspaces[actives.Name()] = actives
132
133	userInfo, e := newUserInfoKeyspace(p)
134	if e != nil {
135		return e
136	}
137	p.keyspaces[userInfo.Name()] = userInfo
138
139	myUserInfo, e := newMyUserInfoKeyspace(p)
140	if e != nil {
141		return e
142	}
143	p.keyspaces[myUserInfo.Name()] = myUserInfo
144
145	nodes, e := newNodesKeyspace(p)
146	if e != nil {
147		return e
148	}
149	p.keyspaces[nodes.Name()] = nodes
150
151	applicableRoles, e := newApplicableRolesKeyspace(p)
152	if e != nil {
153		return e
154	}
155	p.keyspaces[applicableRoles.Name()] = applicableRoles
156
157	return nil
158}
159