Beispiel #1
0
 public DbUpdateConfig(XmlDocument xmlConfig)
 {
     ProviderType = xmlConfig.GetValue(ToolConfigNames.Provider);
       ConnectionString = xmlConfig.GetValue(ToolConfigNames.ConnectionString);
       Driver = ToolHelper.CreateDriver(ProviderType, ConnectionString);
       ModelUpdateOptions = ReflectionHelper.ParseEnum<DbUpgradeOptions>(xmlConfig.GetValue(ToolConfigNames.ModelUpdateOptions));
       DbOptions = ReflectionHelper.ParseEnum<DbOptions>(xmlConfig.GetValue(ToolConfigNames.DbOptions));
       AssemblyPath = xmlConfig.GetValue(ToolConfigNames.AssemblyPath);
       AppClassName = xmlConfig.GetValue(ToolConfigNames.AppClassName);
       OutputPath = xmlConfig.GetValue(ToolConfigNames.OutputPath);
 }
Beispiel #2
0
 public DbSettings(DbDriver driver, DbOptions options, 
     string connectionString,
     string schemaManagementConnectionString = null,
     DbUpgradeMode upgradeMode = DbUpgradeMode.NonProductionOnly,
     DbUpgradeOptions upgradeOptions = DbUpgradeOptions.Default,
     IDbInfoService dbInfoProvider = null,
     string dataSourceName = null,
     DbNamingPolicy namingPolicy = null)
     : this(new DbModelConfig(driver, options, namingPolicy), connectionString, schemaManagementConnectionString, 
       upgradeMode, upgradeOptions, dbInfoProvider, dataSourceName)
 {
 }
        public DbOptions GetDbOptions()
        {
            DbOptions options = new DbOptions()
                                .SetCreateIfMissing()
                                .SetCreateMissingColumnFamilies();

            if (this.env.Is32BitProcess || !this.optimizeForPerformance)
            {
                // restrict some sizes if 32 bit OS.
                options.SetWriteBufferSize(WriteBufferSize);
                options.SetTargetFileSizeBase(TargetFileSizeBase);
                options.SetMaxBytesForLevelBase(MaxBytesForLevelBase);
                options.SetSoftPendingCompactionBytesLimit(SoftPendingCompactionBytes);
                options.SetHardPendingCompactionBytesLimit(HardPendingCompactionBytes);
            }

            return(options);
        }
Beispiel #4
0
        public DbModelConfig(DbDriver driver, DbOptions options = DbOptions.Default, 
            DbNamingPolicy namingPolicy = null, IDictionary<string, string> schemaMappings = null)
        {
            Driver = driver;
              Options = options;
              NamingPolicy = namingPolicy ?? new DbNamingPolicy();
              //import schema mappings
              if (schemaMappings != null)
            foreach (var de in schemaMappings)
              SchemaMappings[de.Key] = de.Value;

              //Verify options
              if (!Driver.Supports(DbFeatures.StoredProcedures))
            Options &= ~DbOptions.UseStoredProcs;
              //Batch mode is not available without stored procedures
              if (!Options.IsSet(DbOptions.UseStoredProcs))
            Options &= ~DbOptions.UseBatchMode;
        }
Beispiel #5
0
        public SqlServerDbContextOptions(DbOptions dbOptions, DbModuleOptions options, ILoggerFactory loggerFactory, ILoginInfo loginInfo) : base(dbOptions, options, new SqlServerAdapter(dbOptions, options), loggerFactory, loginInfo)
        {
            Check.NotNull(dbOptions.Server, nameof(dbOptions.Server), "数据库服务器地址不能为空");
            Check.NotNull(dbOptions.UserId, nameof(dbOptions.UserId), "数据库用户名不能为空");
            Check.NotNull(dbOptions.Password, nameof(dbOptions.Password), "数据库密码不能为空");

            options.Version = DbOptions.Version;
            var connStrBuilder = new SqlConnectionStringBuilder
            {
                DataSource = DbOptions.Port > 0 ? DbOptions.Server + "," + DbOptions.Port : DbOptions.Server,
                UserID     = DbOptions.UserId,
                Password   = DbOptions.Password,
                MultipleActiveResultSets = true,
                InitialCatalog           = DbModuleOptions.Database
            };

            options.ConnectionString = connStrBuilder.ToString();
        }
