/** * Adds a table operation to insert the specified entity to the batch operation. * * @param entity * The {@link TableEntity} to insert. */ public void insert(final TableEntity entity) { insert(entity, false); }
/** * Reserved for internal use. Clears internal fields when the batch operation is empty. */ private void checkResetEntityLocks() { if (this.size() == 0) { this.partitionKey = null; this.hasQuery = false; this.containsWrites = false; } }
private Status insertBatch(String key, Map<String, ByteIterator> values) { HashMap<String, EntityProperty> properties = new HashMap<String, EntityProperty>(); for (Entry<String, ByteIterator> entry : values.entrySet()) { String fieldName = entry.getKey(); byte[] fieldVal = entry.getValue().toArray(); properties.put(fieldName, new EntityProperty(fieldVal)); } DynamicTableEntity entity = new DynamicTableEntity(partitionKey, key, properties); BATCH_OPERATION.insertOrReplace(entity); if (++curIdx == batchSize) { try { cloudTable.execute(BATCH_OPERATION); BATCH_OPERATION.clear(); curIdx = 0; } catch (Exception e) { return Status.ERROR; } } return Status.OK; }
@Test public void testBatchLockToPartitionKey() { try { TableBatchOperation batch = new TableBatchOperation(); batch.insert(TableTestHelper.generateRandomEntity("jxscl_odata")); batch.insert(TableTestHelper.generateRandomEntity("jxscl_odata2")); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), SR.OPS_IN_BATCH_MUST_HAVE_SAME_PARTITION_KEY); } }
@Override public void deleteVreAuthorizations(String vreId) throws AuthorizationUnavailableException { String condition = TableQuery.generateFilterCondition( "PartitionKey", TableQuery.QueryComparisons.EQUAL, vreId ); TableBatchOperation deletes = new TableBatchOperation(); for (DynamicTableEntity entity : table.execute(TableQuery.from(DynamicTableEntity.class).where(condition))) { deletes.delete(entity); } try { table.execute(deletes); } catch (StorageException e) { LOG.error("deleteVreAuthorizations failed", e); throw new AuthorizationUnavailableException("Could not delete authorizations"); } }
/** * Adds the table operation to the batch operation <code>ArrayList</code>. * * @param element * The {@link TableOperation} to add to the batch operation. * @return * <code>true</code> if the operation was added successfully. */ @Override public boolean add(final TableOperation element) { Utility.assertNotNull("element", element); this.checkSingleQueryPerBatch(element, this.size()); if (element.getOperationType() == TableOperationType.RETRIEVE) { // Query operation this.lockToPartitionKey(((QueryTableOperation) element).getPartitionKey()); } else { this.lockToPartitionKey(element.getEntity().getPartitionKey()); } return super.add(element); }
/** * Adds a table operation to retrieve an entity of the specified class type with the specified PartitionKey and * RowKey to the batch operation. * * @param partitionKey * A <code>String</code> containing the PartitionKey of the entity to retrieve. * @param rowKey * A <code>String</code> containing the RowKey of the entity to retrieve. * @param clazzType * The class of the {@link TableEntity} type for the entity to retrieve. */ public void retrieve(final String partitionKey, final String rowKey, final Class<? extends TableEntity> clazzType) { this.lockToPartitionKey(partitionKey); this.add(TableOperation.retrieve(partitionKey, rowKey, clazzType)); }
@Test @Category(SecondaryTests.class) public void testBatchRemoveRange() throws StorageException { ArrayList<TableOperation> ops = allOpsList(); TableBatchOperation batch = new TableBatchOperation(); batch.addAll(ops); batch.removeRange(0, ops.size()); assertEquals(0, batch.size()); // should be able to add an entity with a different partition key Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata_2"); batch.insert(baseEntity); batch.removeRange(0, 1); batch.addAll(ops); batch.removeRange(0, ops.size() - 1); // should be not be able to add an entity with a different partition key baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata_2"); try { batch.insert(baseEntity); fail(SR.OPS_IN_BATCH_MUST_HAVE_SAME_PARTITION_KEY); } catch (IllegalArgumentException e) { assertEquals(SR.OPS_IN_BATCH_MUST_HAVE_SAME_PARTITION_KEY, e.getMessage()); } batch.removeRange(0, 1); // should be able to add a retrieve to the batch Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); TableOperation queryOp = TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); batch.add(queryOp); }
@Test @Category(SecondaryTests.class) public void testBatchAddIndex() { TableBatchOperation batch = new TableBatchOperation(); // add a retrieve Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); TableOperation queryOp = TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); batch.add(0, queryOp); // remove the retrieve batch.remove(0); assertEquals(0, batch.size()); // should be able to add an entity with a different partition key Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata_2"); TableOperation op = TableOperation.insert(baseEntity); batch.add(0, op); // remove the insert batch.remove(0); assertEquals(0, batch.size()); // add a retrieve to the batch ref = TableTestHelper.generateRandomEntity("jxscl_odata"); queryOp = TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); batch.add(0, queryOp); }
@Test public void testBatchRemoveAll() throws StorageException { ArrayList<TableOperation> ops = allOpsList(); TableBatchOperation batch = new TableBatchOperation(); batch.addAll(ops); assertTrue(batch.removeAll(ops)); assertEquals(0, batch.size()); // should be able to add an entity with a different partition key Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata_2"); batch.insert(baseEntity); }
private void processBatch() throws IOException { TableBatchOperation batch = new TableBatchOperation(); batch.addAll(batchOperations); // try { tableservice.executeOperation(tableName, batch); handleSuccess(null, batchOperationsCount); } catch (StorageException e) { LOGGER.error(i18nMessages.getMessage("error.ProcessBatch", actionData, e.getLocalizedMessage())); handleReject(null, e, batchOperationsCount); if (dieOnError) { throw new ComponentException(e); } } catch (URISyntaxException | InvalidKeyException e) { throw new ComponentException(e); // connection problem so next operation will also fail, we stop the process } // reset operations, count and marker batchOperations.clear(); batchRecords.clear(); batchOperationsCount = 0; latestPartitionKey = ""; }
TableBatchOperation batch = new TableBatchOperation(); batch.insert(ref); this.table.execute(batch, options, null); TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), Class1.class); res = this.table.execute(batch, options, null).get(0); TableBatchOperation batch = new TableBatchOperation(); batch.merge(ref); this.table.execute(batch, options, null); TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), Class1.class); res = this.table.execute(batch, options, null).get(0); TableBatchOperation batch = new TableBatchOperation(); batch.replace(ref); this.table.execute(batch, options, null); TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), Class1.class); res = this.table.execute(batch, options, null).get(0); TableBatchOperation batch = new TableBatchOperation(); batch.delete(retObj); res = this.table.execute(batch, options, null).get(0);
private void deleteEntities(Iterator<TaskCheckpointEntity> entitiesToDelete) { TableBatchOperation batchOperation = new TableBatchOperation(); while (entitiesToDelete.hasNext()) { TaskCheckpointEntity entity = entitiesToDelete.next(); // Add to batch operation batchOperation.delete(entity); // Execute when batch reaches capacity or when this is the last item if (batchOperation.size() >= MAX_WRITE_BATCH_SIZE || !entitiesToDelete.hasNext()) { try { cloudTable.execute(batchOperation); } catch (StorageException e) { LOG.error("Executing batch failed for deleting checkpoints"); throw new AzureException(e); } batchOperation.clear(); } } } }
TableBatchOperation batchOperation = new TableBatchOperation(); batchOperation.insertOrReplace(taskCheckpoint); if (batchOperation.size() >= MAX_WRITE_BATCH_SIZE || !iterator.hasNext()) { try { cloudTable.execute(batchOperation); throw new AzureException(e); batchOperation.clear();
@Test public void testBatchAddNullShouldThrow() { try { TableBatchOperation batch = new TableBatchOperation(); batch.add(null); fail(); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), String.format(SR.ARGUMENT_NULL_OR_EMPTY, "element")); } }
@Test public void testBatchRetrieveWithNullResolverShouldThrow() { try { TableBatchOperation batch = new TableBatchOperation(); batch.retrieve("foo", "blah", (EntityResolver<?>) null); fail(); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), String.format(SR.ARGUMENT_NULL_OR_EMPTY, SR.QUERY_REQUIRES_VALID_CLASSTYPE_OR_RESOLVER)); } }
private void testQueryOnSupportedTypes(TableRequestOptions options, boolean usePropertyResolver) throws InterruptedException, StorageException { TableBatchOperation batch = new TableBatchOperation(); String pk = UUID.randomUUID().toString(); batch.insert(ent); if (j == 50) { middleRef = ent; TableBatchOperation delBatch = new TableBatchOperation(); TableQuery<ComplexEntity> query = TableQuery.from(ComplexEntity.class).where( String.format("PartitionKey eq '%s'", pk)); delBatch.delete(e);
@Test public void testBatchRetrieveAndOneMoreOperationShouldThrow() { Class1 ref2 = TableTestHelper.generateRandomEntity("jxscl_odata"); try { TableBatchOperation batch = new TableBatchOperation(); batch.insert(TableTestHelper.generateRandomEntity("jxscl_odata")); batch.retrieve(ref2.getPartitionKey(), ref2.getRowKey(), ref2.getClass()); fail(); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH); } try { TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref2.getPartitionKey(), ref2.getRowKey(), ref2.getClass()); batch.insert(TableTestHelper.generateRandomEntity("jxscl_odata")); fail(); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH); } }
@Test public void testEmptyBatchShouldThrow() throws StorageException { TableBatchOperation batch = new TableBatchOperation(); try { this.table.execute(batch, null, null); fail(SR.EMPTY_BATCH_NOT_ALLOWED); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), SR.EMPTY_BATCH_NOT_ALLOWED); } }