Skip to content

Commit 9b52e17

Browse files
committed
Merge branch 'master' into user
2 parents 69a79f8 + ce450f3 commit 9b52e17

File tree

3 files changed

+484
-1
lines changed

3 files changed

+484
-1
lines changed

collection.go

+66
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,39 @@ type Collection interface {
2929
// Name returns the name of the collection.
3030
Name() string
3131

32+
// Status fetches the current status of the collection.
33+
Status(ctx context.Context) (CollectionStatus, error)
34+
35+
// Count fetches the number of document in the collection.
36+
Count(ctx context.Context) (int64, error)
37+
38+
// Revision fetches the revision ID of the collection.
39+
// The revision ID is a server-generated string that clients can use to check whether data
40+
// in a collection has changed since the last revision check.
41+
Revision(ctx context.Context) (string, error)
42+
43+
// Properties fetches extended information about the collection.
44+
Properties(ctx context.Context) (CollectionProperties, error)
45+
46+
// SetProperties changes properties of the collection.
47+
SetProperties(ctx context.Context, options SetCollectionPropertiesOptions) error
48+
49+
// Load the collection into memory.
50+
Load(ctx context.Context) error
51+
52+
// UnLoad the collection from memory.
53+
Unload(ctx context.Context) error
54+
55+
// Rename the collection
56+
Rename(ctx context.Context, newName string) error
57+
3258
// Remove removes the entire collection.
3359
// If the collection does not exist, a NotFoundError is returned.
3460
Remove(ctx context.Context) error
3561

62+
// Truncate removes all documents from the collection, but leaves the indexes intact.
63+
Truncate(ctx context.Context) error
64+
3665
// All index functions
3766
CollectionIndexes
3867

@@ -54,6 +83,43 @@ type CollectionInfo struct {
5483
IsSystem bool `json:"isSystem,omitempty"`
5584
}
5685

86+
// CollectionProperties contains extended information about a collection.
87+
type CollectionProperties struct {
88+
CollectionInfo
89+
90+
// WaitForSync; If true then creating, changing or removing documents will wait until the data has been synchronized to disk.
91+
WaitForSync bool `json:"waitForSync,omitempty"`
92+
// DoCompact specifies whether or not the collection will be compacted.
93+
DoCompact bool `json:"doCompact,omitempty"`
94+
// JournalSize is the maximal size setting for journals / datafiles in bytes.
95+
JournalSize int64 `json:"journalSize,omitempty"`
96+
KeyOptions struct {
97+
// Type specifies the type of the key generator. The currently available generators are traditional and autoincrement.
98+
Type KeyGeneratorType `json:"type,omitempty"`
99+
// AllowUserKeys; if set to true, then it is allowed to supply own key values in the _key attribute of a document.
100+
// If set to false, then the key generator is solely responsible for generating keys and supplying own key values in
101+
// the _key attribute of documents is considered an error.
102+
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
103+
} `json:"keyOptions,omitempty"`
104+
// NumberOfShards is the number of shards of the collection.
105+
// Only available in cluster setup.
106+
NumberOfShards int `json:"numberOfShards,omitempty"`
107+
// ShardKeys contains the names of document attributes that are used to determine the target shard for documents.
108+
// Only available in cluster setup.
109+
ShardKeys []string `json:"shardKeys,omitempty"`
110+
// ReplicationFactor contains how many copies of each shard are kept on different DBServers.
111+
// Only available in cluster setup.
112+
ReplicationFactor int `json:"replicationFactor,omitempty"`
113+
}
114+
115+
// SetCollectionPropertiesOptions contains data for Collection.SetProperties.
116+
type SetCollectionPropertiesOptions struct {
117+
// If true then creating or changing a document will wait until the data has been synchronized to disk.
118+
WaitForSync *bool `json:"waitForSync,omitempty"`
119+
// The maximal size of a journal or datafile in bytes. The value must be at least 1048576 (1 MB). Note that when changing the journalSize value, it will only have an effect for additional journals or datafiles that are created. Already existing journals or datafiles will not be affected.
120+
JournalSize int64 `json:"journalSize,omitempty"`
121+
}
122+
57123
// CollectionStatus indicates the status of a collection.
58124
type CollectionStatus int
59125

collection_impl.go

+188
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,178 @@ func (c *collection) Name() string {
5959
return c.name
6060
}
6161

62+
// Status fetches the current status of the collection.
63+
func (c *collection) Status(ctx context.Context) (CollectionStatus, error) {
64+
req, err := c.conn.NewRequest("GET", c.relPath("collection"))
65+
if err != nil {
66+
return CollectionStatus(0), WithStack(err)
67+
}
68+
resp, err := c.conn.Do(ctx, req)
69+
if err != nil {
70+
return CollectionStatus(0), WithStack(err)
71+
}
72+
if err := resp.CheckStatus(200); err != nil {
73+
return CollectionStatus(0), WithStack(err)
74+
}
75+
var data CollectionInfo
76+
if err := resp.ParseBody("", &data); err != nil {
77+
return CollectionStatus(0), WithStack(err)
78+
}
79+
return data.Status, nil
80+
}
81+
82+
// Count fetches the number of document in the collection.
83+
func (c *collection) Count(ctx context.Context) (int64, error) {
84+
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "count"))
85+
if err != nil {
86+
return 0, WithStack(err)
87+
}
88+
resp, err := c.conn.Do(ctx, req)
89+
if err != nil {
90+
return 0, WithStack(err)
91+
}
92+
if err := resp.CheckStatus(200); err != nil {
93+
return 0, WithStack(err)
94+
}
95+
var data struct {
96+
Count int64 `json:"count,omitempty"`
97+
}
98+
if err := resp.ParseBody("", &data); err != nil {
99+
return 0, WithStack(err)
100+
}
101+
return data.Count, nil
102+
}
103+
104+
// Revision fetches the revision ID of the collection.
105+
// The revision ID is a server-generated string that clients can use to check whether data
106+
// in a collection has changed since the last revision check.
107+
func (c *collection) Revision(ctx context.Context) (string, error) {
108+
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "revision"))
109+
if err != nil {
110+
return "", WithStack(err)
111+
}
112+
resp, err := c.conn.Do(ctx, req)
113+
if err != nil {
114+
return "", WithStack(err)
115+
}
116+
if err := resp.CheckStatus(200); err != nil {
117+
return "", WithStack(err)
118+
}
119+
var data struct {
120+
Revision string `json:"revision,omitempty"`
121+
}
122+
if err := resp.ParseBody("", &data); err != nil {
123+
return "", WithStack(err)
124+
}
125+
return data.Revision, nil
126+
}
127+
128+
// Properties fetches extended information about the collection.
129+
func (c *collection) Properties(ctx context.Context) (CollectionProperties, error) {
130+
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "properties"))
131+
if err != nil {
132+
return CollectionProperties{}, WithStack(err)
133+
}
134+
resp, err := c.conn.Do(ctx, req)
135+
if err != nil {
136+
return CollectionProperties{}, WithStack(err)
137+
}
138+
if err := resp.CheckStatus(200); err != nil {
139+
return CollectionProperties{}, WithStack(err)
140+
}
141+
var data CollectionProperties
142+
if err := resp.ParseBody("", &data); err != nil {
143+
return CollectionProperties{}, WithStack(err)
144+
}
145+
return data, nil
146+
}
147+
148+
// SetProperties changes properties of the collection.
149+
func (c *collection) SetProperties(ctx context.Context, options SetCollectionPropertiesOptions) error {
150+
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "properties"))
151+
if err != nil {
152+
return WithStack(err)
153+
}
154+
if _, err := req.SetBody(options); err != nil {
155+
return WithStack(err)
156+
}
157+
resp, err := c.conn.Do(ctx, req)
158+
if err != nil {
159+
return WithStack(err)
160+
}
161+
if err := resp.CheckStatus(200); err != nil {
162+
return WithStack(err)
163+
}
164+
return nil
165+
}
166+
167+
// Load the collection into memory.
168+
func (c *collection) Load(ctx context.Context) error {
169+
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "load"))
170+
if err != nil {
171+
return WithStack(err)
172+
}
173+
opts := struct {
174+
Count bool `json:"count"`
175+
}{
176+
Count: false,
177+
}
178+
if _, err := req.SetBody(opts); err != nil {
179+
return WithStack(err)
180+
}
181+
resp, err := c.conn.Do(ctx, req)
182+
if err != nil {
183+
return WithStack(err)
184+
}
185+
if err := resp.CheckStatus(200); err != nil {
186+
return WithStack(err)
187+
}
188+
return nil
189+
}
190+
191+
// UnLoad the collection from memory.
192+
func (c *collection) Unload(ctx context.Context) error {
193+
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "unload"))
194+
if err != nil {
195+
return WithStack(err)
196+
}
197+
resp, err := c.conn.Do(ctx, req)
198+
if err != nil {
199+
return WithStack(err)
200+
}
201+
if err := resp.CheckStatus(200); err != nil {
202+
return WithStack(err)
203+
}
204+
return nil
205+
206+
}
207+
208+
// Rename the collection
209+
func (c *collection) Rename(ctx context.Context, newName string) error {
210+
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "rename"))
211+
if err != nil {
212+
return WithStack(err)
213+
}
214+
opts := struct {
215+
Name string `json:"name"`
216+
}{
217+
Name: newName,
218+
}
219+
if _, err := req.SetBody(opts); err != nil {
220+
return WithStack(err)
221+
}
222+
resp, err := c.conn.Do(ctx, req)
223+
if err != nil {
224+
return WithStack(err)
225+
}
226+
if err := resp.CheckStatus(200); err != nil {
227+
return WithStack(err)
228+
}
229+
// Update internal name
230+
c.name = newName
231+
return nil
232+
}
233+
62234
// Remove removes the entire collection.
63235
// If the collection does not exist, a NotFoundError is returned.
64236
func (c *collection) Remove(ctx context.Context) error {
@@ -75,3 +247,19 @@ func (c *collection) Remove(ctx context.Context) error {
75247
}
76248
return nil
77249
}
250+
251+
// Truncate removes all documents from the collection, but leaves the indexes intact.
252+
func (c *collection) Truncate(ctx context.Context) error {
253+
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "truncate"))
254+
if err != nil {
255+
return WithStack(err)
256+
}
257+
resp, err := c.conn.Do(ctx, req)
258+
if err != nil {
259+
return WithStack(err)
260+
}
261+
if err := resp.CheckStatus(200); err != nil {
262+
return WithStack(err)
263+
}
264+
return nil
265+
}

0 commit comments

Comments
 (0)