Skip to content

Commit

Permalink
getHIstory gives exception now if the object is unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed May 10, 2024
1 parent 2c0f061 commit 68fdd63
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public Stream<TransactionReference> getHistory(StorageReference object) throws U
if (history != null)
return Stream.of(history);
else
return Stream.empty();
//throw new UnknownReferenceException(object); // TODO
throw new UnknownReferenceException(object);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface Store<S extends Store<S, T>, T extends StoreTransaction<S, T>>
* that can be used to reconstruct the current values of its fields.
*
* @param object the reference of the object
* @return the history. Yields an empty stream if there is no history for {@code object}
* @return the history
* @throws StoreException if the store is not able to perform the operation
*/
Stream<TransactionReference> getHistory(StorageReference object) throws UnknownReferenceException, StoreException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,12 +1271,17 @@ private void expandHistory(TransactionReference reference, TransactionResponseWi
* @return the simplified history, with {@code added} in front followed by a subset of the old history
*/
private Stream<TransactionReference> simplifiedHistory(StorageReference object, TransactionReference added, Stream<Update> addedUpdates) throws StoreException {
// if the object has been created at the added transaction, that is its history
if (object.getTransaction().equals(added))
return Stream.of(added);

Stream<TransactionReference> old;

try {
old = getHistoryUncommitted(object);
}
catch (UnknownReferenceException e) {
// the object was created before this transaction: it must have a history or otherwise the store is corrupted
throw new StoreException("The computed response reports a modified object that is not in store", e);
}

Expand All @@ -1287,13 +1292,13 @@ private Stream<TransactionReference> simplifiedHistory(StorageReference object,
simplified.add(added);

var oldAsArray = old.toArray(TransactionReference[]::new);
int length = oldAsArray.length;
for (int pos = 0; pos < length - 1; pos++)
int lastPos = oldAsArray.length - 1;
for (int pos = 0; pos < lastPos; pos++)
addIfUncovered(oldAsArray[pos], object, covered, simplified);

// the last is always useful, since it contains at least the class tag of the object
if (length >= 1)
simplified.add(oldAsArray[length - 1]);
if (lastPos >= 0)
simplified.add(oldAsArray[lastPos]);

return simplified.stream();
}
Expand All @@ -1315,15 +1320,8 @@ private void addIfUncovered(TransactionReference reference, StorageReference obj
else if (maybeResponse.get() instanceof TransactionResponseWithUpdates trwu) {
// we check if there is at least an update for a field of the object
// that is not yet covered by another update in a previous element of the history
Set<Update> diff = trwu.getUpdates()
.filter(update -> update.getObject().equals(object) && covered.stream().noneMatch(update::sameProperty))
.collect(Collectors.toSet());

if (!diff.isEmpty()) {
// the transaction reference actually adds at least one useful update
if (trwu.getUpdates().filter(update -> update.getObject().equals(object) && covered.stream().noneMatch(update::sameProperty) && covered.add(update)).count() > 0)
history.add(reference);
covered.addAll(diff);
}
}
else
throw new StoreException("The history contains a reference to a transaction without updates");
Expand Down

0 comments on commit 68fdd63

Please sign in to comment.