use of org.apache.commons.math3.util.Pair in project mica2 by obiba.
the class AbstractFileSystemResource method doGetAttachment.
protected Attachment doGetAttachment(String path, @Nullable String version, @Nullable String shareKey) {
String basePath = normalizePath(path);
if (isPublishedFileSystem())
subjectAclService.checkAccess("/file", basePath);
else
subjectAclService.checkPermission("/draft/file", "VIEW", basePath, shareKey);
if (path.endsWith("/"))
throw new IllegalArgumentException("Folder download is not supported");
Pair<String, String> pathName = FileSystemService.extractPathName(basePath);
AttachmentState state = fileSystemService.getAttachmentState(pathName.getKey(), pathName.getValue(), isPublishedFileSystem());
if (isPublishedFileSystem())
return state.getPublishedAttachment();
if (Strings.isNullOrEmpty(version))
return state.getAttachment();
List<Attachment> attachments = fileSystemService.getAttachmentRevisions(state).stream().filter(a -> a.getId().equals(version)).collect(Collectors.toList());
if (attachments.isEmpty())
throw new NoSuchElementException("No file version " + version + " found at path: " + basePath);
return attachments.get(0);
}
use of org.apache.commons.math3.util.Pair in project iobserve-analysis by research-iobserve.
the class BehaviorModelTable method toInstances.
/**
* create an Instances object for clustering.
*
* @return instance
*/
public Instances toInstances() {
final FastVector fastVector = new FastVector();
// add transitions
for (int i = 0; i < this.signatures.size(); i++) {
for (int j = 0; j < this.signatures.size(); j++) {
if (this.transitions[i][j] > AbstractBehaviorModelTable.TRANSITION_THRESHOLD) {
final Attribute attribute = new Attribute(AbstractBehaviorModelTable.EDGE_INDICATOR + this.inverseSignatures[i] + AbstractBehaviorModelTable.EDGE_DIVIDER + this.inverseSignatures[j]);
fastVector.addElement(attribute);
} else {
continue;
}
}
}
// add informations
this.signatures.values().stream().forEach(pair -> Arrays.stream(pair.getSecond()).forEach(callInformation -> fastVector.addElement(new Attribute(AbstractBehaviorModelTable.INFORMATION_INDICATOR + this.inverseSignatures[pair.getFirst()] + AbstractBehaviorModelTable.INFORMATION_DIVIDER + callInformation.getSignature()))));
// TODO name
final Instances instances = new Instances("Test", fastVector, 0);
final Instance instance = this.toInstance();
instances.add(instance);
return instances;
}
use of org.apache.commons.math3.util.Pair in project incubator-gobblin by apache.
the class CouchbaseWriter method write.
@Override
public Future<WriteResponse> write(final D record, final WriteCallback callback) {
assertRecordWritable(record);
if (record instanceof TupleDocument) {
((TupleDocument) record).content().value1().retain();
}
Observable<D> observable;
try {
observable = _bucket.async().upsert(setDocumentTTL(record));
} catch (DataRecordException e) {
throw new RuntimeException("Caught exception trying to set TTL of the document", e);
}
if (callback == null) {
return new WriteResponseFuture<>(observable.timeout(_operationTimeout, _operationTimeunit).toBlocking().toFuture(), _defaultWriteResponseMapper);
} else {
final AtomicBoolean callbackFired = new AtomicBoolean(false);
final BlockingQueue<Pair<WriteResponse, Throwable>> writeResponseQueue = new ArrayBlockingQueue<>(1);
final Future<WriteResponse> writeResponseFuture = new Future<WriteResponse>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return callbackFired.get();
}
@Override
public WriteResponse get() throws InterruptedException, ExecutionException {
Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.take();
return getWriteResponseOrThrow(writeResponseThrowablePair);
}
@Override
public WriteResponse get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.poll(timeout, unit);
if (writeResponseThrowablePair == null) {
throw new TimeoutException("Timeout exceeded while waiting for future to be done");
} else {
return getWriteResponseOrThrow(writeResponseThrowablePair);
}
}
};
observable.timeout(_operationTimeout, _operationTimeunit).subscribe(new Subscriber<D>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
callbackFired.set(true);
writeResponseQueue.add(new Pair<WriteResponse, Throwable>(null, e));
callback.onFailure(e);
}
@Override
public void onNext(D doc) {
try {
callbackFired.set(true);
WriteResponse writeResponse = new GenericWriteResponse<D>(doc);
writeResponseQueue.add(new Pair<WriteResponse, Throwable>(writeResponse, null));
callback.onSuccess(writeResponse);
} finally {
if (doc instanceof TupleDocument) {
((TupleDocument) doc).content().value1().release();
}
}
}
});
return writeResponseFuture;
}
}
use of org.apache.commons.math3.util.Pair in project incubator-gobblin by apache.
the class GobblinMultiTaskAttempt method runWorkUnits.
/**
* Run a given list of {@link WorkUnit}s of a job.
*
* <p>
* This method assumes that the given list of {@link WorkUnit}s have already been flattened and
* each {@link WorkUnit} contains the task ID in the property {@link ConfigurationKeys#TASK_ID_KEY}.
* </p>
*
* @param countDownLatch a {@link java.util.concurrent.CountDownLatch} waited on for job completion
* @return a list of {@link Task}s from the {@link WorkUnit}s, as well as if there's a failure in task creation
* which should be handled separately to avoid silently starving on certain workunit.
*/
private synchronized Pair<List<Task>, Boolean> runWorkUnits(CountUpAndDownLatch countDownLatch) {
List<Task> tasks = Lists.newArrayList();
// have been submitted to the underlying task executor.
if (this.stopped.get()) {
return new Pair<>(tasks, false);
}
// A flag indicating if there are any tasks not submitted successfully.
// Caller of this method should handle tasks with submission failures accordingly.
boolean areAllTasksSubmitted = true;
while (this.workUnits.hasNext()) {
WorkUnit workUnit = this.workUnits.next();
String taskId = workUnit.getProp(ConfigurationKeys.TASK_ID_KEY);
// skip tasks that executed successfully in a prior attempt
if (taskSuccessfulInPriorAttempt(taskId)) {
continue;
}
SubscopedBrokerBuilder<GobblinScopeTypes, ?> taskBrokerBuilder = this.jobBroker.newSubscopedBuilder(new TaskScopeInstance(taskId));
WorkUnitState workUnitState = new WorkUnitState(workUnit, this.jobState, taskBrokerBuilder);
workUnitState.setId(taskId);
workUnitState.setProp(ConfigurationKeys.JOB_ID_KEY, this.jobId);
workUnitState.setProp(ConfigurationKeys.TASK_ID_KEY, taskId);
workUnitState.setProp(ConfigurationKeys.TASK_START_TIME_MILLIS_KEY, Long.toString(System.currentTimeMillis()));
if (this.containerIdOptional.isPresent()) {
workUnitState.setProp(ConfigurationKeys.TASK_ATTEMPT_ID_KEY, this.containerIdOptional.get());
}
// Create a new task from the work unit and submit the task to run.
// If an exception occurs here then the count down latch is decremented
// to avoid being stuck waiting for a task that was not created and submitted successfully.
Task task = null;
try {
countDownLatch.countUp();
task = createTaskWithRetry(workUnitState, countDownLatch);
this.taskStateTracker.registerNewTask(task);
task.setTaskFuture(this.taskExecutor.submit(task));
tasks.add(task);
} catch (Throwable e) {
if (e instanceof OutOfMemoryError) {
log.error("Encountering memory error in task creation/execution stage, please investigate memory usage:", e);
printMemoryUsage();
}
if (task == null) {
if (e instanceof RetryException) {
// Indicating task being null due to failure in creation even after retrying.
areAllTasksSubmitted = false;
}
// task could not be created, so directly count down
countDownLatch.countDown();
log.error("Could not create task for workunit {}", workUnit, e);
} else if (!task.hasTaskFuture()) {
// Task was created and may have been registered, but not submitted, so call the
// task state tracker task run completion directly since the task cancel does nothing if not submitted
this.taskStateTracker.onTaskRunCompletion(task);
areAllTasksSubmitted = false;
log.error("Could not submit task for workunit {}", workUnit, e);
} else {
// task was created and submitted, but failed later, so cancel the task to decrement the CountDownLatch
task.cancel();
log.error("Failure after task submitted for workunit {}", workUnit, e);
}
}
}
EventSubmitter.Builder eventSubmitterBuilder = new EventSubmitter.Builder(JobMetrics.get(this.jobId, new JobMetrics.CreatorTag(this.attemptId)).getMetricContext(), "gobblin.runtime");
eventSubmitterBuilder.addMetadata(this.taskEventMetadataGenerator.getMetadata(jobState, JobEvent.TASKS_SUBMITTED));
eventSubmitterBuilder.build().submit(JobEvent.TASKS_SUBMITTED, "tasksCount", Long.toString(countDownLatch.getRegisteredParties()));
return new Pair<>(tasks, areAllTasksSubmitted);
}
use of org.apache.commons.math3.util.Pair in project android_packages_apps_Dialer by LineageOS.
the class NewVoicemailMediaPlayerView method queryVoicemailSourcePackage.
@Nullable
private Pair<String, Uri> queryVoicemailSourcePackage(Pair<Context, Uri> contextUriPair) {
LogUtil.enterBlock("NewVoicemailMediaPlayer.queryVoicemailSourcePackage");
Context context = contextUriPair.first;
Uri uri = contextUriPair.second;
String sourcePackage;
try (Cursor cursor = context.getContentResolver().query(uri, new String[] { Voicemails.SOURCE_PACKAGE }, null, null, null)) {
if (!hasContent(cursor)) {
LogUtil.e("NewVoicemailMediaPlayer.queryVoicemailSourcePackage", "uri: %s does not return a SOURCE_PACKAGE", uri.toString());
sourcePackage = null;
} else {
sourcePackage = cursor.getString(0);
LogUtil.i("NewVoicemailMediaPlayer.queryVoicemailSourcePackage", "uri: %s has a SOURCE_PACKAGE: %s", uri.toString(), sourcePackage);
}
LogUtil.i("NewVoicemailMediaPlayer.queryVoicemailSourcePackage", "uri: %s has a SOURCE_PACKAGE: %s", uri.toString(), sourcePackage);
}
return new Pair<>(sourcePackage, uri);
}
Aggregations