本文整理汇总了C#中TableBatchOperation.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# TableBatchOperation.Replace方法的具体用法?C# TableBatchOperation.Replace怎么用?C# TableBatchOperation.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TableBatchOperation
的用法示例。
在下文中一共展示了TableBatchOperation.Replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TableBatchAllSupportedOperationsAPM
public void TableBatchAllSupportedOperationsAPM()
{
CloudTableClient tableClient = GenerateCloudTableClient();
TableBatchOperation batch = new TableBatchOperation();
string pk = Guid.NewGuid().ToString();
// insert
batch.Insert(GenerateRandomEnitity(pk));
// delete
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.Delete(entity);
}
// replace
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.Replace(entity);
}
// insert or replace
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.InsertOrReplace(entity);
}
// merge
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.Merge(entity);
}
// insert or merge
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.InsertOrMerge(entity);
}
IList<TableResult> results = null;
using (ManualResetEvent evt = new ManualResetEvent(false))
{
IAsyncResult asyncRes = null;
currentTable.BeginExecuteBatch(batch, (res) =>
{
asyncRes = res;
evt.Set();
}, null);
evt.WaitOne();
results = currentTable.EndExecuteBatch(asyncRes);
}
Assert.AreEqual(results.Count, 6);
IEnumerator<TableResult> enumerator = results.GetEnumerator();
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.Created);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
}
示例2: TableBatchOperationsWithEmptyKeys
public void TableBatchOperationsWithEmptyKeys()
{
CloudTableClient tableClient = GenerateCloudTableClient();
// Insert Entity
DynamicTableEntity ent = new DynamicTableEntity() { PartitionKey = "", RowKey = "" };
ent.Properties.Add("foo2", new EntityProperty("bar2"));
ent.Properties.Add("foo", new EntityProperty("bar"));
TableBatchOperation batch = new TableBatchOperation();
batch.Insert(ent);
currentTable.ExecuteBatch(batch);
// Retrieve Entity
TableBatchOperation retrieveBatch = new TableBatchOperation();
retrieveBatch.Retrieve(ent.PartitionKey, ent.RowKey);
TableResult result = currentTable.ExecuteBatch(retrieveBatch).First();
DynamicTableEntity retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(ent.Properties["foo"].StringValue, retrievedEntity.Properties["foo"].StringValue);
Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
Assert.AreEqual(ent.Properties["foo2"].StringValue, retrievedEntity.Properties["foo2"].StringValue);
Assert.AreEqual(ent.Properties["foo2"], retrievedEntity.Properties["foo2"]);
// InsertOrMerge
DynamicTableEntity insertOrMergeEntity = new DynamicTableEntity(ent.PartitionKey, ent.RowKey);
insertOrMergeEntity.Properties.Add("foo3", new EntityProperty("value"));
batch = new TableBatchOperation();
batch.InsertOrMerge(insertOrMergeEntity);
currentTable.ExecuteBatch(batch);
result = currentTable.ExecuteBatch(retrieveBatch).First();
retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(insertOrMergeEntity.Properties["foo3"], retrievedEntity.Properties["foo3"]);
// InsertOrReplace
DynamicTableEntity insertOrReplaceEntity = new DynamicTableEntity(ent.PartitionKey, ent.RowKey);
insertOrReplaceEntity.Properties.Add("prop2", new EntityProperty("otherValue"));
batch = new TableBatchOperation();
batch.InsertOrReplace(insertOrReplaceEntity);
currentTable.ExecuteBatch(batch);
result = currentTable.ExecuteBatch(retrieveBatch).First();
retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(1, retrievedEntity.Properties.Count);
Assert.AreEqual(insertOrReplaceEntity.Properties["prop2"], retrievedEntity.Properties["prop2"]);
// Merge
DynamicTableEntity mergeEntity = new DynamicTableEntity(retrievedEntity.PartitionKey, retrievedEntity.RowKey) { ETag = retrievedEntity.ETag };
mergeEntity.Properties.Add("mergeProp", new EntityProperty("merged"));
batch = new TableBatchOperation();
batch.Merge(mergeEntity);
currentTable.ExecuteBatch(batch);
// Retrieve Entity & Verify Contents
result = currentTable.ExecuteBatch(retrieveBatch).First();
retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(mergeEntity.Properties["mergeProp"], retrievedEntity.Properties["mergeProp"]);
// Replace
DynamicTableEntity replaceEntity = new DynamicTableEntity(ent.PartitionKey, ent.RowKey) { ETag = retrievedEntity.ETag };
replaceEntity.Properties.Add("replaceProp", new EntityProperty("replace"));
batch = new TableBatchOperation();
batch.Replace(replaceEntity);
currentTable.ExecuteBatch(batch);
// Retrieve Entity & Verify Contents
result = currentTable.ExecuteBatch(retrieveBatch).First();
retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(replaceEntity.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(replaceEntity.Properties["replaceProp"], retrievedEntity.Properties["replaceProp"]);
// Delete Entity
batch = new TableBatchOperation();
batch.Delete(retrievedEntity);
currentTable.ExecuteBatch(batch);
// Retrieve Entity
result = currentTable.ExecuteBatch(retrieveBatch).First();
Assert.IsNull(result.Result);
}
示例3: TableBatchReplaceFailAPM
public void TableBatchReplaceFailAPM()
{
CloudTableClient tableClient = GenerateCloudTableClient();
// Insert Entity
DynamicTableEntity baseEntity = new DynamicTableEntity("merge test", "foo");
baseEntity.Properties.Add("prop1", new EntityProperty("value1"));
currentTable.Execute(TableOperation.Insert(baseEntity));
string staleEtag = baseEntity.ETag;
// update entity to rev etag
baseEntity.Properties["prop1"].StringValue = "updated value";
currentTable.Execute(TableOperation.Replace(baseEntity));
OperationContext opContext = new OperationContext();
try
{
// Attempt a merge with stale etag
DynamicTableEntity replaceEntity = new DynamicTableEntity(baseEntity.PartitionKey, baseEntity.RowKey) { ETag = staleEtag };
replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
TableBatchOperation batch = new TableBatchOperation();
batch.Replace(replaceEntity);
using (ManualResetEvent evt = new ManualResetEvent(false))
{
IAsyncResult asyncRes = null;
currentTable.BeginExecuteBatch(batch, null, opContext, (res) =>
{
asyncRes = res;
evt.Set();
}, null);
evt.WaitOne();
currentTable.EndExecuteBatch(asyncRes);
}
Assert.Fail();
}
catch (StorageException)
{
TestHelper.ValidateResponse(opContext,
1,
(int)HttpStatusCode.PreconditionFailed,
new string[] { "UpdateConditionNotSatisfied", "ConditionNotMet" },
new string[] { "The update condition specified in the request was not satisfied.", "The condition specified using HTTP conditional header(s) is not met." });
}
// Delete Entity
currentTable.Execute(TableOperation.Delete(baseEntity));
opContext = new OperationContext();
// try replacing with deleted entity
try
{
DynamicTableEntity replaceEntity = new DynamicTableEntity(baseEntity.PartitionKey, baseEntity.RowKey) { ETag = baseEntity.ETag };
replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
TableBatchOperation batch = new TableBatchOperation();
batch.Replace(replaceEntity);
using (ManualResetEvent evt = new ManualResetEvent(false))
{
IAsyncResult asyncRes = null;
currentTable.BeginExecuteBatch(batch, null, opContext, (res) =>
{
asyncRes = res;
evt.Set();
}, null);
evt.WaitOne();
currentTable.EndExecuteBatch(asyncRes);
}
Assert.Fail();
}
catch (StorageException)
{
TestHelper.ValidateResponse(opContext, 1, (int)HttpStatusCode.NotFound, new string[] { "ResourceNotFound" }, "The specified resource does not exist.");
}
}
示例4: TableBatchAllSupportedOperationsSync
public void TableBatchAllSupportedOperationsSync()
{
CloudTableClient tableClient = GenerateCloudTableClient();
TableBatchOperation batch = new TableBatchOperation();
string pk = Guid.NewGuid().ToString();
// insert
batch.Insert(GenerateRandomEnitity(pk));
// delete
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.Delete(entity);
}
// replace
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.Replace(entity);
}
// insert or replace
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.InsertOrReplace(entity);
}
// merge
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.Merge(entity);
}
// insert or merge
{
DynamicTableEntity entity = GenerateRandomEnitity(pk);
currentTable.Execute(TableOperation.Insert(entity));
batch.InsertOrMerge(entity);
}
IList<TableResult> results = currentTable.ExecuteBatch(batch);
Assert.AreEqual(results.Count, 6);
IEnumerator<TableResult> enumerator = results.GetEnumerator();
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.Created);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
}
示例5: DoTableBatchAllSupportedOperationsAsync
private async Task DoTableBatchAllSupportedOperationsAsync(TablePayloadFormat format)
{
tableClient.DefaultRequestOptions.PayloadFormat = format;
TableBatchOperation batch = new TableBatchOperation();
string pk = Guid.NewGuid().ToString();
// insert
batch.Insert(GenerateRandomEntity(pk));
// delete
{
DynamicTableEntity entity = GenerateRandomEntity(pk);
await currentTable.ExecuteAsync(TableOperation.Insert(entity));
batch.Delete(entity);
}
// replace
{
DynamicTableEntity entity = GenerateRandomEntity(pk);
await currentTable.ExecuteAsync(TableOperation.Insert(entity));
batch.Replace(entity);
}
// insert or replace
{
DynamicTableEntity entity = GenerateRandomEntity(pk);
await currentTable.ExecuteAsync(TableOperation.Insert(entity));
batch.InsertOrReplace(entity);
}
// merge
{
DynamicTableEntity entity = GenerateRandomEntity(pk);
await currentTable.ExecuteAsync(TableOperation.Insert(entity));
batch.Merge(entity);
}
// insert or merge
{
DynamicTableEntity entity = GenerateRandomEntity(pk);
await currentTable.ExecuteAsync(TableOperation.Insert(entity));
batch.InsertOrMerge(entity);
}
IList<TableResult> results = await currentTable.ExecuteBatchAsync(batch);
Assert.AreEqual(results.Count, 6);
IEnumerator<TableResult> enumerator = results.GetEnumerator();
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.Created);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
enumerator.MoveNext();
Assert.AreEqual(enumerator.Current.HttpStatusCode, (int)HttpStatusCode.NoContent);
}
示例6: Execute
public void Execute(TableBatchOperation batchOperation, TableEntity entity)
{
entity.ETag = "*"; // Always overwrite (ignore concurrency).
batchOperation.Replace(entity);
}
示例7: DoRandomAtomicCalls
async Task DoRandomAtomicCalls()
{
for (int callNum = 0; callNum < MigrationModel.NUM_CALLS_PER_MACHINE; callNum++)
{
SortedDictionary<PrimaryKey, DynamicTableEntity> dump = await peekProxy.DumpReferenceTableAsync();
if (PSharpRuntime.Nondeterministic())
{
// Query
var query = new TableQuery<DynamicTableEntity>();
query.FilterString = ChainTableUtils.CombineFilters(
TableQuery.GenerateFilterCondition(
TableConstants.PartitionKey, QueryComparisons.Equal, MigrationModel.SINGLE_PARTITION_KEY),
TableOperators.And,
NondeterministicUserPropertyFilterString());
await RunQueryAtomicAsync(query);
}
else
{
// Batch write
int batchSize = PSharpRuntime.Nondeterministic() ? 2 : 1;
var batch = new TableBatchOperation();
var rowKeyChoices = new List<string> { "0", "1", "2", "3", "4", "5" };
for (int opNum = 0; opNum < batchSize; opNum++)
{
int opTypeNum = PSharpNondeterminism.Choice(7);
int rowKeyI = PSharpNondeterminism.Choice(rowKeyChoices.Count);
string rowKey = rowKeyChoices[rowKeyI];
rowKeyChoices.RemoveAt(rowKeyI); // Avoid duplicate in same batch
var primaryKey = new PrimaryKey(MigrationModel.SINGLE_PARTITION_KEY, rowKey);
string eTag = null;
if (opTypeNum >= 1 && opTypeNum <= 3)
{
DynamicTableEntity existingEntity;
int etagTypeNum = PSharpNondeterminism.Choice(
dump.TryGetValue(primaryKey, out existingEntity) ? 3 : 2);
switch (etagTypeNum)
{
case 0: eTag = ChainTable2Constants.ETAG_ANY; break;
case 1: eTag = "wrong"; break;
case 2: eTag = existingEntity.ETag; break;
}
}
DynamicTableEntity entity = new DynamicTableEntity
{
PartitionKey = MigrationModel.SINGLE_PARTITION_KEY,
RowKey = rowKey,
ETag = eTag,
Properties = new Dictionary<string, EntityProperty> {
// Give us something to see on merge. Might help with tracing too!
{ string.Format("{0}_c{1}_o{2}", machineId.ToString(), callNum, opNum),
new EntityProperty(true) },
// Property with 50%/50% distribution for use in filters.
{ "isHappy", new EntityProperty(PSharpRuntime.Nondeterministic()) }
}
};
switch (opTypeNum)
{
case 0: batch.Insert(entity); break;
case 1: batch.Replace(entity); break;
case 2: batch.Merge(entity); break;
case 3: batch.Delete(entity); break;
case 4: batch.InsertOrReplace(entity); break;
case 5: batch.InsertOrMerge(entity); break;
case 6:
entity.ETag = ChainTable2Constants.ETAG_DELETE_IF_EXISTS;
batch.Delete(entity); break;
}
}
await RunBatchAsync(batch);
}
}
}
示例8: DoTableBatchReplaceAsync
private async Task DoTableBatchReplaceAsync(TablePayloadFormat format)
{
tableClient.DefaultRequestOptions.PayloadFormat = format;
// Insert Entity
DynamicTableEntity baseEntity = new DynamicTableEntity("merge test", "foo" + format.ToString());
baseEntity.Properties.Add("prop1", new EntityProperty("value1"));
await currentTable.ExecuteAsync(TableOperation.Insert(baseEntity));
// ReplaceEntity
DynamicTableEntity replaceEntity = new DynamicTableEntity(baseEntity.PartitionKey, baseEntity.RowKey) { ETag = baseEntity.ETag };
replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
TableBatchOperation batch = new TableBatchOperation();
batch.Replace(replaceEntity);
await currentTable.ExecuteBatchAsync(batch);
// Retrieve Entity & Verify Contents
TableResult result = await currentTable.ExecuteAsync(TableOperation.Retrieve(baseEntity.PartitionKey, baseEntity.RowKey));
DynamicTableEntity retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(replaceEntity.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(replaceEntity.Properties["prop2"], retrievedEntity.Properties["prop2"]);
}
示例9: TableBatchAllSupportedOperationsSync
public void TableBatchAllSupportedOperationsSync()
{
TableBatchOperation batch = new TableBatchOperation();
string pk = Guid.NewGuid().ToString();
// insert
batch.Insert(GenerateRandomEnitity(pk));
// delete
{
DynamicReplicatedTableEntity entity = GenerateRandomEnitity(pk);
this.repTable.Execute(TableOperation.Insert(entity));
batch.Delete(entity);
}
// replace
{
DynamicReplicatedTableEntity entity = GenerateRandomEnitity(pk);
this.repTable.Execute(TableOperation.Insert(entity));
batch.Replace(entity);
}
// insert or replace
{
DynamicReplicatedTableEntity entity = GenerateRandomEnitity(pk);
this.repTable.Execute(TableOperation.Insert(entity));
batch.InsertOrReplace(entity);
}
// merge
{
DynamicReplicatedTableEntity entity = GenerateRandomEnitity(pk);
this.repTable.Execute(TableOperation.Insert(entity));
batch.Merge(entity);
}
// insert or merge
{
DynamicReplicatedTableEntity entity = GenerateRandomEnitity(pk);
this.repTable.Execute(TableOperation.Insert(entity));
batch.InsertOrMerge(entity);
}
IList<TableResult> results = this.repTable.ExecuteBatch(batch);
Assert.AreEqual(results.Count, 6);
IEnumerator<TableResult> enumerator = results.GetEnumerator();
for (int i = 0; i < results.Count; i++)
{
enumerator.MoveNext();
Assert.AreEqual((int)HttpStatusCode.NoContent, enumerator.Current.HttpStatusCode, "HttpStatusCode mismatch i={0}", i);
}
}
示例10: TableBatchReplaceSync
public void TableBatchReplaceSync()
{
// Insert Entity
Console.WriteLine("Calling Insert()...");
DynamicReplicatedTableEntity baseEntity = new DynamicReplicatedTableEntity("replace test", "foo");
baseEntity.Properties.Add("prop1", new EntityProperty("value1"));
this.repTable.Execute(TableOperation.Insert(baseEntity));
// ReplaceEntity
DynamicReplicatedTableEntity replaceEntity = new DynamicReplicatedTableEntity(baseEntity.PartitionKey, baseEntity.RowKey)
{
ETag = "1"
};
replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
Console.WriteLine("Calling Replace()...");
TableBatchOperation batch = new TableBatchOperation();
batch.Replace(replaceEntity);
this.repTable.ExecuteBatch(batch);
// Retrieve Entity & Verify Contents
Console.WriteLine("Calling Retrieve()...");
TableResult result = this.repTable.Execute(TableOperation.Retrieve<DynamicReplicatedTableEntity>(baseEntity.PartitionKey, baseEntity.RowKey));
DynamicReplicatedTableEntity retrievedEntity = result.Result as DynamicReplicatedTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(replaceEntity.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(replaceEntity.Properties["prop2"], retrievedEntity.Properties["prop2"]);
//
// Replace() again
//
Console.WriteLine("Calling Replace() again, setting Etag to {0}", retrievedEntity._rtable_Version);
replaceEntity = new DynamicReplicatedTableEntity(baseEntity.PartitionKey, baseEntity.RowKey)
{
ETag = retrievedEntity._rtable_Version.ToString()
};
replaceEntity.Properties.Add("prop3", new EntityProperty("value3"));
batch = new TableBatchOperation();
batch.Replace(replaceEntity);
this.repTable.ExecuteBatch(batch);
Console.WriteLine("Calling Retrieve()...");
result = this.repTable.Execute(TableOperation.Retrieve<DynamicReplicatedTableEntity>(baseEntity.PartitionKey, baseEntity.RowKey));
retrievedEntity = result.Result as DynamicReplicatedTableEntity;
Assert.IsNotNull(retrievedEntity);
Console.WriteLine("{0}", retrievedEntity.ToString());
Assert.AreEqual(replaceEntity.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(replaceEntity.Properties["prop3"], retrievedEntity.Properties["prop3"]);
}
示例11: BatchOperationExceptionWhenUsingSmallerViewId
public void BatchOperationExceptionWhenUsingSmallerViewId()
{
long currentViewId = 100;
long badViewId = currentViewId - 1;
this.UpdateConfiguration(replicas, 0, false, currentViewId);
string jobType = "jobType-BatchOperationExceptionWhenUsingSmallerViewId";
string jobId = "jobId-BatchOperationExceptionWhenUsingSmallerViewId";
int count = 3; // number of operations in the batch _rtable_Operation
List<TableOperationType> opTypes = new List<TableOperationType>()
{
TableOperationType.Replace,
TableOperationType.InsertOrReplace,
TableOperationType.Delete,
};
//
// Insert
//
string jobIdTemplate = jobId + "-{0}";
string messageTemplate = "message-{0}";
string updatedMessageTemplate = "updated-" + messageTemplate;
string partitionKey = string.Empty;
//
// Insert entities
//
for (int i = 0; i < count; i++)
{
SampleRTableEntity originalEntity = new SampleRTableEntity(
jobType,
string.Format(jobIdTemplate, i),
string.Format(messageTemplate, i));
this.repTable.Execute(TableOperation.Insert(originalEntity));
partitionKey = originalEntity.PartitionKey;
}
//
// Retrieve entities and use them to create batchOperation to Replace or Delete
//
IEnumerable<SampleRTableEntity> allEntities = this.rtableWrapper.GetAllRows(partitionKey);
TableBatchOperation batchOperation = new TableBatchOperation();
int m = 0;
foreach (SampleRTableEntity entity in allEntities)
{
Console.WriteLine("{0}", entity.ToString());
Console.WriteLine("---------------------------------------");
if (opTypes[m] == TableOperationType.Replace)
{
SampleRTableEntity replaceEntity = new SampleRTableEntity(
entity.JobType,
entity.JobId,
string.Format(updatedMessageTemplate, m))
{
ETag = entity._rtable_Version.ToString()
};
batchOperation.Replace(replaceEntity);
}
else if (opTypes[m] == TableOperationType.InsertOrReplace)
{
SampleRTableEntity replaceEntity = new SampleRTableEntity(
entity.JobType,
entity.JobId,
string.Format(updatedMessageTemplate, m))
{
ETag = entity._rtable_Version.ToString()
};
batchOperation.InsertOrReplace(replaceEntity);
}
else if (opTypes[m] == TableOperationType.Delete)
{
entity.ETag = entity._rtable_Version.ToString();
batchOperation.Delete(entity);
}
else
{
throw new ArgumentException(
string.Format("opType={0} is NOT supported", opTypes[m]),
"opType");
}
m++;
}
//
// Call ModifyConfigurationBlob to change the viewId of the wrapper to an older value
//
Console.WriteLine("Changing the viewId to badViewId {0}", badViewId);
this.UpdateConfiguration(replicas, 0, false, badViewId);
//
// Execute Batch _rtable_Operation with bad viewId
//
Console.WriteLine("\nCalling BatchOperation with badViewId...");
try
{
this.repTable.ExecuteBatch(batchOperation);
}
//.........这里部分代码省略.........
示例12: SetupAndRunTemperBatchOperation
/// <summary>
/// Helper function to create some initial entities and then call the specified batchOperation with HttpMangler enabled.
/// </summary>
/// <param name="count"></param>
/// <param name="jobType"></param>
/// <param name="jobId"></param>
/// <param name="targetStorageAccount"></param>
/// <param name="opTypes"></param>
/// <param name="targetApiExpectedToFail"></param>
/// <param name="checkOriginalEntityUnchanged"></param>
/// <param name="checkStorageAccountsConsistent"></param>
/// <param name="httpManglerStartTime"></param>
/// <param name="skipInitialSessions"></param>
/// <returns>ParitionKey of the initial entities created</returns>
protected string SetupAndRunTemperBatchOperation(
int count,
string jobType,
string jobId,
int targetStorageAccount,
List<TableOperationType> opTypes,
bool targetApiExpectedToFail,
bool checkOriginalEntityUnchanged,
bool checkStorageAccountsConsistent,
out DateTime httpManglerStartTime,
int skipInitialSessions = 0)
{
Assert.IsTrue(0 <= targetStorageAccount && targetStorageAccount < this.actualStorageAccountsUsed.Count,
"SetupAndRunTemperBatchOperation() is called with out-of-range targetStorageAccount={0}", targetStorageAccount);
int index = this.actualStorageAccountsUsed[targetStorageAccount];
string accountNameToTamper = this.rtableTestConfiguration.StorageInformation.AccountNames[index];
Console.WriteLine("SetupAndRunTemperBatchOperation(): accountNameToTamper={0} skipInitialSessions={1}",
accountNameToTamper, skipInitialSessions);
Assert.AreEqual(count, opTypes.Count, "count and opTypes.Count should be the same");
//
// Tamper behavior
//
ProxyBehavior[] behaviors = new[]
{
TamperBehaviors.TamperAllRequestsIf(
(session => { session.Abort();}),
skipInitialSessions,
AzureStorageSelectors.TableTraffic().IfHostNameContains(accountNameToTamper + "."))
};
string jobIdTemplate = jobId + "-{0}";
string messageTemplate = "message-{0}";
string updatedMessageTemplate = "updated-" + messageTemplate;
string partitionKey = string.Empty;
//
// Insert entities
//
for (int i = 0; i < count; i++)
{
SampleRTableEntity originalEntity = new SampleRTableEntity(
jobType,
string.Format(jobIdTemplate, i),
string.Format(messageTemplate, i));
this.repTable.Execute(TableOperation.Insert(originalEntity));
partitionKey = originalEntity.PartitionKey;
}
//
// Retrieve entities and use them to create batchOperation to Replace or Delete
//
IEnumerable<SampleRTableEntity> allEntities = this.rtableWrapper.GetAllRows(partitionKey);
TableBatchOperation batchOperation = new TableBatchOperation();
int m = 0;
foreach (SampleRTableEntity entity in allEntities)
{
Console.WriteLine("{0}", entity.ToString());
Console.WriteLine("---------------------------------------");
if (opTypes[m] == TableOperationType.Replace)
{
SampleRTableEntity replaceEntity = new SampleRTableEntity(
entity.JobType,
entity.JobId,
string.Format(updatedMessageTemplate, m))
{
ETag = entity._rtable_Version.ToString()
};
batchOperation.Replace(replaceEntity);
}
else if (opTypes[m] == TableOperationType.Delete)
{
entity.ETag = entity._rtable_Version.ToString();
batchOperation.Delete(entity);
}
else
{
throw new ArgumentException(
string.Format("opType={0} is NOT supported", opTypes[m]),
"opType");
}
m++;
}
//.........这里部分代码省略.........
示例13: ExecuteBatchOperationAndValidate
/// <summary>
/// Helper function to execute the specified BatchOperation after HttpMangler is turned off and validate correctness.
/// </summary>
/// <param name="count">Number of operations in the batch</param>
/// <param name="partitionKey">partitionKey to operate on</param>
/// <param name="jobType">partitionKey is generated from jobType</param>
/// <param name="jobId">RowKey is generated from jobId. JobIdTemplate = jobId-{0}</param>
/// <param name="opTypes">Specifies the batch operation to be performed</param>
protected void ExecuteBatchOperationAndValidate(
int count,
string partitionKey,
string jobType,
string jobId,
List<TableOperationType> opTypes)
{
Console.WriteLine("\nExecuteBatchOperationAndValidate(): Trying to batch update {0} entities...", count);
Assert.IsNotNull(opTypes, "opTypes = null");
Assert.AreEqual(count, opTypes.Count, "count and opTypes.Count should be the same");
string jobIdTemplate = jobId + "-{0}";
string replaceMessageTemplate = "updated-after-httpMangler-{0}";
IEnumerable<SampleRTableEntity> allEntities = null;
TableBatchOperation batchOperation = null;
int m = 0;
bool gotExceptionInLastAttempt = true;
int retries = 0;
while (retries < MaxRetries)
{
try
{
//
// GetAllRows()
//
allEntities = this.rtableWrapper.GetAllRows(partitionKey);
//
// Create a batchOperation to perform the specified opTypes
//
batchOperation = new TableBatchOperation();
m = 0;
foreach (SampleRTableEntity entity in allEntities)
{
if (opTypes[m] == TableOperationType.Replace)
{
// set up the new entity to be used in the batch operation
SampleRTableEntity replaceEntity = new SampleRTableEntity(
entity.JobType,
entity.JobId,
string.Format(replaceMessageTemplate, m))
{
ETag = entity._rtable_Version.ToString()
};
// add the operation to the batch operation
batchOperation.Replace(replaceEntity);
}
else if (opTypes[m] == TableOperationType.Delete)
{
batchOperation.Delete(entity);
}
else
{
throw new ArgumentException(
string.Format("opType={0} is NOT supported", opTypes[m]),
"opType");
}
m++;
}
//
// Call this.repTable.ExecuteBatch(batchOperation);
//
if (batchOperation.Count == 0)
{
Console.WriteLine("retries={0}. Done. batchOperation.Count == 0", retries);
}
else
{
this.repTable.ExecuteBatch(batchOperation);
Console.WriteLine("retries={0}. Done ExecuteBatch()", retries);
}
gotExceptionInLastAttempt = false;
break;
}
catch (RTableConflictException ex)
{
Console.WriteLine("retries={0}. ExecuteBatch() got an RTableConflictException: {1}",
retries, ex.ToString());
retries++;
gotExceptionInLastAttempt = true;
Thread.Sleep(ConflictExceptionSleepTimeInMsec);
}
}
Console.WriteLine("gotExceptionInLastAttempt={0} retries={1} MaxRetries={2}",
gotExceptionInLastAttempt, retries, MaxRetries);
Assert.IsFalse(gotExceptionInLastAttempt, "The last API call should not throw an exception.");
//.........这里部分代码省略.........
示例14: TableBatchReplaceSync
public void TableBatchReplaceSync()
{
CloudTableClient tableClient = GenerateCloudTableClient();
// Insert Entity
DynamicTableEntity baseEntity = new DynamicTableEntity("merge test", "foo");
baseEntity.Properties.Add("prop1", new EntityProperty("value1"));
currentTable.Execute(TableOperation.Insert(baseEntity));
// ReplaceEntity
DynamicTableEntity replaceEntity = new DynamicTableEntity(baseEntity.PartitionKey, baseEntity.RowKey) { ETag = baseEntity.ETag };
replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
TableBatchOperation batch = new TableBatchOperation();
batch.Replace(replaceEntity);
currentTable.ExecuteBatch(batch);
// Retrieve Entity & Verify Contents
TableResult result = currentTable.Execute(TableOperation.Retrieve(baseEntity.PartitionKey, baseEntity.RowKey));
DynamicTableEntity retrievedEntity = result.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(replaceEntity.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(replaceEntity.Properties["prop2"], retrievedEntity.Properties["prop2"]);
}
示例15: DoEscapeTest
private void DoEscapeTest(string data, bool useBatch, bool includeKey)
{
DynamicTableEntity ent = new DynamicTableEntity(includeKey ? "temp" + data : "temp", Guid.NewGuid().ToString());
ent.Properties.Add("foo", new EntityProperty(data));
// Insert
if (useBatch)
{
TableBatchOperation batch = new TableBatchOperation();
batch.Insert(ent);
currentTable.ExecuteBatch(batch);
}
else
{
currentTable.Execute(TableOperation.Insert(ent));
}
// Retrieve
TableResult res = null;
if (useBatch)
{
TableBatchOperation batch = new TableBatchOperation();
batch.Retrieve(ent.PartitionKey, ent.RowKey);
res = (currentTable.ExecuteBatch(batch))[0];
}
else
{
res = currentTable.Execute(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
}
// Check equality
DynamicTableEntity retrievedEntity = res.Result as DynamicTableEntity;
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
// Query using data filter
TableQuery query = new TableQuery();
query.Where(string.Format(
"(PartitionKey eq \'{0}\') and (RowKey eq \'{1}\') and (foo eq \'{2}\')",
ent.PartitionKey,
ent.RowKey,
data.Replace("\'", "\'\'")));
retrievedEntity = currentTable.ExecuteQuery(query).Single();
Assert.IsNotNull(retrievedEntity);
Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
// Merge
ent.Properties.Add("foo2", new EntityProperty("bar2"));
if (useBatch)
{
TableBatchOperation batch = new TableBatchOperation();
batch.Merge(ent);
currentTable.ExecuteBatch(batch);
}
else
{
currentTable.Execute(TableOperation.Merge(ent));
}
// Retrieve
if (useBatch)
{
TableBatchOperation batch = new TableBatchOperation();
batch.Retrieve(ent.PartitionKey, ent.RowKey);
res = (currentTable.ExecuteBatch(batch))[0];
}
else
{
res = currentTable.Execute(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
}
retrievedEntity = res.Result as DynamicTableEntity;
Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
// Replace
ent.Properties.Remove("foo2");
ent.Properties.Add("foo3", new EntityProperty("bar3"));
if (useBatch)
{
TableBatchOperation batch = new TableBatchOperation();
batch.Replace(ent);
currentTable.ExecuteBatch(batch);
}
else
//.........这里部分代码省略.........