Skip to content

Commit eb1757c

Browse files
author
Elad Zelingher
committed
Playing a bit with event history and LiteDB
1 parent 1e51594 commit eb1757c

File tree

8 files changed

+318
-0
lines changed

8 files changed

+318
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
using LiteDB;
5+
6+
namespace WampSharp.LiteDB
7+
{
8+
internal class EventEntry
9+
{
10+
[BsonRef("subscriptions")]
11+
public SubscriptionEntry Subscription { get; set; }
12+
13+
public int Id { get; set; }
14+
15+
[DataMember(Name = "timestamp")]
16+
public DateTime Timestamp { get; set; }
17+
18+
[DataMember(Name = "publisher")]
19+
public long PublisherId { get; set; }
20+
21+
[DataMember(Name = "publication")]
22+
public long PublicationId { get; set; }
23+
24+
[DataMember(Name = "topic")]
25+
public string Topic { get; internal set; }
26+
27+
[DataMember(Name = "args")]
28+
public BsonValue[] Arguments { get; set; }
29+
30+
[DataMember(Name = "kwargs")]
31+
public IDictionary<string, BsonValue> ArgumentsKeywords { get; set; }
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using LiteDB;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace WampSharp.LiteDB
7+
{
8+
internal class JsonToBson
9+
{
10+
public static BsonValue ConvertToBson(JToken value)
11+
{
12+
return InnerConvertToBson((dynamic) value);
13+
}
14+
15+
private static BsonValue InnerConvertToBson(JValue value)
16+
{
17+
return new BsonValue(value.Value);
18+
}
19+
20+
private static BsonValue InnerConvertToBson(JObject value)
21+
{
22+
Dictionary<string, BsonValue> result = new Dictionary<string, BsonValue>();
23+
24+
foreach (KeyValuePair<string, JToken> propertyNameToValue in value)
25+
{
26+
result[propertyNameToValue.Key] = ConvertToBson(propertyNameToValue.Value);
27+
}
28+
29+
return new BsonValue(result);
30+
}
31+
32+
private static BsonValue InnerConvertToBson(JArray value)
33+
{
34+
return new BsonArray(value.Select(x => ConvertToBson(x)));
35+
}
36+
}
37+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Collections.Immutable;
2+
using System.Linq;
3+
using LiteDB;
4+
using Newtonsoft.Json.Linq;
5+
using WampSharp.Core.Serialization;
6+
using WampSharp.V2.MetaApi;
7+
8+
namespace WampSharp.LiteDB
9+
{
10+
public class LiteDBEventStore : IWampEventStore
11+
{
12+
private readonly LiteDatabase mDatabase;
13+
private readonly LiteCollection<SubscriptionEntry> mSubscriptions;
14+
private ImmutableDictionary<(string topic,string match), SubscriptionEntry> mLocalSubscriptions =
15+
ImmutableDictionary.Create<(string topic, string match), SubscriptionEntry>();
16+
17+
private readonly object mLock = new object();
18+
19+
private readonly LiteCollection<EventEntry> mEvents;
20+
21+
public LiteDBEventStore(LiteDatabase database)
22+
{
23+
mDatabase = database;
24+
mSubscriptions = mDatabase.GetCollection<SubscriptionEntry>("subscriptions");
25+
mSubscriptions.EnsureIndex(x => x.Topic);
26+
mSubscriptions.EnsureIndex(x => x.Match);
27+
28+
mEvents = mDatabase.GetCollection<EventEntry>("events");
29+
mEvents.EnsureIndex(x => x.Subscription);
30+
}
31+
32+
public void StoreEvent<TMessage>(IWampFormatter<TMessage> formatter,
33+
HistoricalSubscription subscription,
34+
WampHistoricalEvent<TMessage> historicalEvent)
35+
{
36+
SubscriptionEntry storedSubscription = GetSubscription(subscription);
37+
38+
EventEntry value = new EventEntry()
39+
{
40+
Subscription = storedSubscription,
41+
PublicationId = historicalEvent.PublicationId,
42+
PublisherId = historicalEvent.PublisherId,
43+
Timestamp = historicalEvent.Timestamp,
44+
Topic = historicalEvent.Topic,
45+
Arguments =
46+
historicalEvent.Arguments.Select(x => JsonToBson.ConvertToBson
47+
(formatter.Deserialize<JToken>(x)))
48+
.ToArray(),
49+
ArgumentsKeywords = historicalEvent.ArgumentsKeywords.ToDictionary(x => x.Key,
50+
x => JsonToBson.ConvertToBson
51+
(formatter.Deserialize<JToken>(
52+
x.Value)))
53+
};
54+
55+
mEvents.Insert(value);
56+
}
57+
58+
private SubscriptionEntry GetSubscription(HistoricalSubscription subscription)
59+
{
60+
SubscriptionEntry value;
61+
62+
if (mLocalSubscriptions.TryGetValue((subscription.TopicUri, subscription.SubscribeOptions.Match),
63+
out value))
64+
{
65+
return value;
66+
}
67+
68+
SubscriptionEntry result;
69+
70+
lock (mLock)
71+
{
72+
result =
73+
mSubscriptions.FindOne(x => x.Match == subscription.SubscribeOptions.Match &&
74+
x.Topic == subscription.TopicUri);
75+
76+
if (result == null)
77+
{
78+
result = new SubscriptionEntry()
79+
{
80+
Match = subscription.SubscribeOptions.Match,
81+
Topic = subscription.TopicUri
82+
};
83+
84+
mSubscriptions.Insert(result);
85+
}
86+
87+
88+
mLocalSubscriptions = mLocalSubscriptions.SetItem((result.Topic, result.Match), result);
89+
}
90+
91+
return result;
92+
}
93+
94+
public WampHistoricalEvent<object>[] GetEvents(HistoricalSubscription subscription, long limit)
95+
{
96+
lock (mLock)
97+
{
98+
SubscriptionEntry subscriptionEntry = GetSubscription(subscription);
99+
}
100+
101+
return null;
102+
}
103+
}
104+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("WampSharp.LiteDB")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("WampSharp.LiteDB")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("caee8c29-c306-4a5f-b8e4-ddd2f05fc144")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace WampSharp.LiteDB
2+
{
3+
internal class SubscriptionEntry
4+
{
5+
public int Id { get; set; }
6+
public string Match { get; set; }
7+
public string Topic { get; set; }
8+
}
9+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>WampSharp.LiteDB</RootNamespace>
11+
<AssemblyName>WampSharp.LiteDB</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="LiteDB, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
35+
<HintPath>..\packages\LiteDB.3.1.2\lib\net35\LiteDB.dll</HintPath>
36+
</Reference>
37+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Newtonsoft.Json.6.0.5\lib\net45\Newtonsoft.Json.dll</HintPath>
39+
</Reference>
40+
<Reference Include="System" />
41+
<Reference Include="System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
42+
<HintPath>..\packages\System.Collections.Immutable.1.2.0\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
43+
</Reference>
44+
<Reference Include="System.Core" />
45+
<Reference Include="System.Runtime.Serialization" />
46+
<Reference Include="System.ValueTuple, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
47+
<HintPath>..\packages\System.ValueTuple.4.0.0-rc3-24212-01\lib\netstandard1.1\System.ValueTuple.dll</HintPath>
48+
</Reference>
49+
<Reference Include="System.Xml.Linq" />
50+
<Reference Include="System.Data.DataSetExtensions" />
51+
<Reference Include="Microsoft.CSharp" />
52+
<Reference Include="System.Data" />
53+
<Reference Include="System.Net.Http" />
54+
<Reference Include="System.Xml" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<Compile Include="EventEntry.cs" />
58+
<Compile Include="JsonToBson.cs" />
59+
<Compile Include="LiteDBEventStore.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
<Compile Include="SubscriptionEntry.cs" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<ProjectReference Include="..\WampSharp\WampSharp.csproj">
65+
<Project>{653A76DC-00D7-4EFF-A25E-2FA10C5C927D}</Project>
66+
<Name>WampSharp</Name>
67+
</ProjectReference>
68+
</ItemGroup>
69+
<ItemGroup>
70+
<None Include="packages.config" />
71+
</ItemGroup>
72+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
73+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="LiteDB" version="3.1.2" targetFramework="net45" />
4+
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />
5+
<package id="System.Collections.Immutable" version="1.2.0" targetFramework="net45" />
6+
<package id="System.ValueTuple" version="4.0.0-rc3-24212-01" targetFramework="net45" />
7+
</packages>

src/net45/WampSharp.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WampSharp.AspNet.WebSockets
8181
EndProject
8282
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WampSharp.HttpListener", "Extensions\WampSharp.HttpListener\WampSharp.HttpListener.csproj", "{7CAA96BD-6CD3-48FF-B837-C894F7FB56FB}"
8383
EndProject
84+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WampSharp.LiteDB", "WampSharp.LiteDB\WampSharp.LiteDB.csproj", "{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}"
85+
EndProject
8486
Global
8587
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8688
Debug|Any CPU = Debug|Any CPU
@@ -459,6 +461,22 @@ Global
459461
{7CAA96BD-6CD3-48FF-B837-C894F7FB56FB}.Release|x64.Build.0 = Release|Any CPU
460462
{7CAA96BD-6CD3-48FF-B837-C894F7FB56FB}.Release|x86.ActiveCfg = Release|Any CPU
461463
{7CAA96BD-6CD3-48FF-B837-C894F7FB56FB}.Release|x86.Build.0 = Release|Any CPU
464+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
465+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|Any CPU.Build.0 = Debug|Any CPU
466+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|ARM.ActiveCfg = Debug|Any CPU
467+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|ARM.Build.0 = Debug|Any CPU
468+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|x64.ActiveCfg = Debug|Any CPU
469+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|x64.Build.0 = Debug|Any CPU
470+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|x86.ActiveCfg = Debug|Any CPU
471+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Debug|x86.Build.0 = Debug|Any CPU
472+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|Any CPU.ActiveCfg = Release|Any CPU
473+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|Any CPU.Build.0 = Release|Any CPU
474+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|ARM.ActiveCfg = Release|Any CPU
475+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|ARM.Build.0 = Release|Any CPU
476+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|x64.ActiveCfg = Release|Any CPU
477+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|x64.Build.0 = Release|Any CPU
478+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|x86.ActiveCfg = Release|Any CPU
479+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144}.Release|x86.Build.0 = Release|Any CPU
462480
EndGlobalSection
463481
GlobalSection(SolutionProperties) = preSolution
464482
HideSolutionNode = FALSE
@@ -494,5 +512,6 @@ Global
494512
{219F3832-EC76-44CD-B601-EE5850F40FAF} = {FA4B348E-3BBD-4F98-B429-891376EBA17E}
495513
{BA110F39-09D9-4D7F-91CF-0604E5715EA2} = {FA4B348E-3BBD-4F98-B429-891376EBA17E}
496514
{7CAA96BD-6CD3-48FF-B837-C894F7FB56FB} = {FA4B348E-3BBD-4F98-B429-891376EBA17E}
515+
{CAEE8C29-C306-4A5F-B8E4-DDD2F05FC144} = {FA4B348E-3BBD-4F98-B429-891376EBA17E}
497516
EndGlobalSection
498517
EndGlobal

0 commit comments

Comments
 (0)