/** * Create a bounding box starting with the given object. * * @param objA the object * @return the bounding box */ Object createBoundingBox(Object objA) { SpatialKey a = (SpatialKey) objA; if (a.isNull()) { return a; } float[] minMax = new float[dimensions * 2]; for (int i = 0; i < dimensions; i++) { minMax[i + i] = a.min(i); minMax[i + i + 1] = a.max(i); } return new SpatialKey(0, minMax); }
@Override public void write(WriteBuffer buff, Object obj) { SpatialKey k = (SpatialKey) obj; if (k.isNull()) { buff.putVarInt(-1); buff.putVarLong(k.getId()); return; } int flags = 0; for (int i = 0; i < dimensions; i++) { if (k.min(i) == k.max(i)) { flags |= 1 << i; } } buff.putVarInt(flags); for (int i = 0; i < dimensions; i++) { buff.putFloat(k.min(i)); if ((flags & (1 << i)) == 0) { buff.putFloat(k.max(i)); } } buff.putVarLong(k.getId()); }
SpatialKey key = getKey(row); if (key.isNull()) { return; while (it.hasNext()) { SpatialKey k = it.next(); if (k.equalsIgnoringId(key)) { throw getDuplicateKeyException(key.toString()); while (it.hasNext()) { SpatialKey k = it.next(); if (k.equalsIgnoringId(key)) { if (map.isSameTransaction(k)) { continue; if (map.get(k) != null) { throw getDuplicateKeyException(k.toString());
/** * Check whether two spatial values are equal. * * @param a the first value * @param b the second value * @return true if they are equal */ public boolean equals(Object a, Object b) { if (a == b) { return true; } else if (a == null || b == null) { return false; } long la = ((SpatialKey) a).getId(); long lb = ((SpatialKey) b).getId(); return la == lb; }
@Override public Object read(ByteBuffer buff) { int flags = DataUtils.readVarInt(buff); if (flags == -1) { long id = DataUtils.readVarLong(buff); return new SpatialKey(id); } float[] minMax = new float[dimensions * 2]; for (int i = 0; i < dimensions; i++) { float min = buff.getFloat(); float max; if ((flags & (1 << i)) != 0) { max = min; } else { max = buff.getFloat(); } minMax[i + i] = min; minMax[i + i + 1] = max; } long id = DataUtils.readVarLong(buff); return new SpatialKey(id, minMax); }
while (it.hasNext()) { SpatialKey k = it.next(); if (k.equalsIgnoringId(key)) { throw getDuplicateKeyException(key.toString()); while (it.hasNext()) { SpatialKey k = it.next(); if (k.equalsIgnoringId(key)) { if (map.isSameTransaction(k)) { continue; if (map.get(k) != null) { throw getDuplicateKeyException(k.toString());
private static ArrayList<Object> getNotNull(ArrayList<Object> list) { ArrayList<Object> result = null; for (Object o : list) { SpatialKey a = (SpatialKey) o; if (a.isNull()) { result = New.arrayList(); break; } } if (result == null) { return list; } for (Object o : list) { SpatialKey a = (SpatialKey) o; if (!a.isNull()) { result.add(a); } } return result; }
@Override public boolean equals(Object other) { if (other == this) { return true; } else if (!(other instanceof SpatialKey)) { return false; } SpatialKey o = (SpatialKey) other; if (id != o.id) { return false; } return equalsIgnoringId(o); }
/** * Increase the bounds in the given spatial object. * * @param bounds the bounds (may be modified) * @param add the value */ public void increaseBounds(Object bounds, Object add) { SpatialKey a = (SpatialKey) add; SpatialKey b = (SpatialKey) bounds; if (a.isNull() || b.isNull()) { return; } for (int i = 0; i < dimensions; i++) { b.setMin(i, Math.min(b.min(i), a.min(i))); b.setMax(i, Math.max(b.max(i), a.max(i))); } }
/** * Check whether a is completely inside b and does not touch the * given bound. * * @param objA the object to check * @param objB the bounds * @return true if a is completely inside b */ public boolean isInside(Object objA, Object objB) { SpatialKey a = (SpatialKey) objA; SpatialKey b = (SpatialKey) objB; if (a.isNull() || b.isNull()) { return false; } for (int i = 0; i < dimensions; i++) { if (a.min(i) <= b.min(i) || a.max(i) >= b.max(i)) { return false; } } return true; }
private SpatialKey getKey(SearchRow row) { Value v = row.getValue(columnIds[0]); if (v == ValueNull.INSTANCE) { return new SpatialKey(row.getKey()); } Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometryNoCopy(); Envelope env = g.getEnvelopeInternal(); return new SpatialKey(row.getKey(), (float) env.getMinX(), (float) env.getMaxX(), (float) env.getMinY(), (float) env.getMaxY()); }
@Override public void remove(Session session, Row row) { SpatialKey key = getKey(row); if (key.isNull()) { return; } TransactionMap<SpatialKey, Value> map = getMap(session); try { Value old = map.remove(key); if (old == null) { old = map.remove(key); throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey()); } } catch (IllegalStateException e) { throw mvTable.convertException(e); } }
@Override public boolean equals(Object other) { if (other == this) { return true; } else if (!(other instanceof SpatialKey)) { return false; } SpatialKey o = (SpatialKey) other; if (id != o.id) { return false; } return equalsIgnoringId(o); }
SpatialKey boundsInner = (SpatialKey) createBoundingBox(bounds); for (int i = 0; i < dimensions; i++) { float t = boundsInner.min(i); boundsInner.setMin(i, boundsInner.max(i)); boundsInner.setMax(i, t); int bestDim = 0; for (int i = 0; i < dimensions; i++) { float inner = boundsInner.max(i) - boundsInner.min(i); if (inner < 0) { continue; float outer = bounds.max(i) - bounds.min(i); float d = inner / outer; if (d > best) { return null; float min = boundsInner.min(bestDim); float max = boundsInner.max(bestDim); int firstIndex = -1, lastIndex = -1; for (int i = 0; i < list.size() && (firstIndex < 0 || lastIndex < 0); i++) { SpatialKey o = (SpatialKey) list.get(i); if (firstIndex < 0 && o.max(bestDim) == min) { firstIndex = i; } else if (lastIndex < 0 && o.min(bestDim) == max) { lastIndex = i;
/** * Check whether the two objects overlap. * * @param objA the first object * @param objB the second object * @return true if they overlap */ public boolean isOverlap(Object objA, Object objB) { SpatialKey a = (SpatialKey) objA; SpatialKey b = (SpatialKey) objB; if (a.isNull() || b.isNull()) { return false; } for (int i = 0; i < dimensions; i++) { if (a.max(i) < b.min(i) || a.min(i) > b.max(i)) { return false; } } return true; }
/** * Create a bounding box starting with the given object. * * @param objA the object * @return the bounding box */ Object createBoundingBox(Object objA) { SpatialKey a = (SpatialKey) objA; if (a.isNull()) { return a; } float[] minMax = new float[dimensions * 2]; for (int i = 0; i < dimensions; i++) { minMax[i + i] = a.min(i); minMax[i + i + 1] = a.max(i); } return new SpatialKey(0, minMax); }
@Override public void write(WriteBuffer buff, Object obj) { SpatialKey k = (SpatialKey) obj; if (k.isNull()) { buff.putVarInt(-1); buff.putVarLong(k.getId()); return; } int flags = 0; for (int i = 0; i < dimensions; i++) { if (k.min(i) == k.max(i)) { flags |= 1 << i; } } buff.putVarInt(flags); for (int i = 0; i < dimensions; i++) { buff.putFloat(k.min(i)); if ((flags & (1 << i)) == 0) { buff.putFloat(k.max(i)); } } buff.putVarLong(k.getId()); }