Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix get elements at commit if element was deleted after commit #263

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'll get refactored when we merge in to the boeing branch anyways


@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
Loading