/** * Return record identity of entity. */ public ORID recordIdentity(final T entity) { return recordIdentity(id(entity)); }
@Override public String getTokenFromId(final Entity entity) { return id(entity).getValue(); } }
private String documentId(final Asset asset) { return EntityHelper.id(asset).getValue(); }
private CapabilityIdentity identity(final CapabilityStorageItem item) { return CapabilityStorageItem.identity(EntityHelper.id(item)); }
private boolean bucketOwns(final Bucket bucket, @Nullable final MetadataNode<?> item) { return item != null && Objects.equals(id(bucket), item.bucketId()); }
public boolean exists(final ODatabaseDocumentTx db, final String name, final Bucket bucket) { Map<String, Object> params = ImmutableMap.of( P_NAME, checkNotNull(name), P_BUCKET, recordIdentity(id(checkNotNull(bucket))) ); return !isEmpty(db.command(EXISTS_QUERY).<Iterable<ODocument>>execute(params)); }
/** * Check for the existence of a component with {@code group}, {@code name}, and {@code version} in {@code bucket}. * * @since 3.8 */ public boolean exists(final ODatabaseDocumentTx db, @Nullable final String group, final String name, @Nullable final String version, final Bucket bucket) { Map<String, Object> params = Maps.newHashMap(); params.put(P_GROUP, group); params.put(P_NAME, checkNotNull(name)); params.put(P_VERSION, version); params.put(P_BUCKET, recordIdentity(id(checkNotNull(bucket)))); return !Iterables.isEmpty(db.command(EXISTS_QUERY).<Iterable<ODocument>>execute(params)); }
private Asset createAsset(final Bucket bucket, final String format) { checkNotNull(bucket); Asset asset = new Asset(); asset.bucketId(id(bucket)); asset.format(format); asset.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>())); return asset; }
@Override @Guarded(by = ACTIVE) public Asset createAsset(final Bucket bucket, final Component component) { checkNotNull(component); Asset asset = createAsset(bucket, component.format()); asset.componentId(id(component)); return asset; }
@Override @Guarded(by = ACTIVE) public Component createComponent(final Bucket bucket, final Format format) { checkNotNull(bucket); checkNotNull(format); Component component = componentFactory.createComponent(); component.bucketId(id(bucket)); component.format(format.toString()); component.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>())); return component; }
private void deleteAsset(final Asset asset, @Nullable final WritePolicy effectiveWritePolicy, final boolean deleteBlob) { checkNotNull(asset); if (deleteBlob) { BlobRef blobRef = asset.blobRef(); if (blobRef != null) { deleteBlob(blobRef, effectiveWritePolicy, format("Deleting asset %s", EntityHelper.id(asset))); } } assetEntityAdapter.deleteEntity(db, asset); }
/** * Finds fresh {@link Asset} instance from passed in TX by entity ID of the {@link Asset} used * to create passed in {@link Content} instance. The passed in {@link Content} must have been created with * method {@link #extractFromAsset(Asset, Iterable, AttributesMap)} to have proper attributes set for this operation. * * @see #extractFromAsset(Asset, Iterable, AttributesMap) */ @Nullable public static Asset findAsset(final StorageTx tx, final Bucket bucket, Content content) { final Asset contentAsset = content.getAttributes().require(Asset.class); if (EntityHelper.hasMetadata(contentAsset)) { return tx.findAsset(EntityHelper.id(contentAsset), bucket); } return null; }
/** * Deletes the existing blob for the asset if one exists, updating the blob updated field if necessary. The * write policy will be enforced for this operation and will throw an exception if updates are not supported. */ private void maybeDeleteBlob(final Asset asset, final WritePolicy effectiveWritePolicy) { DateTime now = DateTime.now(); if (asset.blobRef() != null) { // updating old blob if (!effectiveWritePolicy.checkUpdateAllowed()) { throw new IllegalOperationException("Repository does not allow updating assets: " + repositoryName); } asset.blobUpdated(now); deleteBlob(asset.blobRef(), effectiveWritePolicy, format("Updating asset %s", EntityHelper.id(asset))); } else { // creating new blob asset.blobCreated(now); asset.blobUpdated(now); } }
@Transactional protected NestedAttributesMap loadPackageRootFromCache(final Asset asset) throws IOException { StorageTx tx = UnitOfWork.currentTx(); Asset latest = tx.findAsset(id(asset), tx.findBucket(getRepository())); return loadPackageRoot(tx, latest); }
protected void maybeUpdateLastDownloaded(final StorageFacet storageFacet, final Asset asset) throws IOException { if (asset != null && asset.markAsDownloaded()) { TransactionalTouchMetadata.operation.withDb(storageFacet.txSupplier()) .throwing(IOException.class) .swallow(ORecordNotFoundException.class) .call(() -> { StorageTx tx = UnitOfWork.currentTx(); Asset updatedAsset = tx.findAsset(EntityHelper.id(asset)); updateLastDownloadedTime(tx, updatedAsset); return null; }); } }
private ComponentXO fromComponent(Component component, Repository repository) { String internalId = id(component).getValue(); ComponentXO componentXO = componentXOFactory.createComponentXO(); componentXO .setAssets(browseService.browseComponentAssets(repository, component.getEntityMetadata().getId().getValue()) .getResults() .stream() .map(asset -> fromAsset(asset, repository)) .collect(toList())); componentXO.setGroup(component.group()); componentXO.setName(component.name()); componentXO.setVersion(component.version()); componentXO.setId(new RepositoryItemIDXO(repository.getName(), internalId).getValue()); componentXO.setRepository(repository.getName()); componentXO.setFormat(repository.getFormat().getValue()); for (ComponentsResourceExtension componentsResourceExtension : componentsResourceExtensions) { componentXO = componentsResourceExtension.updateComponentXO(componentXO, component); } return componentXO; }
/** * Associates a {@link BrowseNode} with the given {@link Component}. */ public void createComponentNode(final ODatabaseDocumentTx db, final String repositoryName, final List<String> path, final Component component) { BrowseNode node = newNode(repositoryName, path); ODocument document = findNodeRecord(db, node); if (document == null) { // complete the new entity before persisting node.setComponentId(EntityHelper.id(component)); addEntity(db, node); } else { ORID oldComponentId = document.field(P_COMPONENT_ID, ORID.class); ORID newComponentId = componentEntityAdapter.recordIdentity(component); if (oldComponentId == null) { // shortcut: merge new information directly into existing record document.field(P_COMPONENT_ID, newComponentId); document.save(); } else if (!oldComponentId.equals(newComponentId)) { // retry in case this is due to an out-of-order delete event throw new BrowseNodeCollisionException("Node already has a component"); } } }
if (document == null) { node.setAssetId(EntityHelper.id(asset)); node.setAssetNameLowercase(lower(asset.name())); addEntity(db, node);