Comments (4)
That looks exactly like what I need. Thanks!
from dbreeze.
Hi, it is possible
from dbreeze.
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.
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)
- Why duplicated projects? HOT 5
- Guide on how to fail-over with Raft and DBreeze HOT 10
- TextSearch bug HOT 6
- Getting "Getting table "@utCoins" from the schema failed!" when calling Technical_SetTable_OverwriteIsNotAllowed HOT 2
- DBreeze sorts by ID HOT 4
- DBreeze.Exceptions.DBreezeException: Creation of the database folder failed! ---> DBreeze.Exceptions.DBreezeException: Database is not operable, please find out the problem and restart the engine! FSR INIT FAILED HOT 5
- What's wrong with transactions? HOT 4
- Multiple DBreezeEngine instances to the same db files HOT 1
- Biser Objectifies classes using different namespace than DBreeze's version HOT 6
- Text Search of items < minimum size not possible? HOT 4
- Count of documents ids 0 with data being in the table HOT 3
- .net core 5 HOT 4
- Serialize/Deserialize should not be static
- Reading from multiple tables at once and returning sorted results HOT 2
- TextSearch Tables still contains internal/external references, after using TextRemoveAll HOT 5
- RemoveSession is not called on "DBreeze.Exceptions.DBreezeException: One transaction can be used from one thread only!" HOT 1
- What happens to Old DataBlocks? HOT 7
- Memory mode insert slower than writes on disk HOT 1
- Database insert speed HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbreeze.