use of io.cdap.cdap.api.common.Bytes in project cdap by caskdata.
the class MessagingHttpServiceTest method testLargePublish.
@Test
public void testLargePublish() throws IOException, TopicAlreadyExistsException, TopicNotFoundException, UnauthorizedException {
// A 5MB message, which is larger than the 1MB buffer.
String message = Strings.repeat("01234", 1024 * 1024);
TopicId topicId = new NamespaceId("ns1").topic("testLargePublish");
client.createTopic(new TopicMetadata(topicId));
StoreRequest request = StoreRequestBuilder.of(topicId).addPayload(message).build();
client.publish(request);
// Read it back
List<RawMessage> messages = new ArrayList<>();
try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).setLimit(10).fetch()) {
Iterators.addAll(messages, iterator);
}
Assert.assertEquals(1, messages.size());
Assert.assertEquals(Collections.singletonList(message), messages.stream().map(RawMessage::getPayload).map(Bytes::toString).collect(Collectors.toList()));
}
use of io.cdap.cdap.api.common.Bytes in project cdap by caskdata.
the class MessagingHttpServiceTest method testReuseRequest.
@Test
public void testReuseRequest() throws IOException, TopicAlreadyExistsException, TopicNotFoundException, UnauthorizedException {
// This test a StoreRequest object can be reused.
// This test is to verify storing transaction messages to the payload table
TopicId topicId = new NamespaceId("ns1").topic("testReuseRequest");
client.createTopic(new TopicMetadata(topicId));
StoreRequest request = StoreRequestBuilder.of(topicId).addPayload("m1").addPayload("m2").build();
// Publish the request twice
client.publish(request);
client.publish(request);
// Expects four messages
List<RawMessage> messages = new ArrayList<>();
try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).setLimit(10).fetch()) {
Iterators.addAll(messages, iterator);
}
Assert.assertEquals(4, messages.size());
List<String> expected = Arrays.asList("m1", "m2", "m1", "m2");
Assert.assertEquals(expected, messages.stream().map(RawMessage::getPayload).map(Bytes::toString).collect(Collectors.toList()));
}
use of io.cdap.cdap.api.common.Bytes in project cdap by caskdata.
the class DraftStore method doSort.
/**
* Helper method to apply the sorting onto a list of rows. Sorting needs to take place at the store-level so it can
* leverage the StructuredRow to enable sorting on any field.
*
* @param rows list of {@link StructuredRow} to be sorted
* @param sortRequest {@link SortRequest} describing the sort to be performed
* @return a sorted list of {@link StructuredRow}
*/
private List<StructuredRow> doSort(List<StructuredRow> rows, @Nullable SortRequest sortRequest) {
if (sortRequest == null) {
return rows;
}
String sortField = sortRequest.getFieldName();
FieldType field = TABLE_SPEC.getFieldTypes().stream().filter(f -> f.getName().equals(sortField)).findFirst().orElse(null);
if (field == null) {
throw new IllegalArgumentException(String.format("Invalid value '%s' for sortBy. This field does not exist in the Drafts table.", sortField));
}
FieldType.Type fieldType = field.getType();
Comparator<StructuredRow> comparator;
switch(fieldType) {
case STRING:
comparator = Comparator.<StructuredRow, String>comparing(o -> o.getString(sortField));
break;
case INTEGER:
comparator = Comparator.<StructuredRow, Integer>comparing(o -> o.getInteger(sortField));
break;
case LONG:
comparator = Comparator.<StructuredRow, Long>comparing(o -> o.getLong(sortField));
break;
case FLOAT:
comparator = Comparator.<StructuredRow, Float>comparing(o -> o.getFloat(sortField));
break;
case DOUBLE:
comparator = Comparator.<StructuredRow, Double>comparing(o -> o.getDouble(sortField));
break;
case BYTES:
comparator = Comparator.comparing(o -> o.getBytes(sortField), Bytes.BYTES_COMPARATOR);
break;
default:
throw new UnsupportedOperationException(String.format("Cannot sort field '%s' because type '%s' is not supported.", sortField, fieldType.toString()));
}
if (sortRequest.getOrder() != SortRequest.SortOrder.ASC) {
comparator = comparator.reversed();
}
rows.sort(comparator);
return rows;
}
Aggregations