Java Collections.synchronizedSortedSet Examples

Java Collections.synchronizedSortedSet - 9 examples found. These are the top rated real world Java examples of java.util.Collections.synchronizedSortedSet extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  @Override
  public void start() {
    try {
      // open the data file
      String location = configuration.location();
      if (location == null || location.trim().length() == 0)
        location = "Infinispan-SingleFileStore";

      file = new File(location, ctx.getCache().getName() + ".dat");
      if (!file.exists()) {
        File dir = file.getParentFile();
        if (!dir.mkdirs() && !dir.exists()) {
          throw log.directoryCannotBeCreated(dir.getAbsolutePath());
        }
      }
      channel = new RandomAccessFile(file, "rw").getChannel();

      // initialize data structures
      entries = newEntryMap();
      freeList = Collections.synchronizedSortedSet(new TreeSet<FileEntry>());

      // check file format and read persistent state if enabled for the cache
      byte[] header = new byte[MAGIC.length];
      if (channel.read(ByteBuffer.wrap(header), 0) == MAGIC.length
          && Arrays.equals(MAGIC, header)) {
        rebuildIndex();
        processFreeEntries();
      } else clear(); // otherwise (unknown file format or no preload) just reset the file

      // Initialize the fragmentation factor
      fragmentationFactor = configuration.fragmentationFactor();
    } catch (Exception e) {
      throw new PersistenceException(e);
    }
  }
Example #2
0
  public void run() throws IOException, InterruptedException {
    new NativeLibraryLoader().load(".");
    CompareToReference2 c2r = new CompareToReference2();
    c2r.init8bit(reference);

    include = new BufferedWriter(new FileWriter(in, false));
    exclude = new BufferedWriter(new FileWriter(out, false));

    for (String chr : c2r.getChromosomes()) {
      includeRegions =
          Collections.synchronizedSortedSet(new TreeSet<Feature>(new RegionComparator()));
      excludeRegions =
          Collections.synchronizedSortedSet(new TreeSet<Feature>(new RegionComparator()));
      int i = 0;
      int chromosomeLength = c2r.getReferenceLength(chr);
      while (i < chromosomeLength - ReAligner.MAX_REGION_LENGTH) {
        int regionStart = i;
        int regionStop = i + ReAligner.MAX_REGION_LENGTH;
        int start = Math.max(regionStart - readLength, 0);
        int stop = Math.min(regionStop + readLength, chromosomeLength - 1);
        String regionBases = c2r.getSequence(chr, start + 1, stop - start);
        Feature region = new Feature(chr, regionStart, regionStop);

        // TODO: Handle other ambiguous bases
        if (!regionBases.contains("N")) {
          threadManager.spawnThread(new EvalRunnable(threadManager, this, region, regionBases));
        } else {

          excludeRegions.add(region);
        }

        i += ReAligner.REGION_OVERLAP;
      }

      threadManager.waitForAllThreadsToComplete();

      // TODO: Because assembly regions are overlapped, there is overlap between final
      // include/exclude output
      outputRegions(include, includeRegions);
      outputRegions(exclude, excludeRegions);
    }

    include.close();
    exclude.close();

    System.out.println("Done.");
  }
