/** * Fetches elements like {@link #iterate(PageSupplier, int)} method does using default page size * which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Iterable<E> iterate(PageSupplier<E, X> supplier) throws X { return iterate(supplier, DEFAULT_PAGE_SIZE); }
/** * Fetches elements like {@link #stream(PageSupplier, int)} method does using default page size * which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Stream<E> stream(PageSupplier<E, X> supplier) throws X { return stream(supplier, DEFAULT_PAGE_SIZE); }
/** * Eagerly fetches all the elements page by page and returns an iterable of them. * * @param supplier pages supplier * @param size how many items to retrieve per page * @param <E> the type of the element held by page * @param <X> the type of exception thrown by page supplier * @return iterable of fetched elements * @throws X when supplier throws exception */ public static <E, X extends Exception> Iterable<E> iterate(PageSupplier<E, X> supplier, int size) throws X { return eagerFetch(supplier, size); }
/** * Returns an iterable like {@link #streamLazily(PageSupplier, int)} method does using default * page size which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Iterable<E> iterateLazily(PageSupplier<E, X> supplier) { return iterateLazily(supplier, DEFAULT_PAGE_SIZE); }
/** * Fetches elements like {@link #streamLazily(PageSupplier, int)} method does using default page * size which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Stream<E> streamLazily(PageSupplier<E, X> supplier) { return streamLazily(supplier, DEFAULT_PAGE_SIZE); }
@GET @Path("/namespace/{namespace:.*}") @Produces(APPLICATION_JSON) @ApiOperation( value = "Get workspaces by given namespace", notes = "This operation can be performed only by authorized user", response = WorkspaceDto.class, responseContainer = "List") @ApiResponses({ @ApiResponse(code = 200, message = "The workspaces successfully fetched"), @ApiResponse(code = 500, message = "Internal server error occurred during workspaces fetching") }) public List<WorkspaceDto> getByNamespace( @ApiParam("Workspace status") @QueryParam("status") String status, @ApiParam("The namespace") @PathParam("namespace") String namespace) throws ServerException, BadRequestException { return withLinks( Pages.stream( (maxItems, skipCount) -> workspaceManager.getByNamespace(namespace, false, maxItems, skipCount)) .filter(ws -> status == null || status.equalsIgnoreCase(ws.getStatus().toString())) .map(DtoConverter::asDto) .collect(toList())); }
@Override public void onCascadeEvent(BeforeAccountRemovedEvent event) throws Exception { for (WorkspaceImpl workspace : iterate( (maxItems, skipCount) -> workspaceManager.getByNamespace( event.getAccount().getName(), false, maxItems, skipCount))) { workspaceManager.removeWorkspace(workspace.getId()); } } }
/** * Eagerly fetches all the elements page by page and returns a stream of them. * * @param supplier page supplier * @param size how many items to retrieve per page * @param <E> the type of the element held by page * @param <X> the type of exception thrown by page supplier * @return stream of fetched elements * @throws X when supplier throws exception */ public static <E, X extends Exception> Stream<E> stream(PageSupplier<E, X> supplier, int size) throws X { return eagerFetch(supplier, size).stream(); }
@Override public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final List<WorkspaceImpl> accountWorkspaces = Pages.stream( (maxItems, skipCount) -> workspaceManagerProvider .get() .getByNamespace(account.getName(), false, maxItems, skipCount)) .collect(Collectors.toList()); if (!accountWorkspaces.isEmpty()) { return Optional.of( new ResourceImpl( WorkspaceResourceType.ID, accountWorkspaces.size(), WorkspaceResourceType.UNIT)); } else { return Optional.empty(); } } }
@VisibleForTesting void removeTemporaryWs() throws ServerException { for (WorkspaceImpl workspace : Pages.iterate( (maxItems, skipCount) -> workspaceDao.getWorkspaces(true, maxItems, skipCount))) { WorkspaceStatus status = runtimes.getStatus(workspace.getId()); if (status == WorkspaceStatus.STOPPED) { try { workspaceDao.remove(workspace.getId()); } catch (ServerException e) { LOG.error( "Unable to cleanup temporary workspace {}. Reason is {}", workspace.getId(), e.getMessage()); } } } } }
@Override public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final long currentlyUsedRuntimes = Pages.stream( (maxItems, skipCount) -> workspaceManagerProvider .get() .getByNamespace(account.getName(), false, maxItems, skipCount)) .filter(ws -> STOPPED != ws.getStatus()) .count(); if (currentlyUsedRuntimes > 0) { return Optional.of( new ResourceImpl( RuntimeResourceType.ID, currentlyUsedRuntimes, RuntimeResourceType.UNIT)); } else { return Optional.empty(); } } }
@Override public void onCascadeEvent(BeforeAccountRemovedEvent event) throws Exception { for (WorkspaceImpl workspace : iterate( (maxItems, skipCount) -> workspaceManager.getByNamespace( event.getAccount().getName(), false, maxItems, skipCount))) { workspaceManager.removeWorkspace(workspace.getId()); } } }
final Account account = accountManager.getById(accountId); List<WorkspaceImpl> activeWorkspaces = Pages.stream( (maxItems, skipCount) -> workspaceManagerProvider