Example #1
0
            public async Task StoreAsync(string id, string name, Stream stream, string contentType = null, CancellationToken token = default)
            {
                PutAttachmentCommandHelper.ValidateStream(stream);

                using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(token, _token);
                using (_operation.ConcurrencyCheck())
                {
                    _operation.EndPreviousCommandIfNeeded();

                    await _operation.ExecuteBeforeStore().ConfigureAwait(false);

                    try
                    {
                        if (_operation._first == false)
                        {
                            _operation.WriteComma();
                        }

                        _operation._currentWriter.Write("{\"Id\":\"");
                        _operation.WriteString(id);
                        _operation._currentWriter.Write("\",\"Type\":\"AttachmentPUT\",\"Name\":\"");
                        _operation.WriteString(name);

                        if (contentType != null)
                        {
                            _operation._currentWriter.Write("\",\"ContentType\":\"");
                            _operation.WriteString(contentType);
                        }

                        _operation._currentWriter.Write("\",\"ContentLength\":");
                        _operation._currentWriter.Write(stream.Length);
                        _operation._currentWriter.Write('}');
                        await _operation.FlushIfNeeded().ConfigureAwait(false);

                        PutAttachmentCommandHelper.PrepareStream(stream);
                        // pass the default value for bufferSize to make it compile on netstandard2.0
                        await stream.CopyToAsync(_operation._currentWriter.BaseStream, bufferSize : 16 * 1024, cancellationToken : linkedCts.Token).ConfigureAwait(false);

                        await _operation.FlushIfNeeded().ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await _operation.HandleErrors(id, e).ConfigureAwait(false);
                    }
                }
            }
Example #2
0
            private async Task AppendAsyncInternal(DateTime timestamp, ICollection <double> values, string tag = null)
            {
                using (_operation.ConcurrencyCheck())
                {
                    await _operation.ExecuteBeforeStore().ConfigureAwait(false);

                    try
                    {
                        if (_first)
                        {
                            if (_operation._first == false)
                            {
                                _operation.WriteComma();
                            }

                            WritePrefixForNewCommand();
                        }
                        else if (_timeSeriesInBatch >= _operation._timeSeriesBatchSize)
                        {
                            _operation._currentWriter.Write("]}},");
                            WritePrefixForNewCommand();
                        }

                        _timeSeriesInBatch++;

                        if (_first == false)
                        {
                            _operation.WriteComma();
                        }

                        _first = false;

                        _operation._currentWriter.Write('[');

                        timestamp = timestamp.EnsureUtc();
                        _operation._currentWriter.Write(timestamp.Ticks);
                        _operation.WriteComma();

                        _operation._currentWriter.Write(values.Count);
                        _operation.WriteComma();

                        var firstValue = true;
                        foreach (var value in values)
                        {
                            if (firstValue == false)
                            {
                                _operation.WriteComma();
                            }

                            firstValue = false;
                            _operation._currentWriter.Write(value.ToString("R", CultureInfo.InvariantCulture));
                        }

                        if (tag != null)
                        {
                            _operation._currentWriter.Write(",\"");
                            _operation.WriteString(tag);
                            _operation._currentWriter.Write('\"');
                        }

                        _operation._currentWriter.Write(']');

                        await _operation.FlushIfNeeded().ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await _operation.HandleErrors(_id, e).ConfigureAwait(false);
                    }
                }
            }
Example #3
0
            public async Task IncrementAsync(string id, string name, long delta)
            {
                using (_operation.ConcurrencyCheck())
                {
                    await _operation.ExecuteBeforeStore().ConfigureAwait(false);

                    if (_operation._inProgressCommand == CommandType.TimeSeries)
                    {
                        TimeSeriesBulkInsert.ThrowAlreadyRunningTimeSeries();
                    }

                    try
                    {
                        var isFirst = _id == null;
                        if (isFirst || _id.Equals(id, StringComparison.OrdinalIgnoreCase) == false)
                        {
                            if (isFirst == false)
                            {
                                //we need to end the command for the previous document id
                                _operation._currentWriter.Write("]}},");
                            }
                            else if (_operation._first == false)
                            {
                                _operation.WriteComma();
                            }

                            _operation._first = false;

                            _id = id;
                            _operation._inProgressCommand = CommandType.Counters;

                            WritePrefixForNewCommand();
                        }

                        if (_countersInBatch >= _maxCountersInBatch)
                        {
                            _operation._currentWriter.Write("]}},");

                            WritePrefixForNewCommand();
                        }

                        _countersInBatch++;

                        if (_first == false)
                        {
                            _operation.WriteComma();
                        }

                        _first = false;

                        _operation._currentWriter.Write("{\"Type\":\"Increment\",\"CounterName\":\"");
                        _operation.WriteString(name);
                        _operation._currentWriter.Write("\",\"Delta\":");
                        _operation._currentWriter.Write(delta);
                        _operation._currentWriter.Write('}');

                        await _operation.FlushIfNeeded().ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await _operation.HandleErrors(_id, e).ConfigureAwait(false);
                    }
                }
            }