Skip to content

Commit

Permalink
Return resolved objects from set admin data methods
Browse files Browse the repository at this point in the history
Needed for logging purposes
  • Loading branch information
MrCreosote committed Oct 24, 2023
1 parent e07e8d7 commit 2582975
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/us/kbase/workspace/database/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -1634,18 +1634,20 @@ public long setWorkspaceDeleted(
* searches are not allowed (which are not necessary for admins in any case).
*
* @param update the metadata updates to apply.
* @return A mapping of the input objects to their resolved counterparts.
* @throws WorkspaceCommunicationException if a communication error occurs when contacting the
* storage system.
* @throws CorruptWorkspaceDBException if corrupt data is found in the storage system.
* @throws InaccessibleObjectException if any of the objects are not accessible.
* @throws NoSuchObjectException if any of the objects do not exist.
*/
public void setAdminObjectMetadata(final Map<ObjectIdentifier, MetadataUpdate> update)
public Map<ObjectIdentifier, ResolvedObjectID> setAdminObjectMetadata(
final Map<ObjectIdentifier, MetadataUpdate> update)
throws WorkspaceCommunicationException, InaccessibleObjectException,
CorruptWorkspaceDBException, NoSuchObjectException {
noNulls(requireNonNull(update, "update").keySet(), "null found in update keys");
if (update.isEmpty()) {
return;
return Collections.emptyMap();
}
for (final ObjectIdentifier oi: update.keySet()) {
if (oi.isLookupRequired() || oi.hasRefPath()) {
Expand All @@ -1660,9 +1662,12 @@ public void setAdminObjectMetadata(final Map<ObjectIdentifier, MetadataUpdate> u
final Map<ObjectIdentifier, ObjectIDResolvedWS> res = PermissionsCheckerFactory
.getBuilder(db).withAsAdmin(true).build()
.getObjectChecker(update.keySet(), Permission.WRITE).check();
db.setAdminObjectMeta(update.entrySet().stream()
final Map<ObjectIDResolvedWS, ResolvedObjectID> map = db.setAdminObjectMeta(
update.entrySet().stream()
.collect(Collectors.toMap(s -> res.get(s.getKey()), s -> s.getValue())));
// YAGNI a listener for this most likely. Add if needed
return update.keySet().stream()
.collect(Collectors.toMap(oi -> oi, oi -> map.get(res.get(oi))));
}

/* admin method only, should not be exposed in public API
Expand Down
4 changes: 3 additions & 1 deletion src/us/kbase/workspace/database/WorkspaceDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ Optional<Instant> setWorkspaceMeta(ResolvedWorkspaceID wsid, MetadataUpdate meta
/** Sets administrative metadata on one or more objects, overwriting existing keys if
* duplicate keys are supplied.
* @param update object identifiers mapped to the metadata update for each object.
* @return a mapping of the input objects to the resolved objects.
* @throws NoSuchObjectException if any of the objects don't exist.
* @throws WorkspaceCommunicationException if a communication error occurs.
* @throws CorruptWorkspaceDBException if the workspace database is corrupt.
* @throws IllegalArgumentException if no metadata is supplied or the
* updated metadata exceeds the allowed size.
*/
void setAdminObjectMeta(Map<ObjectIDResolvedWS, MetadataUpdate> update)
Map<ObjectIDResolvedWS, ResolvedObjectID> setAdminObjectMeta(
Map<ObjectIDResolvedWS, MetadataUpdate> update)
throws NoSuchObjectException, WorkspaceCommunicationException,
CorruptWorkspaceDBException;

Expand Down
4 changes: 3 additions & 1 deletion src/us/kbase/workspace/database/mongo/MongoWorkspaceDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ public Optional<Instant> setWorkspaceMeta(


@Override
public void setAdminObjectMeta(final Map<ObjectIDResolvedWS, MetadataUpdate> update)
public Map<ObjectIDResolvedWS, ResolvedObjectID> setAdminObjectMeta(
final Map<ObjectIDResolvedWS, MetadataUpdate> update)
throws NoSuchObjectException, WorkspaceCommunicationException,
CorruptWorkspaceDBException {
noNulls(requireNonNull(update, "update").keySet(), "null object ID in update");
Expand Down Expand Up @@ -770,6 +771,7 @@ public void setAdminObjectMeta(final Map<ObjectIDResolvedWS, MetadataUpdate> upd
throw new IllegalArgumentException(err, e);
}
}
return oids;
}

private Optional<Instant> setMetadataOnDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import us.kbase.workspace.database.ObjectInformation;
import us.kbase.workspace.database.Permission;
import us.kbase.workspace.database.Reference;
import us.kbase.workspace.database.ResolvedObjectID;
import us.kbase.workspace.database.exceptions.CorruptWorkspaceDBException;
import us.kbase.workspace.database.exceptions.NoObjectDataException;
import us.kbase.workspace.database.exceptions.NoSuchObjectException;
Expand Down Expand Up @@ -1074,15 +1075,23 @@ public void setAdminObjectMetaAndGetObjectInfo() throws Exception {
final ObjectIDResolvedWS oi3_1 = new ObjectIDResolvedWS(rwsi, "newobj3", 1);

// 1st round - add metadata
mocks.mdb.setAdminObjectMeta(ImmutableMap.of(
oi1_1, new MetadataUpdate(
new WorkspaceUserMetadata(ImmutableMap.of("foo", "bar")), null),
oi1_2, new MetadataUpdate(
new WorkspaceUserMetadata(ImmutableMap.of("a", "b", "c", "d")), null),
oi2_1, new MetadataUpdate(
new WorkspaceUserMetadata(ImmutableMap.of("x", "y")), null)
final Map<ObjectIDResolvedWS, ResolvedObjectID> res = mocks.mdb.setAdminObjectMeta(
ImmutableMap.of(
oi1_1, new MetadataUpdate(
new WorkspaceUserMetadata(ImmutableMap.of("foo", "bar")), null),
oi1_2, new MetadataUpdate(
new WorkspaceUserMetadata(
ImmutableMap.of("a", "b", "c", "d")), null),
oi2_1, new MetadataUpdate(
new WorkspaceUserMetadata(ImmutableMap.of("x", "y")), null)
));

assertThat("incorrect result", res, is(ImmutableMap.of(
oi1_1, new ResolvedObjectID(rwsi, 1, 1, "newobj", false),
oi1_2, new ResolvedObjectID(rwsi, 1, 2, "newobj", false),
oi2_1, new ResolvedObjectID(rwsi, 2, 1, "newobj2", false)
)));

final Map<ObjectIDResolvedWS, ObjectInformation> infos = mocks.mdb.getObjectInformation(
set(oi1_1, oi1_2, oi2_1, oi3_1), true, true, false, true);

Expand Down
14 changes: 11 additions & 3 deletions src/us/kbase/workspace/test/workspace/WorkspaceUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import us.kbase.workspace.database.ObjectInformation;
import us.kbase.workspace.database.Permission;
import us.kbase.workspace.database.PermissionSet;
import us.kbase.workspace.database.ResolvedObjectID;
import us.kbase.workspace.database.ResolvedWorkspaceID;
import us.kbase.workspace.database.ResourceUsageConfigurationBuilder;
import us.kbase.workspace.database.ResourceUsageConfigurationBuilder.ResourceUsageConfiguration;
Expand Down Expand Up @@ -768,8 +769,10 @@ private Exception failSaveObjects(
@Test
public void setAdminObjectMetadataNoop() throws Exception {
final TestMocks mocks = initMocks();
mocks.ws.setAdminObjectMetadata(Collections.emptyMap());
final Map<ObjectIdentifier, ResolvedObjectID> res = mocks.ws.setAdminObjectMetadata(
Collections.emptyMap());

assertThat("incorrect result", res, is(Collections.emptyMap()));
verify(mocks.db, never()).setAdminObjectMeta(any());
verify(mocks.db, never()).resolveWorkspaces(any(), anyBoolean());
}
Expand All @@ -786,15 +789,20 @@ public void setAdminObjectMetadata() throws Exception {
final MetadataUpdate mu2 = new MetadataUpdate(null, Arrays.asList("fhang"));
final ObjectIdentifier oi1 = ObjectIdentifier.getBuilder("6/4").build();
final ObjectIDResolvedWS roi1 = new ObjectIDResolvedWS(rwsi1, 4);
final ResolvedObjectID rroi1 = new ResolvedObjectID(rwsi1, 4, 8, "stuff", false);
final ObjectIdentifier oi2 = ObjectIdentifier.getBuilder("myws/obj/3").build();
final ObjectIDResolvedWS roi2 = new ObjectIDResolvedWS(rwsi2, "obj", 3);
final ResolvedObjectID rroi2 = new ResolvedObjectID(rwsi2, 89, 3, "obj", false);

when(mocks.db.resolveWorkspaces(set(wsi1, wsi2), false)).thenReturn(ImmutableMap.of(
wsi1, rwsi1, wsi2, rwsi2));
when(mocks.db.setAdminObjectMeta(ImmutableMap.of(roi1, mu1, roi2, mu2))).thenReturn(
ImmutableMap.of(roi1, rroi1, roi2, rroi2));

mocks.ws.setAdminObjectMetadata(ImmutableMap.of(oi1, mu1, oi2, mu2));
final Map<ObjectIdentifier, ResolvedObjectID> res = mocks.ws.setAdminObjectMetadata(
ImmutableMap.of(oi1, mu1, oi2, mu2));

verify(mocks.db).setAdminObjectMeta(ImmutableMap.of(roi1, mu1, roi2, mu2));
assertThat("incorrect result", res, is(ImmutableMap.of(oi1, rroi1, oi2, rroi2)));
}

@Test
Expand Down

0 comments on commit 2582975

Please sign in to comment.