org.mapdb.Fun Java Examples

The following examples show how to use org.mapdb.Fun. 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: NameSaleMap.java    From Qora with MIT License 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<NameSale> get(Account account)
{
	List<NameSale> nameSales = new ArrayList<NameSale>();
	
	try
	{
		Map<Tuple2<String, String>, BigDecimal> accountNames = ((BTreeMap) this.map).subMap(
				Fun.t2(account.getAddress(), null),
				Fun.t2(account.getAddress(), Fun.HI()));
		
		for(Entry<Tuple2<String, String>, BigDecimal> entry: accountNames.entrySet())
		{
			NameSale nameSale = new NameSale(entry.getKey().b, entry.getValue());
			nameSales.add(nameSale);
		}
	}
	catch(Exception e)
	{
		//ERROR
		e.printStackTrace();
	}
	
	return nameSales;
}
 
Example #2
Source File: BlockMap.java    From Qora with MIT License 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
protected void createIndexes(DB database)
{
	//HEIGHT INDEX
	Tuple2Comparator<Integer, byte[]> comparator = new Fun.Tuple2Comparator<Integer, byte[]>(Fun.COMPARATOR, UnsignedBytes.lexicographicalComparator());
	NavigableSet<Tuple2<Integer, byte[]>> heightIndex = database.createTreeSet("blocks_index_height")
			.comparator(comparator)
			.makeOrGet();
	
	NavigableSet<Tuple2<Integer, byte[]>> descendingHeightIndex = database.createTreeSet("blocks_index_height_descending")
			.comparator(new ReverseComparator(comparator))
			.makeOrGet();
	
	createIndex(HEIGHT_INDEX, heightIndex, descendingHeightIndex, new Fun.Function2<Integer, byte[], Block>() {
	   	@Override
	    public Integer run(byte[] key, Block value) {
	   		return value.getHeight();
	    }
	});
}
 
Example #3
Source File: OSMTest.java    From osm-lib with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void testOSM(){
	OSM osm = new OSM("./src/test/resources/tmp");
	osm.readFromFile("./src/test/resources/bangor_maine.osm.pbf");
	assertEquals( osm.nodes.size(), 35747 );
	assertEquals( osm.ways.size(), 2976 );
	assertEquals( osm.relations.size(), 34 );

	// make sure the indices work
	for (Map.Entry<Long, Relation> e : osm.relations.entrySet()) {
		Relation relation = e.getValue();
		long id = e.getKey();
		// Tested: Bangor contains relations with way, node, and relation members
		for (Relation.Member member : relation.members) {
			if (member.type == OSMEntity.Type.NODE)
				assertTrue(osm.relationsByNode.contains(Fun.t2(member.id, id)));
			else if (member.type == OSMEntity.Type.WAY)
				assertTrue(osm.relationsByWay.contains(Fun.t2(member.id, id)));
			else if (member.type == OSMEntity.Type.RELATION)
				assertTrue(osm.relationsByRelation.contains(Fun.t2(member.id, id)));
		}
	}
}
 
Example #4
Source File: TrafficEngine.java    From traffic-engine with GNU General Public License v3.0 6 votes vote down vote up
public List<SpatialDataItem> getStreetSegmentsBySegmentId(Fun.Tuple3<Long, Long, Long> segmentId) {
	List<SpatialDataItem> edges = new ArrayList<>();

	StreetSegment sdi = (StreetSegment)osmData.streetSegments.getBySegmentId(segmentId);
	if(sdi == null) {
		Jumper j = osmData.jumperDataStore.getJumper(segmentId.b, segmentId.c);
		if (j != null) {
			for (long id : j.segments) {
				sdi = (StreetSegment) osmData.streetSegments.getById(id);
				if (sdi != null)
					edges.add(sdi);
			}
		}
	}
	else
		edges.add(sdi);

	return edges;
}
 
Example #5
Source File: OrderMap.java    From Qora with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<BigInteger, Order> openMap(DB database)
{
	//OPEN MAP
	BTreeMap<BigInteger, Order> map = database.createTreeMap("orders")
			.valueSerializer(new OrderSerializer())
			.makeOrGet();
	
	//HAVE/WANT KEY
	this.haveWantKeyMap = database.createTreeMap("orders_key_have_want")
			.comparator(Fun.COMPARATOR)
			.makeOrGet();
	
	//BIND HAVE/WANT KEY
	Bind.secondaryKey(map, this.haveWantKeyMap, new Fun.Function2<Tuple4<Long, Long, BigDecimal, BigInteger>, BigInteger, Order>() {
		@Override
		public Tuple4<Long, Long, BigDecimal, BigInteger> run(BigInteger key, Order value) {
			return new Tuple4<Long, Long, BigDecimal, BigInteger>(value.getHave(), value.getWant(), value.getPrice(), key);
		}	
	});
	
	//RETURN
	return map;
}
 
