-
Notifications
You must be signed in to change notification settings - Fork 34
/
CommandStore.cs
158 lines (134 loc) · 5.14 KB
/
CommandStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using OrigoDB.Core.Logging;
namespace OrigoDB.Core.Storage
{
/// <summary>
/// Base class with common behavior for CommandStore implementations
/// </summary>
public abstract class CommandStore : Initializable, ICommandStore
{
protected static ILogger _log = LogProvider.Factory.GetLoggerForCallingType();
protected EngineConfiguration _config;
protected IFormatter _formatter;
/// <summary>
/// Read the sequence of entries with id greater than or equal to a given entryId
/// </summary>
protected abstract IEnumerable<JournalEntry> GetJournalEntriesFromImpl(ulong entryId);
/// <summary>
/// Get an append-only stream for writing journal entries
/// </summary>
public abstract Stream CreateJournalWriterStream(ulong firstEntryId = 1);
/// <summary>
/// Override if necessary
/// </summary>
/// <param name="pointInTime"></param>
/// <returns></returns>
public virtual IEnumerable<JournalEntry> GetJournalEntriesBeforeOrAt(DateTime pointInTime)
{
return GetJournalEntriesFrom(0).TakeWhile(e => e.Created <= pointInTime);
}
protected virtual IJournalWriter CreateStoreSpecificJournalWriter()
{
return new StreamJournalWriter(_config, CreateJournalWriterStream);
}
protected CommandStore(EngineConfiguration config)
{
_config = config;
}
public IEnumerable<JournalEntry> GetJournalEntriesFrom(ulong entryId)
{
bool firstEntry = true;
foreach (var entry in GetJournalEntriesFromImpl(entryId))
{
if (firstEntry && entry.Id > entryId && entryId > 0)
{
string msg = String.Format("requested journal entry missing [{0}]", entryId);
throw new InvalidOperationException(msg);
}
firstEntry = false;
yield return entry;
}
}
/// <summary>
/// Iterate all the entries in the journal
/// </summary>
/// <returns></returns>
public virtual IEnumerable<JournalEntry> GetJournalEntries()
{
return GetJournalEntriesFrom(0);
}
public virtual IJournalWriter CreateJournalWriter(ulong lastEntryId)
{
IJournalWriter writer = CreateStoreSpecificJournalWriter();
return _config.AsynchronousJournaling
? new AsynchronousJournalWriter(writer)
: writer;
}
public override void Initialize()
{
_formatter = _config.CreateFormatter(FormatterUsage.Journal);
base.Initialize();
}
/// <summary>
/// True if the journal is empty
/// </summary>
public virtual bool IsEmpty
{
get
{
return !GetJournalEntries().Any();
}
}
/// <summary>
/// Throw an exception unless the journal is empty
/// </summary>
protected void AssertEmpty()
{
if (!IsEmpty) throw new InvalidOperationException("Journal must be empty");
}
/// <summary>
/// Get commited commands beginning from a specific entry id (inclusive)
/// </summary>
public IEnumerable<JournalEntry<Command>> CommandEntriesFrom(ulong entryId)
{
return CommittedCommandEntries(() => GetJournalEntriesFromImpl(entryId));
}
/// <summary>
/// Get committed commands from a point in time
/// </summary>
public IEnumerable<JournalEntry<Command>> CommandEntriesBeforeOrAt(DateTime pointInTime)
{
return CommittedCommandEntries(() => GetJournalEntriesBeforeOrAt(pointInTime));
}
/// <summary>
/// Select the items of type Command that are not followed by a rollback marker
/// </summary>
public static IEnumerable<JournalEntry<Command>> CommittedCommandEntries(Func<IEnumerable<JournalEntry>> enumerator)
{
//rollback markers repeat the id of the rolled back command
JournalEntry<Command> previous = null;
foreach (JournalEntry current in enumerator.Invoke())
{
if (current is JournalEntry<Command>)
{
(current as JournalEntry<Command>).Item.Timestamp = current.Created;
if (previous != null) yield return previous;
previous = (JournalEntry<Command>)current;
}
else previous = null;
}
if (previous != null) yield return previous;
}
/// <summary>
/// Get the complete sequence of commands skipping any that were rolled back
/// </summary>
public IEnumerable<JournalEntry<Command>> CommandEntries()
{
return CommandEntriesFrom(1);
}
}
}