BeginTransaction() public method

Create a transaction for use with the environment. The transaction handle may be discarded using Abort() or Commit(). Note: Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction. Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
public BeginTransaction ( ) : LightningTransaction
return LightningTransaction
Example #1
0
        public void TestGet()
        {
            using (var env = new LightningEnvironment("pathtofolder"))
            {
                env.MaxDatabases = 2;
                env.Open();

                using (var tx = env.BeginTransaction(TransactionBeginFlags.ReadOnly))
                {
                    var db = tx.OpenDatabase("custom");
                    var result = tx.Get(db, "hello");
                    Debug.Assert(result == "world");
                }
            }
        }
Example #2
0
        public void TestCreateOpenDataBaseAndPut()
        {
            using (var env = new LightningEnvironment("pathtofolder"))
            {
                env.MaxDatabases = 2;
                env.Open();

                using (var tx = env.BeginTransaction())
                using (var db = tx.OpenDatabase("custom", new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
                {
                    tx.Put(db, "hello", "world");
                    tx.Commit();
                }
            }
        }
        public LightningDocumentStorageFactory(string rootFolder, long mapSize)
        {
            _rootFolder = rootFolder;
            LightningEnvironment = new LightningEnvironment(rootFolder);
            LightningEnvironment.MapSize = mapSize;
            LightningEnvironment.MaxDatabases = 2;
            LightningEnvironment.Open();

            using (var txn = LightningEnvironment.BeginTransaction())
            {
                using (txn.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
                {
                    txn.Commit();
                }
            }
        }
Example #4
0
        private LightningEnvironment NewEnvironment(out LightningDatabase db, bool delete = true)
        {
            if (delete && Directory.Exists(_path))
                Directory.Delete(_path, true);

            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);

            var env = new LightningEnvironment(_path, EnvironmentOpenFlags.None)
                          {
                              MapSize = 1024 * 1024 * 1024 * (long)10
                          };
            env.Open();
            var tx = env.BeginTransaction();
            db = tx.OpenDatabase();
			tx.Commit();

            return env;
        }
Example #5
0
        private static long ReadInternal(IEnumerable<uint> ids, PerfTracker perfTracker, LightningEnvironment env,
			LightningDatabase db)
        {
            using (var tx = env.BeginTransaction(LightningDB.TransactionBeginFlags.ReadOnly))
			using (var cursor = new LightningCursor(db, tx))
            {
                long v = 0;
                foreach (var id in ids)
                {
                    var value = cursor.MoveTo(Encoding.UTF8.GetBytes(id.ToString("0000000000000000")));
                    v += value.Value.Length;
                    //Debug.Assert(value != null);
                }
                return v;
            }
        }
Example #6
0
        private List<PerformanceRecord> WriteInternal(
            string operation,
            IEnumerator<TestData> enumerator,
            long itemsPerTransaction,
            long numberOfTransactions,
            PerfTracker perfTracker,
			rndseq Rflag,
            LightningEnvironment env,
			LightningDatabase db)
        {
            byte[] valueToWrite = null;
            var records = new List<PerformanceRecord>();
            var sw = new Stopwatch();
			LightningDB.PutOptions putflags = LightningDB.PutOptions.None;

			if (Rflag == rndseq.SEQ)
				putflags = LightningDB.PutOptions.AppendData;

            for (var transactions = 0; transactions < numberOfTransactions; transactions++)
            {
                sw.Restart();

                using (var tx = env.BeginTransaction())
                {
                    for (var i = 0; i < itemsPerTransaction; i++)
                    {
                        enumerator.MoveNext();

                        valueToWrite = GetValueToWrite(valueToWrite, enumerator.Current.ValueSize);

                        tx.Put(db, Encoding.UTF8.GetBytes(enumerator.Current.Id.ToString("0000000000000000")), valueToWrite, putflags);
                    }

                    tx.Commit();
                }

                sw.Stop();
                perfTracker.Record(sw.ElapsedMilliseconds);

                records.Add(new PerformanceRecord
                                {
                                    Operation = operation, 
                                    Time = DateTime.Now, 
                                    Duration = sw.ElapsedMilliseconds, 
                                    ProcessedItems = itemsPerTransaction
                                });
            }

            sw.Stop();

            return records;
        }