Exemplo n.º 1
1
  /** UnParse a file. If you want a dump then give -out somefile. */
  private static void unparse(
      List<String> args, Option<String> out, boolean _unqualified, boolean _unmangle)
      throws UserError, InterruptedException, IOException {
    if (args.size() == 0) {
      throw new UserError("The unparse command needs a file to unparse.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());
    boolean unqualified = _unqualified;
    boolean unmangle = _unmangle;

    if (s.startsWith("-")) {
      if (s.equals("-unqualified")) {
        unqualified = true;
      } else if (s.equals("-unmangle")) {
        unmangle = true;
      } else if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-out") && !rest.isEmpty()) {
        out = Option.<String>some(rest.get(0));
        rest = rest.subList(1, rest.size());
      } else invalidFlag(s, "unparse");

      unparse(rest, out, unqualified, unmangle);
    } else {
      unparse(s, out, unqualified, unmangle);
    }
  }
Exemplo n.º 2
1
  /** Compile a file. If you want a dump then give -out somefile. */
  public static int compilerPhases(List<String> args, Option<String> out, String phase)
      throws UserError, InterruptedException, IOException, RepositoryError {
    int return_code = 0;
    if (args.size() == 0) {
      throw new UserError("The " + phase + " command must be given a file.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-out") && !rest.isEmpty()) {
        out = Option.<String>some(rest.get(0));
        rest = rest.subList(1, rest.size());
      } else if (s.equals("-compiler-lib")) {
        WellKnownNames.useCompilerLibraries();
        Types.useCompilerLibraries();
      } else if (s.equals("-typecheck-java")) {
        setScala(false);
      } else if (s.equals("-coercion")) {
        setTestCoercion(true);
      } else invalidFlag(s, phase);
      return_code = compilerPhases(rest, out, phase);
    } else {
      return_code = compileWithErrorHandling(s, out, false);
    }
    return return_code;
  }
Exemplo n.º 3
1
  /**
   * Prompt to confirm that the user wants to delete the JShellItem from the file system. If so,
   * remove it. If -f is supplied, do not prompt and confirm. If the root directory is specified,
   * its contents but not itself will be subject to deletion.
   *
   * @param paths the JShellItem to be removed.
   */
  public void rm(List<String> paths) throws Exception {
    // Loop through the user inputs.
    for (String path : paths) {
      // Initialize the current JShellItem.
      JShellItem item = getItemAtPath(path, 0);
      if (item == null) {
        System.out.printf("%s: does not exist.\n", path);
      } else {
        // Keep track of the size of that JShellItem.
        int size = item.getSize();

        if (size > 0) {
          List<String> recursiveList = recurseOnPath(path, true);
          rm(recursiveList.subList(0, size + 1));

          if (item.getSize() == 0) {
            rm(recursiveList.subList(size + 1, size + 2));
          }

        } else {
          if (item.getPath().equals("/")) {
            return;
          }
          // Check the -f option.
          if (!currentOptions_.equals("f")) {
            System.out.printf(
                "Really remove %s from %s? (y/n) ",
                item.getName(), item.getParentDirectory().getPath());

            // Read the user input.
            ArrayList<String> in = readInput();

            // If 'y'
            if (in.toString().equals("[y]")) {
              item.getParentDirectory().removeItem(item);
              // if not 'n', ask again.
            } else if (!in.toString().equals("[n]")) {
              List<String> tempStore = new ArrayList<String>();
              tempStore.add(path);
              rm(tempStore);
            }

            // If -f is specified.
          } else {
            item.getParentDirectory().removeItem(item);
          }
        }
      }
    }
  }
