use of com.github.ambry.commons.ReadableStreamChannelInputStream in project ambry by linkedin.
the class RouterStore method readAccountMetadataFromBlobID.
/**
* Fetch the {@link Account} metadata from the given blob id.
* @param blobID The blobID to fetch {@link Account} metadata from.
* @return {@link Account} metadata in a map, and null when there is any error.
*/
Map<String, String> readAccountMetadataFromBlobID(String blobID) {
long startTimeMs = System.currentTimeMillis();
Future<GetBlobResult> resultF = router.get().getBlob(blobID, new GetBlobOptionsBuilder().build());
try {
GetBlobResult result = resultF.get();
accountServiceMetrics.accountFetchFromAmbryTimeInMs.update(System.currentTimeMillis() - startTimeMs);
int blobSize = (int) result.getBlobInfo().getBlobProperties().getBlobSize();
InputStream input = new ReadableStreamChannelInputStream(result.getBlobDataChannel());
byte[] bytes = Utils.readBytesFromStream(input, blobSize);
JSONObject object = new JSONObject(new String(bytes, Charsets.UTF_8));
Map<String, String> map = new HashMap<>();
object.keySet().forEach(key -> map.put(key, object.getString(key)));
return map;
} catch (Exception e) {
logger.error("Failed to read account metadata from blob id={}", blobID, e);
accountServiceMetrics.accountFetchFromAmbryServerErrorCount.inc();
}
return null;
}
use of com.github.ambry.commons.ReadableStreamChannelInputStream in project ambry by linkedin.
the class MockRouter method putBlob.
@Override
public Future<String> putBlob(BlobProperties blobProperties, byte[] userMetadata, ReadableStreamChannel channel, PutBlobOptions options, Callback<String> callback, QuotaChargeCallback quotaChargeCallback) {
lock.lock();
try {
FutureResult<String> future = new FutureResult<>();
long size = channel.getSize();
try {
InputStream input = new ReadableStreamChannelInputStream(channel);
byte[] bytes = Utils.readBytesFromStream(input, (int) size);
BlobInfoAndData blob = new BlobInfoAndData(new BlobInfo(blobProperties, userMetadata), bytes);
String id;
do {
id = TestUtils.getRandomString(10);
} while (allBlobs.putIfAbsent(id, blob) != null);
future.done(id, null);
if (callback != null) {
callback.onCompletion(id, null);
}
return future;
} catch (Exception e) {
logger.error("Failed to put blob", e);
future.done(null, e);
if (callback != null) {
callback.onCompletion(null, e);
}
return future;
}
} finally {
lock.unlock();
}
}
Aggregations