Example #1
0
        public static ShardsValues ShardListForeign(short shardId, IList <ShardKey <TRecord, TChild, TGrandChild, TGreatGrandChild> > records)
        {
            var result = new ShardsValues();

            foreach (var record in records)
            {
                if (!record.ShardId.Equals(shardId) && !result.Shards.ContainsKey(record.ShardId))
                {
                    result.Add(record.ShardId);
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Given a list of Models with ShardKChild keys, returns a distinct list of shard Ids, except for the shard Id specified.
        /// Useful for querying foreign shards after the primary shard has returned results.
        /// </summary>
        /// <param name="shardId">The shard id of the shard to exclude. This is typically the current shard and this function is used to determine if any records are foreign to it.</param>
        /// <param name="records">The list of models to evaluate.</param>
        /// <returns>A ShardsValues collection, with the shards listed. The values dictionary will be null.</returns>
        public static ShardsValues ToShardsValues <TModel>(IList <IKeyedModel <TRecord, TChild, TGrandChild, TGreatGrandChild> > records) where TModel : IKeyedModel <TRecord, TChild, TGrandChild, TGreatGrandChild>
        {
            var result = new ShardsValues();

            foreach (var record in records)
            {
                if (!result.Shards.ContainsKey(record.Key.ShardId))
                {
                    result.Add(record.Key.ShardId);
                }
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Given a list of Models with ShardKey keys, returns a distinct list of shard Ids, except for the shard Id specified.
        /// Useful for querying foreign shards after the primary shard has returned results.
        /// </summary>
        /// <param name="shardId"></param>
        /// <param name="records">The list of models to evaluate.</param>
        /// <returns>A ShardsValues collection, with the shards listed and values not set.</returns>
        public static ShardsValues ShardListForeign <TModel>(short shardId, IList <TModel> records) where TModel : IKeyedModel <TRecord, TChild, TGrandChild, TGreatGrandChild>
        {
            var result = new ShardsValues();

            foreach (var record in records)
            {
                if (!record.Key.ShardId.Equals(shardId) && !result.Shards.ContainsKey(record.Key.ShardId))
                {
                    result.Add(record.Key.ShardId);
                }
            }
            return(result);
        }
Example #4
0
            internal async Task BatchAllAsync(ShardSetBatch batch, ShardsValues shardParameterValues, CancellationToken cancellationToken)
            {
                if (batch is null)
                {
                    throw new ArgumentNullException(nameof(batch));
                }
                if (!(this.MockResults is null))
                {
                    return;
                }

                cancellationToken.ThrowIfCancellationRequested();
                var tsks = new List <Task>();
                var cancelTokenSource = new CancellationTokenSource();

                using (var queryCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelTokenSource.Token))
                {
                    if (shardParameterValues is null)
                    {
                        foreach (var shardId in dtn.Keys)
                        {
                            tsks.Add(this.dtn[shardId].Write._manager.RunBatchAsync <object>(batch, null, cancellationToken));
                        }
                    }
                    else
                    {
                        if (shardParameterValues.Shards.Count == 0)
                        {
                            return;
                        }
                        foreach (var shardTuple in shardParameterValues.Shards)
                        {
                            tsks.Add(this.dtn[shardTuple.Key].Write._manager.RunBatchAsync <object>(batch, null, cancellationToken));
                        }
                    }
                    await Task.WhenAll(tsks).ConfigureAwait(false);
                }
            }
Example #5
0
 /// <summary>
 /// Given a list of Models with ShardChld keys, returns a distinct list of shard Ids, except for the shard Id of the current shard.
 /// Useful for querying foreign shards after the primary shard has returned results.
 /// </summary>
 /// <param name="records">The list of ShardKeys to evaluate.</param>
 /// <returns>A ShardsValues collection, with the shards listed. The values dictionary will be null.</returns>
 public ShardsValues ForeignShards <TModel>(List <TModel> records) where TModel : IKeyedModel <TRecord, TChild, TGrandChild, TGreatGrandChild>
 => ShardsValues.ShardListForeign <TRecord, TChild, TGrandChild, TGreatGrandChild, TModel>(_key.ShardId, (IList <TModel>)records);
Example #6
0
 /// <summary>
 /// Given a list of ShardKey values, returns a distinct list of shard Ids, except for the shard Id of the current shard.
 /// Useful for querying foreign shards after the primary shard has returned results.
 /// </summary>
 /// <param name="records">The list of ShardKeys to evaluate.</param>
 /// <returns>A ShardsValues collection, with the shards listed. The values dictionary will be null.</returns>
 public ShardsValues ForeignShards(List <ShardKey <TRecord, TChild, TGrandChild, TGreatGrandChild> > records)
 => ShardsValues.ShardListForeign <TRecord, TChild, TGrandChild, TGreatGrandChild>(_key.ShardId, (IList <ShardKey <TRecord, TChild, TGrandChild, TGreatGrandChild> >)records);
Example #7
0
 /// <summary>
 /// Given a list of Models with ShardKey keys, returns a distinct list of shard Ids, except for the shard Id of the current shard.
 /// Useful for querying foreign shards after the primary shard has returned results.
 /// </summary>
 /// <param name="records">The list of ShardKeys to evaluate.</param>
 /// <returns>A ShardsValues collection, with the shards listed. The values dictionary will be null.</returns>
 public ShardsValues ForeignShards <TModel>(List <TModel> records) where TModel : IKeyedModel <TRecord>
 => ShardsValues.ShardListForeign <TRecord, TModel>(_shardId, (IList <TModel>)records);
Example #8
0
 /// <summary>
 /// Given a list of ShardKey values, returns a distinct list of shard Ids, except for the shard Id of the current shard.
 /// Useful for querying foreign shards after the primary shard has returned results.
 /// </summary>
 /// <param name="records">The list of ShardKeys to evaluate.</param>
 /// <returns>A ShardsValues collection, with the shards listed. The values dictionary will be null.</returns>
 public ShardsValues ForeignShards(List <ShardKey <TRecord> > records)
 => ShardsValues.ShardListForeign <TRecord>(_shardId, (IList <ShardKey <TRecord> >)records);
Example #9
0
 /// <summary>
 /// Given a list of ShardKey values, returns a distinct list of shard Ids, except for the shard Id of the current shard.
 /// Useful for querying foreign shards after the primary shard has returned results.
 /// </summary>
 /// <param name="records">The list of ShardKeys to evaluate.</param>
 /// <returns>A ShardsValues collection, with the shards listed. The values dictionary will be null.</returns>
 public ShardsValues ForeignShards(IList <ShardKey <TRecord, TChild> > records)
 => ShardsValues.ShardListForeign <TRecord, TChild>(_key.ShardId, records);
Example #10
0
            internal async Task <List <TModel> > ReadQueryAllAsync <TArg, TModel>(Query query, DbParameterCollection parameters, ShardsValues shardParameterValues, string shardParameterName, QueryResultModelHandler <TArg, TModel> resultHandler, TArg dataObject, CancellationToken cancellationToken)
            {
                var result = new List <TModel>();

                if (query is null || string.IsNullOrEmpty(query.Sql) || string.IsNullOrEmpty(query.Name))
                {
                    throw new ArgumentNullException(nameof(query));
                }
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }
                if (!(this.MockResults is null) && this.MockResults.Count > 0 && this.MockResults.ContainsKey(query.Name))
                {
                    return((List <TModel>) this.MockResults[query.Name]);
                }
                var shardParameterOrdinal = parameters.GetParameterOrdinal(shardParameterName);

                cancellationToken.ThrowIfCancellationRequested();

                var tsks = new List <Task <TModel> >();
                var cancelTokenSource = new CancellationTokenSource();

                using (var queryCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelTokenSource.Token))
                {
                    if (shardParameterValues is null)
                    {
                        foreach (var shardId in dtn.Keys)
                        {
                            parameters.SetShardId(shardParameterOrdinal, shardId);
                            tsks.Add(this.dtn[shardId].Read._manager.QueryAsync <TArg, TModel>(query, parameters, parameters.GetParameterOrdinal(shardParameterName), null, resultHandler, false, dataObject, null, cancellationToken));
                        }
                    }
                    else
                    {
                        if (shardParameterValues.Shards.Count == 0)
                        {
                            return(result);
                        }
                        foreach (var shardTuple in shardParameterValues.Shards)
                        {
                            parameters.SetShardId(shardParameterOrdinal, shardTuple.Key);
                            tsks.Add(this.dtn[shardTuple.Key].Read._manager.QueryAsync <TArg, TModel>(query, parameters, parameters.GetParameterOrdinal(shardParameterName), shardTuple.Value, resultHandler, false, dataObject, null, cancellationToken));
                        }
                    }
                    await Task.WhenAll(tsks).ConfigureAwait(false);

                    foreach (var tsk in tsks)
                    {
                        var tskResult = tsk.Result;
                        if (tskResult != null)
                        {
                            result.Add(tskResult);
                        }
                    }
                }
                return(result);
            }
Example #11
0
            internal async Task RunAllAsync(Query query, DbParameterCollection parameters, ShardsValues shardParameterValues, string shardParameterName, CancellationToken cancellationToken)
            {
                if (query is null || string.IsNullOrEmpty(query.Sql) || string.IsNullOrEmpty(query.Name))
                {
                    throw new ArgumentNullException(nameof(query));
                }
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }
                if (!(this.MockResults is null) && this.MockResults.Count > 0 && this.MockResults.ContainsKey(query.Name))
                {
                    return;
                }

                cancellationToken.ThrowIfCancellationRequested();
                var shardParameterOrdinal = parameters.GetParameterOrdinal(shardParameterName);
                var tsks = new List <Task>();
                var cancelTokenSource = new CancellationTokenSource();

                using (var queryCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelTokenSource.Token))
                {
                    if (shardParameterValues is null)
                    {
                        foreach (var shardId in dtn.Keys)
                        {
                            parameters.SetShardId(shardParameterOrdinal, shardId);
                            tsks.Add(this.dtn[shardId].Write._manager.RunAsync(query, parameters, parameters.GetParameterOrdinal(shardParameterName), null, null, cancellationToken));
                        }
                    }
                    else
                    {
                        if (shardParameterValues.Shards.Count == 0)
                        {
                            return;
                        }
                        foreach (var shardTuple in shardParameterValues.Shards)
                        {
                            parameters.SetShardId(shardParameterOrdinal, shardTuple.Key);
                            tsks.Add(this.dtn[shardTuple.Key].Write._manager.RunAsync(query, parameters, parameters.GetParameterOrdinal(shardParameterName), shardTuple.Value, null, cancellationToken));
                        }
                    }
                    await Task.WhenAll(tsks).ConfigureAwait(false);
                }
            }
