Example usage for org.apache.lucene.util BytesRef clone

List of usage examples for org.apache.lucene.util BytesRef clone

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef clone.

Prototype

@Override
public BytesRef clone() 

Source Link

Document

Returns a shallow clone of this instance (the underlying bytes are not copied and will be shared by both the returned object and this object.

Usage

From source file:io.datalayer.lucene.frequency.AosTermStats.java

License:Apache License

AosTermStats(String field, BytesRef termtext, int df) {
    this.termtext = termtext.clone();
    this.field = field;
    this.docFreq = df;
}

From source file:io.datalayer.lucene.frequency.AosTermStats.java

License:Apache License

AosTermStats(String field, BytesRef termtext, int df, long tf) {
    this.termtext = termtext.clone();
    this.field = field;
    this.docFreq = df;
    this.totalTermFreq = tf;
}

From source file:jp.sf.fess.solr.plugin.analysis.synonym.NGramSynonymTokenizer.java

License:Apache License

void consultDictionary() throws IOException {
    if (map == null) {
        return;//w  w w. ja va 2s. com
    }
    synonyms.clear();
    final char[] key = block.toString().toCharArray();
    for (int start = 0; start < block.length();) {
        final BytesRef matchOutput = getLongestMatchOutput(key, start);
        if (matchOutput == null) {
            start++;
            continue;
        }

        synonyms.add(new MyToken(key, start, longestMatchEndOffset, 1, matchOutput.clone(), ignoreCase));
        start = longestMatchEndOffset;
    }
}

From source file:org.codelibs.elasticsearch.common.bytes.BytesReference.java

License:Apache License

/**
 * Compares the two references using the given int function.
 *//*ww  w .j  a  va  2s . c  o m*/
private static int compareIterators(final BytesReference a, final BytesReference b,
        final ToIntBiFunction<BytesRef, BytesRef> f) {
    try {
        // we use the iterators since it's a 0-copy comparison where possible!
        final long lengthToCompare = Math.min(a.length(), b.length());
        final BytesRefIterator aIter = a.iterator();
        final BytesRefIterator bIter = b.iterator();
        BytesRef aRef = aIter.next();
        BytesRef bRef = bIter.next();
        if (aRef != null && bRef != null) { // do we have any data?
            aRef = aRef.clone(); // we clone since we modify the offsets and length in the iteration below
            bRef = bRef.clone();
            if (aRef.length == a.length() && bRef.length == b.length()) { // is it only one array slice we are comparing?
                return f.applyAsInt(aRef, bRef);
            } else {
                for (int i = 0; i < lengthToCompare;) {
                    if (aRef.length == 0) {
                        aRef = aIter.next().clone(); // must be non null otherwise we have a bug
                    }
                    if (bRef.length == 0) {
                        bRef = bIter.next().clone(); // must be non null otherwise we have a bug
                    }
                    final int aLength = aRef.length;
                    final int bLength = bRef.length;
                    final int length = Math.min(aLength, bLength); // shrink to the same length and use the fast compare in lucene
                    aRef.length = bRef.length = length;
                    // now we move to the fast comparison - this is the hot part of the loop
                    int diff = f.applyAsInt(aRef, bRef);
                    aRef.length = aLength;
                    bRef.length = bLength;

                    if (diff != 0) {
                        return diff;
                    }
                    advance(aRef, length);
                    advance(bRef, length);
                    i += length;
                }
            }
        }
        // One is a prefix of the other, or, they are equal:
        return a.length() - b.length();
    } catch (IOException ex) {
        throw new AssertionError("can not happen", ex);
    }
}

From source file:org.codelibs.elasticsearch.synonym.analysis.NGramSynonymTokenizer.java

License:Apache License

void consultDictionary() throws IOException {
    if (synonymMap == null)
        return;/*from w w w  .ja v  a 2s.  c om*/
    synonyms.clear();
    char[] key = block.toString().toCharArray();
    for (int start = 0; start < block.length();) {
        BytesRef matchOutput = getLongestMatchOutput(key, start);
        if (matchOutput == null) {
            start++;
            continue;
        }

        synonyms.add(new MyToken(key, start, longestMatchEndOffset, 1, matchOutput.clone(), ignoreCase));
        start = longestMatchEndOffset;
    }
}

From source file:org.elasticsearch.action.allterms.TransportAllTermsShardAction.java

License:Apache License

private void moveIterators(int[] exhausted, List<TermsEnum> termIters, BytesRef lastTerm, ShardId shardId) {

    try {/*  w w  w  .  j a va2  s .com*/
        for (int i = 0; i < termIters.size(); i++) {
            if (exhausted[i] == 1) {
                continue;
            }
            CharsRefBuilder toiString = new CharsRefBuilder();
            toiString.copyUTF8Bytes(lastTerm);
            logger.trace("{} lastTerm is {}", shardId, toiString.toString());
            BytesRef candidate;
            if (termIters.get(i).term().compareTo(lastTerm) == 0) {
                candidate = termIters.get(i).next();

                logger.trace("{} Moving segment {}", shardId, i);
            } else {
                //it must stand on one that is greater so we just get it
                candidate = termIters.get(i).term();
                logger.trace("{} Not moving segment {}", shardId, i);
            }
            if (candidate == null) {
                exhausted[i] = 1;
            } else {
                toiString = new CharsRefBuilder();
                toiString.copyUTF8Bytes(candidate.clone());
                logger.trace("{} Segment is now on {}", shardId, toiString.toString());
            }
        }
    } catch (IOException e) {

    }
}

From source file:org.elasticsearch.common.bytes.AbstractBytesReferenceTestCase.java

License:Apache License

public static BytesRef getSinglePageOrNull(BytesReference ref) throws IOException {
    if (ref.length() > 0) {
        BytesRefIterator iterator = ref.iterator();
        BytesRef next = iterator.next();
        BytesRef retVal = next.clone();
        if (iterator.next() == null) {
            return retVal;
        }/*from   ww w  .  j a v a 2  s .c  o m*/
    } else {
        return new BytesRef();
    }
    return null;
}

From source file:org.eu.bitzone.Leia.java

License:Apache License

public void showPositions(final Object fText) {
    final Term t = (Term) getProperty(fText, "term");
    if (t == null) {
        return;//w  w  w.j av a 2s  . c om
    }
    if (ir == null) {
        showStatus(MSG_NOINDEX);
        return;
    }
    if (ar == null) {
        errorMsg(MSG_LUCENE3828);
        return;
    }
    final SlowThread st = new SlowThread(this) {

        @Override
        public void execute() {

            try {
                final FieldInfo fi = infos.fieldInfo(t.field());
                boolean withOffsets = false;
                if (fi != null) {
                    final IndexOptions opts = fi.getIndexOptions();
                    if (opts != null) {
                        if (opts.equals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)) {
                            withOffsets = true;
                        }
                    }
                }
                final DocsEnum tdd = (DocsEnum) getProperty(fText, "td");
                if (tdd == null) {
                    showStatus("Unknown document number.");
                    return;
                }
                int flags = DocsAndPositionsEnum.FLAG_PAYLOADS;
                if (withOffsets) {
                    flags |= DocsAndPositionsEnum.FLAG_OFFSETS;
                }

                final DocsAndPositionsEnum td = ar.termPositionsEnum(new Term(t.field(), t.bytes()));

                if (td == null) {
                    showStatus("No position information available for this term.");
                    return;
                }
                if (td.advance(tdd.docID()) != td.docID()) {
                    showStatus("No position available for this doc and this term.");
                    return;
                }
                final Object dialog = addComponent(null, "/xml/positions.xml", null, null);
                setString(find(dialog, "term"), "text", t.toString());
                final String docNum = getString(find("docNum"), "text");
                setString(find(dialog, "docNum"), "text", docNum);
                setString(find(dialog, "freq"), "text", String.valueOf(td.freq()));
                setString(find(dialog, "offs"), "text", String.valueOf(withOffsets));
                putProperty(dialog, "td", td);
                final Object pTable = find(dialog, "pTable");
                removeAll(pTable);
                final int freq = td.freq();
                for (int i = 0; i < freq; i++) {
                    try {
                        final int pos = td.nextPosition();
                        final Object r = create("row");
                        Object cell = create("cell");
                        setString(cell, "text", String.valueOf(pos));
                        add(r, cell);
                        cell = create("cell");
                        add(r, cell);
                        if (withOffsets) {
                            setString(cell, "text", td.startOffset() + "-" + td.endOffset());
                        } else {
                            setString(cell, "text", "---");
                            setBoolean(cell, "enabled", false);
                        }
                        cell = create("cell");
                        add(r, cell);
                        final BytesRef payload = td.getPayload();
                        if (payload != null) {
                            putProperty(r, "payload", payload.clone());
                        }
                        add(pTable, r);
                    } catch (final IOException ioe) {
                        errorMsg("Error: " + ioe.toString());
                        return;
                    }
                }
                _showPayloads(dialog);
                add(dialog);
            } catch (final Exception e) {
                e.printStackTrace();
                showStatus(e.getMessage());
            }
        }
    };
    if (slowAccess) {
        st.start();
    } else {
        st.execute();
    }
}

