Ejemplo n.º 1
0
        public static string UpsertStat(DataConfig providerConfig, POCO.Stat stat)
        {
            switch (providerConfig.ProviderType)
            {
            case "azure.tableservice":
                AzureStat az = new AzureStat(stat);

                CloudTable     table     = Utils.GetCloudTable(providerConfig, AzureTableNames.CPStats);
                TableOperation operation = TableOperation.InsertOrReplace(az);
                Task           tUpdate   = table.ExecuteAsync(operation);
                tUpdate.Wait();

                break;

            case "internal.mongodb":
                IMongoCollection <MongoStatUpsert> collection = Utils.GetMongoCollection <MongoStatUpsert>(providerConfig, MongoTableNames.CPStats);
                MongoStatUpsert mongoObject = Utils.ConvertType <MongoStatUpsert>(stat);

                // Create the upsert filter
                List <DataFactory.Filter> filters  = new List <DataFactory.Filter>();
                DataFactory.Filter        pkFilter = new DataFactory.Filter("PartitionKey", Utils.CleanTableKey(stat.PartitionKey), "eq");
                DataFactory.Filter        rkFilter = new DataFactory.Filter("RowKey", Utils.CleanTableKey(stat.RowKey), "eq");
                filters.Add(pkFilter);
                filters.Add(rkFilter);
                FilterDefinition <MongoStatUpsert> filter = Utils.GenerateMongoFilter <MongoStatUpsert>(filters);

                // Create the upsert options
                MongoDB.Driver.ReplaceOptions options = new ReplaceOptions();
                options.IsUpsert = true;

                // Upsert
                collection.ReplaceOne(filter, mongoObject, options);
                return(string.Empty);

            default:
                throw new ApplicationException("Data provider not recognised: " + providerConfig.ProviderType);
            }

            //TODO return id of new object if supported
            return(string.Empty);
        }
Ejemplo n.º 2
0
        public static bool AddFileTypeStats(DataConfig providerConfig, List <POCO.Stat> fileTypeStats)
        {
            switch (providerConfig.ProviderType)
            {
            case "azure.tableservice":

                List <TableOperation> ops = new List <TableOperation>();

                CloudTable table = Utils.GetCloudTable(providerConfig, AzureTableNames.FileTypeStats);
                foreach (POCO.Stat stat in fileTypeStats)
                {
                    AzureStat      az        = new AzureStat(stat);
                    TableOperation operation = TableOperation.InsertOrReplace(az);
                    ops.Add(operation);
                }

                Utils.AzureBatchExecute(table, ops);

                break;

            case "internal.mongodb":

                IMongoCollection <MongoStatUpsert> collection = Utils.GetMongoCollection <MongoStatUpsert>(providerConfig, MongoTableNames.FileTypeStats);

                var operationList = new List <WriteModel <MongoStatUpsert> >();
                foreach (POCO.Stat stat in fileTypeStats)
                {
                    // Convert to mongo-compatible object
                    MongoStatUpsert mongoObject = Utils.ConvertType <MongoStatUpsert>(stat);

                    // Create the filter for the upsert
                    FilterDefinition <MongoStatUpsert> filter = Builders <MongoStatUpsert> .Filter.Eq(x => x.PartitionKey, stat.PartitionKey) &
                                                                Builders <MongoStatUpsert> .Filter.Eq(x => x.RowKey, stat.RowKey);

                    UpdateDefinition <MongoStatUpsert> updateDefinition = new UpdateDefinitionBuilder <MongoStatUpsert>().Unset("______");   // HACK: I found no other way to create an empty update definition

                    updateDefinition = updateDefinition.SetOnInsert("PartitionKey", mongoObject.PartitionKey)
                                       .SetOnInsert("RowKey", mongoObject.RowKey)
                                       .Set("StatsType", mongoObject.StatsType)
                                       .Set("JsonStats", mongoObject.JsonStats);

                    UpdateOneModel <MongoStatUpsert> update = new UpdateOneModel <MongoStatUpsert>(filter, updateDefinition)
                    {
                        IsUpsert = true
                    };
                    operationList.Add(update);
                    operationList.Add(update);
                }

                if (operationList.Count > 0)
                {
                    collection.BulkWrite(operationList);
                }

                return(true);

            default:
                throw new ApplicationException("Data provider not recognised: " + providerConfig.ProviderType);
            }

            //TODO return id of new object if supported
            return(true);
        }