Skip to content

Commit bac8cbe

Browse files
committed
Simplification of code in constraint enumerator.
1 parent b506f29 commit bac8cbe

File tree

5 files changed

+171
-222
lines changed

5 files changed

+171
-222
lines changed

BTDB/KVDBLayer/Helpers/ExtensionMethods.cs

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Threading;
4+
using BTDB.StreamLayer;
45

56
namespace BTDB.KVDBLayer;
67

@@ -14,6 +15,17 @@ public static byte[] SlowGetKey(this IKeyValueDBCursor cursor)
1415
return buf;
1516
}
1617

18+
public static void GetKeyIntoMemWriter(this IKeyValueDBCursor cursor, ref MemWriter writer)
19+
{
20+
writer.Reset();
21+
var span = cursor.GetKeySpan(
22+
writer.BlockWriteToSpan((int)cursor.GetStorageSizeOfCurrentKey().Key, out var needToBeWritten), true);
23+
if (needToBeWritten)
24+
{
25+
writer.WriteBlock(span);
26+
}
27+
}
28+
1729
public static byte[] SlowGetValue(this IKeyValueDBCursor cursor)
1830
{
1931
var buf = new byte[cursor.GetStorageSizeOfCurrentKey().Value];

BTDB/ODBLayer/RelationDBManipulator.cs

+21-16
Original file line numberDiff line numberDiff line change
@@ -1035,12 +1035,14 @@ public TItem FirstByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constrai
10351035
MemWriter keyBytes = new();
10361036
keyBytes.WriteBlock(_relationInfo.Prefix);
10371037

1038+
Span<byte> buffer = stackalloc byte[1024];
1039+
var writer = MemWriter.CreateFromStackAllocatedSpan(buffer);
10381040
if (orderers == null || orderers.Length == 0)
10391041
{
1040-
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes,
1042+
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes, writer,
10411043
loaderIndex, constraints);
10421044

1043-
if (enumerator.MoveNextInGather())
1045+
if (enumerator.MoveNext())
10441046
{
10451047
return enumerator.Current!;
10461048
}
@@ -1054,7 +1056,7 @@ public TItem FirstByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constrai
10541056
var primaryKeyFields = relationVersionInfo.PrimaryKeyFields.Span;
10551057
var ordererIdxs = PrepareOrderers(ref constraints, orderers, primaryKeyFields);
10561058

1057-
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes,
1059+
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes, writer,
10581060
loaderIndex, constraints);
10591061

10601062
var sns = new SortNativeStorage(true);
@@ -1085,11 +1087,12 @@ public TItem FirstBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] constr
10851087
keyBytes.WriteBlock(_relationInfo.PrefixSecondary);
10861088
var remappedSecondaryKeyIndex = RemapPrimeSK(secondaryKeyIndex);
10871089
keyBytes.WriteUInt8((byte)remappedSecondaryKeyIndex);
1088-
1090+
Span<byte> keyBuffer = stackalloc byte[1024];
1091+
var writer = MemWriter.CreateFromStackAllocatedSpan(keyBuffer);
10891092
if (orderers == null || orderers.Length == 0)
10901093
{
10911094
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
1092-
keyBytes,
1095+
keyBytes, writer,
10931096
loaderIndex, constraints, remappedSecondaryKeyIndex, this);
10941097

10951098
if (enumerator.MoveNextInGather())
@@ -1108,7 +1111,7 @@ public TItem FirstBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] constr
11081111
var ordererIdxs = PrepareOrderersSK(ref constraints, orderers, fields, relationVersionInfo);
11091112

11101113
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
1111-
keyBytes,
1114+
keyBytes, writer,
11121115
loaderIndex, constraints, remappedSecondaryKeyIndex, this);
11131116

11141117
var sns = new SortNativeStorage(true);
@@ -1241,20 +1244,23 @@ int[] PrepareOrderers(ref ConstraintInfo[] constraints, IOrderer[] orderers,
12411244
return (count, keySizes, valueSizes);
12421245
}
12431246

