]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Merge "Added a function to read the module source text"
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Tue, 20 Mar 2018 12:25:04 +0000 (14:25 +0200)
committerGerrit Code Review <gerrit2@www.simantics.org>
Tue, 20 Mar 2018 12:25:04 +0000 (14:25 +0200)
bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java
bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java
bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphs.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/ByteFileReader.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java
bundles/org.simantics.project/src/org/simantics/project/management/PlatformUtil.java
bundles/org.simantics.utils/src/org/simantics/utils/Development.java
bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java
bundles/org.simantics/src/org/simantics/SimanticsPlatform.java

index f8a19504c8dc0165f38ddb34f27b7b31fe8a38ed..b429eb49a77d7d254be6599ced45bd39b68d3ef6 100644 (file)
@@ -12,6 +12,7 @@
 package org.simantics.db.indexing;
 
 import java.io.IOException;
+import java.nio.file.FileAlreadyExistsException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
@@ -117,8 +118,12 @@ public final class DatabaseIndexing {
             // Mark change only once per DB session.
             if (getIndexChangedWriter(session).markDirty(changedFile)) {
                 Files.createDirectories(indexPath);
-                Files.createFile(changedFile);
-                FileUtils.sync(changedFile);
+                if (!Files.exists(changedFile)) {
+                    Files.createFile(changedFile);
+                    FileUtils.sync(changedFile);
+                } else if (!Files.isRegularFile(changedFile)) {
+                    throw new FileAlreadyExistsException(changedFile.toString(), null, "index dirtyness indicator file already exists but it is not a regular file");
+                }
             }
         } catch (IOException e) {
             LOGGER.error("Could not mark index changed for indexPath={} and changedFile={}", indexPath.toAbsolutePath(), changedFile.toAbsolutePath());
index 86c3d2fcbb18c51bae4e8bfefbdcd7928de57611..68a94e304581f71d55d1c57d9e3220a5da6bac3c 100644 (file)
@@ -135,9 +135,9 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
                Extensions extensions;
                int resourceCount;
                
-               private int identities;
-               private int stmLength;
-               private int valueLength;
+               private int identities = -1;
+               private int stmLength = -1;
+               private int valueLength = -1;
                
                public FileTransferableGraphSource() throws Exception {
                    init();
@@ -176,33 +176,41 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
 
                @Override
                public int getIdentityCount() throws Exception {
-                       identities = safeInt();
+                       if(identities == -1) {
+                               identities = safeInt();
+                       }
                        return identities;
                }
 
                @Override
                public int getStatementCount() throws Exception {
-                       stmLength = safeInt();
+                       if(stmLength == -1) {
+                               stmLength = safeInt();
+                       }
                        return stmLength;
                }
 
                @Override
                public int getValueCount() throws Exception {
-                       valueLength = safeInt();
+                       if(valueLength == -1) {
+                               valueLength = safeInt();
+                       }
                        return valueLength;
                }
 
                @Override
                public void forStatements(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
-                       
+
                        int[] value = new int[4];
 
+                       int stmLength = getStatementCount();
+
                        for(int stmIndex=0;stmIndex<stmLength;) {
-                               
+
                                value[stmIndex & 3] = safeInt();
                                stmIndex++;
                                if((stmIndex & 3) == 0) procedure.execute(value);
-                               
+
                                // Cached bytes 
                                int avail = (SIZE-byteIndex) >> 2;
                                int allowed = Math.min(stmLength-stmIndex, avail);
@@ -213,94 +221,100 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
 //                                     statements[stmIndex++] = ((bytes[index++]&0xff)<<24) | ((bytes[index++]&0xff)<<16) | ((bytes[index++]&0xff)<<8) | ((bytes[index++]&0xff));                              
                                }
                                byteIndex += allowed<<2;
-                               
+
                        }
-                       
+
                }
 
                @Override
                public void forIdentities(ReadGraph graph, TransferableGraphSourceProcedure<Identity> procedure) throws Exception {
-                       
+
+                       int identities = getIdentityCount();
+
                        for(int i=0;i<identities;i++) {
-                               
+
                                int rid = safeInt();
                                byte type = bytes[byteIndex++];
                                // External
                                if(type == 1) {
-                                       
+
                                        int parent = safeInt();
-                                       int nameLen = bytes[byteIndex++]&0xff;
-                                       
+                                       int nameLen = getDynamicUInt32();
                                        if(byteIndex+nameLen < SIZE) {
                                                procedure.execute(new Identity(rid, new External(parent, utf(bytes, byteIndex, byteIndex + nameLen))));
                                                byteIndex += nameLen;
                                        } else {
                                                procedure.execute(new Identity(rid, new External(parent, utf(safeBytes(nameLen), 0, nameLen))));
                                        }
-                                       
                                } 
                                // Internal
                                else if(type == 3) {
-                                       
+
                                        int parent = safeInt();
-                                       int nameLen = bytes[byteIndex++]&0xff;
+                                       int nameLen = getDynamicUInt32();
                                        if(byteIndex+nameLen < SIZE) {
                                                procedure.execute(new Identity(rid, new Internal(parent, utf(bytes, byteIndex, byteIndex + nameLen))));
                                                byteIndex += nameLen;
                                        } else {
                                                procedure.execute(new Identity(rid, new Internal(parent, utf(safeBytes(nameLen), 0, nameLen))));
                                        }
-                                       
+
                                }
                                // Root
                                else if(type == 0) {
-                                       
-                                       int nameLen = bytes[byteIndex++]&0xff;
+
+                                       int nameLen = getDynamicUInt32();
                                        String name = utf(safeBytes(nameLen), 0, nameLen);
-                                       int nameLen2 = bytes[byteIndex++]&0xff;
+                                       int nameLen2 = getDynamicUInt32();
                                        String rType = utf(safeBytes(nameLen2), 0, nameLen2);
                                        procedure.execute(new Identity(rid, new Root(name, rType)));
 
-                               } else if(type == 2) {
-                                       throw new UnsupportedOperationException();
+                               }
+                               // Optional
+                               else if(type == 2) {
+                                       throw new UnsupportedOperationException("Optional identities not supported");
+                               } else {
+                                       throw new IllegalStateException("Unsupported identity type " + type);
                                }
 
                        }
-                       
+
                }
 
                @Override
                public void forValues(ReadGraph graph, TransferableGraphSourceProcedure<Value> procedure) throws Exception {
-                       
+
+                       int valueLength = getValueCount();
+
                        Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
-                       
+
                        List<Object> idcontext = new ArrayList<>(); 
-                       
+
                        for(int i=0;i<valueLength;i++) {
                                int resource = safeInt();
+                               idcontext.clear();
                                Variant value = (Variant)variantSerializer
-                                       .deserialize((DataInput)dis, idcontext);
+                                               .deserialize((DataInput)dis, idcontext);
                                procedure.execute(new Value(resource, value));
                        }
-                       
-                       
+
                }
-               
+
                @Override
                public void forValues2(ReadGraph graph, TransferableGraphSourceValueProcedure procedure) throws Exception {
 
                    Binding datatypeBinding = Bindings.getBinding(Datatype.class);
             Serializer datatypeSerializer = Bindings.getSerializerUnchecked(datatypeBinding);
-            
+
             List<Object> idContext = new ArrayList<>(); 
-            
+
             for(int i=0;i<valueLength;i++) {
                 int resource = safeInt();
                 idContext.clear();
                 Datatype type = (Datatype)datatypeSerializer.deserialize((DataInput)dis, idContext);
                 procedure.execute(resource, type, dis);
             }
-                   
+
                }
 
                @Override
@@ -316,7 +330,7 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
        public TransferableGraphSource readTG() throws Exception {
 
                if(getSize() == 0) return null;
-               
+
                return new FileTransferableGraphSource();
 
        }
index 0841e1455569ed08e0f184c750c5125e9fe55aa3..2b93df74dceeb0fed0b3f0c871a62fe043968d8a 100644 (file)
@@ -597,15 +597,14 @@ public class TransferableGraphs {
 
        public static TransferableGraph1 create(ReadGraph graph, TransferableGraphSource source) throws DatabaseException {
                
-               final TIntArrayList statements = new TIntArrayList();
-               final ArrayList<Value> values = new ArrayList<>();
-               final ArrayList<Identity> identities = new ArrayList<>();
-               
                try {
 
+                       ArrayList<Identity> identities = new ArrayList<>(source.getIdentityCount());
+                       source.forIdentities(graph, i -> identities.add(i));
+                       TIntArrayList statements = new TIntArrayList(source.getStatementCount());
                        source.forStatements(graph, r -> statements.addAll(r));
+                       ArrayList<Value> values = new ArrayList<>(source.getValueCount());
                        source.forValues(graph, v -> values.add(v));
-                       source.forIdentities(graph, i -> identities.add(i));
 
                        return new TransferableGraph1(source.getResourceCount(), 
                                        identities.toArray(new Identity[identities.size()]),
index 4c14bded6639fbf0662e6548f486d38dd27be912..e930a00fcad35b9c36bc323bd119c165f8f495dc 100644 (file)
@@ -69,9 +69,7 @@ public class ByteFileReader implements Closeable {
        }
 
        final protected byte[] safeBytes(int amount) throws IOException {
-
                byte[] result = new byte[amount];
-               
                int has = size-byteIndex;
                if(amount >= has) {
                        ReadableByteChannel c = channel;
@@ -86,6 +84,9 @@ public class ByteFileReader implements Closeable {
                                int got = c.read(bb2);
                                if(got == -1) throw new IOException("Unexpected end-of-file");
                                has += got; 
+                               // For some unknown reason this is needed!
+                               // Spec indicates that read would increment position but it does not.
+                               bb2.position(has);
                        }
                        size = c.read(bb);
                        bb.position(0);
@@ -104,7 +105,7 @@ public class ByteFileReader implements Closeable {
            int result;
         if(has == 0) {
             ReadableByteChannel c = channel;
-            ByteBuffer bb = byteBuffer;            
+            ByteBuffer bb = byteBuffer;
             size = c.read(bb);
             if(size == -1) {
                                throw new EOFException("Unexpected end-of-file");
@@ -114,43 +115,40 @@ public class ByteFileReader implements Closeable {
             if(size == 0)
                 return -1;
         }
-        result = bytes[byteIndex];
-        if(result < 0)
-            result += 256;
-        ++byteIndex;        
+        result = bytes[byteIndex++] & 0xff;
         return result;
        }
 
        public int getDynamicUInt32() throws IOException {
-               int length = getByte()&0xff
+               int length = getByte(); 
                if(length >= 0x80) {
                        if(length >= 0xc0) {
                                if(length >= 0xe0) {
                                        if(length >= 0xf0) {
                                                length &= 0x0f;
-                                               length += ((getByte()&0xff)<<3);
-                                               length += ((getByte()&0xff)<<11);
-                                               length += ((getByte()&0xff)<<19);
+                                               length += (getByte()<<3);
+                                               length += (getByte()<<11);
+                                               length += (getByte()<<19);
                                                length += 0x10204080;
                                        }
                                        else {
                                                length &= 0x1f;
-                                               length += ((getByte()&0xff)<<4);
-                                               length += ((getByte()&0xff)<<12);
-                                               length += ((getByte()&0xff)<<20);
+                                               length += (getByte()<<4);
+                                               length += (getByte()<<12);
+                                               length += (getByte()<<20);
                                                length += 0x204080;
                                        }
                                }
                                else {
                                        length &= 0x3f;
-                                       length += ((getByte()&0xff)<<5);
-                                       length += ((getByte()&0xff)<<13);
+                                       length += (getByte()<<5);
+                                       length += (getByte()<<13);
                                        length += 0x4080;
                                }
                        }
                        else {
                                length &= 0x7f;
-                               length += ((getByte()&0xff)<<6);
+                               length += (getByte()<<6);
                                length += 0x80;
                        }
                }
@@ -159,10 +157,12 @@ public class ByteFileReader implements Closeable {
 
        final protected int safeInt() throws IOException {
 
+               byte[] bytes = this.bytes;
+
                if(byteIndex >= (size-5)) {
                        int result = 0;
                        ReadableByteChannel c = channel;
-               ByteBuffer bb = byteBuffer;
+                       ByteBuffer bb = byteBuffer;
                        if(byteIndex == size) {
                                size = c.read(bb);
                                if(size == -1) throw new EOFException("Unexpected end-of-file");
@@ -190,7 +190,7 @@ public class ByteFileReader implements Closeable {
                                bb.position(0);
                                byteIndex = 0;
                        }
-                       result |= ((int)(bytes[byteIndex++]&0xff)<<0);
+                       result |= ((int)(bytes[byteIndex++]&0xff));
                        if(byteIndex == size) {
                                size = c.read(bb);
                                bb.position(0);
@@ -200,9 +200,9 @@ public class ByteFileReader implements Closeable {
                } else {
                        return ((bytes[byteIndex++]&0xff)<<24) | ((bytes[byteIndex++]&0xff)<<16) | ((bytes[byteIndex++]&0xff)<<8) | ((bytes[byteIndex++]&0xff));
                }
-               
+
        }
-       
+
        final protected int getSize() {
                return size;
        }
index ae1ec711ce676e9d3932cbbbc10485601cbb2f1d..077eb6617a1703f3cbffa913633e2b4c738f95ab 100644 (file)
@@ -161,7 +161,7 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 
                int resourceCount = safeInt();
                
-               List<Object> idcontext = new ArrayList<Object>(); 
+               List<Object> idcontext = new ArrayList<>(); 
                dis = new DataInputStream(in);
                Extensions extensions = (Extensions)Bindings.getSerializerUnchecked(Extensions.class).deserialize((DataInput)dis, idcontext);
                
@@ -174,27 +174,27 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 //             long duration = System.nanoTime() - start;
 //             LOGGER.warn("start in " + 1e-9*duration + "s.");
 //             start = System.nanoTime();
-               
+
                for(int i=0;i<identities;i++) {
                        int rid = safeInt();
                        byte type = bytes[byteIndex++];
                        // External
                        if(type == 1) {
-                               
+
                                int parent = safeInt();
                                int nameLen = getDynamicUInt32();
-                               
+
                                if(byteIndex+nameLen < SIZE) {
                                        ids[i] = new Identity(rid, new External(parent, utf(bytes, byteIndex, byteIndex + nameLen)));
                                        byteIndex += nameLen;
                                } else {
                                        ids[i] = new Identity(rid, new External(parent, utf(safeBytes(nameLen), 0, nameLen)));
                                }
-                               
+
                        } 
                        // Internal
                        else if(type == 3) {
-                               
+
                                int parent = safeInt();
                                int nameLen = getDynamicUInt32();
                                if(byteIndex+nameLen < SIZE) {
@@ -215,6 +215,8 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 
                        } else if(type == 2) {
                                throw new UnsupportedOperationException();
+                       } else {
+                               throw new IllegalStateException();
                        }
 
                }
@@ -257,11 +259,11 @@ final public class TransferableGraphFileReader extends ByteFileReader {
 
                Serializer variantSerializer = Bindings.getSerializerUnchecked(Bindings.VARIANT);
                
-               idcontext = new ArrayList<Object>(); 
                dis = new DataInputStream(in);
                
                for(int i=0;i<valueLength;i++) {
                        int resource = safeInt();
+                       idcontext.clear();
                        Variant value = (Variant)variantSerializer
                                .deserialize((DataInput)dis, idcontext);
                        values[i] = new Value(resource, value);
index 9dfa67efef7be394081c583f325b09ed63dfea4e..a8916251e07c738413866bde501a42b3e7c3eec6 100644 (file)
@@ -392,8 +392,10 @@ public class PlatformUtil {
        private static GraphBundleEx tryGetOnDemandGraph(Bundle bundle, URL url) throws IOException {
                try {
                        Integer cachedHash = readCachedHash(url);
-                       if (cachedHash == null)
+                       if (cachedHash == null) {
+                               LOGGER.info("No cached hash for " + bundle);
                                return null;
+                       }
 
                        Supplier<TransferableGraph1> graphSource = () -> {
                                try {
@@ -443,7 +445,7 @@ public class PlatformUtil {
                // For an unknown reason this is totally broken when running the TestSCLOsgi
                // in the SDK Tycho build. It returns incomplete results because the
                // ReadableByteChannel used by ByteFileReader starts returning 0 unexpectedly.
-//                             try (TransferableGraphFileReader reader = new TransferableGraphFileReader(is)) {
+//             try (TransferableGraphFileReader reader = new TransferableGraphFileReader(is)) {
 //                     return reader.readTG();
 //             }
                return DataContainers.readFile(new DataInputStream(is), handlers);
index 84c13b4d82973f81706ae881fe2736821f5dff1e..96c3453c759020f7a8d2d072ace6af721a20b892 100644 (file)
@@ -16,7 +16,7 @@ import org.slf4j.LoggerFactory;
 public class Development {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(Development.class);
-       public static TreeMap<String,Integer> histogram = new TreeMap<String,Integer>();
+       public static TreeMap<String,Integer> histogram = new TreeMap<>();
 
        public static final boolean DEVELOPMENT = false;
        
@@ -28,9 +28,9 @@ public class Development {
                
        }
        
-       final static private HashMap<String, Variant> properties = new HashMap<String, Variant>(); 
+       final static private HashMap<String, Variant> properties = new HashMap<>(); 
        
-       final static private CopyOnWriteArrayList<DevelopmentListener> listeners = new CopyOnWriteArrayList<DevelopmentListener>();
+       final static private CopyOnWriteArrayList<DevelopmentListener> listeners = new CopyOnWriteArrayList<>();
 
        static {
                
@@ -96,13 +96,8 @@ public class Development {
        
        public static void recordHistogram(Object o) {
                String key = o.toString();
-               Integer i = Development.histogram.get(key);
-               if(i == null) i = 0;
-               int newValue = i+1;
-               if (newValue == 1000) {
-                   newValue = newValue;
-               }
-               Development.histogram.put(key, newValue);
+               Integer i = histogram.get(key);
+               histogram.put(key, i == null ? 1 : i+1);
        }
        
 }
index d0b8e69492ff44c274077fffcc7940f646029dc5..a3b409027a0c314354a3e172eac62163094b8be0 100644 (file)
@@ -61,12 +61,7 @@ public class BaselineCreatorApplication implements IApplication {
 
        private static Path getInstanceLocation() throws CoreException, IOException {
                Location l = Platform.getInstanceLocation();
-               if (l == null)
-                       throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
-                                       "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
-
-               Location instanceLoc = Platform.getInstanceLocation();
-               if (instanceLoc == null || instanceLoc.isReadOnly())
+               if (l == null || l.isReadOnly())
                        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                        "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
 
index 3638d3407df271caa1869b2e9d97cc266e3b535e..ad3b86d9c989814c64fbb1783c5a8982fe60940c 100644 (file)
@@ -41,6 +41,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.osgi.service.datalocation.Location;
 import org.eclipse.osgi.service.resolver.BundleDescription;
 import org.ini4j.Ini;
 import org.ini4j.InvalidFileFormatException;
@@ -696,7 +697,42 @@ public class SimanticsPlatform implements LifecycleListener {
         resetDatabase(monitor);
     }
 
-    public boolean handleBaselineDatabase() throws PlatformException {
+    private static Path tryGetInstallLocation() {
+        Location l = Platform.getInstallLocation();
+        return l == null ? null : new File(l.getURL().getPath()).toPath();
+    }
+
+    private Path resolveBaselineFile() throws PlatformException {
+        String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
+        if (dbBaselineArchive == null)
+            return null;
+
+        Path baseline = Paths.get(dbBaselineArchive);
+        if (baseline.isAbsolute()) {
+            if (!Files.isRegularFile(baseline))
+                throw new PlatformException("Specified database baseline archive " + baseline
+                        + " does not exist. Cannot initialize workspace database from baseline.");
+            return baseline;
+        }
+
+        // Relative path resolution order:
+        // 1. from the platform "install location"
+        // 2. from working directory
+        Path installLocation = tryGetInstallLocation();
+        if (installLocation != null) {
+            Path installedBaseline = installLocation.resolve(dbBaselineArchive);
+            if (Files.isRegularFile(installedBaseline))
+                return installedBaseline;
+        }
+        if (!Files.isRegularFile(baseline))
+            throw new PlatformException("Specified database baseline archive " + baseline
+                    + " does not exist in either the install location (" + installLocation
+                    + ") or the working directory (" + Paths.get(".").toAbsolutePath()
+                    + "). Cannot initialize workspace database.");
+        return null;
+    }
+
+    private boolean handleBaselineDatabase() throws PlatformException {
         Path workspaceLocation = Platform.getLocation().toFile().toPath();
         Path baselineIndicatorFile = workspaceLocation.resolve(".baselined");
         if (Files.isRegularFile(baselineIndicatorFile)) {
@@ -705,14 +741,10 @@ public class SimanticsPlatform implements LifecycleListener {
             return true;
         }
 
-        String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
-        if (dbBaselineArchive == null)
+        Path baseline = resolveBaselineFile();
+        if (baseline == null)
             return false;
 
-        Path baseline = Paths.get(dbBaselineArchive);
-        if (!Files.isRegularFile(baseline))
-            throw new PlatformException("Specified database baseline archive " + baseline + " does not exist. Cannot initialize workspace database.");
-
         DatabaseBaselines.validateBaselineFile(baseline);
         DatabaseBaselines.validateWorkspaceForBaselineInitialization(workspaceLocation);
         DatabaseBaselines.initializeWorkspaceWithBaseline(baseline, workspaceLocation, baselineIndicatorFile);