Ejemplo n.º 1
0
        public int Commit()
        {
            var rsp = Assert.ThrowIfError(() => ReindexerBinding.reindexer_commit_transaction(_rx, _tr, _ctxInfo));

            try
            {
                var reader         = new CJsonReader(rsp.@out);
                var rawQueryParams = reader.ReadRawQueryParams();

                return(rawQueryParams.count);
            }
            finally
            {
                [email protected]();
            }
        }
        /// <inheritdoc/>
        public QueryItemsOf <T> ExecuteSql <T>(string sql, Func <byte[], T> deserializeItem)
        {
            var result = new QueryItemsOf <T>
            {
                Items = new List <T>()
            };

            using (var sqlRx = sql.GetHandle())
            {
                var rsp = Assert.ThrowIfError(() => ReindexerBinding.reindexer_select(Rx, sqlRx, 1, new int[0], 0, _ctxInfo));
                try
                {
                    var reader         = new CJsonReader(rsp.@out);
                    var rawQueryParams = reader.ReadRawQueryParams();
                    var explain        = rawQueryParams.explainResults;

                    result.QueryTotalItems = rawQueryParams.totalcount != 0 ? rawQueryParams.totalcount : rawQueryParams.count;
                    if (explain.Length > 0)
                    {
                        result.Explain = JsonSerializer.Deserialize <ExplainDef>(explain.ToArray(), //todo: use span when utf8json supports it.
                                                                                 StandardResolver.ExcludeNull);
                    }

                    for (var i = 0; i < rawQueryParams.count; i++)
                    {
                        var item = reader.ReadRawItemParams();
                        if (item.data.Length > 0)
                        {
                            result.Items.Add(deserializeItem(item.data.ToArray())); //todo: use span when utf8json supports it.
                        }
                    }

                    if ((rawQueryParams.flags & CJsonReader.ResultsWithJoined) != 0 && reader.GetVarUInt() != 0)
                    {
                        throw new NotImplementedException("Sorry, not implemented: Can't return join query results as json");
                    }

                    return(result);
                }
                finally
                {
                    [email protected]();
                }
            }
        }
        /// <inheritdoc/>
        public int ModifyItem(string nsName, ItemModifyMode mode, byte[] itemJson, params string[] precepts)
        {
            var result = 0;

            using (var writer = new CJsonWriter())
            {
                writer.PutVString(nsName);
                writer.PutVarCUInt((int)DataFormat.FormatJson); //format
                writer.PutVarCUInt((int)mode);                  //mode
                writer.PutVarCUInt(0);                          //stateToken

                writer.PutVarCUInt(precepts.Length);            //len(precepts)
                foreach (var precept in precepts)
                {
                    writer.PutVString(precept);
                }

                reindexer_buffer.PinBufferFor(writer.CurrentBuffer, args =>
                {
                    using (var data = reindexer_buffer.From(itemJson))
                    {
                        var rsp = Assert.ThrowIfError(() => ReindexerBinding.reindexer_modify_item_packed(Rx, args, data, _ctxInfo));
                        try
                        {
                            var reader         = new CJsonReader(rsp.@out);
                            var rawQueryParams = reader.ReadRawQueryParams();

                            result = rawQueryParams.count;
                        }
                        finally
                        {
                            [email protected]();
                        }
                    }
                });
            }

            return(result);
        }
        public void ParallelModifyItemPacked()
        {
            var nsName = "ParallelTestNs";

            AssertError(ReindexerBinding.reindexer_open_namespace(_rx, nsName.GetHandle(),
                                                                  new StorageOpts {
                options = StorageOpt.kStorageOptCreateIfMissing | StorageOpt.kStorageOptEnabled
            },
                                                                  _ctxInfo));

            var indexDefJson = JsonSerializer.ToJsonString(
                new Index
            {
                Name      = "Id",
                IsPk      = true,
                FieldType = FieldType.Int64,
                IndexType = IndexType.Hash,
                JsonPaths = new List <string> {
                    "Id"
                }
            }, Utf8Json.Resolvers.StandardResolver.ExcludeNull);

            AssertError(ReindexerBinding.reindexer_add_index(_rx, nsName.GetHandle(), indexDefJson.GetHandle(), _ctxInfo));

            using (var ser1 = new CJsonWriter())
            {
                ser1.PutVString(nsName);
                ser1.PutVarCUInt((int)DataFormat.FormatJson);
                ser1.PutVarCUInt((int)ItemModifyMode.Upsert);
                ser1.PutVarCUInt(0);
                ser1.PutVarCUInt(0);

                reindexer_buffer.PinBufferFor(ser1.CurrentBuffer, args =>
                {
                    Parallel.For(0, 30000, i =>
                    {
                        var dataHandle = reindexer_buffer.From(Encoding.UTF8.GetBytes($"{{\"Id\":{i}, \"Guid\":\"{Guid.NewGuid()}\"}}"));
                        var rsp        = ReindexerBinding.reindexer_modify_item_packed(_rx, args, dataHandle.Buffer, _ctxInfo);
                        try
                        {
                            if (rsp.err_code != 0)
                            {
                                Assert.AreEqual(null, (string)rsp.@out);
                            }

                            Assert.AreNotEqual(UIntPtr.Zero, [email protected]_ptr);

                            var reader         = new CJsonReader(rsp.@out);
                            var rawQueryParams = reader.ReadRawQueryParams();

                            Assert.AreEqual(1, rawQueryParams.count);

                            reader.ReadRawItemParams();
                        }
                        finally
                        {
                            dataHandle.Dispose();
                            [email protected]();
                        }
                    });
                });
            }

            Thread.Sleep(6000);
#pragma warning disable S1215 // "GC.Collect" should not be called
            GC.Collect();
            GC.WaitForPendingFinalizers();
#pragma warning restore S1215 // "GC.Collect" should not be called
            AssertError(ReindexerBinding.reindexer_truncate_namespace(_rx, nsName.GetHandle(), _ctxInfo));
        }
        public void ModifyItemPacked(string itemJson = null)
        {
            AssertError(ReindexerBinding.reindexer_open_namespace(_rx, DataTestNamespace.GetHandle(),
                                                                  new StorageOpts {
                options = StorageOpt.kStorageOptCreateIfMissing | StorageOpt.kStorageOptEnabled
            },
                                                                  _ctxInfo));

            var indexDefJson = JsonSerializer.ToJsonString(
                new Index
            {
                Name      = "Id",
                IsPk      = true,
                FieldType = FieldType.Int64,
                IndexType = IndexType.Hash,
                JsonPaths = new List <string> {
                    "Id"
                }
            }, Utf8Json.Resolvers.StandardResolver.ExcludeNull);

            AssertError(ReindexerBinding.reindexer_add_index(_rx, DataTestNamespace.GetHandle(), indexDefJson.GetHandle(), _ctxInfo));
            indexDefJson = JsonSerializer.ToJsonString(
                new Index
            {
                Name      = "Guid",
                IsPk      = false,
                FieldType = FieldType.String,
                IndexType = IndexType.Hash,
                JsonPaths = new List <string> {
                    "Guid"
                }
            }, Utf8Json.Resolvers.StandardResolver.ExcludeNull);
            AssertError(ReindexerBinding.reindexer_add_index(_rx, DataTestNamespace.GetHandle(), indexDefJson.GetHandle(), _ctxInfo));

            var rsp = ReindexerBinding.reindexer_select(_rx,
                                                        $"SELECT \"indexes.name\" FROM #namespaces WHERE name = \"{DataTestNamespace}\"".GetHandle(),
                                                        1, new int[0], 0, _ctxInfo);

            if (rsp.err_code != 0)
            {
                Assert.AreEqual(null, (string)rsp.@out);
            }
            Assert.AreNotEqual(UIntPtr.Zero, [email protected]_ptr);

            var(json, offsets, explain) = BindingHelpers.RawResultToJson(rsp.@out, "items", "total_count");
            var indexNames = JsonSerializer.Deserialize <ItemsOf <Namespace> >(json).Items.SelectMany(n => n.Indexes.Select(i => i.Name)).ToList();

            CollectionAssert.Contains(indexNames as ICollection, "Id");
            CollectionAssert.Contains(indexNames as ICollection, "Guid");

            using (var ser1 = new CJsonWriter())
            {
                ser1.PutVString(DataTestNamespace);
                ser1.PutVarCUInt((int)DataFormat.FormatJson); //format
                ser1.PutVarCUInt((int)ItemModifyMode.Upsert); //mode
                ser1.PutVarCUInt(0);                          //stateToken
                ser1.PutVarCUInt(0);                          //len(precepts)

                reindexer_buffer.PinBufferFor(ser1.CurrentBuffer, args =>
                {
                    using (var data = reindexer_buffer.From(Encoding.UTF8.GetBytes(itemJson ?? $"{{\"Id\":1, \"Guid\":\"{Guid.NewGuid()}\"}}")))
                    {
                        rsp = ReindexerBinding.reindexer_modify_item_packed(_rx, args, data.Buffer, _ctxInfo);
                        if (rsp.err_code != 0)
                        {
                            Assert.AreEqual(null, (string)rsp.@out);
                        }

                        Assert.AreNotEqual(UIntPtr.Zero, [email protected]_ptr);

                        var reader         = new CJsonReader(rsp.@out);
                        var rawQueryParams = reader.ReadRawQueryParams();

                        Assert.AreEqual(1, rawQueryParams.count);

                        reader.ReadRawItemParams();

                        if ([email protected]_ptr != UIntPtr.Zero)
                        {
                            AssertError(ReindexerBinding.reindexer_free_buffer(rsp.@out));
                        }
                    }
                });
            }
        }