Ejemplo n.º 1
0
        /// <summary>
        /// Starts LiteDB database. Open database file or create a new one if not exits
        /// </summary>
        /// <param name="connectionString">Full filename or connection string</param>
        public LiteDatabase(string connectionString)
        {
            this.ConnectionString = new ConnectionString(connectionString);

            if (!File.Exists(this.ConnectionString.Filename))
            {
                DiskService.CreateNewDatafile(this.ConnectionString);
            }

            this.Mapper = BsonMapper.Global;

            this.Recovery = new RecoveryService(this.ConnectionString);

            this.Recovery.TryRecovery();

            this.Disk = new DiskService(this.ConnectionString);

            this.Cache = new CacheService(this.Disk);

            this.Pager = new PageService(this.Disk, this.Cache);

            this.Journal = new JournalService(this.ConnectionString, this.Cache);

            this.Indexer = new IndexService(this.Cache, this.Pager);

            this.Transaction = new TransactionService(this.Disk, this.Cache, this.Journal);

            this.Data = new DataService(this.Disk, this.Cache, this.Pager);

            this.Collections = new CollectionService(this.Cache, this.Pager, this.Indexer, this.Data);

            this.UpdateDatabaseVersion();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Create a empty database ready to be used using connectionString as parameters
 /// </summary>
 public static void CreateNewDatafile(ConnectionString connectionString)
 {
     using (var stream = File.Create(connectionString.Filename))
     {
         using (var writer = new BinaryWriter(stream))
         {
             DiskService.WritePage(writer, new HeaderPage());
         }
     }
 }
Ejemplo n.º 3
0
        public void CreateJournalFile(Action action)
        {
            if (!_connectionString.JournalEnabled)
            {
                action();
                return;
            }

            FileStream journal = null;

            // try create journal file in EXCLUSIVE mode
            DiskService.TryExec(_connectionString.Timeout, () =>
            {
                journal = new FileStream(_connectionString.JournalFilename,
                                         FileMode.Create, FileAccess.ReadWrite, FileShare.None, BasePage.PAGE_SIZE);
            });

            try
            {
                using (var writer = new BinaryWriter(journal))
                {
                    // first, allocate all journal file
                    var total = (uint)_cache.GetDirtyPages().Count();
                    journal.SetLength(total * BasePage.PAGE_SIZE);

                    // write all dirty pages in sequence on journal file
                    foreach (var page in _cache.GetDirtyPages())
                    {
                        this.WritePageInJournal(writer, page);
                    }

                    // flush all data
                    writer.Flush();

                    // mark header as finish
                    journal.Seek(FINISH_POSITION, SeekOrigin.Begin);

                    writer.Write(true); // mark as TRUE

                    // flush last finish mark
                    writer.Flush();

                    action();
                }

                journal.Dispose();

                File.Delete(_connectionString.JournalFilename);
            }
            catch
            {
                journal.Dispose();
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a empty database ready to be used using connectionString as parameters
 /// </summary>
 private static void CreateNewDatabase(ConnectionString connectionString)
 {
     using (var stream = File.Create(connectionString.Filename))
     {
         using (var writer = new BinaryWriter(stream))
         {
             // creating header + master collection
             DiskService.WritePage(writer, new HeaderPage {
                 PageID = 0, LastPageID = 1
             });
             DiskService.WritePage(writer, new CollectionPage {
                 PageID = 1, CollectionName = "_master"
             });
         }
     }
 }
Ejemplo n.º 5
0
        private void DoRecovery(BinaryReader reader)
        {
            // open disk service
            using (var disk = new DiskService(_connectionString))
            {
                disk.Lock();

                // while pages, read from redo, write on disk
                while (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    var page = this.ReadPageJournal(reader);

                    disk.WritePage(page);
                }

                reader.Close();

                disk.UnLock();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Create a empty database ready to be used using connectionString as parameters
        /// </summary>
        private static void CreateNewDatabase(ConnectionString connectionString)
        {
            using (var stream = File.Create(connectionString.Filename))
            {
                using (var writer = new BinaryWriter(stream))
                {
                    // creating header + master collection
                    DiskService.WritePage(writer, new HeaderPage {
                        PageID = 0, LastPageID = 1
                    });
                    DiskService.WritePage(writer, new CollectionPage {
                        PageID = 1, CollectionName = "_master"
                    });
                }
            }

            // create _id index on _master collection
            using (var db = new LiteEngine("filename=" + connectionString.Filename + ";journal=false"))
            {
                db.GetCollection("_master").EnsureIndex("_id", true);
            }
        }
Ejemplo n.º 7
0
 public PageService(DiskService disk, CacheService cache)
 {
     _disk  = disk;
     _cache = cache;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Write a page from memory to disk
 /// </summary>
 public void WritePage(BasePage page)
 {
     DiskService.WritePage(GetWriter(), page);
 }
Ejemplo n.º 9
0
 public DataService(DiskService disk, CacheService cache, PageService pager)
 {
     _disk  = disk;
     _cache = cache;
     _pager = pager;
 }
Ejemplo n.º 10
0
 internal TransactionService(DiskService disk, CacheService cache, JournalService journal)
 {
     _disk    = disk;
     _cache   = cache;
     _journal = journal;
 }
Ejemplo n.º 11
0
        public CacheService(DiskService disk)
        {
            _disk = disk;

            _cache = new SortedDictionary <uint, BasePage>();
        }