From 85b1542cd0bef4088aa81eff11f7535719676caf Mon Sep 17 00:00:00 2001 From: Rich Ellis Date: Mon, 17 Feb 2020 17:19:46 +0000 Subject: [PATCH] Updated DbInfo sizes for CouchDB 3 --- CHANGES.md | 2 + .../com/cloudant/client/api/model/DbInfo.java | 34 ++++++++- .../com/cloudant/tests/DbInfoMockTests.java | 71 ++++++++++++++++++- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2c83f5028..3985c72d6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,7 @@ # Unreleased - [NEW] Add getter for total row count to AllDocsResponse +- [NEW] Added `DbInfo#getSizes()` for access to improved sizes information. +- [FIXED] Corrected `DbInfo#getDiskSize()` to work with `sizes` object if `disk_size` is unavailable. - [FIXED] `ViewMultipleRequest` updated to preferentially use view `queries` format URL instead of deprecated format. diff --git a/cloudant-client/src/main/java/com/cloudant/client/api/model/DbInfo.java b/cloudant-client/src/main/java/com/cloudant/client/api/model/DbInfo.java index a174b03f7..e809a30d6 100644 --- a/cloudant-client/src/main/java/com/cloudant/client/api/model/DbInfo.java +++ b/cloudant-client/src/main/java/com/cloudant/client/api/model/DbInfo.java @@ -1,5 +1,5 @@ /* - * Copyright © 2015, 2019 IBM Corp. All rights reserved. + * Copyright © 2015, 2020 IBM Corp. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at @@ -113,6 +113,25 @@ public boolean getPartitioned() { } } + public static class Sizes { + + private long active = 0; + private long external = 0; + private long file = 0; + + public long getActive() { + return this.active; + } + + public long getExternal() { + return this.external; + } + + public long getFile() { + return this.file; + } + } + @SerializedName("db_name") private String dbName; @SerializedName("doc_count") @@ -134,6 +153,8 @@ public boolean getPartitioned() { private Props props; @SerializedName("partitioned_indexes") private PartitionedIndexes partitionedIndexes; + @SerializedName("sizes") + private Sizes sizes; public String getDbName() { return dbName; @@ -204,6 +225,12 @@ public boolean isCompactRunning() { } public long getDiskSize() { + if (diskSize == 0) { + Sizes s = getSizes(); + if (s != null) { + return s.getFile(); + } + } return diskSize; } @@ -235,6 +262,10 @@ public PartitionedIndexes getPartitionedIndexes() { return partitionedIndexes; } + public Sizes getSizes() { + return sizes; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -270,5 +301,4 @@ public String toString() { return sb.toString(); } - } diff --git a/cloudant-client/src/test/java/com/cloudant/tests/DbInfoMockTests.java b/cloudant-client/src/test/java/com/cloudant/tests/DbInfoMockTests.java index 6a16a0b58..01b1f1195 100644 --- a/cloudant-client/src/test/java/com/cloudant/tests/DbInfoMockTests.java +++ b/cloudant-client/src/test/java/com/cloudant/tests/DbInfoMockTests.java @@ -1,5 +1,5 @@ /* - * Copyright © 2018, 2019 IBM Corp. All rights reserved. + * Copyright © 2018, 2020 IBM Corp. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at @@ -152,4 +152,73 @@ public void getDbInfoDocDelCount() { assertEquals(7l, info.getDocDelCountLong()); } + /** + * Make sure the fallback caused by disk_size 0, doesn't cause an exception + */ + @Test + public void getDbInfoDiskSizeZeroWithoutException() { + CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build(); + Database db = c.database("animaldb", false); + + MockResponse response = new MockResponse() + .setResponseCode(200) + .setBody("{\"disk_size\":0}"); + server.enqueue(response); + + DbInfo info = db.info(); + assertEquals(0L, info.getDiskSize(), "The mock disk size should be 0"); + } + + /** + * Make sure disk_size still works + */ + @Test + public void getDbInfoDiskSize() { + CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build(); + Database db = c.database("animaldb", false); + + MockResponse response = new MockResponse() + .setResponseCode(200) + .setBody("{\"disk_size\":17}"); + server.enqueue(response); + + DbInfo info = db.info(); + assertEquals(17L, info.getDiskSize(), "The mock disk size should be 17"); + } + + /** + * Make sure disk_size using sizes works + */ + @Test + public void getDbInfoDiskSizeFromSizes() { + CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build(); + Database db = c.database("animaldb", false); + + MockResponse response = new MockResponse() + .setResponseCode(200) + .setBody("{\"sizes\": {\"active\": 21, \"external\":22, \"file\": 23}}"); + server.enqueue(response); + + DbInfo info = db.info(); + assertEquals(23L, info.getDiskSize(), "The mock disk size should be 23"); + } + + /** + * Make sure disk_size using sizes works + */ + @Test + public void getDbInfoSizes() { + CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build(); + Database db = c.database("animaldb", false); + + MockResponse response = new MockResponse() + .setResponseCode(200) + .setBody("{\"sizes\": {\"active\": 21, \"external\":22, \"file\": 23}}"); + server.enqueue(response); + + DbInfo info = db.info(); + assertEquals(21L, info.getSizes().getActive(), "The mock active disk size should be 21"); + assertEquals(22L, info.getSizes().getExternal(), "The mock external disk size should be 22"); + assertEquals(23L, info.getSizes().getFile(), "The mock file disk size should be 23"); + } }