// Blindly append entries to the end of the log. Note that there is
 // no check to make sure that the last entry is from the correct
 // term. This method should only be used in testing.
 //
 // @param entries to append (in order of 0 to append.length-1)
 // @return highest index in log after entries have been appended.
 public int append(Entry[] entries) {
   try {
     if (entries != null) {
       OutputStream out =
           Files.newOutputStream(
               mLogPath,
               StandardOpenOption.CREATE,
               StandardOpenOption.APPEND,
               StandardOpenOption.SYNC);
       for (Entry entry : entries) {
         if (entry != null) {
           out.write(entry.toString().getBytes());
           out.write('\n');
           mEntries.add(entry);
         } else {
           //	    System.out.println ("Tried to append null entry to RaftLog.");
           break;
         }
       }
       out.close();
     }
   } catch (IOException e) {
     System.out.println(e.getMessage());
     e.printStackTrace();
   }
   return (mEntries.size() - 1);
 }
示例#2
0
  /**
   * ** Prints all the default values from <code>RTKey</code> and {@link RTConfig} ** to the
   * specified <code>PrintStream</code>. Used for debugging/testing ** @param out The <code>
   * PrintStream</code>
   */
  public static void printDefaults(PrintStream out) {

    /* print standard runtime entries */
    Set<String> keyList = new OrderedSet<String>();
    String keyGrp = null;

    for (Iterator<Entry> v = RTKey.getRuntimeEntryMap().values().iterator(); v.hasNext(); ) {
      Entry rtk = v.next();
      if (rtk.isHelp()) {
        out.println("");
        out.println("# ===== " + rtk.getHelp());
      } else {
        Object dft = rtk.getDefault();
        out.println("# --- " + rtk.getHelp());
        out.println("# " + rtk.toString(dft));
        String key = rtk.getKey();
        keyList.add(key);
        if (!key.equals(CONFIG_FILE) && RTConfig.hasProperty(key)) {
          String val = RTConfig.getString(key, null);
          // if ((val != null) && ((dft == null) || !val.equals(dft.toString()))) {
          out.println(rtk.toString(val));
          // }
        }
      }
    }

    /* orphaned entries */
    RTProperties cmdLineProps = RTConfig.getConfigFileProperties();
    if (cmdLineProps != null) {
      boolean orphanHeader = false;
      for (Iterator i = cmdLineProps.keyIterator(); i.hasNext(); ) {
        Object k = i.next();
        if (!k.equals(COMMAND_LINE_CONF) && !keyList.contains(k)) {
          if (!orphanHeader) {
            out.println("");
            out.println("# ===== Other entries");
            orphanHeader = true;
          }
          Object v = cmdLineProps.getProperty(k, null);
          out.println(k + "=" + ((v != null) ? v : NULL_VALUE));
        }
      }
    }

    /* final blank line */
    out.println("");
  }
