/** * returns all graphs objects matching the given graphIds, that is visible * to the user. * * @param user to match visibility against. * @param graphIds the graphIds to get graphs for. * @return visible graphs from the given graphIds. */ public Collection<Graph> get(final User user, final List<String> graphIds) { if (null == user) { return Collections.emptyList(); } validateAllGivenGraphIdsAreVisibleForUser(user, graphIds); Stream<Graph> graphs = getStream(user, graphIds); if (null != graphIds) { graphs = graphs.sorted((g1, g2) -> graphIds.indexOf(g1.getGraphId()) - graphIds.indexOf(g2.getGraphId())); } final Set<Graph> rtn = graphs.collect(Collectors.toCollection(LinkedHashSet::new)); return Collections.unmodifiableCollection(rtn); }
private void validateExisting(final GraphSerialisable graph) throws StorageException { final String graphId = graph.getDeserialisedConfig().getGraphId(); for (final Set<Graph> graphs : storage.values()) { for (final Graph g : graphs) { if (g.getGraphId().equals(graphId)) { throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graphId))); } } } }
/** * @param user to match visibility against. * @param graphIds filter on graphIds * @return a stream of graphs for the given graphIds and the user has visibility for. * If graphIds is null then only enabled by default graphs are returned that the user can see. */ private Stream<Graph> getStream(final User user, final Collection<String> graphIds) { if (null == graphIds) { return storage.entrySet() .stream() .filter(entry -> isValidToView(user, entry.getKey())) .filter(entry -> !entry.getKey().isDisabledByDefault()) .flatMap(entry -> entry.getValue().stream()); } return storage.entrySet() .stream() .filter(entry -> isValidToView(user, entry.getKey())) .flatMap(entry -> entry.getValue().stream()) .filter(graph -> graphIds.contains(graph.getGraphId())); }
/** * Removes a graph from storage and returns the success. The given user * must * have visibility of the graph to be able to remove it. * * @param graphId the graphId to remove. * @param user to match visibility against. * @return if a graph was removed. * @see #isValidToView(User, FederatedAccess) */ public boolean remove(final String graphId, final User user) { boolean isRemoved = false; for (final Entry<FederatedAccess, Set<Graph>> entry : storage.entrySet()) { if (isValidToView(user, entry.getKey())) { final Set<Graph> graphs = entry.getValue(); if (null != graphs) { HashSet<Graph> remove = Sets.newHashSet(); for (final Graph graph : graphs) { if (graph.getGraphId().equals(graphId)) { remove.add(graph); deleteFromCache(graphId); isRemoved = true; } } graphs.removeAll(remove); } } } return isRemoved; }
private void addToCache(final Graph newGraph, final FederatedAccess access) { final String graphId = newGraph.getGraphId(); if (federatedStoreCache.contains(graphId)) { validateSameAsFromCache(newGraph, graphId); } else { try { federatedStoreCache.addGraphToCache(newGraph, access, false); } catch (final OverwritingException e) { throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); } catch (final CacheOperationException e) { throw new RuntimeException(e); } } }
private void validateSameAsFromCache(final Graph newGraph, final String graphId) { final Graph fromCache = federatedStoreCache.getGraphSerialisableFromCache(graphId).getGraph(graphLibrary); if (!newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties())) { throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.PROPERTIES.toString(), graphId)); } else { if (!JsonUtil.equals(newGraph.getSchema().toJson(false), fromCache.getSchema().toJson(false))) { throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.SCHEMA.toString(), graphId)); } else { if (!newGraph.getGraphId().equals(fromCache.getGraphId())) { throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, "GraphId", graphId)); } } } }
/** * Add the specified {@link Graph} to the cache. * * @param graph the {@link Graph} to be added * @param overwrite if true, overwrite any graphs already in the cache with the same ID * @param access Access for the graph being stored. * @throws CacheOperationException if there was an error trying to add to the cache */ public void addGraphToCache(final Graph graph, final FederatedAccess access, final boolean overwrite) throws CacheOperationException { Pair<GraphSerialisable, FederatedAccess> pair = new Pair<>(new GraphSerialisable.Builder().graph(graph).build(), access); final ICacheService service = CacheServiceLoader.getService(); try { if (overwrite) { service.putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), pair); } else { service.putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), pair); } } catch (final CacheOperationException e) { throw new CacheOperationException(String.format(ERROR_ADDING_GRAPH_TO_CACHE_GRAPH_ID_S, graph.getGraphId()), e); } }
/** * returns a set of {@link StoreTrait} that are common for all visible graphs. * traits1 = [a,b,c] * traits2 = [b,c] * traits3 = [a,b] * return [b] * * @param op the GetTraits operation * @param context the user context * @return the set of {@link StoreTrait} that are common for all visible graphs */ public Set<StoreTrait> getTraits(final GetTraits op, final Context context) { final Set<StoreTrait> traits = Sets.newHashSet(StoreTrait.values()); if (null != op && op.isCurrentTraits()) { final List<String> graphIds = FederatedStoreUtil.getGraphIds(op.getOptions()); final Stream<Graph> graphs = getStream(context.getUser(), graphIds); final GetTraits getTraits = op.shallowClone(); graphs.forEach(g -> { try { traits.retainAll(g.execute(getTraits, context)); } catch (final OperationException e) { throw new RuntimeException("Unable to fetch traits from graph " + g.getGraphId(), e); } }); } return traits; }
@Override public O doOperation(final OP operation, final Context context, final Store store) throws OperationException { final Collection<Graph> graphs = ((FederatedStore) store).getGraphs(context.getUser(), operation.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)); final List<O> results = new ArrayList<>(graphs.size()); for (final Graph graph : graphs) { final OP updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); if (null != updatedOp) { O execute = null; try { execute = graph.execute(updatedOp, context); } catch (final Exception e) { if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(updatedOp))) { throw new OperationException(FederatedStoreUtil.createOperationErrorMsg(operation, graph.getGraphId(), e), e); } } if (null != execute) { results.add(execute); } } } try { return mergeResults(results, operation, context, store); } catch (final Exception e) { throw new OperationException(e); } }
@Override public CloseableIterable<O_ITEM> doOperation(final FederatedOperationChain<I, O_ITEM> operation, final Context context, final Store store) throws OperationException { final Collection<Graph> graphs = ((FederatedStore) store).getGraphs(context.getUser(), operation.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)); final List<Object> results = new ArrayList<>(graphs.size()); for (final Graph graph : graphs) { final OperationChain opChain = operation.getOperationChain(); OperationHandlerUtil.updateOperationInput(opChain, operation.getInput()); final OperationChain updatedOp = FederatedStoreUtil.updateOperationForGraph(opChain, graph); if (null != updatedOp) { Object result = null; try { result = graph.execute(updatedOp, context); } catch (final Exception e) { if (!Boolean.valueOf(updatedOp.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE))) { throw new OperationException(FederatedStoreUtil.createOperationErrorMsg(operation, graph.getGraphId(), e), e); } } if (null != result) { results.add(result); } } } return mergeResults(results, operation, context, store); }
public Object doOperation(final Operation operation, final Context context, final Store store) throws OperationException { final Collection<Graph> graphs = ((FederatedStore) store).getGraphs(context.getUser(), operation.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)); for (final Graph graph : graphs) { final Operation updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); if (null != updatedOp) { try { graph.execute(updatedOp, context); } catch (final Exception e) { if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(updatedOp))) { throw new OperationException(FederatedStoreUtil.createOperationErrorMsg(operation, graph.getGraphId(), e), e); } } } } return null; } }
schemaBuilder.merge(g.execute(getSchema, context)); } catch (final OperationException e) { throw new RuntimeException("Unable to fetch schema from graph " + g.getGraphId(), e);
if (null == addElements.getInput()) { if (!addElements.isValidate() || !addElements.isSkipInvalidElements()) { LOGGER.debug("Invalid elements will be skipped when added to {}", graph.getGraphId()); resultOp = (OP) addElements.shallowClone(); ((AddElements) resultOp).setValidate(true);