Skip to content

Commit

Permalink
Nullable storage classes (#3718)
Browse files Browse the repository at this point in the history
* Nullable storage

* Fix ut

* Complete ut
  • Loading branch information
shargon authored Feb 7, 2025
1 parent f99268b commit 24cf3cc
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 81 deletions.
5 changes: 4 additions & 1 deletion src/Neo/Persistence/IReadOnlyStoreView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using Neo.SmartContract;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Neo.Persistence
{
Expand Down Expand Up @@ -41,6 +44,6 @@ public interface IReadOnlyStoreView
/// <param name="key">The key to get.</param>
/// <param name="item">The entry if found, null otherwise.</param>
/// <returns>True if the entry exists, false otherwise.</returns>
bool TryGet(StorageKey key, out StorageItem item);
bool TryGet(StorageKey key, [NotNullWhen(true)] out StorageItem? item);
}
}
2 changes: 2 additions & 0 deletions src/Neo/Persistence/ISnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using System;

namespace Neo.Persistence
Expand Down
2 changes: 2 additions & 0 deletions src/Neo/Persistence/IStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using System;

namespace Neo.Persistence
Expand Down
2 changes: 2 additions & 0 deletions src/Neo/Persistence/IStoreProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

namespace Neo.Persistence
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Neo/Persistence/MemoryStoreProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

namespace Neo.Persistence
{
public class MemoryStoreProvider : IStoreProvider
Expand Down
22 changes: 15 additions & 7 deletions src/Neo/Persistence/ReadOnlyStoreView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using Neo.SmartContract;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Neo.Persistence
{
public class ReadOnlyStoreView : IReadOnlyStoreView
{
private readonly IReadOnlyStore store;
private readonly IReadOnlyStore _store;

public ReadOnlyStoreView(IReadOnlyStore store)
{
this.store = store;
_store = store;
}

/// <inheritdoc/>
public bool Contains(StorageKey key) => store.Contains(key.ToArray());
public bool Contains(StorageKey key) => _store.Contains(key.ToArray());

/// <inheritdoc/>
public StorageItem this[StorageKey key]
Expand All @@ -38,11 +41,16 @@ public StorageItem this[StorageKey key]
}

/// <inheritdoc/>
public bool TryGet(StorageKey key, out StorageItem item)
public bool TryGet(StorageKey key, [NotNullWhen(true)] out StorageItem? item)
{
var ok = store.TryGet(key.ToArray(), out byte[] value);
item = ok ? new StorageItem(value) : null;
return ok;
if (_store.TryGet(key.ToArray(), out var value))
{
item = new StorageItem(value);
return true;
}

item = null;
return false;
}
}
}
14 changes: 8 additions & 6 deletions src/Neo/Persistence/StoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using System.Collections.Generic;

namespace Neo.Persistence
{
public static class StoreFactory
{
private static readonly Dictionary<string, IStoreProvider> providers = new();
private static readonly Dictionary<string, IStoreProvider> s_providers = [];

static StoreFactory()
{
var memProvider = new MemoryStoreProvider();
RegisterProvider(memProvider);

// Default cases
providers.Add("", memProvider);
s_providers.Add("", memProvider);
}

public static void RegisterProvider(IStoreProvider provider)
{
providers.Add(provider.Name, provider);
s_providers.Add(provider.Name, provider);
}

/// <summary>
/// Get store provider by name
/// </summary>
/// <param name="name">Name</param>
/// <returns>Store provider</returns>
public static IStoreProvider GetStoreProvider(string name)
public static IStoreProvider? GetStoreProvider(string name)
{
if (providers.TryGetValue(name, out var provider))
if (s_providers.TryGetValue(name, out var provider))
{
return provider;
}
Expand All @@ -54,7 +56,7 @@ public static IStoreProvider GetStoreProvider(string name)
/// <returns>The storage engine.</returns>
public static IStore GetStore(string storageProvider, string path)
{
return providers[storageProvider].GetStore(path);
return s_providers[storageProvider].GetStore(path);
}
}
}
25 changes: 12 additions & 13 deletions src/Neo/SmartContract/KeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

#nullable enable

using Neo.IO;
using System;
using System.Buffers.Binary;
Expand All @@ -22,7 +24,7 @@ namespace Neo.SmartContract
/// </summary>
public class KeyBuilder
{
private readonly MemoryStream stream;
private readonly MemoryStream _stream;

/// <summary>
/// Initializes a new instance of the <see cref="KeyBuilder"/> class.
Expand All @@ -35,9 +37,9 @@ public KeyBuilder(int id, byte prefix, int keySizeHint = ApplicationEngine.MaxSt
Span<byte> data = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteInt32LittleEndian(data, id);

stream = new(keySizeHint);
stream.Write(data);
stream.WriteByte(prefix);
_stream = new(keySizeHint);
_stream.Write(data);
_stream.WriteByte(prefix);
}

/// <summary>
Expand All @@ -48,7 +50,7 @@ public KeyBuilder(int id, byte prefix, int keySizeHint = ApplicationEngine.MaxSt
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public KeyBuilder Add(byte key)
{
stream.WriteByte(key);
_stream.WriteByte(key);
return this;
}

Expand All @@ -60,7 +62,7 @@ public KeyBuilder Add(byte key)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public KeyBuilder Add(ReadOnlySpan<byte> key)
{
stream.Write(key);
_stream.Write(key);
return this;
}

Expand Down Expand Up @@ -95,7 +97,7 @@ public KeyBuilder Add(ReadOnlySpan<byte> key)
/// <returns>A reference to this instance after the add operation has completed.</returns>
public KeyBuilder Add(ISerializable key)
{
using (BinaryWriter writer = new(stream, Utility.StrictUTF8, true))
using (BinaryWriter writer = new(_stream, Utility.StrictUTF8, true))
{
key.Serialize(writer);
writer.Flush();
Expand Down Expand Up @@ -165,18 +167,15 @@ public KeyBuilder AddBigEndian(ulong key)
/// <returns>The storage key.</returns>
public byte[] ToArray()
{
using (stream)
using (_stream)
{
return stream.ToArray();
return _stream.ToArray();
}
}

public static implicit operator StorageKey(KeyBuilder builder)
{
using (builder.stream)
{
return new StorageKey(builder.stream.ToArray());
}
return new StorageKey(builder.ToArray());
}
}
}
Loading

0 comments on commit 24cf3cc

Please sign in to comment.