Example #1
0
        public IList <Author_DocumentEntity> GetAllEntities()
        {
            var list = new List <Author_DocumentEntity>();

            options.CalcBTreeOrder(4, 4);
            options.CreateFile = CreatePolicy.Never;
            options.FileName   = Path.GetFileName("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/TreeAuthor_Document");
            int count = 0;

            using (var tree = new BPlusTree <int, int>(options))
            {
                using (FileStream fs = new FileStream("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/Author_Document", FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        foreach (var item in tree)
                        {
                            if (count == 50)
                            {
                                return(list);
                            }
                            fs.Seek(item.Value, 0);
                            list.Add(new Author_DocumentEntity()
                            {
                                A_D_id      = br.ReadInt32().ToString(),
                                Author_id   = br.ReadInt32().ToString(),
                                Document_id = br.ReadInt32().ToString()
                            });
                            count++;
                        }
                    }
                }
            }
            return(list);
        }
Example #2
0
        public ContentStorage(string directory, bool asReadonly)
        {
            _disposables = new DisposingList();
            _readonly    = asReadonly;

            _storageDir = directory;
            _dataDir    = Path.Combine(directory, "content");
            if (!_readonly && !Directory.Exists(_dataDir))
            {
                Directory.CreateDirectory(_dataDir);
            }
            _indexDir = Path.Combine(directory, "index");

            BPlusTree <string, ContentRecord> .Options options = new BPlusTree <string, ContentRecord> .Options(
                PrimitiveSerializer.Instance, new ProtoSerializer <ContentRecord, ContentRecord.Builder>()
                );

            options.CacheKeepAliveMaximumHistory = 1000;
            options.CacheKeepAliveMinimumHistory = 100;
            options.CacheKeepAliveTimeout        = int.MaxValue;
            options.CachePolicy = asReadonly ? CachePolicy.All : CachePolicy.Recent;

            options.CreateFile     = asReadonly ? CreatePolicy.Never : CreatePolicy.IfNeeded;
            options.FileName       = Path.Combine(directory, "content.index");
            options.FileBlockSize  = 0x02000; //8kb
            options.ReadOnly       = asReadonly;
            options.CallLevelLock  = asReadonly ? (ILockStrategy) new IgnoreLocking() : new SimpleReadWriteLocking();
            options.LockingFactory = asReadonly ? (ILockFactory) new LockFactory <IgnoreLocking>() : new LockFactory <SimpleReadWriteLocking>();

            options.CalcBTreeOrder(64, 256);
            _index = new BPlusTree <string, ContentRecord>(options);
            _disposables.Add(_index);

            _index.EnableCount();
        }
Example #3
0
        } //lerCSV()

        /// <summary>
        /// Cria a árvore à partir dos dados lidos do CSV
        /// </summary>
        /// <param name="dicionario">Dados lidos do CSV</param>
        /// <returns>Indica se houve erro (false) ou não (true) no processo</returns>
        public static bool criaArvore(Dictionary <int, DadosOcorrencia> dicionario)
        {
            //Cria o componente responsável por serializar os dados a serem escritos na árvore
            ProtoNetSerializer <DadosOcorrencia> serializer = new ProtoNetSerializer <DadosOcorrencia>();

            //Prepara as opções da árvore
            var tree_options = new BPlusTree <int, DadosOcorrencia> .OptionsV2(PrimitiveSerializer.Int32, serializer);

            tree_options.CalcBTreeOrder(8, 30);
            tree_options.CreateFile = CreatePolicy.IfNeeded;
            tree_options.FileName   = path_btree;

            //Checa se o arquivo já existe
            if (!File.Exists(path_btree))
            {
                using (var tree = new BPlusTree <int, DadosOcorrencia>(tree_options))
                {
                    foreach (KeyValuePair <int, DadosOcorrencia> entry in dicionario)
                    {
                        //Percorre o dicionário e adiciona na árvore
                        tree.Add(entry.Key, entry.Value);
                    }
                }
            }
            //Erro, a árvore já existe!
            else
            {
                return(false);
            }

            //Se não houve erros retorna true
            return(true);
        } //criaArvore()
Example #4
0
        public static void Test_DuplicateHandelingOptions()
        {
            var options = new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String);

            options.CalcBTreeOrder(16, 24);
            options.FileBlockSize = 8192;
            options.CreateFile    = CreatePolicy.Always;
            options.FileName      = "I:\\test.tmp";

            BulkInsertOptions opts = new BulkInsertOptions();

            opts.CommitOnCompletion = true; // check how to properly set this value using Roger examples.
            opts.DuplicateHandling  = DuplicateHandling.FirstValueWins;
            opts.InputIsSorted      = true;
            opts.ReplaceContents    = false;

            var sortedContent = new SortedDictionary <double, string>();

            sortedContent.Add(10.0, "Demah");

            using (var tree = new BPlusTree <double, string>(options))
            {
                tree.Add(10.0, "Hamed");
                tree.BulkInsert(sortedContent, opts);
            }
        }
Example #5
0
        private BPlusTree <string, Tuple <OSSIndexProject, IEnumerable <OSSIndexProjectVulnerability> > > InitialiseProjectVulnerabilitiesCache(string file)
        {
            lock (project_vulnerabilities_cache_lock)
            {
                BPlusTree <string, Tuple <OSSIndexProject, IEnumerable <OSSIndexProjectVulnerability> > > .OptionsV2 cache_file_options =
                    new BPlusTree <string, Tuple <OSSIndexProject, IEnumerable <OSSIndexProjectVulnerability> > > .OptionsV2(PrimitiveSerializer.String,
                                                                                                                             new BsonSerializer <Tuple <OSSIndexProject, IEnumerable <OSSIndexProjectVulnerability> > >());

                cache_file_options.CalcBTreeOrder(4, 128);
                cache_file_options.CreateFile         = CreatePolicy.IfNeeded;
                cache_file_options.FileName           = file;
                cache_file_options.StoragePerformance = StoragePerformance.CommitToDisk;
                var c = new BPlusTree <string, Tuple <OSSIndexProject, IEnumerable <OSSIndexProjectVulnerability> > >(cache_file_options);
                c.EnableCount();
                IEnumerable <string> expired_cache_keys =
                    from cache_key in c.Keys
                    where DateTime.UtcNow.Subtract(GetProjectVulnerabilitiesCacheEntry(cache_key).Item2) >= this.ProjectVulnerabilitiesCacheTTL
                    join artifact in ArtifactsWithProjects
                    on GetProjectVulnerabilitiesCacheEntry(cache_key).Item1 equals artifact.ProjectId
                    select cache_key;

                this.ProjectVulnerabilitiesExpiredCacheKeys = expired_cache_keys;
                foreach (string k in expired_cache_keys)
                {
                    if (!c.Remove(k))
                    {
                        throw new Exception("Error removing expired cache item with key: " + k + ".");
                    }
                }
                return(c);
            }
        }
 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);
         }
     }
 }
Example #7
0
        public static void Run()
        {
            Console.WriteLine("Now generating sorted items ...");

            int nNewItems   = 100000;
            int nOldItems   = 100000;
            var rnd         = new Random();
            var sortedItems = new SortedDictionary <double, string>();
            int newItem     = 0;

            for (int i = 1; i <= nNewItems; i++)
            {
                do
                {
                    newItem = rnd.Next(0, Int32.MaxValue);
                }while (sortedItems.ContainsKey(newItem));

                sortedItems.Add(newItem, Convert.ToString(rnd.Next(0, Int32.MaxValue)));
                Console.Write("\rAdding {0,5}% : {1,10:N0}/{2:N0}", Math.Round((((double)i / (double)nNewItems) * 100)), i, nNewItems);
            }

            Stopwatch stp = new Stopwatch();

            var options = new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String);

            options.CalcBTreeOrder(16, 24);
            options.FileBlockSize = 8192;
            options.CreateFile    = CreatePolicy.Always;
            options.FileName      = "I:\\test.tmp";

            BulkInsertOptions opts = new BulkInsertOptions();

            opts.CommitOnCompletion = true; // check how to properly set this value using Roger examples.
            opts.DuplicateHandling  = DuplicateHandling.LastValueWins;
            opts.InputIsSorted      = true;
            opts.ReplaceContents    = false;

            AddUpdateValue update = new AddUpdateValue();

            Console.WriteLine();
            Console.WriteLine("Now creating tree ...");
            using (var tree = new BPlusTree <double, string>(options))
            {
                stp.Start();
                for (int i = 0; i < nOldItems; i++)
                {
                    tree.AddOrUpdate(rnd.Next(0, Int32.MaxValue), ref update);
                }
                stp.Stop();
                Console.WriteLine("Initial <{0:N0}> items =>  ET : {1}     Speed : {2:N0} item/sec", nOldItems, stp.Elapsed, Math.Round((double)(nOldItems / stp.Elapsed.TotalSeconds), 5));

                stp.Restart();
                tree.AddRangeSorted(sortedItems, true);
                stp.Stop();
                Console.WriteLine("Bulk    <{0:N0}> items =>  ET : {1}     Speed : {2:N0} item/sec", nNewItems, stp.Elapsed, Math.Round((double)(nNewItems / stp.Elapsed.TotalSeconds), 5));
            }

            Console.ReadLine();
        }