示例#3
0
 private static String get(int hashCode, String userId, String tweetTime) {
   StringBuilder sb = new StringBuilder();
   List<Entry> entries = entryDAO.getEntry(hashCode, userId, tweetTime);
   for (Entry entry : entries) {
     String textDecoded = StringEscapeUtils.unescapeJava(entry.getTweetText());
     entry.setTweetText(textDecoded);
     sb.append(entry.toString());
   }
   return sb.toString();
 }
  /**
   * Combine the actual line data. The data is read line for line and then appended one after the
   * other and then written to the file.
   *
   * <p>The order of the files is maintained so that the data is not corrupted once the writing out
   * to the final result file is performed.
   *
   * @param writer destination of the final output.
   * @param partials list of partial results.
   */
  private void combineData(final BufferedWriter writer, final List<File> partials) {
    List<BufferedReader> readers = Lists.newArrayListWithCapacity(partials.size());
    for (File f : partials) {
      try {
        InputStreamReader is =
            new InputStreamReader(new FileInputStream(f), Charset.forName("UTF-8"));
        readers.add(new BufferedReader(is));
      } catch (FileNotFoundException ex) {
        throw new RuntimeException(ex);
      }
    }

    try {
      BufferedReader checkReader = readers.get(0); // There will always be at least 1
      String checkLine = checkReader.readLine();
      String[] parts = checkLine.split(" ");
      Entry entry = new Entry(parts[0], parts.length - 1);
      for (int i = 1; i < parts.length; i++) {
        entry.add(i - 1, parts[i]);
      }

      List<BufferedReader> loopingList = readers.subList(1, readers.size());
      while (checkLine != null) {
        for (BufferedReader reader : loopingList) {
          String string = reader.readLine();
          String[] localParts = string.split(" ");
          for (int i = 1; i < localParts.length; i++) {
            entry.add(i - 1, localParts[i]);
          }
        }

        writer.write(entry.toString());
        writer.newLine();
        checkLine = checkReader.readLine();

        if (checkLine != null) {
          parts = checkLine.split(" ");
          entry = new Entry(parts[0], parts.length - 1);
          for (int i = 1; i < parts.length; i++) {
            entry.add(i - 1, parts[i]);
          }
        }
      }

      for (BufferedReader reader : readers) {
        reader.close();
      }

      for (File f : partials) {
        f.delete();
      }
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
  }
示例#5
0
 /**
  * Return a string that describes the statistics for the top n entries, sorted by descending
  * order of total time spent dealing with the query. This will always include the totals entry
  * in the first position.
  *
  * @param n the desired number of entries to describe
  * @return a string describing the statistics for the top n entries
  */
 public synchronized String toStringTop(int n) {
   StringBuilder out = new StringBuilder();
   List<Entry> list = entries();
   if (list.isEmpty()) return "<no queries executed>";
   Collections.sort(list, TOTAL_TIME_DESCENDING);
   int maxCountLength = COUNT_FORMAT.format(list.get(0).numQueries).length();
   double totalDuration = list.get(0).queryTime;
   for (Entry entry : list.subList(0, Math.min(n, list.size())))
     out.append(entry.toString(maxCountLength, totalDuration)).append('\n');
   return out.toString();
 }
示例#6
0
文件: Block.java 项目: ejrh/Whiley
  public String toString() {
    String r = "[";

    boolean firstTime = true;
    for (Entry s : stmts) {
      if (!firstTime) {
        r += ", ";
      }
      firstTime = false;
      r += s.toString();
    }

    return r + "]";
  }
示例#7
0
 public String show() {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < table.length; i++) {
     sb.append(i + "\t");
     if (table[i] != null) {
       Entry<K, V> e = table[i];
       while (e != null) {
         sb.append(e.toString() + " ");
         e = e.next;
       }
     }
     sb.append("\n");
   }
   return sb.toString();
 }
示例#8
0
  @Override
  public void execute(Tuple input) {
    Source src = parseComponentId(input.getSourceComponent());

    if (src == Source.GACD) {
      avg = input.getDoubleByField(Field.AVERAGE);
    } else {
      CallDetailRecord cdr = (CallDetailRecord) input.getValueByField(Field.RECORD);
      String number = input.getString(0);
      long timestamp = input.getLong(1);
      double rate = input.getDouble(2);

      String key = String.format("%s:%d", number, timestamp);

      if (map.containsKey(key)) {
        Entry e = map.get(key);
        e.set(src, rate);

        if (e.isFull()) {
          // calculate the score for the ratio
          double ratio = (e.get(Source.CT24) / e.get(Source.ECR24)) / avg;
          double score = score(thresholdMin, thresholdMax, ratio);

          LOG.debug(
              String.format(
                  "T1=%f; T2=%f; CT24=%f; ECR24=%f; AvgCallDur=%f; Ratio=%f; Score=%f",
                  thresholdMin,
                  thresholdMax,
                  e.get(Source.CT24),
                  e.get(Source.ECR24),
                  avg,
                  ratio,
                  score));

          collector.emit(new Values(number, timestamp, score, cdr));
          map.remove(key);
        } else {
          LOG.debug(
              String.format(
                  "Inconsistent entry: source=%s; %s", input.getSourceComponent(), e.toString()));
        }
      } else {
        Entry e = new Entry(cdr);
        e.set(src, rate);
        map.put(key, e);
      }
    }
  }
示例#9
0
  /**
   * Formats the message.
   *
   * <p>Entries are formated like {@code key=value}. The entries are concatenated with {@link
   * Tacoli#COLUMN_DELIMITER} as separator.
   *
   * @return never {@code null}, maybe empty
   */
  @Override
  public String toString() {
    final StringBuilder buffer = new StringBuilder();
    boolean firstEntry = true;

    for (final Entry entry : data) {
      if (!firstEntry) {
        buffer.append(Tacoli.COLUMN_DELIMITER);
      }

      buffer.append(entry.toString());
      firstEntry = false;
    }

    return buffer.toString();
  }
示例#10
0
文件: Cluster.java 项目: proog/mdmi
 @Override
 public String toString() {
   String s = "Cluster:\n";
   for (Entry e : entries) s += "\t" + e.toString() + "\n";
   return s;
 }
示例#11
0
  // @param entries to append (in order of 0 to append.length-1). must
  // be non-null.
  // @param index of log entry before entries to append (-1 if
  // inserting at index 0)
  // @param term of log entry before entries to append (ignored if
  // prevIndex is -1)
  // @return highest index in log after entries have been appended, if
  // the entry at prevIndex is not from prevTerm or if the log does
  // not have an entry at prevIndex, the append request will fail, and
  // the method will return -1.
  public int insert(Entry[] entries, int prevIndex, int prevTerm) {
    if (entries == null) {
      // cannot insert null entries
      return -1;
    } else if ((prevIndex == -1)
        || ((mEntries.size() > prevIndex)
            && (mEntries.get(prevIndex) != null)
            && (mEntries.get(prevIndex).term == prevTerm))) {
      // Because we are inserting in the middle of our log, we
      // will update our log by creating a temporary on-disk log
      // with the new entries and then replacing the old on-disk
      // log with the temporary one.

      // First, create an in-memory copy of the existing log up to
      // the point where the new entries will be added
      LinkedList<Entry> tmpEntries = new LinkedList<Entry>();
      for (int i = 0; i <= prevIndex; i++) {
        Entry entry = mEntries.get(i);
        tmpEntries.add(entry);
      }
      Path tmpLogPath;
      // Next, add the new entries to temporary in-memory and
      // on-disk logs
      try {
        tmpLogPath =
            FileSystems.getDefault().getPath(mLogPath.toAbsolutePath().toString() + ".tmp");

        OutputStream out =
            Files.newOutputStream(
                tmpLogPath,
                StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.SYNC);

        // Write out the prefix
        for (Entry entry : tmpEntries) {
          out.write(entry.toString().getBytes());
          out.write('\n');
        }

        // Add the new entries
        for (Entry entry : entries) {
          if (entry != null) {
            out.write(entry.toString().getBytes());
            out.write('\n');
            tmpEntries.add(entry);
          }
        }
        out.close();
      } catch (IOException e) {
        System.out.println("Error creating temporary log.");
        System.out.println(e.getMessage());
        e.printStackTrace();
        return -1;
      }

      // Switch the on-disk log to the new version
      try {
        Files.move(
            tmpLogPath,
            mLogPath,
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.ATOMIC_MOVE);
      } catch (IOException e) {
        System.out.println("Error replacing old log.");
        System.out.println(e.getMessage());
        e.printStackTrace();
        return -1;
      }

      // Switch the in-memory log to the new version
      mEntries = tmpEntries;
    } else {
      System.out.println(
          "RaftLog: " + "index and term mismatch, could not insert new log entries.");
      return -1;
    }

    return (mEntries.size() - 1);
  }