Exemplo n.º 4
1
  public void writeTasks(OutputStream output) {
    try {
      output.write("[\n".getBytes());
      boolean needComma = false;
      File[] files = tasksDirectory.listFiles((FileFilter) FileFilterUtils.directoryFileFilter());
      List<Integer> numbers = new ArrayList<>();
      for (File directory : files) {
        try {
          numbers.add(Integer.valueOf(directory.getName()));
        } catch (Throwable ignored) {

        }
      }
      numbers.sort(Comparator.<Integer>reverseOrder());
      numbers = numbers.subList(0, Math.min(100, numbers.size()));
      for (int taskId : numbers) {
        File infoFile = new File(new File(tasksDirectory, String.valueOf(taskId)), "info.json");
        if (!infoFile.exists()) {
          continue;
        }
        if (needComma) {
          output.write(",\n".getBytes());
        } else {
          needComma = true;
        }
        try (FileInputStream fis = new FileInputStream(infoFile)) {
          IOUtils.copy(fis, output);
        }
      }
      output.write("]\n".getBytes());
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Exemplo n.º 5
0
 private static <T> Pair<List<T>, List<T>> splitData(List<T> original, double splitForSecond) {
   List<T> first = new ArrayList<>();
   List<T> second = new ArrayList<>();
   if (splitForSecond > 0.0) {
     Collections.shuffle(original, new Random(0L));
     int numFirst = (int) ((1.0 - splitForSecond) * original.size());
     first.addAll(original.subList(0, numFirst));
     second.addAll(original.subList(numFirst, original.size()));
   } else {
     first.addAll(original);
     // second stays empty
   }
   return Tuples.pair(first, second);
 }
  // Deze method maakt alle studenten aan
  public void getStudents() {
    try {
      BufferedReader csvStudentGegevens =
          new BufferedReader(new FileReader("resources/studenten_roostering.csv"));
      String student;
      csvStudentGegevens.readLine();

      while ((student = csvStudentGegevens.readLine()) != null) {
        List<String> gegevens = Arrays.asList(student.split(";"));
        String lastName = gegevens.get(0);
        String firstName = gegevens.get(1);
        int studentNumber = Integer.parseInt(gegevens.get(2));
        List<String> studentCourses = gegevens.subList(3, gegevens.size());
        List<Activity> studentActivities = new ArrayList<>();
        Student newStudent = new Student(lastName, firstName, studentNumber, studentCourses);
        students.add(newStudent);

        // Voeg student toe aan course
        for (int i = 0; i < courses.size(); i++) {
          Course course = courses.get(i);
          if (studentCourses.contains(course.name)) {
            course.courseStudents.add(newStudent);
          }
        }
      }
      csvStudentGegevens.close();
    } catch (IOException e) {
      System.out.println("File Read Error Students");
    }
  }
Exemplo n.º 7
0
  static <T> List<List<T>> combinations(List<T> list, int n) {

    List<List<T>> result;

    if (list.size() <= n) {

      result = new ArrayList<List<T>>();
      result.add(new ArrayList<T>(list));

    } else if (n <= 0) {

      result = new ArrayList<List<T>>();
      result.add(new ArrayList<T>());

    } else {

      List<T> sublist = list.subList(1, list.size());

      result = combinations(sublist, n);

      for (List<T> alist : combinations(sublist, n - 1)) {
        List<T> thelist = new ArrayList<T>(alist);
        thelist.add(list.get(0));
        result.add(thelist);
      }
    }

    return result;
  }
Exemplo n.º 8
0
  private List<String> trimMovieActors(List<String> acts, int trim) {

    if (trim >= acts.size()) {
      return acts;
    }

    return acts.subList(0, trim);
  }
Exemplo n.º 9
0
  private static void walkTests(List<String> args, boolean verbose)
      throws UserError, IOException, Throwable {
    boolean _verbose = verbose;
    if (args.size() == 0) {
      throw new UserError("Need a file to run through the Fortress interpreter.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-verbose")) {
        _verbose = true;
      } else invalidFlag(s, "test");
      walkTests(rest, _verbose);
    } else {
      for (String file : args) {
        try {
          Iterable<? extends StaticError> errors = IterUtil.empty();
          try {
            if (!isComponent(file)) throw new UserError(file + " is not a component file.");
            APIName name = NodeUtil.apiName(file);
            Path path = sourcePath(file, name);
            GraphRepository bcr = specificInterpreterRepository(path);
            ComponentIndex cu = bcr.getLinkedComponent(name);
            Driver.runTests(bcr, cu, _verbose);
          } catch (Throwable th) {
            // TODO FIXME what is the proper treatment of errors/exceptions etc.?
            if (th instanceof FortressException) {
              FortressException pe = (FortressException) th;
              if (pe.getStaticErrors() != null) errors = pe.getStaticErrors();
            }
            if (th instanceof RuntimeException) throw (RuntimeException) th;
            if (th instanceof Error) throw (Error) th;
            throw new WrappedException(th, Debug.stackTraceOn());
          }

          for (StaticError error : errors) {
            System.err.println(error);
          }
          // If there are no errors,
          // all components will have been written to disk
          // by the CacheBasedRepository.
        } catch (StaticError e) {
          System.err.println(e);
          if (Debug.stackTraceOn()) {
            e.printStackTrace();
          }
        } catch (RepositoryError e) {
          System.err.println(e.getMessage());
        } catch (FortressException e) {
          failureBoilerplate(e);
          System.exit(1);
        }
      }
    }
  }
  /** subList throws IndexOutOfBoundsException when the second index is lower then the first */
  public void testSubList3_IndexOutOfBoundsException() {
    try {
      List c = emptyArray();
      c.subList(3, 1);

      shouldThrow();
    } catch (IndexOutOfBoundsException e) {
    }
  }
 /** subList throws an IndexOutOfBoundsException on a too high index */
 public void testSubList2_IndexOutOfBoundsException() {
   try {
     List c = emptyArray();
     c.add("asdasd");
     c.subList(1, 100);
     shouldThrow();
   } catch (IndexOutOfBoundsException e) {
   }
 }
  /** sublists contains elements at indexes offset from their base */
  public void testSubList() {
    List a = populatedArray(10);
    assertTrue(a.subList(1, 1).isEmpty());
    for (int j = 0; j < 9; ++j) {
      for (int i = j; i < 10; ++i) {
        List b = a.subList(j, i);
        for (int k = j; k < i; ++k) {
          assertEquals(new Integer(k), b.get(k - j));
        }
      }
    }

    List s = a.subList(2, 5);
    assertEquals(3, s.size());
    s.set(2, m1);
    assertEquals(a.get(4), m1);
    s.clear();
    assertEquals(7, a.size());
  }
Exemplo n.º 13
0
  /**
   * Parse a file. If the file parses ok it will say "Ok". If you want a dump then give -out
   * somefile.
   */
  private static int parse(List<String> args, Option<String> out)
      throws UserError, InterruptedException, IOException {
    if (args.size() == 0) {
      throw new UserError("Need a file to parse");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-out") && !rest.isEmpty()) {
        out = Option.<String>some(rest.get(0));
        rest = rest.subList(1, rest.size());
      } else invalidFlag(s, "parse");

      return parse(rest, out);
    } else {
      return parse(s, out);
    }
  }
  /**
   * Tests offset and limit clauses for query.
   *
   * @throws Exception If failed.
   */
  public void testOffsetLimit() throws Exception {
    IgniteCache<Integer, Integer> c =
        ignite(0).getOrCreateCache(cacheConfig("ints", true, Integer.class, Integer.class));

    try {
      List<Integer> res = new ArrayList<>();

      Random rnd = new GridRandom();

      for (int i = 0; i < 10; i++) {
        int val = rnd.nextInt(100);

        c.put(i, val);
        res.add(val);
      }

      Collections.sort(res);

      String qry = "select _val from Integer order by _val ";

      assertEqualsCollections(res, columnQuery(c, qry));
      assertEqualsCollections(res.subList(0, 0), columnQuery(c, qry + "limit ?", 0));
      assertEqualsCollections(res.subList(0, 3), columnQuery(c, qry + "limit ?", 3));
      assertEqualsCollections(res.subList(0, 9), columnQuery(c, qry + "limit ? offset ?", 9, 0));
      assertEqualsCollections(res.subList(3, 7), columnQuery(c, qry + "limit ? offset ?", 4, 3));
      assertEqualsCollections(res.subList(7, 9), columnQuery(c, qry + "limit ? offset ?", 2, 7));
      assertEqualsCollections(res.subList(8, 10), columnQuery(c, qry + "limit ? offset ?", 2, 8));
      assertEqualsCollections(res.subList(9, 10), columnQuery(c, qry + "limit ? offset ?", 1, 9));
      assertEqualsCollections(res.subList(10, 10), columnQuery(c, qry + "limit ? offset ?", 1, 10));
      assertEqualsCollections(
          res.subList(9, 10), columnQuery(c, qry + "limit ? offset abs(-(4 + ?))", 1, 5));
    } finally {
      c.destroy();
    }
  }
