Exemplo n.º 1
0
        public void AddAndLookupItemsPersisted()
        {
            string path = Path.GetFullPath("TestData\\AddAndLookupItemsPersisted");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            JournalWriter jw = new JournalWriter("TestData\\AddAndLookupItemsPersisted", 523, false);

            List<KeyValuePair<Key, Value>> values = new List<KeyValuePair<Key, Value>>();

            for (int i = 0; i < 10000; i++) {
                var randomKey = Key.Random(40);
                var randomValue = Value.Random(256);

                values.Add(new KeyValuePair<Key, Value>(randomKey, randomValue));
                jw.Add(randomKey, randomValue);
            }
            jw.Close();

            MemTable mtl = new MemTable();
            mtl.ReadFromJournal("TestData\\AddAndLookupItemsPersisted", 523);

            Value value;
            foreach (var pair in values) {
                Assert.IsTrue(mtl.Lookup(pair.Key, out value));
                Assert.AreEqual(pair.Value, value);
            }
            Assert.IsFalse(mtl.Lookup(Key.Random(40), out value));

            Assert.AreEqual(10000 * (40 + 256), mtl.Size);
            Assert.IsTrue(mtl.Full);
        }
Exemplo n.º 2
0
        public void ReadAndWriteJournalFileWithAppend()
        {
            string path = Path.GetFullPath("TestData\\RWJournalAppend");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            JournalWriter jw = new JournalWriter(path, 324, false);

            List<KeyValuePair<Key, Value>> items = new List<KeyValuePair<Key, Value>>();
            for (int i = 0; i < 5000; i++) {
                Key randKey = Key.Random(20);
                Value randValue = Value.Random(100);
                jw.Add(randKey, randValue);
                items.Add(new KeyValuePair<Key, Value>(randKey, randValue));
            }
            jw.Close();

            // reopen the same log for append
            jw = new JournalWriter(path, 324, true);
            for (int i = 0; i < 5000; i++) {
                Key randKey = Key.Random(20);
                Value randValue = Value.Random(100);
                jw.Add(randKey, randValue);
                items.Add(new KeyValuePair<Key, Value>(randKey, randValue));
            }
            jw.Close();

            JournalReader jr = new JournalReader(path, 324);
            int j = 0;
            foreach (var pair in jr.Enumerate()) {
                Assert.AreEqual(items[j].Key, pair.Key);
                Assert.AreEqual(items[j].Value, pair.Value);
                j++;
            }
            jr.Close();
        }
Exemplo n.º 3
0
        public JournaledMemTable(string baseFileName, int version)
        {
            _baseFileName = baseFileName;
            _version      = version;
            _memTable     = new MemTable();

            // If the journal exists from a previous run, then load its data into the memtable
            string journalFile = Config.JournalFile(baseFileName, version);

            if (File.Exists(journalFile))
            {
                var journalReader = new JournalReader(baseFileName, version);
                try {
                    foreach (var pair in journalReader.Enumerate())
                    {
                        _memTable.Add(pair.Key, pair.Value);
                    }
                } finally {
                    journalReader.Close();
                }
                _journal = new JournalWriter(baseFileName, version, true);
            }
            else
            {
                _journal = new JournalWriter(baseFileName, version, false);
            }
        }
Exemplo n.º 4
0
        public void CreateJournalFile()
        {
            string path = Path.GetFullPath("TestData\\JournalFileV1");
             if (!Directory.Exists(path))
                 Directory.CreateDirectory(path);
             JournalWriter jw = new JournalWriter(path, 324, false);

             List<KeyValuePair<KeyEx, Value>> items = new List<KeyValuePair<KeyEx, Value>>();
             for (int i = 0; i < 1000; i++) {
                 KeyEx randKey = KeyEx.Random(20);
                 Value randValue = Value.Random(100);
                 jw.Add(randKey, randValue);
                 items.Add(new KeyValuePair<KeyEx, Value>(randKey, randValue));
             }
             jw.Close();

             JournalReader jr = new JournalReader(path, 324);
             int j = 0;
             foreach (var pair in jr.Enumerate()) {
                 Assert.AreEqual(items[j].Key, pair.Key);
                 Assert.AreEqual(items[j].Value, pair.Value);
                 j++;
             }
             jr.Close();
        }
Exemplo n.º 5
0
 public void Close()
 {
     if (_journal != null)
     {
         _journal.Close();
     }
     _journal  = null;
     _memTable = null;
 }
Exemplo n.º 6
0
        public JournaledMemTable(string baseFileName, int version)
        {
            _baseFileName = baseFileName;
            _version = version;
            _memTable = new MemTable();

            // If the journal exists from a previous run, then load its data into the memtable
            string journalFile = Config.JournalFile(baseFileName, version);
            if (File.Exists(journalFile)) {
                var journalReader = new JournalReader(baseFileName, version);
                try {
                    foreach (var pair in journalReader.Enumerate()) {
                        _memTable.Add(pair.Key, pair.Value);
                    }
                } finally {
                    journalReader.Close();
                }
                _journal = new JournalWriter(baseFileName, version, true);
            } else {
                _journal = new JournalWriter(baseFileName, version, false);
            }
        }
Exemplo n.º 7
0
        public void ReadCorruptedJournalFile()
        {
            string path = Path.GetFullPath("TestData\\ReadCorruptedJournal");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            JournalWriter jw = new JournalWriter(path, 324, false);

            List<KeyValuePair<Key, Value>> items = new List<KeyValuePair<Key, Value>>();
            for (int i = 0; i < 10; i++) {
                Key randKey = Key.Random(20);
                Value randValue = Value.Random(100);
                jw.Add(randKey, randValue);
                items.Add(new KeyValuePair<Key, Value>(randKey, randValue));
            }
            jw.Close();

            // Reopen the file and add a partial record
            var fileName = Config.JournalFile(path, 324);
            var writer = new BinaryWriter(new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None, 1024, false));
            Key key = Key.Random(20);
            writer.Write7BitEncodedInt(key.Length);
            writer.Write(key.InternalBytes);
            writer.Write7BitEncodedInt(0);
            writer.Flush();
            writer.Close();

            JournalReader jr = new JournalReader(path, 324);
            int j = 0;
            foreach (var pair in jr.Enumerate()) {
                Assert.AreEqual(items[j].Key, pair.Key);
                Assert.AreEqual(items[j].Value, pair.Value);
                j++;
            }
            jr.Close();
        }
Exemplo n.º 8
0
 public void Close()
 {
     if (_journal != null)
         _journal.Close();
     _journal = null;
     _memTable = null;
 }