Example #6
Source File: OSMDataStore.java    From traffic-engine with GNU General Public License v3.0 6 votes vote down vote up
public void loadPbfFile(Fun.Tuple2<Integer, Integer> tile, Envelope env, File pbfFile) {

		log.log(Level.INFO, "loading osm from: " + pbfFile.getAbsolutePath());

		// load pbf osm source and merge into traffic engine
		OSM osm = new OSM(null);
		osm.readFromFile(pbfFile.getAbsolutePath().toString());
		try {
			// add OSM an truncate geometries
			OSMArea osmArea = addOsm(tile, env, osm, false);
		}
		catch (Exception e) {
			e.printStackTrace();
			log.log(Level.SEVERE, "Unable to load osm: " + pbfFile.getAbsolutePath());
		}
		finally {
			osm.close();
		}
	}
 
Example #7
Source File: Histogram.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    HTreeMap<Long, Double> map = DBMaker.newTempHashMap();

    // histogram, category is a key, count is a value
    ConcurrentMap<String, Long> histogram = new ConcurrentHashMap<String, Long>(); //any map will do

    // bind histogram to primary map
    // we need function which returns category for each map entry
    Bind.histogram(map, histogram, new Fun.Function2<String, Long, Double>(){
        @Override
        public String run(Long key, Double value) {
            if(value<0.25) return "first quarter";
            else if(value<0.5) return "second quarter";
            else if(value<0.75) return "third quarter";
            else return "fourth quarter";
        }
    });

    //insert some random stuff
    for(long key=0;key<1e4;key++){
        map.put(key, Math.random());
    }

    System.out.println(histogram);
}
 
Example #8
Source File: NameExchangeMap.java    From Qora with MIT License 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void createIndexes(DB database)
{
	//AMOUNT INDEX
	NavigableSet<Tuple2<BigDecimal, String>> namesIndex = database.createTreeSet("namesales_index_amount")
			.comparator(Fun.COMPARATOR)
			.makeOrGet();
	
	NavigableSet<Tuple2<BigDecimal, String>> descendingNamesIndex = database.createTreeSet("namesales_index_amount_descending")
			.comparator(new ReverseComparator(Fun.COMPARATOR))
			.makeOrGet();
	
	createIndex(AMOUNT_INDEX, namesIndex, descendingNamesIndex, new Fun.Function2<BigDecimal, String, BigDecimal>() {
	   	@Override
	    public BigDecimal run(String key, BigDecimal value) {
	   		return value;
	    }
	});
}
 
Example #9
Source File: TransactionMap.java    From Qora with MIT License 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void createIndexes(DB database)
{
	//TIMESTAMP INDEX
	Tuple2Comparator<Long, byte[]> comparator = new Fun.Tuple2Comparator<Long, byte[]>(Fun.COMPARATOR, UnsignedBytes.lexicographicalComparator());
	NavigableSet<Tuple2<Integer, byte[]>> heightIndex = database.createTreeSet("transactions_index_timestamp")
			.comparator(comparator)
			.makeOrGet();
			
	NavigableSet<Tuple2<Integer, byte[]>> descendingHeightIndex = database.createTreeSet("transactions_index_timestamp_descending")
			.comparator(new ReverseComparator(comparator))
			.makeOrGet();
			
	createIndex(TIMESTAMP_INDEX, heightIndex, descendingHeightIndex, new Fun.Function2<Long, byte[], Transaction>() {
	   	@Override
	    public Long run(byte[] key, Transaction value) {
	   		return value.getTimestamp();
	    }
	});
}
 
