コード例 #1
0
        public unsafe long SetHelper(Table table, params object[] args)
        {
            var handles1 = new List <GCHandle>();

            var builder = new TableValueBuilder();

            foreach (var o in args)
            {
                byte[]   buffer;
                GCHandle gcHandle;

                var s = o as string;
                if (s != null)
                {
                    buffer   = Encoding.UTF8.GetBytes(s);
                    gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    builder.Add((byte *)gcHandle.AddrOfPinnedObject(), buffer.Length);
                    handles1.Add(gcHandle);
                    continue;
                }

                if (o is Slice)
                {
                    var slice = (Slice)o;
                    builder.Add(slice.Content.Ptr, slice.Content.Length);

                    continue;
                }

                var stream = o as MemoryStream;
                if (stream != null)
                {
                    buffer   = stream.ToArray();
                    gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    builder.Add((byte *)gcHandle.AddrOfPinnedObject(), buffer.Length);
                    handles1.Add(gcHandle);

                    continue;
                }

                var l = (long)o;
                buffer   = EndianBitConverter.Big.GetBytes(l);
                gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                builder.Add((byte *)gcHandle.AddrOfPinnedObject(), buffer.Length);
                handles1.Add(gcHandle);
            }

            var handles = handles1;

            long id = table.Set(builder);

            foreach (var gcHandle in handles)
            {
                gcHandle.Free();
            }

            return(id);
        }
コード例 #2
0
        private void StoreAggregationResult(long modifiedPage, int aggregatedEntries, Table table, AggregationResult result, IndexingStatsScope stats)
        {
            using (_treeReductionStats.StoringReduceResult.Start())
            {
                var pageNumber      = Bits.SwapBytes(modifiedPage);
                var numberOfOutputs = result.Count;

                var tvb = new TableValueBuilder
                {
                    pageNumber,
                    aggregatedEntries,
                    numberOfOutputs
                };

                foreach (var output in result.GetOutputsToStore())
                {
                    tvb.Add(output.BasePointer, output.Size);
                }

                table.Set(tvb);
            }
        }
コード例 #3
0
ファイル: RavenDB_13195.cs プロジェクト: morhilai/ravendb
        public unsafe void CanDeleteTableWithLargeValues()
        {
            TableSchema schema = new TableSchema();

            schema.DefineKey(new TableSchema.SchemaIndexDef
            {
                StartIndex = 0,
                Count      = 1,
                Name       = Etag,
                IsGlobal   = true
            });

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 0,
                Count      = 1,
                Name       = Local,
                IsGlobal   = false
            });

            using (Transaction tx = Env.WriteTransaction())
            {
                schema.Create(tx, "first", 256);
                schema.Create(tx, "second", 256);

                Table fst = tx.OpenTable(schema, "first");
                Table snd = tx.OpenTable(schema, "second");

                for (var i = 0; i < 1000;)
                {
                    var bytes = new byte[2 * RawDataSection.MaxItemSize + 100];

                    new Random(i).NextBytes(bytes);

                    TableValueBuilder tvb1 = new TableValueBuilder();
                    TableValueBuilder tvb2 = new TableValueBuilder();

                    tvb1.Add(i++);
                    tvb2.Add(i++);

                    fixed(byte *ptr = bytes)
                    {
                        tvb1.Add(ptr, bytes.Length);
                        tvb2.Add(ptr, bytes.Length);

                        fst.Insert(tvb1);
                        snd.Insert(tvb2);
                    }
                }

                tx.Commit();
            }

            using (var tx = Env.WriteTransaction())
            {
                tx.DeleteTable("first");
                tx.DeleteTable("second");

                tx.Commit();
            }

            using (var tx = Env.WriteTransaction())
            {
                Assert.Null(tx.LowLevelTransaction.RootObjects.Read("first"));
                Assert.Null(tx.LowLevelTransaction.RootObjects.Read("second"));
            }
        }