Example #8
0
        internal BPlusTree <T1, T2> CreateStoredBPlusForWrite <T1, T2>(string tablePath, ISerializer <T1> keySerializer, ISerializer <T2> valueSerializer)
        {
            var options = new BPlusTree <T1, T2> .OptionsV2(keySerializer, valueSerializer);

            options.CalcBTreeOrder(8, 64);
            options.CreateFile = CreatePolicy.IfNeeded;
            options.FileName   = tablePath;
            return(new BPlusTree <T1, T2>(options));
        }
Example #9
0
        public IList <SourceEntity> GetAllEntities()
        {
            var list = new List <SourceEntity>();

            options.CalcBTreeOrder(4, 4);
            options.CreateFile = CreatePolicy.Never;
            options.FileName   = Path.GetFileName("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/TreeSource");
            int count = 0;

            using (var tree = new BPlusTree <int, int>(options))
            {
                using (FileStream fs = new FileStream("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/Source", FileMode.Open))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        foreach (var item in tree)
                        {
                            //if (count == 50)
                            //{
                            //    IComparer<SourceEntity> comparer = new MyOrderingClass();
                            //    list.Sort(comparer);
                            //    return list;
                            //}
                            fs.Seek(item.Value, 0);
                            list.Add(new SourceEntity()
                            {
                                Source_id = br.ReadInt32().ToString(),
                                Title     = br.ReadString(),
                                Type      = br.ReadString()
                            });
                            count++;
                        }
                    }
                }
            }
            IComparer <SourceEntity> comparer = new MyOrderingClass();

            list.Sort(comparer);
            return(list);
        }
        BPlusTree<Guid, TestInfo>.OptionsV2 GetOptions(TempFile temp)
        {
            BPlusTree<Guid, TestInfo>.OptionsV2 options = new BPlusTree<Guid, TestInfo>.OptionsV2(
                PrimitiveSerializer.Guid, new TestInfoSerializer());
            options.CalcBTreeOrder(Marshal.SizeOf(typeof(Guid)), Marshal.SizeOf(typeof(TestInfo)));
            options.CreateFile = CreatePolicy.IfNeeded;
            options.FileName = temp.TempPath;

            // The following three options allow for automatic commit/recovery:
            options.CallLevelLock = new ReaderWriterLocking();
            options.TransactionLogFileName = Path.ChangeExtension(options.FileName, ".tlog");
            return options;
        }
        BPlusTree<Guid, TestInfo>.OptionsV2 GetOptions(TempFile temp)
        {
            BPlusTree<Guid, TestInfo>.OptionsV2 options = new BPlusTree<Guid, TestInfo>.OptionsV2(
                PrimitiveSerializer.Guid, new TestInfoSerializer());
            options.CalcBTreeOrder(Marshal.SizeOf(typeof(Guid)), Marshal.SizeOf(typeof(TestInfo)));
            options.CreateFile = CreatePolicy.IfNeeded;
            options.FileName = temp.TempPath;

            // The following three options allow for automatic commit/recovery:
            options.CallLevelLock = new ReaderWriterLocking();
            options.TransactionLogFileName = Path.ChangeExtension(options.FileName, ".tlog");
            return options;
        }
Example #12
0
        public BinaryStorage(StorageConfiguration configuration)
        {
            var options = new BPlusTree <string, Data> .OptionsV2(PrimitiveSerializer.String, new DataSerializer());

            options.CreateFile    = CreatePolicy.IfNeeded;
            options.FileName      = Path.Combine(configuration.WorkingFolder, "index.bin");
            options.CallLevelLock = new ReaderWriterLocking();
            options.CachePolicy   = CachePolicy.Recent;
            options.CacheKeepAliveMaximumHistory = cacheSize / (estimatedKeySize + estimatedDataSize);
            options.CalcBTreeOrder(estimatedKeySize, estimatedDataSize);
            index = new BPlusTree <string, Data> (options);

            storageFile   = Path.Combine(configuration.WorkingFolder, "storage.bin");
            storageStream = new FileStream(storageFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan | FileOptions.WriteThrough);
        }
Example #13
0
        private static TimeSpan BasicInsertionSpeedTest(int inputSize)
        {
            Stopwatch watch = new Stopwatch();

            Random rnd = new Random();

            string longString = "";

            for (int i = 0; i < 300; i++)
            {
                longString += (char)rnd.Next(48, 90);
            }

            int longStringSize = longString.Length * sizeof(Char);

            BPlusTree <double, string> .OptionsV2 options =
                new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String);

            //options.CalcBTreeOrder(16, 20);
            options.CalcBTreeOrder(16, longStringSize);


            // to write to RAM:
            //options.CreateFile = CreatePolicy.Never;


            // to write to disk:
            options.CreateFile = CreatePolicy.Always;
            options.FileName   = Path.GetTempFileName();


            using (var tree = new BPlusTree <double, string>(options))
            {
                watch.Start();

                while (inputSize-- > 0)
                {
                    Console.Write("\rRemaining: {0}", inputSize.ToString("N0", CultureInfo.InvariantCulture));
                    tree.Add(inputSize, longString);
                }

                watch.Stop();
            }



            return(watch.Elapsed);
        }
        public void TestAbortWritersAndRecover()
        {
            BPlusTree <KeyInfo, DataValue> .Options options = new BPlusTree <KeyInfo, DataValue> .Options(
                new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());

            options.CalcBTreeOrder(32, 300);
            options.FileName   = TempFile.TempPath;
            options.CreateFile = CreatePolicy.Always;

            using (TempFile copy = new TempFile())
            {
                copy.Delete();
                int minRecordCreated = StartAndAbortWriters(options, copy);

                using (TempFile.Attach(copy.TempPath + ".recovered"))   //used to create the new copy
                    using (TempFile.Attach(copy.TempPath + ".deleted")) //renamed existing file
                    {
                        options.CreateFile = CreatePolicy.Never;
                        int recoveredRecords = BPlusTree <KeyInfo, DataValue> .RecoverFile(options);

                        if (recoveredRecords < RecordsCreated)
                        {
                            Assert.Fail("Unable to recover records, recieved ({0} of {1}).", recoveredRecords, RecordsCreated);
                        }

                        options.FileName = copy.TempPath;
                        recoveredRecords = BPlusTree <KeyInfo, DataValue> .RecoverFile(options);

                        Assert.IsTrue(recoveredRecords >= minRecordCreated, "Expected at least " + minRecordCreated + " found " + recoveredRecords);

                        using (BPlusTree <KeyInfo, DataValue> dictionary = new BPlusTree <KeyInfo, DataValue>(options))
                        {
                            dictionary.EnableCount();
                            Assert.AreEqual(recoveredRecords, dictionary.Count);

                            foreach (KeyValuePair <KeyInfo, DataValue> kv in dictionary)
                            {
                                Assert.AreEqual(kv.Key.UID, kv.Value.Key.UID);
                                dictionary.Remove(kv.Key);
                            }

                            Assert.AreEqual(0, dictionary.Count);
                        }
                    }
            }
        }
