Пример #1
0
        public int GetNextValue(string entity)
        {
            lock (_d)
            {
                KeyRange kr;
                if (_d.TryGetValue(entity, out kr))
                {
                    kr.Current++;
                    if (kr.Current < kr.Max)
                    {
                        return kr.Current;
                    }
                    _d.Remove(entity);
                }

                var res = Db.GetCollection("hilo").FindAndModify(
                    MongoQueryBuilder.DynQuery(x => x._id == entity),
                    SortBy.Ascending("_id"),
                    Update.Inc("cnt", Range), false, true);
                if (!res.Ok) throw new Exception(res.ErrorMessage);
                var d = res.ModifiedDocument;
                kr = new KeyRange();
                kr.Current = d.GetValue("cnt", MongoDB.Bson.BsonValue.Create(0)).AsInt32;
                kr.Max = kr.Current + Range;
                _d[entity] = kr;
                return kr.Current;
            }
        }
Пример #2
0
        public void TestKeyRangeIntersect14()
        {
            var range1   = new KeyRange <string>(Key <string> .CreateKey("a", true), Key <string> .CreatePrefixKey("x"));
            var range2   = new KeyRange <string>(Key <string> .CreateKey("x", true), null);
            var expected = new KeyRange <string>(Key <string> .CreateKey("x", true), Key <string> .CreatePrefixKey("x"));

            Assert.IsFalse(expected.IsEmpty);
            KeyRangeIntersectionHelper(range1, range2, expected);
        }
Пример #3
0
        public void TestKeyRangeIntersect13()
        {
            var range1   = new KeyRange <int>(Key <int> .CreateKey(8, true), Key <int> .CreateKey(10, true));
            var range2   = new KeyRange <int>(Key <int> .CreateKey(10, true), Key <int> .CreateKey(14, true));
            var expected = new KeyRange <int>(Key <int> .CreateKey(10, true), Key <int> .CreateKey(10, true));

            Assert.IsFalse(expected.IsEmpty);
            KeyRangeIntersectionHelper(range1, range2, expected);
        }
Пример #4
0
        public void TestKeyRangeUnion14()
        {
            // The prefix matches more records so we want to use it
            var range1   = new KeyRange <string>(Key <string> .CreateKey("b", false), Key <string> .CreateKey("cc", true));
            var range2   = new KeyRange <string>(Key <string> .CreateKey("a", false), Key <string> .CreatePrefixKey("c"));
            var expected = new KeyRange <string>(Key <string> .CreateKey("a", false), Key <string> .CreatePrefixKey("c"));

            KeyRangeUnionHelper(range1, range2, expected);
        }
Пример #5
0
        protected virtual Task <List <Slice> > LoadPartsAsync(IFdbReadOnlyTransaction trans, TId id)
        {
            var key = this.Subspace.Keys.EncodePartial(id);

            return(trans
                   .GetRange(KeyRange.StartsWith(key))              //TODO: options ?
                   .Select(kvp => kvp.Value)
                   .ToListAsync());
        }
Пример #6
0
        private void ClearTask(IFdbTransaction tr, Slice taskId)
        {
            tr.Annotate("Deleting task {0:P}", taskId);

            // clear all metadata about the task
            tr.ClearRange(KeyRange.StartsWith(this.TaskStore.Keys.Encode(taskId)));
            // decrement pending number of tasks
            this.Counters.Decrement(tr, COUNTER_PENDING_TASKS);
        }
        /// <summary>Remove all the values for a specific key</summary>
        /// <param name="trans"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public void Remove([NotNull] IFdbTransaction trans, TKey key)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }

            trans.ClearRange(KeyRange.StartsWith(this.Location.Partial.Keys.Encode(key)));
        }
Пример #8
0
        public void Test_KeyRange_Intersects()
        {
            Func <byte, byte, KeyRange> range = (x, y) => KeyRange.Create(Slice.FromByte(x), Slice.FromByte(y));

            #region Not Intersecting...

            // [0, 1) [2, 3)
            // #X
            //   #X
            Assert.That(range(0, 1).Intersects(range(2, 3)), Is.False);
            // [2, 3) [0, 1)
            //   #X
            // #X
            Assert.That(range(2, 3).Intersects(range(0, 1)), Is.False);

            // [0, 1) [1, 2)
            // #X
            //  #X
            Assert.That(range(0, 1).Intersects(range(1, 2)), Is.False);
            // [1, 2) [0, 1)
            //  #X
            // #X
            Assert.That(range(1, 2).Intersects(range(0, 1)), Is.False);

            #endregion

            #region Intersecting...

            // [0, 2) [1, 3)
            // ##X
            //  ##X
            Assert.That(range(0, 2).Intersects(range(1, 3)), Is.True);
            // [1, 3) [0, 2)
            //  ##X
            // ##X
            Assert.That(range(1, 3).Intersects(range(0, 2)), Is.True);

            // [0, 1) [0, 2)
            // #X
            // ##X
            Assert.That(range(0, 1).Intersects(range(0, 2)), Is.True);
            // [0, 2) [0, 1)
            // ##X
            // #X
            Assert.That(range(0, 2).Intersects(range(0, 1)), Is.True);

            // [0, 2) [1, 2)
            // ##X
            //  #X
            Assert.That(range(0, 2).Intersects(range(1, 2)), Is.True);
            // [1, 2) [0, 2)
            //  #X
            // ##X
            Assert.That(range(1, 2).Intersects(range(0, 2)), Is.True);

            #endregion
        }
