Skip to content

Commit

Permalink
auto update lastUpdatedTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Mar 27, 2024
1 parent c0a5c31 commit 5884a20
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -58,15 +59,20 @@ public class ContiguousIdBlock implements Comparable<ContiguousIdBlock> {
@Column(nullable = false, length = 255)
private String applicationInstanceId;

@Column(nullable = false)
private long firstValue;

@Column(nullable = false)
private long lastValue;

@Column(nullable = false)
private long lastCommitted;

@Column(nullable = false)
private boolean reserved;

private LocalDateTime createdTimestamp;
@Column(nullable = false)
private LocalDateTime lastUpdatedTimestamp;

// Create / update dates

Expand All @@ -81,7 +87,7 @@ public ContiguousIdBlock(String categoryId, String applicationInstanceId, long f
this.lastValue = firstValue + size - 1;
this.lastCommitted = firstValue - 1;
this.reserved = true;
this.createdTimestamp = LocalDateTime.now();
this.lastUpdatedTimestamp = LocalDateTime.now();
}

/**
Expand Down Expand Up @@ -186,8 +192,17 @@ public boolean isNotFull() {
return lastCommitted != lastValue;
}

public LocalDateTime getLastUpdatedTimestamp() {
return lastUpdatedTimestamp;
}

@Override
public int compareTo(ContiguousIdBlock contiguousIdBlock) {
return Long.compare(firstValue, contiguousIdBlock.getFirstValue());
}

@PreUpdate
protected void onUpdate() {
this.lastUpdatedTimestamp = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -266,4 +267,64 @@ public void testBlocksWithDuplicateCategoryAndFirstValue() {
assertTrue(exception.getCause() instanceof ConstraintViolationException);
}

@Test
public void testLastUpdateTimeStampAutoUpdate() {
// block saved with initial value
ContiguousIdBlock block = new ContiguousIdBlock(CATEGORY_ID, INSTANCE_ID, 100, 1000);
repository.save(block);
entityManager.flush();

// assert block values
ContiguousIdBlock blockInDB = repository.findById(1L).get();
assertEquals(1L, blockInDB.getId());
assertEquals(CATEGORY_ID, blockInDB.getCategoryId());
assertEquals(INSTANCE_ID, blockInDB.getApplicationInstanceId());
assertEquals(100, blockInDB.getFirstValue());
assertEquals(1099, blockInDB.getLastValue());
assertEquals(99, blockInDB.getLastCommitted());

LocalDateTime blockInsertTime = blockInDB.getLastUpdatedTimestamp();

// block updated with last committed 100
block.setLastCommitted(100);
repository.save(block);
entityManager.flush();
blockInDB = repository.findById(1L).get();
assertEquals(100, blockInDB.getLastCommitted());

LocalDateTime blockLastCommittedUpdateTime = blockInDB.getLastUpdatedTimestamp();

// block updated with new instance id
block.setApplicationInstanceId(INSTANCE_ID_2);
repository.save(block);
entityManager.flush();
blockInDB = repository.findById(1L).get();
assertEquals(INSTANCE_ID_2, blockInDB.getApplicationInstanceId());

LocalDateTime blockApplicationInstanceUpdateTime = blockInDB.getLastUpdatedTimestamp();

// block updated - release reserved
block.releaseReserved();
repository.save(block);
entityManager.flush();
blockInDB = repository.findById(1L).get();
assertTrue(blockInDB.isNotReserved());

LocalDateTime blockReleaseAsReservedUpdateTime = blockInDB.getLastUpdatedTimestamp();

// block update - mark as reserved
block.markAsReserved();
repository.save(block);
entityManager.flush();
blockInDB = repository.findById(1L).get();
assertTrue(blockInDB.isReserved());

LocalDateTime blockMarkAsReservedUpdateTime = blockInDB.getLastUpdatedTimestamp();

assertTrue(blockInsertTime.isBefore(blockLastCommittedUpdateTime));
assertTrue(blockLastCommittedUpdateTime.isBefore(blockApplicationInstanceUpdateTime));
assertTrue(blockApplicationInstanceUpdateTime.isBefore(blockReleaseAsReservedUpdateTime));
assertTrue(blockReleaseAsReservedUpdateTime.isBefore(blockMarkAsReservedUpdateTime));
}

}

0 comments on commit 5884a20

Please sign in to comment.