Exemplo n.º 1
0
        public IndexFile(string filename, byte maxKeySize)//, ushort pageNodeCount)
        {
            _T = RDBDataType <T> .ByteHandler();

            if (typeof(T) == typeof(string) && Global.EnableOptimizedStringIndex)
            {
                _externalStrings = true;
                _maxKeySize      = 4;// blocknum:int
            }
            else
            {
                _maxKeySize = maxKeySize;
            }

            _PageNodeCount = Global.PageItemCount;// pageNodeCount;
            _rowSize       = (_maxKeySize + 1 + 4 + 4);

            _FileName = filename.Substring(0, filename.LastIndexOf('.'));
            string path = Path.GetDirectoryName(filename);

            Directory.CreateDirectory(path);
            if (File.Exists(filename))
            {
                // if file exists open and read header
                _file = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                ReadFileHeader();
                if (_externalStrings == false)// if the file says different
                {
                    _rowSize = (_maxKeySize + 1 + 4 + 4);
                }
                // compute last page number from file length
                _PageLength     = (_BlockHeader.Length + _rowSize * (_PageNodeCount));
                _LastPageNumber = (int)((_file.Length - _FileHeader.Length) / _PageLength);
            }
            else
            {
                // else create new file
                _file = File.Open(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

                _PageLength = (_BlockHeader.Length + _rowSize * (_PageNodeCount));

                CreateFileHeader(0);

                _LastPageNumber = (int)((_file.Length - _FileHeader.Length) / _PageLength);
            }
            if (_externalStrings)
            {
                _strings = new KeyStoreHF(path, Path.GetFileNameWithoutExtension(filename) + ".strings");
            }
            if (_LastPageNumber == 0)
            {
                _LastPageNumber = 1;
            }
            // bitmap duplicates
            if (_allowDups)
            {
                _bitmap = new BitmapIndex(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename));
            }
        }
Exemplo n.º 2
0
        public void CompactStorageHF()
        {
            lock (_lock)
            {
                try
                {
                    _log.Debug("Compacting storage file ...");
                    if (Directory.Exists(_Path + "temp"))
                    {
                        Directory.Delete(_Path + "temp", true);
                    }

                    KeyStoreHF newfile = new KeyStoreHF(_Path + "temp");
                    string[]   keys    = _keys.GetKeys().Cast <string>().ToArray();
                    _log.Debug("Number of keys : " + keys.Length);
                    foreach (var k in keys)
                    {
                        newfile.SetObjectHF(k, GetObjectHF(k));
                    }
                    newfile.Shutdown();
                    _log.Debug("Compact done.");
                    // shutdown and move files and restart here
                    if (Directory.Exists(_Path + "old"))
                    {
                        Directory.Delete(_Path + "old", true);
                    }
                    Directory.CreateDirectory(_Path + "old");
                    _datastore.Shutdown();
                    _keys.Shutdown();
                    _log.Debug("Moving files...");
                    foreach (var f in Directory.GetFiles(_Path, "*.*"))
                    {
                        File.Move(f, _Path + "old" + _S + Path.GetFileName(f));
                    }

                    foreach (var f in Directory.GetFiles(_Path + "temp", "*.*"))
                    {
                        File.Move(f, _Path + Path.GetFileName(f));
                    }

                    Directory.Delete(_Path + "temp", true);
                    //Directory.Delete(_Path + "old", true); // FEATURE : delete or keep?
                    _log.Debug("Re-opening storage file");
                    _datastore = new StorageFileHF(_Path + "data.mghf", Global.HighFrequencyKVDiskBlockSize);
                    _keys      = new MGIndex <string>(_Path, "keys.idx", 255, /*Global.PageItemCount,*/ false);

                    _BlockSize = _datastore.GetBlockSize();
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }
            }
        }
Exemplo n.º 3
0
        public void CompactStorageHF()
        {
            lock (_lock)
            {
                try
                {
                    _log.Debug("Compacting storage file ...");
                    if (Directory.Exists(_Path + "temp"))
                        Directory.Delete(_Path + "temp", true);

                    KeyStoreHF newfile = new KeyStoreHF(_Path + "temp");
                    string[] keys = _keys.GetKeys().Cast<string>().ToArray();
                    _log.Debug("Number of keys : " + keys.Length);
                    foreach (var k in keys)
                    {
                        newfile.SetObjectHF(k, GetObjectHF(k));
                    }
                    newfile.Shutdown();
                    _log.Debug("Compact done.");
                    // shutdown and move files and restart here
                    if (Directory.Exists(_Path + "old"))
                        Directory.Delete(_Path + "old", true);
                    Directory.CreateDirectory(_Path + "old");
                    _datastore.Shutdown();
                    _keys.Shutdown();
                    _log.Debug("Moving files...");
                    foreach (var f in Directory.GetFiles(_Path, "*.*"))
                        File.Move(f, _Path + "old" + _S + Path.GetFileName(f));

                    foreach (var f in Directory.GetFiles(_Path + "temp", "*.*"))
                        File.Move(f, _Path + Path.GetFileName(f));

                    Directory.Delete(_Path + "temp", true);
                    //Directory.Delete(_Path + "old", true); // FEATURE : delete or keep?
                    _log.Debug("Re-opening storage file");
                    _datastore = new StorageFileHF(_Path + "data.mghf", Global.HighFrequencyKVDiskBlockSize);
                    _keys = new MGIndex<string>(_Path, "keys.idx", 255, Global.PageItemCount, false);

                    _BlockSize = _datastore.GetBlockSize();
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }
            }
        }