Пример #9
0
        /// <summary>Insert a new document in the collection</summary>
        public async Task InsertAsync(IFdbTransaction trans, TDocument document)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var id = this.IdSelector(document);

            if (id == null)
            {
                throw new InvalidOperationException("Cannot insert a document with a null identifier");
            }

            // encode the document
            var packed = this.ValueEncoder.EncodeValue(document);

            var subspace = await this.Location.Resolve(trans);

            if (subspace == null)
            {
                throw new InvalidOperationException($"Location '{this.Location}' referenced by Document Collection Layer was not found.");
            }

            // Key Prefix = ...(id,)
            var key = subspace.EncodePartial(id);

            // clear previous value
            trans.ClearRange(KeyRange.StartsWith(key));

            int remaining = packed.Count;

            if (remaining <= this.ChunkSize)
            {             // stored as a single element
                // Key = ...(id,)
                trans.Set(key, packed);
            }
            else
            {             // splits in as many chunks as necessary
                // Key = ...(id, N) where N is the chunk index (0-based)
                int p     = 0;
                int index = 0;
                while (remaining > 0)
                {
                    int sz = Math.Max(remaining, this.ChunkSize);
                    trans.Set(subspace[id, index], packed.Substring(p, sz));
                    ++index;
                    p         += sz;
                    remaining -= sz;
                }
            }
        }
Пример #10
0
        public void CreateKeySetFromRanges()
        {
            var ranges = KeySet.FromRanges(
                KeyRange.ClosedOpen(new Key("begin"), new Key("end")), KeyRange.ClosedClosed(new Key(1L), new Key(2L)));

            Assert.Collection(ranges.Ranges,
                              range => Assert.Equal(KeyRange.ClosedOpen(new Key("begin"), new Key("end")), range),
                              range => Assert.Equal(KeyRange.ClosedClosed(new Key(1L), new Key(2L)), range)
                              );
        }
        public FdbRangeQuery <TId> Lookup(IFdbReadOnlyTransaction trans, TValue value, bool reverse = false)
        {
            var prefix = this.Subspace.Keys.EncodePartial(value);

            return(trans
                   .GetRange(KeyRange.StartsWith(prefix), new FdbRangeOptions {
                Reverse = reverse
            })
                   .Select((kvp) => this.Subspace.Keys.Decode(kvp.Key).Item2));
        }
