Skip to content

Commit

Permalink
Merge pull request #698 from MrCreosote/develop
Browse files Browse the repository at this point in the history
Push metadata representation to representation layer
  • Loading branch information
MrCreosote authored Oct 19, 2023
2 parents a092eb2 + fe8df81 commit 2cf8e87
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 153 deletions.
51 changes: 33 additions & 18 deletions src/us/kbase/workspace/WorkspaceServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public GetObjectOutput getObject(GetObjectParams params, AuthToken authPart, Rpc
resourcesToDelete.set(Arrays.asList(ret));
returnVal = new GetObjectOutput()
.withData(ret.getSerializedData().get().getUObject())
.withMetadata(objInfoToMetaTuple(ret.getObjectInfo(), true));
.withMetadata(objInfoToMetaTuple(ret.getObjectInfo(), true, false));
//END get_object
return returnVal;
}
Expand Down Expand Up @@ -868,8 +868,10 @@ public List<List<Tuple11<Long, String, String, String, Long, String, Long, Strin
List<List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String,String>>>> returnVal = null;
//BEGIN list_referencing_objects
final List<ObjectIdentifier> loi = processObjectIdentifiers(objectIds);
returnVal = translateObjectInfoList(ws.getReferencingObjects(
wsmeth.getUser(authPart), loi), false);
returnVal = translateObjectInfoList(
ws.getReferencingObjects(wsmeth.getUser(authPart), loi),
false,
false);
//END list_referencing_objects
return returnVal;
}
Expand Down Expand Up @@ -1045,7 +1047,7 @@ public List<Tuple12<String, String, String, Long, String, String, String, String
.withShowDeleted(longToBoolean(params.getShowDeletedObject()))
.withIncludeMetaData(true)
.build();
returnVal = objInfoToMetaTuple(ws.listObjects(lop), false);
returnVal = objInfoToMetaTuple(ws.listObjects(lop), false, false);
//END list_workspace_objects
return returnVal;
}
Expand Down Expand Up @@ -1085,9 +1087,15 @@ public Tuple12<String, String, String, Long, String, String, String, String, Str
final ObjectIdentifier oi = processObjectIdentifier(
params.getWorkspace(), null, params.getId(), null,
params.getInstance());
returnVal = objInfoToMetaTuple(ws.getObjectInformation(
wsmeth.getUser(params.getAuth(), authPart),
Arrays.asList(oi), true, false).get(0), true);
returnVal = objInfoToMetaTuple(
ws.getObjectInformation(
wsmeth.getUser(params.getAuth(), authPart),
Arrays.asList(oi),
true,
false)
.get(0),
true,
false);
//END get_objectmeta
return returnVal;
}
Expand All @@ -1111,9 +1119,11 @@ public List<Tuple11<Long, String, String, String, Long, String, Long, String, St
List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String,String>>> returnVal = null;
//BEGIN get_object_info
final List<ObjectIdentifier> loi = processObjectIdentifiers(objectIds);
final boolean includeMeta = longToBoolean(includeMetadata);
returnVal = objInfoToTuple(
ws.getObjectInformation(wsmeth.getUser(authPart), loi,
longToBoolean(includeMetadata), false), true);
ws.getObjectInformation(wsmeth.getUser(authPart), loi, includeMeta, false),
true,
!includeMeta);
//END get_object_info
return returnVal;
}
Expand All @@ -1134,10 +1144,15 @@ public List<Tuple11<Long, String, String, String, Long, String, Long, String, St
checkAddlArgs(params.getAdditionalProperties(), params.getClass());
final List<ObjectIdentifier> loi = processObjectSpecifications(
params.getObjects());
final boolean includeMeta = longToBoolean(params.getIncludeMetadata());
returnVal = objInfoToTuple(
ws.getObjectInformation(wsmeth.getUser(authPart), loi,
longToBoolean(params.getIncludeMetadata()),
longToBoolean(params.getIgnoreErrors())), true);
ws.getObjectInformation(
wsmeth.getUser(authPart),
loi,
includeMeta,
longToBoolean(params.getIgnoreErrors())),
true,
!includeMeta);
//END get_object_info_new
return returnVal;
}
Expand Down Expand Up @@ -1193,8 +1208,10 @@ public Tuple11<Long, String, String, String, Long, String, Long, String, String,
//BEGIN rename_object
checkAddlArgs(params.getAdditionalProperties(), params.getClass());
final ObjectIdentifier oi = processObjectIdentifier(params.getObj());
returnVal = objInfoToTuple(ws.renameObject(wsmeth.getUser(authPart),
oi, params.getNewName()), true);
returnVal = objInfoToTuple(
ws.renameObject(wsmeth.getUser(authPart), oi, params.getNewName()),
true,
true);
//END rename_object
return returnVal;
}
Expand All @@ -1214,8 +1231,7 @@ public Tuple11<Long, String, String, String, Long, String, Long, String, String,
checkAddlArgs(params.getAdditionalProperties(), params.getClass());
final ObjectIdentifier from = processObjectIdentifier(params.getFrom());
final ObjectIdentifier to = processObjectIdentifier(params.getTo());
returnVal = objInfoToTuple(ws.copyObject(
wsmeth.getUser(authPart), from, to), true);
returnVal = objInfoToTuple(ws.copyObject(wsmeth.getUser(authPart), from, to), true, false);
//END copy_object
return returnVal;
}
Expand All @@ -1235,8 +1251,7 @@ public Tuple11<Long, String, String, String, Long, String, Long, String, String,
Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String,String>> returnVal = null;
//BEGIN revert_object
final ObjectIdentifier oi = processObjectIdentifier(object);
returnVal = objInfoToTuple(ws.revertObject(
wsmeth.getUser(authPart), oi), true);
returnVal = objInfoToTuple(ws.revertObject(wsmeth.getUser(authPart), oi), true, false);
//END revert_object
return returnVal;
}
Expand Down
39 changes: 27 additions & 12 deletions src/us/kbase/workspace/database/ObjectInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,22 @@ public Optional<UncheckedUserMetadata> getUserMetaData() {
return Optional.ofNullable(usermeta);
}

/** Returns the user supplied and automatically generated metadata for the object as a map
* or null if no user metadata was provided.
* @return the metadata or null.
/** Returns the user supplied and automatically generated metadata for the object as a map.
*
* @param nullIfEmpty return null rather than an empty map if there is no metadata available.
* @return the metadata (which may be empty) or null.
*/
public Map<String, String> getUserMetaDataMapOrNull() {
return usermeta == null ? null : usermeta.getMetadata();
public Map<String, String> getUserMetaDataMap(final boolean nullIfEmpty) {
return getMetadataMap(usermeta, nullIfEmpty);
}

private Map<String, String> getMetadataMap(
final UncheckedUserMetadata meta,
boolean nullIfEmpty) {
if (meta == null) {
return nullIfEmpty ? null : Collections.emptyMap();
}
return meta.getMetadata();
}

/** Returns the administrative user supplied metadata for the object.
Expand All @@ -164,12 +174,13 @@ public Optional<UncheckedUserMetadata> getAdminUserMetaData() {
return Optional.ofNullable(adminmeta);
}

/** Returns the administrative user supplied metadata for the object as a map
* or null if no metadata was provided.
* @return the metadata or null.
/** Returns the administrative user supplied metadata for the object as a map.
* @param nullIfEmpty return null rather than an empty map if there is no metadata available.
* @return the metadata (which may be empty) or null.
*/
public Map<String, String> getAdminUserMetaDataMapOrNull() {
return adminmeta == null ? null : adminmeta.getMetadata();
public Map<String, String> getAdminUserMetaDataMap(final boolean nullIfEmpty) {
return getMetadataMap(adminmeta, nullIfEmpty);
}

/** Returns the resolved reference path to this object from a user-accessible object. There may
Expand Down Expand Up @@ -397,24 +408,28 @@ public Builder withSize(final long size) {
}

/** Add the optional user provided and / or autogenerated metadata to the builder.
*
* Empty metadata is treated the same way as a null argument.
*
* A null argument will remove any previously set metadata.
* @param metadata the metadata
* @return this builder for chaining.
*/
public Builder withUserMetadata(final UncheckedUserMetadata metadata) {
this.usermeta = metadata;
this.usermeta = metadata == null || metadata.isEmpty() ? null : metadata;
return this;
}

/** Add the optional administrative user provided metadata to the builder.
*
* Empty metadata is treated the same way as a null argument.
*
* A null argument will remove any previously set metadata.
* @param metadata the metadata
* @return this builder for chaining.
*/
public Builder withAdminUserMetadata(final UncheckedUserMetadata metadata) {
this.adminmeta = metadata;
this.adminmeta = metadata == null || metadata.isEmpty() ? null : metadata;
return this;
}

Expand Down
25 changes: 18 additions & 7 deletions src/us/kbase/workspace/database/UncheckedUserMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/** User (including, potentially administrative users) provided metadata container.
* This class performs no checks on the metadata and is intended for returning metadata to the
Expand All @@ -16,7 +17,7 @@
*/
public class UncheckedUserMetadata {

private Map<String, String> metadata;
private final Map<String, String> metadata;

/** Create a new unchecked metadata container
* @param meta a map containing metadata. If null, an empty metadata map is
Expand Down Expand Up @@ -48,6 +49,13 @@ public UncheckedUserMetadata(final WorkspaceUserMetadata meta) {
public Map<String, String> getMetadata() {
return Collections.unmodifiableMap(metadata);
}

/** Returns true if there is no metadata.
* @return true if there is no metadata.
*/
public boolean isEmpty() {
return metadata.isEmpty();
}

@Override
public String toString() {
Expand All @@ -60,18 +68,21 @@ public String toString() {

@Override
public int hashCode() {
return 31 + metadata.hashCode();
return Objects.hash(metadata);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
final UncheckedUserMetadata other = (UncheckedUserMetadata) obj;
return metadata.equals(other.metadata);
}
UncheckedUserMetadata other = (UncheckedUserMetadata) obj;
return Objects.equals(metadata, other.metadata);
}
}
4 changes: 4 additions & 0 deletions src/us/kbase/workspace/database/mongo/MongoWorkspaceDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,10 @@ public Instant lockWorkspace(final ResolvedWorkspaceID rwsi)
return Instant.now();
}

// hmm, is there a reason we list the fields rather than just pulling everything?
// seems bug prone
// TODO CODE maybe a field generator that deals with standard field sets? Then only have
// set and test in one place. A few places adding meta fields for instance
private static final Set<String> FLDS_VER_COPYOBJ = newHashSet(
// include full type name for rollback purposes, remove later
Fields.VER_WS_ID, Fields.VER_ID, Fields.VER_VER, Fields.VER_TYPE_FULL,
Expand Down
3 changes: 1 addition & 2 deletions src/us/kbase/workspace/database/mongo/ObjectInfoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ static Map<String, String> metaMongoArrayToHash(
return ret;
}

static List<Map<String, String>> metaHashToMongoArray(
final Map<String, String> usermeta) {
static List<Map<String, String>> metaHashToMongoArray(final Map<String, String> usermeta) {
final List<Map<String, String>> meta = new ArrayList<>();
if (usermeta != null) {
for (String key: usermeta.keySet()) {
Expand Down
29 changes: 17 additions & 12 deletions src/us/kbase/workspace/kbase/ArgUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,24 @@ public static List<Tuple7<String, String, String, Long, String, String, Long>> w
Long, String, String, Long, Map<String, String>>
objInfoToTuple(
final ObjectInformation info,
final boolean logObjects) {
final boolean logObjects,
final boolean nullForEmptyMeta) {
final List<ObjectInformation> m = new ArrayList<ObjectInformation>();
m.add(info);
return objInfoToTuple(m, logObjects).get(0);
return objInfoToTuple(m, logObjects, nullForEmptyMeta).get(0);
}

public static List<List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>>>
translateObjectInfoList(
final List<Set<ObjectInformation>> lsoi,
final boolean logObjects) {
final boolean logObjects,
final boolean nullForEmptyMeta) {
final List<List<Tuple11<Long, String, String, String, Long, String,
Long, String, String, Long, Map<String, String>>>> ret =
new LinkedList<List<Tuple11<Long,String,String,String,Long,String,Long,String,String,Long,Map<String,String>>>>();
for (Set<ObjectInformation> soi: lsoi) {
ret.add(objInfoToTuple(new LinkedList<ObjectInformation>(soi),
logObjects));
logObjects, nullForEmptyMeta));
}
return ret;
}
Expand All @@ -314,7 +316,8 @@ public static List<Tuple7<String, String, String, Long, String, String, Long>> w
Long, String, String, Long, Map<String, String>>>
objInfoToTuple(
final List<ObjectInformation> info,
final boolean logObjects) {
final boolean logObjects,
final boolean nullForEmptyMeta) {

//oh the humanity
final List<Tuple11<Long, String, String, String, Long, String,
Expand Down Expand Up @@ -342,7 +345,7 @@ public static List<Tuple7<String, String, String, Long, String, String, Long>> w
.withE8(m.getWorkspaceName())
.withE9(m.getCheckSum())
.withE10(m.getSize())
.withE11(m.getUserMetaDataMapOrNull()));
.withE11(m.getUserMetaDataMap(nullForEmptyMeta)));
}
}
return ret;
Expand All @@ -353,17 +356,19 @@ public static List<Tuple7<String, String, String, Long, String, String, Long>> w
String, String, String, Map<String, String>, Long>
objInfoToMetaTuple(
final ObjectInformation info,
final boolean logObjects) {
final boolean logObjects,
final boolean nullForEmptyMeta) {
final List<ObjectInformation> m = new ArrayList<ObjectInformation>();
m.add(info);
return objInfoToMetaTuple(m, logObjects).get(0);
return objInfoToMetaTuple(m, logObjects, nullForEmptyMeta).get(0);
}

public static List<Tuple12<String, String, String, Long, String, String, String,
String, String, String, Map<String, String>, Long>>
objInfoToMetaTuple(
final List<ObjectInformation> info,
final boolean logObjects) {
final boolean logObjects,
final boolean nullForEmptyMeta) {
//oh the humanity
final List<Tuple12<String, String, String, Long, String, String, String,
String, String, String, Map<String, String>, Long>> ret =
Expand All @@ -387,7 +392,7 @@ public static List<Tuple7<String, String, String, Long, String, String, Long>> w
.withE8(m.getWorkspaceName())
.withE9("")//ref is deprecated
.withE10(m.getCheckSum())
.withE11(m.getUserMetaDataMapOrNull())
.withE11(m.getUserMetaDataMap(nullForEmptyMeta))
.withE12(m.getObjectId()));
}
return ret;
Expand Down Expand Up @@ -433,7 +438,7 @@ public static List<ObjectData> translateObjectData(
}
ret.add(new ObjectData()
.withData(data)
.withInfo(objInfoToTuple(o.getObjectInfo(), logObjects))
.withInfo(objInfoToTuple(o.getObjectInfo(), logObjects, false))
.withPath(toObjectPath(o.getObjectInfo().getReferencePath()))
.withProvenance(translateProvenanceActions(o.getProvenance().getActions()))
.withCreator(o.getProvenance().getUser().getUser())
Expand Down Expand Up @@ -489,7 +494,7 @@ public static List<us.kbase.workspace.ObjectProvenanceInfo> translateObjectProvI
final PermError error = makeExternalIDsReadable(
Arrays.asList(o), Optional.of(permHandler)).get(o);
ret.add(new us.kbase.workspace.ObjectProvenanceInfo()
.withInfo(objInfoToTuple(o.getObjectInfo(), logObjects))
.withInfo(objInfoToTuple(o.getObjectInfo(), logObjects, false))
.withProvenance(translateProvenanceActions(o.getProvenance().getActions()))
.withCreator(o.getProvenance().getUser().getUser())
.withOrigWsid(o.getProvenance().getWorkspaceID().orElse(null))
Expand Down
Loading

0 comments on commit 2cf8e87

Please sign in to comment.