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

[3892] Only persist the editing context on actual changes #3894

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ A migration participant has been added to automatically keep compatible all diag
- https://github.com/eclipse-sirius/sirius-web/issues/3793[#3793] [sirius-web] Add mechanism to retrieve the parent object of an element in the tree representation
- https://github.com/eclipse-sirius/sirius-web/issues/3880[#3880] [sirius-web] Make some tests methods more generic
- https://github.com/eclipse-sirius/sirius-web/issues/3834[#3834] [deck] Add more variables for DeckDescription
- https://github.com/eclipse-sirius/sirius-web/issues/3892[#3892] [core] Only persist the editing context if there's been actual semantic changes

== v2024.7.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public JsonResource createResource(URI uri) {

var resource = new JsonResourceImpl(uri, options);
resource.setIntrinsicIDToEObjectMap(new HashMap<>());
resource.setTrackingModification(true);
return resource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IEditingContextPersistenceService;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
Expand All @@ -29,6 +30,8 @@
import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataUpdateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -46,6 +49,8 @@ public class EditingContextPersistenceService implements IEditingContextPersiste

private static final String TIMER_NAME = "siriusweb_editingcontext_save";

private final Logger logger = LoggerFactory.getLogger(EditingContextPersistenceService.class);

private final ISemanticDataUpdateService semanticDataUpdateService;

private final IResourceToDocumentService resourceToDocumentService;
Expand All @@ -64,33 +69,36 @@ public EditingContextPersistenceService(ISemanticDataUpdateService semanticDataU
@Override
@Transactional
public void persist(IEditingContext editingContext) {
long start = System.currentTimeMillis();
long start = System.nanoTime();

if (editingContext instanceof IEMFEditingContext emfEditingContext) {
new UUIDParser().parse(editingContext.getId())
.map(AggregateReference::<Project, UUID>to)
.ifPresent(project -> {

var documentData = emfEditingContext.getDomain().getResourceSet().getResources().stream()
.filter(resource -> IEMFEditingContext.RESOURCE_SCHEME.equals(resource.getURI().scheme()))
.filter(resource -> this.persistenceFilters.stream().allMatch(filter -> filter.shouldPersist(resource)))
.map(this.resourceToDocumentService::toDocument)
.flatMap(Optional::stream)
.collect(Collectors.toSet());

var documents = new LinkedHashSet<Document>();
var domainUris = new LinkedHashSet<String>();

documentData.forEach(data -> {
documents.add(data.document());
domainUris.addAll(data.ePackageEntries().stream().map(EPackageEntry::nsURI).toList());
var allResources = emfEditingContext.getDomain().getResourceSet().getResources();
if (allResources.stream().anyMatch(Resource::isModified)) {
new UUIDParser().parse(editingContext.getId())
.map(AggregateReference::<Project, UUID>to)
.ifPresent(project -> {
var documentData = allResources.stream()
.filter(resource -> IEMFEditingContext.RESOURCE_SCHEME.equals(resource.getURI().scheme()))
.filter(resource -> this.persistenceFilters.stream().allMatch(filter -> filter.shouldPersist(resource)))
.map(this.resourceToDocumentService::toDocument)
.flatMap(Optional::stream)
.collect(Collectors.toSet());

var documents = new LinkedHashSet<Document>();
var domainUris = new LinkedHashSet<String>();

documentData.forEach(data -> {
documents.add(data.document());
domainUris.addAll(data.ePackageEntries().stream().map(EPackageEntry::nsURI).toList());
});

this.semanticDataUpdateService.updateDocuments(project, documents, domainUris);
});

this.semanticDataUpdateService.updateDocuments(project, documents, domainUris);
});
}
}

long end = System.currentTimeMillis();
this.timer.record(end - start, TimeUnit.MILLISECONDS);
long durationNs = System.nanoTime() - start;
this.timer.record(durationNs, TimeUnit.NANOSECONDS);
this.logger.debug("Editing context {} saved in {} ms", editingContext.getId(), TimeUnit.NANOSECONDS.toMillis(durationNs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.editingcontext.services;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -21,19 +25,15 @@

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.sirius.components.emf.migration.api.MigrationData;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.migration.MigrationService;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.migration.api.MigrationData;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.components.emf.migration.api.IMigrationParticipant;
import org.eclipse.sirius.components.emf.migration.MigrationService;
import org.eclipse.sirius.web.application.editingcontext.services.api.IResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.stereotype.Service;

/**
Expand Down Expand Up @@ -67,6 +67,7 @@ public Optional<Resource> toResource(ResourceSet resourceSet, String id, String
try (var inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
resourceSet.getResources().add(resource);
resource.eAdapters().add(new ResourceMetadataAdapter(name));
resource.setTrackingModification(true);
resource.load(inputStream, options);

optionalResource = Optional.of(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Optional<DocumentData> toDocument(Resource resource) {
Optional<DocumentData> optionalDocumentData = Optional.empty();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
resource.save(outputStream, options);
resource.setModified(false);

for (Resource.Diagnostic warning : resource.getWarnings()) {
this.logger.warn(warning.getMessage());
Expand Down
Loading