Example #12
0
            internal async Task <List <ShardKey <TRecord, TChild> > > ListAsync <TRecord, TChild>(Query query, char origin, string shardColumnName, string recordColumnName, string childColumnName, DbParameterCollection parameters, ShardsValues shardParameterValues, string shardParameterName, CancellationToken cancellationToken)
                where TRecord : IComparable
                where TChild : IComparable
            {
                var result = new List <ShardKey <TRecord, TChild> >();

                if (query is null || string.IsNullOrEmpty(query.Sql) || string.IsNullOrEmpty(query.Name))
                {
                    throw new ArgumentNullException(nameof(query));
                }
                if (parameters == null)
                {
                    throw new ArgumentNullException(nameof(parameters));
                }
                if (!(this.MockResults is null) && this.MockResults.Count > 0 && this.MockResults.ContainsKey(query.Name))
                {
                    return((List <ShardKey <TRecord, TChild> >) this.MockResults[query.Name]);
                }
                cancellationToken.ThrowIfCancellationRequested();
                var shardParameterOrdinal = parameters.GetParameterOrdinal(shardParameterName);
                var tsks = new List <Task <List <(short, TRecord, TChild)> > >();
                var cancelTokenSource = new CancellationTokenSource();

                using (var queryCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancelTokenSource.Token))
                {
                    if (shardParameterValues is null)
                    {
                        foreach (var shardId in dtn.Keys)
                        {
                            parameters.SetShardId(shardParameterOrdinal, shardId);
                            var tsk = this.dtn[shardId].Read._manager.ListAsync <short, TRecord, TChild>(query, shardColumnName, recordColumnName, childColumnName, parameters, parameters.GetParameterOrdinal(shardParameterName), null, cancellationToken);
                            tsks.Add(tsk);
                        }
                    }
                    else
                    {
                        if (shardParameterValues.Shards.Count == 0)
                        {
                            return(result);
                        }
                        foreach (var shardTuple in shardParameterValues.Shards)
                        {
                            parameters.SetShardId(shardParameterOrdinal, shardTuple.Key);
                            var tsk = this.dtn[shardTuple.Key].Read._manager.ListAsync <short, TRecord, TChild>(query, shardColumnName, recordColumnName, childColumnName, parameters, parameters.GetParameterOrdinal(shardParameterName), shardTuple.Value, cancellationToken);
                            tsks.Add(tsk);
                        }
                    }
                    await Task.WhenAll(tsks).ConfigureAwait(false);

                    foreach (var tsk in tsks)
                    {
                        var interim = tsk.Result;
                        if (!(interim is null))
                        {
                            foreach (var itm in interim)
                            {
                                result.Add(new ShardKey <TRecord, TChild>(origin, itm.Item1, itm.Item2, itm.Item3));
                            }
                        }
                    }
                }
                return(result);
            }