Skip to content

Commit

Permalink
Drop guava runtime dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed May 25, 2017
1 parent 935bc28 commit 867aa22
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 104 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/influxdb/InfluxDBFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.influxdb.impl.InfluxDBImpl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import okhttp3.OkHttpClient;
import org.influxdb.impl.Preconditions;

import java.util.Objects;


/**
Expand All @@ -25,7 +25,7 @@ public enum InfluxDBFactory {
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(url), "The URL may not be null or empty.");
Preconditions.checkNonEmptyString(url, "url");
return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder());
}

Expand All @@ -42,8 +42,8 @@ public static InfluxDB connect(final String url) {
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(url), "The URL may not be null or empty.");
Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "The username may not be null or empty.");
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkNonEmptyString(username, "username");
return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder());
}

Expand All @@ -57,8 +57,8 @@ public static InfluxDB connect(final String url, final String username, final St
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final OkHttpClient.Builder client) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(url), "The URL may not be null or empty.");
Preconditions.checkNotNull(client, "The client may not be null.");
Preconditions.checkNonEmptyString(url, "url");
Objects.requireNonNull(client, "client");
return new InfluxDBImpl(url, null, null, client);
}

Expand All @@ -78,9 +78,9 @@ public static InfluxDB connect(final String url, final OkHttpClient.Builder clie
*/
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(url), "The URL may not be null or empty.");
Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "The username may not be null or empty.");
Preconditions.checkNotNull(client, "The client may not be null.");
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkNonEmptyString(username, "username");
Objects.requireNonNull(client, "client");
return new InfluxDBImpl(url, username, password, client);
}
}
15 changes: 6 additions & 9 deletions src/main/java/org/influxdb/dto/BatchPoints.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package org.influxdb.dto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import org.influxdb.InfluxDB.ConsistencyLevel;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import org.influxdb.impl.Preconditions;

