Esempio n. 1
0
        private void NewHead()
        {
            _log.DebugFormat("switching to new active file at size {0}", _head.Size);
            _files[_head.FileId] = FirkinFile.OpenArchiveFromActive(_head);
            var fileId = (ushort)(_head.FileId + 1);

            _head = FirkinFile.CreateActive(GetDataFilename(fileId), fileId);
            _files[_head.FileId] = _head;
        }
Esempio n. 2
0
 public void Truncate()
 {
     // Note: Have to acquire merge then index syncroots otherwise we're liable to run into a deadlock
     lock (_mergeSyncRoot) {
         lock (_indexSyncRoot) {
             foreach (var file in _files.Values)
             {
                 var filename = file.Filename;
                 file.Dispose();
                 File.Delete(filename);
             }
             _files.Clear();
             _index.Clear();
             _head = FirkinFile.CreateActive(GetDataFilename(1), 1);
             _files[_head.FileId] = _head;
         }
     }
 }
Esempio n. 3
0
 public static IFirkinArchiveFile OpenArchiveFromActive(IFirkinActiveFile file)
 {
     file.Dispose();
     return new FirkinFile(file.Filename, file.FileId, false, 0);
 }
Esempio n. 4
0
 public static IFirkinArchiveFile OpenArchiveFromActive(IFirkinActiveFile file)
 {
     file.Dispose();
     return(new FirkinFile(file.Filename, file.FileId, false, 0));
 }
Esempio n. 5
0
        private void Initialize()
        {
            if (!Directory.Exists(_storeDirectory))
            {
                Directory.CreateDirectory(_storeDirectory);
            }

            // get all data files
            var files = from filename in Directory.GetFiles(_storeDirectory, STORAGE_FILE_PREFIX + "*" + DATA_FILE_EXTENSION)
                        let fileId = ParseFileId(filename)
                                     orderby fileId
                                     select new { FileId = fileId, Filename = filename };
            uint maxSerial          = 0;
            IFirkinArchiveFile last = null;

            foreach (var fileInfo in files)
            {
                maxSerial = 0;
                var file = FirkinFile.OpenArchive(fileInfo.Filename, fileInfo.FileId);
                _files.Add(file.FileId, file);

                // iterate over key info
                var hintFilename = GetHintFilename(fileInfo.FileId);
                var count        = 0;
                var delete       = 0;
                if (File.Exists(hintFilename))
                {
                    var hintFile = new FirkinHintFile(hintFilename);
                    foreach (var hint in hintFile)
                    {
                        var keyInfo = new KeyInfo(fileInfo.FileId, hint);
                        var key     = _serializer.Deserialize(hint.Key);
                        _index[key] = keyInfo;
                        count++;
                    }
                    hintFile.Dispose();
                    _log.DebugFormat("read {0} record markers from hint file {1}", count, fileInfo.FileId);
                }
                else
                {
                    foreach (var pair in file)
                    {
                        var key = _serializer.Deserialize(pair.Key);
                        maxSerial = pair.Value.Serial;
                        if (pair.Value.ValueSize == 0)
                        {
                            _index.Remove(key);
                            delete++;
                        }
                        else
                        {
                            _index[key] = pair.Value;
                            count++;
                        }
                    }
                    _log.DebugFormat("read {0} record and {1} delete markers from data file {2}", count, delete, fileInfo.FileId);
                }
                last = file;
            }
            if (last != null && last.Size < _maxFileSize)
            {
                _head = FirkinFile.OpenActiveFromArchive(last, maxSerial);
            }
            else
            {
                ushort fileId = 1;
                if (last != null)
                {
                    fileId += last.FileId;
                }
                _head = FirkinFile.CreateActive(GetDataFilename(fileId), fileId);
            }
            _files[_head.FileId] = _head;
        }