下面列出了java.util.NavigableMap#put ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private NavigableMap<Long, VersionedValue> toValue(GenericArray<GenericRecord> array) {
NavigableMap<Long, VersionedValue> map = new TreeMap<>();
Schema recordSchema = avroSchema.getElementType();
List<Schema.Field> fields = recordSchema.getFields();
int size = fields.size();
for (GenericRecord record : array) {
Long version = (Long) record.get(0);
Long commit = (Long) record.get(1);
boolean deleted = (Boolean) record.get(2);
Comparable[] row = new Comparable[size - 3];
for (int i = 0; i < row.length; i++) {
Schema schema = fields.get(i + 3).schema();
Comparable value = (Comparable) record.get(i + 3);
row[i] = AvroSchema.fromAvroValue(schema, value);
}
map.put(version, new VersionedValue(version, commit, deleted, row));
}
return map;
}
private void removeStaleKeyFrames() {
Logger.trace("Removing stale key frames");
int size = keyFrames.size();
if (size < KEY_FRAME_BUFFER_MAX_SIZE) {
Logger.trace("Key frame buffer is not full: " + size + (size == 1 ? " entry" : " entries"));
return;
}
// Try to balance the number of key frames.
int window = maxTime / KEY_FRAME_BUFFER_MAX_SIZE;
for (int i = 0; i < maxTime; i += window) {
NavigableMap<Integer, WorldModel<? extends Entity>> next = keyFrames.subMap(i, false, i + window, true);
Logger.trace("Window " + i + " -> " + (i + window) + " has " + next.size() + " entries");
if (next.size() > 1) {
// Remove all but the last entry in this window
Map.Entry<Integer, WorldModel<? extends Entity>> last = next.lastEntry();
next.clear();
next.put(last.getKey(), last.getValue());
Logger.trace("Retained entry " + last);
}
}
Logger.trace("New key frame set: " + keyFrames);
}
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
NavigableMap<String, IndexableField> indexables = new TreeMap<>();
// 兼容UniMi
type = TypeUtility.refineType(type, Map.class);
ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
Type[] types = parameterizedType.getActualTypeArguments();
Type keyType = types[0];
Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
Type valueType = types[1];
Class<?> valueClazz = TypeUtility.getRawType(valueType, null);
try {
// TODO 此处需要代码重构
Map<Object, Object> map = Map.class.cast(instance);
Specification keySpecification = Specification.getSpecification(keyClazz);
StoreConverter keyConverter = context.getStoreConverter(keySpecification);
Specification valueSpecification = Specification.getSpecification(valueClazz);
StoreConverter valueConverter = context.getStoreConverter(valueSpecification);
int size = map.size();
IndexableField indexable = new StoredField(path + ".size", size);
indexables.put(path + ".size", indexable);
int index = 0;
for (Entry<Object, Object> keyValue : map.entrySet()) {
Object key = keyValue.getKey();
indexables.putAll(keyConverter.encode(context, path + "[" + index + "_key]", field, annotation, keyType, key));
Object value = keyValue.getValue();
indexables.putAll(valueConverter.encode(context, path + "[" + index + "_value]", field, annotation, valueType, value));
index++;
}
return indexables;
} catch (Exception exception) {
// TODO
throw new StorageException(exception);
}
}
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
NavigableMap<String, ZoneRules> map = new TreeMap<>();
ZoneRules rules = getRules(zoneId, false);
if (rules != null) {
map.put(versionId, rules);
}
return map;
}
public static <K, V> NavigableMap<K, V> mapFromStore(KafkaStreams streams, String storeName) {
final ReadOnlyKeyValueStore<K, V> store = streams.store(
storeName, QueryableStoreTypes.keyValueStore());
try (final KeyValueIterator<K, V> all = store.all()) {
NavigableMap<K, V> result = new TreeMap<>();
while (all.hasNext()) {
KeyValue<K, V> next = all.next();
result.put(next.key, next.value);
}
return result;
}
}
@Override
public void put(Bitmap bitmap) {
int size = Util.getBitmapByteSize(bitmap);
Key key = keyPool.get(size, bitmap.getConfig());
groupedMap.put(key, bitmap);
NavigableMap<Integer, Integer> sizes = getSizesForConfig(bitmap.getConfig());
Integer current = sizes.get(key.size);
sizes.put(key.size, current == null ? 1 : current + 1);
}
private void decrementBitmapOfSize(Integer size, Bitmap.Config config) {
NavigableMap<Integer, Integer> sizes = getSizesForConfig(config);
Integer current = sizes.get(size);
if (current == 1) {
sizes.remove(size);
} else {
sizes.put(size, current - 1);
}
}
/**
* put(null,x) throws NPE
*/
public void testPut1_NullPointerException() {
NavigableMap c = map5();
try {
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
NavigableMap<String, ZoneRules> result = new TreeMap<>();
result.put("DynamicVersion1", BASE);
if (count > 2) {
result.put("DynamicVersion2", ALTERNATE);
}
return result;
}
private <K, V> V forceFind(NavigableMap<K, V> map, K key, V newObject) {
V data = map.get(key);
if (data == null) {
data = newObject;
map.put(key, data);
}
return data;
}
/**
* test get value as String
*/
@Test
public void testGetValueAsString2() {
NavigableMap<byte[], byte[]> infoValues = new TreeMap<byte[], byte[]>(Bytes.BYTES_COMPARATOR);
infoValues.put(Constants.VERSION_COLUMN_BYTES, Bytes.toBytes(JobDetailsValues.version));
assertEquals(JobDetailsValues.version,
ByteUtil.getValueAsString(Constants.VERSION_COLUMN_BYTES, infoValues));
// test non existent values
assertEquals("", ByteUtil.getValueAsString(Constants.HRAVEN_QUEUE_BYTES, infoValues));
}
private static void checkCyclicIteration(int numOfElements) {
//create a tree map
final NavigableMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
final Integer[] integers = new Integer[numOfElements];
for(int i = 0; i < integers.length; i++) {
integers[i] = 2*i;
map.put(integers[i], integers[i]);
}
System.out.println("\n\nintegers=" + Arrays.asList(integers));
System.out.println("map=" + map);
//try starting everywhere
for(int start = -1; start <= 2*integers.length - 1; start++) {
//get a cyclic iteration
final List<Integer> iteration = new ArrayList<Integer>();
for(Map.Entry<Integer, Integer> e : new CyclicIteration<Integer, Integer>(map, start)) {
iteration.add(e.getKey());
}
System.out.println("start=" + start + ", iteration=" + iteration);
//verify results
for(int i = 0; i < integers.length; i++) {
final int j = ((start+2)/2 + i)%integers.length;
assertEquals("i=" + i + ", j=" + j, iteration.get(i), integers[j]);
}
}
}
private NavigableMap<String, Set<Feature>> getClassNameToFeatures() {
NavigableMap<String, Set<Feature>> classNameToFeatures = new TreeMap<>();
for (List<Object> combination : combinations()) {
Set<Feature> features = getFeatures(combination);
String className = encode(Feature.makeClassName(features));
classNameToFeatures.put(className, features);
}
return classNameToFeatures;
}
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
NavigableMap<String, ZoneRules> result = new TreeMap<>();
result.put("BarVersion", rules);
return result;
}
public void remove(Comparable[] key, long version) {
NavigableMap<Long, VersionedValue> rowData = cache.getOrDefault(key, new ConcurrentSkipListMap<>());
rowData.put(version, new VersionedValue(version, PENDING_TX, true, EMPTY_VALUE));
garbageCollect(rowData);
cache.put(key, rowData);
}
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
NavigableMap<String, ZoneRules> result = new TreeMap<>();
result.put("BarVersion", rules);
return result;
}
/**
* Just write multiple logs then split. Before fix for HADOOP-2283, this
* would fail.
* @throws IOException
*/
@Test
public void testSplit() throws IOException {
final TableName tableName = TableName.valueOf(currentTest.getMethodName());
final byte [] rowName = tableName.getName();
final MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl(1);
final int howmany = 3;
RegionInfo[] infos = new RegionInfo[3];
Path tableDataDir = CommonFSUtils.getTableDir(hbaseDir, tableName);
fs.mkdirs(tableDataDir);
Path tabledir = CommonFSUtils.getWALTableDir(conf, tableName);
fs.mkdirs(tabledir);
for (int i = 0; i < howmany; i++) {
infos[i] = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes("" + i))
.setEndKey(Bytes.toBytes("" + (i + 1))).build();
fs.mkdirs(new Path(tabledir, infos[i].getEncodedName()));
fs.mkdirs(new Path(tableDataDir, infos[i].getEncodedName()));
LOG.info("allo " + new Path(tabledir, infos[i].getEncodedName()).toString());
}
NavigableMap<byte[], Integer> scopes = new TreeMap<>(Bytes.BYTES_COMPARATOR);
scopes.put(Bytes.toBytes("column"), 0);
// Add edits for three regions.
for (int ii = 0; ii < howmany; ii++) {
for (int i = 0; i < howmany; i++) {
final WAL log =
wals.getWAL(infos[i]);
for (int j = 0; j < howmany; j++) {
WALEdit edit = new WALEdit();
byte [] family = Bytes.toBytes("column");
byte [] qualifier = Bytes.toBytes(Integer.toString(j));
byte [] column = Bytes.toBytes("column:" + Integer.toString(j));
edit.add(new KeyValue(rowName, family, qualifier,
System.currentTimeMillis(), column));
LOG.info("Region " + i + ": " + edit);
WALKeyImpl walKey = new WALKeyImpl(infos[i].getEncodedNameAsBytes(), tableName,
System.currentTimeMillis(), mvcc, scopes);
log.appendData(infos[i], walKey, edit);
walKey.getWriteEntry();
}
log.sync();
log.rollWriter(true);
}
}
wals.shutdown();
// The below calculation of logDir relies on insider information... WALSplitter should be connected better
// with the WAL system.... not requiring explicit path. The oldLogDir is just made up not used.
Path logDir =
new Path(new Path(hbaseWALDir, HConstants.HREGION_LOGDIR_NAME),
this.currentServername.toString());
Path oldLogDir = new Path(hbaseDir, HConstants.HREGION_OLDLOGDIR_NAME);
List<Path> splits = WALSplitter.split(hbaseWALDir, logDir, oldLogDir, fs, conf, wals);
verifySplits(splits, howmany);
}
/**
* Tests the case where a RegionServer enters a GC pause,
* comes back online after the master declared it dead and started to split.
* Want log rolling after a master split to fail. See HBASE-2312.
*/
@Test
public void testLogRollAfterSplitStart() throws IOException {
LOG.info("Verify wal roll after split starts will fail.");
String logName = ServerName.valueOf("testLogRollAfterSplitStart",
16010, System.currentTimeMillis()).toString();
Path thisTestsDir = new Path(HBASELOGDIR, AbstractFSWALProvider.getWALDirectoryName(logName));
final WALFactory wals = new WALFactory(conf, logName);
try {
// put some entries in an WAL
TableName tableName =
TableName.valueOf(this.getClass().getName());
RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build();
WAL log = wals.getWAL(regionInfo);
MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl(1);
int total = 20;
for (int i = 0; i < total; i++) {
WALEdit kvs = new WALEdit();
kvs.add(new KeyValue(Bytes.toBytes(i), tableName.getName(), tableName.getName()));
NavigableMap<byte[], Integer> scopes = new TreeMap<>(Bytes.BYTES_COMPARATOR);
scopes.put(Bytes.toBytes("column"), 0);
log.appendData(regionInfo, new WALKeyImpl(regionInfo.getEncodedNameAsBytes(), tableName,
System.currentTimeMillis(), mvcc, scopes), kvs);
}
// Send the data to HDFS datanodes and close the HDFS writer
log.sync();
((AbstractFSWAL<?>) log).replaceWriter(((FSHLog)log).getOldPath(), null, null);
// code taken from MasterFileSystem.getLogDirs(), which is called from
// MasterFileSystem.splitLog() handles RS shutdowns (as observed by the splitting process)
// rename the directory so a rogue RS doesn't create more WALs
Path rsSplitDir = thisTestsDir.suffix(AbstractFSWALProvider.SPLITTING_EXT);
if (!fs.rename(thisTestsDir, rsSplitDir)) {
throw new IOException("Failed fs.rename for log split: " + thisTestsDir);
}
LOG.debug("Renamed region directory: " + rsSplitDir);
LOG.debug("Processing the old log files.");
WALSplitter.split(HBASELOGDIR, rsSplitDir, OLDLOGDIR, fs, conf, wals);
LOG.debug("Trying to roll the WAL.");
try {
log.rollWriter();
Assert.fail("rollWriter() did not throw any exception.");
} catch (IOException ioe) {
if (ioe.getCause() instanceof FileNotFoundException) {
LOG.info("Got the expected exception: ", ioe.getCause());
} else {
Assert.fail("Unexpected exception: " + ioe);
}
}
} finally {
wals.close();
if (fs.exists(thisTestsDir)) {
fs.delete(thisTestsDir, true);
}
}
}
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
NavigableMap<String, IndexableField> indexables = new TreeMap<>();
indexables.put(path, new StoredField(path, instance.toString()));
return indexables;
}
private Path writeWAL(final WALFactory wals, final String tblName, boolean offheap)
throws IOException {
Configuration conf = TEST_UTIL.getConfiguration();
String clsName = conf.get(WALCellCodec.WAL_CELL_CODEC_CLASS_KEY, WALCellCodec.class.getName());
conf.setClass(WALCellCodec.WAL_CELL_CODEC_CLASS_KEY, SecureWALCellCodec.class,
WALCellCodec.class);
try {
TableName tableName = TableName.valueOf(tblName);
NavigableMap<byte[], Integer> scopes = new TreeMap<>(Bytes.BYTES_COMPARATOR);
scopes.put(tableName.getName(), 0);
RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build();
final int total = 10;
final byte[] row = Bytes.toBytes("row");
final byte[] family = Bytes.toBytes("family");
final MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl(1);
// Write the WAL
WAL wal = wals.getWAL(regionInfo);
for (int i = 0; i < total; i++) {
WALEdit kvs = new WALEdit();
KeyValue kv = new KeyValue(row, family, Bytes.toBytes(i), value);
if (offheap) {
ByteBuffer bb = ByteBuffer.allocateDirect(kv.getBuffer().length);
bb.put(kv.getBuffer());
ByteBufferKeyValue offheapKV = new ByteBufferKeyValue(bb, 0, kv.getLength());
kvs.add(offheapKV);
} else {
kvs.add(kv);
}
wal.appendData(regionInfo, new WALKeyImpl(regionInfo.getEncodedNameAsBytes(), tableName,
System.currentTimeMillis(), mvcc, scopes), kvs);
}
wal.sync();
final Path walPath = AbstractFSWALProvider.getCurrentFileName(wal);
wal.shutdown();
return walPath;
} finally {
// restore the cell codec class
conf.set(WALCellCodec.WAL_CELL_CODEC_CLASS_KEY, clsName);
}
}