private LightningDatabase CreateCollectionInternal(string name, Type keyType) { ITransaction transaction = BeginTransactionInternal(null, TransactionBeginFlags.None); ValidateTransaction(transaction); LightningTransaction lmdbTransaction = (LightningTransaction)transaction.InnerObject; LightningDB.DatabaseConfiguration dbOptions = new LightningDB.DatabaseConfiguration(); dbOptions.Flags = DatabaseOpenFlags.Create; //todo: maybe add other integer types too if (keyType == typeof(long) || keyType == typeof(ulong) || keyType == typeof(int) || keyType == typeof(uint) || keyType == typeof(short) || keyType == typeof(ushort)) { dbOptions.Flags |= DatabaseOpenFlags.IntegerKey; } LightningDatabase collectionInstance = lmdbTransaction.OpenDatabase(name, dbOptions); if (name != METADATA_COLLECTION) { StoreCollectionInfo(name, transaction); } ((LMDBTransaction)transaction).ChangeSize(8192); CommitInternal(transaction); return(collectionInstance); }
/// <summary> /// Creates a LightningDatabase instance. /// </summary> /// <param name="name">Database name.</param> /// <param name="transaction">Active transaction.</param> /// <param name="configuration">Options for the database, like encoding, option flags, and comparison logic.</param> internal LightningDatabase(string name, LightningTransaction transaction, DatabaseConfiguration configuration) { if (transaction == null) throw new ArgumentNullException(nameof(transaction)); if (configuration == null) throw new ArgumentNullException(nameof(configuration)); Name = name; _configuration = configuration; Environment = transaction.Environment; Environment.Disposing += Dispose; mdb_dbi_open(transaction.Handle(), name, _configuration.Flags, out _handle); _configuration.ConfigureDatabase(transaction, this); IsOpened = true; }
/// <summary> /// Opens a database in context of this transaction. /// </summary> /// <param name="name">Database name (optional). If null then the default name is used.</param> /// <param name="configuration">Database open options.</param> /// <returns>Created database wrapper.</returns> public LightningDatabase OpenDatabase(string name = null, DatabaseConfiguration configuration = null) { configuration = configuration ?? new DatabaseConfiguration(); var db = new LightningDatabase(name, this, configuration); return db; }
/// <summary> /// Initializes the specified database. NOTE: A LightningDB 'database' acts like an RDBMS 'table'. /// </summary> /// <param name="database">The database.</param> public void InitializeDatabase(string database) { if (_openDatabases.ContainsKey(database)) return; using (var txn = _environment.BeginTransaction()) { var dbConfig = new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }; var db = txn.OpenDatabase(database, dbConfig); _openDatabases.TryAdd(database, db); txn.Commit(); } }