Skip to content

Commit

Permalink
Merge pull request #264 from oscerd/influxdb-263
Browse files Browse the repository at this point in the history
Resolves #263 Add databaseExists method to InfluxDB interface
  • Loading branch information
majst01 committed Jan 3, 2017
2 parents c7837e7 + 031d296 commit abb6421
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#### Features

- Support chunking
- Add a databaseExists method to InfluxDB interface

#### Fixes

- [Issue #263] (https://github.com/influxdata/influxdb-java/issues/263) Add databaseExists method to InfluxDB interface.

#### Improvements


Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/influxdb/InfluxDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ public void write(final String database, final String retentionPolicy,
*/
public List<String> describeDatabases();

/**
* Check if a database exists.
*
* @param name
* the name of the database to search.
*
* @return true if the database exists or false if it doesn't exist
*/
public boolean databaseExists(final String name);

/**
* close thread for asynchronous batch write and UDP socket to release resources if need.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,20 @@ public List<String> describeDatabases() {
return databases;
}

/**
* {@inheritDoc}
*/
@Override
public boolean databaseExists(final String name) {
List<String> databases = this.describeDatabases();
for (String databaseName : databases) {
if (databaseName.trim().equals(name)) {
return true;
}
}
return false;
}

private <T> T execute(final Call<T> call) {
try {
Response<T> response = call.execute();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ public void testDescribeDatabases() {
Assert.assertTrue("It is expected that describeDataBases contents the newly create database.", found);
this.influxDB.deleteDatabase(dbName);
}

/**
* Test that Database exists works.
*/
@Test
public void testDatabaseExists() {
String existentdbName = "unittest_1";
String notExistentdbName = "unittest_2";
this.influxDB.createDatabase(existentdbName);
boolean checkDbExistence = this.influxDB.databaseExists(existentdbName);
Assert.assertTrue("It is expected that databaseExists return true for " + existentdbName + " database", checkDbExistence);
checkDbExistence = this.influxDB.databaseExists(notExistentdbName);
Assert.assertFalse("It is expected that databaseExists return false for " + notExistentdbName + " database", checkDbExistence);
this.influxDB.deleteDatabase(existentdbName);
}

/**
* Test that writing to the new lineprotocol.
Expand Down

0 comments on commit abb6421

Please sign in to comment.