Пример #1
0
        public FasterDictionary(IFasterEqualityComparer <TKey> keyComparer, Options options = default)
        {
            if (keyComparer == null)
            {
                throw new ArgumentNullException(nameof(keyComparer));
            }

            _keyComparer = new KeyComparerAdapter(keyComparer);
            _options     = Options.Default;
            if (options.PersistDirectoryPath != null)
            {
                _options.DeleteOnClose  = options.DeleteOnClose;
                _options.DictionaryName = options.DictionaryName;

                _options.Logger = options.Logger;
                _options.PersistDirectoryPath = options.PersistDirectoryPath;

                if (options.CheckPointType != CheckpointType.Snapshot)
                {
                    _options.CheckPointType = options.CheckPointType;
                }

                if (options.MemorySize != 0)
                {
                    _options.MemorySize = options.MemorySize;
                }

                if (options.PageSize != 0)
                {
                    _options.PageSize = options.PageSize;
                }

                if (options.SegmentSize != 0)
                {
                    _options.SegmentSize = options.SegmentSize;
                }
            }

            JobQueue = Channel.CreateUnbounded <Job>(new UnboundedChannelOptions()
            {
                AllowSynchronousContinuations = true,
                SingleReader = true,
                SingleWriter = false
            });

            StartConsumer();
        }
Пример #2
0
        public FasterStore(IOptions <FasterStoreOptions> optionsProvider, IFasterEqualityComparer <TKey> keyComparer, ILogger <FasterStore <TKey, TValue> > logger)
        {
            _logger = logger;
            var options         = optionsProvider.Value;
            var indexStorePath  = Path.Combine(options.Directory, $"{options.Name}-index.log");
            var objectStorePath = Path.Combine(options.Directory, $"{options.Name}-object.log");

            _indexDevice = Devices.CreateLogDevice(indexStorePath,
                                                   preallocateFile: false, deleteOnClose: false, recoverDevice: true);

            _objectDevice = Devices.CreateLogDevice(objectStorePath,
                                                    preallocateFile: false, deleteOnClose: false, recoverDevice: true);

            _checkpointsPath = Path.Combine(options.Directory, "checkpoints");

            if (!Directory.Exists(_checkpointsPath))
            {
                _logger.LogInformation("Checkpoints directory not exists. Creating...");
                Directory.CreateDirectory(_checkpointsPath);
            }

            var logSettings = new LogSettings
            {
                LogDevice         = _indexDevice,
                ObjectLogDevice   = _objectDevice,
                MemorySizeBits    = (int)options.MemorySize,
                PageSizeBits      = (int)options.PageSize,
                SegmentSizeBits   = (int)options.SegmentSize,
                ReadCacheSettings = new ReadCacheSettings()
                {
                    MemorySizeBits       = (int)options.MemorySize + 1,
                    PageSizeBits         = (int)options.PageSize + 1,
                    SecondChanceFraction = .2
                }
            };

            _serializerSettings = new SerializerSettings <KeyHolder, ValueHolder>
            {
                keySerializer   = () => new KeySerializer(options.JsonOptions),
                valueSerializer = () => new ValueSerializer(options.JsonOptions)
            };

            var comparer            = new KeyComparerAdapter(keyComparer);
            var functions           = new StoreFunctions(_logger);
            var checkpointsSettings = new CheckpointSettings
            {
                CheckpointDir  = _checkpointsPath,
                CheckPointType = CheckpointType.Snapshot
            };

            _keyValueStore = new FasterKV <KeyHolder, ValueHolder, ValueHolder, ValueHolder, StoreContext, StoreFunctions>(
                StoreSize, functions, logSettings, checkpointsSettings, _serializerSettings, comparer);

            var checkpoints = Directory.GetDirectories(_checkpointsPath).Length;

            if (checkpoints > 0)
            {
                _logger.LogInformation($"Found {checkpoints} checkpoints. Recovering store...");
                _keyValueStore.Recover();
                _logger.LogInformation("Store recovered from checkpoints");
            }

            _session = _keyValueStore.NewSession();
        }