Search in sources :

Example 91 with Predicate

use of com.google.common.base.Predicate in project GeoGig by boundlessgeo.

the class OSMExport method getFeatures.

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {

            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
            List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            Entity entity = converter.toEntity(feature, null);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            return container;
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : EntityConverter(org.locationtech.geogig.osm.internal.EntityConverter) Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Envelope(com.vividsolutions.jts.geom.Envelope) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Predicate(com.google.common.base.Predicate) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) Bounded(org.locationtech.geogig.api.Bounded) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 92 with Predicate

use of com.google.common.base.Predicate in project cdap by caskdata.

the class WorkflowTest method testOneActionWorkflow.

@Test(timeout = 120 * 1000L)
public void testOneActionWorkflow() throws Exception {
    final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(OneActionWorkflowApp.class, TEMP_FOLDER_SUPPLIER);
    final Injector injector = AppFabricTestHelper.getInjector();
    final ProgramDescriptor programDescriptor = Iterators.filter(app.getPrograms().iterator(), new Predicate<ProgramDescriptor>() {

        @Override
        public boolean apply(ProgramDescriptor input) {
            return input.getProgramId().getType() == ProgramType.WORKFLOW;
        }
    }).next();
    final SettableFuture<String> completion = SettableFuture.create();
    final ProgramController controller = AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            LOG.info("Initializing");
            injector.getInstance(Store.class).setStart(controller.getProgramRunId().getParent(), controller.getProgramRunId().getRun(), System.currentTimeMillis());
        }

        @Override
        public void completed() {
            LOG.info("Completed");
            completion.set("Completed");
        }

        @Override
        public void error(Throwable cause) {
            LOG.info("Error", cause);
            completion.setException(cause);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    String run = completion.get();
    Assert.assertEquals("Completed", run);
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ApplicationWithPrograms(co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) Injector(com.google.inject.Injector) AbstractListener(co.cask.cdap.internal.app.runtime.AbstractListener) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 93 with Predicate

use of com.google.common.base.Predicate in project cdap by caskdata.

the class LeaderElectionInfoServiceTest method testParticipants.

@Test
public void testParticipants() throws Exception {
    final int size = 5;
    String prefix = "/election";
    List<ZKClientService> zkClients = new ArrayList<>();
    ZKClientService infoZKClient = DefaultZKClientService.Builder.of(zkServer.getConnectionStr()).build();
    infoZKClient.startAndWait();
    zkClients.add(infoZKClient);
    // Start the LeaderElectionInfoService
    LeaderElectionInfoService infoService = new LeaderElectionInfoService(infoZKClient, prefix);
    infoService.startAndWait();
    // This will timeout as there is no leader election node created yet
    try {
        infoService.getParticipants(1, TimeUnit.SECONDS);
        Assert.fail("Expected timeout");
    } catch (TimeoutException e) {
    // Expected
    }
    // Starts multiple leader elections
    List<LeaderElection> leaderElections = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        ZKClientService zkClient = DefaultZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClient.startAndWait();
        zkClients.add(zkClient);
        final int participantId = i;
        LeaderElection leaderElection = new LeaderElection(zkClient, prefix, new ElectionHandler() {

            @Override
            public void leader() {
                LOG.info("Leader: {}", participantId);
            }

            @Override
            public void follower() {
                LOG.info("Follow: {}", participantId);
            }
        });
        leaderElection.start();
        leaderElections.add(leaderElection);
    }
    // Get the dynamic participants map
    final SortedMap<Integer, LeaderElectionInfoService.Participant> participants = infoService.getParticipants(5, TimeUnit.SECONDS);
    // Expects to set all participants with hostname information
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            if (participants.size() != size) {
                return false;
            }
            return Iterables.all(participants.values(), new Predicate<LeaderElectionInfoService.Participant>() {

                @Override
                public boolean apply(LeaderElectionInfoService.Participant input) {
                    return input.getHostname() != null;
                }
            });
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Fetch the static snapshot. It should be the same as the dynamic participants.
    SortedMap<Integer, LeaderElectionInfoService.Participant> snapshot = infoService.fetchCurrentParticipants();
    Assert.assertEquals(size, snapshot.size());
    Assert.assertEquals(participants, snapshot);
    int expectedSize = size;
    for (LeaderElection leaderElection : leaderElections) {
        leaderElection.stopAndWait();
        Tasks.waitFor(--expectedSize, new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                return participants.size();
            }
        }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    }
    // Fetch the static snapshot again. It should be empty and the same as the dynamic one.
    snapshot = infoService.fetchCurrentParticipants();
    Assert.assertTrue(snapshot.isEmpty());
    Assert.assertEquals(participants, snapshot);
    infoService.stopAndWait();
    for (ZKClientService zkClient : zkClients) {
        zkClient.stopAndWait();
    }
}
Also used : ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Predicate(com.google.common.base.Predicate) ZKClientService(org.apache.twill.zookeeper.ZKClientService) DefaultZKClientService(org.apache.twill.internal.zookeeper.DefaultZKClientService) LeaderElection(org.apache.twill.internal.zookeeper.LeaderElection) ElectionHandler(org.apache.twill.api.ElectionHandler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 94 with Predicate

use of com.google.common.base.Predicate in project cdap by caskdata.

the class DistributedProgramRuntimeService method list.

@Override
public synchronized Map<RunId, RuntimeInfo> list(ProgramType type) {
    Map<RunId, RuntimeInfo> result = Maps.newHashMap();
    result.putAll(super.list(type));
    // Table holds the Twill RunId and TwillController associated with the program matching the input type
    Table<ProgramId, RunId, TwillController> twillProgramInfo = HashBasedTable.create();
    // Goes through all live application and fill the twillProgramInfo table
    for (TwillRunner.LiveInfo liveInfo : twillRunner.lookupLive()) {
        String appName = liveInfo.getApplicationName();
        ProgramId programId = TwillAppNames.fromTwillAppName(appName, false);
        if (programId == null) {
            continue;
        }
        if (!type.equals(programId.getType())) {
            continue;
        }
        for (TwillController controller : liveInfo.getControllers()) {
            RunId twillRunId = controller.getRunId();
            if (isTwillRunIdCached(twillRunId)) {
                continue;
            }
            twillProgramInfo.put(programId, twillRunId, controller);
        }
    }
    if (twillProgramInfo.isEmpty()) {
        return ImmutableMap.copyOf(result);
    }
    final Set<RunId> twillRunIds = twillProgramInfo.columnKeySet();
    Collection<RunRecordMeta> activeRunRecords = store.getRuns(ProgramRunStatus.RUNNING, new Predicate<RunRecordMeta>() {

        @Override
        public boolean apply(RunRecordMeta record) {
            return record.getTwillRunId() != null && twillRunIds.contains(org.apache.twill.internal.RunIds.fromString(record.getTwillRunId()));
        }
    }).values();
    for (RunRecordMeta record : activeRunRecords) {
        String twillRunId = record.getTwillRunId();
        if (twillRunId == null) {
            // This is unexpected. Just log and ignore the run record
            LOG.warn("No twill runId for in run record {}.", record);
            continue;
        }
        RunId twillRunIdFromRecord = org.apache.twill.internal.RunIds.fromString(twillRunId);
        // Get the CDAP RunId from RunRecord
        RunId runId = RunIds.fromString(record.getPid());
        // Get the Program and TwillController for the current twillRunId
        Map<ProgramId, TwillController> mapForTwillId = twillProgramInfo.columnMap().get(twillRunIdFromRecord);
        Map.Entry<ProgramId, TwillController> entry = mapForTwillId.entrySet().iterator().next();
        // Create RuntimeInfo for the current Twill RunId
        RuntimeInfo runtimeInfo = createRuntimeInfo(entry.getKey(), entry.getValue(), runId);
        if (runtimeInfo != null) {
            result.put(runId, runtimeInfo);
            updateRuntimeInfo(type, runId, runtimeInfo);
        } else {
            LOG.warn("Unable to find program {} {}", type, entry.getKey());
        }
    }
    return ImmutableMap.copyOf(result);
}
Also used : SimpleRuntimeInfo(co.cask.cdap.internal.app.runtime.service.SimpleRuntimeInfo) TwillRunner(org.apache.twill.api.TwillRunner) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ProgramId(co.cask.cdap.proto.id.ProgramId) Predicate(com.google.common.base.Predicate) TwillController(org.apache.twill.api.TwillController) RunId(org.apache.twill.api.RunId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 95 with Predicate

use of com.google.common.base.Predicate in project cdap by caskdata.

the class ServePathGeneratorTest method testGetServePath.

@Test
public void testGetServePath() throws Exception {
    URL jarUrl = getClass().getResource("/CountRandomWebapp-localhost.jar");
    Assert.assertNotNull(jarUrl);
    final JarResources jarResources = new JarResources(new LocalLocationFactory().create(jarUrl.toURI()));
    Predicate<String> fileExists = new Predicate<String>() {

        @Override
        public boolean apply(@Nullable String file) {
            return file != null && jarResources.getResource(file) != null;
        }
    };
    ServePathGenerator servePathGenerator = new ServePathGenerator(Constants.Webapp.WEBAPP_DIR, fileExists);
    Assert.assertEquals("/webapp/127.0.0.1:20000/netlens/src/index.html", servePathGenerator.getServePath("127.0.0.1:20000", "/netlens"));
    Assert.assertEquals("/webapp/127.0.0.1:20000/netlens/src/index.html", servePathGenerator.getServePath("127.0.0.1:20000", "/netlens/index.html"));
    Assert.assertEquals("/webapp/127.0.0.1:20000/netlens/src/index.html", servePathGenerator.getServePath("127.0.0.1:20000", "/netlens/"));
    Assert.assertEquals("/webapp/127.0.0.1:20000/netlens/src/index.html", servePathGenerator.getServePath("127.0.0.1:20000", "/netlens/"));
    Assert.assertEquals("/webapp/default/src/index.html", servePathGenerator.getServePath("127.0.0.1:30000", "/"));
    Assert.assertEquals("/webapp/default/src/index.html", servePathGenerator.getServePath("127.0.0.1:30000", "/index.html"));
    Assert.assertEquals("/webapp/127.0.0.1:20000/src/netlens/2.txt", servePathGenerator.getServePath("127.0.0.1:20000", "netlens/2.txt"));
    Assert.assertEquals("/webapp/default/netlens/src/1.txt", servePathGenerator.getServePath("127.0.0.1:80", "/netlens/1.txt?count=100"));
    Assert.assertEquals("/webapp/default/netlens/src/data/data.txt", servePathGenerator.getServePath("127.0.0.1:30000", "/netlens/data/data.txt"));
    Assert.assertEquals("/v3/apps?count=10", servePathGenerator.getServePath("127.0.0.1:30000", "/netlens/v3/apps?count=10"));
    Assert.assertEquals("/v3/apps?count=10", servePathGenerator.getServePath("127.0.0.1:30000", "/v3/apps?count=10"));
    Assert.assertEquals("/status", servePathGenerator.getServePath("127.0.0.1:30000", "/netlens/status"));
    Assert.assertEquals("/status", servePathGenerator.getServePath("127.0.0.1:30000", "/status"));
    servePathGenerator = new ServePathGenerator(Constants.Webapp.WEBAPP_DIR + "/", fileExists);
    Assert.assertEquals("/webapp/www.abc.com:80/geo/src/data/data.txt", servePathGenerator.getServePath("www.abc.com", "/geo/data/data.txt"));
    Assert.assertEquals("/webapp/www.abc.com:80/geo/src/data/data.txt", servePathGenerator.getServePath("www.abc.com:80", "/geo/data/data.txt"));
    Assert.assertEquals("/webapp/default/netlens/src/data/data.txt", servePathGenerator.getServePath("www.abc.com:30000", "/netlens/data/data.txt"));
    Assert.assertEquals("/geo/data/data.txt", servePathGenerator.getServePath("www.abc.com:30000", "/geo/data/data.txt"));
}
Also used : LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) URL(java.net.URL) JarResources(co.cask.cdap.common.lang.jar.JarResources) Nullable(javax.annotation.Nullable) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Aggregations

Predicate (com.google.common.base.Predicate)180 ArrayList (java.util.ArrayList)29 Nullable (javax.annotation.Nullable)29 Test (org.junit.Test)29 List (java.util.List)28 UUID (java.util.UUID)21 IOException (java.io.IOException)20 Map (java.util.Map)20 File (java.io.File)15 Test (org.testng.annotations.Test)14 ImmutableList (com.google.common.collect.ImmutableList)12 HashMap (java.util.HashMap)12 Set (java.util.Set)9 DateTime (org.joda.time.DateTime)9 BigDecimal (java.math.BigDecimal)8 Path (javax.ws.rs.Path)8 PaymentTransactionModelDao (org.killbill.billing.payment.dao.PaymentTransactionModelDao)8 Account (org.killbill.billing.account.api.Account)7 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)7 PaymentModelDao (org.killbill.billing.payment.dao.PaymentModelDao)7