Example usage for org.apache.lucene.util.mutable MutableValueInt MutableValueInt

List of usage examples for org.apache.lucene.util.mutable MutableValueInt MutableValueInt

Introduction

In this page you can find the example usage for org.apache.lucene.util.mutable MutableValueInt MutableValueInt.

Prototype

MutableValueInt

Source Link

Usage

From source file:org.apache.solr.CursorPagingTest.java

License:Apache License

/**
 * Given a set of params, executes a cursor query using {@link #CURSOR_MARK_START}
 * and then continuously walks the results using {@link #CURSOR_MARK_START} as long
 * as a non-0 number of docs ar returned.  This method records the the set of all id's
 * (must be positive ints) encountered and throws an assertion failure if any id is
 * encountered more than once, or if the set grows above maxSize.
 *
 * Also checks that facets are the same with each page, and that they are correct.
 *///from   www .  j  ava2 s. c  o m
public SentinelIntSet assertFullWalkNoDupsWithFacets(int maxSize, SolrParams params) throws Exception {

    final String facetField = params.get("facet.field");
    assertNotNull("facet.field param not specified", facetField);
    assertFalse("facet.field param contains multiple values", facetField.contains(","));
    assertEquals("facet.limit param not set to -1", "-1", params.get("facet.limit"));
    final Map<String, MutableValueInt> facetCounts = new HashMap<>();
    SentinelIntSet ids = new SentinelIntSet(maxSize, -1);
    String cursorMark = CURSOR_MARK_START;
    int docsOnThisPage = Integer.MAX_VALUE;
    List previousFacets = null;
    while (0 < docsOnThisPage) {
        String json = assertJQ(req(params, CURSOR_MARK_PARAM, cursorMark));
        Map rsp = (Map) ObjectBuilder.fromJSON(json);
        assertTrue("response doesn't contain " + CURSOR_MARK_NEXT + ": " + json,
                rsp.containsKey(CURSOR_MARK_NEXT));
        String nextCursorMark = (String) rsp.get(CURSOR_MARK_NEXT);
        assertNotNull(CURSOR_MARK_NEXT + " is null", nextCursorMark);
        List<Map<Object, Object>> docs = (List) (((Map) rsp.get("response")).get("docs"));
        docsOnThisPage = docs.size();
        if (null != params.getInt(CommonParams.ROWS)) {
            int rows = params.getInt(CommonParams.ROWS);
            assertTrue("Too many docs on this page: " + rows + " < " + docsOnThisPage, docsOnThisPage <= rows);
        }
        if (0 == docsOnThisPage) {
            assertEquals("no more docs, but " + CURSOR_MARK_NEXT + " isn't same", cursorMark, nextCursorMark);
        }
        for (Map<Object, Object> doc : docs) {
            int id = ((Long) doc.get("id")).intValue();
            assertFalse("walk already seen: " + id, ids.exists(id));
            ids.put(id);
            assertFalse("id set bigger then max allowed (" + maxSize + "): " + ids.size(),
                    maxSize < ids.size());
            Object facet = doc.get(facetField);
            String facetString = null == facet ? null : facet.toString(); // null: missing facet value
            MutableValueInt count = facetCounts.get(facetString);
            if (null == count) {
                count = new MutableValueInt();
                facetCounts.put(facetString, count);
            }
            ++count.value;
        }
        cursorMark = nextCursorMark;

        Map facetFields = (Map) ((Map) rsp.get("facet_counts")).get("facet_fields");
        List facets = (List) facetFields.get(facetField);
        if (null != previousFacets) {
            assertEquals("Facets not the same as on previous page:\nprevious page facets: "
                    + Arrays.toString(facets.toArray(new Object[facets.size()])) + "\ncurrent page facets: "
                    + Arrays.toString(previousFacets.toArray(new Object[previousFacets.size()])),
                    previousFacets, facets);
        }
        previousFacets = facets;
    }

    assertNotNull("previousFacets is null", previousFacets);
    assertEquals("Mismatch in number of facets: ", facetCounts.size(), previousFacets.size() / 2);
    int pos;
    for (pos = 0; pos < previousFacets.size(); pos += 2) {
        String label = (String) previousFacets.get(pos);
        int expectedCount = ((Number) previousFacets.get(pos + 1)).intValue();
        MutableValueInt count = facetCounts.get(label);
        assertNotNull("Expected facet label #" + (pos / 2) + " not found: '" + label + "'", count);
        assertEquals("Facet count mismatch for label #" + (pos / 2) + " '" + label + "'", expectedCount,
                facetCounts.get(label).value);
        pos += 2;
    }
    return ids;
}

