org.dst.client.DstStringProxy Maven / Gradle / Ivy
The newest version!
package org.dst.client;
import org.dst.common.exception.DstException;
import org.dst.common.exception.KeyNotFoundException;
import org.dst.common.protobuf.generated.CommonProtocol;
import org.dst.common.protobuf.generated.StringProtocol;
import org.dst.server.service.DstStringService;
public class DstStringProxy {
private DstStringService service;
public DstStringProxy(DstStringService service) {
this.service = service;
}
public void put(String key, String value) {
StringProtocol.PutRequest request =
StringProtocol.PutRequest.newBuilder()
.setKey(key)
.setValue(value)
.build();
StringProtocol.PutResponse response = service.put(request);
if (response.getStatus() != CommonProtocol.Status.OK) {
throw new DstException(String.format("Error code is %d", response.getStatus().getNumber()));
}
}
public String get(String key) throws DstException {
StringProtocol.GetRequest request =
StringProtocol.GetRequest.newBuilder()
.setKey(key)
.build();
StringProtocol.GetResponse response = service.get(request);
if (response.getStatus() == CommonProtocol.Status.KEY_NOT_FOUND) {
throw new KeyNotFoundException(key);
} else if (response.getStatus() != CommonProtocol.Status.OK) {
throw new DstException(String.format("Error code is %d", response.getStatus().getNumber()));
}
return response.getValue();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy