org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap Java Examples

The following examples show how to use org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap. 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: GsIntObjectMapTest.java    From hashmapTest with The Unlicense 5 votes vote down vote up
@Override
public int test() {
    final IntObjectHashMap<Integer> m_map = new IntObjectHashMap<>( m_keys.length );
    for ( int i = 0; i < m_keys.length; ++i )
        m_map.put( m_keys[ i ], null );
    for ( int i = 0; i < m_keys.length; ++i )
        m_map.put( m_keys[ i ], null );
    return m_map.size();
}
 
Example #2
Source File: UserProfile.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public UserProfile(long uid, UserStatus userStatus) {
    //log.debug("New {}", uid);
    this.uid = uid;
    this.positions = new IntObjectHashMap<>();
    this.adjustmentsCounter = 0L;
    this.accounts = new IntLongHashMap();
    this.userStatus = userStatus;
}
 
Example #3
Source File: SingleUserReportQuery.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<SingleUserReportResult> process(final RiskEngine riskEngine) {

    if (!riskEngine.uidForThisHandler(this.uid)) {
        return Optional.empty();
    }
    final UserProfile userProfile = riskEngine.getUserProfileService().getUserProfile(this.uid);

    if (userProfile != null) {
        final IntObjectHashMap<SingleUserReportResult.Position> positions = new IntObjectHashMap<>(userProfile.positions.size());
        userProfile.positions.forEachKeyValue((symbol, pos) ->
                positions.put(symbol, new SingleUserReportResult.Position(
                        pos.currency,
                        pos.direction,
                        pos.openVolume,
                        pos.openPriceSum,
                        pos.profit,
                        pos.pendingSellSize,
                        pos.pendingBuySize)));

        return Optional.of(SingleUserReportResult.createFromRiskEngineFound(
                uid,
                userProfile.userStatus,
                userProfile.accounts,
                positions));
    } else {
        // not found
        return Optional.of(SingleUserReportResult.createFromRiskEngineNotFound(uid));
    }
}
 
Example #4
Source File: SingleUserReportQuery.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<SingleUserReportResult> process(final MatchingEngineRouter matchingEngine) {
    final IntObjectHashMap<List<Order>> orders = new IntObjectHashMap<>();

    matchingEngine.getOrderBooks().forEach(ob -> {
        final List<Order> userOrders = ob.findUserOrders(this.uid);
        // dont put empty results, so that the report result merge procedure would be simple
        if (!userOrders.isEmpty()) {
            orders.put(ob.getSymbolSpec().symbolId, userOrders);
        }
    });

    //log.debug("ME{}: orders: {}", matchingEngine.getShardId(), orders);
    return Optional.of(SingleUserReportResult.createFromMatchingEngine(uid, orders));
}
 
Example #5
Source File: AnalyzedWildcardPattern.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public void addSubstring(int length, String s)
{
    if (this.substring == null)
    {
        this.substring = new IntObjectHashMap<Set<String>>();
    }
    Set<String> stringSet = this.substring.get(length);
    if (stringSet == null)
    {
        stringSet = UnifiedSet.newSet();
        this.substring.put(length, stringSet);
    }
    stringSet.add(s);
}
 
Example #6
Source File: ConjunctionIndex.java    From vespa with Apache License 2.0 5 votes vote down vote up
public static ConjunctionIndex fromInputStream(DataInputStream in) throws IOException {
    int[] zList = SerializationHelper.readIntArray(in);
    long[] idMapping = SerializationHelper.readLongArray(in);
    int kIndexSize = in.readInt();
    IntObjectHashMap<FeatureIndex> kIndex = new IntObjectHashMap<>(kIndexSize);
    for (int i = 0; i < kIndexSize; i++) {
        int key = in.readInt();
        kIndex.put(key, FeatureIndex.fromInputStream(in));
    }
    kIndex.compact();
    return new ConjunctionIndex(kIndex, zList, idMapping);
}
 
Example #7
Source File: TotalCurrencyBalanceReportQuery.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<TotalCurrencyBalanceReportResult> process(final RiskEngine riskEngine) {

    // prepare fast price cache for profit estimation with some price (exact value is not important, except ask==bid condition)
    final IntObjectHashMap<RiskEngine.LastPriceCacheRecord> dummyLastPriceCache = new IntObjectHashMap<>();
    riskEngine.getLastPriceCache().forEachKeyValue((s, r) -> dummyLastPriceCache.put(s, r.averagingRecord()));

    final IntLongHashMap currencyBalance = new IntLongHashMap();

    final IntLongHashMap symbolOpenInterestLong = new IntLongHashMap();
    final IntLongHashMap symbolOpenInterestShort = new IntLongHashMap();

    final SymbolSpecificationProvider symbolSpecificationProvider = riskEngine.getSymbolSpecificationProvider();

    riskEngine.getUserProfileService().getUserProfiles().forEach(userProfile -> {
        userProfile.accounts.forEachKeyValue(currencyBalance::addToValue);
        userProfile.positions.forEachKeyValue((symbolId, positionRecord) -> {
            final CoreSymbolSpecification spec = symbolSpecificationProvider.getSymbolSpecification(symbolId);
            final RiskEngine.LastPriceCacheRecord avgPrice = dummyLastPriceCache.getIfAbsentPut(symbolId, RiskEngine.LastPriceCacheRecord.dummy);
            currencyBalance.addToValue(positionRecord.currency, positionRecord.estimateProfit(spec, avgPrice));

            if (positionRecord.direction == PositionDirection.LONG) {
                symbolOpenInterestLong.addToValue(symbolId, positionRecord.openVolume);
            } else if (positionRecord.direction == PositionDirection.SHORT) {
                symbolOpenInterestShort.addToValue(symbolId, positionRecord.openVolume);
            }
        });
    });

    return Optional.of(
            new TotalCurrencyBalanceReportResult(
                    currencyBalance,
                    new IntLongHashMap(riskEngine.getFees()),
                    new IntLongHashMap(riskEngine.getAdjustments()),
                    new IntLongHashMap(riskEngine.getSuspends()),
                    null,
                    symbolOpenInterestLong,
                    symbolOpenInterestShort));
}
 
Example #8
Source File: ConjunctionIndexBuilder.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static IntObjectMap<ConjunctionIndex.FeatureIndex> buildKIndex(HashMap<Integer, FeatureIndexBuilder> kIndexBuilder) {
    IntObjectHashMap<ConjunctionIndex.FeatureIndex> map = new IntObjectHashMap<>();
    for (Map.Entry<Integer, FeatureIndexBuilder> entry : kIndexBuilder.entrySet()) {
        map.put(entry.getKey(), buildFeatureIndex(entry.getValue()));
    }
    map.compact();
    return map;
}
 
Example #9
Source File: SerializationUtils.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static <V> IntObjectHashMap<V> mergeOverride(final IntObjectHashMap<V> a, final IntObjectHashMap<V> b) {
    final IntObjectHashMap<V> res = a == null ? new IntObjectHashMap<>() : new IntObjectHashMap<>(a);
    if (b != null) {
        res.putAll(b);
    }
    return res;
}
 
Example #10
Source File: SerializationUtils.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static <T> IntObjectHashMap<T> readIntHashMap(final BytesIn bytes, final Function<BytesIn, T> creator) {
    int length = bytes.readInt();
    final IntObjectHashMap<T> hashMap = new IntObjectHashMap<>(length);
    for (int i = 0; i < length; i++) {
        hashMap.put(bytes.readInt(), creator.apply(bytes));
    }
    return hashMap;
}
 
Example #11
Source File: SerializationUtils.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static <T> void marshallIntHashMap(final IntObjectHashMap<T> hashMap, final BytesOut bytes, final Consumer<T> elementMarshaller) {
    bytes.writeInt(hashMap.size());
    hashMap.forEachKeyValue((k, v) -> {
        bytes.writeInt(k);
        elementMarshaller.accept(v);
    });
}
 
Example #12
Source File: SerializationUtils.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static <T extends WriteBytesMarshallable> void marshallIntHashMap(final IntObjectHashMap<T> hashMap, final BytesOut bytes) {
    bytes.writeInt(hashMap.size());
    hashMap.forEachKeyValue((k, v) -> {
        bytes.writeInt(k);
        v.writeMarshallable(bytes);
    });
}
 
