Search in sources :

Example 1 with CollectionItem

use of com.hazelcast.collection.impl.collection.CollectionItem in project hazelcast by hazelcast.

the class SetContainer method getMap.

@Override
protected Map<Long, CollectionItem> getMap() {
    if (itemMap == null) {
        if (itemSet != null && !itemSet.isEmpty()) {
            itemMap = new HashMap<Long, CollectionItem>(itemSet.size());
            for (CollectionItem item : itemSet) {
                itemMap.put(item.getItemId(), item);
            }
            itemSet.clear();
        } else {
            itemMap = new HashMap<Long, CollectionItem>(INITIAL_CAPACITY);
        }
        itemSet = null;
    }
    return itemMap;
}
Also used : CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem)

Example 2 with CollectionItem

use of com.hazelcast.collection.impl.collection.CollectionItem in project hazelcast by hazelcast.

the class AbstractTransactionalCollectionProxy method remove.

public boolean remove(E e) {
    checkTransactionActive();
    checkObjectNotNull(e);
    final NodeEngine nodeEngine = getNodeEngine();
    final Data value = nodeEngine.toData(e);
    final Iterator<CollectionItem> iterator = getCollection().iterator();
    long reservedItemId = -1;
    while (iterator.hasNext()) {
        final CollectionItem item = iterator.next();
        if (value.equals(item.getValue())) {
            reservedItemId = item.getItemId();
            break;
        }
    }
    final CollectionReserveRemoveOperation operation = new CollectionReserveRemoveOperation(name, reservedItemId, value, tx.getTxnId());
    try {
        final OperationService operationService = nodeEngine.getOperationService();
        Future<CollectionItem> f = operationService.invokeOnPartition(getServiceName(), operation, partitionId);
        CollectionItem item = f.get();
        if (item != null) {
            if (reservedItemId == item.getItemId()) {
                iterator.remove();
                removeFromRecord(reservedItemId);
                itemIdSet.remove(reservedItemId);
                return true;
            }
            if (!itemIdSet.add(item.getItemId())) {
                throw new TransactionException("Duplicate itemId: " + item.getItemId());
            }
            CollectionTxnRemoveOperation op = new CollectionTxnRemoveOperation(name, item.getItemId());
            putToRecord(op);
            return true;
        }
    } catch (Throwable t) {
        throw ExceptionUtil.rethrow(t);
    }
    return false;
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) CollectionReserveRemoveOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionReserveRemoveOperation) TransactionException(com.hazelcast.transaction.TransactionException) CollectionTxnRemoveOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionTxnRemoveOperation) Data(com.hazelcast.nio.serialization.Data) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem) OperationService(com.hazelcast.spi.OperationService)

Example 3 with CollectionItem

use of com.hazelcast.collection.impl.collection.CollectionItem in project hazelcast by hazelcast.

the class TransactionalSetProxy method add.

@Override
public boolean add(E e) {
    checkTransactionActive();
    checkObjectNotNull(e);
    final NodeEngine nodeEngine = getNodeEngine();
    final Data value = nodeEngine.toData(e);
    if (!getCollection().add(new CollectionItem(-1, value))) {
        return false;
    }
    CollectionReserveAddOperation operation = new CollectionReserveAddOperation(name, tx.getTxnId(), value);
    try {
        Future<Long> f = nodeEngine.getOperationService().invokeOnPartition(getServiceName(), operation, partitionId);
        Long itemId = f.get();
        if (itemId != null) {
            if (!itemIdSet.add(itemId)) {
                throw new TransactionException("Duplicate itemId: " + itemId);
            }
            CollectionTxnAddOperation op = new CollectionTxnAddOperation(name, itemId, value);
            putToRecord(op);
            return true;
        }
    } catch (Throwable t) {
        throw ExceptionUtil.rethrow(t);
    }
    return false;
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) TransactionException(com.hazelcast.transaction.TransactionException) CollectionTxnAddOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionTxnAddOperation) CollectionReserveAddOperation(com.hazelcast.collection.impl.txncollection.operations.CollectionReserveAddOperation) Data(com.hazelcast.nio.serialization.Data) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem)

Example 4 with CollectionItem

use of com.hazelcast.collection.impl.collection.CollectionItem in project hazelcast by hazelcast.

the class ListAddOperation method run.

@Override
public void run() throws Exception {
    final ListContainer listContainer = getOrCreateListContainer();
    response = false;
    if (!hasEnoughCapacity(1)) {
        return;
    }
    final CollectionItem item = listContainer.add(index, value);
    if (item != null) {
        itemId = item.getItemId();
        response = true;
    }
}
Also used : ListContainer(com.hazelcast.collection.impl.list.ListContainer) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem)

Example 5 with CollectionItem

use of com.hazelcast.collection.impl.collection.CollectionItem in project hazelcast by hazelcast.

the class ListContainer method sub.

public List<Data> sub(int from, int to) {
    final List<CollectionItem> list;
    if (from == -1 && to == -1) {
        list = getCollection();
    } else if (to == -1) {
        List<CollectionItem> collection = getCollection();
        list = collection.subList(from, collection.size());
    } else {
        list = getCollection().subList(from, to);
    }
    final ArrayList<Data> sub = new ArrayList<Data>(list.size());
    for (CollectionItem item : list) {
        sub.add((Data) item.getValue());
    }
    return sub;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Data(com.hazelcast.nio.serialization.Data) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem) TxCollectionItem(com.hazelcast.collection.impl.collection.TxCollectionItem)

Aggregations

CollectionItem (com.hazelcast.collection.impl.collection.CollectionItem)20 Data (com.hazelcast.nio.serialization.Data)9 TxCollectionItem (com.hazelcast.collection.impl.collection.TxCollectionItem)7 ListContainer (com.hazelcast.collection.impl.list.ListContainer)5 ArrayList (java.util.ArrayList)5 NodeEngine (com.hazelcast.spi.NodeEngine)3 TransactionException (com.hazelcast.transaction.TransactionException)3 CollectionReserveAddOperation (com.hazelcast.collection.impl.txncollection.operations.CollectionReserveAddOperation)2 CollectionTxnAddOperation (com.hazelcast.collection.impl.txncollection.operations.CollectionTxnAddOperation)2 Address (com.hazelcast.nio.Address)2 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)2 HashMap (java.util.HashMap)2 CollectionContainer (com.hazelcast.collection.impl.collection.CollectionContainer)1 ListService (com.hazelcast.collection.impl.list.ListService)1 SetContainer (com.hazelcast.collection.impl.set.SetContainer)1 SetService (com.hazelcast.collection.impl.set.SetService)1 CollectionReserveRemoveOperation (com.hazelcast.collection.impl.txncollection.operations.CollectionReserveRemoveOperation)1 CollectionTxnRemoveOperation (com.hazelcast.collection.impl.txncollection.operations.CollectionTxnRemoveOperation)1 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)1 SerializationServiceBuilder (com.hazelcast.internal.serialization.SerializationServiceBuilder)1