com.influxdb.v3.client.internal.InfluxDBClientImpl Maven / Gradle / Ivy
Show all versions of influxdb3-java Show documentation
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.v3.client.internal;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import io.netty.handler.codec.http.HttpMethod;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import com.influxdb.v3.client.InfluxDBApiException;
import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.Point;
import com.influxdb.v3.client.PointValues;
import com.influxdb.v3.client.config.ClientConfig;
import com.influxdb.v3.client.query.QueryOptions;
import com.influxdb.v3.client.write.WriteOptions;
import com.influxdb.v3.client.write.WritePrecision;
/**
* Implementation of the InfluxDBClient. It is thread-safe and can be safely shared between threads.
*
* Please use {@link InfluxDBClient} to create an instance.
*/
public final class InfluxDBClientImpl implements InfluxDBClient {
private static final Logger LOG = Logger.getLogger(InfluxDBClientImpl.class.getName());
private static final String DATABASE_REQUIRED_MESSAGE = "Please specify the 'Database' as a method parameter "
+ "or use default configuration at 'ClientConfig.database'.";
private static final Map NO_PARAMETERS = Map.of();
private static final List> ALLOWED_NAMED_PARAMETER_TYPES = List.of(
String.class,
Integer.class,
Long.class,
Float.class,
Double.class,
Boolean.class
);
private boolean closed = false;
private final ClientConfig config;
private final RestClient restClient;
private final FlightSqlClient flightSqlClient;
/**
* Creates an instance using the specified config.
*
* Please use {@link InfluxDBClient} to create an instance.
*
* @param config the client config.
*/
public InfluxDBClientImpl(@Nonnull final ClientConfig config) {
this(config, null, null);
}
/**
* Constructor for testing purposes.
*
* @param config the client config
* @param restClient the rest client, if null a new client will be created
* @param flightSqlClient the flight sql client, if null a new client will be created
*/
InfluxDBClientImpl(@Nonnull final ClientConfig config,
@Nullable final RestClient restClient,
@Nullable final FlightSqlClient flightSqlClient) {
Arguments.checkNotNull(config, "config");
config.validate();
this.config = config;
this.restClient = restClient != null ? restClient : new RestClient(config);
this.flightSqlClient = flightSqlClient != null ? flightSqlClient : new FlightSqlClient(config);
}
@Override
public void writeRecord(@Nullable final String record) {
writeRecord(record, WriteOptions.DEFAULTS);
}
@Override
public void writeRecord(@Nullable final String record, @Nonnull final WriteOptions options) {
if (record == null) {
return;
}
writeRecords(Collections.singletonList(record), options);
}
@Override
public void writeRecords(@Nonnull final List records) {
writeRecords(records, WriteOptions.DEFAULTS);
}
@Override
public void writeRecords(@Nonnull final List records, @Nonnull final WriteOptions options) {
writeData(records, options);
}
@Override
public void writePoint(@Nullable final Point point) {
writePoint(point, WriteOptions.DEFAULTS);
}
@Override
public void writePoint(@Nullable final Point point, @Nonnull final WriteOptions options) {
if (point == null) {
return;
}
writePoints(Collections.singletonList(point), options);
}
@Override
public void writePoints(@Nonnull final List points) {
writePoints(points, WriteOptions.DEFAULTS);
}
@Override
public void writePoints(@Nonnull final List points, @Nonnull final WriteOptions options) {
writeData(points, options);
}
@Nonnull
@Override
public Stream