public long AddEdge(Transaction tx,
                            long fromId,
                            long toId,
                            byte *data,
                            int size)
        {
            ThrowIfDisposed();
            var id          = ++tx.NextId;
            var bigEndianId = IPAddress.NetworkToHostOrder(id);             //convert to big endian

            var val = new TableValueBuilder
            {
                { (byte *)&bigEndianId, sizeof(long) },
                { (byte *)&fromId, sizeof(long) },
                { (byte *)&toId, sizeof(long) },
                { data, size }
            };

            tx.EdgeTable.Set(val);

            return(id);
        }
示例#2
0
        private void PutInternal(DocumentsOperationContext context, string key, long newEtagBigEndian, BlittableJsonReaderObject document, Table table)
        {
            byte *lowerKey;
            int   lowerSize;
            byte *keyPtr;
            int   keySize;

            DocumentKeyWorker.GetLowerKeySliceAndStorageKey(context, key, out lowerKey, out lowerSize, out keyPtr, out keySize);

            byte recordSeperator = 30;

            var tbv = new TableValueBuilder
            {
                { lowerKey, lowerSize },
                {&recordSeperator, sizeof(char) },
                { (byte *)&newEtagBigEndian, sizeof(long) },
                { keyPtr, keySize },
                { document.BasePointer, document.Size }
            };

            table.Insert(tbv);
        }
示例#3
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);
            }
        }
示例#4
0
        public void CanBuild()
        {
            var oren   = Encoding.UTF8.GetBytes("Oren Eini");
            var longer = Encoding.UTF8.GetBytes("There is a man on the moon and he's eating cheese");
            var fun    = Encoding.UTF8.GetBytes("Writing code that make funny stuff happen is quite a pleasurable activity");

            fixed(byte *pOren = oren)
            {
                fixed(byte *pLonger = longer)
                {
                    fixed(byte *pFun = fun)
                    {
                        var tableValue = new TableValueBuilder
                        {
                            { pOren, oren.Length },
                            { pFun, fun.Length },
                            { pLonger, longer.Length }
                        };

                        Assert.Equal(longer.Length + oren.Length + fun.Length + 3 + 1, tableValue.Size);
                    }
                }
            }
        }
示例#5
0
        public unsafe void SaveLicense(License license)
        {
            TransactionOperationContext context;

            using (_contextPool.AllocateOperationContext(out context))
                using (var tx = context.OpenWriteTransaction())
                {
                    var table = tx.InnerTransaction.OpenTable(_licenseStorageSchema, LicenseInfoSchema.LicenseTree);

                    using (var id = context.GetLazyString(LicenseStoargeKey))
                        using (var json = context.ReadObject(license.ToJson(), LicenseStoargeKey,
                                                             BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                        {
                            var tvb = new TableValueBuilder
                            {
                                { id.Buffer, id.Size },
                                { json.BasePointer, json.Size }
                            };

                            table.Set(tvb);
                        }
                    tx.Commit();
                }
        }
示例#6
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())
            {
                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"));
            }
        }
示例#7
0
        public unsafe IndexFailureInformation UpdateStats(DateTime indexingTime, IndexingRunStats stats)
        {
            if (_logger.IsInfoEnabled)
            {
                _logger.Info($"Updating statistics for '{_index.Name}'. Stats: {stats}.");
            }

            using (_contextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var tx = context.OpenWriteTransaction())
                {
                    var result = new IndexFailureInformation
                    {
                        Name = _index.Name
                    };

                    var table = tx.InnerTransaction.OpenTable(_errorsSchema, "Errors");

                    var statsTree = tx.InnerTransaction.ReadTree(IndexSchema.StatsTree);

                    result.MapAttempts  = statsTree.Increment(IndexSchema.MapAttemptsSlice, stats.MapAttempts);
                    result.MapSuccesses = statsTree.Increment(IndexSchema.MapSuccessesSlice, stats.MapSuccesses);
                    result.MapErrors    = statsTree.Increment(IndexSchema.MapErrorsSlice, stats.MapErrors);

                    var currentMaxNumberOfOutputs = statsTree.Read(IndexSchema.MaxNumberOfOutputsPerDocument)?.Reader.ReadLittleEndianInt32();

                    using (statsTree.DirectAdd(IndexSchema.MaxNumberOfOutputsPerDocument, sizeof(int), out byte *ptr))
                    {
                        *(int *)ptr = currentMaxNumberOfOutputs > stats.MaxNumberOfOutputsPerDocument
                        ? currentMaxNumberOfOutputs.Value
                        : stats.MaxNumberOfOutputsPerDocument;
                    }

                    if (_index.Type.IsMapReduce())
                    {
                        result.ReduceAttempts  = statsTree.Increment(IndexSchema.ReduceAttemptsSlice, stats.ReduceAttempts);
                        result.ReduceSuccesses = statsTree.Increment(IndexSchema.ReduceSuccessesSlice, stats.ReduceSuccesses);
                        result.ReduceErrors    = statsTree.Increment(IndexSchema.ReduceErrorsSlice, stats.ReduceErrors);
                    }

                    var binaryDate = indexingTime.ToBinary();
                    using (Slice.External(context.Allocator, (byte *)&binaryDate, sizeof(long), out Slice binaryDateslice))
                        statsTree.Add(IndexSchema.LastIndexingTimeSlice, binaryDateslice);

                    if (stats.Errors != null)
                    {
                        for (var i = Math.Max(stats.Errors.Count - MaxNumberOfKeptErrors, 0); i < stats.Errors.Count; i++)
                        {
                            var error          = stats.Errors[i];
                            var ticksBigEndian = Bits.SwapBytes(error.Timestamp.Ticks);
                            using (var document = context.GetLazyString(error.Document))
                                using (var action = context.GetLazyString(error.Action))
                                    using (var e = context.GetLazyString(error.Error))
                                    {
                                        var tvb = new TableValueBuilder
                                        {
                                            { (byte *)&ticksBigEndian, sizeof(long) },
                                            { document.Buffer, document.Size },
                                            { action.Buffer, action.Size },
                                            { e.Buffer, e.Size }
                                        };
                                        table.Insert(tvb);
                                    }
                        }

                        CleanupErrors(table);
                    }

                    tx.Commit();

                    return(result);
                }
        }
示例#8
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();
            }
        }
        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}");
                    }
                }
            }
        }
        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);
            }
        }