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

Add spatial filters #164

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

import java.util.List;

@Getter
@EqualsAndHashCode(callSuper = true)
public class PolygonBound extends SpatialFilterBoundSpec {
private static String SPATIAL_BOUND_TYPE = "polygon";

private List<? extends Number> abscissa;
private List<? extends Number> ordinate;

public PolygonBound(@NonNull List<? extends Number> abscissa, @NonNull List<? extends Number> ordinate) {
this.type = SPATIAL_BOUND_TYPE;
this.abscissa = abscissa;
this.ordinate = ordinate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

import java.util.List;

@Getter
@EqualsAndHashCode(callSuper = true)
public class RadiusBound extends SpatialFilterBoundSpec {
private static String SPATIAL_BOUND_TYPE = "radius";

private List<? extends Number> coords;
private Number radius;

public RadiusBound(@NonNull List<? extends Number> coords, @NonNull Number radius) {
this.type = SPATIAL_BOUND_TYPE;
this.coords = coords;
this.radius = radius;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

import java.util.List;

@Getter
@EqualsAndHashCode(callSuper = true)
public class RectangularBound extends SpatialFilterBoundSpec {
private static String SPATIAL_BOUND_TYPE = "rectangular";

private List<? extends Number> minCoords;
private List<? extends Number> maxCoords;

public RectangularBound(@NonNull List<? extends Number> minCoords, @NonNull List<? extends Number> maxCoords) {
this.type = SPATIAL_BOUND_TYPE;
this.minCoords = minCoords;
this.maxCoords = maxCoords;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import in.zapr.druid.druidry.filter.DruidFilter;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

@Getter
@EqualsAndHashCode(callSuper = true)
public class SpatialFilter extends DruidFilter {
private static String SPATIAL_DRUID_FILTER_TYPE = "spatial";

private String dimension;
private SpatialFilterBoundSpec bound;

public SpatialFilter(@NonNull String dimension, @NonNull SpatialFilterBoundSpec bound) {
this.type = SPATIAL_DRUID_FILTER_TYPE;
this.dimension = dimension;
this.bound = bound;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
public abstract class SpatialFilterBoundSpec {
protected String type;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;

public class PolygonBoundTest {

private static ObjectMapper objectMapper;

@BeforeClass
public void init() {
objectMapper = new ObjectMapper();
}

@Test
public void testAllFields() throws JSONException, JsonProcessingException {
List<Double> abscissaValues = Arrays.asList(1.1, 2.2, 3.3);
List<Double> ordinateValues = Arrays.asList(3.2, 2.2, 1.1);
SpatialFilterBoundSpec filter = new PolygonBound(abscissaValues, ordinateValues);

JSONObject boundObject = new JSONObject();
boundObject.put("type", "polygon");
boundObject.put("abscissa", new JSONArray(abscissaValues));
boundObject.put("ordinate", new JSONArray(ordinateValues));

String actualJSON = objectMapper.writeValueAsString(filter);
String expectedJSON = boundObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test(expectedExceptions = NullPointerException.class)
public void testMinCoordMissing() {
List<Double> ordinateValues = Arrays.asList(3.2, 2.2, 1.1);
SpatialFilterBoundSpec bound = new PolygonBound(null, ordinateValues);
}

@Test(expectedExceptions = NullPointerException.class)
public void testMaxCoordMissing() {
List<Double> abscissaValues = Arrays.asList(1.1, 2.2, 3.3);
SpatialFilterBoundSpec bound = new PolygonBound(abscissaValues, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;

public class RadiusBoundTest {

private static ObjectMapper objectMapper;

@BeforeClass
public void init() {
objectMapper = new ObjectMapper();
}

@Test
public void testAllFields() throws JSONException, JsonProcessingException {

List<Double> coordValues = Arrays.asList(1.1, 2.2, 3.3);
double radius = 5.5;
SpatialFilterBoundSpec bound = new RadiusBound(coordValues, radius);

JSONObject boundObject = new JSONObject();
boundObject.put("type", "radius");
boundObject.put("coords", new JSONArray(coordValues));
boundObject.put("radius", radius);


String actualJSON = objectMapper.writeValueAsString(bound);
String expectedJSON = boundObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test(expectedExceptions = NullPointerException.class)
public void testCoordsMissing() {
double radius = 5.5;
SpatialFilterBoundSpec bound = new RadiusBound(null, radius);
}

@Test(expectedExceptions = NullPointerException.class)
public void testValueMissing() {
List<Double> coordValues = Arrays.asList(1.1, 2.2, 3.3);
SpatialFilterBoundSpec bound = new RadiusBound(coordValues, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed 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 in.zapr.druid.druidry.filter.spatialFilterSpec;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;

public class RectangularFilterTest {

private static ObjectMapper objectMapper;

@BeforeClass
public void init() {
objectMapper = new ObjectMapper();
}

@Test
public void testAllFields() throws JSONException, JsonProcessingException {

List<Double> minCoordValues = Arrays.asList(1.1, 2.2, 3.3);
List<Double> maxCoordValues = Arrays.asList(3.2, 2.2, 1.1);

SpatialFilterBoundSpec filter = new RectangularBound(minCoordValues, maxCoordValues);

JSONObject boundObject = new JSONObject();
boundObject.put("type", "rectangular");
boundObject.put("minCoords", new JSONArray(minCoordValues));
boundObject.put("maxCoords", new JSONArray(maxCoordValues));

String actualJSON = objectMapper.writeValueAsString(filter);
String expectedJSON = boundObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test(expectedExceptions = NullPointerException.class)
public void testMinCoordMissing() {
List<Double> maxCoordValues = Arrays.asList(3.2, 2.2, 1.1);
SpatialFilterBoundSpec bound = new RectangularBound(null, maxCoordValues);
}

@Test(expectedExceptions = NullPointerException.class)
public void testMaxCoordMissing() {
List<Double> minCoordValues = Arrays.asList(1.1, 2.2, 3.3);
SpatialFilterBoundSpec bound = new RectangularBound(minCoordValues, null);
}

}
Loading