Beispiel #6
0
        public UnitOfWork(DbOptions opt)
        {
            DbContextOptions <CustomerAppContext> options;

            if (opt.Environment == "Development" && String.IsNullOrEmpty(opt.ConnectionString))
            {
                options = new DbContextOptionsBuilder <CustomerAppContext>()
                          .UseInMemoryDatabase("TheDB")
                          .Options;
            }
            else
            {
                options = new DbContextOptionsBuilder <CustomerAppContext>()
                          .UseSqlServer(opt.ConnectionString)
                          .Options;
            }

            context = new CustomerAppContext(options);
        }
        public void Load()
        {
            var options = new DbOptions().SetCreateIfMissing().SetErrorIfExists();
            var guid    = Guid.NewGuid();

            using (var db = RocksDb.Open(
                       options,
                       _temporaryDirectory))
            {
                db.Put("foo", "bar");
                db.Put("guid", guid.ToString());
            }

            using (var db = _rocksDbService.Load(_temporaryDirectory))
            {
                Assert.Equal("bar", db.Get("foo"));
                Assert.Equal(guid.ToString(), db.Get("guid"));
            }
        }
        public RocksDbCoindb(
            Network network,
            DataFolder dataFolder,
            IDateTimeProvider dateTimeProvider,
            INodeStats nodeStats,
            DBreezeSerializer dBreezeSerializer)
        {
            this.dbOptions          = new DbOptions().SetCreateIfMissing(true);
            this.rocksDb            = RocksDb.Open(this.dbOptions, dataFolder.CoindbPath);
            this.dBreezeSerializer  = dBreezeSerializer;
            this.logger             = LogManager.GetCurrentClassLogger();
            this.network            = network;
            this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider);

            if (nodeStats.DisplayBenchStats)
            {
                nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, this.GetType().Name, 400);
            }
        }
        public override IReadOnlyCollection <ConfigurationValidationError> Validate(string?prefix = null)
        {
            var errors = new ConfigurationValidationErrorCollection(prefix);

            errors.AddErrors(base.Validate(prefix));
            if (ReverseProxy != null)
            {
                errors.AddErrors(ReverseProxy.Validate(nameof(ReverseProxy)));
            }

            errors.AddErrors(ExternalUrls.Validate(nameof(ExternalUrls)));
            errors.AddErrors(DbOptions.Validate(nameof(DbOptions)));
            errors.AddErrors(MigrationOptions.Validate(nameof(MigrationOptions)));
            errors.AddErrors(AuthOptions.Validate(nameof(AuthOptions)));
            errors.AddErrors(SmtpEMailOptions.Validate(nameof(SmtpEMailOptions)));
            errors.AddErrors(TempFiles.Validate(nameof(TempFiles)));

            return(errors);
        }
        public UnitOfWork(DbOptions opt)
        {
            if (opt.Environment == "Development" && String.IsNullOrEmpty(opt.ConnectionString))
            {
                optionsStatic = new DbContextOptionsBuilder <EASVContext>()
                                .UseInMemoryDatabase("TheDB")
                                .Options;
                context = new EASVContext(optionsStatic);
            }
            else
            {
                var options = new DbContextOptionsBuilder <EASVContext>()
                              .UseSqlServer(opt.ConnectionString)
                              .Options;
                context = new EASVContext(options);
            }

            //CustomerRepository = new CustomerRepository(context);
        }
