Example #1
0
        private IEnumerator <KeyValuePair <AttributeValue, long> > GetValueEnumerator(AttributeValue value)
        {
            IDictionary <long, byte> outRowIds, rowIds;

            if (_tree.TryGetValue(value, out outRowIds))
            {
                rowIds = new HashVector <long, byte>(outRowIds);
            }
            else
            {
                rowIds = new HashVector <long, byte>();
            }

            IDictionary <IndexOp <long>, byte> ops;

            if (_transitionTree.TryGetValue(value, out ops))
            {
                foreach (var indexOp in ops.Keys)
                {
                    indexOp.MergeWith(rowIds);
                }
            }

            foreach (var rowId in rowIds)
            {
                yield return(new KeyValuePair <AttributeValue, long>(value, rowId.Key));
            }
        }
Example #2
0
        public void Rename(string source, string dest)
        {
            ContentRecord rec;

            Check.Assert <ArgumentException>(_index.TryGetValue(source, out rec), "The source was not found.");
            rec = rec.ToBuilder().SetContentUri(dest).Build();
            Check.Assert <ArgumentException>(_index.Add(dest, rec), "The target already exists.");
            _index.Remove(source);
        }
Example #3
0
        protected override bool RequireGetPeersRegisteredInfo(TransactionId msgId, DhtNode node, out byte[] infoHash)
        {
            var nodeId = (ulong)node.CompactEndPoint().ToInt64();
            var path   = nodeId << 16 | (uint)(msgId[0] << 8) | msgId[1];

            return(_treeStore.TryGetValue(path, out infoHash));
        }
Example #4
0
        private static void VerifyDictionary(Dictionary <int, string> expected, BPlusTree <int, string> tree)
        {
            tree.Validate();
            tree.EnableCount();

            Dictionary <int, string>           test  = new Dictionary <int, string>(expected);
            List <KeyValuePair <int, string> > pairs = new List <KeyValuePair <int, string> >(test);

            string val;

            foreach (KeyValuePair <int, string> pair in tree)
            {
                Assert.IsTrue(test.TryGetValue(pair.Key, out val));
                Assert.AreEqual(pair.Value, val);
                Assert.IsTrue(test.Remove(pair.Key));
            }
            Assert.AreEqual(0, test.Count);
            test = null;
            Assert.IsNull(test);
            Assert.AreEqual(pairs.Count, tree.Count);

            foreach (KeyValuePair <int, string> pair in pairs)
            {
                Assert.IsTrue(tree.TryGetValue(pair.Key, out val));
                Assert.AreEqual(pair.Value, val);
            }
        }
Example #5
0
 public void AddDataTree(string key, string fName, BPlusTree <string, String> .OptionsV2 treeData)
 {
     using (var tree = new BPlusTree <string, String>(treeData))
     {
         tree.Add(key, fName);
         tree.TryGetValue(key, out string cmpDate);
         var xmlNAME = new XMLRead();
         xmlNAME.ReadXmlData(cmpDate);
     }
 }
Example #6
0
        /// <summary>
        /// Gets the first by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public T GetFirstById(object id)
        {
            if (!(id is Guid))
            {
                LogError("Attempted to get first by id, but id wasn't the expected type ({0})", typeof(Guid));
                return(default(T));
            }
            T val;

            return(!tree.TryGetValue((Guid)id, out val) ? default(T) : val);
        }
