Skip to content

Commit abb6421

Browse files
authored
Merge pull request #264 from oscerd/influxdb-263
Resolves #263 Add databaseExists method to InfluxDB interface
2 parents c7837e7 + 031d296 commit abb6421

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
#### Features
44

55
- Support chunking
6+
- Add a databaseExists method to InfluxDB interface
67

78
#### Fixes
9+
10+
- [Issue #263] (https://github.com/influxdata/influxdb-java/issues/263) Add databaseExists method to InfluxDB interface.
11+
812
#### Improvements
913

1014

src/main/java/org/influxdb/InfluxDB.java

+10
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ public void write(final String database, final String retentionPolicy,
263263
*/
264264
public List<String> describeDatabases();
265265

266+
/**
267+
* Check if a database exists.
268+
*
269+
* @param name
270+
* the name of the database to search.
271+
*
272+
* @return true if the database exists or false if it doesn't exist
273+
*/
274+
public boolean databaseExists(final String name);
275+
266276
/**
267277
* close thread for asynchronous batch write and UDP socket to release resources if need.
268278
*/

src/main/java/org/influxdb/impl/InfluxDBImpl.java

+14
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,20 @@ public List<String> describeDatabases() {
430430
return databases;
431431
}
432432

433+
/**
434+
* {@inheritDoc}
435+
*/
436+
@Override
437+
public boolean databaseExists(final String name) {
438+
List<String> databases = this.describeDatabases();
439+
for (String databaseName : databases) {
440+
if (databaseName.trim().equals(name)) {
441+
return true;
442+
}
443+
}
444+
return false;
445+
}
446+
433447
private <T> T execute(final Call<T> call) {
434448
try {
435449
Response<T> response = call.execute();

src/test/java/org/influxdb/InfluxDBTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ public void testDescribeDatabases() {
133133
Assert.assertTrue("It is expected that describeDataBases contents the newly create database.", found);
134134
this.influxDB.deleteDatabase(dbName);
135135
}
136+
137+
/**
138+
* Test that Database exists works.
139+
*/
140+
@Test
141+
public void testDatabaseExists() {
142+
String existentdbName = "unittest_1";
143+
String notExistentdbName = "unittest_2";
144+
this.influxDB.createDatabase(existentdbName);
145+
boolean checkDbExistence = this.influxDB.databaseExists(existentdbName);
146+
Assert.assertTrue("It is expected that databaseExists return true for " + existentdbName + " database", checkDbExistence);
147+
checkDbExistence = this.influxDB.databaseExists(notExistentdbName);
148+
Assert.assertFalse("It is expected that databaseExists return false for " + notExistentdbName + " database", checkDbExistence);
149+
this.influxDB.deleteDatabase(existentdbName);
150+
}
136151

137152
/**
138153
* Test that writing to the new lineprotocol.

0 commit comments

Comments
 (0)