Пример #12
0
        public void Test_FdbKey_PrettyPrint()
        {
            // verify that the pretty printing of keys produce a user friendly output

            Assert.That(FdbKey.Dump(Slice.Nil), Is.EqualTo("<null>"));
            Assert.That(FdbKey.Dump(Slice.Empty), Is.EqualTo("<empty>"));

            Assert.That(FdbKey.Dump(Slice.FromByte(0)), Is.EqualTo("<00>"));
            Assert.That(FdbKey.Dump(Slice.FromByte(255)), Is.EqualTo("<FF>"));

            Assert.That(FdbKey.Dump(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }.AsSlice()), Is.EqualTo("<00><01><02><03><04><05><06><07>"));
            Assert.That(FdbKey.Dump(new byte[] { 255, 254, 253, 252, 251, 250, 249, 248 }.AsSlice()), Is.EqualTo("<FF><FE><FD><FC><FB><FA><F9><F8>"));

            Assert.That(FdbKey.Dump(Slice.FromString("hello")), Is.EqualTo("hello"));
            Assert.That(FdbKey.Dump(Slice.FromString("héllø")), Is.EqualTo("h<C3><A9>ll<C3><B8>"));

            // tuples should be decoded properly

            Assert.That(FdbKey.Dump(TuPack.EncodeKey(123)), Is.EqualTo("(123,)"), "Singleton tuples should end with a ','");
            Assert.That(FdbKey.Dump(TuPack.EncodeKey(Slice.FromByteString("hello"))), Is.EqualTo("(`hello`,)"), "ASCII strings should use single back quotes");
            Assert.That(FdbKey.Dump(TuPack.EncodeKey("héllø")), Is.EqualTo("(\"héllø\",)"), "Unicode strings should use double quotes");
            Assert.That(FdbKey.Dump(TuPack.EncodeKey(new byte[] { 1, 2, 3 }.AsSlice())), Is.EqualTo("(`<01><02><03>`,)"));
            Assert.That(FdbKey.Dump(TuPack.EncodeKey(123, 456)), Is.EqualTo("(123, 456)"), "Elements should be separated with a space, and not end up with ','");
            Assert.That(FdbKey.Dump(TuPack.EncodeKey(default(object), true, false)), Is.EqualTo("(null, true, false)"), "Booleans should be displayed as numbers, and null should be in lowercase");             //note: even though it's tempting to using Python's "Nil", it's not very ".NETty"
            //note: the string representation of double is not identical between NetFx and .NET Core! So we cannot used a constant literal here
            Assert.That(FdbKey.Dump(TuPack.EncodeKey(1.0d, Math.PI, Math.E)), Is.EqualTo("(1, " + Math.PI.ToString("R", CultureInfo.InvariantCulture) + ", " + Math.E.ToString("R", CultureInfo.InvariantCulture) + ")"), "Doubles should used dot and have full precision (17 digits)");
            Assert.That(FdbKey.Dump(TuPack.EncodeKey(1.0f, (float)Math.PI, (float)Math.E)), Is.EqualTo("(1, " + ((float)Math.PI).ToString("R", CultureInfo.InvariantCulture) + ", " + ((float)Math.E).ToString("R", CultureInfo.InvariantCulture) + ")"), "Singles should used dot and have full precision (10 digits)");
            var guid = Guid.NewGuid();

            Assert.That(FdbKey.Dump(TuPack.EncodeKey(guid)), Is.EqualTo($"({guid:B},)"), "GUIDs should be displayed as a string literal, surrounded by {{...}}, and without quotes");
            var uuid128 = Uuid128.NewUuid();

            Assert.That(FdbKey.Dump(TuPack.EncodeKey(uuid128)), Is.EqualTo($"({uuid128:B},)"), "Uuid128s should be displayed as a string literal, surrounded by {{...}}, and without quotes");
            var uuid64 = Uuid64.NewUuid();

            Assert.That(FdbKey.Dump(TuPack.EncodeKey(uuid64)), Is.EqualTo($"({uuid64:B},)"), "Uuid64s should be displayed as a string literal, surrounded by {{...}}, and without quotes");

            // ranges should be decoded when possible
            var key = TuPack.ToRange(STuple.Create("hello"));

            // "<02>hello<00><00>" .. "<02>hello<00><FF>"
            Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",).<00>"));
            Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",).<FF>"));

            key = KeyRange.StartsWith(TuPack.EncodeKey("hello"));
            // "<02>hello<00>" .. "<02>hello<01>"
            Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",)"));
            Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",) + 1"));

            var t = TuPack.EncodeKey(123);

            Assert.That(FdbKey.PrettyPrint(t, FdbKey.PrettyPrintMode.Single), Is.EqualTo("(123,)"));
            Assert.That(FdbKey.PrettyPrint(TuPack.ToRange(t).Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(123,).<00>"));
            Assert.That(FdbKey.PrettyPrint(TuPack.ToRange(t).End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(123,).<FF>"));
        }
Пример #13
0
        public void Test_FdbKey_PrettyPrint()
        {
            // verify that the pretty printing of keys produce a user friendly output

            Assert.That(FdbKey.Dump(Slice.Nil), Is.EqualTo("<null>"));
            Assert.That(FdbKey.Dump(Slice.Empty), Is.EqualTo("<empty>"));

            Assert.That(FdbKey.Dump(Slice.FromByte(0)), Is.EqualTo("<00>"));
            Assert.That(FdbKey.Dump(Slice.FromByte(255)), Is.EqualTo("<FF>"));

            Assert.That(FdbKey.Dump(Slice.Create(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 })), Is.EqualTo("<00><01><02><03><04><05><06><07>"));
            Assert.That(FdbKey.Dump(Slice.Create(new byte[] { 255, 254, 253, 252, 251, 250, 249, 248 })), Is.EqualTo("<FF><FE><FD><FC><FB><FA><F9><F8>"));

            Assert.That(FdbKey.Dump(Slice.FromString("hello")), Is.EqualTo("hello"));
            Assert.That(FdbKey.Dump(Slice.FromString("héllø")), Is.EqualTo("h<C3><A9>ll<C3><B8>"));

            // tuples should be decoded properly

            Assert.That(FdbKey.Dump(STuple.EncodeKey(123)), Is.EqualTo("(123,)"), "Singleton tuples should end with a ','");
            Assert.That(FdbKey.Dump(STuple.EncodeKey(Slice.FromAscii("hello"))), Is.EqualTo("('hello',)"), "ASCII strings should use single quotes");
            Assert.That(FdbKey.Dump(STuple.EncodeKey("héllø")), Is.EqualTo("(\"héllø\",)"), "Unicode strings should use double quotes");
            Assert.That(FdbKey.Dump(STuple.EncodeKey(Slice.Create(new byte[] { 1, 2, 3 }))), Is.EqualTo("(<01 02 03>,)"));
            Assert.That(FdbKey.Dump(STuple.EncodeKey(123, 456)), Is.EqualTo("(123, 456)"), "Elements should be separated with a space, and not end up with ','");
            Assert.That(FdbKey.Dump(STuple.EncodeKey(true, false, default(object))), Is.EqualTo("(1, 0, null)"), "Booleans should be displayed as numbers, and null should be in lowercase");             //note: even though it's tempting to using Python's "Nil", it's not very ".NETty"
            Assert.That(FdbKey.Dump(STuple.EncodeKey(1.0d, Math.PI, Math.E)), Is.EqualTo("(1, 3.1415926535897931, 2.7182818284590451)"), "Doubles should used dot and have full precision (17 digits)");
            Assert.That(FdbKey.Dump(STuple.EncodeKey(1.0f, (float)Math.PI, (float)Math.E)), Is.EqualTo("(1, 3.14159274, 2.71828175)"), "Singles should used dot and have full precision (10 digits)");
            var guid = Guid.NewGuid();

            Assert.That(FdbKey.Dump(STuple.EncodeKey(guid)), Is.EqualTo(String.Format("({0},)", guid.ToString("B"))), "GUIDs should be displayed as a string literal, surrounded by {...}, and without quotes");
            var uuid128 = Uuid128.NewUuid();

            Assert.That(FdbKey.Dump(STuple.EncodeKey(uuid128)), Is.EqualTo(String.Format("({0},)", uuid128.ToString("B"))), "Uuid128s should be displayed as a string literal, surrounded by {...}, and without quotes");
            var uuid64 = Uuid64.NewUuid();

            Assert.That(FdbKey.Dump(STuple.EncodeKey(uuid64)), Is.EqualTo(String.Format("({0},)", uuid64.ToString("B"))), "Uuid64s should be displayed as a string literal, surrounded by {...}, and without quotes");

            // ranges should be decoded when possible
            var key = STuple.ToRange(STuple.Create("hello"));

            // "<02>hello<00><00>" .. "<02>hello<00><FF>"
            Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",).<00>"));
            Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",).<FF>"));

            key = KeyRange.StartsWith(STuple.EncodeKey("hello"));
            // "<02>hello<00>" .. "<02>hello<01>"
            Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",)"));
            Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",) + 1"));

            var t = STuple.EncodeKey(123);

            Assert.That(FdbKey.PrettyPrint(t, FdbKey.PrettyPrintMode.Single), Is.EqualTo("(123,)"));
            Assert.That(FdbKey.PrettyPrint(STuple.ToRange(t).Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(123,).<00>"));
            Assert.That(FdbKey.PrettyPrint(STuple.ToRange(t).End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(123,).<FF>"));
        }
		/// <summary>Delete a document from the collection</summary>
		/// <param name="trans"></param>
		/// <param name="ids"></param>
		public void DeleteMultiple(IFdbTransaction trans, IEnumerable<TId> ids)
		{
			if (trans == null) throw new ArgumentNullException(nameof(trans));
			if (ids == null) throw new ArgumentNullException(nameof(ids));

			foreach (var id in ids)
			{
				var key = this.Subspace.Keys.EncodePartial(id);
				trans.ClearRange(KeyRange.StartsWith(key));
			}
		}
Пример #15
0
        public void TestKeyRangeToStringExclusive()
        {
            var    keyrange = new KeyRange <int>(Key <int> .CreateKey(1, false), Key <int> .CreateKey(2, false));
            string s        = keyrange.ToString();

            Assert.IsNotNull(s);
            Assert.AreNotEqual(s, String.Empty);
            StringAssert.Contains(s, "1");
            StringAssert.Contains(s, "2");
            StringAssert.Contains(s, "exclusive");
        }
        public List <byte[]> GetKeys(byte[] startKey, int count)
        {
            var keyRange = new KeyRange {
                StartKey = startKey ?? new byte[0], EndKey = new byte[0], Count = count
            };
            var aquilesSlicePredicate   = new SlicePredicate(new List <byte[]>());
            var getKeyRangeSliceCommand = new GetKeyRangeSliceCommand(keyspaceName, columnFamilyName, readConsistencyLevel, keyRange, aquilesSlicePredicate);

            commandExecutor.Execute(getKeyRangeSliceCommand);
            return(getKeyRangeSliceCommand.Output);
        }
Пример #17
0
        public void TestKeyRangeToStringInclusive()
        {
            var    keyrange = new KeyRange <int>(Key <int> .CreateKey(3, true), Key <int> .CreateKey(4, true));
            string s        = keyrange.ToString();

            Assert.IsNotNull(s);
            Assert.AreNotEqual(s, String.Empty);
            StringAssert.Contains(s, "3");
            StringAssert.Contains(s, "4");
            StringAssert.Contains(s, "inclusive");
        }
Пример #18
0
        public void TestKeyRangeToStringPrefix()
        {
            var    keyrange = new KeyRange <string>(Key <string> .CreateKey("3", true), Key <string> .CreatePrefixKey("4"));
            string s        = keyrange.ToString();

            Assert.IsNotNull(s);
            Assert.AreNotEqual(s, String.Empty);
            StringAssert.Contains(s, "3");
            StringAssert.Contains(s, "4");
            StringAssert.Contains(s, "prefix");
        }
Пример #19
0
        public IAsyncEnumerable <(TValue Value, long Count)> GetCounts([NotNull] IFdbReadOnlyTransaction trans, TKey key)
        {
            var range = KeyRange.StartsWith(this.Subspace.Keys.EncodePartial(key));

            var query = trans
                        .GetRange(range)
                        .Select(kvp => (Value: this.Subspace.Keys.Decode(kvp.Key).Item2, Count: kvp.Value.ToInt64()));

            return(this.AllowNegativeValues
                                ? query
                                : query.Where(x => x.Count > 0));
        }
Пример #20
0
        public IEnumerable <string> ProduceKey(KeyRange keyRange)
        {
            var key = new List <string>();

            for (int number = keyRange.KeyStart; number <= keyRange.KeyStop; number++)
            {
                var checkResult = CheckNumber(number);
                key.Add(!string.IsNullOrEmpty(checkResult) ? checkResult : number.ToString());
            }

            return(key);
        }
Пример #21
0
 internal static Apache.Cassandra.KeyRange ToCassandraKeyRange(this KeyRange keyRange)
 {
     if (keyRange == null)
     {
         return(null);
     }
     return(new Apache.Cassandra.KeyRange
     {
         Count = keyRange.Count,
         Start_key = keyRange.StartKey ?? new byte[0],
         End_key = keyRange.EndKey ?? new byte[0]
     });
 }
Пример #22
0
 public static IEnumerable <KeyRange> Create(IEnumerable <string> keys, IEnumerable <double> values, IEnumerable <double> mins, IEnumerable <double> maxs)
 {
     using (var b = keys.GetEnumerator())
         using (var c = values.GetEnumerator())
             using (var d = mins?.GetEnumerator())
                 using (var e = maxs?.GetEnumerator())
                 {
                     while (b.MoveNext() && c.MoveNext() && (bool)(d?.MoveNext() ?? true) && (bool)(e?.MoveNext() ?? true))
                     {
                         var ff = new KeyRange(b.Current, c.Current, d?.Current, e?.Current);
                         yield return(ff);
                     }
                 }
 }
Пример #23
0
        /// <summary>
        /// Determine if the given range matches at least the records described
        /// by the min/max values.
        /// </summary>
        /// <param name="keyRange">The key range.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>True if the KeyRange is a superset of the values.</returns>
        private static bool KeyRangeIsSufficient(KeyRange <int> keyRange, int min, int max)
        {
            bool minIsSufficient =
                (null == keyRange.Min) ||
                (keyRange.Min.Value == min && keyRange.Min.IsInclusive) ||
                keyRange.Min.Value < min;

            bool maxIsSufficient =
                (null == keyRange.Max) ||
                (keyRange.Max.Value == max && keyRange.Max.IsInclusive) ||
                keyRange.Max.Value > max;

            return(minIsSufficient && maxIsSufficient);
        }
Пример #24
0
        public ConvertTransform(IHostEnvironment env, Arguments args, IDataView input)
            : base(env, RegistrationName, env.CheckRef(args, nameof(args)).Column,
                   input, null)
        {
            Host.AssertNonEmpty(Infos);
            Host.Assert(Infos.Length == Utils.Size(args.Column));

            _exes = new ColInfoEx[Infos.Length];
            for (int i = 0; i < _exes.Length; i++)
            {
                DataKind kind;
                KeyRange range;
                var      col = args.Column[i];
                if (col.ResultType != null)
                {
                    kind  = col.ResultType.Value;
                    range = !string.IsNullOrEmpty(col.Range) ? KeyRange.Parse(col.Range) : col.KeyRange;
                }
                else if (col.KeyRange != null || !string.IsNullOrEmpty(col.Range))
                {
                    kind  = Infos[i].TypeSrc.IsKey ? Infos[i].TypeSrc.RawKind : DataKind.U4;
                    range = col.KeyRange ?? KeyRange.Parse(col.Range);
                }
                else if (args.ResultType != null)
                {
                    kind  = args.ResultType.Value;
                    range = !string.IsNullOrEmpty(args.Range) ? KeyRange.Parse(args.Range) : args.KeyRange;
                }
                else if (args.KeyRange != null || !string.IsNullOrEmpty(args.Range))
                {
                    kind  = Infos[i].TypeSrc.IsKey ? Infos[i].TypeSrc.RawKind : DataKind.U4;
                    range = args.KeyRange ?? KeyRange.Parse(args.Range);
                }
                else
                {
                    kind  = DataKind.Num;
                    range = null;
                }
                Host.CheckUserArg(Enum.IsDefined(typeof(DataKind), kind), nameof(args.ResultType));

                PrimitiveType itemType;
                if (!TryCreateEx(Host, Infos[i], kind, range, out itemType, out _exes[i]))
                {
                    throw Host.ExceptUserArg(nameof(args.Column),
                                             "source column '{0}' with item type '{1}' is not compatible with destination type '{2}'",
                                             input.Schema.GetColumnName(Infos[i].Source), Infos[i].TypeSrc.ItemType, itemType);
                }
            }
            SetMetadata();
        }
Пример #25
0
        /// <summary>Remove all fields of an hashset</summary>
        /// <param name="id"></param>
        public void Delete(IFdbTransaction trans, IVarTuple id)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            // remove all fields of the hash
            trans.ClearRange(KeyRange.StartsWith(GetKey(id)));
        }
Пример #26
0
        /// <summary>
        /// Run a test with one randomly generated expression tree.
        /// </summary>
        /// <param name="rangeWasEmpty">True if an empty range was generated.</param>
        /// <param name="rangeWasExact">
        /// True if the generated range matched the given maximums and minimums exactly.
        /// </param>
        private void DoOneTest(out bool rangeWasEmpty, out bool rangeWasExact)
        {
            // Unfortunately this generates a Func<KeyValuePair<int, int>, bool> instead
            // of a Predicate<KeyValuePair<int, int>. We work around that by calling
            // PredicateExpressionEvaluator directly.
            Expression <Func <KeyValuePair <int, int>, bool> > expression = this.CreateExpression();
            Func <KeyValuePair <int, int>, bool> func = expression.Compile();

            // This is the test Oracle: we create the KeyValuePairs and see which ones
            // are matched by the expression.
            int min   = MinValue;
            int max   = MaxValue;
            int count = 0;

            for (int i = MinValue; i < MaxValue; ++i)
            {
                KeyValuePair <int, int> kvp = new KeyValuePair <int, int>(i, i);
                if (func(kvp))
                {
                    if (count++ == 0)
                    {
                        min = kvp.Key;
                    }

                    max = kvp.Key;
                }
            }

            KeyRange <int> keyRange = PredicateExpressionEvaluator <int> .GetKeyRange(expression.Body, keyMemberInfo);

            if (count > 0)
            {
                Assert.IsTrue(
                    KeyRangeIsSufficient(keyRange, min, max),
                    "KeyRange is too small. Expression: {0}, Min = {1}, Max = {2}, Got {3}",
                    expression,
                    min,
                    max,
                    keyRange);

                rangeWasExact = KeyRangeIsExact(keyRange, min, max);
                rangeWasEmpty = false;
            }
            else
            {
                rangeWasExact = keyRange.IsEmpty;
                rangeWasEmpty = true;
            }
        }
        /// <summary>Insert a new document in the collection</summary>
        public void Insert(IFdbTransaction trans, TDocument document)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var id = this.IdSelector(document);

            if (id == null)
            {
                throw new InvalidOperationException("Cannot insert a document with a null identifier");
            }

            // encode the document
            var packed = this.ValueEncoder.EncodeValue(document);

            // Key Prefix = ...(id,)
            var key = this.Location.Partial.Keys.Encode(id);

            // clear previous value
            trans.ClearRange(KeyRange.StartsWith(key));

            int remaining = packed.Count;

            if (remaining <= this.ChunkSize)
            {             // stored as a single element
                // Key = ...(id,)
                trans.Set(key, packed);
            }
            else
            {             // splits in as many chunks as necessary
                // Key = ...(id, N) where N is the chunk index (0-based)
                int p     = 0;
                int index = 0;
                while (remaining > 0)
                {
                    int sz = Math.Max(remaining, this.ChunkSize);
                    trans.Set(this.Location.Keys.Encode(id, index), packed.Substring(p, sz));
                    ++index;
                    p         += sz;
                    remaining -= sz;
                }
            }
        }
        /// <summary>Delete a document from the collection</summary>
        /// <param name="trans"></param>
        /// <param name="id"></param>
        public void Delete(IFdbTransaction trans, TId id)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var key = this.Location.Partial.Keys.Encode(id);

            trans.ClearRange(KeyRange.StartsWith(key));
        }
