Skip to content

Commit dd7fb34

Browse files
committed
Object disposed instead of crash, hell yeah.
1 parent a616106 commit dd7fb34

File tree

4 files changed

+107
-38
lines changed

4 files changed

+107
-38
lines changed

BTDB/KVDBLayer/Implementation/BTreeKeyValueDBCursor.cs

+30
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,26 @@ public void Dispose()
9191

9292
public bool FindFirstKey(in ReadOnlySpan<byte> prefix)
9393
{
94+
ObjectDisposedException.ThrowIf(_transaction == null, this);
95+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
9496
_modifiedFromLastFind = false;
9597
_keyIndex = -1;
9698
return _cursor!.FindFirst(prefix);
9799
}
98100

99101
public bool FindLastKey(in ReadOnlySpan<byte> prefix)
100102
{
103+
ObjectDisposedException.ThrowIf(_transaction == null, this);
104+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
101105
_modifiedFromLastFind = false;
102106
_keyIndex = _cursor!.FindLastWithPrefix(prefix);
103107
return _keyIndex >= 0;
104108
}
105109

106110
public bool FindPreviousKey(in ReadOnlySpan<byte> prefix)
107111
{
112+
ObjectDisposedException.ThrowIf(_transaction == null, this);
113+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
108114
if (_modifiedFromLastFind)
109115
{
110116
if (FindKeyIndex(_keyIndex - 1) && _cursor!.KeyHasPrefix(prefix))
@@ -132,6 +138,8 @@ public bool FindPreviousKey(in ReadOnlySpan<byte> prefix)
132138

133139
public bool FindNextKey(in ReadOnlySpan<byte> prefix)
134140
{
141+
ObjectDisposedException.ThrowIf(_transaction == null, this);
142+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
135143
if (_modifiedFromLastFind)
136144
{
137145
if (!_removedCurrent) _keyIndex++;
@@ -160,6 +168,8 @@ public bool FindNextKey(in ReadOnlySpan<byte> prefix)
160168

161169
public FindResult Find(in ReadOnlySpan<byte> key, uint prefixLen)
162170
{
171+
ObjectDisposedException.ThrowIf(_transaction == null, this);
172+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
163173
_modifiedFromLastFind = false;
164174
var result = _cursor!.Find(key);
165175
_keyIndex = -1;
@@ -203,6 +213,8 @@ public long GetKeyIndex()
203213

204214
public bool FindKeyIndex(in ReadOnlySpan<byte> prefix, long index)
205215
{
216+
ObjectDisposedException.ThrowIf(_transaction == null, this);
217+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
206218
_modifiedFromLastFind = false;
207219
if (!_cursor!.FindFirst(prefix))
208220
{
@@ -225,6 +237,8 @@ public bool FindKeyIndex(in ReadOnlySpan<byte> prefix, long index)
225237

226238
public bool FindKeyIndex(long index)
227239
{
240+
ObjectDisposedException.ThrowIf(_transaction == null, this);
241+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
228242
_modifiedFromLastFind = false;
229243
_keyIndex = -1;
230244
if (_cursor!.SeekIndex(index))
@@ -269,16 +283,22 @@ public KeyValuePair<uint, uint> GetStorageSizeOfCurrentKey()
269283

270284
public ReadOnlyMemory<byte> GetKeyMemory(ref Memory<byte> buffer, bool copy = false)
271285
{
286+
ObjectDisposedException.ThrowIf(_transaction == null, this);
287+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
272288
return _cursor!.GetKeyMemory(ref buffer, copy);
273289
}
274290

275291
public ReadOnlySpan<byte> GetKeySpan(scoped ref Span<byte> buffer, bool copy = false)
276292
{
293+
ObjectDisposedException.ThrowIf(_transaction == null, this);
294+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
277295
return _cursor!.GetKeySpan(ref buffer, copy);
278296
}
279297

280298
public ReadOnlySpan<byte> GetKeySpan(Span<byte> buffer, bool copy = false)
281299
{
300+
ObjectDisposedException.ThrowIf(_transaction == null, this);
301+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
282302
return _cursor!.GetKeySpan(buffer, copy);
283303
}
284304

@@ -291,6 +311,8 @@ public bool IsValueCorrupted()
291311

292312
public ReadOnlyMemory<byte> GetValueMemory(ref Memory<byte> buffer, bool copy = false)
293313
{
314+
ObjectDisposedException.ThrowIf(_transaction == null, this);
315+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
294316
if (!IsValid()) return new();
295317
var trueValue = _cursor!.GetValue();
296318
var keyValueDB = _transaction!.KeyValueDB;
@@ -319,6 +341,8 @@ public ReadOnlyMemory<byte> GetValueMemory(ref Memory<byte> buffer, bool copy =
319341

320342
public ReadOnlySpan<byte> GetValueSpan(scoped ref Span<byte> buffer, bool copy = false)
321343
{
344+
ObjectDisposedException.ThrowIf(_transaction == null, this);
345+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
322346
if (!IsValid()) return new();
323347
var trueValue = _cursor!.GetValue();
324348
var keyValueDB = _transaction!.KeyValueDB;
@@ -360,6 +384,8 @@ void EnsureValidKey()
360384

361385
void EnsureValidCursor()
362386
{
387+
ObjectDisposedException.ThrowIf(_transaction == null, this);
388+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
363389
if (!_cursor!.IsValid())
364390
{
365391
if (_modifiedFromLastFind && _keyIndex != -1)
@@ -456,6 +482,8 @@ public long EraseUpTo(IKeyValueDBCursor to)
456482
[SkipLocalsInit]
457483
public bool CreateOrUpdateKeyValue(in ReadOnlySpan<byte> key, in ReadOnlySpan<byte> value)
458484
{
485+
ObjectDisposedException.ThrowIf(_transaction == null, this);
486+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
459487
var cursor = (IKeyValueDBCursorInternal)_transaction!.FirstCursor;
460488
while (cursor != null)
461489
{
@@ -491,6 +519,8 @@ public bool CreateOrUpdateKeyValue(in ReadOnlySpan<byte> key, in ReadOnlySpan<by
491519

492520
public UpdateKeySuffixResult UpdateKeySuffix(in ReadOnlySpan<byte> key, uint prefixLen)
493521
{
522+
ObjectDisposedException.ThrowIf(_transaction == null, this);
523+
ObjectDisposedException.ThrowIf(_transaction.BTreeRoot == null, _transaction);
494524
var cursor = (IKeyValueDBCursorInternal)_transaction!.FirstCursor;
495525
while (cursor != null)
496526
{

BTDB/KVDBLayer/Implementation/BTreeKeyValueDBTransaction.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public BTreeKeyValueDBTransaction(BTreeKeyValueDB keyValueDB, IRootNode root, bo
3535

3636
public IKeyValueDBCursor CreateCursor()
3737
{
38+
ObjectDisposedException.ThrowIf(BTreeRoot == null, this);
3839
return BTreeKeyValueDBCursor.Create(this);
3940
}
4041

@@ -70,7 +71,8 @@ internal void MakeWritable()
7071

7172
public long GetKeyValueCount()
7273
{
73-
return BTreeRoot!.GetCount();
74+
ObjectDisposedException.ThrowIf(BTreeRoot == null, this);
75+
return BTreeRoot.GetCount();
7476
}
7577

7678
public bool IsWriting()
@@ -172,6 +174,7 @@ public ulong GetUlong(uint idx)
172174

173175
public void SetUlong(uint idx, ulong value)
174176
{
177+
ObjectDisposedException.ThrowIf(BTreeRoot == null, this);
175178
if (BTreeRoot!.GetUlong(idx) != value)
176179
{
177180
MakeWritable();
@@ -202,8 +205,9 @@ public string? DescriptionForLeaks
202205

203206
public Dictionary<(uint Depth, uint Children), uint> CalcBTreeStats()
204207
{
208+
ObjectDisposedException.ThrowIf(BTreeRoot == null, this);
205209
var stats = new RefDictionary<(uint Depth, uint Children), uint>();
206-
BTreeRoot!.CalcBTreeStats(stats, 0);
210+
BTreeRoot.CalcBTreeStats(stats, 0);
207211
return stats.ToDictionary(kv => kv.Key, kv => kv.Value);
208212
}
209213

0 commit comments

Comments
 (0)