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;
}
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;
}
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;
}
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;
}
}
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;
}
Aggregations