From source file:org.apache.solr.schema.SortableIntField.java

License:Apache License

@Override
public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
    final int def = defVal;

    return new DocTermsIndexDocValues(this, readerContext, field) {
        private final BytesRef spare = new BytesRef();

        @Override//from w w w. j  a  v a2s  . co  m
        protected String toTerm(String readableValue) {
            return NumberUtils.int2sortableStr(readableValue);
        }

        @Override
        public float floatVal(int doc) {
            return (float) intVal(doc);
        }

        @Override
        public boolean exists(int doc) {
            return termsIndex.getOrd(doc) >= 0;
        }

        @Override
        public int intVal(int doc) {
            int ord = termsIndex.getOrd(doc);
            if (ord == -1) {
                return def;
            } else {
                termsIndex.lookupOrd(ord, spare);
                return NumberUtils.SortableStr2int(spare, 0, 3);
            }
        }

        @Override
        public long longVal(int doc) {
            return (long) intVal(doc);
        }

        @Override
        public double doubleVal(int doc) {
            return (double) intVal(doc);
        }

        @Override
        public String strVal(int doc) {
            return Integer.toString(intVal(doc));
        }

        @Override
        public String toString(int doc) {
            return description() + '=' + intVal(doc);
        }

        @Override
        public Object objectVal(int doc) {
            return exists(doc) ? intVal(doc) : null;
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {
                private final MutableValueInt mval = new MutableValueInt();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) {
                    int ord = termsIndex.getOrd(doc);
                    if (ord == -1) {
                        mval.value = def;
                        mval.exists = false;
                    } else {
                        termsIndex.lookupOrd(ord, spare);
                        mval.value = NumberUtils.SortableStr2int(spare, 0, 3);
                        mval.exists = true;
                    }
                }
            };
        }

    };
}

From source file:org.apache.solr.schema.TrieIntField.java

License:Apache License

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {

    return new SortedSetFieldSource(f.getName(), choice) {
        @Override// w ww . j  a v  a 2s  .c  o m
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            SortedSetFieldSource thisAsSortedSetFieldSource = this; // needed for nested anon class ref

            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);

            return new IntDocValues(thisAsSortedSetFieldSource) {
                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException(
                                "docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        lastDocID = docID;
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public int intVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return LegacyNumericUtils.prefixCodedToInt(bytes);
                    } else {
                        return 0;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

                @Override
                public ValueFiller getValueFiller() {
                    return new ValueFiller() {
                        private final MutableValueInt mval = new MutableValueInt();

                        @Override
                        public MutableValue getValue() {
                            return mval;
                        }

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = LegacyNumericUtils.prefixCodedToInt(view.binaryValue());
                            } else {
                                mval.exists = false;
                                mval.value = 0;
                            }
                        }
                    };
                }
            };
        }
    };
}

From source file:org.apache.solr.search.grouping.distributed.command.GroupConverter.java

License:Apache License

static Collection<SearchGroup<MutableValue>> toMutable(SchemaField field,
        Collection<SearchGroup<BytesRef>> values) {
    FieldType fieldType = field.getType();
    List<SearchGroup<MutableValue>> result = new ArrayList<>(values.size());
    for (SearchGroup<BytesRef> original : values) {
        SearchGroup<MutableValue> converted = new SearchGroup<MutableValue>();
        converted.sortValues = original.sortValues; // ?
        TrieField.TrieTypes type = ((TrieField) fieldType).getType();
        final MutableValue v;
        switch (type) {
        case INTEGER:
            MutableValueInt mutableInt = new MutableValueInt();
            if (original.groupValue == null) {
                mutableInt.value = 0;/*  w ww  . j  a  v  a 2 s.c  o m*/
                mutableInt.exists = false;
            } else {
                mutableInt.value = (Integer) fieldType.toObject(field, original.groupValue);
            }
            v = mutableInt;
            break;
        case FLOAT:
            MutableValueFloat mutableFloat = new MutableValueFloat();
            if (original.groupValue == null) {
                mutableFloat.value = 0;
                mutableFloat.exists = false;
            } else {
                mutableFloat.value = (Float) fieldType.toObject(field, original.groupValue);
            }
            v = mutableFloat;
            break;
        case DOUBLE:
            MutableValueDouble mutableDouble = new MutableValueDouble();
            if (original.groupValue == null) {
                mutableDouble.value = 0;
                mutableDouble.exists = false;
            } else {
                mutableDouble.value = (Double) fieldType.toObject(field, original.groupValue);
            }
            v = mutableDouble;
            break;
        case LONG:
            MutableValueLong mutableLong = new MutableValueLong();
            if (original.groupValue == null) {
                mutableLong.value = 0;
                mutableLong.exists = false;
            } else {
                mutableLong.value = (Long) fieldType.toObject(field, original.groupValue);
            }
            v = mutableLong;
            break;
        case DATE:
            MutableValueDate mutableDate = new MutableValueDate();
            if (original.groupValue == null) {
                mutableDate.value = 0;
                mutableDate.exists = false;
            } else {
                mutableDate.value = ((Date) fieldType.toObject(field, original.groupValue)).getTime();
            }
            v = mutableDate;
            break;
        default:
            throw new AssertionError();
        }
        converted.groupValue = v;
        result.add(converted);
    }
    return result;
}

