Search in sources :

Example 1 with FastBufferedReader

use of org.python.pydev.core.FastBufferedReader in project Pydev by fabioz.

the class DiskCacheTest method testDiskCacheWithZipModulesKey.

public void testDiskCacheWithZipModulesKey() throws Exception {
    DiskCache cache = new DiskCache(new File(baseDir, ".cache"), "_test_disk_cache");
    cache.add(new CompleteIndexKey(new ModulesKey("mod1", new File(baseDir, "f1")), 100));
    cache.add(new CompleteIndexKey(new ModulesKey("modnull", null), 100));
    cache.add(new CompleteIndexKey(new ModulesKeyForZip("mod2", new File(baseDir, "my.zip"), "path", true), 100));
    cache.add(new CompleteIndexKey(new ModulesKeyForZip("mod3", new File(baseDir, "my.zip"), "path2", false), 100));
    FastStringBuffer tempBuf = new FastStringBuffer();
    cache.writeTo(tempBuf);
    FastBufferedReader reader = new FastBufferedReader(new StringReader(tempBuf.toString()));
    // 
    FastStringBuffer line = reader.readLine();
    assertEquals(line.toString(), "-- START DISKCACHE_" + DiskCache.VERSION);
    ObjectsPoolMap objectsPoolMap = new ObjectsPoolMap();
    DiskCache loadFrom = DiskCache.loadFrom(reader, objectsPoolMap);
    assertEquals(cache.keys(), loadFrom.keys());
    assertEquals(cache.getFolderToPersist(), loadFrom.getFolderToPersist());
    CompleteIndexKey mod = cache.keys().get(new CompleteIndexKey("mod2"));
    ModulesKeyForZip zip = (ModulesKeyForZip) mod.key;
    assertEquals(zip.zipModulePath, "path");
    mod = loadFrom.keys().get(new CompleteIndexKey("mod2"));
    zip = (ModulesKeyForZip) mod.key;
    assertEquals(zip.zipModulePath, "path");
    mod = loadFrom.keys().get(new CompleteIndexKey("modnull"));
    assertNull(mod.key.file);
    mod = loadFrom.keys().get(new CompleteIndexKey("mod3"));
    zip = (ModulesKeyForZip) mod.key;
    assertEquals(zip.zipModulePath, "path2");
    assertTrue(zip.isFile);
}
Also used : FastBufferedReader(org.python.pydev.core.FastBufferedReader) ObjectsPoolMap(org.python.pydev.core.ObjectsInternPool.ObjectsPoolMap) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) StringReader(java.io.StringReader) ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) File(java.io.File)

Example 2 with FastBufferedReader

use of org.python.pydev.core.FastBufferedReader in project Pydev by fabioz.

the class AbstractAdditionalDependencyInfo method loadContentsFromFile.

private Object loadContentsFromFile(File file, IPythonNature nature) throws FileNotFoundException, IOException, MisconfigurationException {
    FileInputStream fileInputStream = new FileInputStream(file);
    try {
        // Timer timer = new Timer();
        // X is the version
        String expected = "-- VERSION_" + AbstractAdditionalTokensInfo.version;
        InputStreamReader reader = new InputStreamReader(fileInputStream);
        FastBufferedReader bufferedReader = new FastBufferedReader(reader);
        FastStringBuffer string = bufferedReader.readLine();
        ObjectsPoolMap objectsPoolMap = new ObjectsInternPool.ObjectsPoolMap();
        if (string != null && string.startsWith("-- VERSION_")) {
            Tuple<Tuple3<Object, Object, Object>, Object> tupWithResults = new Tuple<Tuple3<Object, Object, Object>, Object>(new Tuple3<Object, Object, Object>(null, null, null), null);
            Tuple3<Object, Object, Object> superTupWithResults = tupWithResults.o1;
            // tupWithResults.o2 = DiskCache
            if (string.toString().equals(expected)) {
                // OK, proceed with new I/O format!
                try {
                    try {
                        FastStringBuffer line;
                        Map<Integer, String> dictionary = null;
                        FastStringBuffer tempBuf = new FastStringBuffer(1024);
                        while ((line = bufferedReader.readLine()) != null) {
                            if (line.startsWith("-- ")) {
                                if (line.startsWith("-- START TREE 1")) {
                                    superTupWithResults.o1 = TreeIO.loadTreeFrom(bufferedReader, dictionary, tempBuf.clear(), objectsPoolMap, nature);
                                } else if (line.startsWith("-- START TREE 2")) {
                                    superTupWithResults.o2 = TreeIO.loadTreeFrom(bufferedReader, dictionary, tempBuf.clear(), objectsPoolMap, nature);
                                } else if (line.startsWith("-- START DICTIONARY")) {
                                    dictionary = TreeIO.loadDictFrom(bufferedReader, tempBuf.clear(), objectsPoolMap);
                                } else if (line.startsWith("-- START DISKCACHE")) {
                                    if (!line.startsWith("-- START DISKCACHE_" + DiskCache.VERSION)) {
                                        throw new RuntimeException("Disk cache version changed");
                                    }
                                    tupWithResults.o2 = DiskCache.loadFrom(bufferedReader, objectsPoolMap);
                                } else if (line.startsWith("-- VERSION_")) {
                                    if (!line.endsWith(String.valueOf(AbstractAdditionalTokensInfo.version))) {
                                        throw new RuntimeException("Expected the version to be: " + AbstractAdditionalTokensInfo.version + " Found: " + line);
                                    }
                                } else if (line.startsWith("-- END TREE")) {
                                // just skip it in this situation.
                                } else {
                                    throw new RuntimeException("Unexpected line: " + line);
                                }
                            }
                        }
                    } finally {
                        bufferedReader.close();
                    }
                } finally {
                    reader.close();
                }
                restoreSavedInfo(tupWithResults);
                // timer.printDiff("Time taken");
                return tupWithResults;
            } else {
                throw new RuntimeException("Version does not match. Found: " + string + ". Expected: " + expected);
            }
        } else {
            // Try the old way of loading it (backward compatibility).
            fileInputStream.close();
            // Timer timer2 = new Timer();
            Object tupWithResults = IOUtils.readFromFile(file);
            restoreSavedInfo(tupWithResults);
            // timer2.printDiff("IOUtils time");
            // Save in new format!
            save();
            return tupWithResults;
        }
    } finally {
        try {
            fileInputStream.close();
        } catch (Exception e) {
        // Ignore error closing.
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) FastBufferedReader(org.python.pydev.core.FastBufferedReader) ObjectsPoolMap(org.python.pydev.core.ObjectsInternPool.ObjectsPoolMap) Tuple3(org.python.pydev.shared_core.structure.Tuple3) Tuple(org.python.pydev.shared_core.structure.Tuple)

Aggregations

FastBufferedReader (org.python.pydev.core.FastBufferedReader)2 ObjectsPoolMap (org.python.pydev.core.ObjectsInternPool.ObjectsPoolMap)2 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 ModulesKey (org.python.pydev.core.ModulesKey)1 ModulesKeyForZip (org.python.pydev.core.ModulesKeyForZip)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1 Tuple3 (org.python.pydev.shared_core.structure.Tuple3)1