示例#1
0
        public void OpenStorageWithWrongPageSizeShouldFail()
        {
            var factory = new StorageFactory();

            using (var storage = factory.CreateBPlusTreeStorage <ComparableKeyOf <Int32>, ValueOf <Int32> >(
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       BPlusTreeStorageSettings.Default()))
            {
                storage.CreateNew(StoragePath);
            }

            var differentSettings = BPlusTreeStorageSettings.Default();

            differentSettings.PageSize = PageSize._8192;

            using (var storage = factory.CreateBPlusTreeStorage <ComparableKeyOf <Int32>, ValueOf <Int32> >(
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       differentSettings))
            {
                storage.OpenExisting(StoragePath);
            }
        }
示例#2
0
        /// <summary>
        /// Creates a new storage instance with specified types of keys and values.
        /// </summary>
        /// <typeparam name="TKey">The type of key</typeparam>
        /// <typeparam name="TValue">The type of value</typeparam>
        /// <param name="serializeKey">Key serialization method</param>
        /// <param name="deserializeKey">Key deserialization method</param>
        /// <param name="serializeValue">Value serialization method</param>
        /// <param name="deserializeValue">Value deserialization method</param>
        /// <param name="settings">Setiings of creating storage</param>
        /// <returns>New storage instance</returns>
        public IBPlusTreeKeyValueStorage <ComparableKeyOf <TKey>, ValueOf <TValue> > CreateBPlusTreeStorage <TKey, TValue>(
            Func <TKey, byte[]> serializeKey,
            Func <byte[], TKey> deserializeKey,
            Func <TValue, byte[]> serializeValue,
            Func <byte[], TValue> deserializeValue,
            BPlusTreeStorageSettings settings)

            where TKey : IComparable
        {
            if (serializeKey == null)
            {
                throw new ArgumentNullException(nameof(serializeKey));
            }

            if (deserializeKey == null)
            {
                throw new ArgumentNullException(nameof(deserializeKey));
            }

            if (serializeValue == null)
            {
                throw new ArgumentNullException(nameof(serializeValue));
            }

            if (deserializeValue == null)
            {
                throw new ArgumentNullException(nameof(deserializeValue));
            }

            return(CreateBPlusTreeStorage(new Serializer <TKey>(serializeKey, deserializeKey),
                                          new Serializer <TValue>(serializeValue, deserializeValue), settings));
        }
示例#3
0
        public void SuccessfulCreateCloseAndOpenStorage()
        {
            var factory = new StorageFactory();

            using (var storage = factory.CreateBPlusTreeStorage <ComparableKeyOf <Int32>, ValueOf <Int32> >(
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       BPlusTreeStorageSettings.Default()))
            {
                storage.CreateNew(StoragePath);
            }

            using (var storage = factory.CreateBPlusTreeStorage <ComparableKeyOf <Int32>, ValueOf <Int32> >(
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       BPlusTreeStorageSettings.Default()))
            {
                storage.OpenExisting(StoragePath);
            }

            using (var storage = factory.CreateBPlusTreeStorage <ComparableKeyOf <Int32>, ValueOf <Int32> >(
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       p => BitConverter.GetBytes(p.Value),
                       p => BitConverter.ToInt32(p, 0),
                       BPlusTreeStorageSettings.Default()))
            {
                storage.OpenOrCreate(StoragePath);
            }
        }
示例#4
0
        /// <summary>
        /// Creates a new bytearray storage instance with specified type of keys using built-in serialization routines for keys.
        /// Supported types of keys are: int, long, uint, ulong, double, float, DateTime, Guid, string and byte[]
        /// </summary>
        /// <typeparam name="TKey">The type of key</typeparam>
        /// <param name="settings">Setiings of creating storage</param>
        /// <returns>New storage instance</returns>
        public IBPlusTreeKeyValueStorage <ComparableKeyOf <TKey>, ValueOf <byte[]> > CreateBPlusTreeByteArrayStorage <TKey>(
            BPlusTreeStorageSettings settings)

            where TKey : IComparable
        {
            return(CreateBPlusTreeStorage <TKey, byte[]>(p => p, p => p, settings));
        }