From source file:org.codelibs.elasticsearch.common.collect.CopyOnWriteHashMap.java

License:Apache License

/**
 * Associate <code>key</code> with <code>value</code> and return a new copy
 * of the hash table. The current hash table is not modified.
 *///from ww w. j  a  v a2s  .  co m
public CopyOnWriteHashMap<K, V> copyAndPut(K key, V value) {
    if (key == null) {
        throw new IllegalArgumentException("null keys are not supported");
    }
    if (value == null) {
        throw new IllegalArgumentException("null values are not supported");
    }
    final int hash = key.hashCode();
    final MutableValueInt newValue = new MutableValueInt();
    final InnerNode<K, V> newRoot = root.put(key, hash, TOTAL_HASH_BITS, value, newValue);
    final int newSize = size + newValue.value;
    return new CopyOnWriteHashMap<>(newRoot, newSize);
}

From source file:org.elasticsearch.common.collect.CopyOnWriteHashMap.java

License:Apache License

/**
 * Associate <code>key</code> with <code>value</code> and return a new copy
 * of the hash table. The current hash table is not modified.
 *///from  w  w w  .  j  a v a 2  s.  c  o  m
public CopyOnWriteHashMap<K, V> copyAndPut(K key, V value) {
    Preconditions.checkArgument(key != null, "null keys are not supported");
    Preconditions.checkArgument(value != null, "null values are not supported");
    final int hash = key.hashCode();
    final MutableValueInt newValue = new MutableValueInt();
    final InnerNode<K, V> newRoot = root.put(key, hash, TOTAL_HASH_BITS, value, newValue);
    final int newSize = size + newValue.value;
    return new CopyOnWriteHashMap<>(newRoot, newSize);
}

From source file:org.tallison.lucene.contrast.QueryToCorpusContraster.java

License:Apache License

public List<TermIDF> contrast(Query query, String fieldName, int numResults) throws IOException {
    TopScoreDocCollector results = TopScoreDocCollector.create(maxDocs, maxDocs + 10000);
    searcher.search(query, results);/*from   w w  w  .j  a v a 2  s. c  o m*/

    ScoreDoc[] scoreDocs = results.topDocs().scoreDocs;
    //if there are fewer documents than minTermFreq
    //return empty list now
    if (scoreDocs.length < minTermFreq) {
        return new ArrayList<TermIDF>();
    }

    //total hack
    int initialSize = scoreDocs.length * 100;
    CharArrayMap<MutableValueInt> map = new CharArrayMap<MutableValueInt>(initialSize, ignoreCase);
    CharArraySet tmpSet = new CharArraySet(100, ignoreCase);
    Set<String> selector = new HashSet<String>();
    selector.add(fieldName);

    for (ScoreDoc scoreDoc : scoreDocs) {
        //get terms from doc
        processDoc(scoreDoc.doc, fieldName, selector, tmpSet);
        //now update global doc freqs
        Iterator<Object> it = tmpSet.iterator();
        while (it.hasNext()) {
            char[] token = (char[]) it.next();
            MutableValueInt docCount = map.get(token, 0, token.length);
            if (docCount == null) {
                docCount = new MutableValueInt();
                docCount.value = 1;
            } else {
                docCount.value++;
            }
            map.put(token, docCount);
        }
        tmpSet.clear();
    }

    return getResults(fieldName, map, numResults);
}

