Code Monkey home page Code Monkey logo

Comments (4)

robotron2084 avatar robotron2084 commented on July 24, 2024 1

That looks exactly like what I need. Thanks!

from dbreeze.

hhblaze avatar hhblaze commented on July 24, 2024

Hi, it is possible

BiserByDefault.zip

from dbreeze.

robotron2084 avatar robotron2084 commented on July 24, 2024

This works well but the GetOrAdd signature used is not available in .net standard 2.0. The below worked for me. I used a Type as the key instead of a string which feels cleaner and I believe should still be concurrent.

    ConcurrentDictionary<Type, MethodInfo> BiserTypes = new ConcurrentDictionary<Type, MethodInfo>();

      CustomSerializator.ByteArraySerializator = (object o) => {
               
        return ((Biser.IEncoder)o).BiserEncoder().Encode();             
      };

      CustomSerializator.ByteArrayDeSerializator = (byte[] bt, Type t) =>
      {
        var minfo = BiserTypes.GetOrAdd(t,(Type typeSelf) => {
          return typeSelf.GetMethod("BiserDecode", BindingFlags.Public | BindingFlags.Static);                   
        });
                
        return minfo.Invoke(null, new object[] { bt, null });                                
      };

Thanks again!

from dbreeze.

hhblaze avatar hhblaze commented on July 24, 2024

For those who can't use the full version of the ConcurrentDictionary, use instead:

static SDictionary<Type, MethodInfo> BiserTypes = new SDictionary<Type, MethodInfo>();

 CustomSerializator.ByteArraySerializator = (object o) => {
               
        return ((Biser.IEncoder)o).BiserEncoder().Encode();             
      };

      CustomSerializator.ByteArrayDeSerializator = (byte[] bt, Type t) =>
      {
        var minfo = BiserTypes.GetOrAdd(t,(Type typeSelf) => {
          return typeSelf.GetMethod("BiserDecode", BindingFlags.Public | BindingFlags.Static);                   
        });
                
        return minfo.Invoke(null, new object[] { bt, null });                                
      };

where SDictionary is (taken and enhanced from https://github.com/hhblaze/Raft.Net/blob/master/Raft/Utils/SDictionary.cs)

 internal class SDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    {
        internal Dictionary<TKey, TValue> _dict = new Dictionary<TKey, TValue>();
        internal ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();


        /// <summary>
        /// Safely Returns List by Predicate
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public List<KeyValuePair<TKey, TValue>> GetSafe(Func<KeyValuePair<TKey, TValue>, bool> predicate)
        {
            _lock.EnterReadLock();
            try
            {
                return _dict.Where(predicate).ToList();
            }
            catch
            {
                return new List<KeyValuePair<TKey, TValue>>();
            }
            finally
            {
                _lock.ExitReadLock();
            }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TArg"></typeparam>
        /// <param name="key"></param>
        /// <param name="valueFactory"></param>
        /// <param name="factoryArgument"></param>
        /// <returns></returns>
        public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
        {
            TValue ret;            

            _lock.EnterUpgradeableReadLock();
            try
            {
                if (_dict.TryGetValue(key, out ret))
                    return ret;

                _lock.EnterWriteLock();
                try
                {
                    ret = valueFactory(key, factoryArgument);
                    _dict[key] = ret;
                    return ret;
                }
                catch
                {
                    return default(TValue);
                }
                finally
                {
                    _lock.ExitWriteLock();
                }
            }
            catch
            {
                return default(TValue);
            }
            finally
            {
                _lock.ExitUpgradeableReadLock();
            }
        }


        /// <summary>
        /// All add method are safe here, but this returns also a value
        /// Enhanced functionality also for update
        /// </summary>
        /// <param name="pair"></param>
        /// <returns></returns>
        public bool AddSafe(KeyValuePair<TKey, TValue> pair)
        {
            bool r = false;


            if (pair.Key == null)
                return r;

            _lock.EnterWriteLock();
            try
            {
                if (!_dict.ContainsKey(pair.Key))
                {
                    _dict.Add(pair.Key, pair.Value);
                }
                else
                {
                    //here we supply new instance of a value
                    _dict[pair.Key] = pair.Value;
                }

                r = true;
            }
            catch
            {
                r = false;
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return r;
        }

        public bool RemoveThenAdd(Func<TKey, bool> toRemoveKeysPredicate, Dictionary<TKey, TValue> toAdd)
        {
            bool r = false;

            _lock.EnterWriteLock();
            try
            {
                var keys = _dict.Keys.Where(k => toRemoveKeysPredicate(k)).ToList();
                foreach (var key in keys)
                {
                    _dict.Remove(key);
                }

                foreach (var kvp in toAdd)
                {
                    if (_dict.ContainsKey(kvp.Key))
                        _dict[kvp.Key] = kvp.Value;
                    else
                        _dict.Add(kvp.Key, kvp.Value);
                }

                r = true;
            }
            catch (Exception ex)
            {
                r = false;
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return r;
        }


        public bool Remove(TKey key)
        {
            bool r = false;

            if (key == null)
                return r;

            _lock.EnterWriteLock();
            try
            {
                if (_dict.ContainsKey(key))
                {
                    _dict.Remove(key);
                    r = true;
                }

            }
            catch (Exception ex)
            {
                r = false;
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return r;
        }

        public TValue this[TKey key]
        {
            get
            {
                if (key == null)
                    return default(TValue);

                _lock.EnterReadLock();
                try
                {
                    if (_dict.ContainsKey(key))
                        return _dict[key];
                }
                catch
                {
                }
                finally
                {
                    _lock.ExitReadLock();
                }

                return default(TValue);
            }
            set
            {
                _lock.EnterWriteLock();
                try
                {
                    if (_dict.ContainsKey(key))
                        _dict[key] = value;
                    else
                        _dict.Add(key, value);

                }
                catch (Exception ex)
                {
                }
                finally
                {
                    _lock.ExitWriteLock();
                }
            }
        }


        /// <summary>
        /// Adds or updates value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(TKey key, TValue value)
        {
            this.AddSafe(new KeyValuePair<TKey, TValue>(key, value));
        }

        void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
        {
            this.AddSafe(item);
        }

        public bool ContainsKey(TKey key)
        {
            _lock.EnterReadLock();
            try
            {
                return _dict.ContainsKey(key);
            }
            catch
            {
                return false;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

        public ICollection<TKey> Keys
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _dict.Keys;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            _lock.EnterReadLock();
            try
            {
                return _dict.TryGetValue(key, out value);
            }
            catch
            {
                value = default(TValue);
                return false;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

        public ICollection<TValue> Values
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _dict.Values;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }



        public void Clear()
        {
            _lock.EnterWriteLock();
            try
            {
                _dict.Clear();
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            _lock.EnterReadLock();
            try
            {
                return _dict.Contains(item);
            }
            catch
            {
                return false;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            _lock.EnterReadLock();
            try
            {
                _dict.ToArray().CopyTo(array, arrayIndex);
            }
            catch
            {
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

        public int Count
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _dict.Count;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            return this.Remove(item.Key);
        }

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            _lock.EnterReadLock();
            try
            {
                return _dict.GetEnumerator();
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            _lock.EnterReadLock();
            try
            {
                return _dict.GetEnumerator();
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
    }

from dbreeze.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.