Exemplo n.º 15
0
  /**
   * Return the names of the contained items, if path leads to a Directory. Or, return the path
   * provided, if it leads to a File.
   *
   * @param paths a list of Strings containing the desired paths.
   * @return a String containing the relevant contents.
   */
  public String ls(List<String> paths) throws Exception {
    // If ls is called without parameters.
    if (paths.isEmpty()) {
      if (!currentOptions_.equals("R")) {
        if (currentDirectory_.getSize() == 0) {
          return "";
        } else {
          return currentDirectory_.ls().substring(2);
        }
      }
      paths.add(currentDirectory_.getPath());
      return ls(paths);

      // If ls is called with paramters.
    } else {
      String lsOutput = "";
      for (int i = 0; i < paths.size(); i++) {
        JShellItem target = getItemAtPath(paths.get(i), 0);
        if (!lsOutput.isEmpty()) {
          lsOutput += "\n";
        }
        lsOutput += paths.get(i);
        if (target == null) {
          lsOutput += ": No such file or directory";
        } else {

          if (target instanceof Directory
              && currentOptions_.equals("R")
              && ((Directory) target).getNumDirectories() > 0
              && !(target instanceof DirectoryAlias)) {
            List<String> recursivePath = recurseOnPath(paths.get(i), false);
            Collections.reverse(recursivePath);
            lsOutput += target.ls();
            lsOutput += "\n";
            lsOutput += ls(recursivePath.subList(1, recursivePath.size()));
          } else {
            // list paths separately
            lsOutput += target.ls();

            // if there are more paths, print a blank line
            if (i < paths.size() - 1) {
              lsOutput += "\n";
            }
            // notify user if there is an error in the path
            // specified
          }
        }
      }
      return lsOutput;
    }
  }
