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