private void GetCompareExchangeValuesByKey(TransactionOperationContext context, Microsoft.Extensions.Primitives.StringValues keys)
        {
            var sw = Stopwatch.StartNew();

            var items = new List <(string Key, long Index, BlittableJsonReaderObject Value)>(keys.Count);

            foreach (var key in keys)
            {
                var item = ServerStore.Cluster.GetCompareExchangeValue(context, CompareExchangeCommandBase.GetActualKey(Database.Name, key));
                if (item.Value == null && keys.Count == 1)
                {
                    HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    return;
                }

                items.Add((key, item.Index, item.Value));
            }

            var  numberOfResults           = 0;
            long totalDocumentsSizeInBytes = 0;

            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                writer.WriteStartObject();

                writer.WriteArray(context, "Results", items,
                                  (textWriter, operationContext, item) =>
                {
                    numberOfResults++;
                    totalDocumentsSizeInBytes += item.Value?.Size ?? 0;
                    operationContext.Write(textWriter, new DynamicJsonValue
                    {
                        ["Key"]   = item.Key,
                        ["Value"] = item.Value,
                        ["Index"] = item.Index
                    });
                });

                writer.WriteEndObject();
            }

            AddPagingPerformanceHint(PagingOperationType.CompareExchange, nameof(GetCompareExchangeValuesByKey), HttpContext.Request.QueryString.Value,
                                     numberOfResults, keys.Count, sw.ElapsedMilliseconds, totalDocumentsSizeInBytes);
        }
Esempio n. 2
0
        private void GetCompareExchangeValues(TransactionOperationContext context)
        {
            var sw = Stopwatch.StartNew();

            var start    = GetStart();
            var pageSize = GetPageSize();

            var startsWithKey = GetStringQueryString("startsWith", false);
            var items         = ServerStore.Cluster.GetCompareExchangeValuesStartsWith(context, Database.Name, CompareExchangeCommandBase.GetActualKey(Database.Name, startsWithKey), start, pageSize);

            var numberOfResults = 0;

            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                writer.WriteStartObject();

                writer.WriteArray(context, "Results", items,
                                  (textWriter, operationContext, item) =>
                {
                    numberOfResults++;
                    operationContext.Write(textWriter, new DynamicJsonValue
                    {
                        [nameof(CompareExchangeListItem.Key)]   = item.Key,
                        [nameof(CompareExchangeListItem.Value)] = item.Value,
                        [nameof(CompareExchangeListItem.Index)] = item.Index
                    });
                });

                writer.WriteEndObject();
            }

            AddPagingPerformanceHint(PagingOperationType.CompareExchange, nameof(ClusterStateMachine.GetCompareExchangeValuesStartsWith),
                                     HttpContext.Request.QueryString.Value, numberOfResults, pageSize, sw.ElapsedMilliseconds);
        }
Esempio n. 3
0
        public IEnumerable <(string key, long index, BlittableJsonReaderObject value)> GetCompareExchangeValues()
        {
            Debug.Assert(_serverContext != null);

            return(_database.ServerStore.Cluster.GetCompareExchangeValuesStartsWith(_serverContext, _database.Name, CompareExchangeCommandBase.GetActualKey(_database.Name, null), 0, int.MaxValue));
        }
Esempio n. 4
0
        public bool Update(UpdateStep step)
        {
            var oldCompareExchangeSchema = new TableSchema().
                                           DefineKey(new TableSchema.SchemaIndexDef
            {
                StartIndex = (int)ClusterStateMachine.CompareExchangeTable.Key,
                Count      = 1
            });

            var newCompareExchangeSchema = new TableSchema()
                                           .DefineKey(new TableSchema.SchemaIndexDef
            {
                StartIndex = (int)ClusterStateMachine.CompareExchangeTable.Key,
                Count      = 1
            }).DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = (int)ClusterStateMachine.CompareExchangeTable.PrefixIndex,
                Count      = 1,
                Name       = ClusterStateMachine.CompareExchangeIndex
            });

            const string oldTableName = "CmpXchg";

            using (Slice.From(step.WriteTx.Allocator, oldTableName, out var oldCompareExchangeTable))
            {
                var oldTable = step.ReadTx.OpenTable(oldCompareExchangeSchema, oldCompareExchangeTable);
                if (oldTable == null)
                {
                    return(true);
                }

                var newTableName = ClusterStateMachine.CompareExchange.ToString();
                foreach (var db in SchemaUpgradeExtensions.GetDatabases(step))
                {
                    // update CompareExchange
                    newCompareExchangeSchema.Create(step.WriteTx, newTableName, null);
                    var newTable = step.WriteTx.OpenTable(newCompareExchangeSchema, newTableName);
                    var compareExchangeOldKey = $"{db.ToLowerInvariant()}/";

                    using (Slice.From(step.ReadTx.Allocator, compareExchangeOldKey, out var keyPrefix))
                    {
                        foreach (var item in oldTable.SeekByPrimaryKeyPrefix(keyPrefix, Slices.Empty, 0))
                        {
                            var index = DocumentsStorage.TableValueToLong((int)ClusterStateMachine.CompareExchangeTable.Index, ref item.Value.Reader);

                            using (CompareExchangeCommandBase.GetPrefixIndexSlices(step.ReadTx.Allocator, db, index, out var buffer))
                                using (Slice.External(step.WriteTx.Allocator, buffer.Ptr, buffer.Length, out var prefixIndexSlice))
                                    using (newTable.Allocate(out TableValueBuilder write))
                                        using (var ctx = JsonOperationContext.ShortTermSingleUse())
                                        {
                                            using (var bjro = new BlittableJsonReaderObject(
                                                       item.Value.Reader.Read((int)ClusterStateMachine.CompareExchangeTable.Value, out var size1),
                                                       size1, ctx).Clone(ctx)
                                                   )
                                            {
                                                write.Add(item.Key);
                                                write.Add(index);
                                                write.Add(bjro.BasePointer, bjro.Size);
                                                write.Add(prefixIndexSlice);

                                                newTable.Set(write);
                                            }
                                        }
                        }
                    }
                }
            }

            // delete the old table
            step.WriteTx.DeleteTable(oldTableName);

            // remove the remaining CompareExchange global index
            if (step.WriteTx.LowLevelTransaction.RootObjects.Read(ClusterStateMachine.CompareExchangeIndex) != null)
            {
                step.WriteTx.DeleteTree(ClusterStateMachine.CompareExchangeIndex);
            }

            return(true);
        }