/// <summary>
        /// Starts LiteDB database using a connection string for file system database
        /// </summary>
        public LiteDatabase(ConnectionString connectionString, BsonMapper mapper = null, Logger log = null)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            _connectionString = connectionString;
            _log       = log ?? new Logger();
            _log.Level = log?.Level ?? _connectionString.Log;

            if (_connectionString.Upgrade)
            {
                LiteEngine.Upgrade(_connectionString.Filename, _connectionString.Password);
            }

            _mapper = mapper ?? BsonMapper.Global;

            var options = new FileOptions
            {
#if HAVE_SYNC_OVER_ASYNC
                Async = _connectionString.Async,
#endif
#if HAVE_FLUSH_DISK
                Flush = _connectionString.Flush,
#endif
                InitialSize = _connectionString.InitialSize,
                LimitSize   = _connectionString.LimitSize,
                Journal     = _connectionString.Journal,
                FileMode    = _connectionString.Mode
            };

            _engine = new LazyLoad <LiteEngine>(() => new LiteEngine(new FileDiskService(_connectionString.Filename, options), _connectionString.Password, _connectionString.Timeout, _connectionString.CacheSize, _log, _connectionString.UtcDate));
        }
示例#2
0
        public LiteCollection(string name, LazyLoad <LiteEngine> engine, BsonMapper mapper, Logger log)
        {
            _name     = name ?? mapper.ResolveCollectionName(typeof(T));
            _engine   = engine;
            _mapper   = mapper;
            _log      = log;
            _visitor  = new QueryVisitor <T>(mapper);
            _includes = new List <string>();

            // if strong typed collection, get _id member mapped (if exists)
            if (typeof(T) != typeof(BsonDocument))
            {
                var entity = mapper.GetEntityMapper(typeof(T));
                _id = entity.Id;

                if (_id != null && _id.AutoId)
                {
                    _autoId =
                        _id.DataType == typeof(ObjectId) ? BsonType.ObjectId :
                        _id.DataType == typeof(Guid) ? BsonType.Guid :
                        _id.DataType == typeof(DateTime) ? BsonType.DateTime :
                        _id.DataType == typeof(Int32) ? BsonType.Int32 :
                        _id.DataType == typeof(Int64) ? BsonType.Int64 :
                        BsonType.Null;
                }
            }
            else
            {
                _autoId = BsonType.ObjectId;
            }
        }
        /// <summary>
        /// Starts LiteDB database using a Stream disk
        /// </summary>
        public LiteDatabase(Stream stream, BsonMapper mapper = null, string password = null, bool disposeStream = false)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            _mapper = mapper ?? BsonMapper.Global;
            _log    = new Logger();

            _engine = new LazyLoad <LiteEngine>(() => new LiteEngine(new StreamDiskService(stream, disposeStream), password: password, log: _log));
        }
        /// <summary>
        /// Starts LiteDB database using a custom IDiskService with all parameters available
        /// </summary>
        /// <param name="diskService">Custom implementation of persist data layer</param>
        /// <param name="mapper">Instance of BsonMapper that map poco classes to document</param>
        /// <param name="password">Password to encrypt you datafile</param>
        /// <param name="timeout">Locker timeout for concurrent access</param>
        /// <param name="cacheSize">Max memory pages used before flush data in Journal file (when available)</param>
        /// <param name="log">Custom log implementation</param>
        public LiteDatabase(IDiskService diskService, BsonMapper mapper = null, string password = null, TimeSpan?timeout = null, int cacheSize = 5000, Logger log = null)
        {
            if (diskService == null)
            {
                throw new ArgumentNullException(nameof(diskService));
            }

            _mapper = mapper ?? BsonMapper.Global;
            _log    = log ?? new Logger();

            _engine = new LazyLoad <LiteEngine>(() => new LiteEngine(diskService, password: password, timeout: timeout, cacheSize: cacheSize, log: _log));
        }