use of com.emc.metalnx.core.domain.entity.DataGridMetadataSearch in project metalnx-web by irods-contrib.
the class PostgresSpecificQueryProviderImpl method buildSpecificQueryForMetadataSearch.
/*
* (non-Javadoc)
*
* @see com.emc.metalnx.services.irods.utils.SpecificQueryProvider#
* buildSpecificQueryForMetadataSearch(java.util.List, java.lang.String,
* boolean)
*/
@Override
public String buildSpecificQueryForMetadataSearch(List<DataGridMetadataSearch> metadataSearch, String zone, boolean searchAgainstColls) {
// This is the first part of the query. It selects all objects (collections or
// data objects)
// that has a piece of metadata matching the query
StringBuilder objQuery = new StringBuilder();
// This is the query that actually looks for metadata tags in the data grid.
StringBuilder metadataSearchQuery = new StringBuilder();
// This is the last part of the query. It groups the objects by their attributes
// in order to
// have unique results.
StringBuilder gb = new StringBuilder();
// This is the final query that will be sent to the data grid database
StringBuilder q = new StringBuilder();
if (searchAgainstColls) {
objQuery.append(" SELECT obj_name, parent_path, obj_owner, create_ts, modify_ts, resc_name, totalMatches");
objQuery.append(" FROM (");
objQuery.append(" SELECT c.coll_name as obj_name,");
objQuery.append(" c.parent_coll_name as parent_path,");
objQuery.append(" c.coll_owner_name as obj_owner,");
objQuery.append(" c.create_ts as create_ts,");
objQuery.append(" c.modify_ts as modify_ts,");
objQuery.append(" '' as resc_name,");
objQuery.append(" c.coll_inheritance,");
objQuery.append(" COUNT(c.coll_name) as totalMatches");
objQuery.append(" FROM ");
objQuery.append(" r_coll_main c ");
objQuery.append(" JOIN ( ");
gb.append(" ) AS coll_metadata ON (c.coll_id = map_object_id) ");
gb.append(" GROUP BY ");
gb.append(" c.coll_name,");
gb.append(" c.parent_coll_name,");
gb.append(" c.coll_owner_name,");
gb.append(" c.create_ts,");
gb.append(" c.modify_ts,");
gb.append(" resc_name,");
gb.append(" c.coll_inheritance");
gb.append(" ORDER BY totalMatches DESC, c.coll_name ");
gb.append(" ) AS ms ");
} else {
objQuery.append(" SELECT obj_name, size, obj_owner, repl_num, create_ts, modify_ts, resc_name, parent_path, totalMatches");
objQuery.append(" FROM (");
objQuery.append(" SELECT ");
objQuery.append(" d.data_name as obj_name, ");
objQuery.append(" d.data_size as size, ");
objQuery.append(" d.data_owner_name as obj_owner, ");
objQuery.append(" d.data_repl_num as repl_num, ");
objQuery.append(" d.create_ts as create_ts,");
objQuery.append(" d.modify_ts as modify_ts, ");
objQuery.append(" d.resc_name as resc_name, ");
objQuery.append(" c.coll_name as parent_path,");
objQuery.append(" COUNT(d.data_name) as totalMatches");
objQuery.append(" FROM r_data_main d ");
objQuery.append(" JOIN r_coll_main c ON (d.coll_id = c.coll_id) ");
objQuery.append(" JOIN ( ");
gb.append(" ) AS data_obj_metadata ON (d.data_id = map_object_id) ");
gb.append(" GROUP BY ");
gb.append(" d.data_name,");
gb.append(" d.data_size,");
gb.append(" d.data_owner_name,");
gb.append(" d.data_repl_num,");
gb.append(" d.create_ts,");
gb.append(" d.modify_ts,");
gb.append(" d.resc_name, ");
gb.append(" c.coll_name");
gb.append(" ORDER BY totalMatches DESC, d.data_name ");
gb.append(" ) AS ms ");
}
for (DataGridMetadataSearch d : metadataSearch) {
metadataSearchQuery.append(d.getSpecQueryAsString());
// appending conditions
if (metadataSearch.indexOf(d) != metadataSearch.size() - 1) {
metadataSearchQuery.append(" UNION ALL ");
}
}
// combining the three parts of the metadata query into a single SQL query
q.append(objQuery.toString());
q.append(metadataSearchQuery.toString());
q.append(gb.toString());
return q.toString();
}
use of com.emc.metalnx.core.domain.entity.DataGridMetadataSearch in project metalnx-web by irods-contrib.
the class SpecQueryServiceImplTest method testCountCollectionsMatchingMetadata.
@Test
public void testCountCollectionsMatchingMetadata() throws Exception {
SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);
IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);
EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory().getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory().getSpecificQueryAO(irodsAccount);
Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);
specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(COLL_AVU_ATTR1, COLL_AVU_VAL1, "", DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
int count = specQueryService.countCollectionsMatchingMetadata(metadataSearch, irodsAccount.getZone());
Assert.assertTrue("no recs returned", count > 1);
}
use of com.emc.metalnx.core.domain.entity.DataGridMetadataSearch in project metalnx-web by irods-contrib.
the class SpecQueryServiceImplTest method testSearchByMetadataCollections.
@Test
public void testSearchByMetadataCollections() throws Exception {
SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);
IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);
EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory().getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory().getSpecificQueryAO(irodsAccount);
Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);
specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(COLL_AVU_ATTR1, COLL_AVU_VAL1, "", DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
SpecificQueryResultSet result = specQueryService.searchByMetadata(metadataSearch, irodsAccount.getZone(), true, null, 0, 0);
Assert.assertFalse("no result", result.getResults().isEmpty());
}
use of com.emc.metalnx.core.domain.entity.DataGridMetadataSearch in project metalnx-web by irods-contrib.
the class SpecQueryServiceImplTest method testCountDataObjectsMatchingMetadata.
@Test
public void testCountDataObjectsMatchingMetadata() throws Exception {
SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);
IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);
EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory().getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory().getSpecificQueryAO(irodsAccount);
Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);
specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(DATA_AVU_ATTR1, DATA_AVU_VAL1, "", DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
int count = specQueryService.countDataObjectsMatchingMetadata(metadataSearch, irodsAccount.getZone());
Assert.assertTrue("no recs returned", count > 1);
}
use of com.emc.metalnx.core.domain.entity.DataGridMetadataSearch in project metalnx-web by irods-contrib.
the class MetadataController method searchByMetadata.
@RequestMapping(value = "/search/", method = RequestMethod.POST)
@ResponseBody
public String searchByMetadata(@RequestParam(required = false) final String jsonMetadataSearch, @RequestParam("draw") final int draw, @RequestParam("start") final int start, @RequestParam("length") final int length) throws DataGridConnectionRefusedException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonResponse = new HashMap<String, Object>();
jsonResponse.put("draw", String.valueOf(draw));
jsonResponse.put("recordsTotal", String.valueOf(0));
jsonResponse.put("recordsFiltered", String.valueOf(0));
jsonResponse.put("data", new ArrayList<String>());
String jsonString = "";
try {
if (jsonMetadataSearch != null) {
currPage = (int) (Math.floor(start / length) + 1);
this.jsonMetadataSearch = jsonMetadataSearch;
}
// Creating parser
JsonNode jsonObject = mapper.readTree(this.jsonMetadataSearch);
currSearch = new ArrayList<>();
JsonNode attributes = jsonObject.get("attribute");
JsonNode operators = jsonObject.get("operator");
JsonNode values = jsonObject.get("value");
JsonNode units = jsonObject.get("unit");
for (int i = 0; i < attributes.size(); i++) {
String attr = attributes.get(i).textValue();
String val = values.get(i).textValue();
String unit = units.get(i).textValue();
String opt = operators.get(i).textValue();
DataGridSearchOperatorEnum op = DataGridSearchOperatorEnum.valueOf(opt);
DataGridMetadataSearch ms = new DataGridMetadataSearch(attr, val, unit, op);
currSearch.add(ms);
}
DataGridPageContext pageContext = new DataGridPageContext();
List<DataGridCollectionAndDataObject> dgCollDataObjs = metadataService.findByMetadata(currSearch, pageContext, currPage, length);
jsonResponse.put("recordsTotal", String.valueOf(pageContext.getTotalNumberOfItems()));
jsonResponse.put("recordsFiltered", String.valueOf(pageContext.getTotalNumberOfItems()));
jsonResponse.put("data", dgCollDataObjs);
} catch (DataGridConnectionRefusedException e) {
throw e;
} catch (Exception e) {
logger.error("Could not search by metadata: ", e.getMessage());
}
try {
jsonString = mapper.writeValueAsString(jsonResponse);
} catch (JsonProcessingException e) {
logger.error("Could not parse hashmap in metadata search to json: {}", e.getMessage());
}
return jsonString;
}
Aggregations