use of com.enonic.xp.node.NodeVersionsMetadata in project xp by enonic.
the class VersionTableVacuumCommand method doProcessRepository.
private void doProcessRepository(final Repository repository) {
int counter = 0;
NodeVersionId lastVersionId = null;
NodeVersionQuery query = createQuery(lastVersionId);
NodeVersionQueryResult versionsResult = nodeService.findVersions(query);
long hits = versionsResult.getHits();
final long totalHits = versionsResult.getTotalHits();
if (listener != null) {
listener.stepBegin(repository.getId().toString(), totalHits);
}
while (hits > 0) {
final List<NodeVersionId> versionsToDelete = new ArrayList<>();
final Set<BlobKey> nodeBlobToCheckSet = new HashSet<>();
final Set<BlobKey> binaryBlobToCheckSet = new HashSet<>();
final NodeVersionsMetadata versions = versionsResult.getNodeVersionsMetadata();
for (NodeVersionMetadata version : versions) {
final boolean toDelete = processVersion(repository, version);
if (toDelete) {
result.deleted();
versionsToDelete.add(version.getNodeVersionId());
nodeBlobToCheckSet.add(version.getNodeVersionKey().getNodeBlobKey());
binaryBlobToCheckSet.addAll(version.getBinaryBlobKeys().getSet());
} else {
result.inUse();
}
lastVersionId = version.getNodeVersionId();
counter++;
}
versionService.delete(versionsToDelete, InternalContext.from(ContextAccessor.current()));
nodeBlobToCheckSet.stream().filter(blobKey -> !isBlobKeyUsed(blobKey, VersionIndexPath.NODE_BLOB_KEY)).forEach(blobKey -> removeNodeBlobRecord(repository.getId(), NodeConstants.NODE_SEGMENT_LEVEL, blobKey));
binaryBlobToCheckSet.stream().filter(blobKey -> !isBlobKeyUsed(blobKey, VersionIndexPath.BINARY_BLOB_KEYS)).forEach(blobKey -> removeNodeBlobRecord(repository.getId(), NodeConstants.BINARY_SEGMENT_LEVEL, blobKey));
query = createQuery(lastVersionId);
versionsResult = nodeService.findVersions(query);
hits = versionsResult.getHits();
}
if (listener != null) {
listener.processed(counter);
}
}
use of com.enonic.xp.node.NodeVersionsMetadata in project xp by enonic.
the class NodeVersionQueryResultFactory method create.
public static NodeVersionQueryResult create(final NodeVersionQuery query, final SearchResult searchResult) {
final NodeVersionQueryResult.Builder findNodeVersionsResult = NodeVersionQueryResult.create();
findNodeVersionsResult.hits(searchResult.getHits().getSize());
findNodeVersionsResult.totalHits(searchResult.getTotalHits());
findNodeVersionsResult.from(query.getFrom());
findNodeVersionsResult.to(query.getSize());
final NodeVersionsMetadata nodeVersionsMetadata = buildEntityVersions(query, searchResult);
findNodeVersionsResult.entityVersions(nodeVersionsMetadata);
return findNodeVersionsResult.build();
}
use of com.enonic.xp.node.NodeVersionsMetadata in project xp by enonic.
the class FindNodeVersionsCommandTest method get_multiple_version.
@Test
void get_multiple_version() {
PropertyTree data = new PropertyTree();
final Node node = createNode(CreateNodeParams.create().data(data).name("my-node").parent(NodePath.ROOT).build());
final int updateCount = 4;
updateNode(node.id(), updateCount);
refresh();
final NodeVersionQuery query = NodeVersionQuery.create().size(100).from(0).nodeId(node.id()).addOrderBy(FieldOrderExpr.create(VersionIndexPath.TIMESTAMP, OrderExpr.Direction.DESC)).build();
final NodeVersionQueryResult result = FindNodeVersionsCommand.create().query(query).searchService(this.searchService).build().execute();
assertEquals(updateCount + 1, result.getHits());
final NodeVersionsMetadata nodeVersionsMetadata = result.getNodeVersionsMetadata();
Instant previousTimestamp = null;
for (final NodeVersionMetadata nodeVersionMetadata : nodeVersionsMetadata) {
final Instant currentNodeTimestamp = nodeVersionMetadata.getTimestamp();
if (previousTimestamp != null) {
if (currentNodeTimestamp.isAfter(previousTimestamp)) {
fail(String.format("expected timestamp of current item to be before or equal to previous. Previous: [%s], current: [%s]", previousTimestamp, currentNodeTimestamp));
}
}
previousTimestamp = currentNodeTimestamp;
}
}
use of com.enonic.xp.node.NodeVersionsMetadata in project xp by enonic.
the class NodeServiceImplTest method test_commit.
@Test
public void test_commit() {
// Create and update node
final Node createdNode = createNode(CreateNodeParams.create().name("my-node").parent(NodePath.ROOT).build());
final NodeId nodeId = createdNode.id();
final UpdateNodeParams updateNodeParams = UpdateNodeParams.create().id(nodeId).editor(toBeEdited -> {
toBeEdited.data.addString("newField", "fisk");
}).build();
final Node updatedNode = updateNode(updateNodeParams);
nodeService.refresh(RefreshMode.STORAGE);
// Check that the two versions have no commit ID by default
final NodeVersionsMetadata versionsMetadata = getVersionsMetadata(nodeId);
assertEquals(2, versionsMetadata.size());
final Iterator<NodeVersionMetadata> versionMetadataIterator = versionsMetadata.iterator();
final NodeVersionMetadata latestVersionMetadata = versionMetadataIterator.next();
final NodeVersionMetadata firstVersionMetadata = versionMetadataIterator.next();
assertNull(latestVersionMetadata.getNodeCommitId());
assertNull(firstVersionMetadata.getNodeCommitId());
// Call commit with node ID
final NodeCommitEntry commitEntry = NodeCommitEntry.create().message("Commit message").build();
final NodeCommitEntry returnedCommitEntry = nodeService.commit(commitEntry, NodeIds.from(nodeId));
nodeService.refresh(RefreshMode.STORAGE);
// Check created commit entry
final NodeCommitId nodeCommitId = returnedCommitEntry.getNodeCommitId();
assertNotNull(nodeCommitId);
assertEquals("Commit message", returnedCommitEntry.getMessage());
assertNotNull(returnedCommitEntry.getTimestamp());
assertEquals("user:system:test-user", returnedCommitEntry.getCommitter().toString());
// Check that only the latest version has a commit ID
final NodeVersionsMetadata versionsMetadata2 = getVersionsMetadata(nodeId);
assertEquals(2, versionsMetadata2.size());
final Iterator<NodeVersionMetadata> versionMetadataIterator2 = versionsMetadata2.iterator();
final NodeVersionMetadata latestVersionMetadata2 = versionMetadataIterator2.next();
final NodeVersionMetadata firstVersionMetadata2 = versionMetadataIterator2.next();
assertEquals(nodeCommitId, latestVersionMetadata2.getNodeCommitId());
assertNull(firstVersionMetadata2.getNodeCommitId());
// Call commit with the node version ID of the first version
final NodeCommitEntry commitEntry2 = NodeCommitEntry.create().message("Commit message 2").build();
final RoutableNodeVersionId routableNodeVersionId = RoutableNodeVersionId.from(nodeId, firstVersionMetadata2.getNodeVersionId());
final NodeCommitEntry returnedCommitEntry2 = nodeService.commit(commitEntry, RoutableNodeVersionIds.from(routableNodeVersionId));
nodeService.refresh(RefreshMode.STORAGE);
// Check that only the first version has been impacted
final NodeVersionsMetadata versionsMetadata3 = getVersionsMetadata(nodeId);
assertEquals(2, versionsMetadata3.size());
final Iterator<NodeVersionMetadata> versionMetadataIterator3 = versionsMetadata3.iterator();
final NodeVersionMetadata latestVersionMetadata3 = versionMetadataIterator3.next();
final NodeVersionMetadata firstVersionMetadata3 = versionMetadataIterator3.next();
assertEquals(nodeCommitId, latestVersionMetadata3.getNodeCommitId());
assertEquals(returnedCommitEntry2.getNodeCommitId(), firstVersionMetadata3.getNodeCommitId());
}
use of com.enonic.xp.node.NodeVersionsMetadata in project xp by enonic.
the class FindVersionsHandlerTest method testFindVersionsHandler.
@Test
public void testFindVersionsHandler() {
final NodeVersionMetadata newNodeVersionMeta = NodeVersionMetadata.create().nodeId(NodeId.from("nodeId1")).nodeVersionId(NodeVersionId.from("nodeVersionNew")).nodePath(NodePath.ROOT).timestamp(Instant.ofEpochSecond(1000)).build();
final NodeVersionMetadata oldNodeVersionMeta = NodeVersionMetadata.create().nodeId(NodeId.from("nodeId1")).nodeVersionId(NodeVersionId.from("nodeVersionOld")).nodePath(NodePath.ROOT).timestamp(Instant.ofEpochSecond(500)).build();
final NodeVersionsMetadata nodeVersionsMetadata = NodeVersionsMetadata.create(NodeId.from("nodeId1")).add(newNodeVersionMeta).add(oldNodeVersionMeta).build();
final NodeVersionQueryResult result = NodeVersionQueryResult.create().entityVersions(nodeVersionsMetadata).from(0).to(2).hits(2).totalHits(40).build();
final ArgumentCaptor<GetNodeVersionsParams> getNodeVersionsParamsCaptor = ArgumentCaptor.forClass(GetNodeVersionsParams.class);
Mockito.when(nodeService.findVersions(Mockito.any(GetNodeVersionsParams.class))).thenReturn(result);
runScript("/lib/xp/examples/node/findVersions.js");
Mockito.verify(nodeService).findVersions(getNodeVersionsParamsCaptor.capture());
assertEquals("nodeId", getNodeVersionsParamsCaptor.getValue().getNodeId().toString());
assertEquals(0, getNodeVersionsParamsCaptor.getValue().getFrom());
assertEquals(2, getNodeVersionsParamsCaptor.getValue().getSize());
}
Aggregations