@Override public String getTokenFromId(final Entity entity) { return id(entity).getValue(); } }
/** * 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; }
/** * Check if given entity is attached. */ public static boolean isAttached(final Entity entity) { return EntityHelper.metadata(entity) instanceof AttachedEntityMetadata; }
Iterable<T> browseByBucket(final ODatabaseDocumentTx db, final Bucket bucket) { checkNotNull(bucket); checkState(EntityHelper.hasMetadata(bucket)); Map<String, Object> parameters = ImmutableMap.<String, Object>of( P_BUCKET, bucketEntityAdapter.recordIdentity(bucket) ); String query = String.format( "select from %s where %s = :bucket", getTypeName(), P_BUCKET ); Iterable<ODocument> docs = OrientAsyncHelper.asyncIterable(db, query, parameters); return transform(docs); }
private void updateRevision(final NestedAttributesMap packageRoot, final Asset packageRootAsset, final boolean createdPackageRoot) { String newRevision = "1"; if (!createdPackageRoot) { if (packageRoot.contains(NpmMetadataUtils.META_REV)) { String rev = packageRoot.get(NpmMetadataUtils.META_REV, String.class); newRevision = Integer.toString(Integer.parseInt(rev) + 1); } else { /* This is covering the edge case when a new package is uploaded to a repository where the packageRoot already exists. If that packageRoot was created using an earlier version of NXRM where we didn't store the rev then we need to add it in. We also add the rev in on download but it is possible that someone is uploading a package where the packageRoot has never been downloaded before. */ newRevision = EntityHelper.version(packageRootAsset).getValue(); } } packageRoot.set(NpmMetadataUtils.META_ID, packageRootAsset.name()); packageRoot.set(NpmMetadataUtils.META_REV, newRevision); }
@Override @Guarded(by = ACTIVE) public void saveComponent(final Component component) { if (EntityHelper.hasMetadata(component)) { componentEntityAdapter.editEntity(db, component); } else { componentEntityAdapter.addEntity(db, component); } }
/** * For NEXUS-18094 we moved the revision number to live in the package root file so that the revision number doesn't * change as the database record changes (previously it used the Orient Document Version number). This method allows * us to avoid the need for an upgrade step by upgrading package roots without a rev as they are fetched. */ @TransactionalStoreBlob protected void maybeAddRevision(final Asset packageRootAsset, final NestedAttributesMap packageRoot) { if (!packageRoot.contains(NpmMetadataUtils.META_REV)) { String newRevision = EntityHelper.version(packageRootAsset).getValue(); packageRoot.set(NpmMetadataUtils.META_ID, packageRootAsset.name()); packageRoot.set(NpmMetadataUtils.META_REV, newRevision); try { NpmFacetUtils.savePackageRoot(UnitOfWork.currentTx(), packageRootAsset, packageRoot); } catch (IOException e) { log.error("Failed to update revision in package root {}", packageRoot.get(NpmMetadataUtils.NAME), e); } } }
/** * Return record identity of entity. */ public ORID recordIdentity(final T entity) { return recordIdentity(id(entity)); }
@Override @Guarded(by = ACTIVE) public void saveAsset(final Asset asset) { if (EntityHelper.hasMetadata(asset)) { assetEntityAdapter.editEntity(db, asset); } else { assetEntityAdapter.addEntity(db, asset); } }
/** * Returns attached metadata of entity. */ public static AttachedEntityMetadata metadata(final Entity entity) { EntityMetadata metadata = EntityHelper.metadata(entity); checkState(metadata instanceof AttachedEntityMetadata, "Entity not attached"); return (AttachedEntityMetadata) metadata; }
private String documentId(final Asset asset) { return EntityHelper.id(asset).getValue(); }
@Override public void save(final Iterable<Asset> assets) { inTxRetry(databaseInstance).run(db -> { assets.forEach(asset -> { if (hasMetadata(asset)) { entityAdapter.editEntity(db, asset); } else { entityAdapter.addEntity(db, asset); } }); }); } }
private CapabilityIdentity identity(final CapabilityStorageItem item) { return CapabilityStorageItem.identity(EntityHelper.id(item)); }
Iterable<Asset> browseByComponent(final ODatabaseDocumentTx db, final Component component) { checkNotNull(component); checkState(EntityHelper.hasMetadata(component)); Map<String, Object> parameters = ImmutableMap.of( "bucket", bucketEntityAdapter.recordIdentity(component.bucketId()), "component", componentEntityAdapter.recordIdentity(component) ); String query = String.format( "select from %s where %s = :bucket and %s = :component", DB_CLASS, P_BUCKET, P_COMPONENT ); Iterable<ODocument> docs = db.command(new OCommandSQL(query)).execute(parameters); return transform(docs); }
private boolean bucketOwns(final Bucket bucket, @Nullable final MetadataNode<?> item) { return item != null && Objects.equals(id(bucket), item.bucketId()); }
@Override @Guarded(by = STARTED) public Asset save(Asset asset) { if (hasMetadata(asset)) { inTxRetry(databaseInstance).run(db -> entityAdapter.editEntity(db, asset)); return asset; } else { return inTxRetry(databaseInstance).call(db -> entityAdapter.readEntity(entityAdapter.addEntity(db, asset))); } }
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; }