Search in sources :

Example 6 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class TestParameterConverter method testStringToPolygonType.

public void testStringToPolygonType() throws Exception {
    testOneStringToPolygon("polygon((0 0, 1 0, 1 1, 0 1, 0 0))", new GeographyValue(Collections.singletonList(Arrays.asList(new GeographyPointValue[] { new GeographyPointValue(0, 0), new GeographyPointValue(1, 0), new GeographyPointValue(1, 1), new GeographyPointValue(0, 1), new GeographyPointValue(0, 0) }))));
    GeographyValue geog;
    // The Bermuda Triangle, counter clockwise.
    List<GeographyPointValue> outerLoop = Arrays.asList(new GeographyPointValue(-64.751, 32.305), new GeographyPointValue(-80.437, 25.244), new GeographyPointValue(-66.371, 18.476), new GeographyPointValue(-64.751, 32.305));
    // A triangular hole
    // Note that this needs to be clockwise.
    List<GeographyPointValue> innerLoop = Arrays.asList(new GeographyPointValue(-68.855, 25.361), new GeographyPointValue(-73.381, 28.376), new GeographyPointValue(-68.874, 28.066), new GeographyPointValue(-68.855, 25.361));
    geog = new GeographyValue(Arrays.asList(outerLoop, innerLoop));
    String geogRep = "POLYGON((-64.751 32.305, -80.437 25.244, -66.371 18.476, -64.751 32.305), " + "(-68.855 25.361, -73.381 28.376, -68.874 28.066, -68.855 25.361))";
    testOneStringToPolygon(geogRep, geog);
    // round trip
    geog = new GeographyValue(geogRep);
    testOneStringToPolygon(geogRep, geog);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 7 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class ParameterSet method fromArrayNoCopy.

public static ParameterSet fromArrayNoCopy(Object... params) {
    byte[][][] encodedStringArrays = new byte[params.length][][];
    byte[][] encodedStrings = new byte[params.length][];
    int size = 2;
    for (int ii = 0; ii < params.length; ii++) {
        Object obj = params[ii];
        if ((obj == null) || (obj == JSONObject.NULL)) {
            size++;
            continue;
        }
        //everything has a type even arrays and null
        size += 1;
        Class<?> cls = obj.getClass();
        if (cls.isArray()) {
            if (obj instanceof byte[]) {
                final byte[] b = (byte[]) obj;
                size += 4 + b.length;
                continue;
            }
            VoltType type;
            try {
                type = VoltType.typeFromClass(cls.getComponentType());
            } catch (VoltTypeException e) {
                obj = getAKosherArray((Object[]) obj);
                cls = obj.getClass();
                type = VoltType.typeFromClass(cls.getComponentType());
            }
            // component type, array length
            size += 1 + 2;
            switch(type) {
                case SMALLINT:
                    size += 2 * ((short[]) obj).length;
                    break;
                case INTEGER:
                    size += 4 * ((int[]) obj).length;
                    break;
                case BIGINT:
                    size += 8 * ((long[]) obj).length;
                    break;
                case FLOAT:
                    size += 8 * ((double[]) obj).length;
                    break;
                case STRING:
                    String[] strings = (String[]) obj;
                    byte[][] arrayEncodedStrings = new byte[strings.length][];
                    for (int zz = 0; zz < strings.length; zz++) {
                        if (strings[zz] == null) {
                            size += 4;
                        } else {
                            arrayEncodedStrings[zz] = strings[zz].getBytes(Constants.UTF8ENCODING);
                            size += 4 + arrayEncodedStrings[zz].length;
                        }
                    }
                    encodedStringArrays[ii] = arrayEncodedStrings;
                    break;
                case TIMESTAMP:
                    size += 8 * ((TimestampType[]) obj).length;
                    break;
                case DECIMAL:
                    size += 16 * ((BigDecimal[]) obj).length;
                    break;
                case VOLTTABLE:
                    for (VoltTable vt : (VoltTable[]) obj) {
                        size += vt.getSerializedSize();
                    }
                    break;
                case VARBINARY:
                    for (byte[] buf : (byte[][]) obj) {
                        // length prefix
                        size += 4;
                        if (buf != null) {
                            size += buf.length;
                        }
                    }
                    break;
                case GEOGRAPHY_POINT:
                    size += VoltType.GEOGRAPHY_POINT.getLengthInBytesForFixedTypesWithoutCheck() * ((GeographyPointValue[]) obj).length;
                    break;
                case GEOGRAPHY:
                    for (GeographyValue gv : (GeographyValue[]) obj) {
                        // length prefix
                        size += 4;
                        if (gv != null) {
                            size += gv.getLengthInBytes();
                        }
                    }
                    break;
                default:
                    throw new RuntimeException("FIXME: Unsupported type " + type);
            }
            continue;
        }
        // Handle NULL mappings not encoded by type.min_value convention
        if (obj == VoltType.NULL_TIMESTAMP) {
            size += 8;
            continue;
        } else if (obj == VoltType.NULL_STRING_OR_VARBINARY) {
            size += 4;
            continue;
        } else if (obj == VoltType.NULL_DECIMAL) {
            size += 16;
            continue;
        } else if (obj == VoltType.NULL_POINT) {
            size += VoltType.GEOGRAPHY_POINT.getLengthInBytesForFixedTypesWithoutCheck();
            continue;
        } else if (obj == VoltType.NULL_GEOGRAPHY) {
            size += 4;
            continue;
        } else if (obj instanceof BBContainer) {
            size += 4 + ((BBContainer) obj).b().remaining();
            continue;
        }
        VoltType type = VoltType.typeFromClass(cls);
        switch(type) {
            case TINYINT:
                size++;
                break;
            case SMALLINT:
                size += 2;
                break;
            case INTEGER:
                size += 4;
                break;
            case BIGINT:
                size += 8;
                break;
            case FLOAT:
                size += 8;
                break;
            case STRING:
                byte[] encodedString = ((String) obj).getBytes(Constants.UTF8ENCODING);
                size += 4 + encodedString.length;
                encodedStrings[ii] = encodedString;
                break;
            case TIMESTAMP:
                size += 8;
                break;
            case DECIMAL:
                size += 16;
                break;
            case GEOGRAPHY_POINT:
                size += VoltType.GEOGRAPHY_POINT.getLengthInBytesForFixedTypesWithoutCheck();
                break;
            case GEOGRAPHY:
                size += 4 + ((GeographyValue) obj).getLengthInBytes();
                break;
            case VOLTTABLE:
                size += ((VoltTable) obj).getSerializedSize();
                break;
            default:
                throw new RuntimeException("FIXME: Unsupported type " + type);
        }
    }
    return new ParameterSet(params, size, encodedStrings, encodedStringArrays);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) JSONString(org.json_voltpatches.JSONString) BBContainer(org.voltcore.utils.DBBPool.BBContainer) JSONObject(org.json_voltpatches.JSONObject)

Example 8 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class PolygonFactory method CreateRegularConvex.

/**
     * Create a regular convex polygon, with an optional hole.
     *
     * Note that the resulting polygon will be symmetric around any line
     * through the center and a vertex.  Consequently, the centroid of such
     * a polygon must be the center of the polygon.
     *
     * @param center The center of the polygon.
     * @param firstVertex The coordinates of the first vertex.
     * @param numVertices The number of vertices.
     * @param sizeOfHole If this is positive, we also create a hole whose vertices are
     *                   at the same angle from the polygon's center, but whose distance
     *                   is scaled by sizeOfHole.  This value must be in the range [0,1).
     * @return
     */
public static GeographyValue CreateRegularConvex(GeographyPointValue center, GeographyPointValue firstVertex, int numVertices, double sizeOfHole) {
    assert (0 <= sizeOfHole && sizeOfHole < 1.0);
    double phi = 360.0 / numVertices;
    GeographyPointValue holeFirstVertex = null;
    if (sizeOfHole > 0) {
        holeFirstVertex = firstVertex.scale(center, sizeOfHole);
    }
    List<GeographyPointValue> oneLoop = new ArrayList<GeographyPointValue>();
    List<GeographyPointValue> hole = (sizeOfHole < 0 ? null : new ArrayList<GeographyPointValue>());
    // as points.
    for (int idx = 0; idx < numVertices; idx += 1) {
        oneLoop.add(firstVertex.rotate(idx * phi, center));
        if (sizeOfHole > 0) {
            hole.add(holeFirstVertex.rotate(-(idx * phi), center));
        }
    }
    // Add the closing vertices.
    oneLoop.add(firstVertex);
    if (sizeOfHole > 0) {
        hole.add(holeFirstVertex);
    }
    List<List<GeographyPointValue>> loops = new ArrayList<List<GeographyPointValue>>();
    loops.add(oneLoop);
    if (sizeOfHole > 0) {
        loops.add(hole);
    }
    return new GeographyValue(loops);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 9 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class RegressionSuite method assertApproximateContentOfRow.

private static void assertApproximateContentOfRow(int row, Object[] expectedRow, VoltTable actualRow, double epsilon) {
    assertEquals("Actual row has wrong number of columns", expectedRow.length, actualRow.getColumnCount());
    for (int i = 0; i < expectedRow.length; ++i) {
        String msg = "Row " + row + ", col " + i + ": ";
        Object expectedObj = expectedRow[i];
        if (expectedObj == null) {
            VoltType vt = actualRow.getColumnType(i);
            actualRow.get(i, vt);
            assertTrue(msg, actualRow.wasNull());
        } else if (expectedObj instanceof GeographyPointValue) {
            assertApproximatelyEquals(msg, (GeographyPointValue) expectedObj, actualRow.getGeographyPointValue(i), epsilon);
        } else if (expectedObj instanceof GeographyValue) {
            assertApproximatelyEquals(msg, (GeographyValue) expectedObj, actualRow.getGeographyValue(i), epsilon);
        } else if (expectedObj instanceof Long) {
            long val = ((Long) expectedObj).longValue();
            assertEquals(msg, val, actualRow.getLong(i));
        } else if (expectedObj instanceof Integer) {
            long val = ((Integer) expectedObj).longValue();
            assertEquals(msg, val, actualRow.getLong(i));
        } else if (expectedObj instanceof Double) {
            double expectedValue = (Double) expectedObj;
            double actualValue = actualRow.getDouble(i);
            // for null values, convert value into double min
            if (actualRow.wasNull()) {
                actualValue = Double.MIN_VALUE;
            }
            if (epsilon <= 0) {
                String fullMsg = msg + String.format("Expected value %f != actual value %f", expectedValue, actualValue);
                assertEquals(fullMsg, expectedValue, actualValue);
            } else {
                String fullMsg = msg + String.format("abs(Expected Value - Actual Value) = %e >= %e", Math.abs(expectedValue - actualValue), epsilon);
                assertTrue(fullMsg, Math.abs(expectedValue - actualValue) < epsilon);
            }
        } else if (expectedObj instanceof BigDecimal) {
            BigDecimal exp = (BigDecimal) expectedObj;
            BigDecimal got = actualRow.getDecimalAsBigDecimal(i);
            // Either both are null or neither are null.
            assertEquals(exp == null, got == null);
            assertEquals(msg, exp.doubleValue(), got.doubleValue(), epsilon);
        } else if (expectedObj instanceof String) {
            String val = (String) expectedObj;
            assertEquals(msg, val, actualRow.getString(i));
        } else if (expectedObj instanceof TimestampType) {
            TimestampType val = (TimestampType) expectedObj;
            assertEquals(msg, val, actualRow.getTimestampAsTimestamp(i));
        } else {
            fail("Unexpected type in expected row: " + expectedObj.getClass().getSimpleName());
        }
    }
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) VoltType(org.voltdb.VoltType) TimestampType(org.voltdb.types.TimestampType) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

Example 10 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class TestGeographyValueQueries method testLoopOrderInCheesyPolygon.

public void testLoopOrderInCheesyPolygon() throws Exception {
    final double EPSILON = 1.0e-13;
    Client client = getClient();
    fillCheesyTable(client);
    GeographyValue cheesyPolygon = GeographyValue.fromWKT(cheesyWKT);
    VoltTable vt = client.callProcedure("@AdHoc", "select t.poly from t where t.pk = 1 order by t.pk;").getResults()[0];
    assertEquals("Expected only one row.", 1, vt.getRowCount());
    assertTrue(vt.advanceRow());
    GeographyValue cheesyRoundTripper = vt.getGeographyValue(0);
    assertApproximatelyEquals("Expected Equivalent Round Trip Polygons", cheesyPolygon, cheesyRoundTripper, EPSILON);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) Client(org.voltdb.client.Client) VoltTable(org.voltdb.VoltTable)

Aggregations

GeographyValue (org.voltdb.types.GeographyValue)33 GeographyPointValue (org.voltdb.types.GeographyPointValue)23 TimestampType (org.voltdb.types.TimestampType)11 BigDecimal (java.math.BigDecimal)10 VoltTable (org.voltdb.VoltTable)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Client (org.voltdb.client.Client)6 IOException (java.io.IOException)5 JSONString (org.json_voltpatches.JSONString)4 VoltType (org.voltdb.VoltType)3 ByteBuffer (java.nio.ByteBuffer)2 JSONObject (org.json_voltpatches.JSONObject)2 BBContainer (org.voltcore.utils.DBBPool.BBContainer)2 ColumnInfo (org.voltdb.VoltTable.ColumnInfo)2 NullCallback (org.voltdb.client.NullCallback)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1