Example #7
0
 private IEnumerable <DocumentPosting> GetPostings(Term term)
 {
     DocumentPosting[] postings;
     if (_postingDb.TryGetValue(term, out postings))
     {
         foreach (var posting in postings)
         {
             yield return(posting);
         }
     }
 }
        public override void Put(byte[] key, byte[] value)
        {
            try {
                Console.WriteLine("{0}:", name);
                Console.Write("{0}:", key.ToHexadecimal());
                try {
                    Console.WriteLine("{0}:", System.Text.Encoding.Unicode.GetString(value));
                } catch {
                    Console.WriteLine("{0}", value.ToHexadecimal());
                }
            } catch {
                Console.WriteLine("XXX");
            }

            long OO;

            lock (index)
                if (index.TryGetValue(key, out OO))
                {
                    //TODO:resize object if we can
                    Delete(key);
                    Put(key, value);
                }
                else
                {
                    ObjectHeader ObjectHeader = new ObjectHeader();
                    ObjectHeader.Deleted = false;
                    ObjectHeader.Length  = value.Length;
                    ObjectHeader.Key     = key;
                    int       ObjectSize       = value.Length;
                    const int ObjectHeaderSize = 40;
                    OO              = mh.WriteOffset;
                    mh.WriteOffset += ObjectSize + ObjectHeaderSize;
                    EnsureCapacity(mh.WriteOffset);
                    MemoryMappedViewStream accessor = mmfBlock.CreateViewStream(reservedBytes + OO, ObjectSize + ObjectHeaderSize, MemoryMappedFileAccess.Write);
                    byte[] ObjectHeaderBytes        = new byte[ObjectHeaderSize];
                    System.IO.BinaryWriter bw       = new BinaryWriter(new MemoryStream(ObjectHeaderBytes, true));
                    bw.Write(ObjectHeader.Deleted);
                    bw.BaseStream.Seek(3, SeekOrigin.Current);
                    bw.Write(ObjectHeader.Length);
                    bw.Write(ObjectHeader.Key);
                    accessor.Write(ObjectHeaderBytes, 0, ObjectHeaderSize);
                    accessor.Write(value, 0, value.Length);
                    accessor.Flush();
                    accessor.Dispose();
                    maMeta.Write(0, ref mh);
                    maMeta.Flush();
                    index.Add(key, OO);
                    index.Commit();
                }
        }
Example #9
0
 void FetchStuff(BPlusTree <Guid, TestInfo> tree)
 {
     while (!mreStop.WaitOne(0, false))
     {
         foreach (Guid k in tree.Keys)
         {
             TestInfo ti;
             if (tree.TryGetValue(k, out ti) && ti.MyKey != k)
             {
                 throw new ApplicationException();
             }
         }
     }
 }
Example #10
0
        private static void FlushToDisk(BPlusTree <string, int> map, Dictionary <string, int> dict)
        {
            int takenCount = 0;

            while (takenCount < dict.Count)
            {
                int count = 0;

                var keysToUpdate = new List <Tuple <string, int> >();

                // Only update in blocks of 10000 because we don't want to
                // Pop our max memory when calculating diffs.
                int amountToTake = 10000;

                // We might not be able to take exactly 10000.
                if (dict.Count - count < 10000)
                {
                    amountToTake = dict.Count - count;
                    count        = 0;
                }

                // Create list of diffs
                foreach (var kvp in dict.Skip(takenCount).Take(amountToTake))
                {
                    int value;
                    if (map.TryGetValue(kvp.Key, out value))
                    {
                        keysToUpdate.Add(new Tuple <string, int>(kvp.Key, kvp.Value + value));
                    }
                }

                // Apply diffs (can't modify dictionary during enumeration)
                foreach (var update in keysToUpdate)
                {
                    dict[update.Item1] = update.Item2;
                }

                count      += amountToTake;
                takenCount += amountToTake;
            }

            BulkInsertOptions bio = new BulkInsertOptions();

            //bio.ReplaceContents = true;
            bio.DuplicateHandling = DuplicateHandling.LastValueWins;

            // BPlusTree is optimized to insert in batches.
            map.BulkInsert(dict, bio);
        }
