Skip to content

Commit 35a1a9d

Browse files
author
SirJosh3917
committed
fix readonly/writeonly dbs
1 parent d96e8e6 commit 35a1a9d

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

src/StringDB/Databases/BufferedDatabase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class BufferedDatabase<TKey, TValue>
1919
/// Creates a new <see cref="BufferedDatabase{TKey, TValue}"/>
2020
/// with the specified buffer.
2121
/// </summary>
22+
/// <param name="database">The database to buffer.</param>
2223
/// <param name="bufferSize">The size of the buffer.</param>
24+
/// <param name="disposeDatabase">If the underlying database should be disposed on dispose.</param>
2325
public BufferedDatabase
2426
(
2527
[NotNull] IDatabase<TKey, TValue> database,
Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using JetBrains.Annotations;
22

33
using System;
4+
using System.Collections;
45
using System.Collections.Generic;
56

67
namespace StringDB.Databases
@@ -9,9 +10,9 @@ namespace StringDB.Databases
910
/// A database which can only be read from.
1011
/// </summary>
1112
[PublicAPI]
12-
public class ReadOnlyDatabase<TKey, TValue> : BaseDatabase<TKey, TValue>
13+
public class ReadOnlyDatabase<TKey, TValue>
14+
: IDatabase<TKey, TValue>, IDatabaseLayer<TKey, TValue>
1315
{
14-
[NotNull] private readonly IDatabase<TKey, TValue> _database;
1516
private readonly bool _disposeDatabase;
1617

1718
/// <summary>
@@ -22,23 +23,42 @@ public class ReadOnlyDatabase<TKey, TValue> : BaseDatabase<TKey, TValue>
2223
public ReadOnlyDatabase([NotNull] IDatabase<TKey, TValue> database, bool disposeDatabase = true)
2324
{
2425
_disposeDatabase = disposeDatabase;
25-
_database = database;
26+
InnerDatabase = database;
2627
}
2728

2829
/// <inheritdoc/>
29-
public override void Dispose()
30+
[NotNull] public IDatabase<TKey, TValue> InnerDatabase { get; }
31+
32+
/// <inheritdoc/>
33+
public void Dispose()
3034
{
3135
if (_disposeDatabase)
3236
{
33-
_database.Dispose();
37+
InnerDatabase.Dispose();
3438
}
3539
}
3640

41+
private const string Error = "Writing is not supported.";
42+
43+
/// <inheritdoc/>
44+
public TValue Get([NotNull] TKey key) => InnerDatabase.Get(key);
45+
46+
/// <inheritdoc/>
47+
public bool TryGet([NotNull] TKey key, [CanBeNull] out TValue value) => InnerDatabase.TryGet(key, out value);
48+
49+
/// <inheritdoc/>
50+
public IEnumerable<ILazyLoader<TValue>> GetAll([NotNull] TKey key) => InnerDatabase.GetAll(key);
51+
52+
/// <inheritdoc/>
53+
public IEnumerator<KeyValuePair<TKey, ILazyLoader<TValue>>> GetEnumerator() => InnerDatabase.GetEnumerator();
54+
55+
/// <inheritdoc/>
56+
public void Insert([NotNull] TKey key, [NotNull] TValue value) => throw new NotSupportedException(Error);
57+
3758
/// <inheritdoc/>
38-
public override void InsertRange(params KeyValuePair<TKey, TValue>[] items)
39-
=> throw new NotSupportedException($"Writing is not supported.");
59+
public void InsertRange([NotNull] params KeyValuePair<TKey, TValue>[] items) => throw new NotSupportedException(Error);
4060

4161
/// <inheritdoc/>
42-
protected override IEnumerable<KeyValuePair<TKey, ILazyLoader<TValue>>> Evaluate() => _database;
62+
IEnumerator IEnumerable.GetEnumerator() => InnerDatabase.GetEnumerator();
4363
}
4464
}
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using JetBrains.Annotations;
22

33
using System;
4+
using System.Collections;
45
using System.Collections.Generic;
56

67
namespace StringDB.Databases
@@ -9,9 +10,9 @@ namespace StringDB.Databases
910
/// A database which can only be read from.
1011
/// </summary>
1112
[PublicAPI]
12-
public class WriteOnlyDatabase<TKey, TValue> : BaseDatabase<TKey, TValue>
13+
public class WriteOnlyDatabase<TKey, TValue>
14+
: IDatabase<TKey, TValue>, IDatabaseLayer<TKey, TValue>
1315
{
14-
[NotNull] private readonly IDatabase<TKey, TValue> _database;
1516
private readonly bool _disposeDatabase;
1617

1718
/// <summary>
@@ -22,24 +23,42 @@ public class WriteOnlyDatabase<TKey, TValue> : BaseDatabase<TKey, TValue>
2223
public WriteOnlyDatabase([NotNull] IDatabase<TKey, TValue> database, bool disposeDatabase = true)
2324
{
2425
_disposeDatabase = disposeDatabase;
25-
_database = database;
26+
InnerDatabase = database;
2627
}
2728

2829
/// <inheritdoc/>
29-
public override void Dispose()
30+
[NotNull] public IDatabase<TKey, TValue> InnerDatabase { get; }
31+
32+
/// <inheritdoc/>
33+
public void Dispose()
3034
{
3135
if (_disposeDatabase)
3236
{
33-
_database.Dispose();
37+
InnerDatabase.Dispose();
3438
}
3539
}
3640

41+
private const string Error = "Reading is not supported.";
42+
43+
/// <inheritdoc/>
44+
public TValue Get([NotNull] TKey key) => throw new NotSupportedException(Error);
45+
46+
/// <inheritdoc/>
47+
public bool TryGet([NotNull] TKey key, [CanBeNull] out TValue value) => throw new NotSupportedException(Error);
48+
49+
/// <inheritdoc/>
50+
public IEnumerable<ILazyLoader<TValue>> GetAll([NotNull] TKey key) => throw new NotSupportedException(Error);
51+
52+
/// <inheritdoc/>
53+
public IEnumerator<KeyValuePair<TKey, ILazyLoader<TValue>>> GetEnumerator() => throw new NotSupportedException(Error);
54+
55+
/// <inheritdoc/>
56+
public void Insert([NotNull] TKey key, [NotNull] TValue value) => InnerDatabase.Insert(key, value);
57+
3758
/// <inheritdoc/>
38-
public override void InsertRange(params KeyValuePair<TKey, TValue>[] items)
39-
=> _database.InsertRange(items);
59+
public void InsertRange([NotNull] params KeyValuePair<TKey, TValue>[] items) => InnerDatabase.InsertRange(items);
4060

4161
/// <inheritdoc/>
42-
protected override IEnumerable<KeyValuePair<TKey, ILazyLoader<TValue>>> Evaluate()
43-
=> throw new NotSupportedException($"Reading is not supported.");
62+
IEnumerator IEnumerable.GetEnumerator() => InnerDatabase.GetEnumerator();
4463
}
4564
}

0 commit comments

Comments
 (0)