Beispiel #1
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 IKeyValueStorage <KeyOf <TKey>, ValueOf <TValue> > CreateRadixTreeStorage <TKey, TValue>(
            Func <TKey, byte[]> serializeKey,
            Func <byte[], TKey> deserializeKey,
            Func <TValue, byte[]> serializeValue,
            Func <byte[], TValue> deserializeValue,
            RadixTreeStorageSettings settings)
        {
            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(CreateRadixTreeStorage(new Serializer <TKey>(serializeKey, deserializeKey),
                                          new Serializer <TValue>(serializeValue, deserializeValue), settings));
        }
Beispiel #2
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 IKeyValueStorage <KeyOf <TKey>, ValueOf <byte[]> > CreateRadixTreeByteArrayStorage <TKey>(
     Func <TKey, byte[]> serializeKey,
     Func <byte[], TKey> deserializeKey,
     RadixTreeStorageSettings settings)
 {
     return(CreateRadixTreeStorage(serializeKey, deserializeKey, p => p, p => p, settings));
 }
Beispiel #3
0
        /// <summary>
        /// Creates a new storage instance with specified types of keys using built-in serialization routines.
        /// 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="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 IKeyValueStorage <KeyOf <TKey>, ValueOf <TValue> > CreateRadixTreeStorage <TKey, TValue>(
            Func <TValue, byte[]> serializeValue,
            Func <byte[], TValue> deserializeValue,
            RadixTreeStorageSettings settings)
        {
            var keys = BuiltInKeySerializers();
            var type = typeof(TKey);

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

            return(CreateRadixTreeStorage((ISerializer <TKey>)keys[type], new Serializer <TValue>(serializeValue, deserializeValue), settings));
        }
        private IRadixTreeKeyValueStorage <KeyOf <String>, ValueOf <String> > GetStorage()
        {
            var factory = new StorageFactory();

            var settings = RadixTreeStorageSettings.Default();

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

            return((IRadixTreeKeyValueStorage <KeyOf <String>, ValueOf <String> >)factory.CreateRadixTreeStorage(
                       p => Encoding.UTF8.GetBytes(p),
                       p => Encoding.UTF8.GetString(p),
                       p => Encoding.UTF8.GetBytes(p),
                       p => Encoding.UTF8.GetString(p),
                       settings));
        }
        private static IKeyValueStorage <KeyOf <string>, ValueOf <byte[]> > GetByteArrayStorage()
        {
            var factory = new StorageFactory();

            return(factory.CreateRadixTreeByteArrayStorage <string>(RadixTreeStorageSettings.Default()));
        }
Beispiel #6
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 IKeyValueStorage <KeyOf <TKey>, ValueOf <TValue> > CreateRadixTreeStorage <TKey, TValue>(ISerializer <TKey> keySerializer,
                                                                                                        ISerializer <TValue> valueSerializer,
                                                                                                        RadixTreeStorageSettings settings)
        {
            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 <KeyOf <TKey> >(obj => keySerializer.Serialize(obj), bytes => keySerializer.Deserialize(bytes));
                var vs = new Serializer <ValueOf <TValue> >(obj => valueSerializer.Serialize(obj), bytes => valueSerializer.Deserialize(bytes));

                var radixTree = new RadixTree <KeyOf <TKey>, ValueOf <TValue> >(
                    new RadixTreeNodeStorage(pageManager),
                    new ValueStorage <ValueOf <TValue> >(new MemoryManager(new FreeSpaceMap(pageManager), pageManager), vs), ks);

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

                throw;
            }
        }
Beispiel #7
0
 /// <summary>
 /// Creates a new bytearray storage instance with specified type of keys using built-in serialization routines.
 /// 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 IKeyValueStorage <KeyOf <TKey>, ValueOf <byte[]> > CreateRadixTreeByteArrayStorage <TKey>(
     RadixTreeStorageSettings settings)
 {
     return(CreateRadixTreeStorage <TKey, byte[]>(p => p, p => p, settings));
 }
Beispiel #8
0
        /// <summary>
        /// Creates a new storage instance with specified types of keys using built-in serialization routines.
        /// 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 creating storage</param>
        /// <returns>New storage instance</returns>
        public IKeyValueStorage <KeyOf <TKey>, ValueOf <TValue> > CreateRadixTreeStorage <TKey, TValue>(ISerializer <TValue> valueSerializer, RadixTreeStorageSettings settings)
        {
            var keys = BuiltInKeySerializers();
            var type = typeof(TKey);

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

            return(CreateRadixTreeStorage((ISerializer <TKey>)keys[type], valueSerializer, settings));
        }