Exemple #1
0
 public static BPlusTree GetStringTree(BPlusTree bpt, ref Stream treefile, ref Stream blockfile, bool isReadOnly)
 {
     if (tempDirectory != null)
     {
         // allocate in filesystem
         string treename  = Path.Combine(tempDirectory, "BPlusTreeString.dat");
         string blockname = Path.Combine(tempDirectory, "BPlusTreeStringBlock.dat");
         treefile  = null;
         blockfile = null;
         if (bpt == null)
         {
             // allocate new
             if (File.Exists(treename))
             {
                 File.Delete(treename);
             }
             if (File.Exists(blockname))
             {
                 File.Delete(blockname);
             }
             bpt = BPlusTree.Create(treename, blockname, keylength, nodesize, buffersize);
         }
         else
         {
             // reopen old
             bpt.Shutdown();
             if (isReadOnly)
             {
                 bpt = BPlusTree.OpenReadOnly(treename, blockname);
             }
             else
             {
                 bpt = BPlusTree.Open(treename, blockname);
             }
         }
     }
     else
     {
         // allocate in memory
         if (bpt == null)
         {
             // allocate new
             treefile  = new MemoryStream();
             blockfile = new MemoryStream();;
             bpt       = BPlusTree.Create(treefile, blockfile, keylength, nodesize, buffersize);
         }
         else
         {
             // reopen
             bpt = BPlusTree.Open(treefile, blockfile);
         }
     }
     return(bpt);
 }