Beispiel #11
0
        static void Test9()
        {
            var dbop = new DbOptions()
                       .SetCreateIfMissing(true)
                       .SetCreateMissingColumnFamilies(true);

            var columnFamilies = new ColumnFamilies
            {
                { "reverse1", new ColumnFamilyOptions() },
                { "reverse2", new ColumnFamilyOptions() },
            };
            var db      = TransactionDB.Open(dbop, new TransactionDBOptions(), "transaction_db_test5", columnFamilies);
            var reverse = db.GetColumnFamily("reverse1");
            var wop     = new WriteOptions();

            for (int i = 0; i < 10; i += 2)
            {
                db.Put(i.ToString(), i.ToString(), reverse);
            }
            Console.WriteLine("begin");
            var t1 = Task.Run(async() =>
            {
                var it = db.NewIterator(reverse);
                it.SeekToFirst();
                while (it.Valid())
                {
                    Console.WriteLine("it:key=" + it.StringKey() + ",value" + it.StringValue());
                    await Task.Delay(500);
                    it.Next();
                }
            });
            var t2 = Task.Run(async() =>
            {
                for (int i = 1; i < 10; i += 2)
                {
                    await Task.Delay(500);
                    db.Remove(i.ToString());
                    Console.WriteLine("write key=" + i);
                }
            });

            Task.WaitAll(t1, t2);
        }
        public RocksDbStore(string path)
        {
            var options = new DbOptions()
                .SetCreateIfMissing(true)
                .SetCreateMissingColumnFamilies(true);

            db = RocksDb.Open(options, path, ColumnFamilies);

            var writeBatch = new WriteBatch();
            var readOptions = new ReadOptions().SetFillCache(true);
            using (Iterator it = db.NewIterator(readOptions: readOptions))
            {
                for (it.SeekToFirst(); it.Valid(); it.Next())
                {
                    writeBatch.Delete(it.Key());
                }
            }
            db.Write(writeBatch);
        }
Beispiel #13
0
        private DbOptions BuildOptions(IDbConfig dbConfig)
        {
            BlockBasedTableOptions tableOptions = new BlockBasedTableOptions();

            tableOptions.SetBlockSize(16 * 1024);
            tableOptions.SetPinL0FilterAndIndexBlocksInCache(true);
            tableOptions.SetCacheIndexAndFilterBlocks(dbConfig.CacheIndexAndFilterBlocks);

            tableOptions.SetFilterPolicy(BloomFilterPolicy.Create(10, true));
            tableOptions.SetFormatVersion(2);

            ulong  blockCacheSize = dbConfig.BlockCacheSize;
            IntPtr cache          = Native.Instance.rocksdb_cache_create_lru(new UIntPtr(blockCacheSize));

            tableOptions.SetBlockCache(cache);

            DbOptions options = new DbOptions();

            options.SetCreateIfMissing(true);

            options.OptimizeForPointLookup(blockCacheSize); // I guess this should be the one option controlled by the DB size property - bind it to LRU cache size
            //options.SetCompression(CompressionTypeEnum.rocksdb_snappy_compression);
            //options.SetLevelCompactionDynamicLevelBytes(true);

            /*
             * Multi-Threaded Compactions
             * Compactions are needed to remove multiple copies of the same key that may occur if an application overwrites an existing key. Compactions also process deletions of keys. Compactions may occur in multiple threads if configured appropriately.
             * The entire database is stored in a set of sstfiles. When a memtable is full, its content is written out to a file in Level-0 (L0). RocksDB removes duplicate and overwritten keys in the memtable when it is flushed to a file in L0. Some files are periodically read in and merged to form larger files - this is called compaction.
             * The overall write throughput of an LSM database directly depends on the speed at which compactions can occur, especially when the data is stored in fast storage like SSD or RAM. RocksDB may be configured to issue concurrent compaction requests from multiple threads. It is observed that sustained write rates may increase by as much as a factor of 10 with multi-threaded compaction when the database is on SSDs, as compared to single-threaded compactions.
             * TKS: Observed 500MB/s compared to ~100MB/s between multithreaded and single thread compactions on my machine (processor count is returning 12 for 6 cores with hyperthreading)
             * TKS: CPU goes to insane 30% usage on idle - compacting only app
             */
            options.SetMaxBackgroundCompactions(Environment.ProcessorCount);

            //options.SetMaxOpenFiles(32);
            options.SetWriteBufferSize(dbConfig.WriteBufferSize);
            options.SetMaxWriteBufferNumber((int)dbConfig.WriteBufferNumber);
            options.SetMinWriteBufferNumberToMerge(2);
            options.SetBlockBasedTableFactory(tableOptions);
            options.IncreaseParallelism(Environment.ProcessorCount);
//            options.SetLevelCompactionDynamicLevelBytes(true); // only switch on on empty DBs
            return(options);
        }
