Ejemplo n.º 1
0
        public unsafe VarLenHeapContainer(ref T obj, IVariableLengthStruct <T> varLenStruct, SectorAlignedBufferPool pool)
        {
            var len = varLenStruct.GetLength(ref obj);

            mem = pool.Get(len);
            Buffer.MemoryCopy(Unsafe.AsPointer(ref obj), mem.GetValidPointer(), len, len);
        }
Ejemplo n.º 2
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>;
                }
            }
        }
Ejemplo n.º 3
0
        public VariableLengthBlittableAllocator(LogSettings settings, VariableLengthStructSettings <Key, Value> vlSettings, IFasterEqualityComparer <Key> comparer, Action <long, long> evictCallback = null, LightEpoch epoch = null, Action <CommitInfo> flushCallback = null)
            : base(settings, comparer, evictCallback, epoch, flushCallback)
        {
            values   = new byte[BufferSize][];
            handles  = new GCHandle[BufferSize];
            pointers = new long[BufferSize];

            ptrHandle      = GCHandle.Alloc(pointers, GCHandleType.Pinned);
            nativePointers = (long *)ptrHandle.AddrOfPinnedObject();

            KeyLength   = vlSettings.keyLength;
            ValueLength = vlSettings.valueLength;

            if (KeyLength == null)
            {
                fixedSizeKey = true;
                KeyLength    = new FixedLengthStruct <Key>();
            }

            if (ValueLength == null)
            {
                fixedSizeValue = true;
                ValueLength    = new FixedLengthStruct <Value>();
            }
        }
Ejemplo n.º 4
0
        public unsafe VarLenHeapContainer(ref T obj, IVariableLengthStruct <T> varLenStruct, SectorAlignedBufferPool pool)
        {
            this.varLenStruct = varLenStruct;
            var len = varLenStruct.GetLength(ref obj);

            mem = pool.Get(len);
            varLenStruct.Serialize(ref obj, mem.GetValidPointer());
        }
Ejemplo n.º 5
0
 public void Copy(ref MyValue src, ref MyValue dst, IVariableLengthStruct <MyValue> valueLength)
 {
     if (src.value == 21)
     {
         CopyCalled = true;
     }
     dst = src;
 }
 public void Copy(ref ValueStruct src, ref ValueStruct dst, IVariableLengthStruct <ValueStruct> valueLength)
 {
     if (src.vfield1 == 11 && src.vfield2 == 21)
     {
         CopyCalled = true;
     }
     dst = src;
 }
Ejemplo n.º 7
0
        public void Copy(ref Value src, ref Value dst, IVariableLengthStruct <Value> valueLength)
        {
            var srcLength = valueLength.GetLength(ref src);

            Buffer.MemoryCopy(
                Unsafe.AsPointer(ref src),
                Unsafe.AsPointer(ref dst),
                srcLength,
                srcLength);
        }
Ejemplo n.º 8
0
 public bool CopyInPlace(ref Memory <T> src, ref Memory <T> dst, IVariableLengthStruct <Memory <T> > valueLength)
 {
     if (src.Length > dst.Length)
     {
         return(false);
     }
     src.CopyTo(dst);
     dst.ShrinkSerializedLength(src.Length);
     return(true);
 }
Ejemplo n.º 9
0
 /// <inheritdoc />
 public FasterKV(long size, Functions functions, LogSettings logSettings,
                 CheckpointSettings checkpointSettings  = null, SerializerSettings <Key, Value> serializerSettings = null,
                 IFasterEqualityComparer <Key> comparer = null,
                 VariableLengthStructSettings <Key, Value> variableLengthStructSettings = null, IVariableLengthStruct <Value, Input> variableLengthStructForInput = null)
 {
     _functions = functions;
     _fasterKV  = new FasterKV <Key, Value>(size, logSettings, checkpointSettings, serializerSettings, comparer, variableLengthStructSettings);
     _variableLengthStructForInput = variableLengthStructForInput;
     if (_fasterKV.hlog is VariableLengthBlittableAllocator <Key, Value> allocator && _variableLengthStructForInput == default)
     {
         _variableLengthStructForInput = new DefaultVariableLengthStruct <Value, Input>(allocator.ValueLength);
     }
 }