Example #15
0
        public void TestConcurrency()
        {
            mreStop.Reset();
            using (TempFile temp = new TempFile())
            {
                BPlusTree <Guid, TestInfo> .OptionsV2 options = new BPlusTree <Guid, TestInfo> .OptionsV2(
                    PrimitiveSerializer.Guid, new TestInfoSerializer());

                options.CalcBTreeOrder(16, 24);
                options.CreateFile = CreatePolicy.Always;
                options.FileName   = temp.TempPath;
                using (BPlusTree <Guid, TestInfo> tree = new BPlusTree <Guid, TestInfo>(options))
                {
                    tree.EnableCount();
                    var actions = new List <IAsyncResult>();
                    var tests   = new Action <BPlusTree <Guid, TestInfo> >[]
                    {
                        DeleteStuff, UpdateStuff, AddStuff, AddRanges, BulkyInserts,
                        FetchStuff, FetchStuff, FetchStuff, FetchStuff, FetchStuff
                    };

                    foreach (var t in tests)
                    {
                        actions.Add(t.BeginInvoke(tree, null, null));
                    }

                    const int waitIterations = 8;                       // wait for n seconds

                    int timesWaited = 0;
                    do
                    {
                        Trace.TraceInformation("Dictionary.Count = {0}", tree.Count);
                        Thread.Sleep(1000);
                        timesWaited++;
                    } while (timesWaited < waitIterations && Debugger.IsAttached);

                    mreStop.Set();
                    for (int i = 0; i < actions.Count; i++)
                    {
                        tests[i].EndInvoke(actions[i]);
                    }

                    Trace.TraceInformation("Dictionary.Count = {0}", tree.Count);
                }
            }
        }
        public void TestErrorsOnInsertAndDelete()
        {
            const int CountPerThread = 100;

            BPlusTree <KeyInfo, DataValue> .OptionsV2 options = new BPlusTree <KeyInfo, DataValue> .OptionsV2(
                new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());

            options.CalcBTreeOrder(32, 300);
            options.FileName   = TempFile.TempPath;
            options.CreateFile = CreatePolicy.Always;

            using (BPlusTree <KeyInfo, DataValue> dictionary = new BPlusTree <KeyInfo, DataValue>(options))
                using (WorkQueue work = new WorkQueue(Environment.ProcessorCount))
                {
                    Exception lastError = null;
                    work.OnError += delegate(object o, ErrorEventArgs e) { lastError = e.GetException(); };

                    for (int i = 0; i < Environment.ProcessorCount; i++)
                    {
                        work.Enqueue(new ThreadedTest(dictionary, CountPerThread).Run);
                    }

                    for (int i = 0; i < CountPerThread; i++)
                    {
                        if (i % 2 == 0)
                        {
                            try {
                                dictionary.TryAdd(new KeyInfo(Guid.NewGuid(), i), k => { throw new ExpectedException(); });
                            } catch { }
                        }
                        else
                        {
                            try
                            {
                                dictionary.TryRemove(dictionary.First().Key, (k, v) => { throw new ExpectedException(); });
                            }
                            catch { }
                        }
                    }

                    Assert.IsTrue(work.Complete(true, 60000));
                    Assert.IsNull(lastError, "Exception raised in worker: {0}", lastError);
                }
        }
        public void TestConcurrency()
        {
            mreStop.Reset();
            using(TempFile temp = new TempFile())
            {
                BPlusTree<Guid, TestInfo>.OptionsV2 options = new BPlusTree<Guid, TestInfo>.OptionsV2(
                    PrimitiveSerializer.Guid, new TestInfoSerializer());
                options.CalcBTreeOrder(16, 24);
                options.CreateFile = CreatePolicy.Always;
                options.FileName = temp.TempPath;
                using (BPlusTree<Guid, TestInfo> tree = new BPlusTree<Guid, TestInfo>(options))
                {
                    tree.EnableCount();
                    var actions = new List<IAsyncResult>();
                    var tests = new Action<BPlusTree<Guid, TestInfo>>[] 
                    {
                        DeleteStuff, UpdateStuff, AddStuff, AddRanges, BulkyInserts,
                        FetchStuff, FetchStuff, FetchStuff, FetchStuff, FetchStuff
                    };

                    foreach (var t in tests)
                        actions.Add(t.BeginInvoke(tree, null, null));

					const int waitIterations = 8;	// wait for n seconds

					int timesWaited = 0;
                    do
                    {
                        Trace.TraceInformation("Dictionary.Count = {0}", tree.Count);
                        Thread.Sleep(1000);
						timesWaited++;
                    } while (timesWaited<waitIterations && Debugger.IsAttached);

                    mreStop.Set();
                    for (int i = 0; i < actions.Count; i++)
                    {
                        tests[i].EndInvoke(actions[i]);
                    }

                    Trace.TraceInformation("Dictionary.Count = {0}", tree.Count);
                }
            }
        }
