From 5dd28a2a484cef1b56756b8fce56c56a48d47263 Mon Sep 17 00:00:00 2001 From: Anatoly Popov Date: Sun, 11 Feb 2018 21:49:26 +0300 Subject: [PATCH 1/2] Prototype. And I don't like it --- src/progaudi.tarantool/ISchema.cs | 6 ++-- src/progaudi.tarantool/ISpace.cs | 10 +------ src/progaudi.tarantool/Schema.cs | 35 ++++++++++++++++++++-- src/progaudi.tarantool/Space.cs | 49 ++++++++++++++++++++++--------- 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/src/progaudi.tarantool/ISchema.cs b/src/progaudi.tarantool/ISchema.cs index b0d1e9cb..9c7f92fe 100644 --- a/src/progaudi.tarantool/ISchema.cs +++ b/src/progaudi.tarantool/ISchema.cs @@ -1,18 +1,16 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace ProGaudi.Tarantool.Client { - public interface ISchema + public interface ISchema : IReadOnlyDictionary, IReadOnlyDictionary { [Obsolete("Use indexer")] Task GetSpace(string name); [Obsolete("Use indexer")] Task GetSpace(uint id); - ISpace this[string name] { get; } - ISpace this[uint id] { get; } - Task Reload(); DateTimeOffset LastReloadTime { get; } diff --git a/src/progaudi.tarantool/ISpace.cs b/src/progaudi.tarantool/ISpace.cs index f6e04e4f..ea0c3f99 100644 --- a/src/progaudi.tarantool/ISpace.cs +++ b/src/progaudi.tarantool/ISpace.cs @@ -8,7 +8,7 @@ namespace ProGaudi.Tarantool.Client { - public interface ISpace + public interface ISpace : IReadOnlyDictionary, IReadOnlyDictionary { uint Id { get; } @@ -30,10 +30,6 @@ public interface ISpace [Obsolete("Use indexer")] Task GetIndex(uint indexId); - IIndex this[string name] { get; } - - IIndex this[uint id] { get; } - Task> Insert(TTuple tuple); Task> Select(TKey selectKey); @@ -50,10 +46,6 @@ public interface ISpace Task> Delete(TKey key); - Task Count(TKey key); - - Task Length(); - Task> Increment(TKey key); Task> Decrement(TKey key); diff --git a/src/progaudi.tarantool/Schema.cs b/src/progaudi.tarantool/Schema.cs index a377caed..4cd8ddf0 100644 --- a/src/progaudi.tarantool/Schema.cs +++ b/src/progaudi.tarantool/Schema.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -31,9 +32,13 @@ public Schema(ILogicalConnection logicalConnection) public Task GetSpace(uint id) => Task.FromResult(this[id]); - public ISpace this[string name] => _indexByName.TryGetValue(name, out var space) ? space : throw ExceptionHelper.InvalidSpaceName(name); + public ISpace this[string name] => _indexByName.TryGetValue(name, out var space) + ? space + : throw ExceptionHelper.InvalidSpaceName(name); - public ISpace this[uint id] => _indexById.TryGetValue(id, out var space) ? space : throw ExceptionHelper.InvalidSpaceId(id); + public ISpace this[uint id] => _indexById.TryGetValue(id, out var space) + ? space + : throw ExceptionHelper.InvalidSpaceId(id); public DateTimeOffset LastReloadTime { get; private set; } @@ -65,5 +70,31 @@ private async Task Select(uint spaceId, Iterator iterator = Iterator.All .ConfigureAwait(false); return response.Data; } + + IEnumerator> IEnumerable>.GetEnumerator() => _indexById.GetEnumerator(); + + IEnumerator> IEnumerable>.GetEnumerator() => _indexByName.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => throw new InvalidOperationException(); + + int IReadOnlyCollection>.Count => _indexById.Count; + + bool IReadOnlyDictionary.ContainsKey(string key) => _indexByName.ContainsKey(key); + + bool IReadOnlyDictionary.TryGetValue(string key, out ISpace value) => _indexByName.TryGetValue(key, out value); + + bool IReadOnlyDictionary.ContainsKey(uint key) => _indexById.ContainsKey(key); + + bool IReadOnlyDictionary.TryGetValue(uint key, out ISpace value) => _indexById.TryGetValue(key, out value); + + IEnumerable IReadOnlyDictionary.Keys => _indexById.Keys; + + IEnumerable IReadOnlyDictionary.Values => _indexById.Values; + + IEnumerable IReadOnlyDictionary.Keys => _indexByName.Keys; + + IEnumerable IReadOnlyDictionary.Values => _indexByName.Values; + + int IReadOnlyCollection>.Count => _indexById.Count; } } \ No newline at end of file diff --git a/src/progaudi.tarantool/Space.cs b/src/progaudi.tarantool/Space.cs index 848122a6..91519f92 100644 --- a/src/progaudi.tarantool/Space.cs +++ b/src/progaudi.tarantool/Space.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -60,13 +61,17 @@ internal void SetIndices(IReadOnlyCollection value) public IReadOnlyCollection Fields { get; } - public Task GetIndex(string name) => Task.FromResult(_indexByName.TryGetValue(name, out var index) ? index : throw ExceptionHelper.InvalidIndexName(name, Name)); + public Task GetIndex(string name) => Task.FromResult(this[name]); - public Task GetIndex(uint id) => Task.FromResult(_indexById.TryGetValue(id, out var index) ? index : throw ExceptionHelper.InvalidIndexId(id, Name)); + public Task GetIndex(uint id) => Task.FromResult(this[id]); - public IIndex this[string name] => _indexByName.TryGetValue(name, out var index) ? index : throw ExceptionHelper.InvalidIndexName(name, Name); + public IIndex this[string name] => _indexByName.TryGetValue(name, out var index) + ? index + : throw ExceptionHelper.InvalidIndexName(name, Name); - public IIndex this[uint id] => _indexById.TryGetValue(id, out var index) ? index : throw ExceptionHelper.InvalidIndexId(id, Name); + public IIndex this[uint id] => _indexById.TryGetValue(id, out var index) + ? index + : throw ExceptionHelper.InvalidIndexId(id, Name); public async Task> Insert(TTuple tuple) { @@ -117,16 +122,6 @@ public async Task> Delete(TKey key) return await LogicalConnection.SendRequest, TTuple>(deleteRequest).ConfigureAwait(false); } - public Task Count(TKey key) - { - throw new NotImplementedException(); - } - - public Task Length() - { - throw new NotImplementedException(); - } - public Task> Increment(TKey key) { // Currently we can't impelment that method because Upsert returns void. @@ -143,5 +138,31 @@ public override string ToString() { return $"{Name}, id={Id}"; } + + IEnumerator> IEnumerable>.GetEnumerator() => _indexById.GetEnumerator(); + + IEnumerator> IEnumerable>.GetEnumerator() => _indexByName.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => throw new InvalidOperationException(); + + int IReadOnlyCollection>.Count => _indexById.Count; + + bool IReadOnlyDictionary.ContainsKey(string key) => _indexByName.ContainsKey(key); + + bool IReadOnlyDictionary.TryGetValue(string key, out IIndex value) => _indexByName.TryGetValue(key, out value); + + bool IReadOnlyDictionary.ContainsKey(uint key) => _indexById.ContainsKey(key); + + bool IReadOnlyDictionary.TryGetValue(uint key, out IIndex value) => _indexById.TryGetValue(key, out value); + + IEnumerable IReadOnlyDictionary.Keys => _indexById.Keys; + + IEnumerable IReadOnlyDictionary.Values => _indexById.Values; + + IEnumerable IReadOnlyDictionary.Keys => _indexByName.Keys; + + IEnumerable IReadOnlyDictionary.Values => _indexByName.Values; + + int IReadOnlyCollection>.Count => _indexById.Count; } } \ No newline at end of file From b545a20d49d88a50bd5ba28924d5a0fae351a057 Mon Sep 17 00:00:00 2001 From: Anatoly Popov Date: Sun, 11 Feb 2018 21:54:21 +0300 Subject: [PATCH 2/2] Ugly iteration --- .../Schema/GetSpace_Should.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/progaudi.tarantool.tests/Schema/GetSpace_Should.cs b/tests/progaudi.tarantool.tests/Schema/GetSpace_Should.cs index ec221675..2a67cfb1 100644 --- a/tests/progaudi.tarantool.tests/Schema/GetSpace_Should.cs +++ b/tests/progaudi.tarantool.tests/Schema/GetSpace_Should.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Xunit; @@ -81,5 +82,17 @@ public async Task read_multiple_spaces_in_a_row() } } } + + [Fact] + public async Task iterating_over_schema() + { + using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7())) + { + foreach (var pair in (IReadOnlyDictionary)tarantoolClient.Schema) + { + pair.Value.Name.ShouldBe(pair.Key); + } + } + } } } \ No newline at end of file