Ejemplo n.º 1
0
            static async Task <World[]> LoadUncachedQueries(ConcurrentRandom random, int id, int i, int count, RawDb rawdb, World[] result)
            {
                using (var db = new NpgsqlConnection(rawdb._connectionString))
                {
                    await db.OpenAsync();

                    var(cmd, idParameter) = rawdb.CreateReadCommand(db, random);

                    using (cmd)
                    {
                        Func <ICacheEntry, Task <CachedWorld> > create = async(entry) =>
                        {
                            return(await rawdb.ReadSingleRow(cmd));
                        };

                        var cacheKeys = _cacheKeys;
                        var key       = cacheKeys[id];

                        idParameter.TypedValue = id;

                        for (; i < result.Length; i++)
                        {
                            var data = await rawdb._cache.GetOrCreateAsync <CachedWorld>(key, create);

                            result[i] = data;

                            id = random.Next(1, 10001);
                            idParameter.TypedValue = id;
                            key = cacheKeys[id];
                        }
                    }
                }

                return(result);
            }
Ejemplo n.º 2
0
        public async Task <World[]> LoadMultipleUpdatesRows(int count)
        {
            var random  = new ConcurrentRandom();
            var results = new World[count];

            using (var db = new NpgsqlConnection(_connectionString))
            {
                await db.OpenAsync();

                var(queryCmd, queryParameter) = CreateReadCommand(db, random);

                using (queryCmd)
                {
                    for (int i = 0; i < results.Length; i++)
                    {
                        results[i] = await ReadSingleRow(queryCmd);

                        queryParameter.TypedValue = random.Next(1, 10001);
                    }
                }

                using (var updateCmd = new NpgsqlCommand(BatchUpdateString.Query(count), db))
                {
                    var ids     = BatchUpdateString.Ids;
                    var randoms = BatchUpdateString.Randoms;

                    for (int i = 0; i < results.Length; i++)
                    {
                        var randomNumber = random.Next(1, 10001);

                        updateCmd.Parameters.Add(new NpgsqlParameter <int>(parameterName: ids[i], value: results[i].id));
                        updateCmd.Parameters.Add(new NpgsqlParameter <int>(parameterName: randoms[i], value: randomNumber));

                        results[i].randomNumber = randomNumber;
                    }

                    await updateCmd.ExecuteNonQueryAsync();
                }
            }

            return(results);
        }
Ejemplo n.º 3
0
        public async Task <World[]> LoadMultipleQueriesRows(int count)
        {
            var random = new ConcurrentRandom();
            var result = new World[count];

            using (var db = new NpgsqlConnection(_connectionString))
            {
                await db.OpenAsync();

                var(cmd, parameter) = CreateReadCommand(db, random);

                using (cmd)
                {
                    for (int i = 0; i < count; i++)
                    {
                        result[i] = await ReadSingleRow(cmd);

                        parameter.Value = random.Next(1, 10001);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        public Task <World[]> LoadCachedQueries(int count)
        {
            var result    = new World[count];
            var cacheKeys = _cacheKeys;
            var cache     = _cache;
            var random    = new ConcurrentRandom();

            for (var i = 0; i < result.Length; i++)
            {
                var id   = random.Next(1, 10001);
                var key  = cacheKeys[id];
                var data = cache.Get <CachedWorld>(key);

                if (data != null)
                {
                    result[i] = data;
                }
                else
                {
                    return(LoadUncachedQueries(random, id, i, count, this, result));
                }
            }

            return(Task.FromResult(result));
Ejemplo n.º 5
0
 public RawDb(ConcurrentRandom random, DbProviderFactory dbProviderFactory, IOptions <AppSettings> appSettings)
 {
     _random            = random;
     _dbProviderFactory = dbProviderFactory;
     _connectionString  = appSettings.Value.ConnectionString;
 }
Ejemplo n.º 6
0
 public EfDb(ConcurrentRandom random, ApplicationDbContext dbContext, IOptions <AppSettings> appSettings)
 {
     _random         = random;
     _dbContext      = dbContext;
     _useBatchUpdate = appSettings.Value.Database != DatabaseServer.PostgreSql;
 }