示例#1
0
        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);
        }