예제 #1
0
파일: FASTER.cs 프로젝트: microsoft/FASTER
        private static void UpdateVarLen(ref VariableLengthStructSettings <Key, Value> variableLengthStructSettings)
        {
            if (typeof(Key) == typeof(SpanByte))
            {
                if (variableLengthStructSettings == null)
                {
                    variableLengthStructSettings = new VariableLengthStructSettings <SpanByte, Value>() as VariableLengthStructSettings <Key, Value>;
                }

                if (variableLengthStructSettings.keyLength == null)
                {
                    (variableLengthStructSettings as VariableLengthStructSettings <SpanByte, Value>).keyLength = new SpanByteVarLenStruct();
                }
            }
            else if (typeof(Key).IsGenericType && (typeof(Key).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Key).GetGenericArguments()[0]))
            {
                if (variableLengthStructSettings == null)
                {
                    variableLengthStructSettings = new VariableLengthStructSettings <Key, Value>();
                }

                if (variableLengthStructSettings.keyLength == null)
                {
                    var    m = typeof(MemoryVarLenStruct <>).MakeGenericType(typeof(Key).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    variableLengthStructSettings.keyLength = o as IVariableLengthStruct <Key>;
                }
            }
            else if (typeof(Key).IsGenericType && (typeof(Key).GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) && Utility.IsBlittableType(typeof(Key).GetGenericArguments()[0]))
            {
                if (variableLengthStructSettings == null)
                {
                    variableLengthStructSettings = new VariableLengthStructSettings <Key, Value>();
                }

                if (variableLengthStructSettings.keyLength == null)
                {
                    var    m = typeof(ReadOnlyMemoryVarLenStruct <>).MakeGenericType(typeof(Key).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    variableLengthStructSettings.keyLength = o as IVariableLengthStruct <Key>;
                }
            }

            if (typeof(Value) == typeof(SpanByte))
            {
                if (variableLengthStructSettings == null)
                {
                    variableLengthStructSettings = new VariableLengthStructSettings <Key, SpanByte>() as VariableLengthStructSettings <Key, Value>;
                }

                if (variableLengthStructSettings.valueLength == null)
                {
                    (variableLengthStructSettings as VariableLengthStructSettings <Key, SpanByte>).valueLength = new SpanByteVarLenStruct();
                }
            }
            else if (typeof(Value).IsGenericType && (typeof(Value).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Value).GetGenericArguments()[0]))
            {
                if (variableLengthStructSettings == null)
                {
                    variableLengthStructSettings = new VariableLengthStructSettings <Key, Value>();
                }

                if (variableLengthStructSettings.valueLength == null)
                {
                    var    m = typeof(MemoryVarLenStruct <>).MakeGenericType(typeof(Value).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    variableLengthStructSettings.valueLength = o as IVariableLengthStruct <Value>;
                }
            }
            else if (typeof(Value).IsGenericType && (typeof(Value).GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) && Utility.IsBlittableType(typeof(Value).GetGenericArguments()[0]))
            {
                if (variableLengthStructSettings == null)
                {
                    variableLengthStructSettings = new VariableLengthStructSettings <Key, Value>();
                }

                if (variableLengthStructSettings.valueLength == null)
                {
                    var    m = typeof(ReadOnlyMemoryVarLenStruct <>).MakeGenericType(typeof(Value).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    variableLengthStructSettings.valueLength = o as IVariableLengthStruct <Value>;
                }
            }
        }
예제 #2
0
        internal ClientSession(
            FasterKV <Key, Value> fht,
            FasterKV <Key, Value> .FasterExecutionContext <Input, Output, Context> ctx,
            Functions functions,
            bool supportAsync,
            SessionVariableLengthStructSettings <Value, Input> sessionVariableLengthStructSettings = null)
        {
            this.fht          = fht;
            this.ctx          = ctx;
            this.functions    = functions;
            SupportAsync      = supportAsync;
            LatestCommitPoint = new CommitPoint {
                UntilSerialNo = -1, ExcludedSerialNos = null
            };
            FasterSession = new AsyncFasterSession(this);

            this.variableLengthStruct = sessionVariableLengthStructSettings?.valueLength;
            if (this.variableLengthStruct == default)
            {
                UpdateVarlen(ref this.variableLengthStruct);

                if ((this.variableLengthStruct == default) && (fht.hlog is VariableLengthBlittableAllocator <Key, Value> allocator))
                {
                    Debug.WriteLine("Warning: Session did not specify Input-specific functions for variable-length values via IVariableLengthStruct<Value, Input>");
                    this.variableLengthStruct = new DefaultVariableLengthStruct <Value, Input>(allocator.ValueLength);
                }
            }
            else
            {
                if (!(fht.hlog is VariableLengthBlittableAllocator <Key, Value>))
                {
                    Debug.WriteLine("Warning: Session param of variableLengthStruct provided for non-varlen allocator");
                }
            }

            this.inputVariableLengthStruct = sessionVariableLengthStructSettings?.inputLength;

            if (inputVariableLengthStruct == default)
            {
                if (typeof(Input) == typeof(SpanByte))
                {
                    inputVariableLengthStruct = new SpanByteVarLenStruct() as IVariableLengthStruct <Input>;
                }
                else if (typeof(Input).IsGenericType && (typeof(Input).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Input).GetGenericArguments()[0]))
                {
                    var    m = typeof(MemoryVarLenStruct <>).MakeGenericType(typeof(Input).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    inputVariableLengthStruct = o as IVariableLengthStruct <Input>;
                }
                else if (typeof(Input).IsGenericType && (typeof(Input).GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) && Utility.IsBlittableType(typeof(Input).GetGenericArguments()[0]))
                {
                    var    m = typeof(ReadOnlyMemoryVarLenStruct <>).MakeGenericType(typeof(Input).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    inputVariableLengthStruct = o as IVariableLengthStruct <Input>;
                }
            }

            // Session runs on a single thread
            if (!supportAsync)
            {
                UnsafeResumeThread();
            }
        }
예제 #3
0
        public static IFasterEqualityComparer <T> Get <T>()
        {
            var t = typeof(T);

            if (t == typeof(string))
            {
                return(new StringFasterEqualityComparer() as IFasterEqualityComparer <T>);
            }
            else if (t == typeof(byte[]))
            {
                return(new ByteArrayFasterEqualityComparer() as IFasterEqualityComparer <T>);
            }
            else if (t == typeof(long))
            {
                return(new LongFasterEqualityComparer() as IFasterEqualityComparer <T>);
            }
            else if (t == typeof(int))
            {
                return(new IntFasterEqualityComparer() as IFasterEqualityComparer <T>);
            }
            else if (t == typeof(Guid))
            {
                return(new GuidFasterEqualityComparer() as IFasterEqualityComparer <T>);
            }
            else if (t == typeof(SpanByte))
            {
                return(new SpanByteComparer() as IFasterEqualityComparer <T>);
            }
            else if (t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(t.GetGenericArguments()[0]))
            {
                var    m = typeof(MemoryComparer <>).MakeGenericType(t.GetGenericArguments());
                object o = Activator.CreateInstance(m);
                return(o as IFasterEqualityComparer <T>);
            }
            else if (t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) && Utility.IsBlittableType(t.GetGenericArguments()[0]))
            {
                var    m = typeof(ReadOnlyMemoryComparer <>).MakeGenericType(t.GetGenericArguments());
                object o = Activator.CreateInstance(m);
                return(o as IFasterEqualityComparer <T>);
            }
            else
            {
                Debug.WriteLine("***WARNING*** Creating default FASTER key equality comparer based on potentially slow EqualityComparer<Key>.Default. To avoid this, provide a comparer (IFasterEqualityComparer<Key>) as an argument to FASTER's constructor, or make Key implement the interface IFasterEqualityComparer<Key>");
                return(DefaultFasterEqualityComparer <T> .Default);
            }
        }
예제 #4
0
        private void UpdateVarlen(ref IVariableLengthStruct <Value, Input> variableLengthStruct)
        {
            if (!(fht.hlog is VariableLengthBlittableAllocator <Key, Value>))
            {
                return;
            }

            if (typeof(Value) == typeof(SpanByte) && typeof(Input) == typeof(SpanByte))
            {
                variableLengthStruct = new SpanByteVarLenStructForSpanByteInput() as IVariableLengthStruct <Value, Input>;
            }
            else if (typeof(Value).IsGenericType && (typeof(Value).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Value).GetGenericArguments()[0]))
            {
                if (typeof(Input).IsGenericType && (typeof(Input).GetGenericTypeDefinition() == typeof(Memory <>)) && typeof(Input).GetGenericArguments()[0] == typeof(Value).GetGenericArguments()[0])
                {
                    var    m = typeof(MemoryVarLenStructForMemoryInput <>).MakeGenericType(typeof(Value).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    variableLengthStruct = o as IVariableLengthStruct <Value, Input>;
                }
                else if (typeof(Input).IsGenericType && (typeof(Input).GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) && typeof(Input).GetGenericArguments()[0] == typeof(Value).GetGenericArguments()[0])
                {
                    var    m = typeof(MemoryVarLenStructForReadOnlyMemoryInput <>).MakeGenericType(typeof(Value).GetGenericArguments());
                    object o = Activator.CreateInstance(m);
                    variableLengthStruct = o as IVariableLengthStruct <Value, Input>;
                }
            }
        }
예제 #5
0
        public long Compact(long untilAddress, bool shiftBeginAddress)
        {
            if (allocator is VariableLengthBlittableAllocator <Key, Value> varLen)
            {
                if (typeof(Key).IsGenericType && (typeof(Key).GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) && Utility.IsBlittableType(typeof(Key).GetGenericArguments()[0]) &&
                    typeof(Value).IsGenericType && (typeof(Value).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Value).GetGenericArguments()[0]))
                {
                    MethodInfo method  = GetType().GetMethod("CompactReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
                    MethodInfo generic = method.MakeGenericMethod(typeof(Key).GetGenericArguments()[0]);
                    return((long)generic.Invoke(this, new object[] { untilAddress, shiftBeginAddress }));
                }
                else if (typeof(Key).IsGenericType && (typeof(Key).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Key).GetGenericArguments()[0]) &&
                         typeof(Value).IsGenericType && (typeof(Value).GetGenericTypeDefinition() == typeof(Memory <>)) && Utility.IsBlittableType(typeof(Value).GetGenericArguments()[0]))
                {
                    MethodInfo method  = GetType().GetMethod("CompactMemory", BindingFlags.NonPublic | BindingFlags.Instance);
                    MethodInfo generic = method.MakeGenericMethod(typeof(Key).GetGenericArguments()[0]);
                    return((long)generic.Invoke(this, new object[] { untilAddress, shiftBeginAddress }));
                }
                else
                {
                    var functions = new LogVariableCompactFunctions <Key, Value, DefaultVariableCompactionFunctions <Key, Value> >(varLen, default);
                    var variableLengthStructSettings = new VariableLengthStructSettings <Key, Value>
                    {
                        keyLength   = varLen.KeyLength,
                        valueLength = varLen.ValueLength,
                    };

                    return(Compact(functions, default(DefaultVariableCompactionFunctions <Key, Value>), untilAddress, variableLengthStructSettings, shiftBeginAddress));
                }
            }
            else
            {
                return(Compact(new LogCompactFunctions <Key, Value, DefaultCompactionFunctions <Key, Value> >(default), default(DefaultCompactionFunctions <Key, Value>), untilAddress, null, shiftBeginAddress));