Example #10
Source File: VehicleStates.java    From traffic-engine with GNU General Public License v3.0 6 votes vote down vote up
public void placeVehicleInTile(Fun.Tuple2<Integer, Integer> tile, Long vehicleId) {
	synchronized (vehicleCache) {
		if(!tileVehicleMap.containsKey(tile)) {
			tileVehicleMap.put(tile, new ConcurrentHashMap<>());
		}

		if(!tileCount.containsKey(tile)){
			tileCount.put(tile, new AtomicInteger());
		}

		if(tileVehicleMap.get(tile).containsKey(vehicleId)) {
			tileCount.get(tile).decrementAndGet();
			tileVehicleMap.get(tile).remove(vehicleId);
		}

		tileCount.get(tile).incrementAndGet();
		tileVehicleMap.get(tile).put(vehicleId, true);
	}
}
 
Example #11
Source File: TradeMap.java    From Qora with MIT License 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public SortableList<Tuple2<BigInteger, BigInteger>, Trade> getTradesSortableList(long have, long want)
{
	String pairKey;
	if(have > want)
	{
		pairKey = have + "/" + want;
	}
	else
	{
		pairKey = want + "/" + have;
	}
	
	//FILTER ALL KEYS
	Collection<Tuple2<BigInteger, BigInteger>> keys = ((BTreeMap<Tuple3, Tuple2<BigInteger, BigInteger>>) this.pairKeyMap).subMap(
			Fun.t3(pairKey, null, null),
			Fun.t3(pairKey, Fun.HI(), Fun.HI())).values();
	
	//RETURN
	return new SortableList<Tuple2<BigInteger, BigInteger>, Trade>(this, keys);
}
 
Example #12
Source File: BalanceMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public SortableList<Tuple2<String, Long>, BigDecimal> getBalancesSortableList(Account account) 
{
	BTreeMap map = (BTreeMap) this.map;
	
	//FILTER ALL KEYS
	Collection keys = ((BTreeMap<Tuple2, BigDecimal>) map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI())).keySet();
	
	//RETURN
	return new SortableList<Tuple2<String, Long>, BigDecimal>(this, keys);
}
 
Example #13
Source File: PollMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void delete(Account account)
{
	//GET ALL POLLS THAT BELONG TO THAT ADDRESS
	Map<Tuple2<String, String>, Poll> accountPolls = ((BTreeMap) this.map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI()));
	
	//DELETE NAMES
	for(Tuple2<String, String> key: accountPolls.keySet())
	{
		this.delete(key);
	}
}
 
Example #14
Source File: BalanceMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public SortableList<Tuple2<String, Long>, BigDecimal> getBalancesSortableList(long key)
{
	//FILTER ALL KEYS
	Collection<Tuple2<String, Long>> keys = ((BTreeMap<Tuple3, Tuple2<String, Long>>) this.assetKeyMap).subMap(
			Fun.t3(key, null, null),
			Fun.t3(key, Fun.HI(), Fun.HI())).values();
	
	//RETURN
	return new SortableList<Tuple2<String, Long>, BigDecimal>(this, keys);
}
 
Example #15
Source File: BlockMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void delete(Account account)
{
	//GET ALL TRANSACTIONS THAT BELONG TO THAT ADDRESS
	Map<Tuple2<String, String>, Block> accountBlocks = ((BTreeMap) this.map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI()));
	
	//DELETE TRANSACTIONS
	for(Tuple2<String, String> key: accountBlocks.keySet())
	{
		this.delete(key);
	}
}
 
Example #16
Source File: BalanceMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked"})
@Override
protected Map<Tuple2<String, Long>, BigDecimal> getMap(DB database) 
{
	//OPEN MAP
	BTreeMap<Tuple2<String, Long>, BigDecimal> map =  database.createTreeMap("balances")
			.keySerializer(BTreeKeySerializer.TUPLE2)
			.counterEnable()
			.makeOrGet();
	
	//HAVE/WANT KEY
	this.assetKeyMap = database.createTreeMap("balances_key_asset")
			.comparator(Fun.COMPARATOR)
			.counterEnable()
			.makeOrGet();
	
	//BIND ASSET KEY
	Bind.secondaryKey(map, this.assetKeyMap, new Fun.Function2<Tuple3<Long, BigDecimal, String>, Tuple2<String, Long>, BigDecimal>() {
		@Override
		public Tuple3<Long, BigDecimal, String> run(Tuple2<String, Long> key, BigDecimal value) {
			return new Tuple3<Long, BigDecimal, String>(key.b, value.negate(), key.a);
		}	
	});
	
	//RETURN
	return map;
}
 
