Skip to content

Commit

Permalink
Merge pull request #263 from Open-MBEE/fix/261
Browse files Browse the repository at this point in the history
fix get elements at commit if element was deleted after commit
  • Loading branch information
dlamoris authored Feb 21, 2024
2 parents 4e4e858 + e1080c0 commit e40e8f0
Show file tree
Hide file tree
Showing 5 changed files with 954 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ jobs:

- setup_remote_docker

- run:
- run:
name: "Create and start all services from the docker-compose configuration"
command: |
cp example/src/main/resources/application.properties.example ./example/src/main/resources/application.properties
docker-compose up --build -d
docker run --network container:mms curlimages/curl --retry 8 --retry-delay 10 --retry-max-time 90 --retry-connrefused http://mms:8080/healthcheck
- run:
- run:
name: "Run and test Postman Collection"
command: |
docker create -v /etc/newman --name mms_test_configs alpine:3.4 /bin/true
docker cp example/. mms_test_configs:/etc/newman
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run crud.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run getAtCommits.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run cameo.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run jupyter.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run localauth.postman_collection.json -e test-env.json --delay-request 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface CommitIndexDAO {

List<CommitJson> elementHistory(String id, Set<String> commitIds);

List<CommitJson> elementDeletedHistory(String id, Collection<String> commitIds);

CommitJson update(CommitJson commitJson);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@
import org.openmbee.mms.data.domains.scoped.Branch;
import org.openmbee.mms.data.domains.scoped.Commit;
import org.openmbee.mms.data.domains.scoped.Node;
import org.openmbee.mms.core.dao.CommitIndexDAO;
import org.openmbee.mms.json.CommitJson;
import org.openmbee.mms.json.ElementJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.openmbee.mms.core.config.ContextHolder.getContext;

@Service
public class NodeGetHelper extends NodeOperation {

protected CommitIndexDAO commitIndex;

@Autowired
public void setCommitIndex(CommitIndexDAO commitIndex) {
this.commitIndex = commitIndex;
}

public NodeGetInfo processGetJsonFromNodes(List<Node> nodes, NodeService service) {
NodeGetInfo info = initInfoFromNodes(nodes, null);
return processLatest(info, service);
Expand Down Expand Up @@ -122,21 +132,45 @@ private NodeGetInfo processCommit(NodeGetInfo info, String commitId, NodeService
Optional<ElementJson> e = nodeIndex.getElementLessThanOrEqualTimestamp(nodeId,
formatter.format(time), refCommitIds);
if (e.isPresent()) { // found version of element at commit time
//TODO determine if element was deleted at the time?
e.get().setRefId(ContextHolder.getContext().getBranchId());
info.getActiveElementMap().put(nodeId, e.get());
Instant realModified = Instant.from(formatter.parse(e.get().getModified()));
if (elementDeleted(nodeId, commitId, time, realModified, refCommitIds)) {
rejectDeleted(info, nodeId, e.get());
} else {
e.get().setRefId(ContextHolder.getContext().getBranchId());
info.getActiveElementMap().put(nodeId, e.get());
}
} else {
rejectNotFound(info, nodeId); // element not found at commit time
}
} else if (info.getExistingNodeMap().get(nodeId).isDeleted()) { // latest element is before commit, but deleted
rejectDeleted(info, nodeId, indexElement);
if (refCommitIds == null) { // need list of commitIds of current ref to filter on
refCommitIds = getRefCommitIds(time);
}
if (elementDeleted(nodeId, commitId, time, modified, refCommitIds)) {
rejectDeleted(info, nodeId, indexElement);
} else {
info.getActiveElementMap().put(nodeId, indexElement);
}
} else { // latest element version is version at commit, not deleted
info.getActiveElementMap().put(nodeId, indexElement);
}
}
return info;
}

private boolean elementDeleted(String nodeId, String commitId, Instant time, Instant modified, List<String> refCommitIds) {
List<CommitJson> commits = commitIndex.elementDeletedHistory(nodeId, refCommitIds);
for (CommitJson c: commits) {
Instant deletedTime = Instant.from(formatter.parse(c.getCreated()));
if ((deletedTime.isBefore(time) || c.getId().equals(commitId)) && deletedTime.isAfter(modified)) {
//there's a delete between element last modified time and requested commit time
//or element is deleted at commit
return true;
}
}
return false;
}

public NodeGetInfo processGetJson(List<ElementJson> elements, Instant time, NodeService service) {
Optional<Branch> ref = branchRepository.findByBranchId(getContext().getBranchId());
if (ref.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ private QueryBuilder getCommitHistoryQuery(String id, Set<String> commitIds) {
return query;
}

@Override
public List<CommitJson> elementDeletedHistory(String id, Collection<String> commitIds) {
QueryBuilder query = QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery("deleted.id", id))
.filter(QueryBuilders.termsQuery(CommitJson.ID, commitIds));
try {
List<CommitJson> commits = new ArrayList<>();
SearchHits hits = getCommitResults(query);
if (hits.getTotalHits().value == 0) {
return new ArrayList<>();
}
for (SearchHit hit : hits.getHits()) {
Map<String, Object> source = hit.getSourceAsMap();// gets "_source"
CommitJson ob = newInstance();
ob.putAll(source);
ob.remove(CommitJson.ADDED);
ob.remove(CommitJson.UPDATED);
ob.remove(CommitJson.DELETED);
commits.add(ob);
}
return commits;
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new InternalErrorException(e);
}
}

/**
* Returns the commit history of a element
* <p> Returns a list of commit metadata for the specified id
Expand Down
Loading

0 comments on commit e40e8f0

Please sign in to comment.