Example #3
0
    /** Key report. */
    public static class KeyReport {

      /** Used keys. */
      private final List<String> usedKeys = new ArrayList<String>();

      /** Defined keys. */
      private final Set<String> dfndKeys = Collections.synchronizedSortedSet(new TreeSet<String>());

      /** @return Defined keys. */
      public Set<String> getDefinedKeys() {
        return dfndKeys;
      }

      /**
       * Get used keys.
       *
       * @return Used keys.
       */
      public List<String> getUsedKeys() {
        return usedKeys;
      }
    }
  /**
   * Create a Verse from a human readable string. The opposite of toString(), Given any
   * DistinctPassage v1, and the following <code>
   * DistinctPassage v2 = new DistinctPassage(v1.toString());</code> Then <code>v1.equals(v2);
   * </code> Theoretically, since there are many ways of representing a DistinctPassage as text
   * string comparision along the lines of: <code>v1.toString().equals(v2.toString())</code> could
   * be false. Practically since toString() is standardized this will be true however. We don't need
   * to worry about thread safety in a ctor since we don't exist yet.
   *
   * @param refs A String containing the text of the DistinctPassage
   * @throws NoSuchVerseException If the string is not valid
   */
  protected DistinctPassage(String refs) throws NoSuchVerseException {
    super(refs);

    store = Collections.synchronizedSortedSet(new TreeSet());
    addVerses(refs);
  }
  /** - (void) beginReplicating in CBL_Replicator.m */
  @Override
  @InterfaceAudience.Private
  public void beginReplicating() {
    // If we're still waiting to create the remote db, do nothing now. (This method will be
    // re-invoked after that request finishes; see -maybeCreateRemoteDB above.)

    Log.d(Log.TAG_SYNC, "%s: beginReplicating() called", this);

    // If we're still waiting to create the remote db, do nothing now. (This method will be
    // re-invoked after that request finishes; see maybeCreateRemoteDB() above.)
    if (creatingTarget) {
      Log.d(Log.TAG_SYNC, "%s: creatingTarget == true, doing nothing", this);
      return;
    }

    pendingSequences = Collections.synchronizedSortedSet(new TreeSet<Long>());
    try {
      maxPendingSequence = Long.parseLong(lastSequence);
    } catch (NumberFormatException e) {
      Log.w(Log.TAG_SYNC, "Error converting lastSequence: %s to long.  Using 0", lastSequence);
      maxPendingSequence = new Long(0);
    }

    filter = compilePushReplicationFilter();
    if (filterName != null && filter == null) {
      Log.w(
          Log.TAG_SYNC,
          "%s: No ReplicationFilter registered for filter '%s'; ignoring",
          this,
          filterName);
    }

    // Process existing changes since the last push:
    long lastSequenceLong = 0;
    if (lastSequence != null) {
      lastSequenceLong = Long.parseLong(lastSequence);
    }
    ChangesOptions options = new ChangesOptions();
    options.setIncludeConflicts(true);
    Log.d(Log.TAG_SYNC, "%s: Getting changes since %s", this, lastSequence);
    RevisionList changes = db.changesSince(lastSequenceLong, options, filter, filterParams);
    if (changes.size() > 0) {
      Log.d(Log.TAG_SYNC, "%s: Queuing %d changes since %s", this, changes.size(), lastSequence);
      int remaining = changes.size();
      int size = batcher.getCapacity();
      int start = 0;
      while (remaining > 0) {
        if (size > remaining) size = remaining;
        RevisionList subChanges = new RevisionList(changes.subList(start, start + size));
        batcher.queueObjects(subChanges);
        start += size;
        remaining -= size;
        pauseOrResume();
        waitIfPaused();
      }
    } else {
      Log.d(Log.TAG_SYNC, "%s: No changes since %s", this, lastSequence);
    }

    // Now listen for future changes (in continuous mode):
    if (isContinuous()) {
      observing = true;
      db.addChangeListener(this);
    }
  }