Exemplo n.º 16
0
  /** Automatically generate an API from a component. */
  private static void api(List<String> args, Option<String> out, Option<String> prepend)
      throws UserError, InterruptedException, IOException {
    if (args.size() == 0) {
      throw new UserError("Need a file to generate an API.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-out") && !rest.isEmpty()) {
        out = Option.<String>some(rest.get(0));
        rest = rest.subList(1, rest.size());
      } else if (s.equals("-prepend") && !rest.isEmpty()) {
        prepend = Option.<String>some(rest.get(0));
        rest = rest.subList(1, rest.size());
      } else invalidFlag(s, "api");
      api(rest, out, prepend);
    } else {
      api(s, out, prepend);
    }
  }
Exemplo n.º 17
0
  /** Link compiled components implementing APIs imported by the given component. */
  private static int junit(List<String> args) throws UserError, IOException {
    if (args.size() == 0) {
      throw new UserError("Need a file to run junit tests.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else invalidFlag(s, "junit");
      return junit(rest);
    }

    junit.textui.TestRunner.run(FileTests.suiteFromListOfFiles(args, "", "", "", false, false));

    return 0;
  }
Exemplo n.º 18
0
  /** Compare results of two components. */
  private static void compare(List<String> args)
      throws UserError, InterruptedException, IOException, Throwable {
    if (args.size() == 0) {
      throw new UserError("Need files to compare the results.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else invalidFlag(s, "compare");

      compare(rest);
    } else {
      if (args.size() == 1) throw new UserError("Need one more file to compare the results.");
      compare(s, args.get(1));
    }
  }
Exemplo n.º 19
0
  public static List<File> createTiffFiles(List<IIOImage> imageList, int index, int dpiX, int dpiY)
      throws IOException {
    List<File> tiffFiles = new ArrayList<File>();

    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);

    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    ImageWriter writer = writers.next();

    if (writer == null) {
      throw new RuntimeException(
          "Need to install JAI Image I/O package.\nhttps://jai-imageio.dev.java.net");
    }

    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);

    // all if index == -1; otherwise, only index-th
    for (IIOImage oimage : (index == -1 ? imageList : imageList.subList(index, index + 1))) {
      if (dpiX != 0 && dpiY != 0) {
        // Get the default image metadata.
        ImageTypeSpecifier imageType =
            ImageTypeSpecifier.createFromRenderedImage(oimage.getRenderedImage());
        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
        imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
        oimage.setMetadata(imageMetadata);
      }

      File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
      ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
      writer.setOutput(ios);
      writer.write(streamMetadata, oimage, tiffWriteParam);
      ios.close();
      tiffFiles.add(tiffFile);
    }
    writer.dispose();

    return tiffFiles;
  }
Exemplo n.º 20
0
  /** Link compiled components implementing APIs imported by the given component. */
  private static int link(List<String> args) throws UserError, IOException {
    if (args.size() == 0) {
      throw new UserError("Need a file to link.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else invalidFlag(s, "link");
      return link(rest);
    } else {
      if (!s.endsWith(".fss")) {
        s = s + ".fss";
      }

      return compileWithErrorHandling(s, Option.<String>none(), true);
    }
  }
Exemplo n.º 21
0
  /** Run a file through the Fortress interpreter. */
  private static void walk(List<String> args) throws UserError, IOException, Throwable {
    if (args.size() == 0) {
      throw new UserError("Need a file to run through the Fortress interpreter.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-compiler-lib")) {
        WellKnownNames.useCompilerLibraries();
        Types.useCompilerLibraries();
      } else invalidFlag(s, "walk");

      walk(rest);
    } else {
      walk(s, rest);
    }
  }
Exemplo n.º 22
0
  /**
   * Prints the path of the files specified by the paths argument that contain a string that matches
   * regex, followed by the particular line.
   *
   * @param regex is a String regular expression.
   * @param paths is a List of paths.
   */
  public String grep(String regex, List<String> paths) throws Exception {
    if (regex.startsWith("\"")) {
      int i = 0;
      for (i = 0; i < paths.size() && !regex.endsWith("\""); i++) regex += " " + paths.get(i);
      regex = regex.substring(1, regex.length() - 1);
      paths = paths.subList(i, paths.size());
    }
    if (paths.isEmpty()) throw new Exception("Usage: grep [OPTION]... PATTERN [FILE]...");
    String results = "";

    for (String path : paths) {

      JShellItem item = getItemAtPath(path, 0);
      if (item.getSize() > 0 && currentOptions_.equals("R")) {

        results += grep(regex, recurseOnPath(path, true).subList(0, item.getSize()));
      } else if (item instanceof File) {
        String content = ((File) item).getContent();
        if (content.contains(regex)) {
          if (!results.isEmpty()) results += "\n";
          results += item.getPath() + ":\n";
          for (String line : content.split("\\n")) {
            if (line.contains(regex)) {
              results += line + "\n";
            }
          }
          results = results.substring(0, results.length() - 1);
        }
      } else if (item instanceof Directory && !currentOptions_.equals("R")) {
        results += "Cannot call grep on a directory without -R.";
      } else {
        results += "";
      }
    }
    return results;
  }