Example #18
0
        public static void Test_AllowUpdates()
        {
            var options = new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String);

            options.CalcBTreeOrder(16, 24);
            options.FileBlockSize = 8192;
            options.CreateFile    = CreatePolicy.Always;
            options.FileName      = "I:\\test.tmp";

            var sortedContent = new SortedDictionary <double, string>();

            sortedContent.Add(10.0, "Demah");

            using (var tree = new BPlusTree <double, string>(options))
            {
                tree.Add(10.0, "Hamed");
                tree.AddRangeSorted(sortedContent, true);
            }
        }
Example #19
0
        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);
                }
            }
        }
Example #20
0
        } //criaArvore()

        /// <summary>
        /// Busca um valor específico na árvore
        /// </summary>
        /// <returns>Dados lidos da árvore</returns>
        public static List <DadosOcorrencia> buscaValor(string valor)
        {
            //Cria uma nova lista para retornar
            List <DadosOcorrencia> lista = new List <DadosOcorrencia>();

            //Cria o componente responsável por desserializar os dados a serem lidos da árvore
            ProtoNetSerializer <DadosOcorrencia> serializer = new ProtoNetSerializer <DadosOcorrencia>();

            //Prepara as opções da árvore
            var tree_options = new BPlusTree <int, DadosOcorrencia> .OptionsV2(PrimitiveSerializer.Int32, serializer);

            tree_options.CalcBTreeOrder(8, 30);
            tree_options.CreateFile = CreatePolicy.IfNeeded;
            tree_options.FileName   = path_btree;

            //Checa se o arquivo já existe
            if (File.Exists(path_btree))
            {
                using (var tree = new BPlusTree <int, DadosOcorrencia>(tree_options))
                {
                    //Varre a árvore buscando o dado
                    foreach (KeyValuePair <int, DadosOcorrencia> par in tree)
                    {
                        if (par.Value.ocorrencia.localidade.IndexOf(valor, StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            lista.Add(par.Value);
                        }
                    }
                }
            }
            else
            {
                //Erro, a árvore não existe!
            }

            //Retorna a lista lida
            return(lista);
        } //buscaValor()
Example #21
0
        public void TestCommonConfiguration()
        {
            BPlusTree <KeyInfo, DataValue> .Options options =
                new BPlusTree <KeyInfo, DataValue> .Options(new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());

            options.CalcBTreeOrder(32, 300);          //we can simply just guess close
            options.FileName   = TempFile.TempPath;
            options.CreateFile = CreatePolicy.Always; //obviously this is just for testing
            Assert.AreEqual(FileVersion.Version1, options.FileVersion);

            Random  rand = new Random();
            KeyInfo k1 = new KeyInfo(), k2 = new KeyInfo();

            using (BPlusTree <KeyInfo, DataValue> tree = new BPlusTree <KeyInfo, DataValue>(options))
            {
                byte[] data = new byte[255];

                rand.NextBytes(data);
                tree.Add(k1, new DataValue(k1, data));

                Assert.IsTrue(tree.ContainsKey(k1));
                Assert.IsFalse(tree.ContainsKey(k1.Next()));
                Assert.AreEqual(data, tree[k1].Bytes);

                rand.NextBytes(data);
                tree.Add(k2, new DataValue(k2, data));

                Assert.IsTrue(tree.ContainsKey(k2));
                Assert.IsFalse(tree.ContainsKey(k2.Next()));
                Assert.AreEqual(data, tree[k2].Bytes);
            }
            options.CreateFile = CreatePolicy.Never;
            using (BPlusTree <KeyInfo, DataValue> tree = new BPlusTree <KeyInfo, DataValue>(options))
            {
                Assert.IsTrue(tree.ContainsKey(k1));
                Assert.IsTrue(tree.ContainsKey(k2));
            }
        }
        public void TestConcurrentCreateReadUpdateDelete8000()
        {
            BPlusTree <KeyInfo, DataValue> .OptionsV2 options = new BPlusTree <KeyInfo, DataValue> .OptionsV2(
                new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());

            const int keysize   = 16 + 4;
            const int valuesize = keysize + 256 + 44;

            options.CalcBTreeOrder(keysize, valuesize);
            options.FileName      = TempFile.TempPath;
            options.CreateFile    = CreatePolicy.Always;
            options.FileBlockSize = 8192;
            options.StorageType   = StorageType.Disk;

            options.CacheKeepAliveTimeout        = 10000;
            options.CacheKeepAliveMinimumHistory = 0;
            options.CacheKeepAliveMaximumHistory = 200;

            options.CallLevelLock  = new ReaderWriterLocking();
            options.LockingFactory = new LockFactory <SimpleReadWriteLocking>();
            options.LockTimeout    = 10000;

            using (BPlusTree <KeyInfo, DataValue> dictionary = new BPlusTree <KeyInfo, DataValue>(options))
                using (WorkQueue work = new WorkQueue(Environment.ProcessorCount))
                {
                    Exception lastError = null;
                    work.OnError += delegate(object o, ErrorEventArgs e) { lastError = e.GetException(); };

                    for (int i = 0; i < Environment.ProcessorCount; i++)
                    {
                        work.Enqueue(new ThreadedTest(dictionary, 1000).Run);
                    }

                    Assert.IsTrue(work.Complete(true, 60000));
                    Assert.IsNull(lastError, "Exception raised in worker: {0}", lastError);
                }
        }
Example #23
0
File: Di4.cs Project: olgatei/Di4
        private BPlusTree <C, B> .OptionsV2 GetIncOptions()
        {
            _lambdaItemSerializer  = new LambdaItemSerializer();
            _lambdaArraySerializer = new LambdaArraySerializer(_lambdaItemSerializer);
            _incBSerializer        = new BSerializer(_lambdaArraySerializer);
            var rtv = new BPlusTree <C, B> .OptionsV2(_options.CSerializer, _incBSerializer, _options.Comparer);

            rtv.ReadOnly = _options.OpenReadOnly;

            if (_options.MaximumChildNodes >= 4 &&
                _options.MinimumChildNodes >= 2 &&
                _options.MaximumValueNodes >= 4 &&
                _options.MinimumValueNodes >= 2)
            {
                rtv.MaximumChildNodes = _options.MaximumChildNodes;
                rtv.MinimumChildNodes = _options.MinimumChildNodes;
                rtv.MaximumValueNodes = _options.MaximumValueNodes;
                rtv.MinimumValueNodes = _options.MinimumValueNodes;
            }

            if (_options.AverageKeySize != 0 && _options.AverageValueSize != 0)
            {
                rtv.CalcBTreeOrder(_options.AverageKeySize, _options.AverageValueSize);
            }

            if (_options.FileBlockSize != 0)
            {
                rtv.FileBlockSize = _options.FileBlockSize;
            }

            rtv.CachePolicy = _options.cacheOptions.CachePolicy;
            if (_options.CreatePolicy != CreatePolicy.Never)
            {
                rtv.FileName = _options.FileName + ".iidx";
            }

            rtv.CreateFile         = _options.CreatePolicy;
            rtv.ExistingLogAction  = _options.ExistingLogAction;
            rtv.StoragePerformance = _options.StoragePerformance;

            rtv.CallLevelLock = new ReaderWriterLocking();
            if (_options.LockTimeout > 0)
            {
                rtv.LockTimeout = _options.LockTimeout;
            }

            switch (_options.Locking)
            {
            case LockMode.WriterOnlyLocking:
                rtv.LockingFactory = new LockFactory <WriterOnlyLocking>();
                break;

            case LockMode.ReaderWriterLocking:
                rtv.LockingFactory = new LockFactory <ReaderWriterLocking>();
                break;

            case LockMode.SimpleReadWriteLocking:
                rtv.LockingFactory = new LockFactory <SimpleReadWriteLocking>();
                break;

            case LockMode.IgnoreLocking:
                rtv.LockingFactory = new IgnoreLockFactory();
                break;
            }

            if (_options.cacheOptions.CacheMaximumHistory != 0 && _options.cacheOptions.CacheKeepAliveTimeOut != 0)
            {
                rtv.CacheKeepAliveMaximumHistory = _options.cacheOptions.CacheMaximumHistory;
                rtv.CacheKeepAliveMinimumHistory = _options.cacheOptions.CacheMinimumHistory;
                rtv.CacheKeepAliveTimeout        = _options.cacheOptions.CacheKeepAliveTimeOut;
            }

            return(rtv);
        }
Example #24
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");
                }
            }
        }