Esempio n. 1
0
 public IEnumerable <ITableEntity> Create(long partitionKey, string id, ObjectWithMetadata <TMain> model, Func <object, string> keyParser)
 {
     if (_keyMappings.Any())
     {
         var search = _createFunc(model.Data, model.Metadata);
         if (search != null)
         {
             var rowKeys = FindAllKeys(0, id, model.Data, keyParser, new List <string>());
             foreach (var key in rowKeys)
             {
                 search = _createFunc(model.Data, model.Metadata); // Create a new model per item
                 if (_finalMapAction != null)
                 {
                     _finalMapAction(model.Data, search, key.Item2);
                 }
                 yield return(new TableEntity <TSearch>(partitionKey.ToString(CultureInfo.InvariantCulture), key.Item1, search));
             }
         }
     }
     else
     {
         var search = _createFunc(model.Data, model.Metadata);
         if (search != null)
         {
             if (_finalMapAction != null)
             {
                 _finalMapAction(model.Data, search, null);
             }
             yield return(new TableEntity <TSearch>(partitionKey.ToString(CultureInfo.InvariantCulture), GetRowKey(id, null, null), search));
         }
     }
 }
Esempio n. 2
0
        public async Task <ObjectPartitionWriteResult <T> > Save(T data, long id, UpdateAuditInfo audit, Metadata metadata = null)
        {
            await Initialise();

            var enc    = await _encryptor.Value;
            var obj    = new ObjectWithMetadata <T>(data, metadata);
            var result = await _store.SaveObject(GetLocation(id), obj, audit, enc, _options);

            return(new ObjectPartitionWriteResult <T>
            {
                Id = id,
                Data = new ObjectWithMetadata <T>(data, result)
            });
        }
Esempio n. 3
0
 public IEnumerable <ITableEntity> Create(long partitionKey, string id, ObjectWithMetadata <TMain> model, Func <object, string> keyParser)
 {
     if (_keyMappings.Any())
     {
         if (_createPredicate(model.Data))
         {
             var rowKeys = FindAllKeys(0, model.Data, keyParser, new List <string>());
             var partKey = partitionKey.ToString(CultureInfo.InvariantCulture);
             foreach (var key in rowKeys)
             {
                 yield return(new TableEntity <object>(partKey, key, null));
             }
         }
     }
 }
Esempio n. 4
0
        public async Task <ObjectPartitionWriteResult <T> > Save(T data, Expression <Func <T, long?> > idField, UpdateAuditInfo audit, Func <long, Task> preSaveProcessing = null, Metadata metadata = null)
        {
            await Initialise().ConfigureAwait(false);

            var member = idField.Body as MemberExpression;

            if (member == null)
            {
                throw new ArgumentException(string.Format("Expression '{0}' refers to a method, not a property.", idField.ToString()));
            }

            var propInfo = member.Member as PropertyInfo;

            if (propInfo == null)
            {
                throw new ArgumentException(string.Format("Expression '{0}' refers to a field, not a property.", idField.ToString()));
            }

            var id = (long?)propInfo.GetValue(data);

            if (!id.HasValue)
            {
                id = await _idGenerator.Value.NextId().ConfigureAwait(false);

                propInfo.SetValue(data, id);
            }

            if (preSaveProcessing != null)
            {
                await preSaveProcessing(id.Value).ConfigureAwait(false);
            }

            var enc = await _encryptor.Value.ConfigureAwait(false);

            var obj    = new ObjectWithMetadata <T>(data, metadata);
            var result = await _store.SaveObject(GetLocation(id.Value), obj, audit, enc, _options).ConfigureAwait(false);

            return(new ObjectPartitionWriteResult <T>
            {
                Id = id.Value,
                Data = new ObjectWithMetadata <T>(data, result)
            });
        }
        public Task Delete(long partitionKey, string id, ObjectWithMetadata <TMain> main)
        {
            var context = _client.Context(_tableName, null);

            var entity = main as ITableEntity;

            if (entity != null)
            {
                context.Delete(entity);
            }

            // Remove all related entities
            foreach (var mapping in _mappings)
            {
                var items = mapping.Create(partitionKey, id, main, KeyParser).ToList();
                foreach (var item in items)
                {
                    context.Delete(item);
                }
            }

            return(context.Save());
        }
        public async Task Save(long partitionKey, string id, ObjectWithMetadata <TMain> item, ObjectWithMetadata <TMain> previous, IEncryptor encryptor)
        {
            var context = _client.Context(_tableName, encryptor);

            var rowKeys = new List <string>();

            foreach (var mapping in _mappings)
            {
                var oldItems = new List <ITableEntity>();
                if (previous != null)
                {
                    oldItems = mapping.Create(partitionKey, id, previous, KeyParser).ToList();
                }

                var newItems = mapping.Create(partitionKey, id, item, KeyParser).ToList();

                // Remove any old items that are not in the new items collection
                foreach (var oldItem in oldItems.Where(o => !newItems.Any(n => n.RowKey == o.RowKey)))
                {
                    context.Delete(oldItem);
                    rowKeys.Add(oldItem.RowKey);
                }

                // If we have any new items lets update/create them
                foreach (var newItem in newItems)
                {
                    var matchingOld = oldItems.FirstOrDefault(o => o.RowKey == newItem.RowKey);

                    if (matchingOld != null && mapping.AdditionalActions.Any())
                    {
                        // Get the real old item to map from
                        var actualOldItem = await _client.Query <TSearch>(_tableName, encryptor).ById(matchingOld.PartitionKey, matchingOld.RowKey);

                        var actualOldItemEntity = new TableEntity <TSearch>(matchingOld.PartitionKey, matchingOld.RowKey, actualOldItem);

                        foreach (var action in mapping.AdditionalActions)
                        {
                            action(newItem, actualOldItemEntity);
                        }
                    }

                    // Be specific about which update we want...
                    rowKeys.Add(newItem.RowKey);
                    if (mapping.IsStrict && matchingOld == null)
                    {
                        // If there is no old item for this set, then expect this to be actually a new item
                        context.Insert(newItem);
                    }
                    else
                    {
                        context.InsertOrReplace(newItem);
                    }
                }
            }

            try
            {
                await context.Save();
            }
            catch (StorageEntityAlreadyExistsException e)
            {
                var    failedItem = e.Message.Split(':')[0];
                int    failedEntity;
                string rowKey = string.Empty;
                if (int.TryParse(failedItem, out failedEntity) && failedEntity < rowKeys.Count)
                {
                    rowKey = rowKeys[failedEntity];
                }
                throw new CompositionException(rowKey, "Entity already exists", e);
            }
        }
Esempio n. 7
0
 public Task Delete(long id, ObjectWithMetadata <TMain> current)
 {
     return(Delete(id.ToString(CultureInfo.InvariantCulture), current));
 }
Esempio n. 8
0
 public Task Delete(string id, ObjectWithMetadata <TMain> current)
 {
     return(_composition.Delete(_partitionId, id, current));
 }
Esempio n. 9
0
 public Task Save(long id, ObjectWithMetadata <TMain> item, ObjectWithMetadata <TMain> previous)
 {
     return(Save(id.ToString(CultureInfo.InvariantCulture), item, previous));
 }
Esempio n. 10
0
 public async Task Save(string id, ObjectWithMetadata <TMain> item, ObjectWithMetadata <TMain> previous)
 {
     var encryptor = await _encryptor.Value;
     await _composition.Save(_partitionId, id, item, previous, encryptor);
 }