use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class Sdk2CompatibleLegacyTranscoder method deserialize.
protected Object deserialize(byte[] in) {
Object rv = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
rv = is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
throw new CouchbaseException("Caught IOException decoding bytes of data", e);
} catch (ClassNotFoundException e) {
throw new CouchbaseException("Caught ClassNotFoundException decoding bytes of data", e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception ex) {
// ignored
}
try {
if (bis != null) {
bis.close();
}
} catch (Exception ex) {
// ignored
}
}
return rv;
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class CouchbaseArrayList method set.
@Override
public E set(int index, E element) {
// fail fast on negative values, as they are interpreted as "starting from the back of the array" otherwise
if (index < 0) {
throw new IndexOutOfBoundsException("Index: " + index);
}
String idx = "[" + index + "]";
for (int i = 0; i < arrayListOptions.casMismatchRetries(); i++) {
try {
LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
long returnCas = current.cas();
// this loop ensures we return exactly what we replaced
final E result = current.contentAs(0, entityTypeClass);
collection.mutateIn(id, Collections.singletonList(MutateInSpec.replace(idx, element)), arrayListOptions.mutateInOptions().cas(returnCas));
return result;
} catch (DocumentNotFoundException e) {
createEmptyList();
} catch (CasMismatchException ex) {
// will need to retry get-and-set
} catch (PathNotFoundException ex) {
throw new IndexOutOfBoundsException("Index: " + index);
}
}
throw new CouchbaseException("CouchbaseArrayList set failed", new RetryExhaustedException("Couldn't perform set in less than " + arrayListOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class ConnectionString method parseParams.
static Map<String, String> parseParams(final String input) {
try {
String[] parts = input.split("\\?");
Map<String, String> params = new HashMap<>();
if (parts.length > 1) {
String found = parts[1];
String[] exploded = found.split("&");
for (int i = 0; i < exploded.length; i++) {
String[] pair = exploded[i].split("=");
params.put(pair[0], pair[1]);
}
}
return params;
} catch (Exception ex) {
throw new CouchbaseException("Could not parse Params of connection string: " + input, ex);
}
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class ReplicaHelper method getAnyReplicaAsync.
/**
* @param clientContext (nullable)
* @param parentSpan (nullable)
* @param responseMapper converts the GetReplicaResponse to the client's native result type
*/
public static <R> CompletableFuture<R> getAnyReplicaAsync(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Duration timeout, final RetryStrategy retryStrategy, final Map<String, Object> clientContext, final RequestSpan parentSpan, final Function<GetReplicaResponse, R> responseMapper) {
RequestSpan getAnySpan = core.context().environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_GET_ANY_REPLICA, parentSpan);
CompletableFuture<List<CompletableFuture<R>>> listOfFutures = getAllReplicasAsync(core, collectionIdentifier, documentId, timeout, retryStrategy, clientContext, getAnySpan, responseMapper);
// Aggregating the futures here will discard the individual errors, which we don't need
CompletableFuture<R> anyReplicaFuture = new CompletableFuture<>();
listOfFutures.whenComplete((futures, throwable) -> {
if (throwable != null) {
anyReplicaFuture.completeExceptionally(throwable);
}
final AtomicBoolean successCompleted = new AtomicBoolean(false);
final AtomicInteger totalCompleted = new AtomicInteger(0);
final List<ErrorContext> nestedContexts = Collections.synchronizedList(new ArrayList<>());
futures.forEach(individual -> individual.whenComplete((result, error) -> {
int completed = totalCompleted.incrementAndGet();
if (error != null) {
if (error instanceof CompletionException && error.getCause() instanceof CouchbaseException) {
nestedContexts.add(((CouchbaseException) error.getCause()).context());
}
}
if (result != null && successCompleted.compareAndSet(false, true)) {
anyReplicaFuture.complete(result);
}
if (!successCompleted.get() && completed == futures.size()) {
anyReplicaFuture.completeExceptionally(new DocumentUnretrievableException(new AggregateErrorContext(nestedContexts)));
}
}));
});
return anyReplicaFuture.whenComplete((getReplicaResult, throwable) -> getAnySpan.end());
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class Bytes method readAllBytes.
public static byte[] readAllBytes(InputStream is) {
notNull(is, "input stream");
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[4 * 1024];
int len;
while ((len = is.read(buffer)) != -1) {
result.write(buffer, 0, len);
}
return result.toByteArray();
} catch (IOException e) {
throw new CouchbaseException("Failed to read bytes from stream.", e);
}
}
Aggregations