コード例 #4
0
        public unsafe void CanDeleteTableWithLargeValues()
        {
            TableSchema schema = new TableSchema();

            schema.DefineKey(new TableSchema.SchemaIndexDef
            {
                StartIndex = 0,
                Count      = 1,
                Name       = Etag,
                IsGlobal   = true
            });

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 0,
                Count      = 1,
                Name       = Local,
                IsGlobal   = false
            });

            using (Transaction tx = Env.WriteTransaction())
            {
                schema.Create(tx, "first", 256);
                schema.Create(tx, "second", 256);

                Table fst = tx.OpenTable(schema, "first");
                Table snd = tx.OpenTable(schema, "second");

                for (var i = 0; i < 1000;)
                {
                    var bytes = new byte[2 * RawDataSection.MaxItemSize + 100];

                    new Random(i).NextBytes(bytes);

                    TableValueBuilder tvb1 = new TableValueBuilder();
                    TableValueBuilder tvb2 = new TableValueBuilder();

                    tvb1.Add(i++);
                    tvb2.Add(i++);

                    fixed(byte *ptr = bytes)
                    {
                        tvb1.Add(ptr, bytes.Length);
                        tvb2.Add(ptr, bytes.Length);

                        fst.Insert(tvb1);
                        snd.Insert(tvb2);
                    }
                }

                tx.Commit();
            }

            using (var tx = Env.WriteTransaction())
            {
                var firstTable = tx.OpenTable(schema, "first");

                firstTable.DeleteByPrimaryKey(Slices.BeforeAllKeys, x =>
                {
                    if (schema.Key.IsGlobal)
                    {
                        return(firstTable.IsOwned(x.Reader.Id));
                    }

                    return(true);
                });

                var secondTable = tx.OpenTable(schema, "first");

                secondTable.DeleteByPrimaryKey(Slices.BeforeAllKeys, x =>
                {
                    if (schema.Key.IsGlobal)
                    {
                        return(secondTable.IsOwned(x.Reader.Id));
                    }

                    return(true);
                });

                tx.Commit();
            }
        }
コード例 #5
0
        public unsafe void ShouldNotError()
        {
            TableSchema schema = new TableSchema();

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 0,
                Count      = 1,
                Name       = Etag,
                IsGlobal   = true
            });

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 1,
                Count      = 1,
                Name       = Global,
                IsGlobal   = true
            });

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 2,
                Count      = 1,
                Name       = Local,
                IsGlobal   = false
            });

            using (Transaction tx = Env.WriteTransaction())
            {
                schema.Create(tx, "test", 256);

                Table fst = tx.OpenTable(schema, "test");

                schema.Create(tx, "snd", 256);

                for (int i = 0; i < 20_000; i++)
                {
                    TableValueBuilder tvb = new TableValueBuilder();
                    tvb.Add(i);
                    tvb.Add(0);
                    tvb.Add(i);

                    fst.Insert(tvb);
                }

                tx.Commit();
            }

            using (Transaction tx = Env.WriteTransaction())
            {
                Table snd = tx.OpenTable(schema, "snd");
                Table fst = tx.OpenTable(schema, "test");


                for (int i = 1; i < 20_000; i += 2)
                {
                    using (Slice.From(tx.Allocator, BitConverter.GetBytes(i), out var key))
                    {
                        var a = fst.DeleteForwardFrom(schema.Indexes[Etag], key, true, 1);
                        Assert.True(1 == a, $"{a} on {i}");
                    }

                    snd.Insert(new TableValueBuilder
                    {
                        i,
                        0,
                        i
                    });
                }

                tx.Commit();
            }

            using (var tx = Env.WriteTransaction())
            {
                Table snd = tx.OpenTable(schema, "snd");

                for (int i = 1; i < 20_000; i += 2)
                {
                    using (Slice.From(tx.Allocator, BitConverter.GetBytes(i), out var key))
                    {
                        var a = snd.DeleteForwardFrom(schema.Indexes[Etag], key, true, 1);
                        Assert.True(1 == a, $"{a} on {i}");
                    }
                }
            }
        }
コード例 #6
0
        public unsafe void SameTransaction()
        {
            TableSchema schema = new TableSchema();

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 1,
                Count      = 1,
                Name       = Global,
                IsGlobal   = true
            });

            schema.DefineIndex(new TableSchema.SchemaIndexDef
            {
                StartIndex = 2,
                Count      = 1,
                Name       = Local,
                IsGlobal   = false
            });

            using (Transaction tx = Env.WriteTransaction())
            {
                schema.Create(tx, "test", 256);

                Table tbl = tx.OpenTable(schema, "test");

                schema.Create(tx, "snd", 256);

                Table snd = tx.OpenTable(schema, "snd");


                for (int i = 0; i < 20_000; i += 2)
                {
                    var tvb = new TableValueBuilder();
                    tvb.Add(i);
                    tvb.Add(0);
                    tvb.Add(i);

                    tbl.Insert(tvb);
                }

                for (int i = 1; i < 20_000; i += 2)
                {
                    TableValueBuilder tvb = new TableValueBuilder();
                    tvb.Add(i);
                    tvb.Add(0);
                    tvb.Add(i);

                    snd.Insert(tvb);
                }

                tx.Commit();
            }

            using (var tx = Env.WriteTransaction())
            {
                Table snd = tx.OpenTable(schema, "snd");

                var a = snd.DeleteForwardFrom(schema.Indexes[Local], Slices.BeforeAllKeys, false, long.MaxValue);
                Assert.Equal(10_000, a);
            }
        }