use of java.util.function.Predicate in project graal by oracle.
the class SuspensionFilterTest method testSourceFilter.
@Test
public void testSourceFilter() {
final Source source1 = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(foo1,\n" + " STATEMENT(CONSTANT(43))\n" + " ))\n", "Source1").buildLiteral();
final Source source2 = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + " DEFINE(foo2,\n" + " STATEMENT(CONSTANT(44))\n" + " ))\n", "Source2").buildLiteral();
final Source source3 = testSource("ROOT(\n" + " CALL(foo1),\n" + " CALL(foo2),\n" + " STATEMENT(CALL(foo1)),\n" + " STATEMENT(CALL(foo2)),\n" + " STATEMENT(CALL(foo1)),\n" + " STATEMENT(CALL(foo2)),\n" + " STATEMENT(CONSTANT(100))\n" + ")\n");
try (DebuggerSession session = startSession()) {
// Filter out all sections
SuspensionFilter suspensionFilter = SuspensionFilter.newBuilder().sourceIs(s -> false).build();
session.setSteppingFilter(suspensionFilter);
session.suspendNextExecution();
startEval(source1);
expectDone();
startEval(source2);
expectDone();
startEval(source3);
expectDone();
Predicate<com.oracle.truffle.api.source.Source> filterSource1 = source -> {
return source.getName().indexOf("Source1") < 0;
};
suspensionFilter = SuspensionFilter.newBuilder().sourceIs(filterSource1).build();
session.setSteppingFilter(suspensionFilter);
session.suspendNextExecution();
startEval(source3);
// Skip foo1 and suspend in foo2:
expectSuspended((SuspendedEvent event) -> {
checkState(event, 3, true, "STATEMENT(CONSTANT(44))");
event.prepareStepOver(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 3, false, "CALL(foo2)");
event.prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 4, true, "STATEMENT(CALL(foo1))");
event.prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 5, true, "STATEMENT(CALL(foo2))");
event.prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 3, true, "STATEMENT(CONSTANT(44))");
event.prepareStepInto(2);
});
// Change the filter to filter Source2 out:
expectSuspended((SuspendedEvent event) -> {
checkState(event, 6, true, "STATEMENT(CALL(foo1))");
Predicate<com.oracle.truffle.api.source.Source> filterSource2 = source -> {
return source.getName().indexOf("Source2") < 0;
};
session.setSteppingFilter(SuspensionFilter.newBuilder().sourceIs(filterSource2).build());
event.prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 3, true, "STATEMENT(CONSTANT(43))");
event.prepareStepOut(1).prepareStepOver(1).prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 8, true, "STATEMENT(CONSTANT(100))");
event.prepareContinue();
});
expectDone();
}
}
use of java.util.function.Predicate in project minecolonies by Minecolonies.
the class AbstractBuilding method getRequiredItemsAndAmount.
/**
* Override this method if you want to keep an amount of items in inventory.
* When the inventory is full, everything get's dumped into the building chest.
* But you can use this method to hold some stacks back.
*
* @return a list of objects which should be kept.
*/
public Map<Predicate<ItemStack>, Integer> getRequiredItemsAndAmount() {
final Map<Predicate<ItemStack>, Integer> toKeep = new HashMap<>();
toKeep.putAll(keepX);
final IRequestManager manager = colony.getRequestManager();
toKeep.put(stack -> this.getOpenRequestsByCitizen().values().stream().anyMatch(list -> list.stream().anyMatch(token -> manager.getRequestForToken(token).getRequest() instanceof IDeliverable && ((IDeliverable) manager.getRequestForToken(token).getRequest()).matches(stack))), Integer.MAX_VALUE);
return toKeep;
}
use of java.util.function.Predicate in project minecolonies by Minecolonies.
the class AbstractBuilding method buildingRequiresCertainAmountOfItem.
/**
* Check if the worker requires a certain amount of that item and the alreadykept list contains it.
* Always leave one stack behind if the worker requires a certain amount of it. Just to be sure.
*
* @param stack the stack to check it with.
* @param localAlreadyKept already kept items.
* @return true if it should be leave it behind.
*/
public boolean buildingRequiresCertainAmountOfItem(final ItemStack stack, final List<ItemStorage> localAlreadyKept) {
for (final Map.Entry<Predicate<ItemStack>, Integer> entry : getRequiredItemsAndAmount().entrySet()) {
if (entry.getKey().test(stack)) {
final ItemStorage kept = ItemStorage.getItemStackOfListMatchingPredicate(localAlreadyKept, entry.getKey());
if (kept != null) {
if (kept.getAmount() >= entry.getValue()) {
return false;
}
localAlreadyKept.remove(kept);
kept.setAmount(kept.getAmount() + ItemStackUtils.getSize(stack));
localAlreadyKept.add(kept);
return true;
}
localAlreadyKept.add(new ItemStorage(stack));
return true;
}
}
return false;
}
use of java.util.function.Predicate in project bookkeeper by apache.
the class ListUnderReplicatedLedgerService method handle.
/*
* Print the node which holds the auditor lock.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
// parameter as this: ?missingreplica=<bookie_address>&excludingmissingreplica=<bookid_address>
Map<String, String> params = request.getParams();
if (HttpServer.Method.GET == request.getMethod()) {
final String includingBookieId;
final String excludingBookieId;
if (params != null && params.containsKey("missingreplica")) {
includingBookieId = params.get("missingreplica");
} else {
includingBookieId = null;
}
if (params != null && params.containsKey("excludingmissingreplica")) {
excludingBookieId = params.get("excludingmissingreplica");
} else {
excludingBookieId = null;
}
Predicate<List<String>> predicate = null;
if (!StringUtils.isBlank(includingBookieId) && !StringUtils.isBlank(excludingBookieId)) {
predicate = replicasList -> (replicasList.contains(includingBookieId) && !replicasList.contains(excludingBookieId));
} else if (!StringUtils.isBlank(includingBookieId)) {
predicate = replicasList -> replicasList.contains(includingBookieId);
} else if (!StringUtils.isBlank(excludingBookieId)) {
predicate = replicasList -> !replicasList.contains(excludingBookieId);
}
try {
List<Long> outputLedgers = Lists.newArrayList();
LedgerManagerFactory mFactory = bookieServer.getBookie().getLedgerManagerFactory();
LedgerUnderreplicationManager underreplicationManager = mFactory.newLedgerUnderreplicationManager();
Iterator<Long> iter = underreplicationManager.listLedgersToRereplicate(predicate);
while (iter.hasNext()) {
outputLedgers.add(iter.next());
}
if (outputLedgers.isEmpty()) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("No under replicated ledgers found");
return response;
} else {
response.setCode(HttpServer.StatusCode.OK);
String jsonResponse = JsonUtil.toJson(outputLedgers);
LOG.debug("output body: " + jsonResponse);
response.setBody(jsonResponse);
return response;
}
} catch (Exception e) {
LOG.error("Exception occurred while listing under replicated ledgers", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Exception when get." + e.getMessage());
return response;
}
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be GET method");
return response;
}
}
use of java.util.function.Predicate in project bookkeeper by apache.
the class BookKeeperAdmin method decommissionBookie.
/**
* Triggers AuditTask by resetting lostBookieRecoveryDelay and then make
* sure the ledgers stored in the given decommissioning bookie are properly
* replicated and they are not underreplicated because of the given bookie.
* This method waits untill there are no underreplicatedledgers because of this
* bookie. If the given Bookie is not shutdown yet, then it will throw
* BKIllegalOpException.
*
* @param bookieAddress
* address of the decommissioning bookie
* @throws CompatibilityException
* @throws UnavailableException
* @throws KeeperException
* @throws InterruptedException
* @throws IOException
* @throws BKAuditException
* @throws TimeoutException
* @throws BKException
*/
public void decommissionBookie(BookieSocketAddress bookieAddress) throws CompatibilityException, UnavailableException, KeeperException, InterruptedException, IOException, BKAuditException, TimeoutException, BKException {
if (getAvailableBookies().contains(bookieAddress) || getReadOnlyBookies().contains(bookieAddress)) {
LOG.error("Bookie: {} is not shutdown yet", bookieAddress);
throw BKException.create(BKException.Code.IllegalOpException);
}
triggerAudit();
/*
* Sleep for 30 secs, so that Auditor gets chance to trigger its
* force audittask and let the underreplicationmanager process
* to do its replication process
*/
Thread.sleep(30 * 1000);
/*
* get the collection of the ledgers which are stored in this
* bookie, by making a call to
* bookieLedgerIndexer.getBookieToLedgerIndex.
*/
BookieLedgerIndexer bookieLedgerIndexer = new BookieLedgerIndexer(bkc.ledgerManager);
Map<String, Set<Long>> bookieToLedgersMap = bookieLedgerIndexer.getBookieToLedgerIndex();
Set<Long> ledgersStoredInThisBookie = bookieToLedgersMap.get(bookieAddress.toString());
if ((ledgersStoredInThisBookie != null) && (!ledgersStoredInThisBookie.isEmpty())) {
/*
* wait untill all the ledgers are replicated to other
* bookies by making sure that these ledgers metadata don't
* contain this bookie as part of their ensemble.
*/
waitForLedgersToBeReplicated(ledgersStoredInThisBookie, bookieAddress, bkc.ledgerManager);
}
// for double-checking, check if any ledgers are listed as underreplicated because of this bookie
Predicate<List<String>> predicate = replicasList -> replicasList.contains(bookieAddress.toString());
Iterator<Long> urLedgerIterator = underreplicationManager.listLedgersToRereplicate(predicate);
if (urLedgerIterator.hasNext()) {
// if there are any then wait and make sure those ledgers are replicated properly
LOG.info("Still in some underreplicated ledgers metadata, this bookie is part of its ensemble. " + "Have to make sure that those ledger fragments are rereplicated");
List<Long> urLedgers = new ArrayList<>();
urLedgerIterator.forEachRemaining(urLedgers::add);
waitForLedgersToBeReplicated(urLedgers, bookieAddress, bkc.ledgerManager);
}
}
Aggregations