Skip to content
Merged
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ before_script:
- docker -v
- docker-compose down
- docker-compose up -d
- pushd samples/docker-compose
- docker-compose down
- docker-compose up -d --build
- popd

script:
- dotnet build -f netcoreapp1.0 src/tarantool.client
- dotnet test tests/tarantool.client.tests
- dotnet test tests/tarantool.client.tests
- curl --fail http://localhost:5000
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ configuration:
matrix:
fast_finish: false

skip_branch_with_pr: true

init:
- git config --global core.autocrlf true
- ps: $Env:LABEL = "CI" + $Env:APPVEYOR_BUILD_NUMBER.PadLeft(5, "0")
Expand All @@ -22,8 +24,10 @@ before_build:

build_script:
- dotnet build "src\tarantool.client" -c %CONFIGURATION% --no-dependencies --version-suffix %LABEL%
- dotnet build "tests\tarantool.client.tests" -c %CONFIGURATION% --no-dependencies --version-suffix %LABEL%

after_build:
- rm -rf tests\tarantool.client.tests\bin tests\tarantool.client.tests\obj
- dotnet pack "src\tarantool.client" -c %CONFIGURATION% --no-build --version-suffix %LABEL% -o artifacts

artifacts:
Expand Down
1 change: 0 additions & 1 deletion samples/docker-compose/.dockerignore

This file was deleted.

16 changes: 8 additions & 8 deletions samples/docker-compose/dotnet/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Threading.Tasks;
using dotnet.Models;
using Microsoft.AspNetCore.Mvc;
using ProGaudi.Tarantool.Client;
using ProGaudi.Tarantool.Client.Model;
using ProGaudi.Tarantool.Client.Model.Enums;
using Tarantool.Client;
using Tarantool.Client.Model;
using Tarantool.Client.Model.Enums;

namespace dotnet.Controllers
{
Expand All @@ -25,22 +25,22 @@ public HomeController(Box box)
this._secondaryIndex = result.Item3;
}

private async Task<TarantoolTuple<Space, Index, Index>> Initialize()
private async Task<Tarantool.Client.Model.Tuple<Space, Index, Index>> Initialize()
{
var schema = this._box.GetSchema();

var space = await schema.GetSpace("some_space");
var primaryIndex = await space.GetIndex("primary");
var index = await space.GetIndex("some_secondary_index");

return TarantoolTuple.Create(space, primaryIndex, index);
return Tarantool.Client.Model.Tuple.Create(space, primaryIndex, index);
}

