Java Code Examples for java.util.SortedMap#clear()

The following examples show how to use java.util.SortedMap#clear() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: journalkeeper   File: PositioningStore.java    License: Apache License 2.0 6 votes vote down vote up
private void rollbackFiles(long position) throws IOException {

        if (!storeFileMap.isEmpty()) {
            // position 所在的Page需要截断至position
            Map.Entry<Long, StoreFile> entry = storeFileMap.floorEntry(position);
            StoreFile storeFile = entry.getValue();
            if (position > storeFile.position()) {
                int relPos = (int) (position - storeFile.position());
                logger.info("Truncate store file {} to relative position {}.", storeFile.file().getAbsolutePath(), relPos);
                storeFile.rollback(relPos);
            }

            SortedMap<Long, StoreFile> toBeRemoved = storeFileMap.tailMap(position);

            for (StoreFile sf : toBeRemoved.values()) {
                logger.info("Delete store file {}.", sf.file().getAbsolutePath());
                forceDeleteStoreFile(sf);
                if (writeStoreFile == sf) {
                    writeStoreFile = null;
                }
            }
            toBeRemoved.clear();
        }


    }
 
Example 2
Source Project: datawave   File: IngestRateIterator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void processRow(SortedMap<Key,Value> row) {
    LinkedList<Double> rates = new LinkedList<>();
    Text timestamp = row.firstKey().getRow();
    IngestEntryKey iek = new IngestEntryKey();
    for (Entry<Key,Value> e : row.entrySet()) {
        try {
            iek.parse(e.getKey());
        } catch (InvalidKeyException e1) {
            continue;
        }
        
        // value will be in Events/s
        double rate = ((double) iek.getCount()) / (((double) iek.getDuration()) / 1000.0);
        rates.add(rate);
    }
    
    // get the avg
    double avgRate = 0;
    for (Double d : rates) {
        avgRate += d / rates.size();
    }
    
    row.clear();
    row.put(new Key(timestamp, new Text(Double.toString(avgRate)), et), ev);
}
 
Example 3
Source Project: incubator-ratis   File: SlidingWindow.java    License: Apache License 2.0 6 votes vote down vote up
void endOfRequests(long nextToProcess, REQUEST end, Consumer<REQUEST> replyMethod) {
  final REQUEST nextToProcessRequest = requests.get(nextToProcess);
  Preconditions.assertNull(nextToProcessRequest,
      () -> "nextToProcessRequest = " + nextToProcessRequest + " != null, nextToProcess = " + nextToProcess);

  final SortedMap<Long, REQUEST> tail = requests.tailMap(nextToProcess);
  for (REQUEST r : tail.values()) {
    final AlreadyClosedException e = new AlreadyClosedException(
        getName() + " is closing: seq = " + r.getSeqNum() + " > nextToProcess = " + nextToProcess
            + " will NEVER be processed; request = " + r);
    r.fail(e);
    replyMethod.accept(r);
  }
  tail.clear();

  putNewRequest(end);
}
 
