TEST_F(ClientTxnTest, testTxnRollback) { util::CountDownLatch memberRemovedLatch(1); std::string queueName = "testTxnRollback"; MyLifecycleListener myLifecycleListener(memberRemovedLatch); client->getCluster().addMembershipListener(&myLifecycleListener); TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalQueue<std::string> queue = context.getQueue<std::string>(queueName); queue.offer("item"); server->shutdown(); ASSERT_THROW(context.commitTransaction(), exception::IException); context.rollbackTransaction(); ASSERT_TRUE(memberRemovedLatch.await(10)); IQueue<std::string> q = client->getQueue<std::string>(queueName); try { ASSERT_EQ(0, q.size()); ASSERT_EQ(q.poll().get(), (std::string *)NULL); } catch (exception::IException& e) { std::cout << e.what() << std::endl; } }
void ClientTxnMapTest::testKeySetAndValuesWithPredicates() { std::string name = "testKeysetAndValuesWithPredicates"; IMap<Employee, Employee> map = client->getMap<Employee, Employee>(name); Employee emp1("abc-123-xvz", 34); Employee emp2("abc-123-xvz", 20); map.put(emp1, emp1); TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalMap<Employee, Employee> txMap = context.getMap<Employee, Employee>(name); assertNull(txMap.put(emp2, emp2).get()); assertEqual(2, (int)txMap.size()); assertEqual(2, (int)txMap.keySet().size()); query::SqlPredicate predicate("a = 10"); assertEqual(0, (int)txMap.keySet(&predicate).size()); assertEqual(0, (int)txMap.values(&predicate).size()); predicate.setSql("a >= 10"); assertEqual(2, (int)txMap.keySet(&predicate).size()); assertEqual(2, (int)txMap.values(&predicate).size()); context.commitTransaction(); assertEqual(2, (int)map.size()); assertEqual(2, (int)map.values().size()); }
TEST_F(ClientTxnQueueTest, testTransactionalOfferPoll1) { std::string name = "defQueue"; TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalQueue<std::string> q = context.getQueue<std::string>(name); ASSERT_TRUE(q.offer("ali")); ASSERT_EQ(1, q.size()); ASSERT_EQ("ali", *(q.poll())); ASSERT_EQ(0, q.size()); context.commitTransaction(); ASSERT_EQ(0, client->getQueue<std::string>(name).size()); }
StringCursor::StringCursor( TransactionContext &txn, ObjectManager &objectManager, OId oId) : BaseObject(txn.getPartitionId(), objectManager, oId), zeroLengthStr_(ZERO_LENGTH_STR_BINARY_) { length_ = ValueProcessor::decodeVarSize(getBaseAddr()); moveCursor(ValueProcessor::getEncodedVarSize(length_)); }
void ClientTxnMapTest::testPutGet() { std::string name = "defMap"; TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalMap<std::string, std::string> map = context.getMap<std::string, std::string>(name); assertNull(map.put("key1", "value1").get()); assertEqual("value1", *(map.get("key1"))); assertNull(client->getMap<std::string, std::string>(name).get("key1").get()); context.commitTransaction(); assertEqual("value1", *(client->getMap<std::string, std::string>(name).get("key1"))); }
void ClientTxnSetTest::testAddRemove() { ISet<std::string> s = client->getSet<std::string>("testAddRemove"); s.add("item1"); TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalSet<std::string> set = context.getSet<std::string>("testAddRemove"); assertTrue(set.add("item2")); assertEqual(2, set.size()); assertEqual(1, s.size()); assertFalse(set.remove("item3")); assertTrue(set.remove("item1")); context.commitTransaction(); assertEqual(1, s.size()); }
TEST_F(ClientTxnQueueTest, testTransactionalOfferPoll2) { util::CountDownLatch latch(1); util::Thread t(testTransactionalOfferPoll2Thread, &latch, client.get()); TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalQueue<std::string> q0 = context.getQueue<std::string>("defQueue0"); TransactionalQueue<std::string> q1 = context.getQueue<std::string>("defQueue1"); boost::shared_ptr<std::string> s; latch.countDown(); s = q0.poll(10 * 1000); ASSERT_EQ("item0", *s); ASSERT_TRUE(q1.offer(*s)); ASSERT_NO_THROW(context.commitTransaction()); ASSERT_EQ(0, client->getQueue<std::string>("defQueue0").size()); ASSERT_EQ("item0", *(client->getQueue<std::string>("defQueue1").poll())); }
BaseContainer::RowCache::RowCache(TransactionContext &txn, BaseContainer *container) : fieldCacheList_(txn.getDefaultAllocator()) { ObjectManager &objectManager = *(container->getObjectManager()); new (frontFieldCache_.addr()) FieldCache(txn.getPartitionId(), objectManager); const uint32_t varCount = container->getVariableColumnNum(); lastCachedField_ = 0; if (varCount > 0) { assert(fieldCacheList_.empty()); fieldCacheList_.resize(varCount); for (uint32_t i = varCount; i > 0; i--) { void *addr = &fieldCacheList_[i - 1]; new (addr) FieldCache(txn.getPartitionId(), objectManager); } } }
BaseContainer::RowArray::RowArray(TransactionContext &txn, BaseContainer *container) : rowArrayImplList_(txn.getDefaultAllocator()), rowArrayStorage_(txn.getPartitionId(), *container->getObjectManager()), rowCache_(txn, container), defaultImpl_(NULL) { if (container->getContainerType() == COLLECTION_CONTAINER) { latestParam_.rowDataOffset_ = getColFixedOffset(); latestParam_.rowIdOffset_ = getColRowIdOffset(); latestParam_.rowHeaderOffset_ = COL_ROW_HEADER_OFFSET; } else if (container->getContainerType() == TIME_SERIES_CONTAINER) { latestParam_.rowDataOffset_ = sizeof(RowHeader); latestParam_.rowIdOffset_ = latestParam_.rowDataOffset_ + container->getColumnInfo(ColumnInfo::ROW_KEY_COLUMN_ID) .getColumnOffset(); latestParam_.rowHeaderOffset_ = TIM_ROW_HEADER_OFFSET; } else { assert(false); GS_THROW_USER_ERROR(GS_ERROR_DS_DS_CONTAINER_TYPE_INVALID, ""); } latestParam_.varColumnNum_ = container->getVariableColumnNum(); latestParam_.varHeaderSize_ = ValueProcessor::getEncodedVarSize(latestParam_.varColumnNum_); latestParam_.columnNum_ = container->getColumnNum(); latestParam_.nullOffsetDiff_ = 0; latestParam_.columnOffsetDiff_ = 0; latestParam_.rowSize_ = container->getRowSize(); latestParam_.rowFixedColumnSize_ = container->getRowFixedColumnSize(); latestParam_.nullbitsSize_ = container->getNullbitsSize(); latestParam_.nullsOffset_ = latestParam_.rowSize_ - (latestParam_.nullbitsSize_ + latestParam_.rowFixedColumnSize_); typedef BaseContainer::RowArrayImpl<BaseContainer, BaseContainer::ROW_ARRAY_GENERAL> GeneralRowArrayImpl; typedef BaseContainer::RowArrayImpl<BaseContainer, BaseContainer::ROW_ARRAY_PLAIN> PlainRowArrayImpl; util::StackAllocator &alloc = txn.getDefaultAllocator(); GeneralRowArrayImpl *generalImpl = ALLOC_NEW(alloc) GeneralRowArrayImpl( txn, container, rowArrayStorage_, rowCache_, latestParam_); PlainRowArrayImpl *plainImpl = reinterpret_cast<PlainRowArrayImpl *>(generalImpl); rowArrayImplList_.push_back(generalImpl); rowArrayImplList_.push_back(plainImpl); defaultImpl_ = getImpl<BaseContainer, BaseContainer::ROW_ARRAY_GENERAL>(); }
void putGetRemoveTestThread(util::ThreadArgs& args) { MultiMap<std::string, std::string> *mm = (MultiMap<std::string, std::string > *)args.arg0; HazelcastClient *client = (HazelcastClient *)args.arg1; util::CountDownLatch *latch = (util::CountDownLatch *)args.arg2; std::string key = util::IOUtil::to_string(util::Thread::getThreadID()); client->getMultiMap<std::string, std::string>("testPutGetRemove").put(key, "value"); TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalMultiMap<std::string, std::string> originalMultiMap = context.getMultiMap<std::string, std::string >("testPutGetRemove"); client::adaptor::RawPointerTransactionalMultiMap<std::string, std::string> multiMap(originalMultiMap); ASSERT_FALSE(multiMap.put(key, "value")); ASSERT_TRUE(multiMap.put(key, "value1")); ASSERT_TRUE(multiMap.put(key, "value2")); ASSERT_EQ(3, (int)multiMap.get(key)->size()); context.commitTransaction(); ASSERT_EQ(3, (int)mm->get(key).size()); latch->countDown(); }
VariableArrayCursor::VariableArrayCursor(TransactionContext &txn, ObjectManager &objectManager, OId oId, AccessMode accessMode) : BaseObject(txn.getPartitionId(), objectManager), curObject_(*this), elemCursor_(UNDEF_CURSOR_POS), accessMode_(accessMode) { BaseObject::load(oId, accessMode_); rootOId_ = getBaseOId(); elemNum_ = ValueProcessor::decodeVarSize(curObject_.getBaseAddr()); curObject_.moveCursor( ValueProcessor::getEncodedVarSize(elemNum_)); }
void ClientTxnMapTest::testKeySetValues() { std::string name = "testKeySetValues"; IMap<std::string, std::string> map = client->getMap<std::string, std::string>(name); map.put("key1", "value1"); map.put("key2", "value2"); TransactionContext context = client->newTransactionContext(); context.beginTransaction(); TransactionalMap<std::string, std::string> txMap = context.getMap<std::string, std::string>(name); assertNull(txMap.put("key3", "value3").get()); assertEqual(3, (int)txMap.size()); assertEqual(3, (int)txMap.keySet().size()); assertEqual(3, (int)txMap.values().size()); context.commitTransaction(); assertEqual(3, (int)map.size()); assertEqual(3, (int)map.keySet().size()); assertEqual(3, (int)map.values().size()); }
StringCursor::StringCursor(TransactionContext &txn, const char *str) : BaseObject(NULL), zeroLengthStr_(ZERO_LENGTH_STR_BINARY_) { setBaseOId(UNDEF_OID); if (str == NULL) { setBaseAddr(&zeroLengthStr_); length_ = 0; } else { length_ = static_cast<uint32_t>(strlen(str)); size_t offset = ValueProcessor::getEncodedVarSize(length_); setBaseAddr( ALLOC_NEW(txn.getDefaultAllocator()) uint8_t[offset + length_]); uint64_t encodedLength = ValueProcessor::encodeVarSize(length_); memcpy(getBaseAddr(), &encodedLength, offset); memcpy(getBaseAddr() + offset, str, length_); moveCursor(offset); } }
void HashMap::split(TransactionContext& txn) { if (hashMapImage_->split_ == hashMapImage_->front_) { hashMapImage_->split_ = 0; hashMapImage_->front_ = hashMapImage_->front_ << 1; hashMapImage_->toSplit_ = false; } uint64_t bucketAddr0 = hashMapImage_->split_; uint64_t bucketAddr1 = hashMapImage_->rear_; hashMapImage_->split_++; hashMapImage_->rear_++; if (hashMapImage_->rear_ > hashArray_.size()) { hashArray_.twiceHashArray(txn); if (hashMapImage_->rear_ > hashArray_.size()) { GS_THROW_SYSTEM_ERROR(GS_ERROR_DS_HM_MAX_ARRY_SIZE_OVER, ""); } } Bucket splitBucket( txn, *getObjectManager(), maxArraySize_, maxCollisionArraySize_); hashArray_.get(txn, bucketAddr0, splitBucket); Bucket::Cursor cursor(txn, *getObjectManager()); splitBucket.set(txn, cursor); if (cursor.size_ == 0) { ; } else { util::XArray<OId> remainOIdList(txn.getDefaultAllocator()); Bucket newBucket1( txn, *getObjectManager(), maxArraySize_, maxCollisionArraySize_); hashArray_.get(txn, bucketAddr1, newBucket1); size_t bucket0Size = 0; size_t bucket1Size = 0; bool isCurrent = splitBucket.next(txn, cursor); while (isCurrent) { OId oId = splitBucket.getCurrentOId(cursor); uint32_t addr = hashBucketAddrFromObject<T>(txn, oId); if (addr == bucketAddr0) { bucket0Size++; remainOIdList.push_back(oId); } else { bucket1Size++; newBucket1.append(txn, oId); } isCurrent = splitBucket.next(txn, cursor); } cursor.array_.reset(); if (bucket1Size > 0) { splitBucket.clear(txn); for (size_t i = 0; i < remainOIdList.size(); i++) { splitBucket.append(txn, remainOIdList[i]); } } else if (bucket0Size > 1) { remainOIdList.clear(); } } return; }
/*! @brief Clone */ OId VariableArrayCursor::clone(TransactionContext &txn, const AllocateStrategy &allocateStrategy, OId neighborOId) { if (UNDEF_OID == getBaseOId()) { return UNDEF_OID; } OId currentNeighborOId = neighborOId; OId linkOId = UNDEF_OID; uint32_t srcDataLength = getArrayLength(); if (srcDataLength == 0) { uint8_t *srcObj = getBaseAddr(); Size_t srcObjSize = getObjectManager()->getSize(srcObj); BaseObject destObject(txn.getPartitionId(), *getObjectManager()); OId destOId = UNDEF_OID; if (currentNeighborOId != UNDEF_OID) { destObject.allocateNeighbor<uint8_t>(srcObjSize, allocateStrategy, destOId, currentNeighborOId, OBJECT_TYPE_VARIANT); } else { destObject.allocate<uint8_t>( srcObjSize, allocateStrategy, destOId, OBJECT_TYPE_VARIANT); } memcpy(destObject.getBaseAddr(), srcObj, srcObjSize); linkOId = destOId; } else { OId srcOId = UNDEF_OID; OId destOId = UNDEF_OID; OId prevOId = UNDEF_OID; uint8_t *srcObj = NULL; BaseObject destObject(txn.getPartitionId(), *getObjectManager()); uint8_t *srcElem; uint32_t srcElemSize; uint32_t srcCount; uint8_t *lastElem = NULL; uint32_t lastElemSize = 0; while (nextElement()) { srcObj = data(); srcOId = getElementOId(); srcElem = getElement(srcElemSize, srcCount); if (srcOId != prevOId) { Size_t srcObjSize = getObjectManager()->getSize(srcObj); if (currentNeighborOId != UNDEF_OID) { destObject.allocateNeighbor<uint8_t>(srcObjSize, allocateStrategy, destOId, currentNeighborOId, OBJECT_TYPE_VARIANT); } else { destObject.allocate<uint8_t>(srcObjSize, allocateStrategy, destOId, OBJECT_TYPE_VARIANT); } currentNeighborOId = destOId; memcpy(destObject.getBaseAddr(), srcObj, srcObjSize); if (UNDEF_OID == linkOId) { linkOId = destOId; } else { assert(lastElem > 0); uint8_t *linkOIdAddr = lastElem + lastElemSize + ValueProcessor::getEncodedVarSize(lastElemSize); uint64_t encodedOId = ValueProcessor::encodeVarSizeOId(destOId); memcpy(linkOIdAddr, &encodedOId, sizeof(uint64_t)); } prevOId = srcOId; } lastElem = destObject.getBaseAddr() + (srcElem - srcObj); lastElemSize = srcElemSize; } } return linkOId; }