Example #13
Source File: GsIntObjectMapTest.java    From hashmapTest with The Unlicense 5 votes vote down vote up
@Override
public int test() {
    final IntObjectHashMap<Integer> m_map = new IntObjectHashMap<>( m_keys.length / 2 + 1 );
    final Integer value = 1;
    int add = 0, remove = 0;
    while ( add < m_keys.length )
    {
        m_map.put( m_keys[ add ], value );
        ++add;
        m_map.put( m_keys[ add ], value );
        ++add;
        m_map.remove( m_keys[ remove++ ] );
    }
    return m_map.size();
}
 
Example #14
Source File: MatchingEngineRouter.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
private void handleBinaryMessage(Object message) {

        if (message instanceof BatchAddSymbolsCommand) {
            final IntObjectHashMap<CoreSymbolSpecification> symbols = ((BatchAddSymbolsCommand) message).getSymbols();
            symbols.forEach(this::addSymbol);
        } else if (message instanceof BatchAddAccountsCommand) {
            // do nothing
        }
    }
 
Example #15
Source File: EclipseStorage.java    From TarsosLSH with GNU Lesser General Public License v3.0 5 votes vote down vote up
public EclipseStorage(int numberOfTables){		
	hashtables = new ArrayList<>();
	for(int i = 0 ; i < numberOfTables ; i++){
		IntObjectHashMap<long[]> map = new IntObjectHashMap<>();
		hashtables.add(map);
	}
}
 
Example #16
Source File: MatchingEngineRouter.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public MatchingEngineRouter(final int shardId,
                            final long numShards,
                            final ISerializationProcessor serializationProcessor,
                            final IOrderBook.OrderBookFactory orderBookFactory,
                            final SharedPool sharedPool,
                            final ExchangeConfiguration exchangeCfg) {

    if (Long.bitCount(numShards) != 1) {
        throw new IllegalArgumentException("Invalid number of shards " + numShards + " - must be power of 2");
    }
    this.shardId = shardId;
    this.shardMask = numShards - 1;
    this.serializationProcessor = serializationProcessor;
    this.orderBookFactory = orderBookFactory;
    this.eventsHelper = new OrderBookEventsHelper(sharedPool::getChain);

    this.loggingCfg = exchangeCfg.getLoggingCfg();
    this.logDebug = loggingCfg.getLoggingLevels().contains(LoggingConfiguration.LoggingLevel.LOGGING_MATCHING_DEBUG);

    // initialize object pools // TODO move to perf config
    final HashMap<Integer, Integer> objectsPoolConfig = new HashMap<>();
    objectsPoolConfig.put(ObjectsPool.DIRECT_ORDER, 1024 * 1024);
    objectsPoolConfig.put(ObjectsPool.DIRECT_BUCKET, 1024 * 64);
    objectsPoolConfig.put(ObjectsPool.ART_NODE_4, 1024 * 32);
    objectsPoolConfig.put(ObjectsPool.ART_NODE_16, 1024 * 16);
    objectsPoolConfig.put(ObjectsPool.ART_NODE_48, 1024 * 8);
    objectsPoolConfig.put(ObjectsPool.ART_NODE_256, 1024 * 4);
    this.objectsPool = new ObjectsPool(objectsPoolConfig);
    if (exchangeCfg.getInitStateCfg().fromSnapshot()) {
        final DeserializedData deserialized = serializationProcessor.loadData(
                exchangeCfg.getInitStateCfg().getSnapshotId(),
                ISerializationProcessor.SerializedModuleType.MATCHING_ENGINE_ROUTER,
                shardId,
                bytesIn -> {
                    if (shardId != bytesIn.readInt()) {
                        throw new IllegalStateException("wrong shardId");
                    }
                    if (shardMask != bytesIn.readLong()) {
                        throw new IllegalStateException("wrong shardMask");
                    }

                    final BinaryCommandsProcessor bcp = new BinaryCommandsProcessor(
                            this::handleBinaryMessage,
                            this::handleReportQuery,
                            sharedPool,
                            exchangeCfg.getReportsQueriesCfg(),
                            bytesIn,
                            shardId + 1024);

                    final IntObjectHashMap<IOrderBook> ob = SerializationUtils.readIntHashMap(
                            bytesIn,
                            bytes -> IOrderBook.create(bytes, objectsPool, eventsHelper, loggingCfg));

                    return DeserializedData.builder().binaryCommandsProcessor(bcp).orderBooks(ob).build();
                });

        this.binaryCommandsProcessor = deserialized.binaryCommandsProcessor;
        this.orderBooks = deserialized.orderBooks;

    } else {
        this.binaryCommandsProcessor = new BinaryCommandsProcessor(
                this::handleBinaryMessage,
                this::handleReportQuery,
                sharedPool,
                exchangeCfg.getReportsQueriesCfg(),
                shardId + 1024);

        this.orderBooks = new IntObjectHashMap<>();
    }

    final OrdersProcessingConfiguration ordersProcCfg = exchangeCfg.getOrdersProcessingCfg();
    this.cfgMarginTradingEnabled = ordersProcCfg.getMarginTradingMode() == OrdersProcessingConfiguration.MarginTradingMode.MARGIN_TRADING_ENABLED;
}
 
