Skip to content

Commit

Permalink
Global Unique Index (same entity save)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-sg-v committed Sep 26, 2024
1 parent 5926ab7 commit a432dc6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ private void validateUniqueness(long txId, long version, T entity) {
.toList();
for (Schema.Index index : indexes) {
Map<String, Object> entityIndexValues = buildIndexValues(index, entity);
for (InMemoryEntityLine line : entityLines.values()) {
entityLines.forEach((id, line) -> {
Columns columns = line.get(txId, version);
if (columns != null && entityIndexValues.equals(buildIndexValues(index, columns.toSchema(schema)))) {
if (columns != null && !id.equals(entity.getId())
&& entityIndexValues.equals(buildIndexValues(index, columns.toSchema(schema)))) {
throw new EntityAlreadyExistsException("Entity " + entity.getId() + " already exists");
}
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,10 +1355,12 @@ private void findInKeysViewFilteredAndOrdered(Set<IndexedEntity.Key> keys, boole

@Test
public void testUniqueIndex() {
UniqueProject ue1 = new UniqueProject(new UniqueProject.Id("id1"), "valuableName");
String verySameName = "valuableName";
UniqueProject ue1 = new UniqueProject(new UniqueProject.Id("id1"), verySameName, 1);
db.tx(() -> db.table(UniqueProject.class).save(ue1));
UniqueProject ue2 = new UniqueProject(new UniqueProject.Id("id2"), "valuableName");
assertThrows(EntityAlreadyExistsException.class, () -> db.tx(() -> db.table(UniqueProject.class).insert(ue2)));
db.tx(() -> db.table(UniqueProject.class).save(ue1).withVersion(2)); // no exception
UniqueProject ue2 = new UniqueProject(new UniqueProject.Id("id2"), verySameName, 1);
assertThrows(EntityAlreadyExistsException.class, () -> db.tx(() -> db.table(UniqueProject.class).save(ue2)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class UniqueProject implements Entity<UniqueProject> {
Id id;
@With
String name;
@With
int version;

@Value
public static class Id implements Entity.Id<UniqueProject> {
Expand Down

0 comments on commit a432dc6

Please sign in to comment.