Ejemplo n.º 10
0
        public bool CopyInPlace(ref Value src, ref Value dst, IVariableLengthStruct <Value> valueLength)
        {
            var srcLength = valueLength.GetLength(ref src);
            var dstLength = valueLength.GetLength(ref dst);

            if (srcLength != dstLength)
            {
                return(false);
            }

            Buffer.MemoryCopy(
                Unsafe.AsPointer(ref src),
                Unsafe.AsPointer(ref dst),
                dstLength,
                srcLength);

            return(true);
        }
Ejemplo n.º 11
0
        public VariableLengthBlittableAllocator(LogSettings settings, VariableLengthStructSettings <Key, Value> vlSettings, IFasterEqualityComparer <Key> comparer, Action <long, long> evictCallback = null, LightEpoch epoch = null, Action <CommitInfo> flushCallback = null)
            : base(settings, comparer, evictCallback, epoch, flushCallback)
        {
            overflowPagePool = new OverflowPool <PageUnit>(4, p =>
#if NET5_0_OR_GREATER
                                                           { }
#else
                                                           p.handle.Free()
#endif
                                                           );

            if (BufferSize > 0)
            {
                values = new byte[BufferSize][];

#if NET5_0_OR_GREATER
                pointers       = GC.AllocateArray <long>(BufferSize, true);
                nativePointers = (long *)Unsafe.AsPointer(ref pointers[0]);
#else
                pointers       = new long[BufferSize];
                handles        = new GCHandle[BufferSize];
                ptrHandle      = GCHandle.Alloc(pointers, GCHandleType.Pinned);
                nativePointers = (long *)ptrHandle.AddrOfPinnedObject();
#endif
            }

            KeyLength   = vlSettings.keyLength;
            ValueLength = vlSettings.valueLength;

            if (KeyLength == null)
            {
                fixedSizeKey = true;
                KeyLength    = new FixedLengthStruct <Key>();
            }

            if (ValueLength == null)
            {
                fixedSizeValue = true;
                ValueLength    = new FixedLengthStruct <Value>();
            }
        }