Example #6
0
  public static void main(String[] args) {

    try {

      final Options options = new Options();

      {
        final Option d = new Option("d", "directory", true, "target directory");
        d.setArgName("directory");
        d.setArgs(1);
        d.setRequired(true);
        options.addOption(d);
      }

      {
        final Option ad = new Option("ad", "another-directory", true, "another target directory");
        ad.setArgName("another-directory");
        ad.setArgs(1);
        ad.setRequired(false);
        options.addOption(ad);
      }

      {
        final Option o = new Option("o", "output", true, "output file");
        o.setArgName("file");
        o.setArgs(1);
        o.setRequired(true);
        options.addOption(o);
      }

      {
        final Option s = new Option("s", "size", true, "size");
        s.setArgName("size");
        s.setArgs(1);
        s.setRequired(true);
        options.addOption(s);
      }

      {
        final Option t = new Option("t", "thread", true, "number of threads");
        t.setArgName("thread");
        t.setArgs(1);
        t.setRequired(false);
        options.addOption(t);
      }

      {
        final Option cross =
            new Option(
                "cross", "cross-project-only", true, "whether to detect cross project clones only");
        cross.setArgName("on or off");
        cross.setRequired(false);
        options.addOption(cross);
      }

      {
        final Option lowmem =
            new Option("lowmem", "low-memory-mode", true, "whether to run on the low memory mode");
        lowmem.setArgName("on or off");
        lowmem.setRequired(false);
        options.addOption(lowmem);
      }

      {
        final Option v = new Option("v", "verbose", true, "verbose output");
        v.setArgName("on or off");
        v.setRequired(false);
        options.addOption(v);
      }

      {
        final Option C = new Option("C", "control", true, "use of control dependency");
        C.setArgName("on or off");
        C.setArgs(1);
        C.setRequired(false);
        options.addOption(C);
      }

      {
        final Option D = new Option("D", "data", true, "use of data dependency");
        D.setArgName("on or off");
        D.setArgs(1);
        D.setRequired(false);
        options.addOption(D);
      }

      {
        final Option E = new Option("E", "execution", true, "use of execution dependency");
        E.setArgName("on or off");
        E.setArgs(1);
        E.setRequired(false);
        options.addOption(E);
      }

      {
        final Option M = new Option("M", "merging", true, "merging consecutive similar nodes");
        M.setArgName("on or off");
        M.setArgs(1);
        M.setRequired(false);
        options.addOption(M);
      }

      final CommandLineParser parser = new PosixParser();
      final CommandLine cmd = parser.parse(options, args);

      final File target = new File(cmd.getOptionValue("d"));
      if (!target.exists()) {
        System.err.println("specified directory or file does not exist.");
        System.exit(0);
      }

      final String output = cmd.getOptionValue("o");
      final int SIZE_THRESHOLD = Integer.parseInt(cmd.getOptionValue("s"));
      final int NUMBER_OF_THREADS =
          cmd.hasOption("t") ? Integer.parseInt(cmd.getOptionValue("t")) : 1;

      boolean useOfControl = !cmd.hasOption("C");
      if (!useOfControl) {
        if (cmd.getOptionValue("C").equals("on")) {
          useOfControl = true;
        } else if (cmd.getOptionValue("C").equals("off")) {
          useOfControl = false;
        } else {
          System.err.println("option of \"-C\" must be \"on\" or \"off\".");
        }
      }

      boolean useOfData = !cmd.hasOption("D");
      if (!useOfData) {
        if (cmd.getOptionValue("D").equals("on")) {
          useOfData = true;
        } else if (cmd.getOptionValue("D").equals("off")) {
          useOfData = false;
        } else {
          System.err.println("option of \"-D\" must be \"on\" or \"off\".");
        }
      }

      boolean useOfExecution = !cmd.hasOption("E");
      if (!useOfExecution) {
        if (cmd.getOptionValue("E").equals("on")) {
          useOfExecution = true;
        } else if (cmd.getOptionValue("E").equals("off")) {
          useOfExecution = false;
        } else {
          System.err.println("option of \"-E\" must be \"on\" or \"off\".");
        }
      }

      boolean useOfMerging = !cmd.hasOption("M");
      if (!useOfMerging) {
        if (cmd.getOptionValue("M").equals("on")) {
          useOfMerging = true;
        } else if (cmd.getOptionValue("M").equals("off")) {
          useOfMerging = false;
        } else {
          System.err.println("option of \"-M\" must be \"on\" or \"off\".");
        }
      }

      if (!useOfExecution && useOfMerging) {
        useOfMerging = false;
      }

      boolean crossProjectOnly = false;
      if (cmd.hasOption("cross")) {
        if (cmd.getOptionValue("cross").equals("on")) {
          crossProjectOnly = true;
        } else if (cmd.getOptionValue("cross").equals("off")) {
          crossProjectOnly = false;
        } else {
          System.err.println("option of \"-cross\" must be \"on\" or \"off\".");
        }
      }

      boolean lowMemoryMode = false;
      if (cmd.hasOption("lowmem")) {
        if (cmd.getOptionValue("lowmem").equals("on")) {
          lowMemoryMode = true;
        } else if (cmd.getOptionValue("lowmem").equals("off")) {
          lowMemoryMode = false;
        } else {
          System.err.println("option of \"-lowmem\" must be \"on\" or \"off\".");
        }
      }

      // default verbose level is "off"
      if (cmd.hasOption("v")) {
        if (cmd.getOptionValue("v").equals("on")) {
          Message.setVerbose(true);
        } else if (cmd.getOptionValue("v").equals("off")) {
          Message.setVerbose(false);
        } else {
          System.err.println("option of \"-v\" must be \"on\" or \"off\".");
        }
      }

      File anotherTarget = null;
      if (cmd.hasOption("ad")) {
        anotherTarget = new File(cmd.getOptionValue("ad"));
        if (!anotherTarget.exists()) {
          System.err.println("specified directory or file does not exist.");
          System.exit(0);
        }
      }

      if (crossProjectOnly && anotherTarget == null) {
        System.err.println(
            "detecting cross project only is ON, but no second directory or file has been specified");
        System.exit(0);
      }

      final long time1 = System.nanoTime();
      System.out.print("generating PDGs ... ");
      Message.log("");
      final PDG[] pdgArray;
      {
        final List<File> files = getFiles(target);

        if (anotherTarget != null) {
          files.addAll(getFiles(anotherTarget));
        }

        int count = 0;
        final int numOfFiles = files.size();

        final List<MethodInfo> methods = new ArrayList<MethodInfo>();
        for (final File file : files) {
          Message.log(
              "\t["
                  + (++count)
                  + "/"
                  + numOfFiles
                  + "] building an AST for "
                  + file.getAbsolutePath());

          final CompilationUnit unit = TinyPDGASTVisitor.createAST(file);
          final ASTVisitor visitor;

          if (lowMemoryMode) {
            visitor = new OffsetBasedTinyPDGASTVisitor(file.getAbsolutePath(), unit, methods);
          } else {
            visitor = new TinyPDGASTVisitor(file.getAbsolutePath(), unit, methods);
          }

          unit.accept(visitor);
        }

        long memoryElapsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        System.out.println("MEMORY: " + (memoryElapsed / 1024));

        final SortedSet<PDG> pdgs = Collections.synchronizedSortedSet(new TreeSet<PDG>());
        final CFGNodeFactory cfgNodeFactory = new CFGNodeFactory();
        final PDGNodeFactory pdgNodeFactory = new PDGNodeFactory();
        final Thread[] pdgGenerationThreads = new Thread[NUMBER_OF_THREADS];
        for (int i = 0; i < pdgGenerationThreads.length; i++) {
          pdgGenerationThreads[i] =
              new Thread(
                  new PDGGenerationThread(
                      methods,
                      pdgs,
                      cfgNodeFactory,
                      pdgNodeFactory,
                      useOfControl,
                      useOfData,
                      useOfExecution,
                      useOfMerging,
                      SIZE_THRESHOLD));
          pdgGenerationThreads[i].start();
        }
        for (final Thread thread : pdgGenerationThreads) {
          try {
            thread.join();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        pdgArray = pdgs.toArray(new PDG[0]);
      }
      System.out.print("done: ");
      final long time2 = System.nanoTime();
      printTime(time2 - time1);

      System.out.print("calculating hash values ... ");
      Message.log("");
      final SortedMap<PDG, SortedMap<PDGNode<?>, Integer>> mappingPDGToPDGNodes =
          Collections.synchronizedSortedMap(new TreeMap<PDG, SortedMap<PDGNode<?>, Integer>>());
      final SortedMap<PDG, SortedMap<PDGEdge, Integer>> mappingPDGToPDGEdges =
          Collections.synchronizedSortedMap(new TreeMap<PDG, SortedMap<PDGEdge, Integer>>());
      {
        final Thread[] hashCalculationThreads = new Thread[NUMBER_OF_THREADS];
        for (int i = 0; i < hashCalculationThreads.length; i++) {
          hashCalculationThreads[i] =
              new Thread(
                  new HashCalculationThread(pdgArray, mappingPDGToPDGNodes, mappingPDGToPDGEdges));
          hashCalculationThreads[i].start();
        }
        for (final Thread thread : hashCalculationThreads) {
          try {
            thread.join();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
      System.out.print("done: ");
      final long time3 = System.nanoTime();
      printTime(time3 - time2);

      System.out.print("detecting clone pairs ... ");
      Message.log("");
      final SortedSet<ClonePairInfo> clonepairs =
          Collections.synchronizedSortedSet(new TreeSet<ClonePairInfo>());
      {
        int numIgnored = 0;
        final List<PDGPairInfo> pdgpairs = new ArrayList<PDGPairInfo>();
        Message.log("\tmaking PDG pairs ... ");
        for (int i = 0; i < pdgArray.length; i++) {
          for (int j = i + 1; j < pdgArray.length; j++) {
            final PDG pdg1 = pdgArray[i];
            final PDG pdg2 = pdgArray[j];

            if (!crossProjectOnly || isCrossProject(pdg1, pdg2, target, anotherTarget)) {
              pdgpairs.add(new PDGPairInfo(pdgArray[i], pdgArray[j]));
            } else {
              // Message.log("\t\tignore the PDG pair \"" +
              // pdg1.unit.name + " in "
              // + pdg1.unit.path + "\" and \""
              // + pdg2.unit.name + " in " + pdg2.unit.path
              // + "\"");
              numIgnored++;
            }
          }
        }
        Message.log("\tdone: the number of ignored PDG pairs is " + numIgnored);

        final PDGPairInfo[] pdgpairArray = pdgpairs.toArray(new PDGPairInfo[0]);
        final Thread[] slicingThreads = new Thread[NUMBER_OF_THREADS];
        for (int i = 0; i < slicingThreads.length; i++) {
          slicingThreads[i] =
              new Thread(
                  new SlicingThread(
                      pdgpairArray,
                      pdgArray,
                      mappingPDGToPDGNodes,
                      mappingPDGToPDGEdges,
                      clonepairs,
                      SIZE_THRESHOLD,
                      crossProjectOnly));
          slicingThreads[i].start();
        }
        for (final Thread thread : slicingThreads) {
          try {
            thread.join();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
      System.out.print("done: ");
      final long time4 = System.nanoTime();
      printTime(time4 - time3);

      System.out.print("writing to a file ... ");
      final Writer writer = new BellonWriter(output, clonepairs);
      writer.write();
      System.out.print("done: ");
      final long time5 = System.nanoTime();
      printTime(time5 - time4);

      System.out.print("total elapsed time: ");
      printTime(time5 - time1);

      System.out.print("number of comparisons: ");
      printNumberOfComparison(Slicing.getNumberOfComparison());

    } catch (Exception e) {
      System.err.println(e.getMessage());
      System.exit(0);
    }
  }