示例#5
0
        private static IKeyValueStorage <ComparableKeyOf <int>, ValueOf <string> > GetStorage()
        {
            var settings = BPlusTreeStorageSettings.Default(4); // use default settings with 4-byte keys

            settings.CacheSettings.MaxCachedPages = 10000;      // speedup massive insert operations
            settings.CacheSettings.MaxDirtyPages  = 1000;       // by increasing cache size

            return(new StorageFactory().CreateBPlusTreeStorage <int, string>(settings));
        }
示例#6
0
        /// <summary>
        /// Creates a new bytearray storage instance with specified type of keys.
        /// </summary>
        /// <typeparam name="TKey">The type of key</typeparam>
        /// <param name="serializeKey">Key serialization method</param>
        /// <param name="deserializeKey">Key deserialization method</param>
        /// <param name="settings">Setiings of creating storage</param>
        /// <returns>New storage instance</returns>
        public IBPlusTreeKeyValueStorage <ComparableKeyOf <TKey>, ValueOf <byte[]> > CreateBPlusTreeByteArrayStorage <TKey>(
            Func <TKey, byte[]> serializeKey,
            Func <byte[], TKey> deserializeKey,
            BPlusTreeStorageSettings settings)

            where TKey : IComparable
        {
            return(CreateBPlusTreeStorage(serializeKey, deserializeKey, p => p, p => p, settings));
        }
        private static IKeyValueStorage <ComparableKeyOf <int>, ValueOf <int> > GetIntStorage()
        {
            var factory = new StorageFactory();

            return(factory.CreateBPlusTreeStorage <int, int>(
                       BitConverter.GetBytes,
                       p => BitConverter.ToInt32(p, 0),
                       BPlusTreeStorageSettings.Default(sizeof(int))));
        }
示例#8
0
        private static IKeyValueStorage <ComparableKeyOf <int>, ValueOf <string> > GetStorage()
        {
            var settings = BPlusTreeStorageSettings.Default(4); // use default settings with 4-byte keys

            settings.AutoFlushTimeout = TimeSpan.FromMilliseconds(50);

            return(new StorageFactory().CreateBPlusTreeStorage <int, string>(
                       p => Encoding.UTF8.GetBytes(p),   // value serialization
                       p => Encoding.UTF8.GetString(p),  // value deserialization
                       settings));
        }
        private IBPlusTreeKeyValueStorage <ComparableKeyOf <String>, ValueOf <String> > GetStorage()
        {
            var factory = new StorageFactory();

            var settings = BPlusTreeStorageSettings.Default();

            settings.CacheSettings.MaxCachedPages = 3000;
            settings.CacheSettings.MaxDirtyPages  = 2000;
            settings.ForcedWrites = false;
            settings.MaxKeySize   = 16;
            settings.PageSize     = PageSize._4096;

            return((IBPlusTreeKeyValueStorage <ComparableKeyOf <String>, ValueOf <String> >)factory.CreateBPlusTreeStorage(
                       p => Encoding.UTF8.GetBytes(p),
                       p => Encoding.UTF8.GetString(p),
                       p => Encoding.UTF8.GetBytes(p),
                       p => Encoding.UTF8.GetString(p),
                       settings));
        }
示例#10
0
        /// <summary>
        /// Creates a new storage instance with specified type of keys and values using built-in serialization routines for keys.
        /// Supported types of keys are: int, long, uint, ulong, double, float, DateTime, Guid, string and byte[]
        /// </summary>
        /// <typeparam name="TKey">The type of key</typeparam>
        /// <typeparam name="TValue">The type of value</typeparam>
        /// <param name="valueSerializer">Object implementing ISerializer interface for value serialization</param>
        /// <param name="settings">Setiings of creraating storage</param>
        /// <returns>New storage instance</returns>
        public IBPlusTreeKeyValueStorage <ComparableKeyOf <TKey>, ValueOf <TValue> > CreateBPlusTreeStorage <TKey, TValue>(ISerializer <TValue> valueSerializer, BPlusTreeStorageSettings settings)
            where TKey : IComparable
        {
            var keys = BuiltInKeySerializers();
            var type = typeof(TKey);

            if (!keys.ContainsKey(type))
            {
                ThrowNotSupportedType(type, nameof(CreateBPlusTreeStorage));
            }

            return(CreateBPlusTreeStorage((ISerializer <TKey>)keys[type], valueSerializer, settings));
        }
