Skip to content

Commit f87797f

Browse files
committed
feature: #295 Create and Drop retention policies
1 parent 4e3425c commit f87797f

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,44 @@ public void write(final String database, final String retentionPolicy,
395395
* @return the InfluxDB instance to be able to use it in a fluent manner.
396396
*/
397397
public InfluxDB setRetentionPolicy(final String retentionPolicy);
398+
399+
/**
400+
* Creates a retentionPolicy.
401+
* @param rpName the name of the retentionPolicy(rp)
402+
* @param database the name of the database
403+
* @param duration the duration of the rp
404+
* @param shardDuration the shardDuration
405+
* @param replicationFactor the replicationFactor of the rp
406+
* @param isDefault if the rp is the default rp for the database or not
407+
*/
408+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
409+
final String shardDuration, final int replicationFactor, final boolean isDefault);
410+
411+
/**
412+
* Creates a retentionPolicy. (optional shardDuration)
413+
* @param rpName the name of the retentionPolicy(rp)
414+
* @param database the name of the database
415+
* @param duration the duration of the rp
416+
* @param replicationFactor the replicationFactor of the rp
417+
* @param isDefault if the rp is the default rp for the database or not
418+
*/
419+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
420+
final int replicationFactor, final boolean isDefault);
421+
422+
/**
423+
* Creates a retentionPolicy. (optional shardDuration and isDefault)
424+
* @param rpName the name of the retentionPolicy(rp)
425+
* @param database the name of the database
426+
* @param duration the duration of the rp
427+
* @param replicationFactor the replicationFactor of the rp
428+
*/
429+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
430+
final String shardDuration, final int replicationFactor);
431+
432+
/**
433+
* Drops a retentionPolicy in a database.
434+
* @param rpName the name of the retentionPolicy
435+
* @param database the name of the database
436+
*/
437+
public void dropRetentionPolicy(final String rpName, final String database);
398438
}

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

+69
Original file line numberDiff line numberDiff line change
@@ -557,4 +557,73 @@ public InfluxDB setRetentionPolicy(final String retentionPolicy) {
557557
this.retentionPolicy = retentionPolicy;
558558
return this;
559559
}
560+
561+
/**
562+
* {@inheritDoc}
563+
*/
564+
@Override
565+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
566+
final String shardDuration, final int replicationFactor, final boolean isDefault) {
567+
Preconditions.checkNonEmptyString(rpName, "retentionPolicyName");
568+
Preconditions.checkNonEmptyString(database, "database");
569+
Preconditions.checkNonEmptyString(duration, "retentionDuration");
570+
Preconditions.checkDuration(duration, "retentionDuration");
571+
if (shardDuration != null && !shardDuration.isEmpty()) {
572+
Preconditions.checkDuration(shardDuration, "shardDuration");
573+
}
574+
Preconditions.checkPositiveNumber(replicationFactor, "replicationFactor");
575+
576+
StringBuilder queryBuilder = new StringBuilder("CREATE RETENTION POLICY \"");
577+
queryBuilder.append(rpName)
578+
.append("\" ON \"")
579+
.append(database)
580+
.append("\" DURATION ")
581+
.append(duration)
582+
.append(" REPLICATION ")
583+
.append(replicationFactor);
584+
if (shardDuration != null && !shardDuration.isEmpty()) {
585+
queryBuilder.append(" SHARD DURATION ");
586+
queryBuilder.append(shardDuration);
587+
}
588+
if (isDefault) {
589+
queryBuilder.append(" DEFAULT");
590+
}
591+
execute(this.influxDBService.postQuery(this.username, this.password, Query.encode(queryBuilder.toString())));
592+
}
593+
594+
/**
595+
* {@inheritDoc}
596+
*/
597+
@Override
598+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
599+
final int replicationFactor, final boolean isDefault) {
600+
createRetentionPolicy(rpName, database, duration, null, replicationFactor, isDefault);
601+
}
602+
603+
/**
604+
* {@inheritDoc}
605+
*/
606+
@Override
607+
public void createRetentionPolicy(final String rpName, final String database, final String duration,
608+
final String shardDuration, final int replicationFactor) {
609+
createRetentionPolicy(rpName, database, duration, null, replicationFactor, false);
610+
}
611+
612+
/**
613+
* {@inheritDoc}
614+
* @param rpName the name of the retentionPolicy
615+
* @param database the name of the database
616+
*/
617+
@Override
618+
public void dropRetentionPolicy(final String rpName, final String database) {
619+
Preconditions.checkNonEmptyString(rpName, "retentionPolicyName");
620+
Preconditions.checkNonEmptyString(database, "database");
621+
StringBuilder queryBuilder = new StringBuilder("DROP RETENTION POLICY \"");
622+
queryBuilder.append(rpName)
623+
.append("\" ON \"")
624+
.append(database)
625+
.append("\"");
626+
execute(this.influxDBService.postQuery(this.username, this.password,
627+
Query.encode(queryBuilder.toString())));
628+
}
560629
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,17 @@ public static void checkPositiveNumber(final Number number, final String name) t
3535
throw new IllegalArgumentException("Expecting a positive number for " + name);
3636
}
3737
}
38+
39+
/**
40+
* Enforces that the duration is a valid influxDB duration.
41+
* @param duration the duration to test
42+
* @param name variable name for reporting
43+
* @throws IllegalArgumentException
44+
*/
45+
public static void checkDuration(final String duration, final String name) throws IllegalArgumentException {
46+
if (!duration.matches("(\\d+[wdmhs])+")) {
47+
throw new IllegalArgumentException("Invalid InfluxDB duration: " + duration
48+
+ "for " + name);
49+
}
50+
}
3851
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,34 @@ public void testFlushThrowsIfBatchingIsNotEnabled() {
715715
this.influxDB.flush();
716716
}
717717

718+
/**
719+
* Test creation and deletion of retention policies
720+
*/
721+
@Test
722+
public void testCreateDropRetentionPolicies() {
723+
String dbName = "rpTest_" + System.currentTimeMillis();
724+
this.influxDB.createDatabase(dbName);
725+
726+
this.influxDB.createRetentionPolicy("testRP1", dbName, "30h", 2, false);
727+
this.influxDB.createRetentionPolicy("testRP2", dbName, "10d", "20m", 2, false);
728+
this.influxDB.createRetentionPolicy("testRP3", dbName, "2d4w", "20m", 2);
729+
730+
Query query = new Query("SHOW RETENTION POLICIES", dbName);
731+
QueryResult result = this.influxDB.query(query);
732+
Assert.assertNull(result.getError());
733+
List<List<Object>> retentionPolicies = result.getResults().get(0).getSeries().get(0).getValues();
734+
Assert.assertTrue(retentionPolicies.get(1).contains("testRP1"));
735+
Assert.assertTrue(retentionPolicies.get(2).contains("testRP2"));
736+
Assert.assertTrue(retentionPolicies.get(3).contains("testRP3"));
737+
738+
this.influxDB.dropRetentionPolicy("testRP1", dbName);
739+
this.influxDB.dropRetentionPolicy("testRP2", dbName);
740+
this.influxDB.dropRetentionPolicy("testRP3", dbName);
741+
742+
result = this.influxDB.query(query);
743+
Assert.assertNull(result.getError());
744+
retentionPolicies = result.getResults().get(0).getSeries().get(0).getValues();
745+
Assert.assertTrue(retentionPolicies.size() == 1);
746+
}
747+
718748
}

0 commit comments

Comments
 (0)