public void TestRangeEnumerate()
        {
            using (BPlusTree <int, string> data = Create(Options))
            {
                for (int i = 0; i < 100; i++)
                {
                    data.Add(i, i.ToString());
                }

                int ix = 0;
                foreach (KeyValuePair <int, string> kv in data.EnumerateRange(-500, 5000))
                {
                    Assert.AreEqual(ix++, kv.Key);
                }
                Assert.AreEqual(100, ix);

                foreach (KeyValuePair <int, int> range in new Dictionary <int, int> {
                    { 6, 25 }, { 7, 25 }, { 8, 25 }, { 9, 25 }, { 22, 25 }, { 28, 28 }
                })
                {
                    ix = range.Key;
                    foreach (KeyValuePair <int, string> kv in data.EnumerateRange(ix, range.Value))
                    {
                        Assert.AreEqual(ix++, kv.Key);
                    }
                    Assert.AreEqual(range.Value, ix - 1);
                }
            }
        }
Ejemplo n.º 2
0
 public virtual IEnumerator <KeyValuePair <AttributeValue, long> > GetEnumerator(FieldDataType type)
 {
     try
     {
         return
             (MergeEnumerators(
                  _tree.EnumerateRange(new BoundaryValueMask(type, Bound.Min),
                                       new BoundaryValueMask(type, Bound.Max))
                  .GetEnumerator(), _transitionTree.EnumerateRange(new BoundaryValueMask(type, Bound.Min),
                                                                   new BoundaryValueMask(type, Bound.Max)).GetEnumerator()));
     }
     catch (Exception ex)
     {
         LoggerManager.Instance.IndexLogger.Error("BPlusIndex", "Index Enumeration Failure: " + ex.ToString());
         throw;
     }
 }
Ejemplo n.º 3
0
 public override IEnumerable <KeyValuePair <byte[], byte[]> > EnumerateRange(byte[] start, byte[] end)
 {
     lock (index)
         foreach (var t in index.EnumerateRange(start, end))
         {
             yield return(new KeyValuePair <byte[], byte[]> (t.Key, Get(t.Key)));
         }
 }
Ejemplo n.º 4
0
        private static void BasicTest()
        {
            BPlusTree <double, string> .OptionsV2 options =
                new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String);

            options.CalcBTreeOrder(16, 24);
            options.CreateFile = CreatePolicy.Always;
            options.FileName   = System.IO.Path.GetTempFileName();
            using (var tree = new BPlusTree <double, string>(options))
            {
                // Insertion to tree.
                // Note: numbers are NOT inserted sorted.
                tree.Add(30.1, "30.2");
                tree.Add(10.1, "10.2");
                tree.Add(20.1, "20.2");
                tree.Add(80.1, "80.2");
                tree.Add(40.1, "40.2");
                tree.Add(60.1, "60.2");
                tree.Add(70.1, "70.2");
                tree.Add(50.1, "50.2");



                // To get first element.
                // Since sorted, first element is: 10.1
                KeyValuePair <double, string> first_with_Try;
                tree.TryGetFirst(out first_with_Try);

                // Similar to previous function.
                var first = tree.First();



                // To get last element.
                // Since sorted, last element is: 80.1
                KeyValuePair <double, string> last_with_Try;
                tree.TryGetLast(out last_with_Try);

                // Similar to previous function.
                var last = tree.Last();



                // Given key get the value.
                // Key is valid, region.e., it is available in tree.
                // Hence it returns: "50.2"
                string value_of_valid_key;
                tree.TryGetValue(50.1, out value_of_valid_key);


                // Given key get the value.
                // Key is invalid, region.e., it is NOT available in tree.
                // Hence it returns: null (default value of int)
                string value_of_invalid_key;
                tree.TryGetValue(55, out value_of_invalid_key);


                // The "100" key is not available and no key is available greater than
                // that, hence .Current should return default value of key type (0 in this case).
                var key_not_available = tree.EnumerateFrom(100).GetEnumerator().Current;


                // Runtime error
                //var list = tree.ToList();


                // Gets an enumerator.
                IEnumerator <KeyValuePair <double, string> > enumerator = tree.GetEnumerator();


                // Iterating through items with enumerator.
                // starting from first item to last.
                while (enumerator.MoveNext())
                {
                    var item = enumerator.Current;
                }


                // Another syntac of iterations, which is automatically
                // calling "GetEnumerator" function.
                // starting from first item to last.
                foreach (var item in tree)
                {
                }


                // Iterates through items starting from given key: 40.1 (inclusively)
                // and goes till the last item.
                foreach (var item in tree.EnumerateFrom(39.1))
                {
                }



                // Iterates through items starting from given index: 2 (inclusively)
                // and goes till the last item.
                foreach (var item in tree.EnumerateFrom(tree.ElementAtOrDefault(2).Key))
                {
                }



                // Iterate from an item that is NOT available in collection
                // to the item which is neither available.
                foreach (var item in tree.EnumerateRange(20.5, 40.9))
                {
                }


                // Gets the item at specific index.
                // All return valid values, but the last one which is
                // refereing to an index out-of-bound; the return of this
                // call is the default value for key and value.
                var element_at_0   = tree.ElementAtOrDefault(0);
                var element_at_1   = tree.ElementAtOrDefault(1);
                var element_at_2   = tree.ElementAtOrDefault(2);
                var element_at_3   = tree.ElementAtOrDefault(3);
                var element_at_100 = tree.ElementAtOrDefault(100);


                using (BPlusTree <double, string> data = new BPlusTree <double, string>(options))
                {
                    bool sT1 = data.TryAdd(1, "a");
                    bool sF1 = data.TryAdd(1, "a");

                    data[1] = "did it";

                    bool sT2       = data.TryUpdate(1, "a");
                    bool sT3       = data.TryUpdate(1, "c");
                    bool sT4       = data.TryUpdate(1, "d", "c");
                    bool sF2       = data.TryUpdate(1, "f", "c");
                    bool equality1 = "d".Equals(data[1]);
                    bool sT5       = data.TryUpdate(1, "a", data[1]);
                    bool equality2 = "a".Equals(data[1]);
                    bool sF3       = data.TryUpdate(2, "b");

                    string val;
                    bool   st6      = data.TryRemove(1, out val) && val == "a";
                    bool   sF4      = data.TryRemove(2, out val);
                    bool   notEqual = val.Equals("a");
                }
            }
        }