Exemple #2
0
        private void btnTestCreateTree_Click(object sender, EventArgs e)
        {
            string fileName = Toolkit.ResolveFilePath("./test.bpt", true);

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            //using (BPlusTree<BPlusString, BPlusListLong>  tree1 = BPlusTree<BPlusString, BPlusListLong>.Create(fileName + "1", Encoding.UTF8, 3)) {
            //    tree1.Insert("aardvark", new BPlusListLong(new long[] { 13, 47, 22 }));
            //    tree1.Insert("byproduct", new BPlusListLong(new long[] { 5 }));
            //    tree1.Insert("charactercode", new BPlusListLong(new long[] { 21 }));
            //    // this one should split the root into [ab] [c] [d]
            //    tree1.Insert("delaware", new BPlusListLong(new long[] { 32, 14 }));
            //    tree1.Insert("elephant", new BPlusListLong(new long[] { 41 }));
            //    tree1.Insert("foliage", new BPlusListLong(new long[] { 59 }));
            //}


            //using (BPlusTree<BPlusString, BPlusListLong> tree1 = BPlusTree<BPlusString, BPlusListLong>.Load(fileName + "1", true)){
            //    foreach(BPlusTreeLeafNode<BPlusString, BPlusListLong> leaf in tree1.GetAllLeaves()){
            //        Debug.WriteLine(leaf.ToString());
            //    }
            //}

            //using (BPlusTree<BPlusString, BPlusListLong> tree1 = BPlusTree<BPlusString, BPlusListLong>.Load(fileName + "1", true)) {
            //    foreach (BPlusTreeNode<BPlusString, BPlusListLong> node in tree1.TraverseBreadthFirst() ) {
            //        Debug.WriteLine(node.ToString());
            //    }
            //}

            SortedDictionary <string, List <long> > dic = new SortedDictionary <string, List <long> >();

            using (BPlusTree <BPlusString, BPlusListLong> bulkTree = BPlusTree <BPlusString, BPlusListLong> .Create(fileName, Encoding.UTF8, 3, 10, 5, 5)) {
                using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) {
                    bw.Write("aardvark");
                    bw.Write(3);
                    bw.Write((long)13);
                    bw.Write((long)47);
                    bw.Write((long)22);

                    bw.Write("byproduct");
                    bw.Write(1);
                    bw.Write((long)5);

                    bw.Write("charactercode");
                    bw.Write(1);
                    bw.Write((long)21);

                    bw.Write("delaware");
                    bw.Write(2);
                    bw.Write((long)32);
                    bw.Write((long)14);

                    bw.Write("elephant");
                    bw.Write(1);
                    bw.Write((long)41);

                    bw.Write("foliage");
                    bw.Write(1);
                    bw.Write((long)59);

                    bw.Write("georgia");
                    bw.Write(1);
                    bw.Write((long)63);

                    bw.Write("homeworkism");
                    bw.Write(1);
                    bw.Write((long)131);

                    bw.Write("indigo");
                    bw.Write(1);
                    bw.Write((long)217);

                    bw.Write("jello");
                    bw.Write(1);
                    bw.Write((long)142);

                    bw.Write("kangaroo");
                    bw.Write(1);
                    bw.Write((long)150);

                    bw.Write("literal");
                    bw.Write(1);
                    bw.Write((long)167);

                    bw.Write("mommy");
                    bw.Write(1);
                    bw.Write((long)169);

                    bw.Write("nuance");
                    bw.Write(1);
                    bw.Write((long)173);

                    bw.Write("omaha");
                    bw.Write(1);
                    bw.Write((long)180);

                    bw.Write("penny");
                    bw.Write(1);
                    bw.Write((long)233);

                    bw.Write("qwerty");
                    bw.Write(1);
                    bw.Write((long)250);

                    bw.Write("rollin'");
                    bw.Write(1);
                    bw.Write((long)261);

                    bw.Write("semi-professional");
                    bw.Write(1);
                    bw.Write((long)99);

                    bw.Write("trouble");
                    bw.Write(1);
                    bw.Write((long)269);

                    bw.Write("umpire");
                    bw.Write(1);
                    bw.Write((long)271);

                    bw.Write("vampire");
                    bw.Write(1);
                    bw.Write((long)280);

                    bw.Write("weaver");
                    bw.Write(1);
                    bw.Write((long)290);

                    bw.Write("xylophone");
                    bw.Write(1);
                    bw.Write((long)102);

                    bw.Write("yellowbelly");
                    bw.Write(1);
                    bw.Write((long)120);

                    bw.Write("zebra");
                    bw.Write(1);
                    bw.Write((long)210);

                    int max = 10000;

                    for (int i = 1; i < max; i++)
                    {
                        bw.Write("zebra" + i.ToString().PadLeft(4, '0'));
                        bw.Write(1);
                        bw.Write((long)(200 + i));
                    }
                    bw.BaseStream.Position = 0;
                    bulkTree.BulkInsert(bw.BaseStream, 1.0M);
                }
            }


            using (BPlusTree <BPlusString, BPlusListLong> tree = BPlusTree <BPlusString, BPlusListLong> .Load(fileName, true)) {
                foreach (BPlusTreeNode <BPlusString, BPlusListLong> node in tree.TraverseBreadthFirst(true))
                {
                    Debug.WriteLine(node.ToString());
                }

                List <KeywordMatch <BPlusString, BPlusListLong> > matches = tree.Search("zebra", KeywordMatchMode.ExactMatch, true);
                foreach (var match in matches)
                {
                    Debug.WriteLine(match.ToString());
                }
            }

            using (BPlusTree <BPlusString, BPlusListLong> tree = BPlusTree <BPlusString, BPlusListLong> .Load(fileName, false)) {
                tree.Insert("abdomen", new BPlusListLong(new long[] { 1, 2, 3 }.ToList()));

//				tree.Update("abdomen", KeywordMatchMode.ExactMatch, new BPlusListLong(new long[] { 14, 98 }.ToList()), UpdateMode.Add);
//				tree.Update("abdomen", KeywordMatchMode.ExactMatch, new BPlusListLong(new long[] { 14, 98 }.ToList()), UpdateMode.Replace);
//				tree.Update("abdomen", KeywordMatchMode.ExactMatch, new BPlusListLong(new long[] { 3 }.ToList()), UpdateMode.Subtract);

                tree.Update("a", KeywordMatchMode.StartsWith, new BPlusListLong(new long[] { 3 }.ToList()), UpdateMode.Add);

                //foreach (KeywordMatch km in tree.GetAllMatches()) {
                //    MessageBox.Show(km.Keyword + "=" + km.Value.ToString());
                //}

                //foreach (BPlusString keyword in tree.GetAllKeywords()) {
                //    MessageBox.Show(keyword.Value);
                //}


                // ExactMatch tests
                List <KeywordMatch <BPlusString, BPlusListLong> > matches = tree.Search("a", KeywordMatchMode.StartsWith, false);               // should return 2 items

                foreach (string key in dic.Keys)
                {
                    matches = tree.Search(new BPlusString(key), KeywordMatchMode.ExactMatch, false);
                    if (matches.Count != 1)
                    {
                        MessageBox.Show("Invalid!  did not find value '" + key + "' in the tree!");
                    }
                }



                matches = tree.Search("homeworkism", KeywordMatchMode.ExactMatch, false);

                matches = tree.Search("Elephant", KeywordMatchMode.ExactMatch, false);                                          // should return no items
                matches = tree.Search("CHaractercode", KeywordMatchMode.ExactMatch, true);                                      // should return 1 item
                matches = tree.Search("semi-professional", KeywordMatchMode.ExactMatch, true);                                  // should return 1 item
                matches = tree.Search("nonexistantkeyhere", KeywordMatchMode.StartsWith, true);                                 // should return no items

                // StartsWith tests
                matches = tree.Search("byp", KeywordMatchMode.StartsWith, false);                               // should return 1 item
                matches = tree.Search("Elepha", KeywordMatchMode.StartsWith, false);                            // should return no items
                matches = tree.Search("CHar", KeywordMatchMode.StartsWith, true);                               // should return 1 item
                matches = tree.Search("nonexistantkeyhere", KeywordMatchMode.StartsWith, true);                 // should return no items
                matches = tree.Search("semi-pro", KeywordMatchMode.StartsWith, true);                           // should return 1 item

                // EndsWith tests
                matches = tree.Search("duct", KeywordMatchMode.EndsWith, false);                                                // should return 1 item
                matches = tree.Search("ANt", KeywordMatchMode.EndsWith, false);                                                 // should return no items
                matches = tree.Search("CoDe", KeywordMatchMode.EndsWith, true);                                                 // should return 1 item
                matches = tree.Search("ISM", KeywordMatchMode.EndsWith, true);                                                  // should return 1 item
                matches = tree.Search("ssional", KeywordMatchMode.EndsWith, true);                                              // should return 1 item

                // Contains tests
                matches = tree.Search("pro", KeywordMatchMode.Contains, true);                                  // should return 2 items
                matches = tree.Search("RAc", KeywordMatchMode.Contains, true);                                  // should return 1 items
                matches = tree.Search("ARA", KeywordMatchMode.Contains, false);                                 // should return no items
                matches = tree.Search("-", KeywordMatchMode.Contains, false);                                   // should return 1 item
                matches = tree.Search("zebra0030", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0012", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0032", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0039", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0040", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0060", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0051", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0058", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0099", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0072", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("zebra0080", KeywordMatchMode.ExactMatch, false);                         // should return 1 item
                matches = tree.Search("semi-professional", KeywordMatchMode.Contains, true);                    // should return 1 item
            }
        }