Skip to content

Instantly share code, notes, and snippets.

View ayende's full-sized avatar

Ayende Rahien ayende

View GitHub Profile
using(var orders = store.Subscriptions.Open<Order>(ordersSubscription, new SubscriptionConnectionOptions()))
{
orders.Subscribe(order =>
{
GenerateInvoice(order);
});
orders.Subscribe(order =>
{
bool hasMoreWork = false;
do
{
var sp = Stopwatch.StartNew();
lock(lockObj)
{
while(sp.ElapsedMilliseconds < 150) // don't hold the lock too long, let other people go in
{
// do work and set hasMoreWork
}
def OnJournalEntry(entry):
journal.AppendHash(entry)
journal.BufferedWrite(entry)
return if not entry.TransactionCommit
journal.WriteHash()
journal.ClearHash()
def MergeTransactionThreadProc():
buffer = Buffer()
while true:
buffer.Clear()
result = DequeueOperation()
if result.Success is false:
WaitForAdditionalOperations()
def MergeTransactionThreadProc():
while true:
buffer = Buffer()
result = DequeueOperation()
if result.Success is false:
WaitForAdditionalOperations()
continue
public class GraphDatabase
{
long AddNode(NameValueCollection node);
long AddEdge(string type, long from, long to, NameValueCollection edge = null);
NameValueCollection GetNode(long id);
IEnumerable<Tuple<long, NameValueCollection>> GetEdgesFor(string type, long id);
}
public class GraphDatabase
{
private TableSchema _emptySchema = new TableSchema
{
AllowNoIndexesOrPrimaryKey = true
};
public GraphDatabase(StorageEnvironmentOptions options)
{
_env = new StorageEnvironment(options);
public unsafe long AddNode(NameValueCollection node);
{
using(var tx = _env.WriteTransaction()
{
var nodesTable = new Table(_emptySchema, "Nodes", tx);
string serializedNode = SerializeNode(node);
byte[] data = Encoding.UTF8.GetBytes(serializedNode);
fixed(byte* pData = data)
{
long id = nodesTable.Insert(new TableValueBuilder
public unsafe long AddEdge(string type, long from, long to, NameValueCollection edge = null)
{
using(var tx = _env.WriteTransaction())
{
long edgeId = -1;
if(edge != null)
{
var edgesDataTable = new Table(_emptySchema, "EdgesData", tx);
string serializededge = SerializeNode(edge);
public NameValueCollection GetNode(long id)
{
using(var tx = _env.ReadTransaction())
{
var nodesTable = new Table(_emptySchema, "Nodes", tx);
var reader = new TableValueReader(table.DirectRead(id, out size), size);
var data = reader.Read(0, out size);
var nodeStr = Encoding.UTF8.GetString(data, size);
return ParseNode(nodeStr);