protected void saveTo(OutputStreamWriter writer, FastStringBuffer tempBuf, File pathToSave)
      throws IOException {
    synchronized (lock) {
      if (DEBUG_ADDITIONAL_INFO) {
        System.out.println(
            "Saving info "
                + this.getClass().getName()
                + " to file (size = "
                + getAllTokens().size()
                + ") "
                + pathToSave);
      }

      Map<String, Integer> dictionary = new HashMap<String, Integer>();
      tempBuf.append("-- START TREE 1\n");
      TreeIO.dumpTreeToBuffer(this.topLevelInitialsToInfo, tempBuf, dictionary);

      tempBuf.append("-- START TREE 2\n");
      TreeIO.dumpTreeToBuffer(this.innerInitialsToInfo, tempBuf, dictionary);

      FastStringBuffer buf2 = new FastStringBuffer(50 * (dictionary.size() + 4));
      TreeIO.dumpDictToBuffer(dictionary, buf2);

      // Write the dictionary before the actual trees.
      writer.write(buf2.getInternalCharsArray(), 0, buf2.length());
      buf2 = null;

      // Note: tried LZFFileInputStream from https://github.com/ning/compress
      // and Snappy from https://github.com/dain/snappy checking to see if by writing less we'd
      // get a better time but it got a bit slower (gzip was slowest, then snappy and the faster was
      // LZFFileInputStream)
      writer.write(tempBuf.getInternalCharsArray(), 0, tempBuf.length());
    }
  }
  public List<IInfo> addAstInfo(ModulesKey key, boolean generateDelta) throws Exception {
    boolean isZipModule = key instanceof ModulesKeyForZip;
    ModulesKeyForZip modulesKeyForZip = null;
    if (isZipModule) {
      modulesKeyForZip = (ModulesKeyForZip) key;
    }

    Object doc;
    if (isZipModule) {
      doc =
          FileUtilsFileBuffer.getCustomReturnFromZip(
              modulesKeyForZip.file, modulesKeyForZip.zipModulePath, null);

    } else {
      doc = FileUtilsFileBuffer.getCustomReturnFromFile(key.file, true, null);
    }

    char[] charArray;
    int len;
    if (doc instanceof IDocument) {
      IDocument document = (IDocument) doc;
      charArray = document.get().toCharArray();
      len = charArray.length;

    } else if (doc instanceof FastStringBuffer) {
      FastStringBuffer fastStringBuffer = (FastStringBuffer) doc;
      // In this case, we can actually get the internal array without doing any copies (and just
      // specifying the len).
      charArray = fastStringBuffer.getInternalCharsArray();
      len = fastStringBuffer.length();

    } else if (doc instanceof String) {
      String str = (String) doc;
      charArray = str.toCharArray();
      len = charArray.length;

    } else if (doc instanceof char[]) {
      charArray = (char[]) doc;
      len = charArray.length;

    } else {
      throw new RuntimeException("Don't know how to handle: " + doc + " -- " + doc.getClass());
    }

    SimpleNode node = FastDefinitionsParser.parse(charArray, key.file.getName(), len);
    if (node == null) {
      return null;
    }

    return addAstInfo(node, key, generateDelta);
  }
  protected void save(File persistingLocation) {
    try {
      FileOutputStream stream = new FileOutputStream(persistingLocation);
      OutputStreamWriter writer = new OutputStreamWriter(stream);
      try {
        FastStringBuffer tempBuf = new FastStringBuffer();
        tempBuf.append("-- VERSION_");
        tempBuf.append(AbstractAdditionalTokensInfo.version);
        tempBuf.append('\n');
        writer.write(tempBuf.getInternalCharsArray(), 0, tempBuf.length());
        tempBuf.clear();

        saveTo(writer, tempBuf, persistingLocation);
      } finally {
        try {
          writer.close();
        } finally {
          stream.close();
        }
      }
    } catch (Exception e) {
      Log.log(e);
    }
  }
Exemplo n.º 4
0
  public static PyPublicTreeMap<String, Set<IInfo>> loadTreeFrom(
      final FastBufferedReader reader,
      final Map<Integer, String> dictionary,
      FastStringBuffer buf,
      ObjectsPoolMap objectsPoolMap,
      IPythonNature nature)
      throws IOException {
    PyPublicTreeMap<String, Set<IInfo>> tree = new PyPublicTreeMap<String, Set<IInfo>>();
    final int size = StringUtils.parsePositiveInt(reader.readLine());

    try {

      final Entry[] entries = new Entry[size];
      // each line is something as: cub|CubeColourDialog!13&999@CUBIC!263@cube!202&999@
      // note: the path (2nd int in record) is optional
      for (int iEntry = 0; iEntry < size; iEntry++) {
        buf.clear();
        FastStringBuffer line = reader.readLine();
        if (line == null || line.startsWith("-- ")) {
          throw new RuntimeException("Unexpected line: " + line);
        }
        char[] internalCharsArray = line.getInternalCharsArray();
        int length = line.length();
        String key = null;
        String infoName = null;
        String path = null;

        int i = 0;

        OUT:
        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '|':
              key = ObjectsInternPool.internLocal(objectsPoolMap, buf.toString());
              buf.clear();
              i++;
              break OUT;
            default:
              buf.appendResizeOnExc(c);
          }
        }

        int hashSize = 0;
        OUT2:
        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '|':
              hashSize = StringUtils.parsePositiveInt(buf);
              buf.clear();
              i++;
              break OUT2;
            default:
              buf.appendResizeOnExc(c);
          }
        }
        HashSet<IInfo> set = new HashSet<IInfo>(hashSize);

        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '!':
              infoName = ObjectsInternPool.internLocal(objectsPoolMap, buf.toString());
              buf.clear();
              break;

            case '&':
              path = dictionary.get(StringUtils.parsePositiveInt(buf));
              buf.clear();
              break;

            case '@':
              int dictKey = StringUtils.parsePositiveInt(buf);
              byte type = (byte) dictKey;
              type &=
                  0x07; // leave only the 3 least significant bits there (this is the type -- value
              // from 0 - 8).

              dictKey =
                  (dictKey
                      >> 3); // the entry in the dict doesn't have the least significant bits there.
              buf.clear();
              String moduleDeclared = dictionary.get(dictKey);
              if (moduleDeclared == null) {
                throw new AssertionError("Unable to find key: " + dictKey);
              }
              if (infoName == null) {
                throw new AssertionError("Info name may not be null. Line: " + line);
              }
              switch (type) {
                case IInfo.CLASS_WITH_IMPORT_TYPE:
                  set.add(new ClassInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.METHOD_WITH_IMPORT_TYPE:
                  set.add(new FuncInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
                  set.add(new AttrInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.NAME_WITH_IMPORT_TYPE:
                  set.add(new NameInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.MOD_IMPORT_TYPE:
                  set.add(new ModInfo(infoName, false, nature));
                  break;
                default:
                  Log.log("Unexpected type: " + type);
              }
              break;
            default:
              buf.appendResizeOnExc(c);
          }
        }

        entries[iEntry] = new MapEntry(key, set);
      }

      tree.buildFromSorted(
          size,
          new Iterator() {
            private int iNext;

            @Override
            public boolean hasNext() {
              return iNext > size;
            }

            @Override
            public Object next() {
              Object o = entries[iNext];
              iNext++;
              return o;
            }

            @Override
            public void remove() {}
          },
          null,
          null);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    return tree;
  }