Skip to content

Commit

Permalink
Merge pull request #3402 from greymistcube/refactor/keybytes
Browse files Browse the repository at this point in the history
🧹 Remove unnecessary `interface`s from `KeyBytes`
  • Loading branch information
greymistcube authored Aug 31, 2023
2 parents 92c709a + 561388d commit 1f0d79f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 67 deletions.
7 changes: 5 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ To be released.
- (Libplanet.Store) Removed `ITrie.Commit()` method. [[#3392]]
- (Libplanet.Store) Added `IStateStore.Commit()` method. [[#3398]]
- (Libplanet.Store) Removed `IKeyValueStore.Get(IEnumerable<KeyBytes> keys)`
method. [[#3362], [#3340]]
method. [[#3362], [#3400]]
- (Libplanet.Store) Added `PathCursor` struct. [[#3399]]
- (Libplanet.Store) Added `Nibbles` struct. [[#3399]]
- (Libplanet.Store) Changed `KeyBytes` to no longer implement
`IEquatable<byte[]>` and `IEquatable<ImmutableArray<byte>>`. [[#3402]]

### Backward-incompatible network protocol changes

Expand Down Expand Up @@ -54,7 +56,8 @@ To be released.
[#3392]: https://github.com/planetarium/libplanet/pull/3392
[#3398]: https://github.com/planetarium/libplanet/pull/3398
[#3399]: https://github.com/planetarium/libplanet/pull/3399
[#3340]: https://github.com/planetarium/libplanet/pull/3340
[#3400]: https://github.com/planetarium/libplanet/pull/3400
[#3402]: https://github.com/planetarium/libplanet/pull/3402
[RocksDB Read Only]: https://github.com/facebook/rocksdb/wiki/Read-only-and-Secondary-instances


Expand Down
50 changes: 4 additions & 46 deletions Libplanet.Store/Trie/KeyBytes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Libplanet.Common;
Expand All @@ -10,8 +11,7 @@ namespace Libplanet.Store.Trie
/// Wraps a byte array and provides equality comparison and hash code calculation. Designed
/// to be used as a key in dictionaries.
/// </summary>
public readonly struct KeyBytes
: IEquatable<KeyBytes>, IEquatable<ImmutableArray<byte>>, IEquatable<byte[]>
public readonly struct KeyBytes : IEquatable<KeyBytes>
{
/// <summary>
/// The default <see cref="System.Text.Encoding"/>, which is <see cref="Encoding.UTF8"/>,
Expand Down Expand Up @@ -55,7 +55,7 @@ public KeyBytes(string str)
/// </summary>
/// <param name="str">The key <see langword="string"/> to encode into bytes.</param>
/// <param name="encoding">The <see cref="System.Text.Encoding"/> to be used for
/// <paramref name="str">.</param>
/// <paramref name="str"/>.</param>
private KeyBytes(string str, Encoding encoding)
{
byte[] neverReusedBuffer = encoding.GetBytes(str);
Expand Down Expand Up @@ -125,49 +125,7 @@ public byte[] ToByteArray() => ByteArray.IsDefault
: ByteArray.ToBuilder().ToArray();

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(ImmutableArray<byte> other)
{
if (other.IsDefaultOrEmpty)
{
return _byteArray.IsDefaultOrEmpty;
}
else if (Length != other.Length)
{
return false;
}

for (int i = 0; i < Length; i++)
{
if (_byteArray[i] != other[i])
{
return false;
}
}

return true;
}

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(KeyBytes other) => Equals(other._byteArray);

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(byte[]? other)
{
if (other is { } o && o.Length == Length)
{
for (int i = 0; i < Length; i++)
{
if (_byteArray[i] != o[i])
{
return false;
}
}

return true;
}

return false;
}
public bool Equals(KeyBytes other) => ByteArray.SequenceEqual(other.ByteArray);

/// <inheritdoc cref="object.Equals(object?)"/>
public override bool Equals(object? obj) => obj is KeyBytes other && Equals(other);
Expand Down
19 changes: 0 additions & 19 deletions Libplanet.Tests/Store/Trie/KeyBytesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ public void Equality()
Assert.True(empty != b123);
Assert.True(empty != b122);
Assert.True(empty != b1234);
Assert.True(empty.Equals(ImmutableArray<byte>.Empty));
Assert.False(empty.Equals(ImmutableArray.Create<byte>(1, 2, 3)));
Assert.False(empty.Equals(ImmutableArray.Create<byte>(1, 2, 2)));
Assert.False(empty.Equals(ImmutableArray.Create<byte>(1, 2, 3, 4)));
Assert.True(empty.Equals(Array.Empty<byte>()));
Assert.False(empty.Equals(new byte[] { 1, 2, 3 }));
Assert.False(empty.Equals(new byte[] { 1, 2, 2 }));
Assert.False(empty.Equals(new byte[] { 1, 2, 3, 4 }));
Assert.False(empty.Equals((byte[])null));
Assert.False(empty.Equals((object)Array.Empty<byte>()));
Assert.True(empty.Equals((object)new KeyBytes(Array.Empty<byte>())));
Assert.False(empty.Equals((object)b123));
Expand All @@ -128,16 +119,6 @@ public void Equality()
Assert.False(b123 != new KeyBytes(1, 2, 3));
Assert.True(b123 != b122);
Assert.True(b123 != b1234);
Assert.False(b123.Equals(ImmutableArray<byte>.Empty));
Assert.True(b123.Equals(ImmutableArray.Create<byte>(1, 2, 3)));
Assert.False(b123.Equals(ImmutableArray.Create<byte>(1, 2, 2)));
Assert.False(b123.Equals(ImmutableArray.Create<byte>(1, 2, 3, 4)));
Assert.False(b123.Equals(Array.Empty<byte>()));
Assert.True(b123.Equals(new byte[] { 1, 2, 3 }));
Assert.False(b123.Equals(new byte[] { 1, 2, 2 }));
Assert.False(b123.Equals(new byte[] { 1, 2, 3, 4 }));
Assert.False(b123.Equals((byte[])null));
Assert.False(b123.Equals((object)Array.Empty<byte>()));
Assert.False(b123.Equals((object)default(KeyBytes)));
Assert.True(b123.Equals((object)b123));
Assert.False(b123.Equals((object)b122));
Expand Down

0 comments on commit 1f0d79f

Please sign in to comment.