org.springframework.data.redis.connection.stream.StreamOffset Java Examples

The following examples show how to use org.springframework.data.redis.connection.stream.StreamOffset. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SyncStreamApiTests.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void basics() {

	// XADD with fixed id
	RecordId fixedId1 = streamOps.add(SensorData.RECORD_1234_0);
	assertThat(fixedId1).isEqualTo(SensorData.RECORD_1234_0.getId());

	RecordId fixedId2 = streamOps.add(SensorData.RECORD_1234_1);
	assertThat(fixedId2).isEqualTo(SensorData.RECORD_1234_1.getId());

	// XLEN
	assertThat(streamOps.size(SensorData.KEY)).isEqualTo(2L);

	// XADD errors when timestamp is less then last inserted
	assertThatExceptionOfType(RedisSystemException.class).isThrownBy(() -> {
		streamOps.add(SensorData.create("1234", "19.8", "invalid").withId(RecordId.of("0-0")));
	}).withMessageContaining("ID specified");

	// XADD with autogenerated id
	RecordId autogeneratedId = streamOps.add(SensorData.create("1234", "19.8", null));

	assertThat(autogeneratedId.getValue()).endsWith("-0");
	assertThat(streamOps.size(SensorData.KEY)).isEqualTo(3L);

	// XREAD from start
	List<MapRecord<String, String, String>> fromStart = streamOps.read(fromStart(SensorData.KEY));
	assertThat(fromStart).hasSize(3).extracting(MapRecord::getId).containsExactly(fixedId1, fixedId2, autogeneratedId);

	// XREAD resume after
	List<MapRecord<String, String, String>> fromOffset = streamOps.read(StreamOffset.create(SensorData.KEY, ReadOffset.from(fixedId2)));
	assertThat(fromOffset).hasSize(1).extracting(MapRecord::getId).containsExactly(autogeneratedId);
}
 
Example #2
Source File: TracingRedisConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public List<ByteRecord> xRead(StreamReadOptions readOptions, StreamOffset<byte[]>... streams) {
  return helper.doInScope(RedisCommand.XREAD, () -> connection.xRead(readOptions, streams));
}
 
Example #3
Source File: TracingRedisConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public List<ByteRecord> xReadGroup(Consumer consumer, StreamReadOptions readOptions,
    StreamOffset<byte[]>... streams) {
  return helper.doInScope(RedisCommand.XREADGROUP,
      () -> connection.xReadGroup(consumer, readOptions, streams));
}
 
Example #4
Source File: ReactiveStreamApiTests.java    From spring-data-examples with Apache License 2.0 4 votes vote down vote up
@Test
public void basics() {

	// XADD with fixed id
	streamOps.add(SensorData.RECORD_1234_0)
			.as(StepVerifier::create)
			.expectNext(SensorData.RECORD_1234_0.getId()).verifyComplete();

	streamOps.add(SensorData.RECORD_1234_1)
			.as(StepVerifier::create)
			.expectNext(SensorData.RECORD_1234_1.getId()).verifyComplete();

	// XLEN
	streamOps.size(SensorData.KEY)
			.as(StepVerifier::create)
			.expectNext(2L).verifyComplete();

	// XADD errors when timestamp is less then last inserted
	streamOps.add(SensorData.create("1234", "19.8", "invalid").withId(RecordId.of("0-0")))
			.as(StepVerifier::create)
			.verifyError(RedisSystemException.class);

	// XADD with autogenerated id
	streamOps.add(SensorData.create("1234", "19.8", null))
			.as(StepVerifier::create)
			.consumeNextWith(autogeneratedId -> autogeneratedId.getValue().endsWith("-0")).verifyComplete();

	streamOps.size(SensorData.KEY)
			.as(StepVerifier::create)
			.expectNext(3L).verifyComplete();

	// XREAD from start
	streamOps.read(fromStart(SensorData.KEY))
			.map(MapRecord::getId)
			.as(StepVerifier::create)
			.expectNext(SensorData.RECORD_1234_0.getId(), SensorData.RECORD_1234_1.getId())
			.expectNextCount(1).verifyComplete();

	// XREAD resume after
	streamOps.read(StreamOffset.create(SensorData.KEY, ReadOffset.from(SensorData.RECORD_1234_1.getId())))
			.as(StepVerifier::create)
			.expectNextCount(1).verifyComplete();
}