@@ -25,7 +25,7 @@ class RelationConstraintEnumerator<T> : IEnumerator<T>, IEnumerable<T> where T :
25
25
protected readonly RelationInfo . ItemLoaderInfo ItemLoader ;
26
26
readonly ConstraintInfo [ ] _constraints ;
27
27
protected readonly IKeyValueDBTransaction KeyValueTr ;
28
- protected IKeyValueDBCursor _cursor ;
28
+ protected IKeyValueDBCursor ? _cursor ;
29
29
30
30
bool _seekNeeded ;
31
31
@@ -123,7 +123,7 @@ unsafe bool FindNextKey(bool first = false)
123
123
_constraints [ i ] . Constraint . WritePrefix ( ref writer , _buffer ) ;
124
124
goto case IConstraint . MatchType . NoPrefix ;
125
125
case IConstraint . MatchType . NoPrefix :
126
- if ( ! _cursor . FindFirstKey ( writer . GetSpan ( ) ) ) return false ;
126
+ if ( ! _cursor ! . FindFirstKey ( writer . GetSpan ( ) ) ) return false ;
127
127
goto nextKeyTest ;
128
128
default :
129
129
throw new InvalidOperationException ( ) ;
@@ -132,7 +132,7 @@ unsafe bool FindNextKey(bool first = false)
132
132
i ++ ;
133
133
}
134
134
135
- if ( _cursor . FindFirstKey ( writer . GetSpan ( ) ) )
135
+ if ( _cursor ! . FindFirstKey ( writer . GetSpan ( ) ) )
136
136
{
137
137
_cursor . GetKeyIntoMemWriter ( ref writer ) ;
138
138
return true ;
@@ -142,7 +142,7 @@ unsafe bool FindNextKey(bool first = false)
142
142
}
143
143
144
144
goNextKey :
145
- if ( ! _cursor . FindNextKey ( new ( ) ) ) return false ;
145
+ if ( ! _cursor ! . FindNextKey ( new ( ) ) ) return false ;
146
146
nextKeyTest :
147
147
var key = _cursor . GetKeySpan ( ref buf ) ;
148
148
var commonUpToOffset = ( uint ) writer . GetSpan ( ) . CommonPrefixLength ( key ) ;
@@ -303,32 +303,32 @@ unsafe bool FindNextKey(bool first = false)
303
303
return false ;
304
304
}
305
305
306
- public virtual T Current
307
- {
308
- get => ( T ) ItemLoader . CreateInstance ( _transaction , _cursor , _key . GetSpan ( ) ) ;
309
- }
306
+ public virtual T Current => ( T ) ItemLoader . CreateInstance ( _transaction , _cursor ! , _key . GetSpan ( ) ) ;
310
307
311
308
public virtual T CurrentFromKeySpan ( ReadOnlySpan < byte > key )
312
309
{
313
- return ( T ) ItemLoader . CreateInstance ( _transaction , _cursor , key ) ;
310
+ return ( T ) ItemLoader . CreateInstance ( _transaction , _cursor ! , key ) ;
314
311
}
315
312
316
313
object IEnumerator . Current => Current ! ;
317
314
318
315
public void Reset ( )
319
316
{
320
- _cursor . Invalidate ( ) ;
317
+ _cursor ! . Invalidate ( ) ;
321
318
_seekNeeded = true ;
322
319
}
323
320
324
321
public void Dispose ( )
325
322
{
326
- _cursor . Dispose ( ) ;
323
+ _cursor ! . Dispose ( ) ;
327
324
_cursor = null ! ;
328
325
}
329
326
330
327
public IEnumerator < T > GetEnumerator ( )
331
328
{
329
+ _seekNeeded = true ;
330
+ _cursor ??= KeyValueTr . CreateCursor ( ) ;
331
+
332
332
return this ;
333
333
}
334
334
@@ -339,7 +339,7 @@ IEnumerator IEnumerable.GetEnumerator()
339
339
340
340
public T CurrentByKeyIndex ( ulong keyIndex )
341
341
{
342
- _cursor . FindKeyIndex ( ( long ) keyIndex ) ;
342
+ _cursor ! . FindKeyIndex ( ( long ) keyIndex ) ;
343
343
_cursor . GetKeyIntoMemWriter ( ref _key ) ;
344
344
return Current ;
345
345
}
@@ -365,7 +365,7 @@ public unsafe ulong FastGather(long skip, long take, ICollection<T> target)
365
365
_constraints [ i ] . Constraint . WritePrefix ( ref writer , _buffer ) ;
366
366
goto case IConstraint . MatchType . NoPrefix ;
367
367
case IConstraint . MatchType . NoPrefix :
368
- if ( ! _cursor . FindFirstKey ( writer . GetSpan ( ) ) ) return 0 ;
368
+ if ( ! _cursor ! . FindFirstKey ( writer . GetSpan ( ) ) ) return 0 ;
369
369
goto startIteration ;
370
370
default :
371
371
throw new InvalidOperationException ( ) ;
@@ -376,7 +376,7 @@ public unsafe ulong FastGather(long skip, long take, ICollection<T> target)
376
376
377
377
startIteration :
378
378
var count = 0UL ;
379
- _cursor . FastIterate ( ref buf , ( index , key ) =>
379
+ _cursor ! . FastIterate ( ref buf , ( index , key ) =>
380
380
{
381
381
if ( ! key . StartsWith ( _key . GetSpan ( ) ) ) return true ;
382
382
fixed ( void * _ = key )
@@ -451,11 +451,11 @@ public RelationConstraintSecondaryKeyEnumerator(IInternalObjectDBTransaction tr,
451
451
}
452
452
453
453
public override T Current =>
454
- ( T ) _manipulator . CreateInstanceFromSecondaryKey ( ItemLoader , _secondaryKeyIndex , _key . GetSpan ( ) ) ;
454
+ ( T ) _manipulator . CreateInstanceFromSecondaryKey ( ItemLoader , _secondaryKeyIndex , _key . GetSpan ( ) ) ! ;
455
455
456
456
public override T CurrentFromKeySpan ( ReadOnlySpan < byte > key )
457
457
{
458
- return ( T ) _manipulator . CreateInstanceFromSecondaryKey ( ItemLoader , _secondaryKeyIndex , key ) ;
458
+ return ( T ) _manipulator . CreateInstanceFromSecondaryKey ( ItemLoader , _secondaryKeyIndex , key ) ! ;
459
459
}
460
460
}
461
461
@@ -465,10 +465,10 @@ class RelationEnumerator<T> : IEnumerator<T>, IEnumerable<T>
465
465
protected readonly RelationInfo . ItemLoaderInfo ItemLoader ;
466
466
readonly IKeyValueDBTransaction _keyValueTr ;
467
467
468
- protected IKeyValueDBCursor ? _cursor ;
468
+ public IKeyValueDBCursor ? Cursor ;
469
469
bool _seekNeeded ;
470
470
471
- protected readonly byte [ ] KeyBytes ;
471
+ readonly byte [ ] _keyBytes ;
472
472
473
473
public RelationEnumerator ( IInternalObjectDBTransaction tr , RelationInfo relationInfo ,
474
474
ReadOnlySpan < byte > keyBytes , int loaderIndex ) : this ( tr , keyBytes . ToArray ( ) ,
@@ -488,53 +488,53 @@ public RelationEnumerator(IInternalObjectDBTransaction tr, byte[] keyBytes, Rela
488
488
ItemLoader = loaderInfo ;
489
489
_keyValueTr = _transaction . KeyValueDBTransaction ;
490
490
491
- KeyBytes = keyBytes ;
491
+ _keyBytes = keyBytes ;
492
492
_seekNeeded = true ;
493
493
}
494
494
495
495
public bool MoveNext ( )
496
496
{
497
- ObjectDisposedException . ThrowIf ( _cursor == null , this ) ;
497
+ ObjectDisposedException . ThrowIf ( Cursor == null , this ) ;
498
498
_transaction . ThrowIfDisposed ( ) ;
499
499
if ( _seekNeeded )
500
500
{
501
- var ret = _cursor . FindFirstKey ( KeyBytes ) ;
501
+ var ret = Cursor . FindFirstKey ( _keyBytes ) ;
502
502
_seekNeeded = false ;
503
503
return ret ;
504
504
}
505
505
506
- return _cursor . FindNextKey ( KeyBytes ) ;
506
+ return Cursor . FindNextKey ( _keyBytes ) ;
507
507
}
508
508
509
509
public T Current
510
510
{
511
511
[ SkipLocalsInit ]
512
512
get
513
513
{
514
- ObjectDisposedException . ThrowIf ( _cursor == null , this ) ;
514
+ ObjectDisposedException . ThrowIf ( Cursor == null , this ) ;
515
515
Span < byte > keyBuffer = stackalloc byte [ 2048 ] ;
516
- var keyBytes = _cursor . GetKeySpan ( keyBuffer ) ;
516
+ var keyBytes = Cursor . GetKeySpan ( keyBuffer ) ;
517
517
return CreateInstance ( keyBytes ) ;
518
518
}
519
519
}
520
520
521
521
protected virtual T CreateInstance ( in ReadOnlySpan < byte > keyBytes )
522
522
{
523
- return ( T ) ItemLoader . CreateInstance ( _transaction , _cursor ! , keyBytes ) ;
523
+ return ( T ) ItemLoader . CreateInstance ( _transaction , Cursor ! , keyBytes ) ;
524
524
}
525
525
526
526
object IEnumerator . Current => Current ! ;
527
527
528
528
public void Reset ( )
529
529
{
530
- _cursor ??= _keyValueTr . CreateCursor ( ) ;
530
+ Cursor ??= _keyValueTr . CreateCursor ( ) ;
531
531
_seekNeeded = true ;
532
532
}
533
533
534
534
public void Dispose ( )
535
535
{
536
- _cursor ? . Dispose ( ) ;
537
- _cursor = null ;
536
+ Cursor ? . Dispose ( ) ;
537
+ Cursor = null ;
538
538
}
539
539
540
540
public IEnumerator < T > GetEnumerator ( )
@@ -556,8 +556,6 @@ public RelationPrimaryKeyEnumerator(IInternalObjectDBTransaction tr, RelationInf
556
556
: base ( tr , relationInfo , keyBytes , loaderIndex )
557
557
{
558
558
}
559
-
560
- public IKeyValueDBCursor Cursor => _cursor ;
561
559
}
562
560
563
561
class RelationSecondaryKeyEnumerator < T > : RelationEnumerator < T >
@@ -945,13 +943,14 @@ public class RelationAdvancedOrderedEnumerator<TKey, TValue> : IOrderedDictionar
945
943
protected readonly IRelationDbManipulator Manipulator ;
946
944
protected readonly RelationInfo . ItemLoaderInfo ItemLoader ;
947
945
readonly IInternalObjectDBTransaction _tr ;
946
+ // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable could be useful for debugging
948
947
readonly IKeyValueDBTransaction _keyValueTr ;
949
948
IKeyValueDBCursor ? _startCursor ;
950
949
IKeyValueDBCursor ? _endCursor ;
951
950
IKeyValueDBCursor ? _cursor ;
952
951
bool _seekNeeded ;
953
952
readonly bool _ascending ;
954
- protected readonly byte [ ] KeyBytes ;
953
+ readonly byte [ ] _keyBytes ;
955
954
protected ReaderFun < TKey > ? KeyReader ;
956
955
957
956
public RelationAdvancedOrderedEnumerator ( IRelationDbManipulator manipulator , EnumerationOrder order ,
@@ -968,13 +967,13 @@ public RelationAdvancedOrderedEnumerator(IRelationDbManipulator manipulator, Enu
968
967
_keyValueTr = _tr . KeyValueDBTransaction ;
969
968
_seekNeeded = true ;
970
969
971
- KeyBytes = startKeyBytes . Slice ( 0 , prefixLen ) . ToArray ( ) ;
970
+ _keyBytes = startKeyBytes [ .. prefixLen ] . ToArray ( ) ;
972
971
973
972
974
973
_startCursor = _keyValueTr . CreateCursor ( ) ;
975
974
if ( startKeyProposition == KeyProposition . Ignored )
976
975
{
977
- if ( ! _startCursor . FindFirstKey ( KeyBytes ) )
976
+ if ( ! _startCursor . FindFirstKey ( _keyBytes ) )
978
977
{
979
978
return ;
980
979
}
@@ -986,12 +985,12 @@ public RelationAdvancedOrderedEnumerator(IRelationDbManipulator manipulator, Enu
986
985
case FindResult . Exact :
987
986
if ( startKeyProposition == KeyProposition . Excluded )
988
987
{
989
- if ( ! _startCursor . FindNextKey ( KeyBytes ) ) return ;
988
+ if ( ! _startCursor . FindNextKey ( _keyBytes ) ) return ;
990
989
}
991
990
992
991
break ;
993
992
case FindResult . Previous :
994
- if ( ! _startCursor . FindNextKey ( KeyBytes ) ) return ;
993
+ if ( ! _startCursor . FindNextKey ( _keyBytes ) ) return ;
995
994
break ;
996
995
case FindResult . Next :
997
996
break ;
@@ -1009,7 +1008,7 @@ public RelationAdvancedOrderedEnumerator(IRelationDbManipulator manipulator, Enu
1009
1008
1010
1009
if ( endKeyProposition == KeyProposition . Ignored )
1011
1010
{
1012
- if ( ! _endCursor . FindLastKey ( KeyBytes ) ) return ;
1011
+ if ( ! _endCursor . FindLastKey ( _keyBytes ) ) return ;
1013
1012
}
1014
1013
else
1015
1014
{
@@ -1018,14 +1017,14 @@ public RelationAdvancedOrderedEnumerator(IRelationDbManipulator manipulator, Enu
1018
1017
case FindResult . Exact :
1019
1018
if ( endKeyProposition == KeyProposition . Excluded )
1020
1019
{
1021
- if ( ! _endCursor . FindPreviousKey ( KeyBytes ) ) return ;
1020
+ if ( ! _endCursor . FindPreviousKey ( _keyBytes ) ) return ;
1022
1021
}
1023
1022
1024
1023
break ;
1025
1024
case FindResult . Previous :
1026
1025
break ;
1027
1026
case FindResult . Next :
1028
- if ( ! _endCursor . FindPreviousKey ( KeyBytes ) ) return ;
1027
+ if ( ! _endCursor . FindPreviousKey ( _keyBytes ) ) return ;
1029
1028
break ;
1030
1029
case FindResult . NotFound :
1031
1030
return ;
@@ -1115,7 +1114,7 @@ public unsafe bool NextKey(out TKey key)
1115
1114
{
1116
1115
if ( _ascending )
1117
1116
{
1118
- if ( ! _cursor . FindNextKey ( KeyBytes ) )
1117
+ if ( ! _cursor . FindNextKey ( _keyBytes ) )
1119
1118
{
1120
1119
key = default ;
1121
1120
return false ;
@@ -1129,7 +1128,7 @@ public unsafe bool NextKey(out TKey key)
1129
1128
}
1130
1129
else
1131
1130
{
1132
- if ( ! _cursor . FindPreviousKey ( KeyBytes ) )
1131
+ if ( ! _cursor . FindPreviousKey ( _keyBytes ) )
1133
1132
{
1134
1133
key = default ;
1135
1134
return false ;
@@ -1143,7 +1142,7 @@ public unsafe bool NextKey(out TKey key)
1143
1142
}
1144
1143
}
1145
1144
1146
- var keySpan = _cursor . GetKeySpan ( stackalloc byte [ 2048 ] ) [ KeyBytes . Length ..] ;
1145
+ var keySpan = _cursor . GetKeySpan ( stackalloc byte [ 2048 ] ) [ _keyBytes . Length ..] ;
1147
1146
fixed ( void * _ = keySpan )
1148
1147
{
1149
1148
var reader = MemReader . CreateFromPinnedSpan ( keySpan ) ;
0 commit comments