Example usage for org.apache.lucene.store IndexOutput getFilePointer

List of usage examples for org.apache.lucene.store IndexOutput getFilePointer

Introduction

In this page you can find the example usage for org.apache.lucene.store IndexOutput getFilePointer.

Prototype

public abstract long getFilePointer();

Source Link

Document

Returns the current position in this file, where the next write will occur.

Usage

From source file:com.bah.lucene.BaseDirectoryTestSuite.java

License:Apache License

private Directory getControlDir(final Directory control, final Directory test) {
    return new Directory() {

        @Override/*from   ww  w  . j  av  a 2s .c  om*/
        public Lock makeLock(String name) {
            return control.makeLock(name);
        }

        @Override
        public void clearLock(String name) throws IOException {
            control.clearLock(name);
        }

        @Override
        public void setLockFactory(LockFactory lockFactory) throws IOException {
            control.setLockFactory(lockFactory);
        }

        @Override
        public LockFactory getLockFactory() {
            return control.getLockFactory();
        }

        @Override
        public String getLockID() {
            return control.getLockID();
        }

        @Override
        public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
            control.copy(to, src, dest, context);
        }

        @Override
        public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException {
            return control.createSlicer(name, context);
        }

        @Override
        public IndexOutput createOutput(final String name, IOContext context) throws IOException {
            final IndexOutput testOutput = test.createOutput(name, context);
            final IndexOutput controlOutput = control.createOutput(name, context);
            return new IndexOutput() {

                @Override
                public void flush() throws IOException {
                    testOutput.flush();
                    controlOutput.flush();
                }

                @Override
                public void close() throws IOException {
                    testOutput.close();
                    controlOutput.close();
                }

                @Override
                public long getFilePointer() {
                    long filePointer = testOutput.getFilePointer();
                    long controlFilePointer = controlOutput.getFilePointer();
                    if (controlFilePointer != filePointer) {
                        System.err.println("Output Name [" + name + "] with filePointer [" + filePointer
                                + "] and control filePointer [" + controlFilePointer + "] does not match");
                    }
                    return filePointer;
                }

                @SuppressWarnings("deprecation")
                @Override
                public void seek(long pos) throws IOException {
                    testOutput.seek(pos);
                    controlOutput.seek(pos);
                }

                @Override
                public long length() throws IOException {
                    long length = testOutput.length();
                    long controlLength = controlOutput.length();
                    if (controlLength != length) {
                        System.err.println("Ouput Name [" + name + "] with length [" + length
                                + "] and control length [" + controlLength + "] does not match");
                    }
                    return length;
                }

                @Override
                public void writeByte(byte b) throws IOException {
                    testOutput.writeByte(b);
                    controlOutput.writeByte(b);
                }

                @Override
                public void writeBytes(byte[] b, int offset, int length) throws IOException {
                    testOutput.writeBytes(b, offset, length);
                    controlOutput.writeBytes(b, offset, length);
                }

            };
        }

        @Override
        public IndexInput openInput(final String name, IOContext context) throws IOException {
            final IndexInput testInput = test.openInput(name, context);
            final IndexInput controlInput = control.openInput(name, context);
            return new IndexInputCompare(name, testInput, controlInput);
        }

        @Override
        public String[] listAll() throws IOException {
            return test.listAll();
        }

        @Override
        public boolean fileExists(String name) throws IOException {
            return test.fileExists(name);
        }

        @Override
        public void deleteFile(String name) throws IOException {
            test.deleteFile(name);
            control.deleteFile(name);
        }

        @Override
        public long fileLength(String name) throws IOException {
            long fileLength = test.fileLength(name);
            long controlFileLength = control.fileLength(name);
            if (controlFileLength != fileLength) {
                System.err.println("Input Name [" + name + "] with length [" + fileLength
                        + "] and control length [" + controlFileLength + "] does not match");
            }
            return fileLength;
        }

        @Override
        public void sync(Collection<String> names) throws IOException {
            test.sync(names);
            test.sync(names);
        }

        @Override
        public void close() throws IOException {
            test.close();
            control.close();
        }
    };
}