示例#11
0
        /// <summary>
        /// Creates a new storage instance with specified types of keys and values.
        /// </summary>
        /// <typeparam name="TKey">The type of key</typeparam>
        /// <typeparam name="TValue">The type of value</typeparam>
        /// <param name="keySerializer">Object implementing ISerializer interface for key serialization</param>
        /// <param name="valueSerializer">Object implementing ISerializer interface for value serialization</param>
        /// <param name="settings">Setiings of creating storage</param>
        /// <returns>New storage instance</returns>
        public IBPlusTreeKeyValueStorage <ComparableKeyOf <TKey>, ValueOf <TValue> > CreateBPlusTreeStorage <TKey, TValue>(ISerializer <TKey> keySerializer,
                                                                                                                           ISerializer <TValue> valueSerializer,
                                                                                                                           BPlusTreeStorageSettings settings)
            where TKey : IComparable
        {
            bool usePageCache = settings.CacheSettings != null;

            if (settings.MaxEmptyPages < 0)
            {
                throw new ArgumentException("MaxEmptyPages shouldn't be negative", nameof(settings));
            }

            if (usePageCache)
            {
                if (settings.CacheSettings.MaxCachedPages < 0)
                {
                    throw new ArgumentException("MaxCachedPages shouldn't be negative", nameof(settings));
                }

                if (settings.CacheSettings.MaxDirtyPages < 0)
                {
                    throw new ArgumentException("MaxDirtyPages shouldn't be negative", nameof(settings));
                }

                if (settings.CacheSettings.MaxDirtyPages > settings.CacheSettings.MaxCachedPages)
                {
                    throw new ArgumentException("MaxDirtyPages shouldn be equal to or less than MaxCachedPages", nameof(settings));
                }
            }

            IPageManager pageManager   = null;
            IPageManager fsPageManager = null;

            try
            {
                fsPageManager = new FileSystemPageManager((int)settings.PageSize, settings.ForcedWrites, 1, true)
                {
                    MaxEmptyPages = settings.MaxEmptyPages
                };

                pageManager = usePageCache ?
                              new CachingPageManager(fsPageManager, settings.CacheSettings.MaxCachedPages, settings.CacheSettings.MaxDirtyPages)
                                : fsPageManager;

                var ks = new Serializer <ComparableKeyOf <TKey> >(obj => keySerializer.Serialize(obj), bytes => keySerializer.Deserialize(bytes));
                var vs = new Serializer <ValueOf <TValue> >(obj => valueSerializer.Serialize(obj), bytes => valueSerializer.Deserialize(bytes));

                if (settings.MaxKeySize <= 0)
                {
                    throw new ArgumentException("MaxKeySize size should be positive", nameof(settings));
                }

                var bPlusTree = new BPlusTree <ComparableKeyOf <TKey>, ValueOf <TValue> >(
                    new BPlusTreeNodeStorage <ComparableKeyOf <TKey> >(pageManager, ks, settings.MaxKeySize),
                    new ValueStorage <ValueOf <TValue> >(new MemoryManager(new FreeSpaceMap(pageManager), pageManager), vs));

                return(new BPlusTreeKeyValueStorage <ComparableKeyOf <TKey>, ValueOf <TValue> >(pageManager, bPlusTree, settings.MaxKeySize, settings.AutoFlushInterval, settings.AutoFlushTimeout));
            }
            catch (Exception)
            {
                if (pageManager != null)
                {
                    pageManager.Close();
                }
                else
                {
                    fsPageManager?.Close();
                }

                throw;
            }
        }
示例#12
0
 private IKeyValueStorage <ComparableKeyOf <string>, ValueOf <string> > GetStorage()
 {
     return(_storageFactory.CreateBPlusTreeStorage <string, string>(BPlusTreeStorageSettings.Default(255)));
 }
        private static IKeyValueStorage <ComparableKeyOf <int>, ValueOf <byte[]> > GetByteArrayStorage()
        {
            var factory = new StorageFactory();

            return(factory.CreateBPlusTreeByteArrayStorage <int>(BPlusTreeStorageSettings.Default(sizeof(int))));
        }