Example #1
0
        public List <Metric> GetMetrics(string metricName, int windowSeconds)
        {
            var metric = new MetricsRequest();

            if (metricName != null)
            {
                metric.Name = metricName;
            }

            if (windowSeconds > 0)
            {
                metric.Seconds = (ulong)windowSeconds;
            }

            var id     = Interlocked.Increment(ref messageNumber) - 1;
            var future = new FutureOperationResult(new TaskCompletionSource <List <Metric> >());

            this.futureOperationResults.TryAdd(id, future);

            var msg = new Message
            {
                Id             = (ulong)id,
                MetricsRequest = metric
            };

            this.child.Add(msg);

            return(future.RightOrDefault?.Task.Result);
        }
Example #2
0
        public TaskCompletionSource <UserRecordResult> AddUserRecord(string stream, string partitionKey, string explicitHashKey, byte[] data)
        {
            if (string.IsNullOrEmpty(stream))
            {
                throw new ArgumentException("Stream name cannot be null or empty", nameof(stream));
            }

            stream = stream.Trim();
            if (stream.Length == 0)
            {
                throw new ArgumentException("Stream name cannot be empty", nameof(stream));
            }

            if (partitionKey == null)
            {
                throw new ArgumentException("PartitionKey cannot be null", nameof(partitionKey));
            }

            if (partitionKey.Length < 1 || partitionKey.Length > 256)
            {
                throw new ArgumentException($"Invalid parition key. Length must be at least 1 and at most 256, got {partitionKey.Length}", nameof(partitionKey));
            }

            try
            {
                Encoding.UTF8.GetBytes(partitionKey);
            }
            catch (Exception)
            {
                throw new ArgumentException("Partition key must be valid UTF-8", nameof(partitionKey));
            }

            BigInteger?b = null;

            if (explicitHashKey != null)
            {
                explicitHashKey = explicitHashKey.Trim();
                try
                {
                    b = BigInteger.Parse(explicitHashKey);
                }
                catch (FormatException)
                {
                    throw new ArgumentException($"Invalid explicitHashKey, must be an integer, got {explicitHashKey}", nameof(explicitHashKey));
                }

                if (b.Value.CompareTo(UINT_128_MAX) > 0 || b.Value.CompareTo(BigInteger.Zero) < 0)
                {
                    throw new ArgumentException($"Invalid explicitHashKey, must be greater or equal to zero and less than or equal to (2^128 - 1), got {explicitHashKey}", nameof(explicitHashKey));
                }
            }

            if (data != null && data.Length > 1024 * 1024)
            {
                throw new ArgumentException($"Data must be less than or equal to 1MB in size, got {data.Length} bytes", nameof(data));
            }

            long id     = Interlocked.Increment(ref messageNumber) - 1;
            var  future = new FutureOperationResult(new TaskCompletionSource <UserRecordResult>());

            futureOperationResults.TryAdd(id, future);

            var putRecord = new PutRecord
            {
                Data         = data ?? new byte[0],
                StreamName   = stream,
                PartitionKey = partitionKey
            };

            if (b.HasValue)
            {
                putRecord.ExplicitHashKey = b.ToString();
            }

            var msg = new Message
            {
                Id        = (ulong)id,
                PutRecord = putRecord
            };

            child.Add(msg);

            return(future.LeftOrDefault);
        }