Example #17
Source File: GsIntObjectMapTest.java    From hashmapTest with The Unlicense 4 votes vote down vote up
@Override
public void setup(int[] keys, float fillFactor, int oneFailOutOf) {
    super.setup(keys, fillFactor, oneFailOutOf );
    m_map = new IntObjectHashMap<>( keys.length );
    for ( int key : keys ) m_map.put( key % oneFailOutOf == 0 ? key + 1 : key, key );
}
 
Example #18
Source File: AnalyzedWildcardPattern.java    From reladomo with Apache License 2.0 4 votes vote down vote up
public IntObjectHashMap<Set<String>> getSubstring()
{
    return substring;
}
 
Example #19
Source File: SingleUserReportResult.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public static SingleUserReportResult createFromRiskEngineFound(long uid, UserStatus userStatus, IntLongHashMap accounts, IntObjectHashMap<Position> positions) {
    return new SingleUserReportResult(uid, userStatus, accounts, positions, null, QueryExecutionStatus.OK);
}
 
Example #20
Source File: SingleUserReportResult.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public static SingleUserReportResult createFromMatchingEngine(long uid, IntObjectHashMap<List<Order>> orders) {
    return new SingleUserReportResult(uid, null, null, null, orders, QueryExecutionStatus.OK);
}
 
Example #21
Source File: BatchAddSymbolsCommand.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public BatchAddSymbolsCommand(final Collection<CoreSymbolSpecification> collection) {
    symbols = new IntObjectHashMap<>(collection.size());
    collection.forEach(s -> symbols.put(s.symbolId, s));
}
 
Example #22
Source File: BatchAddSymbolsCommand.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public BatchAddSymbolsCommand(final CoreSymbolSpecification symbol) {
    symbols = IntObjectHashMap.newWithKeysValues(symbol.symbolId, symbol);
}
 
Example #23
Source File: HashingUtils.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public static <T extends StateHash> int stateHash(final IntObjectHashMap<T> hashMap) {

        final MutableLong mutableLong = new MutableLong();
        hashMap.forEachKeyValue((k, v) -> mutableLong.addAndGet(Objects.hash(k, v.stateHash())));
        return Long.hashCode(mutableLong.value);
    }
 
Example #24
Source File: SymbolSpecificationProvider.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public SymbolSpecificationProvider() {
    this.symbolSpecs = new IntObjectHashMap<>();
}
 