From source file:org.tallison.lucene.search.concordance.windowvisitor.ConcordanceArrayWindow.java

License:Apache License

/**
 * @return all tokens and their counts from pres, posts and targets
 *//*w  w w  . java 2s  . c  om*/
public Map<String, MutableValueInt> getAllTokens() {

    for (int i = 0; i < pres.size(); i++) {
        String s = pres.get(i);
        if (s.equals(STOP_WORD) || s.equals(FIELD_SEPARATOR)) {
            continue;
        }

        s = unescape(s);
        MutableValueInt mutInt = tokens.get(s);
        if (mutInt == null) {
            mutInt = new MutableValueInt();
            mutInt.value = 0;
        }
        mutInt.value++;
        tokens.put(s, mutInt);
    }

    for (int i = 0; i < targs.size(); i++) {
        String s = targs.get(i);
        if (s.equals(STOP_WORD) || s.equals(FIELD_SEPARATOR)) {
            continue;
        }
        s = unescape(s);

        MutableValueInt mutInt = tokens.get(s);
        if (mutInt == null) {
            mutInt = new MutableValueInt();
            mutInt.value = 0;
        }
        mutInt.value++;
        tokens.put(s, mutInt);
    }

    for (int i = 0; i < posts.size(); i++) {
        String s = posts.get(i);
        if (s.equals(STOP_WORD) || s.equals(FIELD_SEPARATOR)) {
            continue;
        }
        s = unescape(s);

        MutableValueInt mutInt = tokens.get(s);
        if (mutInt == null) {
            mutInt = new MutableValueInt();
            mutInt.value = 0;
        }
        mutInt.value++;
        tokens.put(s, mutInt);
    }
    return tokens;
}

From source file:org.tallison.lucene.search.concordance.windowvisitor.CooccurVisitor.java

License:Apache License

@Override
public void visit(String docId, ConcordanceArrayWindow window) throws IOException {

    if (getNumWindowsVisited() >= getMaxWindows()) {
        setHitMax(true);/*from   w w  w.  j av  a  2s  . com*/
        return;
    }

    if (allowDuplicates == false) {
        String key = window.toString();
        if (alreadySeen.contains(key)) {
            return;
        }
        alreadySeen.add(key);
    }

    List<String> tmpGrams = grammer.getGrams(window.getRawPreList(), SPACE);

    tmpGrams.addAll(grammer.getGrams(window.getRawPostList(), SPACE));

    for (String nGram : tmpGrams) {
        MutableValueInt cnt = tfs.get(nGram);
        if (cnt == null) {
            cnt = new MutableValueInt();
            cnt.value = 0;
        }
        cnt.value++;
        tfs.put(nGram, cnt);
    }
    finishedVisit(docId);
}

From source file:org.tallison.lucene.search.concordance.windowvisitor.TargetVisitor.java

License:Apache License

@Override
public void visit(String docId, ConcordanceArrayWindow window) throws IOException {

    if (getNumWindowsVisited() >= getMaxWindows()) {
        setHitMax(true);/*  w  ww.  j  a  v  a2  s  . c om*/
        return;
    }

    //will throw NPE if docId is null
    if (lastDocId != null && !lastDocId.equals(docId)) {
        seenInThisDoc.clear();
    }
    String targ = "";
    StringBuilder sb = new StringBuilder();
    List<String> parts = window.getRawTargList();
    if (parts.size() == 0) {
        targ = "";
    } else {
        sb.append(ConcordanceArrayWindow.tokenToString(parts.get(0)));
        for (int i = 1; i < parts.size(); i++) {
            sb.append(JOINER).append(ConcordanceArrayWindow.tokenToString(parts.get(i)));
        }
        targ = sb.toString();
    }

    MutableValueInt cnt = tf.get(targ);
    if (cnt == null) {
        cnt = new MutableValueInt();
        cnt.value = 1;
    } else {
        cnt.value++;
    }

    tf.put(targ, cnt);

    if (!seenInThisDoc.contains(targ)) {
        cnt = df.get(targ);
        if (cnt == null) {
            cnt = new MutableValueInt();
            cnt.value = 1;
        } else {
            cnt.value++;
        }
        df.put(targ, cnt);
    }
    seenInThisDoc.add(targ);
    lastDocId = docId;
    finishedVisit(docId, true);

}