Beispiel #14
0
        public static RocksDbWrapper Create(IRocksDbOptionsProvider optionsProvider, string path, IEnumerable <string> partitionsList)
        {
            Preconditions.CheckNonWhiteSpace(path, nameof(path));
            Preconditions.CheckNotNull(optionsProvider, nameof(optionsProvider));
            DbOptions            dbOptions = Preconditions.CheckNotNull(optionsProvider.GetDbOptions());
            IEnumerable <string> existingColumnFamilies = ListColumnFamilies(dbOptions, path);
            IEnumerable <string> columnFamiliesList     = existingColumnFamilies.Union(Preconditions.CheckNotNull(partitionsList, nameof(partitionsList)), StringComparer.OrdinalIgnoreCase).ToList();
            var columnFamilies = new ColumnFamilies();

            foreach (string columnFamilyName in columnFamiliesList)
            {
                columnFamilies.Add(columnFamilyName, optionsProvider.GetColumnFamilyOptions());
            }

            RocksDb db             = RocksDb.Open(dbOptions, path, columnFamilies);
            var     rocksDbWrapper = new RocksDbWrapper(dbOptions, db, path);

            return(rocksDbWrapper);
        }
Beispiel #15
0
        /// <summary>
        /// 获取Quartz属性
        /// </summary>
        /// <returns></returns>
        private NameValueCollection GetQuartzProps(QuartzOptions options, DbOptions dbOptions)
        {
            var quartzProps     = new NameValueCollection();
            var quartzDbOptions = dbOptions.Modules.FirstOrDefault(m => m.Name.EqualsIgnoreCase("quartz"));

            if (quartzDbOptions != null)
            {
                quartzProps["quartz.scheduler.instanceName"]              = options.InstanceName;
                quartzProps["quartz.jobStore.type"]                       = "Quartz.Impl.AdoJobStore.JobStoreTX,Quartz";
                quartzProps["quartz.jobStore.driverDelegateType"]         = "Quartz.Impl.AdoJobStore.StdAdoDelegate,Quartz";
                quartzProps["quartz.jobStore.tablePrefix"]                = options.TablePrefix;
                quartzProps["quartz.jobStore.dataSource"]                 = "default";
                quartzProps["quartz.dataSource.default.connectionString"] = quartzDbOptions.ConnectionString;
                quartzProps["quartz.dataSource.default.provider"]         = GetProvider(dbOptions.Dialect);
                quartzProps["quartz.serializer.type"]                     = options.SerializerType;
            }

            return(quartzProps);
        }
Beispiel #16
0
        public void RemoveProvider(string obj, string provider = null, string hashId = null)
        {
            var sql = new SqlDelete("account_links").Where("id", obj);

            if (!string.IsNullOrEmpty(provider))
            {
                sql.Where("provider", provider);
            }
            if (!string.IsNullOrEmpty(hashId))
            {
                sql.Where("uid", hashId);
            }

            var db = DbOptions.Get(DbId);

            db.ExecuteScalar <int>(sql);

            AccountLinkerStorage.RemoveFromCache(obj);
        }
Beispiel #17
0
        public string Lookup(string MAK, string folderName)
        {
            string tempPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            string[] pathSplit = tempPath.Split('\\');
            string   newpath   = "";

            for (int i = 0; i < pathSplit.Length - 3; i++)
            {
                newpath += pathSplit[i] + "\\";
            }
            string path    = newpath + '\\' + folderName + '\\';
            var    options = new DbOptions();

            using (var db = RocksDb.Open(options, path))
            {
                return(db.Get(MAK));
            }
        }
Beispiel #18
0
        static void Test8()
        {
            var dbop = new DbOptions()
                       .SetCreateIfMissing(true)
                       .SetCreateMissingColumnFamilies(true);

            var columnFamilies = new ColumnFamilies
            {
                { "reverse1", new ColumnFamilyOptions() },
                { "reverse2", new ColumnFamilyOptions() },
            };
            var    db      = TransactionDB.Open(dbop, new TransactionDBOptions(), "transaction_db_test2", columnFamilies);
            var    reverse = db.GetColumnFamily("reverse1");
            var    wop     = new WriteOptions();
            var    txnOp   = new TransactionOptions();
            string key     = "1112";
            var    t1      = Task.Run(async() =>
            {
                var txn = new Transaction(db, wop, txnOp);
                await Task.Delay(700);
                Console.WriteLine("11110:" + txn.Get(key, reverse));
                txn.Put(key, "3002", reverse);
                Console.WriteLine("11111:" + txn.Get(key, reverse));
                await Task.Delay(1000);
                Console.WriteLine("11112:" + txn.Get(key, reverse));
                txn.Put(key, "3002", reverse);
                Console.WriteLine("11113:" + txn.Get(key, reverse) + ",thread=" + Thread.CurrentThread.ManagedThreadId);
                txn.Commit();
            });
            var t2 = Task.Run(async() =>
            {
                await Task.Delay(100);
                db.Put(key, "44444", reverse);
                Console.WriteLine("write1:" + db.Get(key, reverse));
                await Task.Delay(1000);
                db.Put(key, "444442", reverse);
                Console.WriteLine("write2:" + db.Get(key, reverse));
            });

            Task.WaitAll(t1, t2);
            Console.WriteLine("end:" + db.Get("11", reverse));
        }
