Skip to content

Commit 86adc0b

Browse files
committed
feat: add capability to skip loading the AST from storage
This commit introduces a new ResourceSetOption, using which it is possible to skip loading the AST when loading a resource from binary storage.
1 parent 908d2d6 commit 86adc0b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/ResourceSetOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
public final class ResourceSetOptions {
2424

2525
private static final String INSTALL_DERIVED_STATE = "com.avaloq.tools.ddk.xtext.resource.ResourceSetOptions.installDerivedState";
26+
private static final String SKIP_AST = "com.avaloq.tools.ddk.xtext.resource.ResourceSetOptions.skipAST";
2627

2728
private ResourceSetOptions() {
2829
// utility class
@@ -52,4 +53,12 @@ public static void setInstallDerivedState(final @NonNull ResourceSet resourceSet
5253
resourceSet.getLoadOptions().put(INSTALL_DERIVED_STATE, installDerivedState);
5354
}
5455

56+
public static boolean skipAST(final @NonNull ResourceSet resourceSet) {
57+
Object object = resourceSet.getLoadOptions().get(SKIP_AST);
58+
return object != null && (boolean) object; // default is false
59+
}
60+
61+
public static void setSkipAST(final @NonNull ResourceSet resourceSet, final @Nullable Boolean installDerivedState) {
62+
resourceSet.getLoadOptions().put(SKIP_AST, installDerivedState);
63+
}
5564
}

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/persistence/DirectLinkingResourceStorageLoadable.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import org.apache.logging.log4j.LogManager;
2626
import org.apache.logging.log4j.Logger;
2727
import org.eclipse.emf.common.notify.Adapter;
28+
import org.eclipse.emf.common.util.BasicEList;
2829
import org.eclipse.emf.common.util.URI;
2930
import org.eclipse.emf.ecore.EObject;
3031
import org.eclipse.emf.ecore.InternalEObject;
32+
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl.Container;
3133
import org.eclipse.emf.ecore.resource.Resource;
3234
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
3335
import org.eclipse.emf.ecore.util.EcoreUtil;
@@ -123,8 +125,6 @@ public void loadResource(final Resource resource) throws IOException {
123125
if (uri != null && uri.isHierarchical() && !uri.isRelative()) {
124126
baseURI = uri;
125127
}
126-
boolean installDerivedState = ResourceSetOptions.installDerivedState(resourceSet); // must be read before readCompressedInt, see HACK comment inside
127-
// readCompressedInt
128128
int size = readCompressedInt();
129129
if (!installDerivedState && size == 2) { // the InfererenceContainer is always in the second slot
130130
size--;
@@ -153,6 +153,8 @@ public void loadResource(final Resource resource) throws IOException {
153153
private final ITraceSet traceSet;
154154

155155
private ResourceLoadMode mode;
156+
private boolean installDerivedState;
157+
private boolean skipAST;
156158

157159
public DirectLinkingResourceStorageLoadable(final InputStream in, final boolean loadNodeModel, final boolean splitContents, final ITraceSet traceSet) {
158160
super(in, loadNodeModel);
@@ -182,6 +184,8 @@ public void loadIntoResource(final StorageAwareResource resource, final Resource
182184
throw new IllegalArgumentException("Incompatible resource load mode: " + loadMode.instruction(Constituent.RESOURCE)); //$NON-NLS-1$
183185
}
184186
this.mode = loadMode;
187+
this.installDerivedState = ResourceSetOptions.installDerivedState(resource.getResourceSet());
188+
this.skipAST = ResourceSetOptions.skipAST(resource.getResourceSet());
185189
traceSet.started(ResourceLoadStorageEvent.class, resource.getURI(), loadMode);
186190
try {
187191
super.loadIntoResource(resource);
@@ -223,7 +227,12 @@ protected void loadEntries(final StorageAwareResource resource, final ZipInputSt
223227
LOG.warn("Proxying of resource contents is not supported: " + resource.getURI()); //$NON-NLS-1$
224228
// fall through
225229
case LOAD:
226-
readContents(resource, new NonLockingBufferInputStream(zipIn));
230+
if (!skipAST) {
231+
readContents(resource, new NonLockingBufferInputStream(zipIn));
232+
} else {
233+
// TODO I need to put something in the resource. Don't want to get into proxying yet (might have to).
234+
addFakeAST(resource);
235+
}
227236
break;
228237
}
229238

@@ -286,6 +295,15 @@ protected void loadEntries(final StorageAwareResource resource, final ZipInputSt
286295
}
287296
}
288297

298+
private void addFakeAST(final StorageAwareResource resource) {
299+
InternalEObject[] values = new InternalEObject[] {new Container()};
300+
BasicEList<InternalEObject> internalEObjectList = new BasicEList<InternalEObject>(); // NOPMD LooseCoupling
301+
internalEObjectList.setData(1, values);
302+
@SuppressWarnings("unchecked")
303+
InternalEList<InternalEObject> internalEObjects = (InternalEList<InternalEObject>) (InternalEList<?>) resource.getContents();
304+
internalEObjects.addAllUnique(internalEObjectList);
305+
}
306+
289307
/**
290308
* Read the node model from the given input stream.
291309
*

0 commit comments

Comments
 (0)