public MapDBSortedMap(File f) { this.db = DBMaker.fileDB(f).closeOnJvmShutdown().transactionEnable().make(); this.treeMap = db.treeMap(f.getName()) .keySerializer(Serializer.LONG) .valueSerializer(Serializer.BYTE_ARRAY) .createOrOpen(); }
@VisibleForTesting TreeMapMaker<Object[], byte[]> getTableMaker( final String tableName, final MapStoreKey key ) { final SerializerArrayTuple storeKeySerializer = createKeySerializer(key.getHashValue().getClass(), key.getRangeField().flatMap(rangeField -> { final Optional<Comparable<?>> rangeValue = Optional.ofNullable(key.getRangeKey().getValue()); return rangeValue.map(rValue -> rValue.getClass()); })); return mapDB.treeMap(tableName) .keySerializer(storeKeySerializer) .valueSerializer(Serializer.BYTE_ARRAY); }
/** * @param mapId physical map id, used to decide whether to reuse or not. */ @SuppressWarnings("unused") public StreamSortedWindowInMapDB(long start, long end, long margin, DB db, String mapId) { super(start, end, margin); this.mapId = mapId; try { btreeMap = db.<Long, StreamEvent>treeMap(mapId) .keySerializer(Serializer.LONG) .valueSerializer(STREAM_EVENT_GROUP_SERIALIZER) .createOrOpen(); LOG.debug("Created BTree map {}", mapId); } catch (Error error) { LOG.info("Failed create BTree {}", mapId, error); } size = new AtomicInteger(0); }
@Override public boolean createIndex( final MapStoreIndexName indexName, final MapStoreTableKey indexKey ) { // Create a new index table using the map store final MapDBIndexMetadata indexMetadata = indexMetadata(indexName, indexKey); final boolean createdIndex = mapStore.createTable(indexMetadata.toString(), indexKey); if (createdIndex) { // Copy all table records to the new index final Function<byte[], Object[]> indexKeyFunction = indexKeyFunction(indexMetadata); final BTreeMap<?, byte[]> table = mapDB.treeMap(indexName.getTableName()) .valueSerializer(Serializer.BYTE_ARRAY) .open(); final BTreeMap<Object[], byte[]> index = mapDB.treeMap(indexMetadata.toString()) .keySerializer(createKeySerializer(indexKey.getHashFieldType(), indexKey.getRangeFieldType())) .valueSerializer(Serializer.BYTE_ARRAY) .open(); table.getValues().forEach((byte[] value) -> { final Object[] newKey = indexKeyFunction.apply(value); index.put(newKey, value); }); } return createdIndex; }
@Override public void setup(final BenchmarkParams b) throws IOException { super.setup(b); wkb = new UnsafeBuffer(new byte[keySize]); wvb = new UnsafeBuffer(new byte[valSize]); db = fileDB(new File(tmp, "map.db")) .fileMmapEnable() .concurrencyDisable() .allocateStartSize(num * valSize) .make(); map = db.treeMap("ba2ba") .keySerializer(BYTE_ARRAY) .valueSerializer(BYTE_ARRAY) .createOrOpen(); }
@Test public void testOnHeapDB() { DB db = DBMaker.heapDB().make(); BTreeMap<Long, String> map = db.treeMap("btree").keySerializer(Serializer.LONG).valueSerializer(Serializer.STRING).create(); Assert.assertFalse(map.putIfAbsentBoolean(1L, "val_1")); Assert.assertTrue(map.putIfAbsentBoolean(1L, "val_2")); Assert.assertTrue(map.putIfAbsentBoolean(1L, "val_3")); Assert.assertFalse(map.putIfAbsentBoolean(2L, "val_4")); Assert.assertEquals("val_1", map.get(1L)); Assert.assertEquals("val_4", map.get(2L)); Assert.assertTrue(map.replace(2L, "val_4", "val_5")); Assert.assertEquals("val_5", map.get(2L)); map.close(); db.close(); } }
MapDBUpdateIndexListener( final DB mapDB, final MapDBIndexMetadata indexMetadata ) { this.keyFunction = MapDBMapStoreIndexPlugin.indexKeyFunction(indexMetadata); this.index = mapDB.treeMap(indexMetadata.toString()) .keySerializer(createKeySerializer( indexMetadata.getIndexKey().getHashFieldType(), indexMetadata.getIndexKey().getRangeFieldType() )) .valueSerializer(Serializer.BYTE_ARRAY) .open(); }
@Override public boolean createTable( final String tableName, final MapStoreTableKey tableKey ) { final SerializerArrayTuple tableKeySerializer = createKeySerializer(tableKey.getHashFieldType(), tableKey.getRangeFieldType()); mapDB.treeMap(tableName) .keySerializer(tableKeySerializer) .valueSerializer(Serializer.BYTE_ARRAY) .createOrOpen(); return true; }