Beispiel #19
0
 public DbModelConfig(DbDriver driver, DbOptions options, IDbNamingPolicy namingPolicy = null,
                      IDictionary <string, string> schemaMappings = null)
 {
     Util.Check(driver != null, "Driver parameter may not be null.");
     Util.Check(driver.TypeRegistry != null,
                "DbDriver.TypRegistry is null, DbDriver-derived class must create driver-specific instance.");
     Util.Check(driver.SqlDialect != null,
                "DbDriver.SqlDialect is null, DbDriver-derived class must create driver-specific instance.");
     Driver       = driver;
     Options      = options;
     NamingPolicy = namingPolicy;
     //import schema mappings
     if (schemaMappings != null)
     {
         foreach (var de in schemaMappings)
         {
             SchemaMappings[de.Key] = de.Value;
         }
     }
 }
Beispiel #20
0
        public static IServiceCollection AddDapperCoreSqlServer(this IServiceCollection services)
        {
            services.AddServiceByIntefaceInCurrentDomain(typeof(IRepositoryAsync <>));
            services.AddServiceByIntefaceInCurrentDomain(typeof(IQueryRepository <>));

            var svcProvider = services.BuildServiceProvider();
            var config      = svcProvider.GetRequiredService <IConfiguration>();

            var options = new DbOptions();

            config.Bind(SectionName, options);
            services.AddSingleton(options);

            if (options.Enabled)
            {
                services.AddSingleton <ISqlConnectionFactory, SqlConnectionFactory>();
                services.AddScoped <IUnitOfWork, DapperUnitOfWork>();
            }
            return(services);
        }
        public void Check_AddUser()
        {
            // ARRANGE
            using IPizzaboxRepository pizzaboxRepository = DbOptions.CreatePizzaboxRepository();
            User U1 = new User
            {
                Username    = "******",
                Pass        = "******",
                FullName    = "Yah Who",
                SessionLive = 0
            };
            User Expected = U1;

            // ACT
            pizzaboxRepository.AddUser(U1);
            User Actual = pizzaboxRepository.GetUserById("whaa");

            // ASSERT
            Assert.Equal(Expected, Actual);
        }
        public void Check_UpdateUser()
        {
            // ARRANGE
            using IPizzaboxRepository pizzaboxRepository = DbOptions.CreatePizzaboxRepository();
            User U1 = new User()
            {
                Username    = "******",
                Pass        = "******",
                FullName    = "That Way",
                SessionLive = 1
            };
            User Expected = U1;

            // ACT
            pizzaboxRepository.UpdateUser(U1);
            User Actual = pizzaboxRepository.GetUserById("Uname23");

            // ASSERT
            Assert.Equal(Expected, Actual);
        }
Beispiel #23
0
        public RocksDbCoindb(Network network, string folder, IDateTimeProvider dateTimeProvider,
                             ILoggerFactory loggerFactory, INodeStats nodeStats, DataStoreSerializer dataStoreSerializer)
        {
            Guard.NotNull(network, nameof(network));
            Guard.NotEmpty(folder, nameof(folder));

            this.dataStoreSerializer = dataStoreSerializer;

            this.logger = loggerFactory.CreateLogger(this.GetType().FullName);

            // Open a connection to a new DB and create if not found
            var options = new DbOptions().SetCreateIfMissing(true);

            this.rocksdb = RocksDb.Open(options, folder);

            this.network            = network;
            this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider);

            nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, this.GetType().Name, 400);
        }