Example #17
Source File: AssetMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Asset> get(Account account)
{
	List<Asset> assets = new ArrayList<Asset>();
	
	try
	{
		Map<Tuple2<String, String>, Asset> accountAssets = ((BTreeMap) this.map).subMap(
				Fun.t2(account.getAddress(), null),
				Fun.t2(account.getAddress(), Fun.HI()));
		
		//GET ITERATOR
		Iterator<Asset> iterator = accountAssets.values().iterator();
		
		while(iterator.hasNext())
		{
			assets.add(iterator.next());
		}
	}
	catch(Exception e)
	{
		//ERROR
		e.printStackTrace();
	}
	
	return assets;
}
 
Example #18
Source File: OrderMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public SortableList<BigInteger, Order> getOrdersSortableList(long have, long want)
{
	//FILTER ALL KEYS
	Collection<BigInteger> keys = ((BTreeMap<Tuple4, BigInteger>) this.haveWantKeyMap).subMap(
			Fun.t4(have, want, null, null),
			Fun.t4(have, want, Fun.HI(), Fun.HI())).values();
	
	//RETURN
	return new SortableList<BigInteger, Order>(this, keys);
}
 
Example #19
Source File: OrderMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
private Collection<BigInteger> getKeys(long have, long want) {
	
	//FILTER ALL KEYS
	Collection<BigInteger> keys = ((BTreeMap<Tuple4, BigInteger>) this.haveWantKeyMap).subMap(
			Fun.t4(have, want, null, null),
			Fun.t4(have, want, Fun.HI(), Fun.HI())).values();
	
	//IF THIS IS A FORK
	if(this.parent != null)
	{
		//GET ALL KEYS FOR FORK
		Collection<BigInteger> forkKeys = ((OrderMap) this.parent).getKeys(have, want);
		
		//COMBINE LISTS
		Set<BigInteger> combinedKeys = new TreeSet<BigInteger>(keys);
		combinedKeys.addAll(forkKeys);
		
		//DELETE DELETED
		for(BigInteger deleted: this.deleted)
		{
			combinedKeys.remove(deleted);
		}
		
		//CONVERT SET BACK TO COLLECTION
		keys = combinedKeys;
	}
	
	return keys;
}
 
Example #20
Source File: AssetMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void delete(Account account)
{
	//GET ALL POLLS THAT BELONG TO THAT ADDRESS
	Map<Tuple2<String, String>, Asset> accountAssets = ((BTreeMap) this.map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI()));
	
	//DELETE NAMES
	for(Tuple2<String, String> key: accountAssets.keySet())
	{
		this.delete(key);
	}
}
 
Example #21
Source File: BlockMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Block> get(Account account)
{
	List<Block> blocks = new ArrayList<Block>();
	
	try
	{
		Map<Tuple2<String, String>, Block> accountBlocks = ((BTreeMap) this.map).subMap(
				Fun.t2(account.getAddress(), null),
				Fun.t2(account.getAddress(), Fun.HI()));
		
		//GET ITERATOR
		Iterator<Block> iterator = accountBlocks.values().iterator();
		
		while(iterator.hasNext())
		{
			blocks.add(iterator.next());
		}
	}
	catch(Exception e)
	{
		//ERROR
		e.printStackTrace();
	}
	
	return blocks;
}
 
Example #22
Source File: PollMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Poll> get(Account account)
{
	List<Poll> polls = new ArrayList<Poll>();
	
	try
	{
		Map<Tuple2<String, String>, Poll> accountPolls = ((BTreeMap) this.map).subMap(
				Fun.t2(account.getAddress(), null),
				Fun.t2(account.getAddress(), Fun.HI()));
		
		//GET ITERATOR
		Iterator<Poll> iterator = accountPolls.values().iterator();
		
		while(iterator.hasNext())
		{
			polls.add(iterator.next());
		}
	}
	catch(Exception e)
	{
		//ERROR
		e.printStackTrace();
	}
	
	return polls;
}
 
Example #23
Source File: OrderMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void delete(Account account)
{
	//GET ALL ORDERS THAT BELONG TO THAT ADDRESS
	Map<Tuple2<String, BigInteger>, Order> accountOrders = ((BTreeMap) this.map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI()));
	
	//DELETE NAMES
	for(Tuple2<String, BigInteger> key: accountOrders.keySet())
	{
		this.delete(key);
	}
}
 
