下面列出了java.util.concurrent.ConcurrentNavigableMap#put ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* pollFirstEntry returns entries in order
*/
public void testPollFirstEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(two, e.getKey());
map.put(one, "A");
e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(three, e.getKey());
map.remove(four);
e = map.pollFirstEntry();
assertEquals(five, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollFirstEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testPollLastEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(four, e.getKey());
map.put(five, "E");
e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(three, e.getKey());
map.remove(two);
e = map.pollLastEntry();
assertEquals(one, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testDescendingPollLastEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m4, e.getKey());
map.put(m5, "E");
e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m3, e.getKey());
map.remove(m2);
e = map.pollLastEntry();
assertEquals(m1, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testPollLastEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(four, e.getKey());
map.put(five, "E");
e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(three, e.getKey());
map.remove(two);
e = map.pollLastEntry();
assertEquals(one, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testDescendingPollLastEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m4, e.getKey());
map.put(m5, "E");
e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m3, e.getKey());
map.remove(m2);
e = map.pollLastEntry();
assertEquals(m1, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* pollFirstEntry returns entries in order
*/
public void testPollFirstEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(two, e.getKey());
map.put(one, "A");
e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(three, e.getKey());
map.remove(four);
e = map.pollFirstEntry();
assertEquals(five, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollFirstEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testPollLastEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(four, e.getKey());
map.put(five, "E");
e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(three, e.getKey());
map.remove(two);
e = map.pollLastEntry();
assertEquals(one, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
private static int estimateRowOverhead(final int count)
{
// calculate row overhead
final OpOrder.Group group = new OpOrder().start();
int rowOverhead;
MemtableAllocator allocator = MEMORY_POOL.newAllocator();
ConcurrentNavigableMap<RowPosition, Object> rows = new ConcurrentSkipListMap<>();
final Object val = new Object();
for (int i = 0 ; i < count ; i++)
rows.put(allocator.clone(new BufferDecoratedKey(new LongToken((long) i), ByteBufferUtil.EMPTY_BYTE_BUFFER), group), val);
double avgSize = ObjectSizes.measureDeep(rows) / (double) count;
rowOverhead = (int) ((avgSize - Math.floor(avgSize)) < 0.05 ? Math.floor(avgSize) : Math.ceil(avgSize));
rowOverhead -= ObjectSizes.measureDeep(new LongToken((long) 0));
rowOverhead += AtomicBTreeColumns.EMPTY_SIZE;
allocator.setDiscarding();
allocator.setDiscarded();
return rowOverhead;
}
@Test public void testSubMap(){
int nums[] = {1,2,3,4,5};
ConcurrentNavigableMap m = new ConcurrentSkipListMap();
for(int a:nums) for(int b:nums){
m.put(t2(a,b),"");
}
assertEquals(5, m.subMap(t2(3, null), t2(3,HI)).size());
assertEquals(3, m.subMap(t2(3, 3), t2(3,HI)).size());
assertEquals(10, m.headMap(t2(3, null)).size());
assertEquals(15, m.tailMap(t2(3, null)).size());
assertEquals(10, m.headMap(t2(2, HI)).size());
assertEquals(15, m.tailMap(t2(2, HI)).size());
}
/**
* pollLastEntry returns entries in order
*/
public void testPollLastEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(four, e.getKey());
map.put(five, "E");
e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(three, e.getKey());
map.remove(two);
e = map.pollLastEntry();
assertEquals(one, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* pollFirstEntry returns entries in order
*/
public void testDescendingPollFirstEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollFirstEntry();
assertEquals(m1, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(m2, e.getKey());
map.put(m1, "A");
e = map.pollFirstEntry();
assertEquals(m1, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(m3, e.getKey());
map.remove(m4);
e = map.pollFirstEntry();
assertEquals(m5, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollFirstEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testDescendingPollLastEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m4, e.getKey());
map.put(m5, "E");
e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m3, e.getKey());
map.remove(m2);
e = map.pollLastEntry();
assertEquals(m1, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* pollLastEntry returns entries in order
*/
public void testDescendingPollLastEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m4, e.getKey());
map.put(m5, "E");
e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m3, e.getKey());
map.remove(m2);
e = map.pollLastEntry();
assertEquals(m1, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
/**
* put(null,x) throws NPE
*/
public void testDescendingPut1_NullPointerException() {
try {
ConcurrentNavigableMap c = dmap5();
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* put(null,x) throws NPE
*/
public void testDescendingPut1_NullPointerException() {
try {
ConcurrentNavigableMap c = dmap5();
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
public static void main(String[] args){
//Configure and open database using builder pattern.
//All options are available with code auto-completion.
File dbFile = Utils.tempDbFile();
DB db = DBMaker.newFileDB(dbFile)
.closeOnJvmShutdown()
.encryptionEnable("password")
.make();
//open an collection, TreeMap has better performance then HashMap
ConcurrentNavigableMap<Integer,String> map = db.getTreeMap("collectionName");
map.put(1,"one");
map.put(2,"two");
//map.keySet() is now [1,2] even before commit
db.commit(); //persist changes into disk
map.put(3,"three");
//map.keySet() is now [1,2,3]
db.rollback(); //revert recent changes
//map.keySet() is now [1,2]
db.close();
}
/**
* put(null,x) throws NPE
*/
public void testPut1_NullPointerException() {
try {
ConcurrentNavigableMap c = map5();
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* Updates last flushed sequence Ids for the regions on server sn
* @param sn
* @param hsl
*/
private void updateLastFlushedSequenceIds(ServerName sn, ServerMetrics hsl) {
for (Entry<byte[], RegionMetrics> entry : hsl.getRegionMetrics().entrySet()) {
byte[] encodedRegionName = Bytes.toBytes(RegionInfo.encodeRegionName(entry.getKey()));
Long existingValue = flushedSequenceIdByRegion.get(encodedRegionName);
long l = entry.getValue().getCompletedSequenceId();
// Don't let smaller sequence ids override greater sequence ids.
if (LOG.isTraceEnabled()) {
LOG.trace(Bytes.toString(encodedRegionName) + ", existingValue=" + existingValue +
", completeSequenceId=" + l);
}
if (existingValue == null || (l != HConstants.NO_SEQNUM && l > existingValue)) {
flushedSequenceIdByRegion.put(encodedRegionName, l);
} else if (l != HConstants.NO_SEQNUM && l < existingValue) {
LOG.warn("RegionServer " + sn + " indicates a last flushed sequence id ("
+ l + ") that is less than the previous last flushed sequence id ("
+ existingValue + ") for region " + Bytes.toString(entry.getKey()) + " Ignoring.");
}
ConcurrentNavigableMap<byte[], Long> storeFlushedSequenceId =
computeIfAbsent(storeFlushedSequenceIdsByRegion, encodedRegionName,
() -> new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR));
for (Entry<byte[], Long> storeSeqId : entry.getValue().getStoreSequenceId().entrySet()) {
byte[] family = storeSeqId.getKey();
existingValue = storeFlushedSequenceId.get(family);
l = storeSeqId.getValue();
if (LOG.isTraceEnabled()) {
LOG.trace(Bytes.toString(encodedRegionName) + ", family=" + Bytes.toString(family) +
", existingValue=" + existingValue + ", completeSequenceId=" + l);
}
// Don't let smaller sequence ids override greater sequence ids.
if (existingValue == null || (l != HConstants.NO_SEQNUM && l > existingValue.longValue())) {
storeFlushedSequenceId.put(family, l);
}
}
}
}
/**
* put(null,x) throws NPE
*/
public void testPut1_NullPointerException() {
try {
ConcurrentNavigableMap c = map5();
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
public SeaCloudsApplicationData addSeaCloudsApplicationData(SeaCloudsApplicationData newApplication) {
ConcurrentNavigableMap<String, SeaCloudsApplicationData> treeMap = dataStore.getTreeMap(SEACLOUDS_APPLICATION_DATA_COLLECTION_TAG);
treeMap.put(newApplication.getSeaCloudsApplicationId(), newApplication);
dataStore.commit();
return newApplication;
}