Skip to content

Commit fbbedac

Browse files
authored
Merge pull request cloudant#503 from cloudant/dbinfo-sizes
Dbinfo sizes
2 parents 5166646 + 85b1542 commit fbbedac

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22
- [NEW] Add getter for total row count to AllDocsResponse
3+
- [NEW] Added `DbInfo#getSizes()` for access to improved sizes information.
4+
- [FIXED] Corrected `DbInfo#getDiskSize()` to work with `sizes` object if `disk_size` is unavailable.
35
- [FIXED] `ViewMultipleRequest` updated to preferentially use view `queries` format URL instead of
46
deprecated format.
57

cloudant-client/src/main/java/com/cloudant/client/api/model/DbInfo.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2020 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -113,6 +113,25 @@ public boolean getPartitioned() {
113113
}
114114
}
115115

116+
public static class Sizes {
117+
118+
private long active = 0;
119+
private long external = 0;
120+
private long file = 0;
121+
122+
public long getActive() {
123+
return this.active;
124+
}
125+
126+
public long getExternal() {
127+
return this.external;
128+
}
129+
130+
public long getFile() {
131+
return this.file;
132+
}
133+
}
134+
116135
@SerializedName("db_name")
117136
private String dbName;
118137
@SerializedName("doc_count")
@@ -134,6 +153,8 @@ public boolean getPartitioned() {
134153
private Props props;
135154
@SerializedName("partitioned_indexes")
136155
private PartitionedIndexes partitionedIndexes;
156+
@SerializedName("sizes")
157+
private Sizes sizes;
137158

138159
public String getDbName() {
139160
return dbName;
@@ -204,6 +225,12 @@ public boolean isCompactRunning() {
204225
}
205226

206227
public long getDiskSize() {
228+
if (diskSize == 0) {
229+
Sizes s = getSizes();
230+
if (s != null) {
231+
return s.getFile();
232+
}
233+
}
207234
return diskSize;
208235
}
209236

@@ -235,6 +262,10 @@ public PartitionedIndexes getPartitionedIndexes() {
235262
return partitionedIndexes;
236263
}
237264

265+
public Sizes getSizes() {
266+
return sizes;
267+
}
268+
238269
@Override
239270
public String toString() {
240271
StringBuilder sb = new StringBuilder();
@@ -270,5 +301,4 @@ public String toString() {
270301

271302
return sb.toString();
272303
}
273-
274304
}

cloudant-client/src/test/java/com/cloudant/tests/DbInfoMockTests.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2018, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2018, 2020 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -152,4 +152,73 @@ public void getDbInfoDocDelCount() {
152152
assertEquals(7l, info.getDocDelCountLong());
153153
}
154154

155+
/**
156+
* Make sure the fallback caused by disk_size 0, doesn't cause an exception
157+
*/
158+
@Test
159+
public void getDbInfoDiskSizeZeroWithoutException() {
160+
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
161+
Database db = c.database("animaldb", false);
162+
163+
MockResponse response = new MockResponse()
164+
.setResponseCode(200)
165+
.setBody("{\"disk_size\":0}");
166+
server.enqueue(response);
167+
168+
DbInfo info = db.info();
169+
assertEquals(0L, info.getDiskSize(), "The mock disk size should be 0");
170+
}
171+
172+
/**
173+
* Make sure disk_size still works
174+
*/
175+
@Test
176+
public void getDbInfoDiskSize() {
177+
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
178+
Database db = c.database("animaldb", false);
179+
180+
MockResponse response = new MockResponse()
181+
.setResponseCode(200)
182+
.setBody("{\"disk_size\":17}");
183+
server.enqueue(response);
184+
185+
DbInfo info = db.info();
186+
assertEquals(17L, info.getDiskSize(), "The mock disk size should be 17");
187+
}
188+
189+
/**
190+
* Make sure disk_size using sizes works
191+
*/
192+
@Test
193+
public void getDbInfoDiskSizeFromSizes() {
194+
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
195+
Database db = c.database("animaldb", false);
196+
197+
MockResponse response = new MockResponse()
198+
.setResponseCode(200)
199+
.setBody("{\"sizes\": {\"active\": 21, \"external\":22, \"file\": 23}}");
200+
server.enqueue(response);
201+
202+
DbInfo info = db.info();
203+
assertEquals(23L, info.getDiskSize(), "The mock disk size should be 23");
204+
}
205+
206+
/**
207+
* Make sure disk_size using sizes works
208+
*/
209+
@Test
210+
public void getDbInfoSizes() {
211+
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
212+
Database db = c.database("animaldb", false);
213+
214+
MockResponse response = new MockResponse()
215+
.setResponseCode(200)
216+
.setBody("{\"sizes\": {\"active\": 21, \"external\":22, \"file\": 23}}");
217+
server.enqueue(response);
218+
219+
DbInfo info = db.info();
220+
assertEquals(21L, info.getSizes().getActive(), "The mock active disk size should be 21");
221+
assertEquals(22L, info.getSizes().getExternal(), "The mock external disk size should be 22");
222+
assertEquals(23L, info.getSizes().getFile(), "The mock file disk size should be 23");
223+
}
155224
}

0 commit comments

Comments
 (0)