-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix
]: keyPrefix
in DataCache.Find
can be all 0xff.
#3681
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,50 +202,58 @@ public void Delete(StorageKey key) | |
/// <summary> | ||
/// Finds the entries starting with the specified prefix. | ||
/// </summary> | ||
/// <param name="key_prefix">The prefix of the key.</param> | ||
/// <param name="keyPrefix">The prefix of the key.</param> | ||
/// <param name="direction">The search direction.</param> | ||
/// <returns>The entries found with the desired prefix.</returns> | ||
public IEnumerable<(StorageKey Key, StorageItem Value)> Find(byte[]? key_prefix = null, SeekDirection direction = SeekDirection.Forward) | ||
public IEnumerable<(StorageKey Key, StorageItem Value)> Find(byte[]? keyPrefix = null, SeekDirection direction = SeekDirection.Forward) | ||
{ | ||
var seek_prefix = key_prefix; | ||
var seekPrefix = keyPrefix; | ||
if (direction == SeekDirection.Backward) | ||
{ | ||
if (key_prefix == null) | ||
if (keyPrefix == null) | ||
{ | ||
// Backwards seek for null prefix is not supported for now. | ||
throw new ArgumentNullException(nameof(key_prefix)); | ||
throw new ArgumentNullException(nameof(keyPrefix)); | ||
} | ||
if (key_prefix.Length == 0) | ||
if (keyPrefix.Length == 0) | ||
{ | ||
// Backwards seek for zero prefix is not supported for now. | ||
throw new ArgumentOutOfRangeException(nameof(key_prefix)); | ||
throw new ArgumentOutOfRangeException(nameof(keyPrefix)); | ||
} | ||
seek_prefix = null; | ||
for (var i = key_prefix.Length - 1; i >= 0; i--) | ||
|
||
seekPrefix = null; | ||
for (var i = keyPrefix.Length - 1; i >= 0; i--) | ||
{ | ||
if (key_prefix[i] < 0xff) | ||
if (keyPrefix[i] < 0xff) | ||
{ | ||
seek_prefix = key_prefix.Take(i + 1).ToArray(); | ||
// The next key after the key_prefix. | ||
seek_prefix[i]++; | ||
seekPrefix = keyPrefix.Take(i + 1).ToArray(); | ||
seekPrefix[i]++; // The next key after the key_prefix. | ||
break; | ||
} | ||
} | ||
if (seek_prefix == null) | ||
|
||
if (seekPrefix == null) | ||
{ | ||
throw new ArgumentException($"{nameof(key_prefix)} with all bytes being 0xff is not supported now"); | ||
// This case is rare | ||
seekPrefix = new byte[ApplicationEngine.MaxStorageKeySize + 1]; | ||
Array.Fill(seekPrefix, (byte)0xff); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a logic change @shargon? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it doesn't matter. |
||
} | ||
|
||
} | ||
return FindInternal(key_prefix, seek_prefix, direction); | ||
|
||
return FindInternal(keyPrefix, seekPrefix, direction); | ||
} | ||
|
||
private IEnumerable<(StorageKey Key, StorageItem Value)> FindInternal(byte[]? key_prefix, byte[]? seek_prefix, SeekDirection direction) | ||
private IEnumerable<(StorageKey Key, StorageItem Value)> FindInternal(byte[]? keyPrefix, byte[]? seekPrefix, SeekDirection direction) | ||
{ | ||
foreach (var (key, value) in Seek(seek_prefix, direction)) | ||
if (key_prefix == null || key.ToArray().AsSpan().StartsWith(key_prefix)) | ||
var prefixIsEmpty = keyPrefix == null || keyPrefix.Length == 0; | ||
foreach (var (key, value) in Seek(seekPrefix, direction)) | ||
{ | ||
if (prefixIsEmpty || key.ToArray().AsSpan().StartsWith(keyPrefix)) | ||
yield return (key, value); | ||
else if (direction == SeekDirection.Forward || (seek_prefix == null || !key.ToArray().SequenceEqual(seek_prefix))) | ||
else if (direction == SeekDirection.Forward || (seekPrefix == null || !key.ToArray().SequenceEqual(seekPrefix))) | ||
yield break; | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -261,10 +269,12 @@ public void Delete(StorageKey key) | |
? ByteArrayComparer.Default | ||
: ByteArrayComparer.Reverse; | ||
foreach (var (key, value) in Seek(start, direction)) | ||
{ | ||
if (comparer.Compare(key.ToArray(), end) < 0) | ||
yield return (key, value); | ||
else | ||
yield break; | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -415,31 +425,24 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory) | |
{ | ||
IEnumerable<(byte[], StorageKey, StorageItem)> cached; | ||
HashSet<StorageKey> cachedKeySet; | ||
ByteArrayComparer comparer = direction == SeekDirection.Forward ? ByteArrayComparer.Default : ByteArrayComparer.Reverse; | ||
var comparer = direction == SeekDirection.Forward ? ByteArrayComparer.Default : ByteArrayComparer.Reverse; | ||
lock (_dictionary) | ||
{ | ||
cached = _dictionary | ||
.Where(p => p.Value.State != TrackState.Deleted && p.Value.State != TrackState.NotFound && (keyOrPrefix == null || comparer.Compare(p.Key.ToArray(), keyOrPrefix) >= 0)) | ||
.Select(p => | ||
( | ||
KeyBytes: p.Key.ToArray(), | ||
p.Key, | ||
p.Value.Item | ||
)) | ||
.Select(p => (KeyBytes: p.Key.ToArray(), p.Key, p.Value.Item)) | ||
.OrderBy(p => p.KeyBytes, comparer) | ||
.ToArray(); | ||
cachedKeySet = new HashSet<StorageKey>(_dictionary.Keys); | ||
} | ||
|
||
var uncached = SeekInternal(keyOrPrefix ?? Array.Empty<byte>(), direction) | ||
.Where(p => !cachedKeySet.Contains(p.Key)) | ||
.Select(p => | ||
( | ||
KeyBytes: p.Key.ToArray(), | ||
p.Key, | ||
p.Value | ||
)); | ||
.Select(p => (KeyBytes: p.Key.ToArray(), p.Key, p.Value)); | ||
|
||
using var e1 = cached.GetEnumerator(); | ||
using var e2 = uncached.GetEnumerator(); | ||
|
||
(byte[] KeyBytes, StorageKey Key, StorageItem Item) i1, i2; | ||
bool c1 = e1.MoveNext(); | ||
bool c2 = e2.MoveNext(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MaxStorageKeySize.
Maybe not long enough.