@@ -57,7 +57,6 @@ private WriteBehind() { }
57
57
58
58
private readonly TimeSpan _writeBehindInterval ;
59
59
private readonly string _dir ;
60
- private bool _dirExists ;
61
60
62
61
private readonly Dictionary < string , DurableDataEnvelope > _pending = new Dictionary < string , DurableDataEnvelope > ( ) ;
63
62
private readonly ILoggingAdapter _log ;
@@ -89,8 +88,11 @@ public LmdbDurableStore(Config config)
89
88
? Path . GetFullPath ( $ "{ path } -{ Context . System . Name } -{ Self . Path . Parent . Name } -{ Cluster . Cluster . Get ( Context . System ) . SelfAddress . Port } ")
90
89
: Path . GetFullPath ( path ) ;
91
90
92
- _dirExists = Directory . Exists ( _dir ) ;
93
-
91
+ if ( ! Directory . Exists ( _dir ) )
92
+ {
93
+ Directory . CreateDirectory ( _dir ) ;
94
+ }
95
+
94
96
_log . Info ( $ "Using durable data in LMDB directory [{ _dir } ]") ;
95
97
Init ( ) ;
96
98
}
@@ -110,41 +112,24 @@ protected override void PostStop()
110
112
111
113
private LightningEnvironment GetLightningEnvironment ( )
112
114
{
113
- LightningEnvironment env ;
114
-
115
- if ( ! _dirExists )
115
+ var t0 = Stopwatch . StartNew ( ) ;
116
+ var env = new LightningEnvironment ( _dir )
116
117
{
117
- var t0 = Stopwatch . StartNew ( ) ;
118
- Directory . CreateDirectory ( _dir ) ;
119
- _dirExists = true ;
120
-
121
- env = new LightningEnvironment ( _dir )
122
- {
123
- MapSize = _mapSize ,
124
- MaxDatabases = 1
125
- } ;
126
- env . Open ( EnvironmentOpenFlags . NoLock ) ;
118
+ MapSize = _mapSize ,
119
+ MaxDatabases = 1
120
+ } ;
121
+ env . Open ( EnvironmentOpenFlags . NoLock ) ;
127
122
128
- using ( var tx = env . BeginTransaction ( ) )
129
- using ( tx . OpenDatabase ( DatabaseName , new DatabaseConfiguration { Flags = DatabaseOpenFlags . Create } ) )
130
- {
131
- tx . Commit ( ) ;
132
- }
133
-
134
- t0 . Stop ( ) ;
135
- if ( _log . IsDebugEnabled )
136
- _log . Debug ( $ "Init of LMDB in directory [{ _dir } ] took [{ t0 . ElapsedMilliseconds } ms]") ;
137
- }
138
- else
123
+ using ( var tx = env . BeginTransaction ( ) )
124
+ using ( tx . OpenDatabase ( DatabaseName , new DatabaseConfiguration { Flags = DatabaseOpenFlags . Create } ) )
139
125
{
140
- env = new LightningEnvironment ( _dir )
141
- {
142
- MapSize = _mapSize ,
143
- MaxDatabases = 1
144
- } ;
145
- env . Open ( EnvironmentOpenFlags . NoLock ) ;
126
+ tx . Commit ( ) ;
146
127
}
147
128
129
+ t0 . Stop ( ) ;
130
+ if ( _log . IsDebugEnabled )
131
+ _log . Debug ( $ "Init of LMDB in directory [{ _dir } ] took [{ t0 . ElapsedMilliseconds } ms]") ;
132
+
148
133
return env ;
149
134
}
150
135
@@ -207,12 +192,13 @@ private void Init()
207
192
}
208
193
209
194
var t0 = Stopwatch . StartNew ( ) ;
210
- using ( var env = GetLightningEnvironment ( ) )
211
- using ( var tx = env . BeginTransaction ( TransactionBeginFlags . ReadOnly ) )
212
- using ( var db = tx . OpenDatabase ( DatabaseName ) )
213
- using ( var cursor = tx . CreateCursor ( db ) )
195
+
196
+ try
214
197
{
215
- try
198
+ using ( var env = GetLightningEnvironment ( ) )
199
+ using ( var tx = env . BeginTransaction ( TransactionBeginFlags . ReadOnly ) )
200
+ using ( var db = tx . OpenDatabase ( DatabaseName ) )
201
+ using ( var cursor = tx . CreateCursor ( db ) )
216
202
{
217
203
var data = cursor . AsEnumerable ( ) . Select ( ( x , i )
218
204
=> {
@@ -236,11 +222,11 @@ private void Init()
236
222
237
223
Become ( Active ) ;
238
224
}
239
- catch ( Exception e )
240
- {
241
- if ( t0 . IsRunning ) t0 . Stop ( ) ;
242
- throw new LoadFailedException ( "failed to load durable distributed-data" , e ) ;
243
- }
225
+ }
226
+ catch ( Exception e )
227
+ {
228
+ if ( t0 . IsRunning ) t0 . Stop ( ) ;
229
+ throw new LoadFailedException ( "failed to load durable distributed-data" , e ) ;
244
230
}
245
231
} ) ;
246
232
}
@@ -251,23 +237,25 @@ private void DoWriteBehind()
251
237
{
252
238
var t0 = Stopwatch . StartNew ( ) ;
253
239
using ( var env = GetLightningEnvironment ( ) )
254
- using ( var tx = env . BeginTransaction ( ) )
255
- using ( var db = tx . OpenDatabase ( DatabaseName ) )
240
+ using ( var tx = env . BeginTransaction ( ) )
256
241
{
257
242
try
258
243
{
259
- foreach ( var entry in _pending )
244
+ using ( var db = tx . OpenDatabase ( DatabaseName ) )
260
245
{
261
- var byteKey = Encoding . UTF8 . GetBytes ( entry . Key ) ;
262
- var byteValue = _serializer . ToBinary ( entry . Value ) ;
263
- tx . Put ( db , byteKey , byteValue ) ;
264
- }
265
- tx . Commit ( ) ;
246
+ foreach ( var entry in _pending )
247
+ {
248
+ var byteKey = Encoding . UTF8 . GetBytes ( entry . Key ) ;
249
+ var byteValue = _serializer . ToBinary ( entry . Value ) ;
250
+ tx . Put ( db , byteKey , byteValue ) ;
251
+ }
252
+ tx . Commit ( ) ;
266
253
267
- t0 . Stop ( ) ;
268
- if ( _log . IsDebugEnabled )
269
- {
270
- _log . Debug ( $ "store and commit of [{ _pending . Count } ] entries took { t0 . ElapsedMilliseconds } ms") ;
254
+ t0 . Stop ( ) ;
255
+ if ( _log . IsDebugEnabled )
256
+ {
257
+ _log . Debug ( $ "store and commit of [{ _pending . Count } ] entries took { t0 . ElapsedMilliseconds } ms") ;
258
+ }
271
259
}
272
260
}
273
261
catch ( Exception cause )
0 commit comments