Пример #29
0
        /// <summary>
        /// Optimized insert of presorted key/value pairs.
        /// If the input is not presorted, please use AddRange() instead.
        /// </summary>
        /// <param name="items">The ordered list of items to insert</param>
        /// <param name="allowUpdates">True to overwrite any existing records</param>
        /// <returns>The total number of records inserted or updated</returns>
        public int AddRangeSorted(IEnumerable <KeyValuePair <TKey, TValue> > items, bool allowUpdates)
        {
            int result = 0;

            using (AddRangeInfo bulk = new AddRangeInfo(allowUpdates, items))
            {
                while (!bulk.IsComplete)
                {
                    KeyRange range = new KeyRange(_keyComparer);
                    using (RootLock root = LockRoot(LockType.Insert, "BulkInsert"))
                        result += AddRange(root.Pin, ref range, bulk, null, int.MinValue);
                }
            }
            DebugComplete("AddRange({0} records)", result);
            return(result);
        }
        /// <summary>Delete a document from the collection</summary>
        /// <param name="trans"></param>
        /// <param name="ids"></param>
        public void DeleteMultiple(IFdbTransaction trans, IEnumerable <TId> ids)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (ids == null)
            {
                throw new ArgumentNullException(nameof(ids));
            }

            foreach (var key in this.Location.Partial.Keys.Encode(ids))
            {
                trans.ClearRange(KeyRange.StartsWith(key));
            }
        }
