Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Meter;
import io.greptime.common.Display;
import io.greptime.common.Endpoint;
import io.greptime.common.Keys;
import io.greptime.common.Lifecycle;
import io.greptime.common.signal.SignalHandlersLoader;
import io.greptime.common.util.MetricsUtil;
import io.greptime.common.util.ServiceLoader;
import io.greptime.common.util.Strings;
import io.greptime.models.Err;
import io.greptime.models.Result;
import io.greptime.models.Table;
import io.greptime.models.WriteOk;
import io.greptime.options.GreptimeOptions;
import io.greptime.options.RouterOptions;
import io.greptime.options.WriteOptions;
import io.greptime.rpc.Context;
import io.greptime.rpc.RpcClient;
import io.greptime.rpc.RpcFactoryProvider;
import io.greptime.rpc.RpcOptions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The GreptimeDB client.
*/
public class GreptimeDB implements Write, WriteObject, Lifecycle, Display {
private static final Logger LOG = LoggerFactory.getLogger(GreptimeDB.class);
private static final Map INSTANCES = new ConcurrentHashMap<>();
private static final AtomicInteger ID = new AtomicInteger(0);
private static final String VERSION = Util.clientVersion();
private static final String NODE_ID = UUID.randomUUID().toString();
private static final PojoObjectMapper POJO_OBJECT_MAPPER = getDefaultPojoObjectMapper();
private final int id;
private final AtomicBoolean started = new AtomicBoolean(false);
private GreptimeOptions opts;
private RouterClient routerClient;
private WriteClient writeClient;
/**
* Returns all instances of {@link GreptimeDB}.
*/
public static List instances() {
return new ArrayList<>(INSTANCES.values());
}
public static GreptimeDB create(GreptimeOptions opts) {
GreptimeDB greptimeDB = new GreptimeDB();
if (!greptimeDB.init(opts)) {
throw new RuntimeException("Failed to start the GreptimeDB client");
}
LOG.info("GreptimeDB client started: {}", greptimeDB);
return greptimeDB;
}
private GreptimeDB() {
this.id = ID.incrementAndGet();
}
@Override
public boolean init(GreptimeOptions opts) {
if (!this.started.compareAndSet(false, true)) {
throw new IllegalStateException("GreptimeDB client has started");
}
this.opts = GreptimeOptions.checkSelf(opts).copy();
if (Strings.isBlank(this.opts.getDatabase())) {
LOG.warn("The `database` is not specified, use default (catalog-database): greptime-public");
}
this.routerClient = makeRouteClient(opts);
this.writeClient = makeWriteClient(opts, this.routerClient);
INSTANCES.put(this.id, this);
Util.scheduleDisplaySelf(this, new LogPrinter(LOG));
return true;
}
@Override
public void shutdownGracefully() {
if (!this.started.compareAndSet(true, false)) {
return;
}
if (this.writeClient != null) {
this.writeClient.shutdownGracefully();
}
if (this.routerClient != null) {
this.routerClient.shutdownGracefully();
}
INSTANCES.remove(this.id);
}
@Override
public void ensureInitialized() {
if (this.started.get() && INSTANCES.containsKey(this.id)) {
return;
}
throw new IllegalStateException(String.format("Client(%d) is not started", this.id));
}
@Override
public CompletableFuture> writeObjects(
Collection> objects, WriteOp writeOp, Context ctx) {
List
rows = new ArrayList<>(objects.size());
for (List> pojo : objects) {
rows.add(POJO_OBJECT_MAPPER.mapToTable(pojo));
}
return write(rows, writeOp, ctx);
}
@Override
public StreamWriter, WriteOk> objectsStreamWriter(int maxPointsPerSecond, Context ctx) {
StreamWriter