Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geo spatial interfaces #16029

Merged
merged 7 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/querying/geo.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ The `radius` bound has the following elements:

|Property|Description|Required|
|--------|-----------|--------|
|`coords`|Origin coordinates in the form [x, y]|yes|
|`radius`|The float radius value|yes|
|`coords`|Center coordinates in the form [x, y]|yes|
|`radius`|The float radius value according to specified unit|yes|
|`radiusUnit`|String value of radius unit in lowercase, default value is 'euclidean'. Allowed units are euclidean, meters, miles, kilometers.|no|

#### Polygon

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.druid.collections.spatial;

import org.apache.druid.collections.bitmap.ImmutableBitmap;
import org.apache.druid.collections.spatial.search.Bound;

public interface BaseImmutableRTee
{
Iterable<ImmutableBitmap> search(Bound bound);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.druid.collections.spatial;

import org.apache.druid.collections.bitmap.BitmapFactory;
import org.apache.druid.collections.bitmap.ImmutableBitmap;

import java.nio.ByteBuffer;
import java.util.Iterator;

/**
* Byte layout:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the child here and what is the dim?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nums of dims = size of float array, needs it here esp for byte layout. Child here in byte layout represents the child node stored at the childOffeset
this.childrenOffset = initialOffset
+ offsetFromInitial
+ HEADER_NUM_BYTES
+ 2 * numDims * Float.BYTES
+ Integer.BYTES
+ bitmapSize;

* Header
* 0 to 1 : the MSB is a boolean flag for isLeaf, the next 15 bits represent the number of children of a node
* Body
* 2 to 2 + numDims * Float.BYTES : minCoordinates
* 2 + numDims * Float.BYTES to 2 + 2 * numDims * Float.BYTES : maxCoordinates
* concise set
* rest (children) : Every 4 bytes is storing an offset representing the position of a child.
*
* The child offset is an offset from the initialOffset
*/
public class ImmutableFloatNode implements ImmutableNode<float[]>
{
public static final int HEADER_NUM_BYTES = 2;

private final int numDims;
private final int initialOffset;
private final int offsetFromInitial;

private final short numChildren;
private final boolean isLeaf;
private final int childrenOffset;

private final ByteBuffer data;

private final BitmapFactory bitmapFactory;

public ImmutableFloatNode(
int numDims,
int initialOffset,
int offsetFromInitial,
ByteBuffer data,
BitmapFactory bitmapFactory
)
{
this.bitmapFactory = bitmapFactory;
this.numDims = numDims;
this.initialOffset = initialOffset;
this.offsetFromInitial = offsetFromInitial;
short header = data.getShort(initialOffset + offsetFromInitial);
this.isLeaf = (header & 0x8000) != 0;
this.numChildren = (short) (header & 0x7FFF);
final int sizePosition = initialOffset + offsetFromInitial + HEADER_NUM_BYTES + 2 * numDims * Float.BYTES;
int bitmapSize = data.getInt(sizePosition);
this.childrenOffset = initialOffset
+ offsetFromInitial
+ HEADER_NUM_BYTES
+ 2 * numDims * Float.BYTES
+ Integer.BYTES
+ bitmapSize;
Comment on lines +73 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.childrenOffset = initialOffset
+ offsetFromInitial
+ HEADER_NUM_BYTES
+ 2 * numDims * Float.BYTES
+ Integer.BYTES
+ bitmapSize;
this.childrenOffset = sizePosition
+ Integer.BYTES
+ bitmapSize;


this.data = data;
Comment on lines +71 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final int sizePosition = initialOffset + offsetFromInitial + HEADER_NUM_BYTES + 2 * numDims * Float.BYTES;
int bitmapSize = data.getInt(sizePosition);
this.childrenOffset = initialOffset
+ offsetFromInitial
+ HEADER_NUM_BYTES
+ 2 * numDims * Float.BYTES
+ Integer.BYTES
+ bitmapSize;
this.data = data;
this(numDims, initialOffset, offsetFromInitial, numChildren, leaf, data, bitmapFactory)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work as this( ) calls needs to first statement. I just renamed the class here with no other changes.

}

public ImmutableFloatNode(
int numDims,
int initialOffset,
int offsetFromInitial,
short numChildren,
boolean leaf,
ByteBuffer data,
BitmapFactory bitmapFactory
)
{
this.bitmapFactory = bitmapFactory;
this.numDims = numDims;
this.initialOffset = initialOffset;
this.offsetFromInitial = offsetFromInitial;
this.numChildren = numChildren;
this.isLeaf = leaf;
final int sizePosition = initialOffset + offsetFromInitial + HEADER_NUM_BYTES + 2 * numDims * Float.BYTES;
int bitmapSize = data.getInt(sizePosition);
this.childrenOffset = initialOffset
+ offsetFromInitial
+ HEADER_NUM_BYTES
+ 2 * numDims * Float.BYTES
+ Integer.BYTES
+ bitmapSize;

this.data = data;
}

@Override
public BitmapFactory getBitmapFactory()
{
return bitmapFactory;
}

@Override
public int getInitialOffset()
{
return initialOffset;
}

@Override
public int getOffsetFromInitial()
{
return offsetFromInitial;
}

@Override
public int getNumDims()
{
return numDims;
}

@Override
public boolean isLeaf()
{
return isLeaf;
}

@Override
public float[] getMinCoordinates()
{
return getCoords(initialOffset + offsetFromInitial + HEADER_NUM_BYTES);
}

@Override
public float[] getMaxCoordinates()
{
return getCoords(initialOffset + offsetFromInitial + HEADER_NUM_BYTES + numDims * Float.BYTES);
}

@Override
public ImmutableBitmap getImmutableBitmap()
{
final int sizePosition = initialOffset + offsetFromInitial + HEADER_NUM_BYTES + 2 * numDims * Float.BYTES;
int numBytes = data.getInt(sizePosition);
data.position(sizePosition + Integer.BYTES);
ByteBuffer tmpBuffer = data.slice();
tmpBuffer.limit(numBytes);
return bitmapFactory.mapImmutableBitmap(tmpBuffer.asReadOnlyBuffer());
}

@Override
@SuppressWarnings("ArgumentParameterSwap")
public Iterable<ImmutableNode<float[]>> getChildren()
{
return new Iterable<ImmutableNode<float[]>>()
{
@Override
public Iterator<ImmutableNode<float[]>> iterator()
{
return new Iterator<ImmutableNode<float[]>>()
{
private int count = 0;

@Override
public boolean hasNext()
{
return (count < numChildren);
}

@Override
public ImmutableNode<float[]> next()
{
if (isLeaf) {
return new ImmutableFloatPoint(
numDims,
initialOffset,
data.getInt(childrenOffset + (count++) * Integer.BYTES),
data,
bitmapFactory
);
}
return new ImmutableFloatNode(
numDims,
initialOffset,
data.getInt(childrenOffset + (count++) * Integer.BYTES),
data,
bitmapFactory
);
}

@Override
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
};
}

@Override
public ByteBuffer getData()
{
return data;
}

private float[] getCoords(int offset)
{
final float[] retVal = new float[numDims];

final ByteBuffer readOnlyBuffer = data.asReadOnlyBuffer();
readOnlyBuffer.position(offset);
readOnlyBuffer.asFloatBuffer().get(retVal);

return retVal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

import java.nio.ByteBuffer;

public class ImmutablePoint extends ImmutableNode
public class ImmutableFloatPoint extends ImmutableFloatNode
{
public ImmutablePoint(
public ImmutableFloatPoint(
int numDims,
int initialOffset,
int offsetFromInitial,
Expand All @@ -36,7 +36,7 @@ public ImmutablePoint(
super(numDims, initialOffset, offsetFromInitial, (short) 0, true, data, bitmapFactory);
}

public ImmutablePoint(ImmutableNode node)
public ImmutableFloatPoint(ImmutableNode node)
{
super(
node.getNumDims(),
Expand All @@ -55,7 +55,7 @@ public float[] getCoords()
}

@Override
public Iterable<ImmutableNode> getChildren()
public Iterable<ImmutableNode<float[]>> getChildren()
{
// should never get here
throw new UnsupportedOperationException();
Expand Down