Beispiel #24
0
        /// <summary>
        /// Initializes a new instance of the object.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - RegTest/TestNet/MainNet.</param>
        /// <param name="folder"><see cref="ProvenBlockHeaderRepository"/> folder path to the DBreeze database files.</param>
        /// <param name="loggerFactory">Factory to create a logger for this type.</param>
        /// <param name="dataStoreSerializer">The serializer to use for <see cref="IBitcoinSerializable"/> objects.</param>
        public ProvenBlockHeaderRepository(Network network, string folder, ILoggerFactory loggerFactory,
                                           DataStoreSerializer dataStoreSerializer)
        {
            Guard.NotNull(network, nameof(network));
            Guard.NotNull(folder, nameof(folder));
            this.dataStoreSerializer = dataStoreSerializer;

            this.logger = loggerFactory.CreateLogger(this.GetType().FullName);

            Directory.CreateDirectory(folder);

            // Open a connection to a new DB and create if not found
            var options = new DbOptions().SetCreateIfMissing(true);

            this.rocksdb = RocksDb.Open(options, folder);

            this.locker = new object();

            this.network = network;
        }
Beispiel #25
0
        public void CopyTo(string path)
        {
            lock (_replicate_lock)
            {
                if (Directory.Exists(path))
                {
                    throw new LinqDbException("Linqdb: replicate folder already exists: " + path);
                }

                Directory.CreateDirectory(path);

                var options = new DbOptions().SetCreateIfMissing(true);

                using (var new_leveld_db = RocksDb.Open(options, Path.Combine(path, "data")))
                    using (var new_struct_db = RocksDb.Open(options, Path.Combine(path, "struct")))
                        using (var data_snapshot = leveld_db.CreateSnapshot())
                            using (var struct_snapshot = struct_db.CreateSnapshot())
                            {
                                var ro = new ReadOptions().SetSnapshot(data_snapshot);
                                using (var it = leveld_db.NewIterator(null, ro))
                                {
                                    it.SeekToFirst();
                                    while (it.Valid())
                                    {
                                        new_leveld_db.Put(it.Key(), it.Value());
                                        it.Next();
                                    }
                                }
                                var struct_ro = new ReadOptions().SetSnapshot(struct_snapshot);
                                using (var it = struct_db.NewIterator(null, struct_ro))
                                {
                                    it.SeekToFirst();
                                    while (it.Valid())
                                    {
                                        new_struct_db.Put(it.Key(), it.Value());
                                        it.Next();
                                    }
                                }
                            }
            }
        }
