-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Serializator for Biser? #62
Comments
Hi, it is possible |
That looks exactly like what I need. Thanks! |
This works well but the 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! |
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();
}
}
} |
Hello,
I am testing out DBreeze and I've been looking at using the built-in Biser serialization that you've created. I understand how to generate the serialization class and I've created the accompanying c# file which is compiled along with my test program. How do i do the equivalent of:
but for Biser?
Thanks
The text was updated successfully, but these errors were encountered: