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(); } } } }
@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)); } }
@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"); } }
@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); } }
@BeforeClass public static void tableQueryTestMethodSetUp() throws Exception { table = TableTestHelper.getRandomTableReference(); table.createIfNotExists(); // Insert 500 entities in Batches to query for (int i = 0; i < 5; i++) { TableBatchOperation batch = new TableBatchOperation(); for (int j = 0; j < 100; j++) { Class1 ent = TableTestHelper.generateRandomEntity("javatables_batch_" + Integer.toString(i)); ent.setRowKey(String.format("%06d", j)); batch.insert(ent); } table.execute(batch); } }
@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); } }
@Test public void testBatchEmptyQuery() throws StorageException { TableRequestOptions options = new TableRequestOptions(); options.setTablePayloadFormat(TablePayloadFormat.Json); // insert entity Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); ArrayList<TableResult> results = this.table.execute(batch, options, null); assertEquals(results.size(), 1); assertNull(results.get(0).getResult()); assertEquals(results.get(0).getHttpStatusCode(), HttpURLConnection.HTTP_NOT_FOUND); }
@Test public void testBatchMultiQueryShouldThrow() { Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); Class1 ref2 = TableTestHelper.generateRandomEntity("jxscl_odata"); try { TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); batch.retrieve(ref2.getPartitionKey(), ref2.getRowKey(), ref2.getClass()); fail(SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH); } }
private void insertAndDeleteBatchWithX(int x, TableRequestOptions options) throws StorageException { TableBatchOperation batch = new TableBatchOperation(); for (int m = 0; m < x; m++) { addInsertBatch(batch); } TableBatchOperation delBatch = new TableBatchOperation(); ArrayList<TableResult> results = this.table.execute(batch, options, null); for (TableResult r : results) { assertEquals(r.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); delBatch.delete((Class1) r.getResult()); } ArrayList<TableResult> delResults = this.table.execute(delBatch, options, null); for (TableResult r : delResults) { assertEquals(r.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); } }
private void upsertAndDeleteBatchWithX(int x, TableRequestOptions options) throws StorageException { TableBatchOperation batch = new TableBatchOperation(); for (int m = 0; m < x; m++) { addInsertOrMergeToBatch(TableTestHelper.generateRandomEntity("jxscl_odata"), batch); } TableBatchOperation delBatch = new TableBatchOperation(); ArrayList<TableResult> results = this.table.execute(batch, options, null); for (TableResult r : results) { assertEquals(r.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); delBatch.delete((Class2) r.getResult()); } ArrayList<TableResult> delResults = this.table.execute(delBatch, options, null); for (TableResult r : delResults) { assertEquals(r.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); } }
private void testBatchInsert(TableRequestOptions options) throws StorageException { TableBatchOperation batch = new TableBatchOperation(); // Add 3 inserts addInsertBatch(batch); // default echo content (true) addInsertBatch(batch, true); // set echo content to true addInsertBatch(batch, false); // set echo content to false // insert entity Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(ref), options, null); batch.delete(ref); ArrayList<TableResult> results = this.table.execute(batch, options, null); assertEquals(results.size(), 4); Iterator<TableResult> iter = results.iterator(); TableResult res = iter.next(); assertEquals(res.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); res = iter.next(); assertEquals(res.getHttpStatusCode(), HttpURLConnection.HTTP_CREATED); res = iter.next(); assertEquals(res.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); // delete assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); }
@Test public void testBatchAddAllWithRetrieveShouldThrow() throws StorageException { ArrayList<TableOperation> ops = allOpsList(); TableBatchOperation batch = new TableBatchOperation(); // Insert entity to retrieve Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(baseEntity), null, null); ops.add(TableOperation.retrieve(baseEntity.getPartitionKey(), baseEntity.getRowKey(), Class1.class)); try { batch.addAll(ops); fail(SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH); } catch (IllegalArgumentException e) { assertEquals(SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH, e.getMessage()); } }
private void testBatchMerge(TableRequestOptions options) throws StorageException { TableBatchOperation batch = new TableBatchOperation(); addInsertBatch(batch); // insert entity to delete Class1 delRef = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(delRef)); batch.delete(delRef); // Insert entity to merge Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(baseEntity), options, null); addMergeToBatch(baseEntity, batch); ArrayList<TableResult> results = this.table.execute(batch); assertEquals(results.size(), 3); Iterator<TableResult> iter = results.iterator(); // insert assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); // delete assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); // merge assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); }
private void testBatchInsertOrMerge(TableRequestOptions options) throws StorageException { TableBatchOperation batch = new TableBatchOperation(); addInsertBatch(batch); // insert entity to delete Class1 delRef = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(delRef), options, null); batch.delete(delRef); // Insert entity to merge Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(baseEntity), options, null); addInsertOrMergeToBatch(baseEntity, batch); ArrayList<TableResult> results = this.table.execute(batch); assertEquals(results.size(), 3); Iterator<TableResult> iter = results.iterator(); // insert assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); // delete assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); // merge assertEquals(iter.next().getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); }
@Test public void testBatchOver100Entities() throws StorageException { TableRequestOptions options = new TableRequestOptions(); options.setTablePayloadFormat(TablePayloadFormat.Json); TableBatchOperation batch = new TableBatchOperation(); try { for (int m = 0; m < 101; m++) { batch.insert(TableTestHelper.generateRandomEntity("jxscl_odata")); } this.table.execute(batch, options, null); fail("Batch with over 100 entities should fail."); } catch (TableServiceException ex) { assertEquals(ex.getMessage(), "Bad Request"); String errorAfterSemiColon = ex.getExtendedErrorInformation().getErrorMessage(); errorAfterSemiColon = errorAfterSemiColon.substring(errorAfterSemiColon.indexOf(":") + 1); assertTrue(errorAfterSemiColon.startsWith("The batch request operation exceeds the maximum 100 changes per change set.")); assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.INVALID_INPUT); } }
private void testBatchRetrieve(TableRequestOptions options) throws StorageException { // insert entity Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(ref), options, null); TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); ArrayList<TableResult> results = this.table.execute(batch, options, null); assertEquals(results.size(), 1); assertEquals(results.get(0).getHttpStatusCode(), HttpURLConnection.HTTP_OK); Class1 retrievedRef = results.get(0).getResultAsType(); assertEquals(ref.getA(), retrievedRef.getA()); assertEquals(ref.getB(), retrievedRef.getB()); assertEquals(ref.getC(), retrievedRef.getC()); assertTrue(Arrays.equals(ref.getD(), retrievedRef.getD())); this.table.execute(TableOperation.delete(ref), options, null); }
@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); }
@Test @Category(SecondaryTests.class) public void testBatchSecondaryNoWrite() throws StorageException { // create and insert an entity Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(ref)); // create a batch and add a query for this entity TableBatchOperation batch = new TableBatchOperation(); TableOperation queryOp = TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); batch.add(queryOp); // should be able to make a request to secondary as there are no writes TableRequestOptions options = new TableRequestOptions(); options.setLocationMode(LocationMode.SECONDARY_ONLY); options.setRetryPolicyFactory(new RetryNoRetry()); this.table.execute(batch, options, null); }
private void testBatchDelete(TableRequestOptions options) throws StorageException { Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); // insert entity this.table.execute(TableOperation.insert(ref), options, null); TableBatchOperation batch = new TableBatchOperation(); batch.delete(ref); ArrayList<TableResult> delResults = this.table.execute(batch, options, null); for (TableResult r : delResults) { assertEquals(r.getHttpStatusCode(), HttpURLConnection.HTTP_NO_CONTENT); } try { this.table.execute(batch, options, null); fail(); } catch (StorageException ex) { assertEquals(ex.getHttpStatusCode(), HttpURLConnection.HTTP_NOT_FOUND); } }