Beispiel #26
0
        private RocksDbStore(string fileName, Logger logger)
        {
            AppDomain.CurrentDomain.ProcessExit += (s, e) => Shutdown();
            this.logger = logger;
            logger.Message("RocksDBStore: " + fileName);
            this.fileName = fileName.Replace("\\", "/");

            var path = Path.GetDirectoryName(fileName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            //TODO check options
            var options = new DbOptions()
                          .SetCreateIfMissing(true)
                          .SetCreateMissingColumnFamilies(true);

            var columnFamilies = new ColumnFamilies
            {
                { "default", new ColumnFamilyOptions().OptimizeForPointLookup(256) },
            };

            try
            {
                var partitionList = RocksDb.ListColumnFamilies(options, path);

                foreach (var partition in partitionList)
                {
                    columnFamilies.Add(partition, new ColumnFamilyOptions());
                }
            }
            catch
            {
                logger.Warning("Inital start, no partitions created yet!");
            }

            logger.Message("Opening database at: " + path);
            _db.Add(fileName, RocksDb.Open(options, path, columnFamilies));
        }
        public static IServiceCollection AddEfCoreSqlServer <TDbContext>(this IServiceCollection services) where TDbContext : DbContext
        {
            services.AddServiceByIntefaceInCurrentDomain(typeof(IRepositoryAsync <>));
            services.AddServiceByIntefaceInCurrentDomain(typeof(IQueryRepository <>));

            var svcProvider = services.BuildServiceProvider();
            var config      = svcProvider.GetRequiredService <IConfiguration>();

            var options = new DbOptions();

            config.Bind(SectionName, options);
            services.AddSingleton(options);
            services.AddScoped <IUnitOfWork, EFUnitOfWork>();
            if (options.Enabled)
            {
                services.AddDbContext <TDbContext>((sp, o) =>
                {
                    string connectionString = options.ConnString;
                    o.UseSqlServer(connectionString,
                                   sqlOptions =>
                    {
                        sqlOptions.EnableRetryOnFailure(
                            15,
                            TimeSpan.FromSeconds(30),
                            null);
                    })
                    .EnableSensitiveDataLogging();
                });
            }
            else
            {
                services.AddDbContext <TDbContext>((sp, o) =>
                {
                    o.UseInMemoryDatabase("DefaultMainDb");
                });
            }

            services.AddScoped <DbContext>(resolver => resolver.GetService <TDbContext>());

            return(services);
        }
Beispiel #28
0
        void v2()
        {
            var options = new DbOptions()
                          .SetCreateIfMissing(true);
            var addTimes = new List <long>();

            using (var db = RocksDb.Open(options, "db1"))
            {
                var elapsedStopwatch = new Stopwatch();
                var addStopwatch     = new Stopwatch();
                elapsedStopwatch.Start();
                var count = 0;
                foreach (var p in PacketStreamUtilities.PacketsFromFileTree(DataDirectory))
                {
                    var len = PacketAccessor.Length(p);
                    if (len < 5)
                    {
                        continue;
                    }
                    var timestamp = PacketAccessor.Time42(p);
                    var apid      = PacketAccessor.APID(p);
                    var key       = new PacketKey {
                        APID = (short)apid, Timestamp = timestamp
                    };
                    var keybuf = KeyToByteArray(key);
                    addStopwatch.Reset();
                    addStopwatch.Start();
                    db.Put(keybuf, p);
                    addStopwatch.Stop();
                    addTimes.Add(addStopwatch.ElapsedTicks);
                    count++;
                }
                elapsedStopwatch.Stop();
                Console.WriteLine($"{count} packets; {count / (elapsedStopwatch.ElapsedMilliseconds / 1000d)} packets/sec");
            }
            addTimes.Sort();
            foreach (var t in addTimes)
            {
                Console.WriteLine((new TimeSpan(t)).TotalSeconds);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dbOptions"></param>
        /// <param name="options"></param>
        /// <param name="sqlAdapter">数据库适配器</param>
        /// <param name="loggerFactory">日志工厂</param>
        /// <param name="loginInfo">登录信息</param>
        protected DbContextOptionsAbstract(DbOptions dbOptions, DbModuleOptions options, ISqlAdapter sqlAdapter, ILoggerFactory loggerFactory, ILoginInfo loginInfo)
        {
            if (options.Name.IsNull())
            {
                throw new ArgumentNullException(nameof(options.Name), "数据库连接名称未配置");
            }

            DbOptions       = dbOptions;
            DbModuleOptions = options;
            SqlAdapter      = sqlAdapter;
            LoggerFactory   = loggerFactory;
            LoginInfo       = loginInfo;

            if (options.EntityTypes != null && options.EntityTypes.Any())
            {
                foreach (var entityType in options.EntityTypes)
                {
                    EntityDescriptorCollection.Add(new EntityDescriptor(dbOptions, options, entityType, sqlAdapter));
                }
            }
        }
Beispiel #30
0
        static void Main(string[] args)
        {
            var options = new DbOptions().SetCreateIfMissing();

            using var db = RocksDbSharp.RocksDb.Open(options, "D:\\LOG\\RocksDbTest");
            var watch = new Stopwatch();

            watch.Start();
            for (int i = 0; i < 10000000; i++)
            {
                db.Put($"{i}", $"{i}");
                var test = db.Get($"{i}");
                //Console.WriteLine(test);
            }

            watch.Stop();

            Console.WriteLine(watch.ElapsedMilliseconds / 1000);

            Console.ReadKey();
        }
Beispiel #31
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dbOptions"></param>
        /// <param name="options"></param>
        /// <param name="loggerFactory"></param>
        /// <param name="loginInfo"></param>
        public MySqlDbContextOptions(DbOptions dbOptions, DbModuleOptions options, ILoggerFactory loggerFactory, ILoginInfo loginInfo) : base(dbOptions, options, new MySqlAdapter(dbOptions, options), loggerFactory, loginInfo)
        {
            Check.NotNull(dbOptions.Server, nameof(dbOptions.Server), "数据库服务器地址不能为空");
            Check.NotNull(dbOptions.UserId, nameof(dbOptions.UserId), "数据库用户名不能为空");
            Check.NotNull(dbOptions.Password, nameof(dbOptions.Password), "数据库密码不能为空");

            options.Version = dbOptions.Version;
            var connStrBuilder = new MySqlConnectionStringBuilder
            {
                Server             = DbOptions.Server,
                Port               = DbOptions.Port > 0 ? (uint)DbOptions.Port : 3306,
                Database           = options.Database,
                UserID             = DbOptions.UserId,
                Password           = DbOptions.Password,
                AllowUserVariables = true,
                CharacterSet       = "utf8",
                SslMode            = MySqlSslMode.None
            };

            options.ConnectionString = connStrBuilder.ToString();
        }
Beispiel #32
0
        public bool ExistTables(DbOptions dbOptions, DbContext dbContext, IEnumerable <string> tableNames)
        {
            if (tableNames.IsNullOrEmpty())
            {
                throw new ArgumentException("tableNames can not be null or empty", nameof(tableNames));
            }
            String conn = dbOptions.GetConnectionString(dbContext.GetType());

            String tableNamesPredicate = tableNames.Select(n => $"[name] = '{n}'").ToArrayString(" or ");
            String sql = $"select count(*) from sqlite_master where type = 'table' and ({tableNamesPredicate})";

            using (SqliteConnection connection = new SqliteConnection(conn))
            {
                connection.Open();
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    return(tableNames.Count() == Convert.ToInt32(command.ExecuteScalar()));
                }
            }
        }
Beispiel #33
0
        public RocksTest()
        {
            var dataPath = Path.Combine(Path.GetFullPath("."), "rockstest");

            if (Directory.Exists(dataPath))
            {
                Directory.Delete(dataPath, true);
            }
            Directory.CreateDirectory("/var/data/rockstest");

            var options = new DbOptions().SetCreateIfMissing(true);

            using (var db = RocksDb.Open(options, "/var/data/rockstest")) {
                // Using strings below, but can also use byte arrays for both keys and values
                // much care has been taken to minimize buffer copying
                db.Put("key", "value1");
                string value = db.Get("key");
                Console.WriteLine(value);
                db.Remove("key");
            }
        }
Beispiel #34
0
 //Enum extensions
 public static bool IsSet(this DbOptions options, DbOptions option)
 {
     return (options & option) != 0;
 }
Beispiel #35
0
 //Prepares for full run with a specified server
 public static void Reset(DbServerType serverType)
 {
     ServerType = serverType;
       //Load connection string
       var connStringName = ServerType.ToString() + "ConnectionString";
       var connString = ConfigurationManager.AppSettings[connStringName];
       Util.Check(!string.IsNullOrEmpty(connString), "Connection string not found for key: {0}.", connStringName);
       if(connString.Contains("{bin}")) {
     var asmPath = Assembly.GetExecutingAssembly().Location;
     var binFolder = Path.GetDirectoryName(asmPath);
     connString = connString.Replace("{bin}", binFolder);
       }
       ConnectionString = connString;
       Driver = ToolHelper.CreateDriver(ServerType); //, ConnectionString);
       DbOptions = ToolHelper.GetDefaultOptions(ServerType);
       //enable stored procs
       DbOptions &= ~DbOptions.UseStoredProcs; //it is on by default
       var useSp = ConfigurationManager.AppSettings["useStoredProcs"] == "true";
       if (useSp && Driver.Supports(DbFeatures.StoredProcedures))
     DbOptions |= Data.DbOptions.UseStoredProcs;
       //enable batch
       var useBatch = ConfigurationManager.AppSettings["useBatchMode"] == "true";
       if(useBatch && Driver.Supports(DbFeatures.BatchedUpdates))
     DbOptions |= DbOptions.UseBatchMode;
       //check connection
       string error;
       if(!ToolHelper.TestConnection(Driver, ConnectionString, out error)) {
     Util.Throw("Failed to connection to the database: {0} \r\n  Connection string: {1}", error, ConnectionString);
       }
 }