/**
* {Purpose of This Type}.
Expand Down Expand Up @@ -49,8 +47,8 @@ public static Builder database(final String database) {
public static final class Builder {
private final String database;
private String retentionPolicy;
private final Map<String, String> tags = Maps.newTreeMap(Ordering.natural());
private final List<Point> points = Lists.newArrayList();
private final Map<String, String> tags = new TreeMap<>();
private final List<Point> points = new ArrayList<>();
private ConsistencyLevel consistency;

/**
Expand Down Expand Up @@ -124,8 +122,7 @@ public Builder consistency(final ConsistencyLevel consistencyLevel) {
* @return the created BatchPoints.
*/
public BatchPoints build() {
Preconditions.checkArgument(!Strings.isNullOrEmpty(this.database),
"Database must not be null or empty.");
Preconditions.checkNonEmptyString(this.database, "database");
BatchPoints batchPoints = new BatchPoints();
batchPoints.setDatabase(this.database);
for (Point point : this.points) {
Expand Down
47 changes: 17 additions & 30 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers;
import org.influxdb.impl.Preconditions;

/**
* Representation of a InfluxDB database Point.
Expand All @@ -28,15 +26,10 @@ public class Point {
private TimeUnit precision = TimeUnit.NANOSECONDS;
private Map<String, Object> fields;

private static final Escaper FIELD_ESCAPER = Escapers.builder()
.addEscape('\\', "\\\\")
.addEscape('"', "\\\"")
.build();
private static final Escaper KEY_ESCAPER = Escapers.builder()
.addEscape(' ', "\\ ")
.addEscape(',', "\\,")
.addEscape('=', "\\=")
.build();
private static final Function<String, String> FIELD_ESCAPER = s ->
s.replace("\\", "\\\\").replace("\"", "\\\"");
private static final Function<String, String> KEY_ESCAPER = s ->
s.replace(" ", "\\ ").replace(",", "\\,").replace("=", "\\=");
private static final int MAX_FRACTION_DIGITS = 340;

Point() {
Expand Down Expand Up @@ -84,8 +77,8 @@ public static final class Builder {
* @return the Builder instance.
*/
public Builder tag(final String tagName, final String value) {
Preconditions.checkArgument(tagName != null);
Preconditions.checkArgument(value != null);
Objects.requireNonNull(tagName, "tagName");
Objects.requireNonNull(value, "value");
if (!tagName.isEmpty() && !value.isEmpty()) {
tags.put(tagName, value);
}
Expand Down Expand Up @@ -161,9 +154,7 @@ public Builder addField(final String field, final Number value) {
}

public Builder addField(final String field, final String value) {
if (value == null) {
throw new IllegalArgumentException("Field value cannot be null");
}
Objects.requireNonNull(value, "value");

fields.put(field, value);
return this;
Expand All @@ -189,7 +180,7 @@ public Builder fields(final Map<String, Object> fieldsToAdd) {
* @return the Builder instance.
*/
public Builder time(final long timeToSet, final TimeUnit precisionToSet) {
Preconditions.checkNotNull(precisionToSet, "Precision must be not null!");
Objects.requireNonNull(precisionToSet, "precisionToSet");
this.time = timeToSet;
this.precision = precisionToSet;
return this;
Expand All @@ -201,12 +192,8 @@ public Builder time(final long timeToSet, final TimeUnit precisionToSet) {
* @return the newly created Point.
*/
public Point build() {
Preconditions
.checkArgument(!Strings.isNullOrEmpty(this.measurement),
"Point name must not be null or empty.");
Preconditions
.checkArgument(this.fields.size() > 0,
"Point must have at least one field specified.");
Preconditions.checkNonEmptyString(this.measurement, "measurement");
Preconditions.checkPositiveNumber(this.fields.size(), "fields size");
Point point = new Point();
point.setFields(this.fields);
point.setMeasurement(this.measurement);
Expand Down Expand Up @@ -321,7 +308,7 @@ public String toString() {
*/
public String lineProtocol() {
final StringBuilder sb = new StringBuilder();
sb.append(KEY_ESCAPER.escape(this.measurement));
sb.append(KEY_ESCAPER.apply(this.measurement));
sb.append(concatenatedTags());
sb.append(concatenateFields());
sb.append(formatedTime());
Expand All @@ -332,9 +319,9 @@ private StringBuilder concatenatedTags() {
final StringBuilder sb = new StringBuilder();
for (Entry<String, String> tag : this.tags.entrySet()) {
sb.append(",")
.append(KEY_ESCAPER.escape(tag.getKey()))
.append(KEY_ESCAPER.apply(tag.getKey()))
.append("=")
.append(KEY_ESCAPER.escape(tag.getValue()));
.append(KEY_ESCAPER.apply(tag.getValue()));
}
sb.append(" ");
return sb;
Expand All @@ -357,10 +344,10 @@ private StringBuilder concatenateFields() {
continue;
}

sb.append(KEY_ESCAPER.escape(field.getKey())).append("=");
sb.append(KEY_ESCAPER.apply(field.getKey())).append("=");
if (value instanceof String) {
String stringValue = (String) value;
sb.append("\"").append(FIELD_ESCAPER.escape(stringValue)).append("\"");
sb.append("\"").append(FIELD_ESCAPER.apply(stringValue)).append("\"");
} else if (value instanceof Number) {
if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) {
sb.append(numberFormat.format(value));
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/org/influxdb/dto/Pong.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.influxdb.dto;

import com.google.common.base.MoreObjects;

/**
* Representation of the response for a influxdb ping.
*
Expand Down Expand Up @@ -47,11 +45,7 @@ public void setResponseTime(final long responseTime) {
*/
@Override
public String toString() {
return MoreObjects
.toStringHelper(this.getClass())
.add("version", this.version)
.add("responseTime", this.responseTime)
.toString();
return "Pong{version=" + version + ", responseTime=" + responseTime + "}";
}

}
21 changes: 10 additions & 11 deletions src/main/java/org/influxdb/impl/BatchProcessor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.influxdb.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -18,9 +20,6 @@
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;

/**
* A BatchProcessor can be attached to a InfluxDB Instance to collect single point writes and
* aggregates them to BatchPoints to get a better write performance.
Expand Down Expand Up @@ -114,12 +113,12 @@ public Builder exceptionHandler(final BiConsumer<Iterable<Point>, Throwable> han
* @return the BatchProcessor instance.
*/
public BatchProcessor build() {
Preconditions.checkNotNull(this.influxDB, "influxDB may not be null");
Preconditions.checkArgument(this.actions > 0, "actions should > 0");
Preconditions.checkArgument(this.flushInterval > 0, "flushInterval should > 0");
Preconditions.checkNotNull(this.flushIntervalUnit, "flushIntervalUnit may not be null");
Preconditions.checkNotNull(this.threadFactory, "threadFactory may not be null");
Preconditions.checkNotNull(this.exceptionHandler, "exceptionHandler may not be null");
Objects.requireNonNull(this.influxDB, "influxDB");
Preconditions.checkPositiveNumber(this.actions, "actions");
Preconditions.checkPositiveNumber(this.flushInterval, "flushInterval");
Objects.requireNonNull(this.flushIntervalUnit, "flushIntervalUnit");
Objects.requireNonNull(this.threadFactory, "threadFactory");
Objects.requireNonNull(this.exceptionHandler, "exceptionHandler");
return new BatchProcessor(this.influxDB, this.threadFactory, this.actions, this.flushIntervalUnit,
this.flushInterval, exceptionHandler);
}
Expand Down Expand Up @@ -212,9 +211,9 @@ void write() {
return;
}
//for batch on HTTP.
Map<String, BatchPoints> batchKeyToBatchPoints = Maps.newHashMap();
Map<String, BatchPoints> batchKeyToBatchPoints = new HashMap<>();
//for batch on UDP.
Map<Integer, List<String>> udpPortToBatchPoints = Maps.newHashMap();
Map<Integer, List<String>> udpPortToBatchPoints = new HashMap<>();
List<AbstractBatchEntry> batchEntries = new ArrayList<>(this.queue.size());
this.queue.drainTo(batchEntries);
currentBatch = new ArrayList<>(batchEntries.size());
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package org.influxdb.impl;


import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

Expand Down Expand Up @@ -41,6 +36,7 @@
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -204,7 +200,7 @@ public boolean isBatchEnabled() {

@Override
public Pong ping() {
Stopwatch watch = Stopwatch.createStarted();
final long started = System.currentTimeMillis();
Call<ResponseBody> call = this.influxDBService.ping();
try {
Response<ResponseBody> response = call.execute();
Expand All @@ -218,7 +214,7 @@ public Pong ping() {
}
Pong pong = new Pong();
pong.setVersion(version);
pong.setResponseTime(watch.elapsed(TimeUnit.MILLISECONDS));
pong.setResponseTime(System.currentTimeMillis() - started);
return pong;
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -290,8 +286,7 @@ public void write(final String database, final String retentionPolicy, final Con
@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency,
final List<String> records) {
final String joinedRecords = Joiner.on("\n").join(records);
write(database, retentionPolicy, consistency, joinedRecords);
write(database, retentionPolicy, consistency, String.join("\n", records));
}

/**
Expand Down Expand Up @@ -327,8 +322,7 @@ private void initialDatagramSocket() {
*/
@Override
public void write(final int udpPort, final List<String> records) {
final String joinedRecords = Joiner.on("\n").join(records);
write(udpPort, joinedRecords);
write(udpPort, String.join("\n", records));
}

/**
Expand Down Expand Up @@ -406,7 +400,7 @@ public QueryResult query(final Query query, final TimeUnit timeUnit) {
*/
@Override
public void createDatabase(final String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Database name may not be null or empty");
Preconditions.checkNonEmptyString(name, "name");
String createDatabaseQueryString = String.format("CREATE DATABASE \"%s\"", name);
if (this.version().startsWith("0.")) {
createDatabaseQueryString = String.format("CREATE DATABASE IF NOT EXISTS \"%s\"", name);
Expand All @@ -433,7 +427,7 @@ public List<String> describeDatabases() {
// {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]}
// Series [name=databases, columns=[name], values=[[mydb], [unittest_1433605300968]]]
List<List<Object>> databaseNames = result.getResults().get(0).getSeries().get(0).getValues();
List<String> databases = Lists.newArrayList();
List<String> databases = new ArrayList<>();
if (databaseNames != null) {
for (List<Object> database : databaseNames) {
databases.add(database.get(0).toString());
Expand Down
Loading

0 comments on commit 867aa22

Please sign in to comment.