Skip to content
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

We need a way to test for space existance without exception #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/progaudi.tarantool/ISchema.cs
Original file line number Diff line number Diff line change
@@ -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<string, ISpace>, IReadOnlyDictionary<uint, ISpace>
{
[Obsolete("Use indexer")]
Task<ISpace> GetSpace(string name);
[Obsolete("Use indexer")]
Task<ISpace> GetSpace(uint id);

ISpace this[string name] { get; }
ISpace this[uint id] { get; }

Task Reload();

DateTimeOffset LastReloadTime { get; }
Expand Down
10 changes: 1 addition & 9 deletions src/progaudi.tarantool/ISpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ProGaudi.Tarantool.Client
{
public interface ISpace
public interface ISpace : IReadOnlyDictionary<string, IIndex>, IReadOnlyDictionary<uint, IIndex>
{
uint Id { get; }

Expand All @@ -30,10 +30,6 @@ public interface ISpace
[Obsolete("Use indexer")]
Task<IIndex> GetIndex(uint indexId);

IIndex this[string name] { get; }

IIndex this[uint id] { get; }

Task<DataResponse<TTuple[]>> Insert<TTuple>(TTuple tuple);

Task<DataResponse<TTuple[]>> Select<TKey, TTuple>(TKey selectKey);
Expand All @@ -50,10 +46,6 @@ public interface ISpace

Task<DataResponse<TTuple[]>> Delete<TKey, TTuple>(TKey key);

Task<uint> Count<TKey>(TKey key);

Task<uint> Length();

Task<DataResponse<TTuple[]>> Increment<TTuple, TKey>(TKey key);

Task<DataResponse<TTuple[]>> Decrement<TTuple, TKey>(TKey key);
Expand Down
35 changes: 33 additions & 2 deletions src/progaudi.tarantool/Schema.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -31,9 +32,13 @@ public Schema(ILogicalConnection logicalConnection)

public Task<ISpace> 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; }

Expand Down Expand Up @@ -65,5 +70,31 @@ private async Task<T[]> Select<T>(uint spaceId, Iterator iterator = Iterator.All
.ConfigureAwait(false);
return response.Data;
}

IEnumerator<KeyValuePair<uint, ISpace>> IEnumerable<KeyValuePair<uint, ISpace>>.GetEnumerator() => _indexById.GetEnumerator();

IEnumerator<KeyValuePair<string, ISpace>> IEnumerable<KeyValuePair<string, ISpace>>.GetEnumerator() => _indexByName.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => throw new InvalidOperationException();

int IReadOnlyCollection<KeyValuePair<string, ISpace>>.Count => _indexById.Count;

bool IReadOnlyDictionary<string, ISpace>.ContainsKey(string key) => _indexByName.ContainsKey(key);

bool IReadOnlyDictionary<string, ISpace>.TryGetValue(string key, out ISpace value) => _indexByName.TryGetValue(key, out value);

bool IReadOnlyDictionary<uint, ISpace>.ContainsKey(uint key) => _indexById.ContainsKey(key);

bool IReadOnlyDictionary<uint, ISpace>.TryGetValue(uint key, out ISpace value) => _indexById.TryGetValue(key, out value);

IEnumerable<uint> IReadOnlyDictionary<uint, ISpace>.Keys => _indexById.Keys;

IEnumerable<ISpace> IReadOnlyDictionary<uint, ISpace>.Values => _indexById.Values;

IEnumerable<string> IReadOnlyDictionary<string, ISpace>.Keys => _indexByName.Keys;

IEnumerable<ISpace> IReadOnlyDictionary<string, ISpace>.Values => _indexByName.Values;

int IReadOnlyCollection<KeyValuePair<uint, ISpace>>.Count => _indexById.Count;
}
}
49 changes: 35 additions & 14 deletions src/progaudi.tarantool/Space.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -60,13 +61,17 @@ internal void SetIndices(IReadOnlyCollection<Index> value)

public IReadOnlyCollection<SpaceField> Fields { get; }

public Task<IIndex> GetIndex(string name) => Task.FromResult(_indexByName.TryGetValue(name, out var index) ? index : throw ExceptionHelper.InvalidIndexName(name, Name));
public Task<IIndex> GetIndex(string name) => Task.FromResult(this[name]);

public Task<IIndex> GetIndex(uint id) => Task.FromResult(_indexById.TryGetValue(id, out var index) ? index : throw ExceptionHelper.InvalidIndexId(id, Name));
public Task<IIndex> 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<DataResponse<TTuple[]>> Insert<TTuple>(TTuple tuple)
{
Expand Down Expand Up @@ -117,16 +122,6 @@ public async Task<DataResponse<TTuple[]>> Delete<TKey, TTuple>(TKey key)
return await LogicalConnection.SendRequest<DeleteRequest<TKey>, TTuple>(deleteRequest).ConfigureAwait(false);
}

public Task<uint> Count<TKey>(TKey key)
{
throw new NotImplementedException();
}

public Task<uint> Length()
{
throw new NotImplementedException();
}

public Task<DataResponse<TTuple[]>> Increment<TTuple, TKey>(TKey key)
{
// Currently we can't impelment that method because Upsert returns void.
Expand All @@ -143,5 +138,31 @@ public override string ToString()
{
return $"{Name}, id={Id}";
}

IEnumerator<KeyValuePair<uint, IIndex>> IEnumerable<KeyValuePair<uint, IIndex>>.GetEnumerator() => _indexById.GetEnumerator();

IEnumerator<KeyValuePair<string, IIndex>> IEnumerable<KeyValuePair<string, IIndex>>.GetEnumerator() => _indexByName.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => throw new InvalidOperationException();

int IReadOnlyCollection<KeyValuePair<string, IIndex>>.Count => _indexById.Count;

bool IReadOnlyDictionary<string, IIndex>.ContainsKey(string key) => _indexByName.ContainsKey(key);

bool IReadOnlyDictionary<string, IIndex>.TryGetValue(string key, out IIndex value) => _indexByName.TryGetValue(key, out value);

bool IReadOnlyDictionary<uint, IIndex>.ContainsKey(uint key) => _indexById.ContainsKey(key);

bool IReadOnlyDictionary<uint, IIndex>.TryGetValue(uint key, out IIndex value) => _indexById.TryGetValue(key, out value);

IEnumerable<uint> IReadOnlyDictionary<uint, IIndex>.Keys => _indexById.Keys;

IEnumerable<IIndex> IReadOnlyDictionary<uint, IIndex>.Values => _indexById.Values;

IEnumerable<string> IReadOnlyDictionary<string, IIndex>.Keys => _indexByName.Keys;

IEnumerable<IIndex> IReadOnlyDictionary<string, IIndex>.Values => _indexByName.Values;

int IReadOnlyCollection<KeyValuePair<uint, IIndex>>.Count => _indexById.Count;
}
}
13 changes: 13 additions & 0 deletions tests/progaudi.tarantool.tests/Schema/GetSpace_Should.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Xunit;
Expand Down Expand Up @@ -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<string, ISpace>)tarantoolClient.Schema)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be pain in the ass, because you always forced to cast schema to exact interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about exposing IndexByName and IndexById as IReadonlyDictionaries instead of decoration?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

box.Schema.IndexByName ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, typo, should be:

  • Properties IndexByName and IndexById in Space
  • Properties SpaceByName and SpaceById in Schema

{
pair.Value.Name.ShouldBe(pair.Key);
}
}
}
}
}