Java Code Examples for java.util.NavigableMap#put()

The following examples show how to use java.util.NavigableMap#put() . 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 File: FileLogReader.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
}
 
Example 2
Source File: KafkaValueDeserializer.java    From kareldb with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: TzdbResourceZoneRulesProvider.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 4
Source File: LocalCacheFactoryGenerator.java    From caffeine with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: TestCyclicIteration.java    From RDFS with Apache License 2.0 5 votes vote down vote up
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]);
    }
  }
}
 
Example 6
Source File: TestByteUtil.java    From hraven with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 7
Source File: MockHTable.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: TCKZoneRulesProvider.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 9
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * put(null,x) throws NPE
 */
public void testPut1_NullPointerException() {
    NavigableMap c = map5();
    try {
        c.put(null, "whatever");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 10
Source File: SizeConfigStrategy.java    From giffun with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: SizeConfigStrategy.java    From giffun with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: StreamUtils.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 13
Source File: MapStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 14
Source File: TCKZoneRulesProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
    NavigableMap<String, ZoneRules> result = new TreeMap<>();
    result.put("BarVersion", rules);
    return result;
}
 
Example 15
Source File: TestWALFactory.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: TestLogRollAbort.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
  }
}
 
Example 17
Source File: EnumerationStoreConverter.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 18
Source File: TestWALReaderOnSecureWAL.java    From hbase with Apache License 2.0 4 votes vote down vote up
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);
  }
}
 
Example 19
Source File: VersionedCache.java    From kareldb with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 20
Source File: TCKZoneRulesProvider.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected NavigableMap<String, ZoneRules> provideVersions(String zoneId) {
    NavigableMap<String, ZoneRules> result = new TreeMap<>();
    result.put("BarVersion", rules);
    return result;
}