public void ShouldNotReadKeyfileIfKeyIsInPath()
        {
            var key           = 1L;
            var filesystem    = new MemoryFileSystem();
            var options       = new FilesystemStorageOptions();
            var mockFormatter = new Mock <IFormatter <BinaryReader, BinaryWriter> >();

            var entityIO = new FilesystemEntityReaderWriter <long, BinaryReader, BinaryWriter>(
                filesystem,
                mockFormatter.Object,
                options
                );

            using (var stream = entityIO.BeginWrite(key)) stream.Write("mock");
            Assert.True(entityIO.Exists(key));
            var keys = entityIO.GetAllKeys();

            Assert.Contains(key, keys);
            Assert.Single(keys);

            mockFormatter.Verify(mock => mock.Deserialize(
                                     It.IsAny <Type>(),
                                     It.IsAny <BinaryReader>(),
                                     null
                                     ), Times.Never());
        }
        public void TestNumericKeyWithPartitioning(string directory)
        {
            int key1     = 123;
            int key2     = 1123;
            var basePath = FileSystemPath.Root.AppendDirectory(directory);
            var options  = new FilesystemStorageOptions()
            {
                BasePath             = basePath,
                EntitiesPerDirectory = 1000
            };

            var expected1 = basePath.AppendDirectory("0")
                            .AppendDirectory(key1.ToString())
                            .AppendFile(EntityFilename);

            var actual1 = FilesystemLocator.LocateEntityFile(key1, options);

            Assert.Equal(expected1, actual1);

            var expected2 = basePath.AppendDirectory("1")
                            .AppendDirectory(key2.ToString())
                            .AppendFile(EntityFilename);

            var actual2 = FilesystemLocator.LocateEntityFile(key2, options);

            Assert.Equal(expected2, actual2);
        }
Esempio n. 3
0
        internal override ISnapshotManager <long, SampleData> Build()
        {
            var options    = new FilesystemStorageOptions();
            var filesystem = new MemoryFileSystem();

            return(new SingleFileSnapshotManager <long, SampleData, XmlDocumentAdapter, XmlWriterAdapter>(
                       options, new XmlFormatter(), filesystem));
        }
Esempio n. 4
0
 /// <summary>
 /// Sets up the engine to use the last-in-first-out optimized snapshot storage,
 /// suitable for fast reading of recent data.
 /// </summary>
 /// <param name="formatter">
 /// Specifies a <see cref="IFormatter<TInputStream, TOutputStream>"/> which
 /// will be used for serialization and deserialization.
 /// </param>
 /// <param name="options">
 /// If not null, overrides the default <see cref="FilesystemStorageOptions"/>.
 /// </param>
 /// <param name="zeroPaddingBytes">
 /// Specifies maximum free space at the start of the file for future
 /// snapshots. When there is no free space left for writing, the file
 /// expands by specified byte count and rewrites.
 /// <para>
 /// If the number is too small, rewrites may appear often.
 /// </para>
 /// <para>
 /// If the number is big, files will be bigger and may consist
 /// of big unused space depending on how many data is being written.
 /// </para>
 /// </param>
 public DiffstoreBuilder <TKey, TValue> WithLastFirstOptimizedSnapshots(
     IFileSystem fileSystem           = null,
     FilesystemStorageOptions options = null,
     int zeroPaddingBytes             = 256)
 {
     (fileSystem, options) = PrepareFileStorage(fileSystem, options);
     sm = new BinaryLIFOSnapshotManager <TKey, TValue>(options,
                                                       fileSystem, zeroPaddingBytes);
     return(this);
 }
 public BinaryLIFOSnapshotManager(FilesystemStorageOptions opts,
                                  IFileSystem fs, int maxPadding)
 {
     (options, fileSystem, zeroPaddingStep) = (opts, fs, maxPadding);
     if (options.MaxSnapshotFileSize % zeroPaddingStep != 0)
     {
         throw new ArgumentException(
                   "MaxSnapshotFileSize must be a multiple of maxPadding");
     }
 }
        internal override ISnapshotManager <long, SampleData> Build()
        {
            var options = new FilesystemStorageOptions()
            {
                MaxSnapshotFileSize = 512
            };
            var filesystem = new MemoryFileSystem();

            return(new BinaryLIFOSnapshotManager <long, SampleData>(
                       options, filesystem, 128));
        }
        public FilesystemEntityReaderWriterTest()
        {
            var filesystem = new MemoryFileSystem();
            var options    = new FilesystemStorageOptions();

            entityIO = new FilesystemEntityReaderWriter <long, BinaryReader, BinaryWriter>(
                filesystem,
                FastBinaryFormatter.Instance,
                options
                );
        }
Esempio n. 8
0
        /// <summary>
        /// Sets up the engine to use file-based entity storage.
        /// </summary>
        /// <param name="formatter">
        /// Specifies a <see cref="IFormatter<TInputStream, TOutputStream>"/> which
        /// will be used for serialization and deserialization.
        /// </param>
        /// <param name="options">
        /// If not null, overrides the default <see cref="FilesystemStorageOptions"/>.
        /// </param>
        /// <param name="fileSystem">
        /// If not null, uses the specified IFileSystem, otherwise uses
        /// what has been called before.
        /// </param>
        public DiffstoreBuilder <TKey, TValue> WithFileBasedEntities <TIn, TOut>(
            IFormatter <TIn, TOut> formatter,
            FilesystemStorageOptions options = null,
            IFileSystem fileSystem           = null)
            where TIn : IDisposable
            where TOut : IDisposable
        {
            (fileSystem, options) = PrepareFileStorage(fileSystem, options);
            var entityIO = new FilesystemEntityReaderWriter <TKey, TIn, TOut>(
                fileSystem, formatter, options);

            em = new EntityManager <TKey, TValue, TIn, TOut>(formatter, entityIO);
            return(this);
        }
        public void TestKeyfileLocation(string directory)
        {
            int key      = 123;
            var basePath = FileSystemPath.Root.AppendDirectory(directory);
            var options  = new FilesystemStorageOptions()
            {
                BasePath             = basePath,
                EntitiesPerDirectory = 0
            };
            var expected = basePath.AppendDirectory(key.ToString()).AppendFile(KeyFilename);
            var actual   = FilesystemLocator.LocateKeyFile(key, options);

            Assert.Equal(expected, actual);
        }
Esempio n. 10
0
        internal override IEntityManager <long, SampleData> Build()
        {
            var filesystem = new MemoryFileSystem();
            var formatter  = FastBinaryFormatter.Instance;
            var options    = new FilesystemStorageOptions()
            {
                EntitiesPerDirectory = 1000
            };

            var entityIO = new FilesystemEntityReaderWriter <long, BinaryReader, BinaryWriter>
                               (filesystem, formatter, options);

            return(new EntityManager <long, SampleData, BinaryReader, BinaryWriter>
                       (formatter, entityIO));
        }
Esempio n. 11
0
        public void TestNonPartitionableKey(string directory, int entitiesPerDir)
        {
            var key      = "Hello World";
            var basePath = FileSystemPath.Root.AppendDirectory(directory);
            var options  = new FilesystemStorageOptions()
            {
                BasePath             = basePath,
                EntitiesPerDirectory = entitiesPerDir
            };

            var expected = basePath.AppendDirectory(key.GetHashCode().ToString())
                           .AppendFile(EntityFilename);

            var actual = FilesystemLocator.LocateEntityFile(key, options);

            Assert.Equal(expected, actual);
        }
Esempio n. 12
0
        private (IFileSystem, FilesystemStorageOptions) PrepareFileStorage(
            IFileSystem fileSystem, FilesystemStorageOptions options)
        {
            if (options == null)
            {
                options = new FilesystemStorageOptions();
            }
            if (fileSystem == null)
            {
                fileSystem = this.fileSystem;
            }

            if (fileSystem == null)
            {
                throw new InvalidOperationException(
                          "To use file-based snapshots, select the filesystem type " +
                          "by calling either InMemory() or OnDisk(...)"
                          );
            }
            return(fileSystem, options);
        }
Esempio n. 13
0
        /// <summary>
        /// Sets up the engine to use file-based entity storage.
        /// </summary>
        /// <param name="format">
        /// Specifies the <see cref="FileFormat"/> which will be used for
        /// serialization and deserialization.
        /// </param>
        /// <param name="options">
        /// If not null, overrides the default <see cref="FilesystemStorageOptions"/>.
        /// </param>
        /// <param name="fileSystem">
        /// If not null, uses the specified IFileSystem, otherwise uses
        /// what has been called before.
        /// </param>
        public DiffstoreBuilder <TKey, TValue> WithFileBasedEntities(
            FileFormat format,
            FilesystemStorageOptions options = null,
            IFileSystem fileSystem           = null)
        {
            (fileSystem, options) = PrepareFileStorage(fileSystem, options);

            switch (format)
            {
            case FileFormat.Binary:
                return(WithFileBasedEntities(FastBinaryFormatter.Instance, options, fileSystem));

            case FileFormat.XML:
                return(WithFileBasedEntities(XmlFormatter.Instance, options, fileSystem));

            case FileFormat.JSON:
                return(WithFileBasedEntities(JsonFormatter.Instance, options, fileSystem));

            default:
                throw new InvalidOperationException("Unsupported format");
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Sets up the engine to use the single-file-per-snapshot storage.
        /// Suitable for big entities and/or when they need to be human-readable.
        /// </summary>
        /// <param name="format">
        /// Specifies the <see cref="FileFormat"/> which will be used for
        /// serialization and deserialization.
        /// </param>
        /// <param name="options">
        /// If not null, overrides the default <see cref="FilesystemStorageOptions"/>.
        /// </param>
        /// <param name="fileSystem">
        /// If not null, uses the specified IFileSystem, otherwise uses
        /// what has been called before.
        /// </param>
        public DiffstoreBuilder <TKey, TValue> WithSingleFileSnapshots(
            FileFormat format,
            IFileSystem fileSystem           = null,
            FilesystemStorageOptions options = null)
        {
            (fileSystem, options) = PrepareFileStorage(fileSystem, options);

            switch (format)
            {
            case FileFormat.XML:
                sm = new SingleFileSnapshotManager <TKey, TValue, XmlDocumentAdapter, XmlWriterAdapter>
                         (options, new XmlFormatter(), fileSystem); break;

            case FileFormat.JSON:
                sm = new SingleFileSnapshotManager <TKey, TValue, JsonReaderAdapter, JsonWriterAdapter>
                         (options, JsonFormatter.Instance, fileSystem); break;

            default:
                sm = new SingleFileSnapshotManager <TKey, TValue, BinaryReader, BinaryWriter>
                         (options, FastBinaryFormatter.Instance, fileSystem); break;
            }

            return(this);
        }
Esempio n. 15
0
 /// <summary>
 /// Sets up the engine to use file-based entity storage.
 /// </summary>
 /// <param name="options">
 /// If not null, overrides the default <see cref="FilesystemStorageOptions"/>.
 /// </param>
 /// <param name="fileSystem">
 /// If not null, uses the specified IFileSystem, otherwise uses
 /// what has been called before.
 /// </param>
 public DiffstoreBuilder <TKey, TValue> WithFileBasedEntities(
     FilesystemStorageOptions options = null,
     IFileSystem fileSystem           = null)
 {
     return(WithFileBasedEntities(FastBinaryFormatter.Instance, options, fileSystem));
 }
 public SingleFileSnapshotManager(FilesystemStorageOptions opts,
                                  IFormatter <TIn, TOut> format,
                                  IFileSystem fs) =>
 (options, fileSystem, formatter) = (opts, fs, format);