From source file:com.lucure.core.codec.CompressingStoredFieldsWriter.java

License:Apache License

/** Sole constructor. */
public CompressingStoredFieldsWriter(Directory directory, SegmentInfo si, String segmentSuffix,
        IOContext context, String formatName, CompressionMode compressionMode, int chunkSize)
        throws IOException {
    assert directory != null;
    this.directory = directory;
    this.segment = si.name;
    this.segmentSuffix = segmentSuffix;
    this.compressionMode = compressionMode;
    this.compressor = compressionMode.newCompressor();
    this.chunkSize = chunkSize;
    this.docBase = 0;
    this.bufferedDocs = new GrowableByteArrayDataOutput(chunkSize);
    this.numStoredFields = new int[16];
    this.endOffsets = new int[16];
    this.numBufferedDocs = 0;

    boolean success = false;
    IndexOutput indexStream = directory.createOutput(
            IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_INDEX_EXTENSION), context);
    try {//from   w  w w  .  j  a  va 2  s .co  m
        fieldsStream = directory.createOutput(
                IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_EXTENSION), context);

        final String codecNameIdx = formatName + CODEC_SFX_IDX;
        final String codecNameDat = formatName + CODEC_SFX_DAT;
        CodecUtil.writeHeader(indexStream, codecNameIdx, VERSION_CURRENT);
        CodecUtil.writeHeader(fieldsStream, codecNameDat, VERSION_CURRENT);
        assert CodecUtil.headerLength(codecNameDat) == fieldsStream.getFilePointer();
        assert CodecUtil.headerLength(codecNameIdx) == indexStream.getFilePointer();

        indexWriter = new CompressingStoredFieldsIndexWriter(indexStream);
        indexStream = null;

        fieldsStream.writeVInt(chunkSize);
        fieldsStream.writeVInt(PackedInts.VERSION_CURRENT);

        success = true;
    } finally {
        if (!success) {
            IOUtils.closeWhileHandlingException(indexStream);
            abort();
        }
    }
}

From source file:org.codelibs.elasticsearch.search.suggest.completion2x.AnalyzingCompletionLookupProvider.java

License:Apache License

@Override
public FieldsConsumer consumer(final IndexOutput output) throws IOException {
    CodecUtil.writeHeader(output, CODEC_NAME, CODEC_VERSION_LATEST);
    return new FieldsConsumer() {
        private Map<String, Long> fieldOffsets = new HashMap<>();

        @Override/*w w w  .j  a va  2 s .  co  m*/
        public void close() throws IOException {
            try {
                /*
                 * write the offsets per field such that we know where
                 * we need to load the FSTs from
                 */
                long pointer = output.getFilePointer();
                output.writeVInt(fieldOffsets.size());
                for (Map.Entry<String, Long> entry : fieldOffsets.entrySet()) {
                    output.writeString(entry.getKey());
                    output.writeVLong(entry.getValue());
                }
                output.writeLong(pointer);
                CodecUtil.writeFooter(output);
            } finally {
                IOUtils.close(output);
            }
        }

        @Override
        public void write(Fields fields) throws IOException {
            for (String field : fields) {
                Terms terms = fields.terms(field);
                if (terms == null) {
                    continue;
                }
                terms.iterator();
                new SuggestPayload();
                throw new UnsupportedOperationException("QueryBuilders does not support this operation.");
                //                    final XAnalyzingSuggester.XBuilder builder = new XAnalyzingSuggester.XBuilder(
                //                        maxSurfaceFormsPerAnalyzedForm, hasPayloads, XAnalyzingSuggester.PAYLOAD_SEP);
                //                    int docCount = 0;
                //                    while (true) {
                //                        BytesRef term = termsEnum.next();
                //                        if (term == null) {
                //                            break;
                //                        }
                //                        docsEnum = termsEnum.postings(docsEnum, PostingsEnum.PAYLOADS);
                //                        builder.startTerm(term);
                //                        int docFreq = 0;
                //                        while (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                //                            for (int i = 0; i < docsEnum.freq(); i++) {
                //                                final int position = docsEnum.nextPosition();
                //                                AnalyzingCompletionLookupProvider.this.parsePayload(docsEnum.getPayload(), spare);
                //                                builder.addSurface(spare.surfaceForm.get(), spare.payload.get(), spare.weight);
                //                                // multi fields have the same surface form so we sum up here
                //                                maxAnalyzedPathsForOneInput = Math.max(maxAnalyzedPathsForOneInput, position + 1);
                //                            }
                //                            docFreq++;
                //                            docCount = Math.max(docCount, docsEnum.docID()+1);
                //                        }
                //                        builder.finishTerm(docFreq);
                //                    }
                //                    /*
                //                     * Here we are done processing the field and we can
                //                     * buid the FST and write it to disk.
                //                     */
                //                    FST<Pair<Long, BytesRef>> build = builder.build();
                //                    assert build != null || docCount == 0: "the FST is null but docCount is != 0 actual value: [" + docCount + "]";
                //                    /*
                //                     * it's possible that the FST is null if we have 2 segments that get merged
                //                     * and all docs that have a value in this field are deleted. This will cause
                //                     * a consumer to be created but it doesn't consume any values causing the FSTBuilder
                //                     * to return null.
                //                     */
                //                    if (build != null) {
                //                        fieldOffsets.put(field, output.getFilePointer());
                //                        build.save(output);
                //                        /* write some more meta-info */
                //                        output.writeVInt(maxAnalyzedPathsForOneInput);
                //                        output.writeVInt(maxSurfaceFormsPerAnalyzedForm);
                //                        output.writeInt(maxGraphExpansions); // can be negative
                //                        int options = 0;
                //                        options |= preserveSep ? SERIALIZE_PRESERVE_SEPARATORS : 0;
                //                        options |= hasPayloads ? SERIALIZE_HAS_PAYLOADS : 0;
                //                        options |= preservePositionIncrements ? SERIALIZE_PRESERVE_POSITION_INCREMENTS : 0;
                //                        output.writeVInt(options);
                //                        output.writeVInt(XAnalyzingSuggester.SEP_LABEL);
                //                        output.writeVInt(XAnalyzingSuggester.END_BYTE);
                //                        output.writeVInt(XAnalyzingSuggester.PAYLOAD_SEP);
                //                        output.writeVInt(XAnalyzingSuggester.HOLE_CHARACTER);
                //                    }
            }
        }
    };
}

From source file:org.elasticsearch.common.compress.CompressedIndexOutput.java

License:Apache License

public CompressedIndexOutput(IndexOutput out, T context) throws IOException {
    this.out = out;
    this.context = context;
    writeHeader(out);//from w  w w. ja va 2s .co m
    out.writeInt(0); // version
    metaDataPointer = out.getFilePointer();
    out.writeLong(-1); // the pointer to the end of the file metadata
}

From source file:org.elasticsearch.search.suggest.completion.AnalyzingCompletionLookupProvider.java

License:Apache License

@Override
public FieldsConsumer consumer(final IndexOutput output) throws IOException {
    CodecUtil.writeHeader(output, CODEC_NAME, CODEC_VERSION_LATEST);
    return new FieldsConsumer() {
        private Map<FieldInfo, Long> fieldOffsets = new HashMap<FieldInfo, Long>();

        @Override/*from  w  w  w  . j av a  2s  .  c  o  m*/
        public void close() throws IOException {
            try { /*
                   * write the offsets per field such that we know where
                   * we need to load the FSTs from
                   */
                long pointer = output.getFilePointer();
                output.writeVInt(fieldOffsets.size());
                for (Map.Entry<FieldInfo, Long> entry : fieldOffsets.entrySet()) {
                    output.writeString(entry.getKey().name);
                    output.writeVLong(entry.getValue());
                }
                output.writeLong(pointer);
                output.flush();
            } finally {
                IOUtils.close(output);
            }
        }

        @Override
        public TermsConsumer addField(final FieldInfo field) throws IOException {

            return new TermsConsumer() {
                final XAnalyzingSuggester.XBuilder builder = new XAnalyzingSuggester.XBuilder(
                        maxSurfaceFormsPerAnalyzedForm, hasPayloads, XAnalyzingSuggester.PAYLOAD_SEP);
                final CompletionPostingsConsumer postingsConsumer = new CompletionPostingsConsumer(
                        AnalyzingCompletionLookupProvider.this, builder);

                @Override
                public PostingsConsumer startTerm(BytesRef text) throws IOException {
                    builder.startTerm(text);
                    return postingsConsumer;
                }

                @Override
                public Comparator<BytesRef> getComparator() throws IOException {
                    return BytesRef.getUTF8SortedAsUnicodeComparator();
                }

                @Override
                public void finishTerm(BytesRef text, TermStats stats) throws IOException {
                    builder.finishTerm(stats.docFreq); // use  doc freq as a fallback
                }

                @Override
                public void finish(long sumTotalTermFreq, long sumDocFreq, int docCount) throws IOException {
                    /*
                     * Here we are done processing the field and we can
                     * buid the FST and write it to disk.
                     */
                    FST<Pair<Long, BytesRef>> build = builder.build();
                    assert build != null
                            || docCount == 0 : "the FST is null but docCount is != 0 actual value: [" + docCount
                                    + "]";
                    /*
                     * it's possible that the FST is null if we have 2 segments that get merged
                     * and all docs that have a value in this field are deleted. This will cause
                     * a consumer to be created but it doesn't consume any values causing the FSTBuilder
                     * to return null.
                     */
                    if (build != null) {
                        fieldOffsets.put(field, output.getFilePointer());
                        build.save(output);
                        /* write some more meta-info */
                        output.writeVInt(postingsConsumer.getMaxAnalyzedPathsForOneInput());
                        output.writeVInt(maxSurfaceFormsPerAnalyzedForm);
                        output.writeInt(maxGraphExpansions); // can be negative
                        int options = 0;
                        options |= preserveSep ? SERIALIZE_PRESERVE_SEPERATORS : 0;
                        options |= hasPayloads ? SERIALIZE_HAS_PAYLOADS : 0;
                        options |= preservePositionIncrements ? SERIALIZE_PRESERVE_POSITION_INCREMENTS : 0;
                        output.writeVInt(options);
                        output.writeVInt(XAnalyzingSuggester.SEP_LABEL);
                        output.writeVInt(XAnalyzingSuggester.END_BYTE);
                        output.writeVInt(XAnalyzingSuggester.PAYLOAD_SEP);
                        output.writeVInt(XAnalyzingSuggester.HOLE_CHARACTER);
                    }
                }
            };
        }
    };
}

From source file:org.elasticsearch.search.suggest.completion.AnalyzingCompletionLookupProviderV1.java

License:Apache License

@Override
public FieldsConsumer consumer(final IndexOutput output) throws IOException {
    CodecUtil.writeHeader(output, CODEC_NAME, CODEC_VERSION);
    return new FieldsConsumer() {
        private Map<FieldInfo, Long> fieldOffsets = new HashMap<FieldInfo, Long>();

        @Override//  w w w  .j a  v a2 s.  c  om
        public void close() throws IOException {
            try { /*
                   * write the offsets per field such that we know where
                   * we need to load the FSTs from
                   */
                long pointer = output.getFilePointer();
                output.writeVInt(fieldOffsets.size());
                for (Map.Entry<FieldInfo, Long> entry : fieldOffsets.entrySet()) {
                    output.writeString(entry.getKey().name);
                    output.writeVLong(entry.getValue());
                }
                output.writeLong(pointer);
                output.flush();
            } finally {
                IOUtils.close(output);
            }
        }

        @Override
        public TermsConsumer addField(final FieldInfo field) throws IOException {

            return new TermsConsumer() {
                final XAnalyzingSuggester.XBuilder builder = new XAnalyzingSuggester.XBuilder(
                        maxSurfaceFormsPerAnalyzedForm, hasPayloads, PAYLOAD_SEP);
                final CompletionPostingsConsumer postingsConsumer = new CompletionPostingsConsumer(
                        AnalyzingCompletionLookupProviderV1.this, builder);

                @Override
                public PostingsConsumer startTerm(BytesRef text) throws IOException {
                    builder.startTerm(text);
                    return postingsConsumer;
                }

                @Override
                public Comparator<BytesRef> getComparator() throws IOException {
                    return BytesRef.getUTF8SortedAsUnicodeComparator();
                }

                @Override
                public void finishTerm(BytesRef text, TermStats stats) throws IOException {
                    builder.finishTerm(stats.docFreq); // use  doc freq as a fallback
                }

                @Override
                public void finish(long sumTotalTermFreq, long sumDocFreq, int docCount) throws IOException {
                    /*
                     * Here we are done processing the field and we can
                     * buid the FST and write it to disk.
                     */
                    FST<Pair<Long, BytesRef>> build = builder.build();
                    assert build != null
                            || docCount == 0 : "the FST is null but docCount is != 0 actual value: [" + docCount
                                    + "]";
                    /*
                     * it's possible that the FST is null if we have 2 segments that get merged
                     * and all docs that have a value in this field are deleted. This will cause
                     * a consumer to be created but it doesn't consume any values causing the FSTBuilder
                     * to return null.
                     */
                    if (build != null) {
                        fieldOffsets.put(field, output.getFilePointer());
                        build.save(output);
                        /* write some more meta-info */
                        output.writeVInt(postingsConsumer.getMaxAnalyzedPathsForOneInput());
                        output.writeVInt(maxSurfaceFormsPerAnalyzedForm);
                        output.writeInt(maxGraphExpansions); // can be negative
                        int options = 0;
                        options |= preserveSep ? SERIALIZE_PRESERVE_SEPERATORS : 0;
                        options |= hasPayloads ? SERIALIZE_HAS_PAYLOADS : 0;
                        options |= preservePositionIncrements ? SERIALIZE_PRESERVE_POSITION_INCREMENTS : 0;
                        output.writeVInt(options);
                    }
                }
            };
        }
    };
}

From source file:org.elasticsearch.search.suggest.completion.old.AnalyzingCompletionLookupProvider.java

License:Apache License

@Override
public FieldsConsumer consumer(final IndexOutput output) throws IOException {
    CodecUtil.writeHeader(output, CODEC_NAME, CODEC_VERSION_LATEST);
    return new FieldsConsumer() {
        private Map<String, Long> fieldOffsets = new HashMap<>();

        @Override/* w  w w .ja  va  2 s  .com*/
        public void close() throws IOException {
            try {
                /*
                 * write the offsets per field such that we know where
                 * we need to load the FSTs from
                 */
                long pointer = output.getFilePointer();
                output.writeVInt(fieldOffsets.size());
                for (Map.Entry<String, Long> entry : fieldOffsets.entrySet()) {
                    output.writeString(entry.getKey());
                    output.writeVLong(entry.getValue());
                }
                output.writeLong(pointer);
                CodecUtil.writeFooter(output);
            } finally {
                IOUtils.close(output);
            }
        }

        @Override
        public void write(Fields fields) throws IOException {
            for (String field : fields) {
                Terms terms = fields.terms(field);
                if (terms == null) {
                    continue;
                }
                TermsEnum termsEnum = terms.iterator();
                PostingsEnum docsEnum = null;
                final SuggestPayload spare = new SuggestPayload();
                int maxAnalyzedPathsForOneInput = 0;
                final XAnalyzingSuggester.XBuilder builder = new XAnalyzingSuggester.XBuilder(
                        maxSurfaceFormsPerAnalyzedForm, hasPayloads, XAnalyzingSuggester.PAYLOAD_SEP);
                int docCount = 0;
                while (true) {
                    BytesRef term = termsEnum.next();
                    if (term == null) {
                        break;
                    }
                    docsEnum = termsEnum.postings(null, docsEnum, PostingsEnum.PAYLOADS);
                    builder.startTerm(term);
                    int docFreq = 0;
                    while (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        for (int i = 0; i < docsEnum.freq(); i++) {
                            final int position = docsEnum.nextPosition();
                            AnalyzingCompletionLookupProvider.this.parsePayload(docsEnum.getPayload(), spare);
                            builder.addSurface(spare.surfaceForm.get(), spare.payload.get(), spare.weight);
                            // multi fields have the same surface form so we sum up here
                            maxAnalyzedPathsForOneInput = Math.max(maxAnalyzedPathsForOneInput, position + 1);
                        }
                        docFreq++;
                        docCount = Math.max(docCount, docsEnum.docID() + 1);
                    }
                    builder.finishTerm(docFreq);
                }
                /*
                 * Here we are done processing the field and we can
                 * buid the FST and write it to disk.
                 */
                FST<Pair<Long, BytesRef>> build = builder.build();
                assert build != null || docCount == 0 : "the FST is null but docCount is != 0 actual value: ["
                        + docCount + "]";
                /*
                 * it's possible that the FST is null if we have 2 segments that get merged
                 * and all docs that have a value in this field are deleted. This will cause
                 * a consumer to be created but it doesn't consume any values causing the FSTBuilder
                 * to return null.
                 */
                if (build != null) {
                    fieldOffsets.put(field, output.getFilePointer());
                    build.save(output);
                    /* write some more meta-info */
                    output.writeVInt(maxAnalyzedPathsForOneInput);
                    output.writeVInt(maxSurfaceFormsPerAnalyzedForm);
                    output.writeInt(maxGraphExpansions); // can be negative
                    int options = 0;
                    options |= preserveSep ? SERIALIZE_PRESERVE_SEPARATORS : 0;
                    options |= hasPayloads ? SERIALIZE_HAS_PAYLOADS : 0;
                    options |= preservePositionIncrements ? SERIALIZE_PRESERVE_POSITION_INCREMENTS : 0;
                    output.writeVInt(options);
                    output.writeVInt(XAnalyzingSuggester.SEP_LABEL);
                    output.writeVInt(XAnalyzingSuggester.END_BYTE);
                    output.writeVInt(XAnalyzingSuggester.PAYLOAD_SEP);
                    output.writeVInt(XAnalyzingSuggester.HOLE_CHARACTER);
                }
            }
        }
    };
}

From source file:org.elasticsearch.search.suggest.completion.old.AnalyzingCompletionLookupProviderV1.java

License:Apache License

