Beispiel #1
0
        public void Truncate()
        {
            _currentJournaledMemTable.Close();
            TableManager.Default.Close(this);
            foreach (var pair in _secondaryIndexes)
            {
                pair.Value.Close(FastClose);
            }

            string basePath = Path.GetFullPath(Manifest.BaseFileName);

            foreach (string file in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
            {
                Helper.DeleteFile(file, false, (msg) => { Manifest.LogMessage(msg); });
            }
            foreach (string dir in Directory.GetDirectories(basePath, "*.*", SearchOption.AllDirectories))
            {
                Helper.DeleteFolder(dir, false, (msg) => { Manifest.LogMessage(msg); });
            }

            _manifest = new Manifest(basePath);
            _currentJournaledMemTable = new JournaledMemTable(_manifest.BaseFileName, _manifest.CurrentVersion(0));
            _cache            = new RazorCache();
            _secondaryIndexes = new Dictionary <string, KeyValueStore>(StringComparer.OrdinalIgnoreCase);

            Manifest.LogMessage("Database Truncated.");
        }
Beispiel #2
0
        public void Close(bool fast = false)
        {
            // Make sure any inflight rotations have occurred before shutting down.
            if (!_rotationSemaphore.WaitOne(30000))
            {
                throw new TimeoutException("Timed out waiting for table rotation to complete.");
            }
            // Release again in case another thread tries to close it again.
            _rotationSemaphore.Release();

            if (!finalizing && !fast)
            {
                TableManager.Default.Close(this);
            }
            if (_currentJournaledMemTable != null)
            {
                _currentJournaledMemTable.Close();
                _currentJournaledMemTable = null;
            }

            if (_secondaryIndexes != null)
            {
                foreach (var idx in _secondaryIndexes)
                {
                    idx.Value.Close(fast);
                }
            }

            // Don't finalize since we already closed it.
            GC.SuppressFinalize(this);
        }
Beispiel #3
0
#pragma warning restore 420

        private void CheckForIncompleteJournalRotation(string baseFileName, int currentMemTableVersion)
        {
            int previousMemTableVersion = currentMemTableVersion - 1;

            // Is there a left-over journal from a previous rotation that was aborted while in rotation.
            if (File.Exists(Config.JournalFile(baseFileName, previousMemTableVersion)))
            {
                var memTable = new JournaledMemTable(baseFileName, previousMemTableVersion);
                memTable.WriteToSortedBlockTable(_manifest);
                memTable.Close();
            }
        }
Beispiel #4
0
        public KeyValueStore(string baseFileName, RazorCache cache) {
            if (!Directory.Exists(baseFileName)) {
                Directory.CreateDirectory(baseFileName);
            }
            _manifest = new Manifest(baseFileName);
            _manifest.Logger = Config.Logger;

            int memTableVersion = _manifest.CurrentVersion(0);
            // Check for a previously aborted journal rotation 
            CheckForIncompleteJournalRotation(baseFileName, memTableVersion);
            // Create new journal for this run (and potentially load from disk, if there was data loaded previously)
            _currentJournaledMemTable = new JournaledMemTable(_manifest.BaseFileName, memTableVersion);
            _cache = cache == null ? new RazorCache() : cache;

        }
Beispiel #5
0
        public KeyValueStore(string baseFileName, RazorCache cache)
        {
            if (!Directory.Exists(baseFileName))
            {
                Directory.CreateDirectory(baseFileName);
            }
            _manifest        = new Manifest(baseFileName);
            _manifest.Logger = Config.Logger;

            int memTableVersion = _manifest.CurrentVersion(0);

            // Check for a previously aborted journal rotation
            CheckForIncompleteJournalRotation(baseFileName, memTableVersion);
            // Create new journal for this run (and potentially load from disk, if there was data loaded previously)
            _currentJournaledMemTable = new JournaledMemTable(_manifest.BaseFileName, memTableVersion);
            _cache = cache == null ? new RazorCache() : cache;
        }
Beispiel #6
0
#pragma warning disable 420
        public void RotateMemTable()
        {
            lock (memTableRotationLock) {
                // Double check the flag in case we have multiple threads that make it into this routine
                if (_currentJournaledMemTable.Full)
                {
                    _rotationSemaphore.WaitOne();    // Wait for the rotation gate to be open, and automatically reset once a single thread gets through.

                    _rotatedJournaledMemTable = Interlocked.Exchange <JournaledMemTable>(ref _currentJournaledMemTable, new JournaledMemTable(_manifest.BaseFileName, _manifest.NextVersion(0)));

                    ThreadPool.QueueUserWorkItem((o) => {
                        try {
                            _rotatedJournaledMemTable.WriteToSortedBlockTable(_manifest);
                            _rotatedJournaledMemTable = null;
                        } finally {
                            _rotationSemaphore.Release(); // Open the gate for the next rotation
                        }
                    });
                }
            }
        }
Beispiel #7
0
        public void Truncate() {
            _currentJournaledMemTable.Close();
            TableManager.Default.Close(this);
            foreach (var pair in _secondaryIndexes) {
                pair.Value.Close(FastClose);
            }

            string basePath = Path.GetFullPath(Manifest.BaseFileName);
            foreach (string file in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories)) {
                Helper.DeleteFile(file, false, (msg) => { Manifest.LogMessage(msg); });
            }
            foreach (string dir in Directory.GetDirectories(basePath, "*.*", SearchOption.AllDirectories)) {
                Helper.DeleteFolder(dir, false, (msg) => { Manifest.LogMessage(msg); });
            }

            _manifest = new Manifest(basePath);
            _currentJournaledMemTable = new JournaledMemTable(_manifest.BaseFileName, _manifest.CurrentVersion(0));
            _cache = new RazorCache();
            _secondaryIndexes = new Dictionary<string, KeyValueStore>(StringComparer.OrdinalIgnoreCase);

            Manifest.LogMessage("Database Truncated.");
        }
