Exemple #1
0
        public virtual void TestSimple()
        {
            Directory dest = NewDirectory();
            var       tw1  = new DirectoryTaxonomyWriter(dest);

            tw1.AddCategory(new FacetLabel("Author", "Mark Twain"));
            tw1.AddCategory(new FacetLabel("Animals", "Dog"));
            tw1.AddCategory(new FacetLabel("Author", "Rob Pike"));

            Directory src = NewDirectory();
            var       tw2 = new DirectoryTaxonomyWriter(src);

            tw2.AddCategory(new FacetLabel("Author", "Rob Pike"));
            tw2.AddCategory(new FacetLabel("Aardvarks", "Bob"));
            tw2.Dispose();

            IOrdinalMap map = randomOrdinalMap();

            tw1.AddTaxonomy(src, map);
            tw1.Dispose();

            validate(dest, src, map);

            IOUtils.Dispose(dest, src);
        }
Exemple #2
0
        private void validate(Directory dest, Directory src, IOrdinalMap ordMap)
        {
            using (var destTr = new DirectoryTaxonomyReader(dest))
            {
                int destSize = destTr.Count;
                using (var srcTR = new DirectoryTaxonomyReader(src))
                {
                    var map = ordMap.GetMap();

                    // validate taxo sizes
                    int srcSize = srcTR.Count;
                    Assert.True(destSize >= srcSize, "destination taxonomy expected to be larger than source; dest=" + destSize + " src=" + srcSize);

                    // validate that all source categories exist in destination, and their
                    // ordinals are as expected.
                    for (int j = 1; j < srcSize; j++)
                    {
                        FacetLabel cp          = srcTR.GetPath(j);
                        int        destOrdinal = destTr.GetOrdinal(cp);
                        Assert.True(destOrdinal > 0, cp + " not found in destination");
                        Assert.AreEqual(destOrdinal, map[j]);
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Takes the categories from the given taxonomy directory, and adds the
        /// missing ones to this taxonomy. Additionally, it fills the given
        /// <see cref="IOrdinalMap"/> with a mapping from the original ordinal to the new
        /// ordinal.
        /// </summary>
        public virtual void AddTaxonomy(Directory taxoDir, IOrdinalMap map)
        {
            EnsureOpen();
            DirectoryReader r = DirectoryReader.Open(taxoDir);

            try
            {
                int         size       = r.NumDocs;
                IOrdinalMap ordinalMap = map;
                ordinalMap.SetSize(size);
                int       @base = 0;
                TermsEnum te    = null;
                DocsEnum  docs  = null;
                foreach (AtomicReaderContext ctx in r.Leaves)
                {
                    AtomicReader ar    = ctx.AtomicReader;
                    Terms        terms = ar.GetTerms(Consts.FULL);
                    te = terms.GetIterator(te);
                    while (te.Next() != null)
                    {
                        FacetLabel cp      = new FacetLabel(FacetsConfig.StringToPath(te.Term.Utf8ToString()));
                        int        ordinal = AddCategory(cp);
                        docs = te.Docs(null, docs, DocsFlags.NONE);
                        ordinalMap.AddMapping(docs.NextDoc() + @base, ordinal);
                    }
                    @base += ar.MaxDoc; // no deletions, so we're ok
                }
                ordinalMap.AddDone();
            }
            finally
            {
                r.Dispose();
            }
        }
Exemple #4
0
        public virtual void TestAddToEmpty()
        {
            Directory dest = NewDirectory();

            Directory src = NewDirectory();
            DirectoryTaxonomyWriter srcTW = new DirectoryTaxonomyWriter(src);

            srcTW.AddCategory(new FacetLabel("Author", "Rob Pike"));
            srcTW.AddCategory(new FacetLabel("Aardvarks", "Bob"));
            srcTW.Dispose();

            DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest);
            IOrdinalMap             map    = randomOrdinalMap();

            destTW.AddTaxonomy(src, map);
            destTW.Dispose();

            validate(dest, src, map);

            IOUtils.Dispose(dest, src);
        }
Exemple #5
0
        private void Dotest(int ncats, int range)
        {
            AtomicInt32 numCats = new AtomicInt32(ncats);

            Directory[] dirs = new Directory[2];
            for (int i = 0; i < dirs.Length; i++)
            {
                dirs[i] = NewDirectory();
                var         tw         = new DirectoryTaxonomyWriter(dirs[i]);
                ThreadJob[] addThreads = new ThreadJob[4];
                for (int j = 0; j < addThreads.Length; j++)
                {
                    addThreads[j] = new ThreadAnonymousInnerClassHelper(this, range, numCats, tw);
                }

                foreach (ThreadJob t in addThreads)
                {
                    t.Start();
                }
                foreach (ThreadJob t in addThreads)
                {
                    t.Join();
                }

                tw.Dispose();
            }

            var         tw1 = new DirectoryTaxonomyWriter(dirs[0]);
            IOrdinalMap map = randomOrdinalMap();

            tw1.AddTaxonomy(dirs[1], map);
            tw1.Dispose();

            validate(dirs[0], dirs[1], map);

            IOUtils.Dispose(dirs);
        }
        private void validate(Directory dest, Directory src, IOrdinalMap ordMap)
        {
            var destTr = new DirectoryTaxonomyReader(dest);
            try
            {
                int destSize = destTr.Count;
                var srcTR = new DirectoryTaxonomyReader(src);
                try
                {
                    var map = ordMap.Map;

                    // validate taxo sizes
                    int srcSize = srcTR.Count;
                    Assert.True(destSize >= srcSize, "destination taxonomy expected to be larger than source; dest=" + destSize + " src=" + srcSize);

                    // validate that all source categories exist in destination, and their
                    // ordinals are as expected.
                    for (int j = 1; j < srcSize; j++)
                    {
                        FacetLabel cp = srcTR.GetPath(j);
                        int destOrdinal = destTr.GetOrdinal(cp);
                        Assert.True(destOrdinal > 0, cp + " not found in destination");
                        Assert.AreEqual(destOrdinal, map[j]);
                    }
                }
                finally
                {
                    ((TaxonomyReader)srcTR).Dispose(true);
                }
            }
            finally
            {
                ((TaxonomyReader)destTr).Dispose(true);
            }
        }