Skip to content

Instantly share code, notes, and snippets.

@rofr
Created August 30, 2015 09:10
Show Gist options
  • Save rofr/d374189f7d4ff1cbd4e8 to your computer and use it in GitHub Desktop.
Save rofr/d374189f7d4ff1cbd4e8 to your computer and use it in GitHub Desktop.
OrigoDB persistence plugin for XSockets
//Found this on pastebin at http://pastebin.com/zkS4Wszb
// See comments below
/// <summary>
/// Key/Value pairs where key is string and value is object
/// </summary>
public interface IStorageModel
{
object AddOrUpdate(string key, object entity);
bool Remove(string key);
void RemoveAll();
IEnumerable<object> GetAll();
object GetById(string key);
bool ContainsKey(string key);
}
[Serializable]
public class StorageModel : Model, IStorageModel
{
private readonly object _locker = new object();
private ConcurrentDictionary<string, object> Container { get; set; }
public StorageModel()
{
this.Container = new ConcurrentDictionary<string, object>();
}
public object AddOrUpdate(string key, object entity)
{
lock (_locker)
{
if (!Container.ContainsKey(key))
Container.TryAdd(key, entity);
else
{
Container[key] = entity;
}
}
return entity;
}
public bool Remove(string key)
{
lock (_locker)
{
object entity;
return Container.TryRemove(key, out entity);
}
}
public void RemoveAll()
{
lock (_locker)
{
Container = new ConcurrentDictionary<string, object>();
}
}
public IEnumerable<object> GetAll()
{
return Container.Values;
}
public object GetById(string key)
{
return Container.ContainsKey(key) ? Container[key] : default(object);
}
public bool ContainsKey(string key)
{
return Container.ContainsKey(key);
}
}
/// <summary>
/// The plugin that XSockets will pick up
/// </summary>
[Export(typeof(IPersistentStorageProvider))]
public interface IPersistentStorageProvider
{
object AddOrUpdate(string key, object entity);
bool Remove(string key);
void RemoveAll();
IEnumerable<object> GetAll();
object GetById(string key);
bool ContainsKey(string key);
}
public class PersistentProvider : IPersistentStorageProvider
{
private static StorageModel Provider { get; set; }
static PersistentProvider()
{
Provider = Engine.For<StorageModel>(new EngineConfiguration(@"C:\temp\OrigoDB")).GetProxy();
}
public object AddOrUpdate(string key, object entity)
{
return Provider.AddOrUpdate(key, entity);
}
public bool Remove(string key)
{
return Provider.Remove(key);
}
public void RemoveAll()
{
Provider.RemoveAll();
}
public IEnumerable<object> GetAll()
{
return Provider.GetAll();
}
public object GetById(string key)
{
return Provider.GetById(key);
}
public bool ContainsKey(string key)
{
return Provider.ContainsKey(key);
}
}
@rofr
Copy link
Author

rofr commented Aug 30, 2015

Some issues with this code:

  1. AddOrUpdate and Remove need to have [Command] attributes, otherwise they will be interpreted as queries and calls will not be persisted!
  2. GetAll() will potentially return a large amount of data, I would add some kind of paging or remove it.
  3. No need to do locking inside the model, the engine will take care of synchronization.
  4. No need to use a ConcurrentDictionary, a plain old Dictionary or perhaps even SortedDictionary will do.
  5. The IStorageModel and IPersistentStorage interfaces are identical, could have been a single interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment