Search in sources :

Example 6 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataHttpHandler method searchMetadata.

@GET
@Path("/namespaces/{namespace-id}/metadata/search")
public void searchMetadata(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("query") String searchQuery, @QueryParam("target") List<String> targets, @QueryParam("sort") @DefaultValue("") String sort, @QueryParam("offset") @DefaultValue("0") int offset, // 2147483647 is Integer.MAX_VALUE
@QueryParam("limit") @DefaultValue("2147483647") int limit, @QueryParam("numCursors") @DefaultValue("0") int numCursors, @QueryParam("cursor") @DefaultValue("") String cursor, @QueryParam("showHidden") @DefaultValue("false") boolean showHidden, @Nullable @QueryParam("entityScope") String entityScope) throws Exception {
    if (searchQuery == null || searchQuery.isEmpty()) {
        throw new BadRequestException("query is not specified");
    }
    Set<EntityTypeSimpleName> types = Collections.emptySet();
    if (targets != null) {
        types = ImmutableSet.copyOf(Iterables.transform(targets, STRING_TO_TARGET_TYPE));
    }
    SortInfo sortInfo = SortInfo.of(URLDecoder.decode(sort, "UTF-8"));
    if (SortInfo.DEFAULT.equals(sortInfo)) {
        if (!(cursor.isEmpty()) || 0 != numCursors) {
            throw new BadRequestException("Cursors are not supported when sort info is not specified.");
        }
    }
    try {
        MetadataSearchResponse response = metadataAdmin.search(namespaceId, URLDecoder.decode(searchQuery, "UTF-8"), types, sortInfo, offset, limit, numCursors, cursor, showHidden, validateEntityScope(entityScope));
        responder.sendJson(HttpResponseStatus.OK, response, MetadataSearchResponse.class, GSON);
    } catch (Exception e) {
        // if MetadataDataset throws an exception, it gets wrapped
        if (Throwables.getRootCause(e) instanceof BadRequestException) {
            throw new BadRequestException(e.getMessage(), e);
        }
        throw e;
    }
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) BadRequestException(co.cask.cdap.common.BadRequestException) MetadataSearchResponse(co.cask.cdap.proto.metadata.MetadataSearchResponse) BadRequestException(co.cask.cdap.common.BadRequestException) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) SortInfo(co.cask.cdap.data2.metadata.dataset.SortInfo) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 7 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataDatasetTest method testIndexRebuilding.

@Test
public void testIndexRebuilding() throws Exception {
    final MetadataDataset dataset = getDataset(DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("testIndexRebuilding"));
    TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) dataset);
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Indexer indexer = new ReversingIndexer();
            dataset.setMetadata(new MetadataEntry(flow1, "flowKey", "flowValue"), Collections.singleton(indexer));
            dataset.setMetadata(new MetadataEntry(dataset1, "datasetKey", "datasetValue"), Collections.singleton(indexer));
        }
    });
    final String namespaceId = flow1.getNamespace();
    final Set<EntityTypeSimpleName> targetTypes = Collections.singleton(EntityTypeSimpleName.ALL);
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            List<MetadataEntry> searchResults = searchByDefaultIndex(dataset, namespaceId, "flowValue", targetTypes);
            Assert.assertTrue(searchResults.isEmpty());
            searchResults = searchByDefaultIndex(dataset, namespaceId, "flowKey:flow*", targetTypes);
            Assert.assertTrue(searchResults.isEmpty());
            searchResults = searchByDefaultIndex(dataset, namespaceId, "datasetValue", targetTypes);
            Assert.assertTrue(searchResults.isEmpty());
            searchResults = searchByDefaultIndex(dataset, namespaceId, "datasetKey:dataset*", targetTypes);
            Assert.assertTrue(searchResults.isEmpty());
        }
    });
    final AtomicReference<byte[]> startRowKeyForNextBatch = new AtomicReference<>();
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // Re-build indexes. Now the default indexer should be used
            startRowKeyForNextBatch.set(dataset.rebuildIndexes(null, 1));
            Assert.assertNotNull(startRowKeyForNextBatch.get());
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            List<MetadataEntry> flowSearchResults = searchByDefaultIndex(dataset, namespaceId, "flowValue", targetTypes);
            List<MetadataEntry> dsSearchResults = searchByDefaultIndex(dataset, namespaceId, "datasetValue", targetTypes);
            if (!flowSearchResults.isEmpty()) {
                Assert.assertEquals(1, flowSearchResults.size());
                flowSearchResults = searchByDefaultIndex(dataset, namespaceId, "flowKey:flow*", targetTypes);
                Assert.assertEquals(1, flowSearchResults.size());
                Assert.assertTrue(dsSearchResults.isEmpty());
                dsSearchResults = searchByDefaultIndex(dataset, namespaceId, "datasetKey:dataset*", targetTypes);
                Assert.assertTrue(dsSearchResults.isEmpty());
            } else {
                flowSearchResults = searchByDefaultIndex(dataset, namespaceId, "flowKey:flow*", targetTypes);
                Assert.assertTrue(flowSearchResults.isEmpty());
                Assert.assertEquals(1, dsSearchResults.size());
                dsSearchResults = searchByDefaultIndex(dataset, namespaceId, "datasetKey:dataset*", targetTypes);
                Assert.assertEquals(1, dsSearchResults.size());
            }
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            startRowKeyForNextBatch.set(dataset.rebuildIndexes(startRowKeyForNextBatch.get(), 1));
            Assert.assertNull(startRowKeyForNextBatch.get());
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            List<MetadataEntry> searchResults = searchByDefaultIndex(dataset, namespaceId, "flowValue", targetTypes);
            Assert.assertEquals(1, searchResults.size());
            searchResults = searchByDefaultIndex(dataset, namespaceId, "flowKey:flow*", targetTypes);
            Assert.assertEquals(1, searchResults.size());
            searchResults = searchByDefaultIndex(dataset, namespaceId, "datasetValue", targetTypes);
            Assert.assertEquals(1, searchResults.size());
            searchResults = searchByDefaultIndex(dataset, namespaceId, "datasetKey:dataset*", targetTypes);
            Assert.assertEquals(1, searchResults.size());
        }
    });
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionFailureException(org.apache.tephra.TransactionFailureException) BadRequestException(co.cask.cdap.common.BadRequestException) EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) Indexer(co.cask.cdap.data2.metadata.indexer.Indexer) InvertedValueIndexer(co.cask.cdap.data2.metadata.indexer.InvertedValueIndexer) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 8 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataDatasetTest method testIndexDeletion.

@Test
public void testIndexDeletion() throws Exception {
    final MetadataDataset dataset = getDataset(DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("testIndexRebuilding"));
    TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) dataset);
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            dataset.setProperty(flow1, "flowKey", "flowValue");
            dataset.setProperty(dataset1, "datasetKey", "datasetValue");
        }
    });
    final String namespaceId = flow1.getNamespace();
    final Set<EntityTypeSimpleName> targetTypes = Collections.singleton(EntityTypeSimpleName.ALL);
    final MetadataEntry expectedFlowEntry = new MetadataEntry(flow1, "flowKey", "flowValue");
    final MetadataEntry expectedDatasetEntry = new MetadataEntry(dataset1, "datasetKey", "datasetValue");
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            List<MetadataEntry> searchResults = searchByDefaultIndex(dataset, namespaceId, "flowValue", targetTypes);
            Assert.assertEquals(ImmutableList.of(expectedFlowEntry), searchResults);
            searchResults = searchByDefaultIndex(dataset, namespaceId, "flowKey:flow*", targetTypes);
            Assert.assertEquals(ImmutableList.of(expectedFlowEntry), searchResults);
            searchResults = searchByDefaultIndex(dataset, namespaceId, "datasetValue", targetTypes);
            Assert.assertEquals(ImmutableList.of(expectedDatasetEntry), searchResults);
            searchResults = searchByDefaultIndex(dataset, namespaceId, "datasetKey:dataset*", targetTypes);
            Assert.assertEquals(ImmutableList.of(expectedDatasetEntry), searchResults);
        }
    });
    // 4 indexes should have been deleted - flowValue, flowKey:flowValue, datasetValue, datasetKey:datasetValue
    for (int i = 0; i < 4; i++) {
        txnl.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Assert.assertEquals(1, dataset.deleteAllIndexes(1));
            }
        });
    }
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Assert.assertEquals(0, dataset.deleteAllIndexes(1));
        }
    });
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) TransactionExecutor(org.apache.tephra.TransactionExecutor) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TransactionFailureException(org.apache.tephra.TransactionFailureException) BadRequestException(co.cask.cdap.common.BadRequestException) Test(org.junit.Test)

Example 9 with EntityTypeSimpleName

use of co.cask.cdap.proto.element.EntityTypeSimpleName in project cdap by caskdata.

the class MetadataDatasetTest method testPagination.

