예제 #1
0
        public BufferRef Allocate(byte bucketIndex, out bool fromFreeList, SharedMemoryBuckets buckets = null)
        {
            BufferRef.EnsureBucketIndexInRange(bucketIndex);

            using (var txn = _env.BeginTransaction())
            {
                try
                {
                    BufferRef freeRef = Allocate(txn, bucketIndex, out fromFreeList, buckets);

                    txn.Commit();
                    // Without packing and returning buffers to pool this kills performance
                    // But it must be done.
                    if (!fromFreeList)
                    {
                        _env.Sync(true);
                    }
                    return(freeRef);
                }
                catch
                {
                    txn.Abort();
                    throw;
                }
            }
        }
예제 #2
0
        private static void StoreFile(string path)
        {
            string key   = path;
            string value = String.Empty;

            byte[] buffer = File.ReadAllBytes(path);

            LMDBEnvironment _env;
            string          dir = "c:\\temp\\cache_net10B";

            _env = new LMDBEnvironment(dir);
            _env.MaxDatabases = 2;
            _env.MapSize      = 10485760 * 100;
            _env.Open();

            DateTime dtStart = DateTime.Now;
            var      tx      = _env.BeginTransaction();
            var      db      = tx.OpenDatabase("DB", new DatabaseConfiguration {
                Flags = DatabaseOpenFlags.Create
            });
            var enc = System.Text.Encoding.UTF8;

            tx.Put(db, enc.GetBytes(key), buffer);
            tx.Commit();
            db.Dispose();

            DateTime dtStop = DateTime.Now;
            TimeSpan ts     = dtStop - dtStart;
            string   str    = String.Format("Time elapsed for set:{0} ms", ts.TotalMilliseconds);

            Logger.LogInfo(str);

            _env.Dispose();
        }
예제 #3
0
        private static void CreateSimplesKeyValue(LMDBEnvironment env, int count)
        {
            Logger.LogInfo("CreateSimplesKeyValue");

            DateTime        dtStart = DateTime.Now;
            LMDBTransaction tx      = env.BeginTransaction(TransactionBeginFlags.NoSync);

            using (var db = tx.OpenDatabase(null,
                                            new DatabaseConfiguration {
                Flags = DatabaseOpenFlags.Create
            }))
            {
                Logger.LogInfo("count:" + count);
                for (int i = 0; i < count; i++)
                {
                    string key   = String.Format("key_{0}", i);
                    string value = String.Format("value_{0}", i);

                    tx.Put(db, key, value);
                }
                tx.Commit();
            }

            DateTime dtStop = DateTime.Now;
            TimeSpan ts     = dtStop - dtStart;
            string   str    = String.Format("Time elapsed for set:{0} ms", ts.TotalMilliseconds);

            Logger.LogInfo(str);
        }
예제 #4
0
        private static void GetSimplesKeyValue(LMDBEnvironment env, int count)
        {
            Logger.LogInfo("GetSimplesKeyValue");

            DateTime        dtStart = DateTime.Now;
            LMDBTransaction tx      = env.BeginTransaction(TransactionBeginFlags.ReadOnly);

            using (LMDBDatabase db = tx.OpenDatabase(null))
            {
                Logger.LogInfo("count:" + count);
                for (int i = 0; i < count; i++)
                {
                    string key = String.Format("key_{0}", i);
                    //string value = String.Format("value_{0}", i);

                    var value = tx.Get(db, key);
                    if (i % 10000 == 0)
                    {
                        string strD = String.Format("key:{0} => value:{1}", key, value);
                        Logger.LogInfo(strD);
                    }
                }
            }

            DateTime dtStop = DateTime.Now;
            TimeSpan ts     = dtStop - dtStart;
            string   str    = String.Format("Time elapsed for get:{0} ms", ts.TotalMilliseconds);

            Logger.LogInfo(str);
        }
예제 #5
0
        public void CanSaveAndReadString()
        {
            string key   = "jam!";
            string value = "jim!";

            using (var db = _env.OpenDatabase(DatabaseName, new DatabaseConfig(DbFlags.Create)))
            {
                using (var tx = _env.BeginTransaction())
                {
                    tx.Put(db, key, value);
                    tx.Commit();
                }

                using (var tx = _env.BeginReadOnlyTransaction())
                {
                    Bufferable b       = default;
                    var        success = tx.TryGet(db, key, out b);
                    Assert.True(success);
                    Assert.Equal(value, b);
                }
            }
        }