public async Task<ViewResult> Index()
{
var allDogs = await this._primaryIndex.Select<TarantoolTuple<long>, TarantoolTuple<long, string, long>>(TarantoolTuple.Create(-1L), new SelectOptions { Iterator = Iterator.All });
var seniorDogs = await this._secondaryIndex.Select<TarantoolTuple<long>, TarantoolTuple<long, string, long>>(TarantoolTuple.Create(5L), new SelectOptions { Iterator = Iterator.Ge });
var juniorDogs = await this._secondaryIndex.Select<TarantoolTuple<long>, TarantoolTuple<long, string, long>>(TarantoolTuple.Create(5L), new SelectOptions { Iterator = Iterator.Le });
var allDogs = await this._primaryIndex.Select<Tuple<long>, Tuple<long, string, long>>(Tuple.Create(-1L), new SelectOptions { Iterator = Iterator.All });
var seniorDogs = await this._secondaryIndex.Select<Tuple<long>, Tuple<long, string, long>>(Tuple.Create(5L), new SelectOptions { Iterator = Iterator.Ge });
var juniorDogs = await this._secondaryIndex.Select<Tuple<long>, Tuple<long, string, long>>(Tuple.Create(5L), new SelectOptions { Iterator = Iterator.Le });

return View(new []
{
Expand Down
4 changes: 2 additions & 2 deletions samples/docker-compose/dotnet/Models/Dog.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using ProGaudi.Tarantool.Client.Model;
using Tarantool.Client.Model;

namespace dotnet.Models
{
public class Dog
{
public Dog(TarantoolTuple<long, string, long> tuple)
public Dog(Tuple<long, string, long> tuple)
{
Id = tuple.Item1;
Name = tuple.Item2;
Expand Down
4 changes: 2 additions & 2 deletions samples/docker-compose/dotnet/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using ProGaudi.Tarantool.Client;
using ProGaudi.Tarantool.Client.Model;
using Tarantool.Client;
using Tarantool.Client.Model;

namespace dotnet
{
Expand Down
64 changes: 35 additions & 29 deletions src/tarantool.client/Box.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;

using ProGaudi.Tarantool.Client.Model;
Expand All @@ -10,17 +11,17 @@ namespace ProGaudi.Tarantool.Client
{
public class Box : IDisposable
{
private readonly ConnectionOptions _connectionOptions;
private readonly ClientOptions _clientOptions;

private readonly ILogicalConnection _logicalConnection;

private readonly IResponseReader _responseReader;

private readonly INetworkStreamPhysicalConnection _physicalConnection;

public Box(ConnectionOptions options)
public Box(ClientOptions options)
{
_connectionOptions = options;
_clientOptions = options;
TarantoolConvertersRegistrator.Register(options.MsgPackContext);

_physicalConnection = new NetworkStreamPhysicalConnection();
Expand All @@ -30,7 +31,7 @@ public Box(ConnectionOptions options)

public async Task Connect()
{
_physicalConnection.Connect(_connectionOptions);
await _physicalConnection.Connect(_clientOptions);

var greetingsResponseBytes = new byte[128];
var readCount = await _physicalConnection.ReadAsync(greetingsResponseBytes, 0, greetingsResponseBytes.Length);
Expand All @@ -41,26 +42,43 @@ public async Task Connect()

var greetings = new GreetingsResponse(greetingsResponseBytes);

_connectionOptions.LogWriter?.WriteLine($"Greetings received, salt is {Convert.ToBase64String(greetings.Salt)} .");
_clientOptions.LogWriter?.WriteLine($"Greetings received, salt is {Convert.ToBase64String(greetings.Salt)} .");

_responseReader.BeginReading();

_connectionOptions.LogWriter?.WriteLine("Server responses reading started.");
_clientOptions.LogWriter?.WriteLine("Server responses reading started.");

await LoginIfNotGuest(greetings);
}

public static async Task<Box> Connect(string replicationSource)
{
var box = new Box(new ClientOptions(replicationSource));
await box.Connect();
return box;
}

public static Task<Box> Connect(string host, int port)
{
return Connect($"{host}:{port}");
}

public static Task<Box> Connect(string host, int port, string user, string password)
{
return Connect($"{user}:{password}@{host}:{port}");
}

public void Dispose()
{
_connectionOptions.LogWriter?.WriteLine("Box is disposing...");
_connectionOptions.LogWriter?.Flush();
_clientOptions.LogWriter?.WriteLine("Box is disposing...");
_clientOptions.LogWriter?.Flush();
_responseReader.Dispose();
_physicalConnection.Dispose();
}

public Schema GetSchema()
{
_connectionOptions.LogWriter?.WriteLine("Schema acquiring...");
_clientOptions.LogWriter?.WriteLine("Schema acquiring...");
return new Schema(_logicalConnection);
}

Expand All @@ -81,30 +99,18 @@ public async Task<DataResponse<TResponse[]>> Eval<TTuple, TResponse>(string expr

private async Task LoginIfNotGuest(GreetingsResponse greetings)
{
if (string.IsNullOrEmpty(_connectionOptions.UserName))
var singleNode = _clientOptions.ConnectionOptions.Nodes.Single();

if (string.IsNullOrEmpty(singleNode.Uri.UserName))
{
if (!_connectionOptions.GuestMode)
{
throw ExceptionHelper.EmptyUsernameInGuestMode();
}
_clientOptions.LogWriter?.WriteLine("Guest mode, no authentication attempt.");
return;
}
else
{
if (_connectionOptions.GuestMode)
{
_connectionOptions.LogWriter?.WriteLine("Guest mode, no authentication attempt.");

return;
}

var authenticateRequest = AuthenticationRequest.Create(
greetings,
_connectionOptions.UserName,
_connectionOptions.Password);
var authenticateRequest = AuthenticationRequest.Create(greetings, singleNode.Uri);

await _logicalConnection.SendRequestWithEmptyResponse(authenticateRequest);
_connectionOptions.LogWriter?.WriteLine($"Authentication request send: {authenticateRequest}");
}
await _logicalConnection.SendRequestWithEmptyResponse(authenticateRequest);
_clientOptions.LogWriter?.WriteLine($"Authentication request send: {authenticateRequest}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ProGaudi.MsgPack.Light;

using ProGaudi.Tarantool.Client.Model;
using ProGaudi.Tarantool.Client.Utils;

namespace ProGaudi.Tarantool.Client.Converters
{
Expand Down
1 change: 0 additions & 1 deletion src/tarantool.client/Converters/PacketSizeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;

using ProGaudi.MsgPack.Light;
Expand Down
1 change: 0 additions & 1 deletion src/tarantool.client/Converters/SpaceFieldConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using ProGaudi.Tarantool.Client.Model;
using ProGaudi.Tarantool.Client.Model.Enums;
using ProGaudi.Tarantool.Client.Utils;

namespace ProGaudi.Tarantool.Client.Converters
{
Expand Down
2 changes: 1 addition & 1 deletion src/tarantool.client/INetworkStreamPhysicalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ProGaudi.Tarantool.Client
{
public interface INetworkStreamPhysicalConnection : IDisposable
{
void Connect(ConnectionOptions options);
Task Connect(ClientOptions options);
Task Flush();
Task<int> ReadAsync(byte[] buffer, int offset, int count);
void Write(byte[] buffer, int offset, int count);
Expand Down
23 changes: 11 additions & 12 deletions src/tarantool.client/LogicalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ internal class LogicalConnection : ILogicalConnection

private readonly INetworkStreamPhysicalConnection _physicalConnection;

private long _currentRequestId = 0;
private long _currentRequestId;

private readonly ConcurrentDictionary<RequestId, TaskCompletionSource<MemoryStream>> _pendingRequests =
new ConcurrentDictionary<RequestId, TaskCompletionSource<MemoryStream>>();

private readonly ILog _logWriter;

public LogicalConnection(ConnectionOptions options, INetworkStreamPhysicalConnection physicalConnection)
public LogicalConnection(ClientOptions options, INetworkStreamPhysicalConnection physicalConnection)
{
_msgPackContext = options.MsgPackContext;
_logWriter = options.LogWriter;
Expand All @@ -48,7 +48,8 @@ public async Task<DataResponse<TResponse[]>> SendRequest<TRequest, TResponse>(TR
return await SendRequestImpl<TRequest, DataResponse<TResponse[]>>(request);
}

public TaskCompletionSource<MemoryStream> PopResponseCompletionSource(RequestId requestId, MemoryStream resultStream)
public TaskCompletionSource<MemoryStream> PopResponseCompletionSource(RequestId requestId,
MemoryStream resultStream)
{
TaskCompletionSource<MemoryStream> request;

Expand All @@ -63,7 +64,7 @@ public TaskCompletionSource<MemoryStream> PopResponseCompletionSource(RequestId
public static byte[] ReadFully(Stream input)
{
input.Position = 0;
byte[] buffer = new byte[16 * 1024];
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
Expand All @@ -83,7 +84,7 @@ public IEnumerable<TaskCompletionSource<MemoryStream>> PopAllResponseCompletionS
}

private async Task<TResponse> SendRequestImpl<TRequest, TResponse>(TRequest request)
where TRequest : IRequest
where TRequest : IRequest
{
var bodyBuffer = MsgPackSerializer.Serialize(request, _msgPackContext);

Expand All @@ -96,7 +97,7 @@ private async Task<TResponse> SendRequestImpl<TRequest, TResponse>(TRequest requ
lock (_physicalConnection)
{
_logWriter?.WriteLine($"Begin sending request header buffer, requestId: {requestId}, code: {request.Code}, length: {headerBuffer.Length}");
_physicalConnection.Write(headerBuffer, 0, Constants.PacketSizeBufferSize + (int)headerLength);
_physicalConnection.Write(headerBuffer, 0, Constants.PacketSizeBufferSize + (int) headerLength);

_logWriter?.WriteLine($"Begin sending request body buffer, length: {bodyBuffer.Length}");
_physicalConnection.Write(bodyBuffer, 0, bodyBuffer.Length);
Expand All @@ -107,13 +108,11 @@ private async Task<TResponse> SendRequestImpl<TRequest, TResponse>(TRequest requ
var responseStream = await responseTask;
_logWriter?.WriteLine($"Response with requestId {requestId} is recieved, length: {responseStream.Length}.");

var deserializedResponse = MsgPackSerializer.Deserialize<TResponse>(responseStream, _msgPackContext);
return deserializedResponse;
return MsgPackSerializer.Deserialize<TResponse>(responseStream, _msgPackContext);
}
catch (ArgumentException)
{
_logWriter?.WriteLine(
$"Response with requestId {requestId} failed, header:\n{ToReadableString(headerBuffer)} \n body: \n{ToReadableString(bodyBuffer)}");
_logWriter?.WriteLine($"Response with requestId {requestId} failed, header:\n{ToReadableString(headerBuffer)} \n body: \n{ToReadableString(bodyBuffer)}");
throw;
}
}
Expand All @@ -137,7 +136,7 @@ private byte[] CreateAndSerializeBuffer<TRequest>(
MsgPackSerializer.Serialize(requestHeader, stream, _msgPackContext);

headerLength = stream.Position - Constants.PacketSizeBufferSize;
var packetLength = new PacketSize((uint)(headerLength + serializedRequest.Length));
var packetLength = new PacketSize((uint) (headerLength + serializedRequest.Length));
stream.Seek(0, SeekOrigin.Begin);
MsgPackSerializer.Serialize(packetLength, stream, _msgPackContext);
return packetSizeBuffer;
Expand All @@ -146,7 +145,7 @@ private byte[] CreateAndSerializeBuffer<TRequest>(
private RequestId GetRequestId()
{
var requestId = Interlocked.Increment(ref _currentRequestId);
return (RequestId)(ulong)requestId;
return (RequestId) (ulong) requestId;
}

private Task<MemoryStream> GetResponseTask(RequestId requestId)
Expand Down
30 changes: 30 additions & 0 deletions src/tarantool.client/Model/ClientOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using ProGaudi.MsgPack.Light;

namespace ProGaudi.Tarantool.Client.Model
{
public class ClientOptions
{
public ClientOptions(ILog log = null, MsgPackContext context = null)
: this(new ConnectionOptions(), log, context)
{
}

public ClientOptions(string replicationSource, ILog log = null, MsgPackContext context = null)
: this(new ConnectionOptions(replicationSource, log), log, context)
{
}

private ClientOptions(ConnectionOptions options, ILog log, MsgPackContext context)
{
ConnectionOptions = options;
LogWriter = log;
MsgPackContext = context ?? new MsgPackContext();
}

public ILog LogWriter { get; }

public MsgPackContext MsgPackContext { get; }

public ConnectionOptions ConnectionOptions { get; }
}
}
Loading