Пример #1
0
        /// <summary>
        /// Initialize LiteEngine using custom disk service implementation and full engine options
        /// </summary>
        public LiteEngine(IDiskService disk, string password = null, TimeSpan?timeout = null, int cacheSize = 5000, Logger log = null, bool utcDate = false)
        {
            if (disk == null)
            {
                throw new ArgumentNullException("disk");
            }

            _timeout    = timeout ?? TimeSpan.FromMinutes(1);
            _cacheSize  = cacheSize;
            _disk       = disk;
            _log        = log ?? new Logger();
            _bsonReader = new BsonReader(utcDate);

            try
            {
                // initialize datafile (create) and set log instance
                _disk.Initialize(_log, password);

                // lock disk (read mode) before read header
                var position = _disk.Lock(LockState.Read, _timeout);

                var buffer = _disk.ReadPage(0);

                _disk.Unlock(LockState.Read, position);

                // create header instance from array bytes
                var header = BasePage.ReadPage(buffer) as HeaderPage;

                // hash password with sha1 or keep as empty byte[20]
                var sha1 = password == null ? new byte[20] : AesEncryption.HashSHA1(password);

                // compare header password with user password even if not passed password (datafile can have password)
                if (sha1.BinaryCompareTo(header.Password) != 0)
                {
                    throw LiteException.DatabaseWrongPassword();
                }

                // initialize AES encryptor
                if (password != null)
                {
                    _crypto = new AesEncryption(password, header.Salt);
                }

                // initialize all services
                this.InitializeServices();

                // if header are marked with recovery, do it now
                if (header.Recovery)
                {
                    _trans.Recovery();
                }
            }
            catch (Exception)
            {
                // explicit dispose
                this.Dispose();
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Initialize LiteEngine using custom disk service implementation and full engine options
        /// </summary>
        public LiteEngine(IDiskService disk, string password = null, TimeSpan?timeout = null, int cacheSize = 5000, Logger log = null)
        {
            if (disk == null)
            {
                throw new ArgumentNullException("disk");
            }

            _timeout   = timeout ?? TimeSpan.FromMinutes(1);
            _cacheSize = cacheSize;
            _disk      = disk;
            _log       = log ?? new Logger();

            try
            {
                // initialize datafile (create) and set log instance
                _disk.Initialize(_log, password);

                // read header page
                var header = BasePage.ReadPage(_disk.ReadPage(0)) as HeaderPage;

                // hash password with sha1 or keep as empty byte[20]
                var sha1 = password == null ? new byte[20] : AesEncryption.HashSHA1(password);

                // compare header password with user password even if not passed password (datafile can have password)
                if (sha1.BinaryCompareTo(header.Password) != 0)
                {
                    throw LiteException.DatabaseWrongPassword();
                }

                // initialize AES encryptor
                if (password != null)
                {
                    _crypto = new AesEncryption(password, header.Salt);
                }

                // initialize all services
                this.InitializeServices();

                // try recovery if has journal file
                _trans.Recovery();
            }
            catch (Exception)
            {
                // explicit dispose
                this.Dispose();
                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Initialize LiteEngine using custom disk service implementation and full engine options
        /// </summary>
        public LiteEngine(IDiskService disk, string password = null, TimeSpan? timeout = null, bool autocommit = true, int cacheSize = 5000, Logger log = null)
        {
            _timeout = timeout ?? TimeSpan.FromMinutes(1);
            _cacheSize = cacheSize;
            _autocommit = autocommit;
            _disk = disk;
            _log = log ?? new Logger();
            _locker = new Locker(_timeout);

            // initialize datafile (create) and set log instance
            _disk.Initialize(_log, password);

            // read header page
            var header = BasePage.ReadPage(_disk.ReadPage(0)) as HeaderPage;

            // hash password with sha1 or keep as empty byte[20]
            var sha1 = password == null ? new byte[20] : AesEncryption.HashSHA1(password);

            // compare header password with user password even if not passed password (datafile can have password)
            if (sha1.BinaryCompareTo(header.Password) != 0)
            {
                // explicit dispose
                _disk.Dispose();
                throw LiteException.DatabaseWrongPassword();
            }

            // initialize AES encryptor
            if (password != null)
            {
                _crypto = new AesEncryption(password, header.Salt);
            }

            // initialize all services
            this.InitializeServices();

            if (_disk.IsJournalEnabled)
            {
                // try recovery if has journal file
                _trans.Recovery();
            }
        }
Пример #4
0
        public DbEngine(IDiskService disk, Logger log)
        {
            // initialize disk service and check if database exists
            var isNew = disk.Initialize();

            // new database? create new datafile
            if (isNew)
            {
                disk.CreateNew();
            }

            _log  = log;
            _disk = disk;

            // initialize all services
            _cache       = new CacheService();
            _pager       = new PageService(_disk, _cache);
            _indexer     = new IndexService(_pager);
            _data        = new DataService(_pager);
            _collections = new CollectionService(_pager, _indexer, _data);
            _transaction = new TransactionService(_disk, _pager, _cache);
        }
Пример #5
0
        public DbEngine(IDiskService disk, Logger log)
        {
            // initialize disk service and check if database exists
            var isNew = disk.Initialize();

            // new database? just create header page and save it
            if (isNew)
            {
                disk.WritePage(0, new HeaderPage().WritePage());
            }

            _log  = log;
            _disk = disk;

            // initialize all services
            _cache       = new CacheService();
            _pager       = new PageService(_disk, _cache);
            _indexer     = new IndexService(_pager);
            _data        = new DataService(_pager);
            _collections = new CollectionService(_pager, _indexer, _data);
            _transaction = new TransactionService(_disk, _pager, _cache);

            // check user verion
        }