Example #25
Source File: RiskEngine.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
public RiskEngine(final int shardId,
                  final long numShards,
                  final ISerializationProcessor serializationProcessor,
                  final SharedPool sharedPool,
                  final ExchangeConfiguration exchangeConfiguration) {
    if (Long.bitCount(numShards) != 1) {
        throw new IllegalArgumentException("Invalid number of shards " + numShards + " - must be power of 2");
    }
    this.shardId = shardId;
    this.shardMask = numShards - 1;
    this.serializationProcessor = serializationProcessor;

    // initialize object pools // TODO move to perf config
    final HashMap<Integer, Integer> objectsPoolConfig = new HashMap<>();
    objectsPoolConfig.put(ObjectsPool.SYMBOL_POSITION_RECORD, 1024 * 256);
    this.objectsPool = new ObjectsPool(objectsPoolConfig);

    this.logDebug = exchangeConfiguration.getLoggingCfg().getLoggingLevels().contains(LoggingConfiguration.LoggingLevel.LOGGING_RISK_DEBUG);

    if (exchangeConfiguration.getInitStateCfg().fromSnapshot()) {

        // TODO refactor, change to creator (simpler init)`
        final State state = serializationProcessor.loadData(
                exchangeConfiguration.getInitStateCfg().getSnapshotId(),
                ISerializationProcessor.SerializedModuleType.RISK_ENGINE,
                shardId,
                bytesIn -> {
                    if (shardId != bytesIn.readInt()) {
                        throw new IllegalStateException("wrong shardId");
                    }
                    if (shardMask != bytesIn.readLong()) {
                        throw new IllegalStateException("wrong shardMask");
                    }
                    final SymbolSpecificationProvider symbolSpecificationProvider = new SymbolSpecificationProvider(bytesIn);
                    final UserProfileService userProfileService = new UserProfileService(bytesIn);
                    final BinaryCommandsProcessor binaryCommandsProcessor = new BinaryCommandsProcessor(
                            this::handleBinaryMessage,
                            this::handleReportQuery,
                            sharedPool,
                            exchangeConfiguration.getReportsQueriesCfg(),
                            bytesIn,
                            shardId);
                    final IntObjectHashMap<LastPriceCacheRecord> lastPriceCache = SerializationUtils.readIntHashMap(bytesIn, LastPriceCacheRecord::new);
                    final IntLongHashMap fees = SerializationUtils.readIntLongHashMap(bytesIn);
                    final IntLongHashMap adjustments = SerializationUtils.readIntLongHashMap(bytesIn);
                    final IntLongHashMap suspends = SerializationUtils.readIntLongHashMap(bytesIn);

                    return new State(
                            symbolSpecificationProvider,
                            userProfileService,
                            binaryCommandsProcessor,
                            lastPriceCache,
                            fees,
                            adjustments,
                            suspends);
                });

        this.symbolSpecificationProvider = state.symbolSpecificationProvider;
        this.userProfileService = state.userProfileService;
        this.binaryCommandsProcessor = state.binaryCommandsProcessor;
        this.lastPriceCache = state.lastPriceCache;
        this.fees = state.fees;
        this.adjustments = state.adjustments;
        this.suspends = state.suspends;

    } else {
        this.symbolSpecificationProvider = new SymbolSpecificationProvider();
        this.userProfileService = new UserProfileService();
        this.binaryCommandsProcessor = new BinaryCommandsProcessor(
                this::handleBinaryMessage,
                this::handleReportQuery,
                sharedPool,
                exchangeConfiguration.getReportsQueriesCfg(),
                shardId);
        this.lastPriceCache = new IntObjectHashMap<>();
        this.fees = new IntLongHashMap();
        this.adjustments = new IntLongHashMap();
        this.suspends = new IntLongHashMap();
    }

    final OrdersProcessingConfiguration ordersProcCfg = exchangeConfiguration.getOrdersProcessingCfg();
    this.cfgIgnoreRiskProcessing = ordersProcCfg.getRiskProcessingMode() == OrdersProcessingConfiguration.RiskProcessingMode.NO_RISK_PROCESSING;
    this.cfgMarginTradingEnabled = ordersProcCfg.getMarginTradingMode() == OrdersProcessingConfiguration.MarginTradingMode.MARGIN_TRADING_ENABLED;
}
 
Example #26
Source File: BinHashTable.java    From TarsosLSH with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Initialize a new hash table, it needs a hash family and a number of hash
 * functions that should be used.
 * 
 * @param numberOfHashes
 *            The number of hash functions that should be used.
 * @param family
 *            The hash function family knows how to create new hash
 *            functions, and is used therefore.
 */
public BinHashTable(int numberOfHashes,HammingHashFamily family){
	hashTable = new IntObjectHashMap<long[] []>();
	this.hashFunctions = new HammingHash[numberOfHashes];
	for(int i=0;i<numberOfHashes;i++){
		hashFunctions[i] = family.createHashFunction();
	}
	this.family = family;
}