frostfs-sdk-java/client/src/main/java/info/frostfs/sdk/FrostFSClient.java
Bruk Ori e10610525e
Some checks failed
DCO / DCO (pull_request) Failing after 29s
[#1] Define SDK structure, add operations with container and object
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2024-07-24 15:09:09 +03:00

139 lines
4.8 KiB
Java

package info.frostfs.sdk;
import frostfs.session.Types;
import info.frostfs.sdk.dto.SessionToken;
import info.frostfs.sdk.dto.Version;
import info.frostfs.sdk.dto.container.Container;
import info.frostfs.sdk.dto.container.ContainerId;
import info.frostfs.sdk.dto.netmap.NetmapSnapshot;
import info.frostfs.sdk.dto.netmap.NodeInfo;
import info.frostfs.sdk.dto.object.ObjectFilter;
import info.frostfs.sdk.dto.object.ObjectFrostFS;
import info.frostfs.sdk.dto.object.ObjectHeader;
import info.frostfs.sdk.dto.object.ObjectId;
import info.frostfs.sdk.jdo.ClientEnvironment;
import info.frostfs.sdk.jdo.ClientSettings;
import info.frostfs.sdk.jdo.NetworkSettings;
import info.frostfs.sdk.jdo.PutObjectParameters;
import info.frostfs.sdk.services.*;
import info.frostfs.sdk.services.impl.*;
import io.grpc.Channel;
import java.util.List;
import static info.frostfs.sdk.tools.GrpcClient.initGrpcChannel;
import static java.util.Objects.isNull;
public class FrostFSClient implements ContainerClient, ObjectClient, NetmapClient, SessionClient, ToolsClient {
private static final String ERROR_CLIENT_OPTIONS_INIT = "Options must be initialized.";
private static final String ERROR_VERSION_SUPPORT_TEMPLATE = "FrostFS %s is not supported.";
private final ContainerClientImpl containerClientImpl;
private final NetmapClientImpl netmapClientImpl;
private final ObjectClientImpl objectClientImpl;
private final SessionClientImpl sessionClientImpl;
private final ObjectToolsImpl objectToolsImpl;
public FrostFSClient(ClientSettings clientSettings) {
if (isNull(clientSettings)) {
throw new IllegalArgumentException(ERROR_CLIENT_OPTIONS_INIT);
}
clientSettings.validate();
Channel channel = initGrpcChannel(clientSettings.getHost(), clientSettings.getCreds());
ClientEnvironment clientEnvironment =
new ClientEnvironment(clientSettings.getKey(), channel, new Version(), this);
this.containerClientImpl = new ContainerClientImpl(clientEnvironment);
this.netmapClientImpl = new NetmapClientImpl(clientEnvironment);
this.sessionClientImpl = new SessionClientImpl(clientEnvironment);
this.objectClientImpl = new ObjectClientImpl(clientEnvironment);
this.objectToolsImpl = new ObjectToolsImpl(clientEnvironment);
checkFrostFsVersionSupport(clientEnvironment.getVersion());
}
private void checkFrostFsVersionSupport(Version version) {
var localNodeInfo = netmapClientImpl.getLocalNodeInfo();
if (!localNodeInfo.getVersion().isSupported(version)) {
throw new IllegalArgumentException(
String.format(ERROR_VERSION_SUPPORT_TEMPLATE, localNodeInfo.getVersion())
);
}
}
@Override
public Container getContainer(ContainerId cid) {
return containerClientImpl.getContainer(cid);
}
@Override
public List<ContainerId> listContainers() {
return containerClientImpl.listContainers();
}
@Override
public ContainerId createContainer(Container container) {
return containerClientImpl.createContainer(container);
}
@Override
public void deleteContainer(ContainerId cid) {
containerClientImpl.deleteContainer(cid);
}
@Override
public ObjectHeader getObjectHead(ContainerId containerId, ObjectId objectId) {
return objectClientImpl.getObjectHead(containerId, objectId);
}
@Override
public ObjectFrostFS getObject(ContainerId containerId, ObjectId objectId) {
return objectClientImpl.getObject(containerId, objectId);
}
@Override
public ObjectId putObject(PutObjectParameters parameters) {
return objectClientImpl.putObject(parameters);
}
@Override
public void deleteObject(ContainerId containerId, ObjectId objectId) {
objectClientImpl.deleteObject(containerId, objectId);
}
@Override
public Iterable<ObjectId> searchObjects(ContainerId cid, ObjectFilter... filters) {
return objectClientImpl.searchObjects(cid, filters);
}
@Override
public NetmapSnapshot getNetmapSnapshot() {
return netmapClientImpl.getNetmapSnapshot();
}
@Override
public NodeInfo getLocalNodeInfo() {
return netmapClientImpl.getLocalNodeInfo();
}
@Override
public NetworkSettings getNetworkSettings() {
return netmapClientImpl.getNetworkSettings();
}
@Override
public SessionToken createSession(long expiration) {
return sessionClientImpl.createSession(expiration);
}
public Types.SessionToken createSessionInternal(long expiration) {
return sessionClientImpl.createSessionInternal(expiration);
}
@Override
public ObjectId calculateObjectId(ObjectHeader header) {
return objectToolsImpl.calculateObjectId(header);
}
}