use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class RemoteNotificationFeedManager method listFeeds.
@Override
public List<NotificationFeedInfo> listFeeds(NamespaceId namespace) throws NotificationFeedException {
String path = String.format("namespaces/%s/feeds", namespace.getNamespace());
HttpResponse response = execute(remoteClient.requestBuilder(HttpMethod.GET, path).build());
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
ObjectResponse<List<NotificationFeedInfo>> r = ObjectResponse.fromJsonBody(response, new TypeToken<List<NotificationFeedInfo>>() {
}.getType());
return r.getResponseObject();
}
throw new NotificationFeedException("Cannot list notification feeds. Reason: " + response);
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class ObjectStoreDatasetTest method testPairStore.
@Test
public void testPairStore() throws Exception {
DatasetId pairs = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("pairs");
createObjectStoreInstance(pairs, new TypeToken<ImmutablePair<Integer, String>>() {
}.getType());
final ObjectStoreDataset<ImmutablePair<Integer, String>> pairStore = dsFrameworkUtil.getInstance(pairs);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor(pairStore);
final ImmutablePair<Integer, String> pair = new ImmutablePair<>(1, "second");
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
pairStore.write(a, pair);
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
ImmutablePair<Integer, String> result = pairStore.read(a);
Assert.assertEquals(pair, result);
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
deleteAndVerify(pairStore, a);
}
});
dsFrameworkUtil.deleteInstance(pairs);
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class ObjectStoreDatasetTest method testInstantiateWrongClass.
@Test
public void testInstantiateWrongClass() throws Exception {
DatasetId pairs = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("pairs");
createObjectStoreInstance(pairs, new TypeToken<ImmutablePair<Integer, String>>() {
}.getType());
// note: due to type erasure, this succeeds
final ObjectStoreDataset<Custom> store = dsFrameworkUtil.getInstance(pairs);
TransactionExecutor storeTxnl = dsFrameworkUtil.newTransactionExecutor(store);
// but now it must fail with incompatible type
try {
storeTxnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Custom custom = new Custom(42, Lists.newArrayList("one", "two"));
store.write(a, custom);
}
});
Assert.fail("write should have failed with incompatible type");
} catch (TransactionFailureException e) {
// expected
}
// write a correct object to the pair store
final ObjectStoreDataset<ImmutablePair<Integer, String>> pairStore = dsFrameworkUtil.getInstance(pairs);
TransactionExecutor pairStoreTxnl = dsFrameworkUtil.newTransactionExecutor(pairStore);
final ImmutablePair<Integer, String> pair = new ImmutablePair<>(1, "second");
pairStoreTxnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// should succeed
pairStore.write(a, pair);
}
});
pairStoreTxnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
ImmutablePair<Integer, String> actualPair = pairStore.read(a);
Assert.assertEquals(pair, actualPair);
}
});
// now try to read that as a custom object, should fail with class cast
try {
storeTxnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Custom custom = store.read(a);
Preconditions.checkNotNull(custom);
}
});
Assert.fail("write should have failed with class cast exception");
} catch (TransactionFailureException e) {
// expected
}
pairStoreTxnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
deleteAndVerify(pairStore, a);
}
});
dsFrameworkUtil.deleteInstance(pairs);
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class ObjectStoreDatasetTest method testBatchCustomList.
@Test
public void testBatchCustomList() throws Exception {
DatasetId customlist = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("customlist");
createObjectStoreInstance(customlist, new TypeToken<List<Custom>>() {
}.getType());
final ObjectStoreDataset<List<Custom>> customStore = dsFrameworkUtil.getInstance(customlist);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor(customStore);
final SortedSet<Long> keysWritten = Sets.newTreeSet();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
List<Custom> customList1 = Arrays.asList(new Custom(1, Lists.newArrayList("one", "ONE")), new Custom(2, Lists.newArrayList("two", "TWO")));
Random rand = new Random(100);
long key1 = rand.nextLong();
keysWritten.add(key1);
customStore.write(Bytes.toBytes(key1), customList1);
List<Custom> customList2 = Arrays.asList(new Custom(3, Lists.newArrayList("three", "THREE")), new Custom(4, Lists.newArrayList("four", "FOUR")));
long key2 = rand.nextLong();
keysWritten.add(key2);
customStore.write(Bytes.toBytes(key2), customList2);
}
});
final SortedSet<Long> keysWrittenCopy = ImmutableSortedSet.copyOf(keysWritten);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// get the splits for the table
List<Split> splits = customStore.getSplits();
for (Split split : splits) {
SplitReader<byte[], List<Custom>> reader = customStore.createSplitReader(split);
reader.initialize(split);
while (reader.nextKeyValue()) {
byte[] key = reader.getCurrentKey();
Assert.assertTrue(keysWritten.remove(Bytes.toLong(key)));
}
}
// verify all keys have been read
if (!keysWritten.isEmpty()) {
System.out.println("Remaining [" + keysWritten.size() + "]: " + keysWritten);
}
Assert.assertTrue(keysWritten.isEmpty());
}
});
deleteAndVerifyInBatch(customStore, txnl, keysWrittenCopy);
dsFrameworkUtil.deleteInstance(customlist);
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class ExploreExecutorHttpHandler method doDropPartition.
private void doDropPartition(HttpRequest request, HttpResponder responder, DatasetId datasetId) {
Dataset dataset;
try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
dataset = datasetInstantiator.getDataset(datasetId);
if (dataset == null) {
responder.sendString(HttpResponseStatus.NOT_FOUND, "Cannot load dataset " + datasetId);
return;
}
} catch (IOException e) {
String classNotFoundMessage = isClassNotFoundException(e);
if (classNotFoundMessage != null) {
JsonObject json = new JsonObject();
json.addProperty("handle", QueryHandle.NO_OP.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
return;
}
LOG.error("Exception instantiating dataset {}.", datasetId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Exception instantiating dataset " + datasetId);
return;
}
try {
if (!(dataset instanceof PartitionedFileSet)) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "not a partitioned dataset.");
return;
}
Partitioning partitioning = ((PartitionedFileSet) dataset).getPartitioning();
Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()));
Map<String, String> properties = GSON.fromJson(reader, new TypeToken<Map<String, String>>() {
}.getType());
PartitionKey partitionKey;
try {
partitionKey = PartitionedFileSetArguments.getOutputPartitionKey(properties, partitioning);
} catch (Exception e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "invalid partition key: " + e.getMessage());
return;
}
if (partitionKey == null) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "no partition key was given.");
return;
}
QueryHandle handle = exploreTableManager.dropPartition(datasetId, properties, partitionKey);
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
} catch (Throwable e) {
LOG.error("Got exception:", e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
Aggregations