/** * Updates timestamp for a received event * * @param ts Object that implements Timestamped interface * @return updated timestamp */ public long update(HybridTimestamp ts) { return update(ts.getLogicalTime(), ts.getLogicalCounter()); }
/** * Updates timestamp for a received event * * @param ts Object that implements Timestamped interface * @return updated timestamp */ public long update(HybridTimestamp ts) { return update(ts.getLogicalTime(), ts.getLogicalCounter()); }
@Test public void testTimestamp() { HybridTimestamp timestamp = new HybridTimestamp(physicalTimeProvider); timestamp.update(); timestamp.update(); assertTrue(timestamp.getLogicalCounter() > 0); HybridTimestamp timestamp1 = new HybridTimestamp(physicalTimeProvider, timestamp.getLogicalTime(), timestamp.getLogicalCounter()); assertEquals(timestamp1.getLogicalCounter(), timestamp.getLogicalCounter()); }
@Test @SneakyThrows public void query() { HybridTimestamp timestamp = new HybridTimestamp(timeProvider); timestamp.update(); NameProtocolTest.TestModel model = new NameProtocolTest.TestModel(repository, UUID.randomUUID()); ChangeDescription changeDescription = new ChangeDescription(model.getId(), "Description"); repository.publish(changeDescription).get(); Collection<TestModel> models = ModelCollectionQuery.query(repository, DescriptionProtocol.described("Description", TestModel::lookup)); assertEquals(models.size(), 1); assertTrue(models.stream().anyMatch(m -> m.description().contentEquals("Description"))); } }
@Test @SneakyThrows public void query() { HybridTimestamp timestamp = new HybridTimestamp(timeProvider); timestamp.update(); TestModel model = new TestModel(repository, UUID.randomUUID()); Rename rename = new Rename(model.getId(), "Name"); repository.publish(rename).get(); Collection<TestModel> models = ModelCollectionQuery .query(repository, NameProtocol.named("Name", TestModel::lookup)); assertEquals(models.size(), 1); assertTrue(models.stream().anyMatch(m -> m.name().contentEquals("Name"))); } }
@Test @SneakyThrows public void layout() { Layout<HybridTimestamp> layout = Layout.forClass(HybridTimestamp.class); List<Property<HybridTimestamp>> properties = layout.getProperties(); assertEquals(properties.size(), 2); assertTrue(properties.stream().anyMatch(p -> p.getName().contentEquals("logicalTime"))); assertTrue(properties.stream().anyMatch(p -> p.getName().contentEquals("logicalCounter"))); HybridTimestamp timestamp = new HybridTimestamp(physicalTimeProvider); timestamp.update(); BinarySerialization serialization = BinarySerialization.getInstance(); ObjectSerializer<HybridTimestamp> serializer = serialization.getSerializer(HybridTimestamp.class); ObjectDeserializer<HybridTimestamp> deserializer = serialization.getDeserializer(HybridTimestamp.class); ByteBuffer buffer = serializer.serialize(timestamp); buffer.rewind(); HybridTimestamp timestamp1 = deserializer.deserialize(buffer); assertEquals(timestamp1.compareTo(timestamp), 0); }
timestamp.update(); assertEquals(ts, timestamp.getLogicalTime()); assertEquals(0, timestamp.getLogicalCounter()); timestamp.update(); assertEquals(ts, timestamp.getLogicalTime()); assertEquals(1, timestamp.getLogicalCounter()); physicalTimeProvider.setPhysicalTime(ts); ts_ = timestamp.getLogicalTime(); timestamp.update(); assertEquals(ts_, timestamp.getLogicalTime()); assertEquals(2, timestamp.getLogicalCounter()); timestamp.update(); assertEquals(ts, timestamp.getLogicalTime()); assertEquals(0, timestamp.getLogicalCounter()); timestamp.update((long) 1 << 32 | 2, 3); assertEquals(ts, timestamp.getLogicalTime()); assertEquals(0, timestamp.getLogicalCounter()); timestamp.update((long) 1 << 32 | 2, 3); assertEquals(ts, timestamp.getLogicalTime()); assertEquals(1, timestamp.getLogicalCounter()); timestamp.update((long) 3 << 32 | 0, 1); assertEquals(ts, timestamp.getLogicalTime()); assertEquals(2, timestamp.getLogicalCounter());
@Test @SneakyThrows public void renaming() { HybridTimestamp timestamp = new HybridTimestamp(timeProvider); timestamp.update(); TestModel model = new TestModel(repository, UUID.randomUUID()); Rename rename = new Rename(model.getId(), "Name #1"); repository.publish(rename).get(); assertEquals(model.name(), "Name #1"); Rename renameBefore = Rename.builder().id(model.getId()).name("Name #0").timestamp(timestamp).build(); assertTrue(renameBefore.timestamp().compareTo(rename.timestamp()) < 0); repository.publish(renameBefore).get(); assertEquals(model.name(), "Name #1"); // earlier change shouldn't affect the name rename = new Rename(model.getId(), "Name #2"); repository.publish(rename).get(); assertEquals(model.name(), "Name #2"); }
@Test @SneakyThrows public void changingDescription() { HybridTimestamp timestamp = new HybridTimestamp(timeProvider); timestamp.update(); TestModel model = new TestModel(repository, UUID.randomUUID()); ChangeDescription changeDescription = new ChangeDescription(model.getId(), "Description #1"); repository.publish(changeDescription).get(); assertEquals(model.description(), "Description #1"); ChangeDescription changeBefore = ChangeDescription.builder() .id(model.getId()).description("Description #0") .timestamp(timestamp).build(); assertTrue(changeBefore.timestamp().compareTo(changeDescription.timestamp()) < 0); repository.publish(changeBefore).get(); assertEquals(model.description(), "Description #1"); // earlier change shouldn't affect the description changeDescription = new ChangeDescription(model.getId(), "Description #2"); repository.publish(changeDescription).get(); assertEquals(model.description(), "Description #2"); }
@Test @SneakyThrows public void serializableComparable() { IndexedCollection<EntityHandle<Car>> collection = new ConcurrentIndexedCollection<>(); NavigableIndex index = onAttribute(Car.TIMESTAMP); index.clear(noQueryOptions()); collection.addIndex(index); Car car1 = CarFactory.createCar(1); Car car2 = CarFactory.createCar(2); NTPServerTimeProvider ntpServerTimeProvider = new NTPServerTimeProvider(); ntpServerTimeProvider.startAsync().awaitRunning(); HybridTimestamp ts1 = new HybridTimestamp(ntpServerTimeProvider); HybridTimestamp ts2 = ts1.clone(); Thread.sleep(1000); ts2.update(); assertTrue(ts2.compareTo(ts1) > 0); assertTrue(ts2.getSerializableComparable().compareTo(ts1.getSerializableComparable()) > 0); car1.timestamp(ts1); car2.timestamp(ts2); collection.add(new ResolvedEntityHandle<>(car1)); collection.add(new ResolvedEntityHandle<>(car2)); try (ResultSet<EntityHandle<Car>> resultSet = collection.retrieve(greaterThan(Car.TIMESTAMP, ts1))) { assertEquals(resultSet.size(), 1); assertEquals(resultSet.uniqueResult().get().getModel(), "Taurus"); } index.clear(noQueryOptions()); ntpServerTimeProvider.stopAsync().awaitTerminated(); }