public void testSubMapContents2() {
   ConcurrentNavigableMap map = map5();
   SortedMap sm = map.subMap(two, three);
   assertEquals(1, sm.size());
   assertEquals(two, sm.firstKey());
   assertEquals(two, sm.lastKey());
   assertFalse(sm.containsKey(one));
   assertTrue(sm.containsKey(two));
   assertFalse(sm.containsKey(three));
   assertFalse(sm.containsKey(four));
   assertFalse(sm.containsKey(five));
   Iterator i = sm.keySet().iterator();
   Object k;
   k = (Integer) (i.next());
   assertEquals(two, k);
   assertFalse(i.hasNext());
   Iterator j = sm.keySet().iterator();
   j.next();
   j.remove();
   assertFalse(map.containsKey(two));
   assertEquals(4, map.size());
   assertEquals(0, sm.size());
   assertTrue(sm.isEmpty());
   assertSame(sm.remove(three), null);
   assertEquals(4, map.size());
 }
 public void testDescendingSubMapContents2() {
   ConcurrentNavigableMap map = dmap5();
   SortedMap sm = map.subMap(m2, m3);
   assertEquals(1, sm.size());
   assertEquals(m2, sm.firstKey());
   assertEquals(m2, sm.lastKey());
   assertFalse(sm.containsKey(m1));
   assertTrue(sm.containsKey(m2));
   assertFalse(sm.containsKey(m3));
   assertFalse(sm.containsKey(m4));
   assertFalse(sm.containsKey(m5));
   Iterator i = sm.keySet().iterator();
   Object k;
   k = (Integer) (i.next());
   assertEquals(m2, k);
   assertFalse(i.hasNext());
   Iterator j = sm.keySet().iterator();
   j.next();
   j.remove();
   assertFalse(map.containsKey(m2));
   assertEquals(4, map.size());
   assertEquals(0, sm.size());
   assertTrue(sm.isEmpty());
   assertSame(sm.remove(m3), null);
   assertEquals(4, map.size());
 }
 /** remove(key,value) removes only if pair present */
 public void testRemove2() {
   ConcurrentNavigableMap map = map5();
   assertTrue(map.containsKey(five));
   assertEquals("E", map.get(five));
   map.remove(five, "E");
   assertEquals(4, map.size());
   assertFalse(map.containsKey(five));
   map.remove(four, "A");
   assertEquals(4, map.size());
   assertTrue(map.containsKey(four));
 }
 /** remove(key,value) removes only if pair present */
 public void testDescendingRemove2() {
   ConcurrentNavigableMap map = dmap5();
   assertTrue(map.containsKey(m5));
   assertEquals("E", map.get(m5));
   map.remove(m5, "E");
   assertEquals(4, map.size());
   assertFalse(map.containsKey(m5));
   map.remove(m4, "A");
   assertEquals(4, map.size());
   assertTrue(map.containsKey(m4));
 }
  /** A deserialized map equals original */
  public void testSerialization() throws Exception {
    ConcurrentNavigableMap q = map5();

    ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
    ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
    out.writeObject(q);
    out.close();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
    ConcurrentNavigableMap r = (ConcurrentNavigableMap) in.readObject();
    assertEquals(q.size(), r.size());
    assertTrue(q.equals(r));
    assertTrue(r.equals(q));
  }
  @SuppressWarnings("resource") // log and writer closed by SSTableTxnWriter
  public SSTableTxnWriter createFlushWriter(
      String filename, PartitionColumns columns, EncodingStats stats) {
    // we operate "offline" here, as we expose the resulting reader consciously when done
    // (although we may want to modify this behaviour in future, to encapsulate full flush behaviour
    // in LifecycleTransaction)
    LifecycleTransaction txn = null;
    try {
      txn = LifecycleTransaction.offline(OperationType.FLUSH);
      MetadataCollector sstableMetadataCollector =
          new MetadataCollector(cfs.metadata.comparator)
              .commitLogIntervals(
                  new IntervalSet(commitLogLowerBound.get(), commitLogUpperBound.get()));

      return new SSTableTxnWriter(
          txn,
          cfs.createSSTableMultiWriter(
              Descriptor.fromFilename(filename),
              (long) partitions.size(),
              ActiveRepairService.UNREPAIRED_SSTABLE,
              sstableMetadataCollector,
              new SerializationHeader(true, cfs.metadata, columns, stats),
              txn));
    } catch (Throwable t) {
      if (txn != null) txn.close();
      throw t;
    }
  }
 /** putAll adds all key-value pairs from the given map */
 public void testDescendingPutAll() {
   ConcurrentNavigableMap empty = dmap0();
   ConcurrentNavigableMap map = dmap5();
   empty.putAll(map);
   assertEquals(5, empty.size());
   assertTrue(empty.containsKey(m1));
   assertTrue(empty.containsKey(m2));
   assertTrue(empty.containsKey(m3));
   assertTrue(empty.containsKey(m4));
   assertTrue(empty.containsKey(m5));
 }
 /** putAll adds all key-value pairs from the given map */
 public void testPutAll() {
   ConcurrentNavigableMap empty = map0();
   ConcurrentNavigableMap map = map5();
   empty.putAll(map);
   assertEquals(5, empty.size());
   assertTrue(empty.containsKey(one));
   assertTrue(empty.containsKey(two));
   assertTrue(empty.containsKey(three));
   assertTrue(empty.containsKey(four));
   assertTrue(empty.containsKey(five));
 }
  /** headMap returns map with keys in requested range */
  public void testTailMapContents() {
    ConcurrentNavigableMap map = map5();
    SortedMap sm = map.tailMap(two);
    assertFalse(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertTrue(sm.containsKey(four));
    assertTrue(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(two, k);
    k = (Integer) (i.next());
    assertEquals(three, k);
    k = (Integer) (i.next());
    assertEquals(four, k);
    k = (Integer) (i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry) (ei.next());
    assertEquals(two, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry) (ei.next());
    assertEquals(three, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry) (ei.next());
    assertEquals(four, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry) (ei.next());
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(four);
    assertEquals(four, ssm.firstKey());
    assertEquals(five, ssm.lastKey());
    assertEquals("D", ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
  }
  /** headMap returns map with keys in requested range */
  public void testDescendingTailMapContents() {
    ConcurrentNavigableMap map = dmap5();
    SortedMap sm = map.tailMap(m2);
    assertFalse(sm.containsKey(m1));
    assertTrue(sm.containsKey(m2));
    assertTrue(sm.containsKey(m3));
    assertTrue(sm.containsKey(m4));
    assertTrue(sm.containsKey(m5));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(m2, k);
    k = (Integer) (i.next());
    assertEquals(m3, k);
    k = (Integer) (i.next());
    assertEquals(m4, k);
    k = (Integer) (i.next());
    assertEquals(m5, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry) (ei.next());
    assertEquals(m2, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry) (ei.next());
    assertEquals(m3, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry) (ei.next());
    assertEquals(m4, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry) (ei.next());
    assertEquals(m5, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(m4);
    assertEquals(m4, ssm.firstKey());
    assertEquals(m5, ssm.lastKey());
    assertEquals("D", ssm.remove(m4));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
  }
 /** 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());
 }
 /** 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());
 }
 /** clear removes all pairs */
 public void testClear() {
   ConcurrentNavigableMap map = map5();
   map.clear();
   assertEquals(map.size(), 0);
 }
  private Collection<SSTableReader> writeSortedContents(File sstableDirectory) {
    boolean isBatchLogTable =
        cfs.name.equals(SystemKeyspace.BATCHES)
            && cfs.keyspace.getName().equals(SystemKeyspace.NAME);

    logger.debug("Writing {}", Memtable.this.toString());

    Collection<SSTableReader> ssTables;
    try (SSTableTxnWriter writer =
        createFlushWriter(
            cfs.getSSTablePath(sstableDirectory), columnsCollector.get(), statsCollector.get())) {
      boolean trackContention = logger.isTraceEnabled();
      int heavilyContendedRowCount = 0;
      // (we can't clear out the map as-we-go to free up memory,
      //  since the memtable is being used for queries in the "pending flush" category)
      for (AtomicBTreePartition partition : partitions.values()) {
        // Each batchlog partition is a separate entry in the log. And for an entry, we only do 2
        // operations: 1) we insert the entry and 2) we delete it. Further, BL data is strictly
        // local,
        // we don't need to preserve tombstones for repair. So if both operation are in this
        // memtable (which will almost always be the case if there is no ongoing failure), we can
        // just skip the entry (CASSANDRA-4667).
        if (isBatchLogTable && !partition.partitionLevelDeletion().isLive() && partition.hasRows())
          continue;

        if (trackContention && partition.usePessimisticLocking()) heavilyContendedRowCount++;

        if (!partition.isEmpty()) {
          try (UnfilteredRowIterator iter = partition.unfilteredIterator()) {
            writer.append(iter);
          }
        }
      }

      if (writer.getFilePointer() > 0) {
        logger.debug(
            String.format(
                "Completed flushing %s (%s) for commitlog position %s",
                writer.getFilename(),
                FBUtilities.prettyPrintMemory(writer.getFilePointer()),
                commitLogUpperBound));

        // sstables should contain non-repaired data.
        ssTables = writer.finish(true);
      } else {
        logger.debug(
            "Completed flushing {}; nothing needed to be retained.  Commitlog position was {}",
            writer.getFilename(),
            commitLogUpperBound);
        writer.abort();
        ssTables = Collections.emptyList();
      }

      if (heavilyContendedRowCount > 0)
        logger.trace(
            String.format(
                "High update contention in %d/%d partitions of %s ",
                heavilyContendedRowCount, partitions.size(), Memtable.this.toString()));

      return ssTables;
    }
  }
 /** remove removes the correct key-value pair from the map */
 public void testRemove() {
   ConcurrentNavigableMap map = map5();
   map.remove(five);
   assertEquals(4, map.size());
   assertFalse(map.containsKey(five));
 }
 /** remove removes the correct key-value pair from the map */
 public void testDescendingRemove() {
   ConcurrentNavigableMap map = dmap5();
   map.remove(m5);
   assertEquals(4, map.size());
   assertFalse(map.containsKey(m5));
 }
 /** size returns the correct values */
 public void testSize() {
   ConcurrentNavigableMap map = map5();
   ConcurrentNavigableMap empty = map0();
   assertEquals(0, empty.size());
   assertEquals(5, map.size());
 }
 public int partitionCount() {
   return partitions.size();
 }