Пример #31
0
        private ConvertTransform(IHost host, ModelLoadContext ctx, IDataView input)
            : base(host, ctx, input, null)
        {
            Host.AssertValue(ctx);

            // *** Binary format ***
            // <prefix handled in static Create method>
            // <base>
            // for each added column
            //   byte: data kind, with high bit set if there is a key range
            //   if there is a key range
            //     ulong: min
            //     int: count (0 for unspecified)
            //     byte: contiguous

            Host.AssertNonEmpty(Infos);
            _exes = new ColInfoEx[Infos.Length];
            for (int i = 0; i < _exes.Length; i++)
            {
                byte b    = ctx.Reader.ReadByte();
                var  kind = (DataKind)(b & 0x7F);
                Host.CheckDecode(Enum.IsDefined(typeof(DataKind), kind));
                KeyRange range = null;
                if ((b & 0x80) != 0)
                {
                    range     = new KeyRange();
                    range.Min = ctx.Reader.ReadUInt64();
                    int count = ctx.Reader.ReadInt32();
                    if (count != 0)
                    {
                        if (count < 0 || (ulong)(count - 1) > ulong.MaxValue - range.Min)
                        {
                            throw Host.ExceptDecode();
                        }
                        range.Max = range.Min + (ulong)(count - 1);
                    }
                    range.Contiguous = ctx.Reader.ReadBoolByte();
                }

                PrimitiveType itemType;
                if (!TryCreateEx(Host, Infos[i], kind, range, out itemType, out _exes[i]))
                {
                    throw Host.ExceptParam(nameof(input), "source column '{0}' is not of compatible type", input.Schema.GetColumnName(Infos[i].Source));
                }
            }
            SetMetadata();
        }
 public void TestSample12()
 {
     Expression<Predicate<int>> expression = x => x >= -1 && x < 101;
     KeyRange<int> actual = PredicateExpressionEvaluator<int>.GetKeyRange(expression.Body, null);
     KeyRange<int> expected = new KeyRange<int>(Key<int>.CreateKey(-1, true), Key<int>.CreateKey(101, false));
     Assert.AreEqual(expected, actual);
 }
 public void TestStringStartsWithIntersection()
 {
     string s = "baz";
     KeyRange<string> actual = KeyValueExpressionEvaluator<string, string>.GetKeyRange(x => x.Key.StartsWith(s) && x.Key.CompareTo("z") < 0);
     var expected = new KeyRange<string>(Key<string>.CreateKey(s, true), Key<string>.CreatePrefixKey(s));
     Assert.AreEqual(expected, actual);
 }
 public void TestNullDateTimeExpression()
 {
     DateTime? min = null;
     Func<DateTime?> fmax = () => DateTime.MaxValue;
     var expected = new KeyRange<DateTime>(null, Key<DateTime>.CreateKey(DateTime.MaxValue, false));
     var actual = KeyValueExpressionEvaluator<DateTime, string>.GetKeyRange(x => min <= x.Key && x.Key < fmax());
     Assert.AreEqual(expected, actual);
 }
 public void VerifyDeMorgansForAnd()
 {
     // This should be the same as (x.Key > 11 || x.Key > 22)
     KeyRange<int> actual = KeyValueExpressionEvaluator<int, string>.GetKeyRange(x => !(x.Key <= 11 && x.Key <= 22));
     KeyRange<int> expected = new KeyRange<int>(Key<int>.CreateKey(11, false), null);
     Assert.AreEqual(expected, actual);
 }
 public void VerifyNotOfGeGivesLt()
 {
     KeyRange<int> actual = KeyValueExpressionEvaluator<int, string>.GetKeyRange(x => !(x.Key >= 4));
     KeyRange<int> expected = new KeyRange<int>(null, Key<int>.CreateKey(4, false));
 }
 public void TestNullableGuidExpression()
 {
     Func<Guid?> f = () => Guid.Empty;
     var expected = new KeyRange<Guid>(Key<Guid>.CreateKey(Guid.Empty, true), Key<Guid>.CreateKey(Guid.Empty, true));
     var actual = KeyValueExpressionEvaluator<Guid, string>.GetKeyRange(x => x.Key == f());
     Assert.AreEqual(expected, actual);
 }
        /// <summary>
        /// Determine if the given range matches at least the records described
        /// by the min/max values.
        /// </summary>
        /// <param name="keyRange">The key range.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>True if the KeyRange is a superset of the values.</returns>
        private static bool KeyRangeIsSufficient(KeyRange<int> keyRange, int min, int max)
        {
            bool minIsSufficient =
                (null == keyRange.Min)
                || (keyRange.Min.Value == min && keyRange.Min.IsInclusive)
                || keyRange.Min.Value < min;

            bool maxIsSufficient =
                (null == keyRange.Max)
                || (keyRange.Max.Value == max && keyRange.Max.IsInclusive)
                || keyRange.Max.Value > max;

            return minIsSufficient && maxIsSufficient;
        }
 public void TestNullableUShortExpression()
 {
     ushort? min = 1;
     Func<ushort?> fmax = () => 8;
     var expected = new KeyRange<ushort>(Key<ushort>.CreateKey(1, true), Key<ushort>.CreateKey(8, false));
     var actual = KeyValueExpressionEvaluator<ushort, string>.GetKeyRange(x => min <= x.Key && x.Key < fmax());
     Assert.AreEqual(expected, actual, "ushort to int? promotion not supported");
 }
 public void TestNullableDoubleExpression()
 {
     double? min = 1;
     Func<double?> fmax = () => 8;
     var expected = new KeyRange<double>(Key<double>.CreateKey(1, true), Key<double>.CreateKey(8, false));
     var actual = KeyValueExpressionEvaluator<double, string>.GetKeyRange(x => min <= x.Key && x.Key < fmax());
     Assert.AreEqual(expected, actual);
 }
 public void TestSample11()
 {
     // Regression test for:
     //   Assert.IsTrue failed.
     //   Error at entry 0. Not enough entries in actual.
     //   First missing entry is [ggi, ggi] 
     //    expression = x => ((Compare("ggi", x.Key) <= 0) && (x.Key.Equals("f*g") || x.Key.StartsWith("g")))
     KeyRange<string> actual =
         KeyValueExpressionEvaluator<string, string>.GetKeyRange(
             x => ((String.Compare("ggi", x.Key) <= 0) && (x.Key.Equals("f*g") || x.Key.StartsWith("g"))));
     KeyRange<string> expected = new KeyRange<string>(
         Key<string>.CreateKey("ggi", true),
         Key<string>.CreatePrefixKey("g"));
     Assert.AreEqual(expected, actual);
     Assert.IsFalse(actual.IsEmpty);
 }
 public void TestSample10()
 {
     // Regression test for:
     //   Assert.IsTrue failed.
     //   Error at entry 21. Not enough entries in actual.
     //   First missing entry is [ibh, ibh]
     //    expression = x => (x.Key.StartsWith("i") || (x.Key.Equals("ibg") || ("f" = x.Key)))
     KeyRange<string> actual =
         KeyValueExpressionEvaluator<string, string>.GetKeyRange(
             x => (x.Key.StartsWith("i") || (x.Key.Equals("ibg") || ("f" == x.Key))));
     KeyRange<string> expected = new KeyRange<string>(Key<string>.CreateKey("f", true), Key<string>.CreatePrefixKey("i"));
     Assert.AreEqual(expected, actual);
 }
 public void TestSample9()
 {
     // Regression test for:
     //   KeyRange is too small.
     //   Expression:
     //     x => Not(((2137 >= x.Key) && Not((-3611 = x.Key))))
     KeyRange<int> actual =
         KeyValueExpressionEvaluator<int, int>.GetKeyRange(
             x => !((2137 >= x.Key) && !(-3611 == x.Key)));
     KeyRange<int> expected = new KeyRange<int>(Key<int>.CreateKey(-3611, true), null);
     Assert.AreEqual(expected, actual);
 }
        public void TestSample5()
        {
            DateTime? date = DateTime.UtcNow;
            TimeSpan? timespan = TimeSpan.FromDays(90);

            KeyRange<DateTime> actual = KeyValueExpressionEvaluator<DateTime, string>.GetKeyRange(d => d.Key >= date && d.Key <= date + timespan);
            KeyRange<DateTime> expected = new KeyRange<DateTime>(Key<DateTime>.CreateKey(date.Value, true), Key<DateTime>.CreateKey(date.Value + timespan.Value, true));
            Assert.AreEqual(expected, actual);
        }
 public void TestEquals()
 {
     var expected = new KeyRange<long>(Key<long>.CreateKey(10, true), Key<long>.CreateKey(10, true));
     var actual = KeyValueExpressionEvaluator<long, string>.GetKeyRange(x => x.Key.Equals(10));
     Assert.AreEqual(expected, actual);
 }
 public void TestIntNullableLongExpression()
 {
     long? min = 1;
     Func<long?> fmax = () => 8;
     var expected = new KeyRange<int>(Key<int>.CreateKey(1, true), Key<int>.CreateKey(8, false));
     var actual = KeyValueExpressionEvaluator<int, string>.GetKeyRange(x => min <= x.Key && x.Key < fmax());
     Assert.AreEqual(expected, actual);
 }
 public void TestSample13()
 {
     string s = "foo";
     Expression<Predicate<int>> expression = x => x > -1 && s.Length < 101;
     KeyRange<int> actual = PredicateExpressionEvaluator<int>.GetKeyRange(expression.Body, null);
     KeyRange<int> expected = new KeyRange<int>(Key<int>.CreateKey(-1, false), null);
     Assert.AreEqual(expected, actual);
 }
 /// <summary>
 /// Common test for constant folding tests.
 /// </summary>
 /// <param name="expression">
 /// An expression which should come out to x.Key LE 32
 /// </param>
 private static void ConstantFoldingHelper(Expression<Predicate<KeyValuePair<int, long>>> expression)
 {
     KeyRange<int> actual = KeyValueExpressionEvaluator<int, long>.GetKeyRange(expression);
     KeyRange<int> expected = new KeyRange<int>(null, Key<int>.CreateKey(32, true));
     Assert.AreEqual(expected, actual);
 }
 public void VerifyStaticMethodCallAccessIsOptimized()
 {
     var expected = new KeyRange<int>(null, Key<int>.CreateKey(8, false));
     var actual =
         KeyValueExpressionEvaluator<int, string>.GetKeyRange(
             x => x.Key < Math.Min(8, 9));
     Assert.AreEqual(expected, actual);
 }
 public void TestNullIntExpression()
 {
     int? min = 1;
     Func<int?> fmax = () => null;
     var expected = new KeyRange<int>(Key<int>.CreateKey(1, true), null);
     var actual = KeyValueExpressionEvaluator<int, string>.GetKeyRange(x => min <= x.Key && x.Key < fmax());
     Assert.AreEqual(expected, actual);
 }
 public void VerifyComparisonWithComplexNullable()
 {
     int? min = -99;
     int? max = 1;
     KeyRange<int> actual = KeyValueExpressionEvaluator<int, string>.GetKeyRange(x => min < x.Key && x.Key < max + 8);
     KeyRange<int> expected = new KeyRange<int>(Key<int>.CreateKey(-99, false), Key<int>.CreateKey(9, false));
     Assert.AreEqual(expected, actual);
 }
 public void TestNullableBoolExpression()
 {
     Func<bool?> f = () => false;
     var expected = new KeyRange<bool>(Key<bool>.CreateKey(false, true), Key<bool>.CreateKey(false, true));
     var actual = KeyValueExpressionEvaluator<bool, string>.GetKeyRange(x => x.Key == f());
     Assert.AreEqual(expected, actual);
 }
 public void VerifyNotOfNe()
 {
     KeyRange<int> actual = KeyValueExpressionEvaluator<int, string>.GetKeyRange(x => !(x.Key != 3));
     KeyRange<int> expected = new KeyRange<int>(Key<int>.CreateKey(3, true), Key<int>.CreateKey(3, true));
     Assert.AreEqual(expected, actual);
 }
 public void TestStringStartsWithUnion()
 {
     KeyRange<string> actual =
         KeyValueExpressionEvaluator<string, string>.GetKeyRange(
             x => x.Key.StartsWith("b") || (x.Key.CompareTo("a") > 0 && x.Key.CompareTo("b") <= 0));
     var expected = new KeyRange<string>(Key<string>.CreateKey("a", false), Key<string>.CreatePrefixKey("b"));
     Assert.AreEqual(expected, actual);
 }
 public void TestNullableTimeSpanExpression()
 {
     TimeSpan? min = TimeSpan.MinValue;
     Func<TimeSpan?> fmax = () => TimeSpan.MaxValue;
     var expected = new KeyRange<TimeSpan>(Key<TimeSpan>.CreateKey(TimeSpan.MinValue, true), Key<TimeSpan>.CreateKey(TimeSpan.MaxValue, false));
     var actual = KeyValueExpressionEvaluator<TimeSpan, string>.GetKeyRange(x => min <= x.Key && x.Key < fmax());
     Assert.AreEqual(expected, actual);
 }
        /// <summary>
        /// Determine if the given range matches the given min/max values exactly.
        /// </summary>
        /// <param name="keyRange">The key range.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>True if the KeyRange is an exact match.</returns>
        private static bool KeyRangeIsExact(KeyRange<int> keyRange, int min, int max)
        {
            bool minIsCorrect =
                (null != keyRange.Min)
                && ((keyRange.Min.Value == min && keyRange.Min.IsInclusive)
                    || (keyRange.Min.Value == min - 1 && !keyRange.Min.IsInclusive));

            bool maxIsCorrect =
                (null != keyRange.Max)
                && ((keyRange.Max.Value == max && keyRange.Max.IsInclusive)
                    || (keyRange.Max.Value == max + 1 && !keyRange.Max.IsInclusive));

            if (!minIsCorrect && MinValue == min && null == keyRange.Min)
            {
                // No min value is correct if we start at the minimum value
                minIsCorrect = true;
            }

            if (!maxIsCorrect && MaxValue - 1 == max && null == keyRange.Max)
            {
                // No max value is correct if we end at the maximum value
                maxIsCorrect = true;
            }

            return minIsCorrect && maxIsCorrect;
        }
 public void TestStringStaticEqualsReversed()
 {
     KeyRange<string> actual = KeyValueExpressionEvaluator<string, string>.GetKeyRange(x => String.Equals("baz", x.Key));
     var expected = new KeyRange<string>(Key<string>.CreateKey("baz", true), Key<string>.CreateKey("baz", true));
     Assert.AreEqual(expected, actual);
 }