/// <summary> /// Gets all sequence definitions available. /// </summary> /// <returns></returns> public IEnumerable <SequenceIdStore> GetSequenceIdStores() { var schemas = SchemaTableProxy.QueryPartitions("0", "zzzzz"); var sequences = SequenceTableProxy.QueryPartitions("0", "zzzzz") ?? new List <TableEntityProxy <DynamicTableEntity> >(); var stores = new List <SequenceIdStore>(); foreach (var schema in schemas) { var sequence = sequences.FirstOrDefault(x => String.Equals(schema.Entity.TableName, x.Entity[TableConstants.PartitionKey].StringValue, StringComparison.InvariantCultureIgnoreCase)); if (sequence == null) { sequence = new TableEntityProxy <DynamicTableEntity>(new DynamicTableEntity(schema.PartitionKey, schema.RowKey, "", new Dictionary <string, EntityProperty>())); sequence.Entity.Properties.Add(PropertyFinalCachedId, new EntityProperty(schema.Entity.SeedValue)); SequenceTableProxy.Insert(sequence.Entity); //this could throw if another thread created after SequenceTableProxy.QueryPartitions was called above. } stores.Add(new SequenceIdStore { FinalCachedId = sequence.Entity.Properties[PropertyFinalCachedId].StringValue, Schema = schema.Entity }); } return(stores); }
/// <summary> /// Gets the sequence for a given table. /// </summary> /// <param name="tableName"></param> /// <returns></returns> public SequenceIdStore GetSequenceIdStore(string tableName) { var schema = SchemaTableProxy.Get(tableName.GetValidPartitionKey(), tableName.GetValidRowKey()); if (schema == null || schema.Entity == null) { return(null); } var sequence = SequenceTableProxy.Get(tableName.GetValidPartitionKey(), tableName.GetValidRowKey()) ?? CreateSequenceFromSchema(tableName); return(new SequenceIdStore { FinalCachedId = sequence.Entity.Properties[PropertyFinalCachedId].StringValue, Schema = schema.Entity }); }
private TableEntityProxy <DynamicTableEntity> CreateSequenceFromSchema(string tableName) { var schema = SchemaTableProxy.Get(tableName.GetValidPartitionKey(), tableName.GetValidRowKey()); if (schema == null) { throw new ArgumentException(String.Format("Sequence '{0}' is not defined. Cannot get the latest reserved key value.", tableName)); } var sequence = new TableEntityProxy <DynamicTableEntity>(new DynamicTableEntity(schema.PartitionKey, schema.RowKey, "", new Dictionary <string, EntityProperty>())); sequence.Entity.Properties.Add(PropertyFinalCachedId, new EntityProperty(schema.Entity.SeedValue)); SequenceTableProxy.Insert(sequence.Entity); //Insert would throw if another thread created after SequenceTableProxy.Get was called above. Only one thread can create the sequence. // - a lock would not help, since the other thread may be in another role (web or worker) instance return(sequence); }
/// <summary> /// Saves the specified sequence schema, overwriting the existing schema if applicable. /// The schema does not contain the current latest sequence value. /// </summary> /// <param name="sequence"></param> public void InsertOrUpdate(SequenceIdSchema sequence) { SchemaTableProxy.InsertOrUpdate(sequence); }