Java Code Examples for com.hazelcast.core.IMap#size()

The following examples show how to use com.hazelcast.core.IMap#size() . 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: HazelcastRequestRateLimiterInternalTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(2), 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);

    String key = "ip:127.0.0.5";

    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(100L);
        assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
    });

    IMap<Object, Object> map = hz.getMap(key);
    while (map.size() != 0) {
        Thread.sleep(10);
    }
    assertThat(map.size()).isZero();
}
 
Example 2
Source File: PushDepthJob.java    From match-trade with Apache License 2.0 5 votes vote down vote up
/**
 * -★ -获取行情深度
 * 
 * @param coinTeam 交易队
 * @param isBuy    是否是买
 * @return List<Depth>
 */
public List<Depth> getMarketDepth(String coinTeam, Boolean isBuy) {
	// XBIT-USDT 买盘
	IMap<BigDecimal, BigDecimal> buyMap = hzInstance.getMap(HazelcastUtil.getMatchKey(coinTeam, isBuy));
	List<Depth> depths = new ArrayList<Depth>();
	if (buyMap.size() > 0) {
		List<Depth> list = new ArrayList<Depth>();
		if (isBuy) {
			list = buyMap.entrySet().stream().sorted(Entry.<BigDecimal, BigDecimal>comparingByKey().reversed())
					.map(obj -> new Depth(obj.getKey().toString(), obj.getValue().toString(),obj.getValue().toString(), 1, coinTeam, isBuy))
					.collect(Collectors.toList());
		}else {
			list = buyMap.entrySet().stream().sorted(Entry.<BigDecimal, BigDecimal>comparingByKey())
					.map(obj -> new Depth(obj.getKey().toString(), obj.getValue().toString(),obj.getValue().toString(), 1, coinTeam, isBuy))
					.collect(Collectors.toList());
		}
		list.stream().reduce(new Depth("0", "0", "0", 1, coinTeam, isBuy), (one, two) -> {
			one.setTotal((new BigDecimal(one.getTotal()).add(new BigDecimal(two.getNumber()))).toString());
			depths.add(new Depth(two.getPrice(), two.getNumber(), one.getTotal(), two.getPlatform(),two.getCoinTeam(), two.getIsBuy()));
			return one;
		});
	} else {
		Depth depth = new Depth("0.00", "0.0000", "0.0000", 1, coinTeam, isBuy);
		depths.add(depth);
	}
	return depths;
}
 
Example 3
Source File: MatchExecutor.java    From match-trade with Apache License 2.0 4 votes vote down vote up
public MatchOrder doMatch(MatchOrder input) {
	try {
		// 获取对手盘口
		IMap<BigDecimal, BigDecimal> outMap = hzInstance.getMap(HazelcastUtil.getMatchKey(input.getCoinTeam(), !input.getIsBuy()));
		if (null!=outMap&&outMap.size()>0) {
			BigDecimal outPrice = HazelcastUtil.getOptimalMatch(outMap,input.getIsBuy()); 
			if (HazelcastUtil.canMatch(input, outPrice)) {
				BigDecimal outNum = outMap.get(outPrice);
				if (outNum.compareTo(BigDecimal.ZERO)<1) {
					outMap.remove(outPrice);
					doMatch(input); // 递归处理
				}
				int contrast = input.getUnFinishNumber().compareTo(outNum);
				BigDecimal dealNum = contrast > -1?outNum:input.getUnFinishNumber();
				input.setFinishNumber(input.getFinishNumber().add(dealNum));
				input.setUnFinishNumber(input.getUnFinishNumber().subtract(dealNum));
				if (input.getIsBuy()) {
					input.setSurplusFrozen(input.getSurplusFrozen().subtract(outPrice.multiply(dealNum)));
				}else {
					input.setSurplusFrozen(input.getSurplusFrozen().subtract(dealNum));
				}
				//撮合详情记录 >>> 一个价格一个详情
				matchDetailHandler.sendTradeRecord(input, outPrice, dealNum, DealWay.TAKER);
				List<LevelMatch> lms = input.getList();
				LevelMatch lm = new LevelMatch(outPrice, dealNum);
				lm.setEatUp(contrast > -1 ?true:false);
				lms.add(lm);
				input.setList(lms);
				if (contrast == 1) {//水平价格被吃完
					outMap.remove(outPrice);
					input.setState(OrderState.PART.value);
					doMatch(input); // 递归处理
				}else if (contrast == 0) {//都被吃完
					outMap.remove(outPrice);
					input.setState(OrderState.ALL.value);
				}else {//水平价格有剩余
					outMap.compute(outPrice, (k,v) -> v.subtract(dealNum));
					input.setState(OrderState.ALL.value);
				}
			}
		}
	} catch (Exception e) {
		log.error("执行撮合错误:"+e);
		input.setState(3);//撤销掉
	} 
	return input;
}