예제 #6
0
        private static void CreateAnEntry(LMDBEnvironment env)
        {
            Logger.LogInfo("CreateAnEntry");

            var tx = env.BeginTransaction();

            using (var db = tx.OpenDatabase(null,
                                            new DatabaseConfiguration {
                Flags = DatabaseOpenFlags.Create
            }))
            {
                tx.Put(db, "hello", "world");

                var result = tx.Get(db, "hello");
                Logger.LogInfo("Value for key hello =>" + result);
                tx.Commit();
            }
        }
예제 #7
0
        /// <inheritdocs/>
        public void Append(LogEntry[] entries, long?startingOffset = null)
        {
            if (startingOffset.HasValue && startingOffset.Value != LastIndex + 1)
            {
                throw new InvalidOperationException($"Starting index is {startingOffset} but LastIndex is {LastIndex}");
            }

            if (entries.Length == 0)
            {
                throw new InvalidOperationException("Entries is empty.");
            }

            TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - Before Lock");
            lock (_lock)
            {
                startingOffset = LastIndex + 1;
                TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - Inside Lock. Entries: {entries.Length}. startingOffset: {startingOffset}");
                var indices = Enumerable.Range(0, entries.Length).Select(x => x + startingOffset);
                TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - Before tx");
                using (var tx = _env.BeginTransaction())
                {
                    TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - After tx");
                    foreach (var e in entries.Zip(indices, (l, i) => (i, l)).Select(x => new Bufferable(x.l.Body).PrefixWithIndexAndTerm(x.i.Value, x.l.Term)))
                    {
                        TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - Before put");
                        tx.Put(_logDb, LogKey, e, TransactionPutOptions.AppendDuplicateData);
                        TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - After Put");
                    }

                    tx.Commit();
                }

                TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - Before LoadLastTermAndIndex");
                LoadLastTermAndIndex();
                TheTrace.TraceVerbose($"[{Name}] LmdbPersister.Append - After LoadLastTermAndIndex");
                if (LastIndex != startingOffset + entries.Length - 1)
                {
                    throw new InvalidOperationException($"THIS IS BAD!! expected {startingOffset} + {entries.Length} but found {LastIndex}.");
                }
                if (LastEntryTerm != entries.Last().Term)
                {
                    throw new InvalidOperationException($"THIS IS BAD!! expected last term {entries.Last().Term} but found {LastEntryTerm}.");
                }
            }
        }
예제 #8
0
        private static void EnumData(LMDBEnvironment env)
        {
            Logger.LogInfo("EnumData");

            DateTime        dtStart = DateTime.Now;
            LMDBTransaction tx      = env.BeginTransaction(TransactionBeginFlags.NoSync);

            using (LMDBDatabase db = tx.OpenDatabase(null,
                                                     new DatabaseConfiguration {
                Flags = DatabaseOpenFlags.Create
            }))
            {
                var cur = tx.CreateCursor(db);
                //cur.MoveToFirst();
                int count1 = 0;
                while (cur.MoveNext())
                {
                    var fKey   = Encoding.UTF8.GetString(cur.Current.Key);
                    var fValue = Encoding.UTF8.GetString(cur.Current.Value);
                    if (count1 % 10000 == 0)
                    {
                        string str3 = String.Format("count:{2} - key:{0} => value:{1}", fKey, fValue, count1);
                        Logger.LogInfo(str3);
                    }

                    ++count1;
                }
                tx.Commit();
            }

            DateTime dtStop = DateTime.Now;
            TimeSpan ts     = dtStop - dtStart;
            string   str    = String.Format("Time elapsed for enum:{0} ms", ts.TotalMilliseconds);

            Logger.LogInfo(str);
        }
예제 #9
0
 public ReadWriteTransaction OpenReadWriteTransaction()
 {
     ThrowIfDisposed();
     return(new ReadWriteTransaction(environment.BeginTransaction(), databasesHolder, indexesHolder));
 }