@Override
public FieldsConsumer consumer(final IndexOutput output) throws IOException {
    // TODO write index header?
    CodecUtil.writeHeader(output, CODEC_NAME, CODEC_VERSION);
    return new FieldsConsumer() {
        private Map<String, Long> fieldOffsets = new HashMap<>();

        @Override/*from  ww w  .  j a  v a 2  s.c  om*/
        public void close() throws IOException {
            try { /*
                   * write the offsets per field such that we know where
                   * we need to load the FSTs from
                   */
                long pointer = output.getFilePointer();
                output.writeVInt(fieldOffsets.size());
                for (Map.Entry<String, Long> entry : fieldOffsets.entrySet()) {
                    output.writeString(entry.getKey());
                    output.writeVLong(entry.getValue());
                }
                output.writeLong(pointer);
            } finally {
                IOUtils.close(output);
            }
        }

        @Override
        public void write(Fields fields) throws IOException {
            for (String field : fields) {
                Terms terms = fields.terms(field);
                if (terms == null) {
                    continue;
                }
                TermsEnum termsEnum = terms.iterator();
                PostingsEnum docsEnum = null;
                final SuggestPayload spare = new SuggestPayload();
                int maxAnalyzedPathsForOneInput = 0;
                final XAnalyzingSuggester.XBuilder builder = new XAnalyzingSuggester.XBuilder(
                        maxSurfaceFormsPerAnalyzedForm, hasPayloads, XAnalyzingSuggester.PAYLOAD_SEP);
                int docCount = 0;
                while (true) {
                    BytesRef term = termsEnum.next();
                    if (term == null) {
                        break;
                    }
                    docsEnum = termsEnum.postings(null, docsEnum, PostingsEnum.PAYLOADS);
                    builder.startTerm(term);
                    int docFreq = 0;
                    while (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        for (int i = 0; i < docsEnum.freq(); i++) {
                            final int position = docsEnum.nextPosition();
                            AnalyzingCompletionLookupProviderV1.this.parsePayload(docsEnum.getPayload(), spare);
                            builder.addSurface(spare.surfaceForm.get(), spare.payload.get(), spare.weight);
                            // multi fields have the same surface form so we sum up here
                            maxAnalyzedPathsForOneInput = Math.max(maxAnalyzedPathsForOneInput, position + 1);
                        }
                        docFreq++;
                        docCount = Math.max(docCount, docsEnum.docID() + 1);
                    }
                    builder.finishTerm(docFreq);
                }
                /*
                 * Here we are done processing the field and we can
                 * buid the FST and write it to disk.
                 */
                FST<Pair<Long, BytesRef>> build = builder.build();
                assert build != null || docCount == 0 : "the FST is null but docCount is != 0 actual value: ["
                        + docCount + "]";
                /*
                 * it's possible that the FST is null if we have 2 segments that get merged
                 * and all docs that have a value in this field are deleted. This will cause
                 * a consumer to be created but it doesn't consume any values causing the FSTBuilder
                 * to return null.
                 */
                if (build != null) {
                    fieldOffsets.put(field, output.getFilePointer());
                    build.save(output);
                    /* write some more meta-info */
                    output.writeVInt(maxAnalyzedPathsForOneInput);
                    output.writeVInt(maxSurfaceFormsPerAnalyzedForm);
                    output.writeInt(maxGraphExpansions); // can be negative
                    int options = 0;
                    options |= preserveSep ? SERIALIZE_PRESERVE_SEPARATORS : 0;
                    options |= hasPayloads ? SERIALIZE_HAS_PAYLOADS : 0;
                    options |= preservePositionIncrements ? SERIALIZE_PRESERVE_POSITION_INCREMENTS : 0;
                    output.writeVInt(options);
                }
            }
        }
    };
}

From source file:org.elasticsearch.search.suggest.completion2x.AnalyzingCompletionLookupProvider.java

License:Apache License

