Skip to content

Commit

Permalink
Merge branch 'ferature/blob-support' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstonis committed Oct 1, 2021
2 parents db42be3 + 2a5f046 commit ae12cbd
Showing 1 changed file with 58 additions and 25 deletions.
83 changes: 58 additions & 25 deletions Tycho/TychoDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,22 @@ private const string

private readonly ProcessingQueue _processingQueue = new ProcessingQueue();

private readonly StringBuilder _commandBuilder = new StringBuilder();

private SqliteConnection _connection;

private bool _isDisposed;


private StringBuilder ReusableStringBuilder
{
get
{
_commandBuilder.Clear();
return _commandBuilder;
}
}

public TychoDb (string dbPath, IJsonSerializer jsonSerializer, string dbName = "tycho_cache.db", string password = null, bool rebuildCache = false)
{
SQLitePCL.Batteries_V2.Init ();
Expand Down Expand Up @@ -159,18 +171,22 @@ public ValueTask<bool> WriteObjectsAsync<T>(IEnumerable<T> objs, Func<T, object>
try
{

using var insertCommand = conn.CreateCommand ();
insertCommand.CommandText = Queries.InsertOrReplace;

foreach (var obj in objs)
{
var rowId = 0L;

using var insertCommand = conn.CreateCommand ();
insertCommand.CommandText = Queries.InsertOrReplace;
insertCommand.Parameters.Clear();

insertCommand.Parameters.Add (ParameterKey, SqliteType.Text).Value = keySelector (obj);
insertCommand.Parameters.Add (ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull();
insertCommand.Parameters.Add (ParameterFullTypeName, SqliteType.Text).Value = typeof (T).FullName;
insertCommand.Parameters.Add (ParameterJson, SqliteType.Text).Value = _jsonSerializer.Serialize (obj);

await insertCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

rowId = (long)await insertCommand.ExecuteScalarAsync (cancellationToken).ConfigureAwait(false);

writeCount += rowId > 0 ? 1 : 0;
Expand Down Expand Up @@ -201,7 +217,7 @@ public ValueTask<T> ReadObjectAsync<T> (object key, string partition = null, Can
{
using var selectCommand = conn.CreateCommand ();

var commandBuilder = new StringBuilder ();
var commandBuilder = ReusableStringBuilder;

commandBuilder.Append (Queries.SelectDataFromJsonValueWithKeyAndFullTypeName);

Expand All @@ -220,6 +236,8 @@ public ValueTask<T> ReadObjectAsync<T> (object key, string partition = null, Can

selectCommand.CommandText = commandBuilder.ToString ();

await selectCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

using var reader = await selectCommand.ExecuteReaderAsync (cancellationToken).ConfigureAwait (false);

T returnValue = default(T);
Expand Down Expand Up @@ -266,7 +284,7 @@ public ValueTask<IEnumerable<T>> ReadObjectsAsync<T> (string partition = null, F
{
using var selectCommand = conn.CreateCommand ();

var commandBuilder = new StringBuilder ();
var commandBuilder = ReusableStringBuilder;

commandBuilder.Append(Queries.SelectDataFromJsonValueWithFullTypeName);

Expand All @@ -289,6 +307,8 @@ public ValueTask<IEnumerable<T>> ReadObjectsAsync<T> (string partition = null, F

selectCommand.CommandText = commandBuilder.ToString ();

await selectCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

using var reader = await selectCommand.ExecuteReaderAsync (cancellationToken).ConfigureAwait (false);

var objects = new List<T> ();
Expand Down Expand Up @@ -326,7 +346,7 @@ public ValueTask<IEnumerable<TOut>> ReadObjectsAsync<TIn, TOut> (Expression<Func
{
using var selectCommand = conn.CreateCommand ();

var commandBuilder = new StringBuilder ();
var commandBuilder = ReusableStringBuilder;

var selectionPath = QueryPropertyPath.BuildPath (innerObjectSelection);

Expand All @@ -351,6 +371,8 @@ public ValueTask<IEnumerable<TOut>> ReadObjectsAsync<TIn, TOut> (Expression<Func

selectCommand.CommandText = commandBuilder.ToString ();

await selectCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

using var reader = await selectCommand.ExecuteReaderAsync (cancellationToken).ConfigureAwait (false);

while (await reader.ReadAsync (cancellationToken).ConfigureAwait (false))
Expand Down Expand Up @@ -382,28 +404,30 @@ public ValueTask<bool> DeleteObjectAsync<T> (object key, string partition = null

try
{
using var selectCommand = conn.CreateCommand ();
using var deleteCommand = conn.CreateCommand ();

var commandBuilder = new StringBuilder ();
var commandBuilder = ReusableStringBuilder;

commandBuilder.Append (Queries.DeleteDataFromJsonValueWithKeyAndFullTypeName);

selectCommand.Parameters.Add (ParameterKey, SqliteType.Text).Value = key;
selectCommand.Parameters.Add (ParameterFullTypeName, SqliteType.Text).Value = typeof (T).FullName;
deleteCommand.Parameters.Add (ParameterKey, SqliteType.Text).Value = key;
deleteCommand.Parameters.Add (ParameterFullTypeName, SqliteType.Text).Value = typeof (T).FullName;

if (!string.IsNullOrEmpty (partition))
{
commandBuilder.Append (Queries.AndPartitionHasValue);
selectCommand.Parameters.Add (ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull ();
deleteCommand.Parameters.Add (ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull ();
}
else
{
commandBuilder.Append (Queries.AndPartitionIsNull);
}

selectCommand.CommandText = commandBuilder.ToString ();
deleteCommand.CommandText = commandBuilder.ToString ();

var deletionCount = await selectCommand.ExecuteNonQueryAsync (cancellationToken).ConfigureAwait (false);
await deleteCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

var deletionCount = await deleteCommand.ExecuteNonQueryAsync (cancellationToken).ConfigureAwait (false);

await transaction.CommitAsync ().ConfigureAwait (false);

Expand All @@ -428,18 +452,18 @@ public ValueTask<bool> DeleteObjectAsync<T> (object key, string partition = null

try
{
using var selectCommand = conn.CreateCommand ();
using var deleteCommand = conn.CreateCommand ();

var commandBuilder = new StringBuilder ();
var commandBuilder = ReusableStringBuilder;

commandBuilder.Append (Queries.DeleteDataFromJsonValueWithFullTypeName);

selectCommand.Parameters.Add (ParameterFullTypeName, SqliteType.Text).Value = typeof (T).FullName;
deleteCommand.Parameters.Add (ParameterFullTypeName, SqliteType.Text).Value = typeof (T).FullName;

if (!string.IsNullOrEmpty (partition))
{
commandBuilder.Append (Queries.AndPartitionHasValue);
selectCommand.Parameters.Add (ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull ();
deleteCommand.Parameters.Add (ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull ();
}
else
{
Expand All @@ -451,9 +475,11 @@ public ValueTask<bool> DeleteObjectAsync<T> (object key, string partition = null
filter.Build (commandBuilder);
}

selectCommand.CommandText = commandBuilder.ToString ();
deleteCommand.CommandText = commandBuilder.ToString ();

await deleteCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

var deletionCount = await selectCommand.ExecuteNonQueryAsync (cancellationToken).ConfigureAwait (false);
var deletionCount = await deleteCommand.ExecuteNonQueryAsync (cancellationToken).ConfigureAwait (false);

await transaction.CommitAsync ().ConfigureAwait (false);

Expand Down Expand Up @@ -489,6 +515,8 @@ public ValueTask<bool> WriteBlobAsync(Stream stream, string key, string partitio
insertCommand.Parameters.Add(ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull();
insertCommand.Parameters.AddWithValue(ParameterBlobLength, stream.Length);

await insertCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

rowId = (long)await insertCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);

writeCount += rowId > 0 ? 1 : 0;
Expand Down Expand Up @@ -524,7 +552,7 @@ public ValueTask<Stream> ReadBlobAsync(object key, string partition = null, Canc
{
using var selectCommand = conn.CreateCommand();

var commandBuilder = new StringBuilder();
var commandBuilder = ReusableStringBuilder;

commandBuilder.Append(Queries.SelectDataFromStreamValueWithKey);

Expand All @@ -541,6 +569,9 @@ public ValueTask<Stream> ReadBlobAsync(object key, string partition = null, Canc
}

selectCommand.CommandText = commandBuilder.ToString();

await selectCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

using var reader = await selectCommand.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);

Stream returnValue = Stream.Null;
Expand Down Expand Up @@ -569,27 +600,29 @@ public ValueTask<bool> DeleteBlobAsync(object key, string partition = null, Canc

try
{
using var selectCommand = conn.CreateCommand();
using var deleteCommand = conn.CreateCommand();

var commandBuilder = new StringBuilder();
var commandBuilder = ReusableStringBuilder;

commandBuilder.Append(Queries.DeleteDataFromStreamValueWithKey);

selectCommand.Parameters.Add(ParameterKey, SqliteType.Text).Value = key;
deleteCommand.Parameters.Add(ParameterKey, SqliteType.Text).Value = key;

if (!string.IsNullOrEmpty(partition))
{
commandBuilder.Append(Queries.AndPartitionHasValue);
selectCommand.Parameters.Add(ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull();
deleteCommand.Parameters.Add(ParameterPartition, SqliteType.Text).Value = partition.AsValueOrDbNull();
}
else
{
commandBuilder.Append(Queries.AndPartitionIsNull);
}

selectCommand.CommandText = commandBuilder.ToString();
deleteCommand.CommandText = commandBuilder.ToString();

await deleteCommand.PrepareAsync(cancellationToken).ConfigureAwait(false);

var deletionCount = await selectCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
var deletionCount = await deleteCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);

await transaction.CommitAsync().ConfigureAwait(false);

Expand Down

0 comments on commit ae12cbd

Please sign in to comment.