没有办法通过键值 NoSQL 数据库服务(如 Azure Table)以可扩展的方式实现请求的功能。我推荐的解决方案是创建两个新表,Entity1TransitionTable 和 Entity2TransitionTable。该提议是在 Azure 表存储相对便宜的前提下动态检测转换并维护这些表中的转换列表。此解决方案假定应用程序始终可以跟踪 ID 值并且它始终递增 1。
public class LogEntry : TableEntity
{
public LogEntry() { }
public LogEntry(string userid, int id, string date, bool entity1, bool entity2)
{
PartitionKey = userid;
RowKey = id.ToString();
Id = id;
Entity1 = entity1;
Entity2 = entity2;
Date = date;
}
public int Id { get; set; }
public string Date { get; set; }
public bool Entity1 { get; set; }
public bool Entity2 { get; set; }
}
static void TransitionDetection(CloudTable tbl, CloudTable e1Tbl, CloudTable e2Tbl, LogEntry currentLogEntry)
{
if (currentLogEntry.Id> 1)
{
// Retrieve the previous log entry
string pkFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, currentLogEntry.PartitionKey);
string rkFilter = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, (currentLogEntry.Id - 1).ToString());
string combinedFilter = TableQuery.CombineFilters(pkFilter, TableOperators.And, rkFilter);
TableQuery<LogEntry> query = new TableQuery<LogEntry>().Where(combinedFilter);
LogEntry prevLogEntry = tbl.ExecuteQuery(query).ToList()[0];
// If Entity1 transitioned, record it
if (prevLogEntry.Entity1 != currentLogEntry.Entity1)
{
e1Tbl.Execute(TableOperation.Insert(currentLogEntry));
}
// If Entity2 transitioned, record it
if (prevLogEntry.Entity2 != currentLogEntry.Entity2)
{
e2Tbl.Execute(TableOperation.Insert(currentLogEntry));
}
}
}
static void Main(string[] args)
{
string storageConnection = "DefaultEndpointsProtocol=https;AccountName=MyTable;AccountKey=MY_KEY";
// Select table
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnection);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("LogTable");
CloudTable entity1TransitionTable = tableClient.GetTableReference("Entity1TransitionTable");
CloudTable entity2TransitionTable = tableClient.GetTableReference("Entity2TransitionTable");
table.CreateIfNotExists();
entity1TransitionTable.CreateIfNotExists();
entity2TransitionTable.CreateIfNotExists();
// Add Log Entries
int id = 0;
LogEntry le1 = new LogEntry("0", ++id, "May 1 2015", true, false);
TransitionDetection(table, entity1TransitionTable, entity2TransitionTable, le1);
table.Execute(TableOperation.Insert(le1));
LogEntry le2 = new LogEntry("0", ++id, "May 2 2015", false, true);
TransitionDetection(table, entity1TransitionTable, entity2TransitionTable, le2);
table.Execute(TableOperation.Insert(le2));
LogEntry le3 = new LogEntry("0", ++id, "May 3 2015", false, false);
TransitionDetection(table, entity1TransitionTable, entity2TransitionTable, le3);
table.Execute(TableOperation.Insert(le3));
LogEntry le4 = new LogEntry("0", ++id, "May 4 2015", true, false);
TransitionDetection(table, entity1TransitionTable, entity2TransitionTable, le4);
table.Execute(TableOperation.Insert(le4));
LogEntry le5 = new LogEntry("0", ++id, "May 5 2015", false, true);
TransitionDetection(table, entity1TransitionTable, entity2TransitionTable, le5);
table.Execute(TableOperation.Insert(le5));
LogEntry le6 = new LogEntry("0", ++id, "May 7 2015", true, false);
TransitionDetection(table, entity1TransitionTable, entity2TransitionTable, le6);
table.Execute(TableOperation.Insert(le6));
Console.ReadKey();
}
如果您使用 TableXplorer 之类的应用程序检查表格,您将观察到以下结果:
日志表
Entity1TransitionTable
Entity2TransitionTable