/// <summary> /// Initialize LiteEngine using initial engine settings /// </summary> public LiteEngine(EngineSettings settings) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); // clear checkpoint if database is readonly if (_settings.ReadOnly) { _settings.Checkpoint = 0; } LOG($"start initializing{(_settings.ReadOnly ? " (readonly)" : "")}", "ENGINE"); try { // create new flip coin _flipCoin = new FlipCoin(settings.Seed); // initialize locker service (no dependency) _locker = new LockService(settings.Timeout, settings.ReadOnly); // initialize disk service (will create database if needed) _disk = new DiskService(settings); // read page with no cache ref (has a own PageBuffer) - do not Release() support var buffer = _disk.ReadFull(FileOrigin.Data).First(); // if first byte are 1 this datafile are encrypted but has do defined password to open if (buffer[0] == 1) { throw new LiteException(0, "This data file is encrypted and needs a password to open"); } _header = new HeaderPage(buffer); // initialize wal-index service _walIndex = new WalIndexService(_disk, _locker); // if exists log file, restore wal index references (can update full _header instance) if (_disk.GetLength(FileOrigin.Log) > 0) { _walIndex.RestoreIndex(ref _header); } // initialize sort temp disk _sortDisk = new SortDisk(settings.CreateTempFactory(), CONTAINER_SORT_SIZE, settings.UtcDate); // initialize transaction monitor as last service _monitor = new TransactionMonitor(_header, _locker, _disk, _walIndex, _settings); // register system collections this.InitializeSystemCollections(); LOG("initialization completed", "ENGINE"); } catch (Exception ex) { LOG(ex.Message, "ERROR"); // explicit dispose (but do not run shutdown operation) this.Dispose(true); throw; } }
/// <summary> /// Insert a new node index inside an collection index. Flip coin to know level /// </summary> public IndexNode AddNode(CollectionIndex index, BsonValue key, PageAddress dataBlock, IndexNode last, FlipCoin flipCoin) { // do not accept Min/Max value as index key (only head/tail can have this value) if (key.IsMaxValue || key.IsMinValue) { throw LiteException.InvalidIndexKey($"BsonValue MaxValue/MinValue are not supported as index key"); } // random level (flip coin mode) - return number between 1-32 var level = flipCoin.Flip(); // set index collection with max-index level if (level > index.MaxLevel) { // update max level _snapshot.CollectionPage.UpdateCollectionIndex(index.Name).MaxLevel = level; } // call AddNode with key value return(this.AddNode(index, key, dataBlock, level, last)); }