LiteDB.LiteEngine.Upgrade C# (CSharp) Method

Upgrade() public static method

Upgrade datafile from v6 to new v7 format used in LiteDB 3
public static Upgrade ( string filename, string password = null, bool backup = true, int batchSize = 5000 ) : bool
filename string
password string
backup bool
batchSize int
return bool
        public static bool Upgrade(string filename, string password = null, bool backup = true, int batchSize = 5000)
        {
            // if not exists, just exit
            if (!File.Exists(filename)) return false;

            // use a temp file to copy/convert data from
            var tempFile = FileHelper.GetTempFile(filename);

            // open fiel as stream and test if is V6
            using(var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                IDbReader reader = new LiteDB_V6.DbReader();

                if (reader.Initialize(stream, password) == false) return false;

                // open new datafile to copy data from
                using (var engine = new LiteEngine(tempFile, false))
                {
                    foreach(var col in reader.GetCollections())
                    {
                        // first, create all indexes
                        var indexes = reader.GetIndexes(col);

                        foreach(var index in indexes)
                        {
                            engine.EnsureIndex(col, index.Key, index.Value);
                        }

                        // now copy documents in 5000 groups
                        var docs = reader.GetDocuments(col);

                        foreach(var batch in docs.Batch(batchSize))
                        {
                            engine.Insert(col, batch);

                            // just clear pages
                            engine.Rollback();
                        }
                    }
                }
            }

            // if backup, move current file to new -bkp
            if (backup)
            {
                File.Move(filename, FileHelper.GetTempFile(filename, "-bkp"));
            }
            else
            {
                File.Delete(filename);
            }

            // move temp file to original filename
            File.Move(tempFile, filename);

            return true;
        }
    }

Usage Example

Example #1
0
        /// <summary>
        /// Starts LiteDB database using a connection string for file system database
        /// </summary>
        public LiteDatabase(ConnectionString connectionString, BsonMapper mapper = null, Logger log = null)
        {
            _connectionString = connectionString ?? throw new ArgumentNullException(nameof(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));
        }
All Usage Examples Of LiteDB.LiteEngine::Upgrade
LiteEngine