Example #24
Source File: OSMDataStore.java    From traffic-engine with GNU General Public License v3.0 5 votes vote down vote up
public OSMArea checkOsm(double lat, double lon) {

		Fun.Tuple2<Integer, Integer> tile = getOsmId(lat, lon);

		if(!osmAreas.containsKey(tile)){
			synchronized (this){
				loadingOSM = true;
				loadOSMTile(tile);
				loadingOSM = false;
			}
		}
		return osmAreas.get(tile);
	}
 
Example #25
Source File: TransactionMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Transaction> get(Account account, int limit)
{
	List<Transaction> transactions = new ArrayList<Transaction>();
	
	try
	{
		//GET ALL TRANSACTIONS THAT BELONG TO THAT ADDRESS
		/*Map<Tuple2<String, String>, Transaction> accountTransactions = ((BTreeMap) this.map).subMap(
				Fun.t2(null, account.getAddress()),
				Fun.t2(Fun.HI(), account.getAddress()));*/
		
		Map<Tuple2<String, String>, Transaction> accountTransactions = ((BTreeMap) this.map).subMap(
				Fun.t2(account.getAddress(), null),
				Fun.t2(account.getAddress(), Fun.HI()));
		
		//GET ITERATOR
		Iterator<Transaction> iterator = accountTransactions.values().iterator();
		
		//RETURN {LIMIT} TRANSACTIONS
		int counter = 0;
		while(iterator.hasNext() && counter < limit)
		{
			transactions.add(iterator.next());
			counter++;
		}
	}
	catch(Exception e)
	{
		//ERROR
		e.printStackTrace();
	}
	
	return transactions;
}
 
Example #26
Source File: TransactionMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void delete(Account account)
{
	//GET ALL TRANSACTIONS THAT BELONG TO THAT ADDRESS
	Map<Tuple2<String, String>, Transaction> accountTransactions = ((BTreeMap) this.map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI()));
	
	//DELETE TRANSACTIONS
	for(Tuple2<String, String> key: accountTransactions.keySet())
	{
		this.delete(key);
	}
}
 
Example #27
Source File: VehicleStates.java    From traffic-engine with GNU General Public License v3.0 5 votes vote down vote up
public static Map<Fun.Tuple2<Integer, Integer>, AtomicInteger> sortByValue(Map<Fun.Tuple2<Integer, Integer>, AtomicInteger> map)
{
	Map<Fun.Tuple2<Integer, Integer>, AtomicInteger> result = new LinkedHashMap<>();
	Stream<Map.Entry<Fun.Tuple2<Integer, Integer>, AtomicInteger>> st = map.entrySet().stream();

	Comparator<Map.Entry<Fun.Tuple2<Integer, Integer>, AtomicInteger>> comparator =  Comparator.comparing(e -> e.getValue().get());

	st.sorted(comparator.reversed())
			.forEach(e -> result.put(e.getKey(), e.getValue()));

	return result;
}
 
Example #28
Source File: VehicleStates.java    From traffic-engine with GNU General Public License v3.0 5 votes vote down vote up
public List<GPSPoint> getVehicleTilePoints(Fun.Tuple2<Integer, Integer> tile) {
	List<GPSPoint> points = new ArrayList<>();

	if(tileVehicleMap.containsKey(tile)) {
		for (Long vehicleId : tileVehicleMap.get(tile).keySet()) {
			points.add(vehicleCache.get(vehicleId).lastPoint);
		}
	}

	return points;
}
 
Example #29
Source File: NameSaleMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void delete(Account account)
{
	//GET ALL NAMES THAT BELONG TO THAT ADDRESS
	Map<Tuple2<String, String>, BigDecimal> accountNameSales = ((BTreeMap) this.map).subMap(
			Fun.t2(account.getAddress(), null),
			Fun.t2(account.getAddress(), Fun.HI()));
	
	//DELETE NAMES
	for(Tuple2<String, String> key: accountNameSales.keySet())
	{
		this.delete(key);
	}
}
 
Example #30
Source File: NameMap.java    From Qora with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Name> get(Account account)
{
	List<Name> names = new ArrayList<Name>();
	
	try
	{
		Map<Tuple2<String, String>, Name> accountNames = ((BTreeMap) this.map).subMap(
				Fun.t2(account.getAddress(), null),
				Fun.t2(account.getAddress(), Fun.HI()));
		
		//GET ITERATOR
		Iterator<Name> iterator = accountNames.values().iterator();
		
		while(iterator.hasNext())
		{
			names.add(iterator.next());
		}
	}
	catch(Exception e)
	{
		//ERROR
		e.printStackTrace();
	}
	
	return names;
}