Ejemplo n.º 12
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)
            {
                if (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;

            // Session runs on a single thread
            if (!supportAsync)
            {
                UnsafeResumeThread();
            }
        }
 /// <summary>
 /// Default instance of object
 /// </summary>
 /// <param name="variableLengthStruct"></param>
 public DefaultVariableLengthStruct(IVariableLengthStruct <T> variableLengthStruct)
 {
     this.variableLengthStruct = variableLengthStruct;
 }
Ejemplo n.º 14
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();
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Resume (continue) prior client session with FASTER, used during
 /// recovery from failure.
 /// For performance reasons this overload is not recommended if functions is value type (struct).
 /// </summary>
 /// <param name="functions">Callback functions</param>
 /// <param name="sessionId">ID/name of previous session to resume</param>
 /// <param name="commitPoint">Prior commit point of durability for session</param>
 /// <param name="threadAffinitized">For advanced users. Specifies whether session holds the thread epoch across calls. Do not use with async code. Ensure thread calls session Refresh periodically to move the system epoch forward.</param>
 /// <param name="variableLengthStruct">Implementation of input-specific length computation for variable-length structs</param>
 /// <returns>Session instance</returns>
 public ClientSession <Key, Value, Input, Output, Context, IFunctions <Key, Value, Input, Output, Context> > ResumeSession <Input, Output, Context>(IFunctions <Key, Value, Input, Output, Context> functions, string sessionId, out CommitPoint commitPoint, bool threadAffinitized = false, IVariableLengthStruct <Value, Input> variableLengthStruct = null)
 {
     return(ResumeSession <Input, Output, Context, IFunctions <Key, Value, Input, Output, Context> >(functions, sessionId, out commitPoint, threadAffinitized, variableLengthStruct));
 }
Ejemplo n.º 16
0
 public bool CopyInPlace(ref MyValue src, ref MyValue dst, IVariableLengthStruct <MyValue> valueLength)
 {
     dst = src;
     return(true);
 }
Ejemplo n.º 17
0
 public void Copy(ref MyValue src, ref MyValue dst, IVariableLengthStruct <MyValue> valueLength)
 {
     dst = src;
 }
Ejemplo n.º 18
0
 public bool CopyInPlace(ref MyValue src, ref MyValue dst, IVariableLengthStruct <MyValue> valueLength)
 {
     return(false);
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Resume (continue) prior client session with FASTER, used during
        /// recovery from failure.
        /// </summary>
        /// <param name="functions">Callback functions</param>
        /// <param name="sessionId">ID/name of previous session to resume</param>
        /// <param name="commitPoint">Prior commit point of durability for session</param>
        /// <param name="threadAffinitized">For advanced users. Specifies whether session holds the thread epoch across calls. Do not use with async code. Ensure thread calls session Refresh periodically to move the system epoch forward.</param>
        /// <param name="variableLengthStruct">Implementation of input-specific length computation for variable-length structs</param>
        /// <returns>Session instance</returns>

        public ClientSession <Key, Value, Input, Output, Context, Functions> ResumeSession <Input, Output, Context, Functions>(Functions functions, string sessionId, out CommitPoint commitPoint, bool threadAffinitized = false, IVariableLengthStruct <Value, Input> variableLengthStruct = null)
            where Functions : IFunctions <Key, Value, Input, Output, Context>
        {
            if (functions == null)
            {
                throw new ArgumentNullException(nameof(functions));
            }

            if (!threadAffinitized)
            {
                UseRelaxedCPR();
            }

            commitPoint = InternalContinue <Input, Output, Context>(sessionId, out var ctx);
            if (commitPoint.UntilSerialNo == -1)
            {
                throw new Exception($"Unable to find session {sessionId} to recover");
            }


            var session = new ClientSession <Key, Value, Input, Output, Context, Functions>(this, ctx, functions, !threadAffinitized, variableLengthStruct);

            if (_activeSessions == null)
            {
                Interlocked.CompareExchange(ref _activeSessions, new Dictionary <string, IClientSession>(), null);
            }
            lock (_activeSessions)
                _activeSessions.Add(sessionId, session);
            return(session);
        }
Ejemplo n.º 20
0
 public KeyValueStore(string folder, int logSizeBits, IFasterEqualityComparer <TKey> keyComparer, TFunctions functions, IVariableLengthStruct <TKey> keyLength, IVariableLengthStruct <TValue> valueLength)
 {
     _dataFolder          = folder;
     _logSizeBits         = logSizeBits;
     _keyComparer         = keyComparer;
     _functions           = functions;
     _variableLenSettings = new VariableLengthStructSettings <TKey, TValue>
     {
         keyLength   = keyLength,
         valueLength = valueLength
     };
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Start a new client session with FASTER.
 /// For performance reasons this overload is not recommended if functions is value type (struct).
 /// </summary>
 /// <param name="functions">Callback functions</param>
 /// <param name="sessionId">ID/name of session (auto-generated if not provided)</param>
 /// <param name="threadAffinitized">For advanced users. Specifies whether session holds the thread epoch across calls. Do not use with async code. Ensure thread calls session Refresh periodically to move the system epoch forward.</param>
 /// <param name="variableLengthStruct">Implementation of input-specific length computation for variable-length structs</param>
 /// <returns>Session instance</returns>
 public ClientSession <Key, Value, Input, Output, Context, IFunctions <Key, Value, Input, Output, Context> > NewSession <Input, Output, Context>(IFunctions <Key, Value, Input, Output, Context> functions, string sessionId = null, bool threadAffinitized = false, IVariableLengthStruct <Value, Input> variableLengthStruct = null)
 {
     return(NewSession <Input, Output, Context, IFunctions <Key, Value, Input, Output, Context> >(functions, sessionId, threadAffinitized, variableLengthStruct));
 }
Ejemplo n.º 22
0
 public void Copy(ref Memory <T> src, ref Memory <T> dst, IVariableLengthStruct <Memory <T> > valueLength)
 {
     src.CopyTo(dst);
 }
Ejemplo n.º 23
0
            /// <summary>
            /// Resume (continue) prior client session with FASTER, used during
            /// recovery from failure.
            /// </summary>
            /// <param name="sessionId">ID/name of previous session to resume</param>
            /// <param name="commitPoint">Prior commit point of durability for session</param>
            /// <param name="threadAffinitized">For advanced users. Specifies whether session holds the thread epoch across calls. Do not use with async code. Ensure thread calls session Refresh periodically to move the system epoch forward.</param>
            /// <param name="variableLengthStruct">Implementation of input-specific length computation for variable-length structs</param>
            /// <returns>Session instance</returns>
            public ClientSession <Key, Value, Input, Output, Context, Functions> ResumeSession <Functions>(string sessionId, out CommitPoint commitPoint, bool threadAffinitized = false, IVariableLengthStruct <Value, Input> variableLengthStruct = null)
                where Functions : IFunctions <Key, Value, Input, Output, Context>
            {
                if (_functions == null)
                {
                    throw new FasterException("Functions not provided for session");
                }

                return(_fasterKV.ResumeSession <Input, Output, Context, Functions>((Functions)_functions, sessionId, out commitPoint, threadAffinitized, variableLengthStruct));
            }
 public void Copy(ref ValueStruct src, ref ValueStruct dst, IVariableLengthStruct <ValueStruct> valueLength)
 {
     dst = src;
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Start a new client session with FASTER.
        /// </summary>
        /// <param name="functions">Callback functions</param>
        /// <param name="sessionId">ID/name of session (auto-generated if not provided)</param>
        /// <param name="threadAffinitized">For advanced users. Specifies whether session holds the thread epoch across calls. Do not use with async code. Ensure thread calls session Refresh periodically to move the system epoch forward.</param>
        /// <param name="variableLengthStruct">Implementation of input-specific length computation for variable-length structs</param>
        /// <returns>Session instance</returns>
        public ClientSession <Key, Value, Input, Output, Context, Functions> NewSession <Input, Output, Context, Functions>(Functions functions, string sessionId = null, bool threadAffinitized = false, IVariableLengthStruct <Value, Input> variableLengthStruct = null)
            where Functions : IFunctions <Key, Value, Input, Output, Context>
        {
            if (functions == null)
            {
                throw new ArgumentNullException(nameof(functions));
            }

            if (!threadAffinitized)
            {
                UseRelaxedCPR();
            }

            if (sessionId == null)
            {
                sessionId = Guid.NewGuid().ToString();
            }
            var ctx = new FasterExecutionContext <Input, Output, Context>();

            InitContext(ctx, sessionId);
            var prevCtx = new FasterExecutionContext <Input, Output, Context>();

            InitContext(prevCtx, sessionId);
            prevCtx.version--;

            ctx.prevCtx = prevCtx;

            if (_activeSessions == null)
            {
                Interlocked.CompareExchange(ref _activeSessions, new Dictionary <string, IClientSession>(), null);
            }

            var session = new ClientSession <Key, Value, Input, Output, Context, Functions>(this, ctx, functions, !threadAffinitized, variableLengthStruct);

            lock (_activeSessions)
                _activeSessions.Add(sessionId, session);
            return(session);
        }
 public bool CopyInPlace(ref ValueStruct src, ref ValueStruct dst, IVariableLengthStruct <ValueStruct> valueLength)
 {
     return(false);
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Resume (continue) prior client session with FASTER, used during
 /// recovery from failure.
 /// </summary>
 /// <param name="functions">Callback functions</param>
 /// <param name="sessionId">ID/name of previous session to resume</param>
 /// <param name="commitPoint">Prior commit point of durability for session</param>
 /// <param name="threadAffinitized">For advanced users. Specifies whether session holds the thread epoch across calls. Do not use with async code. Ensure thread calls session Refresh periodically to move the system epoch forward.</param>
 /// <param name="variableLengthStruct">Implementation of input-specific length computation for variable-length structs</param>
 /// <returns>Session instance</returns>
 public ClientSession <Key, Value, Input, Output, Context, Functions> ResumeSession <Functions>(Functions functions, string sessionId, out CommitPoint commitPoint, bool threadAffinitized = false, IVariableLengthStruct <Value, Input> variableLengthStruct = null)
     where Functions : IFunctions <Key, Value, Input, Output, Context>
 {
     return(_fasterKV.ResumeSession <Input, Output, Context, Functions>(functions, sessionId, out commitPoint, threadAffinitized, variableLengthStruct));
 }
 public bool CopyInPlace(ref ValueStruct src, ref ValueStruct dst, IVariableLengthStruct <ValueStruct> valueLength)
 {
     dst = src;
     return(true);
 }