@Override public BrowseResult<Asset> browseAssets( final Repository repository, @Nullable final String filter, @Nullable final String sortProperty, @Nullable final String sortDirection, @Nullable final Integer start, @Nullable final Integer limit) { checkNotNull(repository); final List<Repository> repositories = getRepositories(repository); try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) { storageTx.begin(); QueryOptions options = new QueryOptions(filter, sortProperty, sortDirection, start, limit, repository.getName()); BrowseAssetsSqlBuilder builder = new BrowseAssetsSqlBuilder(options); return new BrowseResult<>( storageTx.countAssets( builder.buildWhereClause(), builder.buildSqlParams(), repositories, null), Lists.newArrayList( storageTx.findAssets( builder.buildWhereClause(), builder.buildSqlParams(), repositories, builder.buildQuerySuffix()))); } }
@Override public BrowseResult<Asset> browseComponentAssets( final Repository repository, final String componentId) { checkNotNull(repository); checkNotNull(componentId); try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) { storageTx.begin(); Component component = storageTx.findComponent(new DetachedEntityId(componentId)); if (component == null) { return new BrowseResult<>(0, Collections.emptyList()); } VariableResolverAdapter variableResolverAdapter = variableResolverAdapterManager.get(component.format()); List<Asset> assets = StreamSupport.stream(storageTx.browseAssets(component).spliterator(), false) .filter( (Asset asset) -> contentPermissionChecker.isPermitted( repository.getName(), asset.format(), BreadActions.BROWSE, variableResolverAdapter.fromAsset(asset))) .collect(Collectors.toList()); return new BrowseResult<>(assets.size(), assets); } }
@Override @Transactional public boolean delete(final String path) throws IOException { StorageTx tx = UnitOfWork.currentTransaction(); final Component component = getComponent(tx, path, tx.getBucket()); if (component == null) { return false; } tx.deleteComponent(component); return true; }
@Transactional(retryOn = {ONeedRetryException.class, ORecordDuplicatedException.class}) protected void doPutContent( final String path, final Supplier<InputStream> streamSupplier, final Payload payload) throws IOException { StorageTx tx = UnitOfWork.currentTransaction(); final Bucket bucket = tx.getBucket(); Component component = getComponent(tx, path, bucket); Asset asset; if (component == null) { // CREATE component = tx.createComponent(bucket, getRepository().getFormat()) .group(getGroup(path)) .name(getName(path)); // Set attributes map to contain "raw" format-specific metadata (in this case, path) component.formatAttributes().set(P_PATH, path); tx.saveComponent(component); asset = tx.createAsset(bucket, component); asset.name(component.name()); } else { // UPDATE asset = tx.firstAsset(component); } AttributesMap contentAttributes = null; if (payload instanceof Content) { contentAttributes = ((Content) payload).getAttributes(); } Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes)); tx.setBlob(asset, path, streamSupplier.get(), hashAlgorithms, null, payload.getContentType()); tx.saveAsset(asset); }
@Nullable @Override @Transactional(retryOn = IllegalStateException.class) public Content get(final String path) { StorageTx tx = UnitOfWork.currentTransaction(); final Component component = getComponent(tx, path, tx.getBucket()); if (component == null) { return null; } final Asset asset = tx.firstAsset(component); final Blob blob = tx.requireBlob(asset.requireBlobRef()); return marshall(asset, blob); }
private List<Bucket> getBuckets( final StorageTx storageTx, final Iterable<Repository> repositories) { checkNotNull(storageTx); checkNotNull(repositories); Iterable<Bucket> buckets = storageTx.findBuckets(repositories); if (buckets == null) { return Collections.emptyList(); } return Lists.newArrayList(buckets); }
@Override public BrowseResult<Component> browseComponents( final Repository repository, @Nullable final String filter, @Nullable final String sortProperty, @Nullable final String sortDirection, @Nullable final Integer start, @Nullable final Integer limit) { checkNotNull(repository); final List<Repository> repositories = getRepositories(repository); try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) { storageTx.begin(); List<Bucket> buckets = getBuckets(storageTx, repositories); QueryOptions options = new QueryOptions(filter, sortProperty, sortDirection, start, limit, repository.getName()); BrowseComponentsSqlBuilder builder = new BrowseComponentsSqlBuilder(groupType.equals(repository.getType()), buckets, options); return new BrowseResult<>( getCount(storageTx.browse(builder.buildCountSql(), builder.buildSqlParams())), getComponents(storageTx.browse(builder.buildBrowseSql(), builder.buildSqlParams()))); } }
@Override public BrowseResult<Asset> previewAssets( final List<Repository> repositories, final String jexlExpression, @Nullable final String filter, @Nullable final String sortProperty, @Nullable final String sortDirection, @Nullable final Integer start, @Nullable final Integer limit) { checkNotNull(repositories); checkNotNull(jexlExpression); final Repository repository = repositories.get(0); try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) { storageTx.begin(); List<Repository> previewRepositories; if (repositories.size() == 1 && groupType.equals(repository.getType())) { previewRepositories = repository.facet(GroupFacet.class).leafMembers(); } else { previewRepositories = repositories; } QueryOptions options = new QueryOptions(filter, sortProperty, sortDirection, start, limit, repository.getName()); PreviewAssetsSqlBuilder builder = new PreviewAssetsSqlBuilder( repository.getName(), jexlExpression, previewRepositories.stream().map(Repository::getName).collect(Collectors.toList()), options); return new BrowseResult<>( storageTx.countAssets( builder.buildWhereClause(), builder.buildSqlParams(), repositories, null), Lists.newArrayList( storageTx.findAssets( builder.buildWhereClause(), builder.buildSqlParams(), previewRepositories, builder.buildQuerySuffix()))); } }
@Override @Transactional(retryOn = ONeedRetryException.class) public void setCacheInfo(final String path, final CacheInfo cacheInfo) throws IOException { StorageTx tx = UnitOfWork.currentTransaction(); Component component = tx.findComponentWithProperty(P_PATH, path, tx.getBucket()); if (component == null) { log.debug("Attempting to set last verified date for non-existent raw component {}", path); } final Asset asset = tx.firstAsset(component); log.debug("Updating cacheInfo of {} to {}", path, cacheInfo); CacheInfo.applyToAsset(asset, cacheInfo); tx.saveAsset(tx.firstAsset(component)); }
// TODO: Consider a top-level indexed property (e.g. "locator") to make these common lookups fast private Component getComponent(StorageTx tx, String path, Bucket bucket) { String property = String.format("%s.%s.%s", P_ATTRIBUTES, RawFormat.NAME, P_PATH); return tx.findComponentWithProperty(property, path, bucket); }