Exemplo n.º 23
0
  /**
   * Handles new events.
   *
   * @param obj the event object
   * @param provider the provider
   * @param id the id of the source of the event
   */
  private void handle(EventObject obj, ProtocolProviderService provider, String id) {
    // check if provider - contact exist update message content
    synchronized (recentMessages) {
      ComparableEvtObj existingMsc = null;
      for (ComparableEvtObj msc : recentMessages) {
        if (msc.getProtocolProviderService().equals(provider)
            && msc.getContactAddress().equals(id)) {
          // update
          msc.update(obj);
          updateRecentMessageToHistory(msc);

          existingMsc = msc;
        }
      }

      if (existingMsc != null) {
        Collections.sort(recentMessages);
        oldestRecentMessage = recentMessages.get(recentMessages.size() - 1).getTimestamp();

        if (recentQuery != null) {
          recentQuery.updateContact(existingMsc, existingMsc.getEventObject());
          recentQuery.fireContactChanged(existingMsc);
        }

        return;
      }

      // if missing create source contact
      // and update recent messages, trim and sort
      MessageSourceContact newSourceContact =
          new MessageSourceContact(obj, MessageSourceService.this);
      newSourceContact.initDetails(obj);
      // we have already checked for duplicate
      ComparableEvtObj newMsg = new ComparableEvtObj(obj);
      recentMessages.add(newMsg);

      Collections.sort(recentMessages);
      oldestRecentMessage = recentMessages.get(recentMessages.size() - 1).getTimestamp();

      // trim
      List<ComparableEvtObj> removedItems = null;
      if (recentMessages.size() > numberOfMessages) {
        removedItems =
            new ArrayList<ComparableEvtObj>(
                recentMessages.subList(numberOfMessages, recentMessages.size()));

        recentMessages.removeAll(removedItems);
      }

      // save
      saveRecentMessageToHistory(newMsg);

      // no query nothing to fire
      if (recentQuery == null) return;

      // now fire
      if (removedItems != null) {
        for (ComparableEvtObj msc : removedItems) {
          recentQuery.fireContactRemoved(msc);
        }
      }

      recentQuery.addQueryResult(newSourceContact);
    }
  }
