Skip to content

Commit

Permalink
#6 adding filtering support
Browse files Browse the repository at this point in the history
  • Loading branch information
shroffk committed Jun 2, 2017
1 parent 15f6e22 commit 767b6d1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
50 changes: 34 additions & 16 deletions src/main/java/org/epics/channelfinder/ChannelFinderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -89,16 +93,20 @@ public HandlerQuery(PVStructure args, RPCResponseCallback callback) {

@Override
public void run() {

NTURI uri = NTURI.wrap(args);
log.info(Thread.currentThread().getName().toString());

String[] query = uri.getQueryNames();
TransportClient client = ElasticSearchClientManager.getClient();

try {
BoolQueryBuilder qb = boolQuery();
int size = 10000;
int from = 0;
Optional<String> sortField = Optional.empty();
final Set<String> filteredColumns = new HashSet<>();

for (String parameter : query) {
String value = uri.getQueryField(PVString.class, parameter).get();
if (value != null && !value.isEmpty()) {
Expand Down Expand Up @@ -135,6 +143,13 @@ public void run() {
log.warning("failed to parse the from: " + value);
}
break;
case "_filter":
try {
filteredColumns.addAll(Arrays.asList(value.trim().split(",")));
} catch (NumberFormatException e) {
log.warning("failed to parse the from: " + value);
}
break;
default:
DisMaxQueryBuilder propertyQuery = disMaxQuery();
for (String pattern : value.split("\\|")) {
Expand Down Expand Up @@ -170,8 +185,6 @@ public void run() {
channelTable.put("owner", Arrays.asList(new String[resultSize]));

qbResult.getHits().forEach(hit -> {
hit.getFields().entrySet().forEach(System.out::println);

try {
XmlChannel ch = channelMapper.readValue(hit.source(), XmlChannel.class);

Expand All @@ -180,19 +193,25 @@ public void run() {
channelTable.get("channelName").set(index, ch.getName());
channelTable.get("owner").set(index, ch.getOwner());

ch.getTags().stream().forEach(t -> {
if (!channelTagTable.containsKey(t.getName())) {
channelTagTable.put(t.getName(), new boolean[resultSize]);
}
channelTagTable.get(t.getName())[index] = true;
});

ch.getProperties().stream().forEach(prop -> {
if (!channelPropertyTable.containsKey(prop.getName())) {
channelPropertyTable.put(prop.getName(), Arrays.asList(new String[resultSize]));
}
channelPropertyTable.get(prop.getName()).set(index, prop.getValue());
});
if (!filteredColumns.contains("ALL")) {
ch.getTags().stream().filter((tag) -> {
return filteredColumns.isEmpty() || filteredColumns.contains(tag.getName());
}).forEach(t -> {
if (!channelTagTable.containsKey(t.getName())) {
channelTagTable.put(t.getName(), new boolean[resultSize]);
}
channelTagTable.get(t.getName())[index] = true;
});

ch.getProperties().stream().filter((prop) -> {
return filteredColumns.isEmpty() || filteredColumns.contains(prop.getName());
}).forEach(prop -> {
if (!channelPropertyTable.containsKey(prop.getName())) {
channelPropertyTable.put(prop.getName(), Arrays.asList(new String[resultSize]));
}
channelPropertyTable.get(prop.getName()).set(index, prop.getValue());
});
}
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -233,7 +252,6 @@ public void run() {
} finally {
}
}

}

public void shutdown() {
Expand Down
47 changes: 46 additions & 1 deletion src/test/java/org/epics/channelfinder/CFAdvanceQueryIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.epics.channelfinder.example.PopulateExampleDb;
import org.epics.nt.NTURI;
Expand Down Expand Up @@ -33,11 +34,55 @@ public class CFAdvanceQueryIT {
public static void createDB() {
PopulateExampleDb.createDB(1);
client = new RPCClientImpl(ChannelFinderService.SERVICE_NAME);
;
}

@Test
public void filterTest() {

List<XmlChannel> resultChannels;

NTURIBuilder uriBuilder = NTURI.createBuilder()
.addQueryString("_name")
.addQueryString("_size")
.addQueryString("_filter");
NTURI uri = uriBuilder.create();
uri.getPVStructure().getStringField("scheme").put("pva");
uri.getPVStructure().getStringField("path").put("channels");
uri.getQuery().getStringField("_name").put("*");
uri.getQuery().getStringField("_size").put("10");

try {
// When the _filter is used with ALL then you only get channelNames
uri.getQuery().getStringField("_filter").put("ALL");
PVStructure result = client.request(uri.getPVStructure(), 3.0);
System.out.println(result);
resultChannels = XmlUtil.parse(result);
assertTrue("Failed to filter the result to only return the channel Names ", resultChannels.stream().allMatch((ch) -> {
return ch.getProperties().isEmpty() && ch.getTags().isEmpty();
}));
// Check channels only consist of names

Set<String> filterNames = new HashSet<>();
filterNames.add("type");
filterNames.add("cell");
filterNames.add("family");

uri.getQuery().getStringField("_filter").put(filterNames.stream().collect(Collectors.joining(",")));
result = client.request(uri.getPVStructure(), 3.0);
resultChannels = XmlUtil.parse(result);

assertTrue("Failed to filter the result to only return the channel names and a selecte few properties" +
"expected only the properties type, cell, and family recieved " + result,
resultChannels.stream().allMatch((ch) -> {
return filterNames
.equals(ch.getProperties().stream().map(XmlProperty::getName).collect(Collectors.toSet()))
&&
ch.getTags().isEmpty();
}));

} catch (Exception e) {
e.printStackTrace();
}
}

@Test
Expand Down

0 comments on commit 767b6d1

Please sign in to comment.