From source file:org.getopt.luke.Luke.java

License:Apache License

public void showPositions(final Object fText) {
    final Term t = (Term) getProperty(fText, "term");
    if (t == null)
        return;/*from   w  ww  . j  av a 2 s .  c  om*/
    if (ir == null) {
        showStatus(MSG_NOINDEX);
        return;
    }
    if (ar == null) {
        errorMsg(MSG_LUCENE3828);
        return;
    }
    SlowThread st = new SlowThread(this) {
        public void execute() {

            try {
                FieldInfo fi = infos.fieldInfo(t.field());
                boolean withOffsets = false;
                if (fi != null) {
                    IndexOptions opts = fi.getIndexOptions();
                    if (opts != null) {
                        if (opts.equals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)) {
                            withOffsets = true;
                        }
                    }
                }
                DocsEnum tdd = (DocsEnum) getProperty(fText, "td");
                if (tdd == null) {
                    showStatus("Unknown document number.");
                    return;
                }

                /* not used
                int flags = DocsAndPositionsEnum.FLAG_PAYLOADS;
                if (withOffsets) {
                  flags |= DocsAndPositionsEnum.FLAG_OFFSETS;
                }
                */
                DocsAndPositionsEnum td = ar.getContext().reader().termPositionsEnum(t);
                if (td == null) {
                    showStatus("No position information available for this term.");
                    return;
                }
                if (td.advance(tdd.docID()) != td.docID()) {
                    showStatus("No position available for this doc and this term.");
                    return;
                }
                Object dialog = addComponent(null, "/xml/positions.xml", null, null);
                setString(find(dialog, "term"), "text", t.toString());
                String docNum = getString(find("docNum"), "text");
                setString(find(dialog, "docNum"), "text", docNum);
                setString(find(dialog, "freq"), "text", String.valueOf(td.freq()));
                setString(find(dialog, "offs"), "text", String.valueOf(withOffsets));
                putProperty(dialog, "td", td);
                Object pTable = find(dialog, "pTable");
                removeAll(pTable);
                int freq = td.freq();
                for (int i = 0; i < freq; i++) {
                    try {
                        int pos = td.nextPosition();
                        Object r = create("row");
                        Object cell = create("cell");
                        setString(cell, "text", String.valueOf(pos));
                        add(r, cell);
                        cell = create("cell");
                        add(r, cell);
                        if (withOffsets) {
                            setString(cell, "text", td.startOffset() + "-" + td.endOffset());
                        } else {
                            setString(cell, "text", "---");
                            setBoolean(cell, "enabled", false);
                        }
                        cell = create("cell");
                        add(r, cell);
                        BytesRef payload = td.getPayload();
                        if (payload != null) {
                            putProperty(r, "payload", payload.clone());
                        }
                        add(pTable, r);
                    } catch (IOException ioe) {
                        errorMsg("Error: " + ioe.toString());
                        return;
                    }
                }
                _showPayloads(dialog);
                add(dialog);
            } catch (Exception e) {
                e.printStackTrace();
                showStatus(e.getMessage());
            }
        }
    };
    if (slowAccess) {
        st.start();
    } else {
        st.execute();
    }
}

From source file:org.getopt.luke.TermStats.java

License:Apache License

TermStats(String field, BytesRef termtext, int df) {
    this.termtext = (BytesRef) termtext.clone();
    this.field = field;
    this.docFreq = df;
}