Search in sources :

Example 6 with UnitNRShape

use of org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape in project lucene-solr by apache.

the class DateRangePrefixTreeTest method roundTrip.

private void roundTrip(Calendar calOrig) throws ParseException {
    Calendar cal = (Calendar) calOrig.clone();
    String lastString = null;
    while (true) {
        String calString;
        {
            Calendar preToStringCalClone = (Calendar) cal.clone();
            calString = tree.toString(cal);
            //ensure toString doesn't modify cal state
            assertEquals(preToStringCalClone, cal);
        }
        //test parseCalendar
        assertEquals(cal, tree.parseCalendar(calString));
        //to Shape and back to Cal
        UnitNRShape shape = tree.toShape(cal);
        Calendar cal2 = tree.toCalendar(shape);
        assertEquals(calString, tree.toString(cal2));
        if (!calString.equals("*")) {
            //not world cell
            //to Term and back to Cell
            Cell cell = (Cell) shape;
            BytesRef term = cell.getTokenBytesNoLeaf(null);
            Cell cell2 = tree.readCell(BytesRef.deepCopyOf(term), null);
            assertEquals(calString, cell, cell2);
            Calendar cal3 = tree.toCalendar((UnitNRShape) cell2.getShape());
            assertEquals(calString, tree.toString(cal3));
            // setLeaf comparison
            cell2.setLeaf();
            BytesRef termLeaf = cell2.getTokenBytesWithLeaf(null);
            assertTrue(term.compareTo(termLeaf) < 0);
            assertEquals(termLeaf.length, term.length + 1);
            assertEquals(0, termLeaf.bytes[termLeaf.offset + termLeaf.length - 1]);
            assertTrue(cell.isPrefixOf(cell2));
        }
        //end of loop; decide if should loop again with lower precision
        final int calPrecField = tree.getCalPrecisionField(cal);
        if (calPrecField == -1)
            break;
        int fieldIdx = Arrays.binarySearch(CAL_FIELDS, calPrecField);
        assert fieldIdx >= 0;
        int prevPrecField = (fieldIdx == 0 ? -1 : CAL_FIELDS[--fieldIdx]);
        try {
            tree.clearFieldsAfter(cal, prevPrecField);
        } catch (AssertionError e) {
            if (e.getMessage().equals("Calendar underflow"))
                return;
            throw e;
        }
        lastString = calString;
    }
}
Also used : UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 Calendar (java.util.Calendar)4 Cell (org.apache.lucene.spatial.prefix.tree.Cell)2 BytesRef (org.apache.lucene.util.BytesRef)2 Point (org.locationtech.spatial4j.shape.Point)2 Shape (org.locationtech.spatial4j.shape.Shape)2 Repeat (com.carrotsearch.randomizedtesting.annotations.Repeat)1 ArrayList (java.util.ArrayList)1 GregorianCalendar (java.util.GregorianCalendar)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 Facets (org.apache.lucene.spatial.prefix.NumberRangePrefixTreeStrategy.Facets)1 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)1 NumberRangePrefixTree (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree)1 Bits (org.apache.lucene.util.Bits)1 Test (org.junit.Test)1