@Override
public FieldsConsumer consumer(final IndexOutput output) throws IOException {
    CodecUtil.writeHeader(output, CODEC_NAME, CODEC_VERSION_LATEST);
    return new FieldsConsumer() {
        private Map<String, Long> fieldOffsets = new HashMap<>();

        @Override//from w ww.ja va  2s  .  c  o  m
        public void close() throws IOException {
            try {
                /*
                 * write the offsets per field such that we know where
                 * we need to load the FSTs from
                 */
                long pointer = output.getFilePointer();
                output.writeVInt(fieldOffsets.size());
                for (Map.Entry<String, Long> entry : fieldOffsets.entrySet()) {
                    output.writeString(entry.getKey());
                    output.writeVLong(entry.getValue());
                }
                output.writeLong(pointer);
                CodecUtil.writeFooter(output);
            } finally {
                IOUtils.close(output);
            }
        }

        @Override
        public void write(Fields fields) throws IOException {
            for (String field : fields) {
                Terms terms = fields.terms(field);
                if (terms == null) {
                    continue;
                }
                TermsEnum termsEnum = terms.iterator();
                PostingsEnum docsEnum = null;
                final SuggestPayload spare = new SuggestPayload();
                int maxAnalyzedPathsForOneInput = 0;
                final XAnalyzingSuggester.XBuilder builder = new XAnalyzingSuggester.XBuilder(
                        maxSurfaceFormsPerAnalyzedForm, hasPayloads, XAnalyzingSuggester.PAYLOAD_SEP);
                int docCount = 0;
                while (true) {
                    BytesRef term = termsEnum.next();
                    if (term == null) {
                        break;
                    }
                    docsEnum = termsEnum.postings(docsEnum, PostingsEnum.PAYLOADS);
                    builder.startTerm(term);
                    int docFreq = 0;
                    while (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        for (int i = 0; i < docsEnum.freq(); i++) {
                            final int position = docsEnum.nextPosition();
                            AnalyzingCompletionLookupProvider.this.parsePayload(docsEnum.getPayload(), spare);
                            builder.addSurface(spare.surfaceForm.get(), spare.payload.get(), spare.weight);
                            // multi fields have the same surface form so we sum up here
                            maxAnalyzedPathsForOneInput = Math.max(maxAnalyzedPathsForOneInput, position + 1);
                        }
                        docFreq++;
                        docCount = Math.max(docCount, docsEnum.docID() + 1);
                    }
                    builder.finishTerm(docFreq);
                }
                /*
                 * Here we are done processing the field and we can
                 * buid the FST and write it to disk.
                 */
                FST<Pair<Long, BytesRef>> build = builder.build();
                assert build != null || docCount == 0 : "the FST is null but docCount is != 0 actual value: ["
                        + docCount + "]";
                /*
                 * it's possible that the FST is null if we have 2 segments that get merged
                 * and all docs that have a value in this field are deleted. This will cause
                 * a consumer to be created but it doesn't consume any values causing the FSTBuilder
                 * to return null.
                 */
                if (build != null) {
                    fieldOffsets.put(field, output.getFilePointer());
                    build.save(output);
                    /* write some more meta-info */
                    output.writeVInt(maxAnalyzedPathsForOneInput);
                    output.writeVInt(maxSurfaceFormsPerAnalyzedForm);
                    output.writeInt(maxGraphExpansions); // can be negative
                    int options = 0;
                    options |= preserveSep ? SERIALIZE_PRESERVE_SEPARATORS : 0;
                    options |= hasPayloads ? SERIALIZE_HAS_PAYLOADS : 0;
                    options |= preservePositionIncrements ? SERIALIZE_PRESERVE_POSITION_INCREMENTS : 0;
                    output.writeVInt(options);
                    output.writeVInt(XAnalyzingSuggester.SEP_LABEL);
                    output.writeVInt(XAnalyzingSuggester.END_BYTE);
                    output.writeVInt(XAnalyzingSuggester.PAYLOAD_SEP);
                    output.writeVInt(XAnalyzingSuggester.HOLE_CHARACTER);
                }
            }
        }
    };
}

From source file:org.elasticsearch.test.unit.common.compress.CompressIndexInputOutputTests.java

License:Apache License

private void simple(Compressor compressor) throws Exception {
    Directory dir = new RAMDirectory();
    IndexOutput out = compressor.indexOutput(dir.createOutput("test", IOContext.DEFAULT));
    long pos1 = out.getFilePointer();
    out.writeInt(1);//  w w  w  .  j a  v a2  s .c  o m
    long pos2 = out.getFilePointer();
    out.writeString("test1");
    long pos3 = out.getFilePointer();
    String largeString = RandomStringGenerator.random(0xFFFF + 5);
    out.writeString(largeString);
    long pos4 = out.getFilePointer();
    out.writeInt(2);
    long pos5 = out.getFilePointer();
    out.writeString("test2");
    out.close();

    IndexInput in = compressor.indexInput(dir.openInput("test", IOContext.DEFAULT));
    assertThat(in.readInt(), equalTo(1));
    assertThat(in.readString(), equalTo("test1"));
    assertThat(in.readString(), equalTo(largeString));
    assertThat(in.readInt(), equalTo(2));
    assertThat(in.readString(), equalTo("test2"));

    in.seek(pos3);
    assertThat(in.readString(), equalTo(largeString));
    in.seek(pos2);
    assertThat(in.readString(), equalTo("test1"));
    in.seek(pos5);
    assertThat(in.readString(), equalTo("test2"));
    in.seek(pos1);
    assertThat(in.readInt(), equalTo(1));

    in.seek(0);
    byte[] full = new byte[(int) in.length()];
    in.readBytes(full, 0, full.length);

    in.close();
}