Beispiel #8
0
        public void Close(bool fast = false) {
            // Make sure any inflight rotations have occurred before shutting down.
            if (!_rotationSemaphore.WaitOne(30000))
                throw new TimeoutException("Timed out waiting for table rotation to complete.");
            // Release again in case another thread tries to close it again.
            _rotationSemaphore.Release();

            if (!finalizing && !fast) {
                TableManager.Default.Close(this);
            }
            if (_currentJournaledMemTable != null) {
                _currentJournaledMemTable.Close();
                _currentJournaledMemTable = null;
            }

            if (_secondaryIndexes != null) {
                foreach (var idx in _secondaryIndexes) {
                    idx.Value.Close(fast);
                }
            }

            // Don't finalize since we already closed it.
            GC.SuppressFinalize(this);
        }
Beispiel #9
0
#pragma warning restore 420

        private void CheckForIncompleteJournalRotation(string baseFileName, int currentMemTableVersion) {
            int previousMemTableVersion = currentMemTableVersion - 1;
            // Is there a left-over journal from a previous rotation that was aborted while in rotation.
            if (File.Exists(Config.JournalFile(baseFileName, previousMemTableVersion))) {
                var memTable = new JournaledMemTable(baseFileName, previousMemTableVersion);
                memTable.WriteToSortedBlockTable(_manifest);
                memTable.Close();
            }
        }
Beispiel #10
0
#pragma warning disable 420
        public void RotateMemTable() {
            lock (memTableRotationLock) {
                // Double check the flag in case we have multiple threads that make it into this routine
                if (_currentJournaledMemTable.Full) {
                    _rotationSemaphore.WaitOne();    // Wait for the rotation gate to be open, and automatically reset once a single thread gets through.

                    _rotatedJournaledMemTable = Interlocked.Exchange<JournaledMemTable>(ref _currentJournaledMemTable, new JournaledMemTable(_manifest.BaseFileName, _manifest.NextVersion(0)));

                    ThreadPool.QueueUserWorkItem((o) => {
                        try {
                            _rotatedJournaledMemTable.WriteToSortedBlockTable(_manifest);
                            _rotatedJournaledMemTable = null;
                        } finally {
                            _rotationSemaphore.Release(); // Open the gate for the next rotation
                        }
                    });
                }
            }
        }