Exemplo n.º 24
0
  /** @throws Exception */
  private void loadWorkload() throws Exception {
    final boolean debug = LOG.isDebugEnabled();
    // Workload Trace
    if (this.params.containsKey(PARAM_WORKLOAD)) {
      assert (this.catalog_db != null) : "Missing catalog!";
      String path = new File(this.params.get(PARAM_WORKLOAD)).getAbsolutePath();

      boolean weightedTxns = this.getBooleanParam(PARAM_WORKLOAD_XACT_WEIGHTS, false);
      if (debug) LOG.debug("Use Transaction Weights in Limits: " + weightedTxns);

      // This will prune out duplicate trace records...
      if (params.containsKey(PARAM_WORKLOAD_REMOVE_DUPES)) {
        DuplicateTraceFilter filter = new DuplicateTraceFilter();
        this.workload_filter =
            (this.workload_filter != null ? filter.attach(this.workload_filter) : filter);
        if (debug) LOG.debug("Attached " + filter.debugImpl());
      }

      // TRANSACTION OFFSET
      if (params.containsKey(PARAM_WORKLOAD_XACT_OFFSET)) {
        this.workload_xact_offset = Long.parseLong(params.get(PARAM_WORKLOAD_XACT_OFFSET));
        ProcedureLimitFilter filter =
            new ProcedureLimitFilter(-1l, this.workload_xact_offset, weightedTxns);
        // Important! The offset should go in the front!
        this.workload_filter =
            (this.workload_filter != null ? filter.attach(this.workload_filter) : filter);
        if (debug) LOG.debug("Attached " + filter.debugImpl());
      }

      // BASE PARTITIONS
      if (params.containsKey(PARAM_WORKLOAD_RANDOM_PARTITIONS)
          || params.containsKey(PARAM_WORKLOAD_BASE_PARTITIONS)) {
        BasePartitionTxnFilter filter =
            new BasePartitionTxnFilter(new PartitionEstimator(catalog_db));

        // FIXED LIST
        if (params.containsKey(PARAM_WORKLOAD_BASE_PARTITIONS)) {
          for (String p_str : this.getParam(PARAM_WORKLOAD_BASE_PARTITIONS).split(",")) {
            workload_base_partitions.add(Integer.valueOf(p_str));
          } // FOR
          // RANDOM
        } else {
          double factor = this.getDoubleParam(PARAM_WORKLOAD_RANDOM_PARTITIONS);
          List<Integer> all_partitions =
              new ArrayList<Integer>(CatalogUtil.getAllPartitionIds(catalog_db));
          Collections.shuffle(all_partitions, new Random());
          workload_base_partitions.addAll(
              all_partitions.subList(0, (int) (all_partitions.size() * factor)));
        }
        filter.addPartitions(workload_base_partitions);
        this.workload_filter =
            (this.workload_filter != null ? this.workload_filter.attach(filter) : filter);
        if (debug) LOG.debug("Attached " + filter.debugImpl());
      }

      // Txn Limit
      this.workload_xact_limit = this.getLongParam(PARAM_WORKLOAD_XACT_LIMIT);
      Histogram<String> proc_histogram = null;

      // Include/exclude procedures from the traces
      if (params.containsKey(PARAM_WORKLOAD_PROC_INCLUDE)
          || params.containsKey(PARAM_WORKLOAD_PROC_EXCLUDE)) {
        Filter filter = new ProcedureNameFilter(weightedTxns);

        // INCLUDE
        String temp = params.get(PARAM_WORKLOAD_PROC_INCLUDE);
        if (temp != null && !temp.equals(ProcedureNameFilter.INCLUDE_ALL)) {

          // We can take the counts for PROC_INCLUDE and scale them
          // with the multiplier
          double multiplier = 1.0d;
          if (this.hasDoubleParam(PARAM_WORKLOAD_PROC_INCLUDE_MULTIPLIER)) {
            multiplier = this.getDoubleParam(PARAM_WORKLOAD_PROC_INCLUDE_MULTIPLIER);
            if (debug) LOG.debug("Workload Procedure Multiplier: " + multiplier);
          }

          // Default Txn Frequencies
          String procinclude = params.get(PARAM_WORKLOAD_PROC_INCLUDE);
          if (procinclude.equalsIgnoreCase("default")) {
            procinclude =
                AbstractProjectBuilder.getProjectBuilder(catalog_type)
                    .getTransactionFrequencyString();
          }

          Map<String, Integer> limits = new HashMap<String, Integer>();
          int total_unlimited = 0;
          int total = 0;
          for (String proc_name : procinclude.split(",")) {
            int limit = -1;
            // Check if there is a limit for this procedure
            if (proc_name.contains(":")) {
              String pieces[] = proc_name.split(":");
              proc_name = pieces[0];
              limit = (int) Math.round(Integer.parseInt(pieces[1]) * multiplier);
            }

            if (limit < 0) {
              if (proc_histogram == null) {
                if (debug) LOG.debug("Generating procedure histogram from workload file");
                proc_histogram = WorkloadUtil.getProcedureHistogram(new File(path));
              }
              limit = (int) proc_histogram.get(proc_name, 0);
              total_unlimited += limit;
            } else {
              total += limit;
            }
            limits.put(proc_name, limit);
          } // FOR
          // If we have a workload limit and some txns that we want
          // to get unlimited
          // records from, then we want to modify the other txns so
          // that we fill in the "gap"
          if (this.workload_xact_limit != null && total_unlimited > 0) {
            int remaining = this.workload_xact_limit.intValue() - total - total_unlimited;
            if (remaining > 0) {
              for (Entry<String, Integer> e : limits.entrySet()) {
                double ratio = e.getValue() / (double) total;
                e.setValue((int) Math.ceil(e.getValue() + (ratio * remaining)));
              } // FOR
            }
          }

          Histogram<String> proc_multiplier_histogram = null;
          if (debug) {
            if (proc_histogram != null) LOG.debug("Full Workload Histogram:\n" + proc_histogram);
            proc_multiplier_histogram = new Histogram<String>();
          }
          total = 0;
          for (Entry<String, Integer> e : limits.entrySet()) {
            if (debug) proc_multiplier_histogram.put(e.getKey(), e.getValue());
            ((ProcedureNameFilter) filter).include(e.getKey(), e.getValue());
            total += e.getValue();
          } // FOR
          if (debug)
            LOG.debug("Multiplier Histogram [total=" + total + "]:\n" + proc_multiplier_histogram);
        }

        // EXCLUDE
        temp = params.get(PARAM_WORKLOAD_PROC_EXCLUDE);
        if (temp != null) {
          for (String proc_name : params.get(PARAM_WORKLOAD_PROC_EXCLUDE).split(",")) {
            ((ProcedureNameFilter) filter).exclude(proc_name);
          } // FOR
        }

        // Sampling!!
        if (this.getBooleanParam(PARAM_WORKLOAD_PROC_SAMPLE, false)) {
          if (debug) LOG.debug("Attaching sampling filter");
          if (proc_histogram == null)
            proc_histogram = WorkloadUtil.getProcedureHistogram(new File(path));
          Map<String, Integer> proc_includes = ((ProcedureNameFilter) filter).getProcIncludes();
          SamplingFilter sampling_filter = new SamplingFilter(proc_includes, proc_histogram);
          filter = sampling_filter;
          if (debug) LOG.debug("Workload Procedure Histogram:\n" + proc_histogram);
        }

        // Attach our new filter to the chain (or make it the head if
        // it's the first one)
        this.workload_filter =
            (this.workload_filter != null ? this.workload_filter.attach(filter) : filter);
        if (debug) LOG.debug("Attached " + filter.debugImpl());
      }

      // TRANSACTION LIMIT
      if (this.workload_xact_limit != null) {
        ProcedureLimitFilter filter =
            new ProcedureLimitFilter(this.workload_xact_limit, weightedTxns);
        this.workload_filter =
            (this.workload_filter != null ? this.workload_filter.attach(filter) : filter);
        if (debug) LOG.debug("Attached " + filter.debugImpl());
      }

      // QUERY LIMIT
      if (params.containsKey(PARAM_WORKLOAD_QUERY_LIMIT)) {
        this.workload_query_limit = Long.parseLong(params.get(PARAM_WORKLOAD_QUERY_LIMIT));
        QueryLimitFilter filter = new QueryLimitFilter(this.workload_query_limit);
        this.workload_filter =
            (this.workload_filter != null ? this.workload_filter.attach(filter) : filter);
      }

      if (this.workload_filter != null && debug)
        LOG.debug("Workload Filters: " + this.workload_filter.toString());
      this.workload = new Workload(this.catalog);
      this.workload.load(path, this.catalog_db, this.workload_filter);
      this.workload_path = new File(path).getAbsolutePath();
      if (this.workload_filter != null) this.workload_filter.reset();
    }

    // Workload Statistics
    if (this.catalog_db != null) {
      this.stats = new WorkloadStatistics(this.catalog_db);
      if (this.params.containsKey(PARAM_STATS)) {
        String path = this.params.get(PARAM_STATS);
        if (debug) LOG.debug("Loading in workload statistics from '" + path + "'");
        this.stats_path = new File(path).getAbsolutePath();
        try {
          this.stats.load(path, this.catalog_db);
        } catch (Throwable ex) {
          throw new RuntimeException("Failed to load stats file '" + this.stats_path + "'", ex);
        }
      }

      // Scaling
      if (this.params.containsKey(PARAM_STATS_SCALE_FACTOR)) {
        double scale_factor = this.getDoubleParam(PARAM_STATS_SCALE_FACTOR);
        LOG.info("Scaling TableStatistics: " + scale_factor);
        AbstractTableStatisticsGenerator generator =
            AbstractTableStatisticsGenerator.factory(
                this.catalog_db, this.catalog_type, scale_factor);
        generator.apply(this.stats);
      }
    }
  }