Example #11
0
        public Stream Get(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException();
            }

            Data data;

            if (!index.TryGetValue(key, out data))
            {
                throw new KeyNotFoundException();
            }

            var readStream = new WindowStream(
                new FileStream(storageFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, writeBufferSize, FileOptions.SequentialScan),
                data.Position, data.Length);

            return(readStream);
        }
        void SequencedTest(int start, int incr, int stop, string name)
        {
            int          count = Math.Abs(start - stop) / Math.Abs(incr);
            const string myTestValue1 = "T1", myTestValue2 = "t2";
            string       test;

            using (BPlusTree <int, string> data = new BPlusTree <int, string>(Options))
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                //large order-forward
                for (int i = start; i != stop; i += incr)
                {
                    if (!data.TryAdd(i, myTestValue1))
                    {
                        throw new ApplicationException();
                    }
                }

                Trace.TraceInformation("{0} insert  {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                {
                    if (!data.TryGetValue(i, out test) || test != myTestValue1)
                    {
                        throw new ApplicationException();
                    }
                }

                Trace.TraceInformation("{0} seek    {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                {
                    if (!data.TryUpdate(i, myTestValue2))
                    {
                        throw new ApplicationException();
                    }
                }

                Trace.TraceInformation("{0} modify  {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                {
                    if (!data.TryGetValue(i, out test) || test != myTestValue2)
                    {
                        throw new ApplicationException();
                    }
                }

                Trace.TraceInformation("{0} seek#2  {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                int tmpCount = 0;
                foreach (KeyValuePair <int, string> tmp in data)
                {
                    if (tmp.Value != myTestValue2)
                    {
                        throw new ApplicationException();
                    }
                    else
                    {
                        tmpCount++;
                    }
                }
                if (tmpCount != count)
                {
                    throw new ApplicationException();
                }

                Trace.TraceInformation("{0} foreach {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                {
                    if (!data.Remove(i))
                    {
                        throw new ApplicationException();
                    }
                }

                Trace.TraceInformation("{0} delete  {1} in {2}", name, count, time.ElapsedMilliseconds);

                for (int i = start; i != stop; i += incr)
                {
                    if (data.TryGetValue(i, out test))
                    {
                        throw new ApplicationException();
                    }
                }
            }
        }
 public void BPlusTreeDemo()
 {
     var options = new BPlusTree<string, DateTime>.OptionsV2(PrimitiveSerializer.String, PrimitiveSerializer.DateTime);
     options.CalcBTreeOrder(16, 24);
     options.CreateFile = CreatePolicy.Always;
     options.FileName = Path.GetTempFileName();
     using (var tree = new BPlusTree<string, DateTime>(options))
     {
         var tempDir = new DirectoryInfo(Path.GetTempPath());
         foreach (var file in tempDir.GetFiles("*", SearchOption.AllDirectories))
         {
             tree.Add(file.FullName, file.LastWriteTimeUtc);
         }
     }
     options.CreateFile = CreatePolicy.Never;
     using (var tree = new BPlusTree<string, DateTime>(options))
     {
         var tempDir = new DirectoryInfo(Path.GetTempPath());
         foreach (var file in tempDir.GetFiles("*", SearchOption.AllDirectories))
         {
             DateTime cmpDate;
             if (!tree.TryGetValue(file.FullName, out cmpDate))
                 Console.WriteLine("New file: {0}", file.FullName);
             else if (cmpDate != file.LastWriteTimeUtc)
                 Console.WriteLine("Modified: {0}", file.FullName);
             tree.Remove(file.FullName);
         }
         foreach (var item in tree)
         {
             Console.WriteLine("Removed: {0}", item.Key);
         }
     }
 }
 void FetchStuff(BPlusTree<Guid, TestInfo> tree)
 {
     while (!mreStop.WaitOne(0, false))
     {
         foreach (Guid k in tree.Keys)
         {
             TestInfo ti;
             if (tree.TryGetValue(k, out ti) && ti.MyKey != k)
                 throw new ApplicationException();
         }
     }
 }
        private static void VerifyDictionary(Dictionary<int, string> expected, BPlusTree<int, string> tree)
        {
            tree.Validate();
            tree.EnableCount();

            Dictionary<int, string> test = new Dictionary<int, string>(expected);
            List<KeyValuePair<int, string>> pairs = new List<KeyValuePair<int, string>>(test);

            string val;
            foreach (KeyValuePair<int, string> pair in tree)
            {
                Assert.IsTrue(test.TryGetValue(pair.Key, out val));
                Assert.AreEqual(pair.Value, val);
                Assert.IsTrue(test.Remove(pair.Key));
            }
            Assert.AreEqual(0, test.Count);
            test = null;
            Assert.IsNull(test);
            Assert.AreEqual(pairs.Count, tree.Count);

            foreach (KeyValuePair<int, string> pair in pairs)
            {
                Assert.IsTrue(tree.TryGetValue(pair.Key, out val));
                Assert.AreEqual(pair.Value, val);
            }
        }
Example #16
0
 internal int GetFileId(string fullPath)
 {
     fullPath = fullPath.Trim();
     filePathToIdMap.TryGetValue(fullPath, out var fileId);
     return(fileId);
 }
        void SequencedTest(int start, int incr, int stop, string name)
        {
            int count = Math.Abs(start - stop)/Math.Abs(incr);
            const string myTestValue1 = "T1", myTestValue2 = "t2";
            string test;

            using (BPlusTree<int, string> data = new BPlusTree<int, string>(Options))
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                //large order-forward
                for (int i = start; i != stop; i += incr)
                    if (!data.TryAdd(i, myTestValue1)) throw new ApplicationException();

                Trace.TraceInformation("{0} insert  {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                    if (!data.TryGetValue(i, out test) || test != myTestValue1) throw new ApplicationException();

                Trace.TraceInformation("{0} seek    {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                    if (!data.TryUpdate(i, myTestValue2)) throw new ApplicationException();

                Trace.TraceInformation("{0} modify  {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                    if (!data.TryGetValue(i, out test) || test != myTestValue2) throw new ApplicationException();

                Trace.TraceInformation("{0} seek#2  {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                int tmpCount = 0;
                foreach (KeyValuePair<int, string> tmp in data)
                    if (tmp.Value != myTestValue2) throw new ApplicationException();
                    else tmpCount++;
                if (tmpCount != count) throw new ApplicationException();

                Trace.TraceInformation("{0} foreach {1} in {2}", name, count, time.ElapsedMilliseconds);
                time.Reset();
                time.Start();

                for (int i = start; i != stop; i += incr)
                    if (!data.Remove(i)) throw new ApplicationException();

                Trace.TraceInformation("{0} delete  {1} in {2}", name, count, time.ElapsedMilliseconds);

                for (int i = start; i != stop; i += incr)
                    if (data.TryGetValue(i, out test)) throw new ApplicationException();
            }
        }
Example #18
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");
                }
            }
        }
        public void ExplicitRangeAddRemove()
        {
            string test;

            using (BPlusTree <int, string> data = Create(Options))
            {
                data.Add(2, "v2");
                data.Add(1, "v1");

                int i = 0;
                for (; i < 8; i++)
                {
                    data.TryAdd(i, "v" + i);
                }
                for (i = 16; i >= 8; i--)
                {
                    data.TryAdd(i, "v" + i);
                }
                data.TryAdd(13, "v" + i);

                for (i = 0; i <= 16; i++)
                {
                    if (!data.TryGetValue(i, out test))
                    {
                        throw new ApplicationException();
                    }
                    Assert.AreEqual("v" + i, test);
                }

                data.Remove(1);
                data.Remove(3);
                IEnumerator <KeyValuePair <int, string> > e = data.GetEnumerator();
                Assert.IsTrue(e.MoveNext());
                Assert.AreEqual(0, e.Current.Key);
                data.Add(1, "v1");
                Assert.IsTrue(e.MoveNext());
                data.Add(3, "v3");
                Assert.IsTrue(e.MoveNext());
                data.Remove(8);
                Assert.IsTrue(e.MoveNext());
                e.Dispose();
                data.Add(8, "v8");

                i = 0;
                foreach (KeyValuePair <int, string> pair in data)
                {
                    Assert.AreEqual(pair.Key, i++);
                }

                for (i = 0; i <= 16; i++)
                {
                    Assert.IsTrue(data.Remove(i) && data.TryAdd(i, "v" + i));
                }

                for (i = 6; i <= 12; i++)
                {
                    Assert.IsTrue(data.Remove(i));
                }

                for (i = 6; i <= 12; i++)
                {
                    Assert.IsFalse(data.TryGetValue(i, out test));
                    Assert.IsNull(test);
                }

                for (i = 0; i <= 5; i++)
                {
                    Assert.IsTrue(data.TryGetValue(i, out test));
                    Assert.AreEqual("v" + i, test);
                }

                for (i = 13; i <= 16; i++)
                {
                    Assert.IsTrue(data.TryGetValue(i, out test));
                    Assert.AreEqual("v" + i, test);
                }

                for (i = 0; i <= 16; i++)
                {
                    Assert.AreEqual(i < 6 || i > 12, data.Remove(i));
                }
            }
        }
Example #20
0
 internal Dictionary <int, string> GetFilesPathsFromIds(int[] fileIds)
 {
     return(fileIds.Distinct().OrderBy(v => v).ToDictionary(
                id => id, id => { idToFilePathMap.TryGetValue(id, out var path); return path; }
                ));
 }