1247+
[SkipLocalsInit]
12441248
public ulong GatherByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constraints, ICollection<TItem> target,
12451249
long skip, long take, IOrderer[]? orderers)
12461250
{
1247-
MemWriter keyBytes = new();
1251+
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
12481252
keyBytes.WriteBlock(_relationInfo.Prefix);
12491253
if (skip < 0)
12501254
{
12511255
take += skip;
12521256
skip = 0;
12531257
}
12541258

1259+
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[1024]);
1260+
12551261
if (orderers == null || orderers.Length == 0)
12561262
{
1257-
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes,
1263+
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes, writer,
12581264
loaderIndex, constraints);
12591265

12601266
var count = 0ul;
@@ -1281,7 +1287,7 @@ public ulong GatherByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constra
12811287
var primaryKeyFields = relationVersionInfo.PrimaryKeyFields.Span;
12821288
var ordererIdxs = PrepareOrderers(ref constraints, orderers, primaryKeyFields);
12831289

1284-
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes,
1290+
using var enumerator = new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes, writer,
12851291
loaderIndex, constraints);
12861292

12871293
var sns = new SortNativeStorage(false);
@@ -1309,8 +1315,7 @@ public ulong GatherByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constra
13091315
public ulong GatherBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] constraints, ICollection<TItem> target,
13101316
long skip, long take, uint secondaryKeyIndex, IOrderer[]? orderers)
13111317
{
1312-
Span<byte> buf = stackalloc byte[4096];
1313-
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(buf);
1318+
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
13141319
keyBytes.WriteBlock(_relationInfo.PrefixSecondary);
13151320
var remappedSecondaryKeyIndex = RemapPrimeSK(secondaryKeyIndex);
13161321
keyBytes.WriteUInt8((byte)remappedSecondaryKeyIndex);
@@ -1319,11 +1324,11 @@ public ulong GatherBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] const
13191324
take += skip;
13201325
skip = 0;
13211326
}
1322-
1327+
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[1024]);
13231328
if (orderers == null || orderers.Length == 0)
13241329
{
13251330
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
1326-
keyBytes,
1331+
keyBytes, writer,
13271332
loaderIndex, constraints, remappedSecondaryKeyIndex, this);
13281333

13291334
var count = 0ul;
@@ -1352,7 +1357,7 @@ public ulong GatherBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] const
13521357
var ordererIdxs = PrepareOrderersSK(ref constraints, orderers, fields, relationVersionInfo);
13531358

13541359
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
1355-
keyBytes,
1360+
keyBytes, writer,
13561361
loaderIndex, constraints, remappedSecondaryKeyIndex, this);
13571362

13581363
var sns = new SortNativeStorage(false);
@@ -1424,7 +1429,7 @@ public IEnumerable<TItem> ScanByPrimaryKeyPrefix<TItem>(int loaderIndex, Constra
14241429
{
14251430
MemWriter keyBytes = new();
14261431
keyBytes.WriteBlock(_relationInfo.Prefix);
1427-
return new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes,
1432+
return new RelationConstraintEnumerator<TItem>(_transaction, _relationInfo, keyBytes, new MemWriter(),
14281433
loaderIndex, constraints);
14291434
}
14301435

@@ -1435,7 +1440,7 @@ public IEnumerable<TItem> ScanBySecondaryKeyPrefix<TItem>(int loaderIndex, Const
14351440
keyBytes.WriteBlock(_relationInfo.PrefixSecondary);
14361441
var remappedSecondaryKeyIndex = RemapPrimeSK(secondaryKeyIndex);
14371442
keyBytes.WriteUInt8((byte)remappedSecondaryKeyIndex);
1438-
return new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo, keyBytes,
1443+
return new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo, keyBytes, new MemWriter(),
14391444
loaderIndex, constraints, remappedSecondaryKeyIndex, this);
14401445
}
14411446

0 commit comments

Comments
 (0)