Exemplo n.º 25
0
  /**
   * Add the ComparableEvtObj, newly added will fire new, for existing fire update and when trimming
   * the list to desired length fire remove for those that were removed
   *
   * @param contactsToAdd
   */
  private void addNewRecentMessages(List<ComparableEvtObj> contactsToAdd) {
    // now find object to fire new, and object to fire remove
    // let us find duplicates and fire update
    List<ComparableEvtObj> duplicates = new ArrayList<ComparableEvtObj>();
    for (ComparableEvtObj msgToAdd : contactsToAdd) {
      if (recentMessages.contains(msgToAdd)) {
        duplicates.add(msgToAdd);

        // save update
        updateRecentMessageToHistory(msgToAdd);
      }
    }
    recentMessages.removeAll(duplicates);

    // now contacts to add has no duplicates, add them all
    boolean changed = recentMessages.addAll(contactsToAdd);

    if (changed) {
      Collections.sort(recentMessages);

      if (recentQuery != null) {
        for (ComparableEvtObj obj : duplicates)
          recentQuery.updateContact(obj, obj.getEventObject());
      }
    }

    if (!recentMessages.isEmpty())
      oldestRecentMessage = recentMessages.get(recentMessages.size() - 1).getTimestamp();

    // trim
    List<ComparableEvtObj> removedItems = null;
    if (recentMessages.size() > numberOfMessages) {
      removedItems =
          new ArrayList<ComparableEvtObj>(
              recentMessages.subList(numberOfMessages, recentMessages.size()));

      recentMessages.removeAll(removedItems);
    }

    if (recentQuery != null) {
      // now fire, removed for all that were in the list
      // and now are removed after trim
      if (removedItems != null) {
        for (ComparableEvtObj msc : removedItems) {
          if (!contactsToAdd.contains(msc)) recentQuery.fireContactRemoved(msc);
        }
      }

      // fire new for all that were added, and not removed after trim
      for (ComparableEvtObj msc : contactsToAdd) {
        if ((removedItems == null || !removedItems.contains(msc)) && !duplicates.contains(msc)) {
          MessageSourceContact newSourceContact =
              new MessageSourceContact(msc.getEventObject(), MessageSourceService.this);
          newSourceContact.initDetails(msc.getEventObject());

          recentQuery.addQueryResult(newSourceContact);
        }
      }

      // if recent messages were changed, indexes have change lets
      // fire event for the last element which will reorder the whole
      // group if needed.
      if (changed) recentQuery.fireContactChanged(recentMessages.get(recentMessages.size() - 1));
    }
  }