@Test
public void testPagination() throws Exception {
    final MetadataDataset dataset = getDataset(DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("testPagination"), MetadataScope.SYSTEM);
    TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) dataset);
    final String flowName = "name11";
    final String dsName = "name21 name22";
    final String appName = "name31 name32 name33";
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            dataset.setProperty(flow1, AbstractSystemMetadataWriter.ENTITY_NAME_KEY, flowName);
            dataset.setProperty(dataset1, AbstractSystemMetadataWriter.ENTITY_NAME_KEY, dsName);
            dataset.setProperty(app1, AbstractSystemMetadataWriter.ENTITY_NAME_KEY, appName);
        }
    });
    final String namespaceId = flow1.getNamespace();
    final EnumSet<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
    final MetadataEntry flowEntry = new MetadataEntry(flow1, AbstractSystemMetadataWriter.ENTITY_NAME_KEY, flowName);
    final MetadataEntry dsEntry = new MetadataEntry(dataset1, AbstractSystemMetadataWriter.ENTITY_NAME_KEY, dsName);
    final MetadataEntry appEntry = new MetadataEntry(app1, AbstractSystemMetadataWriter.ENTITY_NAME_KEY, appName);
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // since no sort is to be performed by the dataset, we return all (ignore limit and offset)
            SearchResults searchResults = dataset.search(namespaceId, "name*", targets, SortInfo.DEFAULT, 0, 3, 1, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(// 4 indexes for app: 'name31', 'name31 name32 name33', 'name32', 'name33'
            ImmutableList.of(flowEntry, dsEntry, dsEntry, dsEntry, appEntry, appEntry, appEntry, appEntry), searchResults.getResults());
            // ascending sort by name. offset and limit should be respected.
            SortInfo nameAsc = new SortInfo(AbstractSystemMetadataWriter.ENTITY_NAME_KEY, SortInfo.SortOrder.ASC);
            // first 2 in ascending order
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 0, 2, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry), searchResults.getResults());
            // return 2 with offset 1 in ascending order
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 1, 2, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry, appEntry), searchResults.getResults());
            // descending sort by name. offset and filter should be respected.
            SortInfo nameDesc = new SortInfo(AbstractSystemMetadataWriter.ENTITY_NAME_KEY, SortInfo.SortOrder.DESC);
            // first 2 in descending order
            searchResults = dataset.search(namespaceId, "*", targets, nameDesc, 0, 2, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(appEntry, dsEntry), searchResults.getResults());
            // last 1 in descending order
            searchResults = dataset.search(namespaceId, "*", targets, nameDesc, 2, 1, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(appEntry, dsEntry, flowEntry), searchResults.getResults());
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 2, 0, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry), searchResults.getResults());
            searchResults = dataset.search(namespaceId, "*", targets, nameDesc, 1, 0, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(appEntry), searchResults.getResults());
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 4, 0, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry, appEntry), searchResults.getResults());
            searchResults = dataset.search(namespaceId, "*", targets, nameDesc, 100, 0, 0, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(appEntry, dsEntry, flowEntry), searchResults.getResults());
            // test cursors
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 0, 1, 3, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry, appEntry), searchResults.getResults());
            Assert.assertEquals(ImmutableList.of(dsName, appName), searchResults.getCursors());
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 0, 1, 3, searchResults.getCursors().get(0), false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(dsEntry, appEntry), searchResults.getResults());
            Assert.assertEquals(ImmutableList.of(appName), searchResults.getCursors());
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 0, 1, 3, searchResults.getCursors().get(0), false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(appEntry), searchResults.getResults());
            Assert.assertEquals(ImmutableList.of(), searchResults.getCursors());
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 0, 2, 3, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry, appEntry), searchResults.getResults());
            Assert.assertEquals(ImmutableList.of(appName), searchResults.getCursors());
            searchResults = dataset.search(namespaceId, "*", targets, nameAsc, 3, 1, 2, null, false, EnumSet.allOf(EntityScope.class));
            Assert.assertEquals(ImmutableList.of(flowEntry, dsEntry, appEntry), searchResults.getResults());
            Assert.assertEquals(ImmutableList.of(), searchResults.getCursors());
        }
    });
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) TransactionExecutor(org.apache.tephra.TransactionExecutor) TransactionFailureException(org.apache.tephra.TransactionFailureException) BadRequestException(co.cask.cdap.common.BadRequestException) EntityScope(co.cask.cdap.proto.EntityScope) Test(org.junit.Test)

Aggregations

EntityTypeSimpleName (co.cask.cdap.proto.element.EntityTypeSimpleName)9 BadRequestException (co.cask.cdap.common.BadRequestException)7 Test (org.junit.Test)7 NamespaceId (co.cask.cdap.proto.id.NamespaceId)4 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)3 TransactionExecutor (org.apache.tephra.TransactionExecutor)3 TransactionFailureException (org.apache.tephra.TransactionFailureException)3 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)2 DatasetInstanceConfiguration (co.cask.cdap.proto.DatasetInstanceConfiguration)2 ViewSpecification (co.cask.cdap.proto.ViewSpecification)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 StreamId (co.cask.cdap.proto.id.StreamId)2 StreamViewId (co.cask.cdap.proto.id.StreamViewId)2 MetadataSearchResponse (co.cask.cdap.proto.metadata.MetadataSearchResponse)2 MetadataSearchResultRecord (co.cask.cdap.proto.metadata.MetadataSearchResultRecord)2 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 NotFoundException (co.cask.cdap.common.NotFoundException)1 SortInfo (co.cask.cdap.data2.metadata.dataset.SortInfo)1 Indexer (co.cask.cdap.data2.metadata.indexer.Indexer)1