@@ -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.
64236func (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