Example 4
Source Project: j2objc   File: TreeSubMapTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testHeadMapContents() {
    NavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
 
Example 5
/**
 * headMap returns map with keys in requested range
 */
public void testHeadMapContents() {
    ConcurrentNavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
 
Example 6
/**
 * headMap returns map with keys in requested range
 */
public void testHeadMapContents() {
    ConcurrentNavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
 
Example 7
public void testFactorInteger() {
	SortedMap<BigInteger, Integer> map = new TreeMap<BigInteger, Integer>();
	Primality.factorInteger(BigInteger.valueOf(990), map);
	assertEquals(map.toString(), "{2=1, 3=2, 5=1, 11=1}");

	map.clear();
	Primality.factorInteger(new BigInteger("341550071728321"), map);
	assertEquals(map.toString(), "{10670053=1, 32010157=1}");

	map.clear();
	Primality.factorInteger(BigInteger.valueOf(2010), map);
	assertEquals(map.toString(), "{2=1, 3=1, 5=1, 67=1}");

	map.clear();
	Primality.factorInteger(BigInteger.valueOf(24), map);
	assertEquals(map.toString(), "{2=3, 3=1}");
}
 
Example 8
/**
 * headMap returns map with keys in requested range
 */
public void testDescendingHeadMapContents() {
    ConcurrentNavigableMap map = dmap5();
    SortedMap sm = map.headMap(m4);
    assertTrue(sm.containsKey(m1));
    assertTrue(sm.containsKey(m2));
    assertTrue(sm.containsKey(m3));
    assertFalse(sm.containsKey(m4));
    assertFalse(sm.containsKey(m5));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m1, k);
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(m4, map.firstKey());
}
 
Example 9
Source Project: j2objc   File: TreeSubMapTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testDescendingHeadMapContents() {
    NavigableMap map = dmap5();
    SortedMap sm = map.headMap(m4);
    assertTrue(sm.containsKey(m1));
    assertTrue(sm.containsKey(m2));
    assertTrue(sm.containsKey(m3));
    assertFalse(sm.containsKey(m4));
    assertFalse(sm.containsKey(m5));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m1, k);
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(m4, map.firstKey());
}
 
Example 10
Source Project: cognition   File: AccumuloItem.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Construct a map based upon all ColumnQualifiers and Values in the Column Family <code>columnFamily</code>,
 * optionally removing any used Key-Value pairs from <code>row</code>, the source map.
 *
 * @param row
 * @param columnFamily
 * @param clearUsed    when true, clear used entries from <code>row</code>
 * @return
 */
public static Map<String, String> readMap(SortedMap<Key, Value> row, String columnFamily, boolean clearUsed) {
  Map<String, String> retVal = new LinkedHashMap<>();
  String rowid = row.firstKey().getRow().toString();

  SortedMap<Key, Value> familyMap = row.subMap(
      new Key(rowid, columnFamily),
      new Key(rowid, columnFamily + "\0"));
  for (Entry<Key, Value> entry : familyMap.entrySet()) {
    retVal.put(entry.getKey().getColumnQualifier().toString(), entry.getValue().toString());
  }
  if (clearUsed) {
    familyMap.clear();
  }
  return retVal;
}
 
Example 11
Source Project: atomix   File: SegmentedJournal.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Compacts the journal up to the given index.
 * <p>
 * The semantics of compaction are not specified by this interface.
 *
 * @param index The index up to which to compact the journal.
 */
public void compact(long index) {
  Map.Entry<Long, JournalSegment<E>> segmentEntry = segments.floorEntry(index);
  if (segmentEntry != null) {
    SortedMap<Long, JournalSegment<E>> compactSegments = segments.headMap(segmentEntry.getValue().index());
    if (!compactSegments.isEmpty()) {
      log.debug("{} - Compacting {} segment(s)", name, compactSegments.size());
      for (JournalSegment segment : compactSegments.values()) {
        log.trace("Deleting segment: {}", segment);
        segment.close();
        segment.delete();
      }
      compactSegments.clear();
      resetHead(segmentEntry.getValue().index());
    }
  }
}
 
Example 12
Source Project: geowave   File: Sampler.java    License: Apache License 2.0 5 votes vote down vote up
public void sample(
    final Iterable<CentroidPairing<T>> pairings,
    final SampleNotification<T> notification,
    final double normalizingConstant) {
  int putCounter = 0;

  final SortedMap<Double, T> reservoir = Maps.newTreeMap();
  for (final CentroidPairing<T> pairing : pairings) {
    final double weight = pairing.getDistance();
    if (weight > 0.0) {
      final double score =
          sampleProbabilityFn.getProbability(weight, normalizingConstant, sampleSize);
      // could add extra to make sure new point is far enough away
      // from the rest
      if (reservoir.size() < sampleSize) {
        reservoir.put(score, pairing.getPairedItem().getWrappedItem());
        putCounter++;
      } else if (score > reservoir.firstKey()) {
        reservoir.remove(reservoir.firstKey());
        reservoir.put(score, pairing.getPairedItem().getWrappedItem());
      }
      if (putCounter > putLimit) {
        // On the off-chance this gets huge, cleanup
        // Can occur if sampleSize > PUT_LIMIT
        notifyAll(notification, reservoir.values(), true);
        reservoir.clear();
        putCounter = 0;
      }
    }
  }
  notifyAll(notification, reservoir.values(), false);
}
 
Example 13
Source Project: TakinRPC   File: DefaultRaftLog.java    License: Apache License 2.0 5 votes vote down vote up
public boolean append(@Nonnull AppendEntries appendEntries) {

        final long prevLogIndex = appendEntries.getPrevLogIndex();
        final long prevLogTerm = appendEntries.getPrevLogTerm();
        final List<Entry> entries = appendEntries.getEntriesList();

        EntryMeta previousEntry = entryIndex.get(prevLogIndex);
        if ((previousEntry == null) || (previousEntry.term != prevLogTerm)) {
            LOGGER.debug("Append prevLogIndex {} prevLogTerm {} previousEntry {}", prevLogIndex, prevLogTerm, previousEntry);
            return false;
        }

        SortedMap<Long, EntryMeta> old = this.entryIndex.tailMap(prevLogIndex + 1);
        for (EntryMeta e : old.values()) {
            try {
                LOGGER.debug("Deleting {}", e.index);
                journal.delete(e.location);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        old.clear();
        lastLogIndex = prevLogIndex;

        for (Entry entry : entries) {
            storeEntry(++lastLogIndex, entry);
        }

        return true;

    }
 
Example 14
Source Project: j2objc   File: UnicodeMapTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param rand 
 * @param nextInt
 * @param test 
 * @return
 */
private SortedMap<String, Integer> fillRandomMap(Random rand, int max, SortedMap<String, Integer> test) {
    test.clear();
    max = rand.nextInt(max);
    for (int i = 0; i < max; ++i) {
        test.put(getRandomKey(rand), rand.nextInt(50)+50);
    }
    return test;
}
 
Example 15
@Override
public boolean next(ByteBuffer key, SortedMap<ByteBuffer, IColumn> value) throws IOException {
  if (this.nextKeyValue()) {
    key.clear();
    key.put(this.getCurrentKey());
    key.rewind();

    value.clear();
    value.putAll(this.getCurrentValue());

    return true;
  }
  return false;
}
 
Example 16
Source Project: flink-statefun   File: Checkpoints.java    License: Apache License 2.0 4 votes vote down vote up
public void commitCheckpointsUntil(long checkpointId) {
  SortedMap<Long, FeedbackLogger<T>> completedCheckpoints =
      uncompletedCheckpoints.headMap(checkpointId, true);
  completedCheckpoints.values().forEach(FeedbackLogger::commit);
  completedCheckpoints.clear();
}
 
Example 17
private static SortedMap<Float, TextLayout> tryTextLayout(
    AttributedString attrString,
    FontRenderContext frc,
    Rectangle box,
    int leftPadding,
    int topPadding
) {
    final SortedMap<Float, TextLayout> lines = new TreeMap<>();
    AttributedCharacterIterator text = attrString.getIterator();
    int paragraphStart = text.getBeginIndex();
    int paragraphEnd = text.getEndIndex();
    LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(text, frc);
    float boxWidth = (float)box.getWidth();
    float boxHeight = (float)box.getHeight();
    float posY = topPadding;
    lineMeasurer.setPosition(paragraphStart);

    //Measure length of string to fit in box
    final AttributedCharacterIterator iter = attrString.getIterator();
    while (lineMeasurer.getPosition() < paragraphEnd) {
        //Check for ptPanel overlap
        int next = lineMeasurer.nextOffset(posY >= 123 ? boxWidth - (leftPadding << 1) - 100 : boxWidth - (leftPadding << 1));
        int limit = next;
        //Check for newlines
        for (int i = lineMeasurer.getPosition(); i < next; ++i) {
            char c = iter.setIndex(i);
            if (c == NEWLINE && i > lineMeasurer.getPosition()) {
                limit = i;
                break;
            }
        }

        //get+draw measured length
        TextLayout layout = lineMeasurer.nextLayout(boxWidth, limit, false);
        posY += layout.getAscent();
        lines.put(posY, layout);

        //add extra space between paragraphs
        if (limit < next) {
            posY += layout.getLeading() + layout.getDescent();
        }

        //move to next line
        posY += layout.getDescent();

        //check if out of room
        if (posY > boxHeight) {
            lines.clear();
            break;
        }
    }
    return lines;
}
 
Example 18
Source Project: teku   File: DutyScheduler.java    License: Apache License 2.0 4 votes vote down vote up
private void removeEpochs(final SortedMap<UnsignedLong, DutyQueue> toRemove) {
  toRemove.values().forEach(DutyQueue::cancel);
  toRemove.clear();
}
 
Example 19
/**
 * Creates a {@link BooleanPerceptronClassifier}
 *
 * @param indexReader     the reader on the index to be used for classification
 * @param analyzer       an {@link Analyzer} used to analyze unseen text
 * @param query          a {@link Query} to eventually filter the docs used for training the classifier, or {@code null}
 *                       if all the indexed docs should be used
 * @param batchSize      the size of the batch of docs to use for updating the perceptron weights
 * @param bias      the bias used for class separation
 * @param classFieldName the name of the field used as the output for the classifier
 * @param textFieldName  the name of the field used as input for the classifier
 * @throws IOException if the building of the underlying {@link FST} fails and / or {@link TermsEnum} for the text field
 *                     cannot be found
 */
public BooleanPerceptronClassifier(IndexReader indexReader, Analyzer analyzer, Query query, Integer batchSize,
                                   Double bias, String classFieldName, String textFieldName) throws IOException {
  this.textTerms = MultiTerms.getTerms(indexReader, textFieldName);

  if (textTerms == null) {
    throw new IOException("term vectors need to be available for field " + textFieldName);
  }

  this.analyzer = analyzer;
  this.textFieldName = textFieldName;

  if (bias == null || bias == 0d) {
    // automatic assign the bias to be the average total term freq
    double t = (double) indexReader.getSumTotalTermFreq(textFieldName) / (double) indexReader.getDocCount(textFieldName);
    if (t != -1) {
      this.bias = t;
    } else {
      throw new IOException(
              "bias cannot be assigned since term vectors for field "
                      + textFieldName + " do not exist");
    }
  } else {
    this.bias = bias;
  }

  // TODO : remove this map as soon as we have a writable FST
  SortedMap<String, Double> weights = new ConcurrentSkipListMap<>();

  TermsEnum termsEnum = textTerms.iterator();
  BytesRef textTerm;
  while ((textTerm = termsEnum.next()) != null) {
    weights.put(textTerm.utf8ToString(), (double) termsEnum.totalTermFreq());
  }
  updateFST(weights);

  IndexSearcher indexSearcher = new IndexSearcher(indexReader);

  int batchCount = 0;

  BooleanQuery.Builder q = new BooleanQuery.Builder();
  q.add(new BooleanClause(new WildcardQuery(new Term(classFieldName, "*")), BooleanClause.Occur.MUST));
  if (query != null) {
    q.add(new BooleanClause(query, BooleanClause.Occur.MUST));
  }
  // run the search and use stored field values
  for (ScoreDoc scoreDoc : indexSearcher.search(q.build(),
          Integer.MAX_VALUE).scoreDocs) {
    Document doc = indexSearcher.doc(scoreDoc.doc);

    IndexableField textField = doc.getField(textFieldName);

    // get the expected result
    IndexableField classField = doc.getField(classFieldName);

    if (textField != null && classField != null) {
      // assign class to the doc
      ClassificationResult<Boolean> classificationResult = assignClass(textField.stringValue());
      Boolean assignedClass = classificationResult.getAssignedClass();

      Boolean correctClass = Boolean.valueOf(classField.stringValue());
      long modifier = correctClass.compareTo(assignedClass);
      if (modifier != 0) {
        updateWeights(indexReader, scoreDoc.doc, assignedClass,
                weights, modifier, batchCount % batchSize == 0);
      }
      batchCount++;
    }
  }
  weights.clear(); // free memory while waiting for GC
}
 
Example 20
Source Project: j2objc   File: UnicodeMapTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void TestAMonkey() {
    SortedMap<String,Integer> stayWithMe = new TreeMap<String,Integer>(OneFirstComparator);

    UnicodeMap<Integer> me = new UnicodeMap<Integer>().putAll(stayWithMe);
    // check one special case, removal near end
    me.putAll(0x10FFFE, 0x10FFFF, 666);
    me.remove(0x10FFFF);

    int iterations = 100000;
    SortedMap<String,Integer> test = new TreeMap();

    Random rand = new Random(0);
    String other;
    Integer value;
    // try modifications
    for (int i = 0; i < iterations ; ++i) {
        switch(rand.nextInt(20)) {
        case 0:
            logln("clear");
            stayWithMe.clear();
            me.clear();
            break;
        case 1:
            fillRandomMap(rand, 5, test);
            logln("putAll\t" + test);
            stayWithMe.putAll(test);
            me.putAll(test);
            break;
        case 2: case 3: case 4: case 5: case 6: case 7: case 8:
            other = getRandomKey(rand);
            //                if (other.equals("\uDBFF\uDFFF") && me.containsKey(0x10FFFF) && me.get(0x10FFFF).equals(me.get(0x10FFFE))) {
            //                    System.out.println("Remove\t" + other + "\n" + me);
            //                }
            logln("remove\t" + other);
            stayWithMe.remove(other);
            try {
                me.remove(other);
            } catch (IllegalArgumentException e) {
                errln("remove\t" + other + "\tfailed: " + e.getMessage() + "\n" + me);
                me.clear();
                stayWithMe.clear();
            }
            break;
        default:
            other = getRandomKey(rand);
            value = rand.nextInt(50)+50;
            logln("put\t" + other + " = " + value);
            stayWithMe.put(other, value);
            me.put(other,value);
            break;
        }
        checkEquals(me, stayWithMe);
    }
}