コード例 #1
0
        public void ContainerIndex_EndToEnd()
        {
            string jsonFilePath   = @"Content\Medium.json";
            string bionFilePath   = Path.ChangeExtension(jsonFilePath, ".bion");
            string dictionaryPath = Path.ChangeExtension(bionFilePath, "dict.bion");
            string comparePath    = Path.ChangeExtension(jsonFilePath, "compare.json");

            JsonBionConverter.JsonToBion(jsonFilePath, bionFilePath, dictionaryPath);

            using (WordCompressor compressor = WordCompressor.OpenRead(dictionaryPath))
                using (ContainerIndex cIndex = ContainerIndex.OpenRead(Path.ChangeExtension(bionFilePath, ".cdx")))
                    using (BionReader reader = new BionReader(File.OpenRead(bionFilePath), cIndex, compressor))
                    {
                        for (int i = 0; i < cIndex.Count; ++i)
                        {
                            ContainerEntry container = cIndex[i];

                            // Seek to container start
                            reader.Seek(container.StartByteOffset);

                            // Verify a container start is there
                            int depth = reader.Depth;
                            reader.Read();

                            bool isObject = (reader.TokenType == BionToken.StartObject);
                            Assert.AreEqual((isObject ? BionToken.StartObject : BionToken.StartArray), reader.TokenType);

                            // Read until the depth is back to the same value
                            while (reader.Depth != depth)
                            {
                                reader.Read();
                            }

                            // Verify this is the end container position
                            Assert.AreEqual((isObject ? BionToken.EndObject : BionToken.EndArray), reader.TokenType);
                            Assert.AreEqual(container.EndByteOffset, reader.BytesRead);
                        }
                    }
        }
コード例 #2
0
ファイル: BionSearcher.cs プロジェクト: Perf-Org-5KRepos/bion
        public int Write(JsonTextWriter writer, ISearchResult wordMatches, int skip = 0, int take = -1)
        {
            int matchCount = 0;

            ContainerEntry lastRun    = ContainerEntry.Empty;
            ContainerEntry lastResult = ContainerEntry.Empty;

            while (!wordMatches.Done)
            {
                int count = wordMatches.Page(ref _termPositions);
                for (int i = 0; i < count; ++i)
                {
                    long position = _termPositions[i];

                    // If this position isn't in the last run's results array, find the run which contains it
                    if (!lastRun.Contains(position))
                    {
                        // Close previous run, if any
                        if (!lastRun.IsEmpty())
                        {
                            WriteRunSubsetEnd(writer);
                        }

                        // Find containing run
                        lastRun = FindContainerAtDepth(position, _runDepth);

                        // Write run subset
                        _bionReader.Seek(lastRun.StartByteOffset);
                        WriteRunSubsetStart(writer);
                    }

                    // Find and write result
                    ContainerEntry result = FindContainerAtDepth(position, _runDepth + 2);
                    if (!result.IsEmpty() && !lastResult.Equals(result))
                    {
                        matchCount++;
                        if (matchCount <= skip)
                        {
                            continue;
                        }

                        _bionReader.Seek(result.StartByteOffset);
                        JsonBionConverter.BionToJson(_bionReader, writer);

                        if (take >= 0 && matchCount >= skip + take)
                        {
                            break;
                        }
                    }
                }

                if (take >= 0 && matchCount >= skip + take)
                {
                    break;
                }
            }

            // Close last run, if any
            if (!lastRun.IsEmpty())
            {
                WriteRunSubsetEnd(writer);
            }

            return(matchCount);
        }