Exemplo n.º 26
0
  /**
   * Call the appropriate command function and notify user of incorrect command input.
   *
   * @param command is the operation to do.
   * @param params is a list of parameters for the command.
   */
  public void executeCommand(String command, List<String> params) {
    if (commands_.containsKey(command)) {
      int commandNum = commands_.get(command);
      try {
        switch (commandNum) {
            // mkdir command.
          case 1:
            if (params.size() > 0) {
              mkdir(params);
            } else {
              System.out.println("mkdir: missing operand");
            }
            break;

            // cd command.
          case 2:
            if (params.size() == 1) {
              cd(params.get(0).toString());
            } else if (params.size() > 1) {
              System.out.println("cd: Too many arguments.");
            } else {
              cd("/");
            }
            break;

            // ls command.
          case 3:
            String lsContent = ("\"" + ls(params) + "\"");
            currentRedirection_.add(0, lsContent);
            System.out.print(echo(currentRedirection_));
            break;

            // pwd command.
          case 4:
            if (params.size() < 1) {
              String pwdContent = ("\"" + pwd() + "\"");
              currentRedirection_.add(0, pwdContent);
              System.out.print(echo(currentRedirection_));
            } else {
              System.out.println("pwd: ignoring non-option arguments");
              System.out.println(pwd());
            }
            break;

            // mv command.
          case 5:
            if (params.size() == 2) {
              mv(params.get(0).toString(), params.get(1).toString());
            } else {
              System.out.println("mv: missing file operand" + "\nSpecify OLDFILE and NEWFILE");
            }
            break;

            // cp command.
          case 6:
            if (params.size() == 2) {
              cp(params.get(0).toString(), params.get(1).toString());
            } else {
              System.out.println("cp: missing file operand" + "\nSpecify OLDFILE and NEWFILE");
            }
            break;

            // cat command.
          case 7:
            if (params.size() == 1) {
              String catContent = ("\"" + cat(params.get(0).toString()) + "\"");
              currentRedirection_.add(0, catContent);
              System.out.print(echo(currentRedirection_));
            } else {
              System.out.println("cat: missing file operand" + "\nInvalid filename");
            }
            break;

            // get command.
          case 8:
            if (params.size() == 1) {
              String getContent = ("\"" + get(params.get(0).toString()) + "\"");
              currentRedirection_.add(0, getContent);
              System.out.print(echo(currentRedirection_));
            } else {
              System.out.println("get: missing operand\nInvalid URL");
            }
            break;

            // echo command.
          case 9:
            if (params.size() > 0) {
              if (currentRedirection_.size() > 0) {
                params.add(currentRedirection_.get(0));
                params.add(currentRedirection_.get(1));
              }
              System.out.print(echo(params));
            } else {
              System.out.print("\n");
            }
            break;

            // rm command.
          case 10:
            if (params.size() > 0) {
              rm(params);
            } else {
              System.out.println("mkdir: missing operand");
            }
            break;

            // ln command.
          case 11:
            if (params.size() == 2) {
              ln(params.get(0).toString(), params.get(1).toString());
            } else {
              System.out.println("ln: missing file operand" + "\nSpecify PATH1 and PATH2");
            }
            break;

            // man command.
          case 12:
            if (params.isEmpty()) {
              params.add("");
            }
            String manContent = ("\"" + man(params.get(0).toString()) + "\"");
            currentRedirection_.add(0, manContent);
            System.out.print(echo(currentRedirection_));
            break;

            // find command.
          case 13:
            if (params.isEmpty()) {
              params.add("");
            }
            // Add regex stuff here.
            String findContent = find(params.get(0), params.subList(1, params.size()));
            assert findContent.equals("");
            findContent = ("\"" + findContent + "\"");
            currentRedirection_.add(0, findContent);
            System.out.print(echo(currentRedirection_));
            break;

            // grep command.
          case 14:
            if (params.size() < 2) {
              System.out.println("Usage: grep [OPTION]... PATTERN [FILE]...");
            } else {
              String grepContent =
                  ("\"" + grep(params.get(0), params.subList(1, params.size())) + "\"");
              currentRedirection_.add(0, grepContent);
              System.out.print(echo(currentRedirection_));
            }
            break;
        }
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }
    } else {
      System.out.println("-JShell: " + command + ": command not found");
    }
  }