Skip to content

Commit

Permalink
Clean up top level provenance class
Browse files Browse the repository at this point in the history
Add a builder, make immutable
  • Loading branch information
MrCreosote committed Apr 6, 2022
1 parent 84aa624 commit 9c0bfb4
Show file tree
Hide file tree
Showing 29 changed files with 945 additions and 550 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package legacy.performance;

import static us.kbase.workspace.test.WorkspaceTestCommon.basicProv;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStreamWriter;
Expand Down Expand Up @@ -36,7 +38,6 @@
import us.kbase.workspace.database.ByteArrayFileCacheManager.ByteArrayFileCache;
import us.kbase.workspace.database.ObjectIDNoWSNoVer;
import us.kbase.workspace.database.ObjectIdentifier;
import us.kbase.workspace.database.Provenance;
import us.kbase.workspace.database.ResourceUsageConfigurationBuilder;
import us.kbase.workspace.database.Types;
import us.kbase.workspace.database.Workspace;
Expand Down Expand Up @@ -106,7 +107,7 @@ public static void main(String[] args) throws Exception {
new WorkspaceSaveObject(//added obj name when autonaming removed
new ObjectIDNoWSNoVer(UUID.randomUUID().toString()
.replace("-", "")),
o, td, null, new Provenance(user), false)), fac);
o, td, null, basicProv(user), false)), fac);
o = null;

ObjectIdentifier oi = ObjectIdentifier.getBuilder(wsi).withName("auto1").build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package legacy.performance.us.kbase.workspace.performance.refsearch;

import static us.kbase.workspace.test.WorkspaceTestCommon.basicProv;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -26,7 +28,6 @@
import us.kbase.workspace.database.ObjectIDNoWSNoVer;
import us.kbase.workspace.database.ObjectIdentifier;
import us.kbase.workspace.database.ObjectInformation;
import us.kbase.workspace.database.Provenance;
import us.kbase.workspace.database.ResourceUsageConfigurationBuilder;
import us.kbase.workspace.database.Types;
import us.kbase.workspace.database.Workspace;
Expand All @@ -35,6 +36,7 @@
import us.kbase.workspace.database.WorkspaceUser;
import us.kbase.workspace.database.mongo.GridFSBlobStore;
import us.kbase.workspace.database.mongo.MongoWorkspaceDB;
import us.kbase.workspace.database.provenance.Provenance;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

Expand Down Expand Up @@ -147,7 +149,7 @@ private static void runBranchedReferencesTest() throws Exception {
WorkspaceUser u2 = new WorkspaceUser("brcu2");
final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(10).build().getFactory(null);
Provenance p = new Provenance(u1);
final Provenance p = basicProv(u1);
WorkspaceIdentifier read = new WorkspaceIdentifier("brcread");
WorkspaceIdentifier priv = new WorkspaceIdentifier("brcpriv");
for (int breadth = 1; breadth <= MAX_TREE_BREADTH; breadth++) {
Expand Down Expand Up @@ -184,7 +186,7 @@ private static List<ObjectInformation> generateReferences(
throws Exception {
final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(100000).build().getFactory(null);
Provenance p = new Provenance(user);
final Provenance p = basicProv(user);
List<WorkspaceSaveObject> objs = new LinkedList<WorkspaceSaveObject>();
for (ObjectInformation oi: increfs) {
String ref = oi.getWorkspaceId() + "/" + oi.getObjectId() + "/" + oi.getVersion();
Expand Down Expand Up @@ -217,7 +219,7 @@ private static void runLinearReferencesTest() throws Exception {

final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(10000).build().getFactory(null);
Provenance p = new Provenance(u1);
final Provenance p = basicProv(u1);
ObjectInformation o = WS.saveObjects(u1, priv, Arrays.asList(
new WorkspaceSaveObject(getRandomName(), new HashMap<String, String>(), LEAF_TYPE,
null, p, false)), fac).get(0);
Expand Down Expand Up @@ -250,7 +252,7 @@ private static ObjectInformation saveRefData(
refdata.put("refs", Arrays.asList(ref));
final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(10000).build().getFactory(null);
Provenance p = new Provenance(u1);
final Provenance p = basicProv(u1);
return WS.saveObjects(u1, priv, Arrays.asList(
new WorkspaceSaveObject(getRandomName(), refdata, REF_TYPE, null, p, false)), fac)
.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package legacy.performance.us.kbase.workspace.performance.refsearch.v0_3_5; // note package changed from orig

import static us.kbase.workspace.test.WorkspaceTestCommon.basicProv;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -17,23 +19,16 @@
import us.kbase.typedobj.core.AbsoluteTypeDefId;
import us.kbase.typedobj.core.TempFilesManager;
import us.kbase.typedobj.core.TypeDefName;
//import us.kbase.typedobj.core.TypedObjectValidator;
//import us.kbase.typedobj.db.MongoTypeStorage;
//import us.kbase.typedobj.db.TypeDefinitionDB;
import us.kbase.typedobj.idref.IdReferenceHandlerSetFactory;
import us.kbase.typedobj.idref.IdReferenceHandlerSetFactoryBuilder;
//import us.kbase.workspace.database.DefaultReferenceParser;
import us.kbase.workspace.database.ObjectIDNoWSNoVer;
import us.kbase.workspace.database.ObjectIdentifier;
import us.kbase.workspace.database.ObjectInformation;
import us.kbase.workspace.database.Provenance;
//import us.kbase.workspace.database.ResourceUsageConfigurationBuilder;
import us.kbase.workspace.database.Workspace;
import us.kbase.workspace.database.WorkspaceIdentifier;
import us.kbase.workspace.database.WorkspaceSaveObject;
import us.kbase.workspace.database.WorkspaceUser;
//import us.kbase.workspace.database.mongo.GridFSBlobStore;
//import us.kbase.workspace.database.mongo.MongoWorkspaceDB;
import us.kbase.workspace.database.provenance.Provenance;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

Expand Down Expand Up @@ -155,7 +150,7 @@ private static void runBranchedReferencesTest() throws Exception {
WorkspaceUser u2 = new WorkspaceUser("brcu2");
final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(10).build().getFactory(null);
Provenance p = new Provenance(u1);
final Provenance p = basicProv(u1);
WorkspaceIdentifier read = new WorkspaceIdentifier("brcread");
WorkspaceIdentifier priv = new WorkspaceIdentifier("brcpriv");
for (int breadth = 1; breadth <= 10; breadth++) {
Expand Down Expand Up @@ -194,7 +189,7 @@ private static List<ObjectInformation> generateReferences(
throws Exception {
final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(100000).build().getFactory(null);
Provenance p = new Provenance(user);
final Provenance p = basicProv(user);
List<WorkspaceSaveObject> objs = new LinkedList<WorkspaceSaveObject>();
for (ObjectInformation oi: increfs) {
String ref = oi.getWorkspaceId() + "/" + oi.getObjectId() + "/" + oi.getVersion();
Expand Down Expand Up @@ -228,7 +223,7 @@ private static void runLinearReferencesTest() throws Exception {

final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(10000).build().getFactory(null);
Provenance p = new Provenance(u1);
final Provenance p = basicProv(u1);
ObjectInformation o = WS.saveObjects(u1, priv, Arrays.asList(
new WorkspaceSaveObject(getRandomName(), new HashMap<String, String>(), LEAF_TYPE,
null, p, false)), fac).get(0);
Expand Down Expand Up @@ -261,7 +256,7 @@ private static ObjectInformation saveRefData(
refdata.put("refs", Arrays.asList(ref));
final IdReferenceHandlerSetFactory fac = IdReferenceHandlerSetFactoryBuilder
.getBuilder(10000).build().getFactory(null);
Provenance p = new Provenance(u1);
final Provenance p = basicProv(u1);
return WS.saveObjects(u1, priv, Arrays.asList(
new WorkspaceSaveObject(getRandomName(), refdata, REF_TYPE, null, p, false)), fac)
.get(0);
Expand Down
4 changes: 4 additions & 0 deletions src/us/kbase/common/test/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ public static Instant inst(final long epoch) {
return Instant.ofEpochMilli(epoch);
}

public static Instant now() {
return Instant.now();
}

public static final Optional<String> ES = Optional.empty();
public static final Optional<Long> EL = Optional.empty();
public static final Optional<Integer> EI = Optional.empty();
Expand Down
70 changes: 0 additions & 70 deletions src/us/kbase/workspace/database/Provenance.java

This file was deleted.

1 change: 1 addition & 0 deletions src/us/kbase/workspace/database/ResolvedSaveObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import us.kbase.typedobj.idref.IdReferenceType;
import us.kbase.typedobj.idref.RemappedId;
import us.kbase.workspace.database.WorkspaceUserMetadata.MetadataException;
import us.kbase.workspace.database.provenance.Provenance;

public class ResolvedSaveObject {

Expand Down
3 changes: 1 addition & 2 deletions src/us/kbase/workspace/database/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,6 @@ public List<ObjectInformation> saveObjects(
int objcount = 1;
for (WorkspaceSaveObject wo: objects) {
//maintain ordering
wo.getProvenance().setWorkspaceID(Long.valueOf(rwsi.getID()));
final List<Reference> provrefs = new LinkedList<Reference>();
for (final ProvenanceAction action: wo.getProvenance().getActions()) {
for (final String ref: action.getWorkspaceObjects()) {
Expand All @@ -751,7 +750,7 @@ public List<ObjectInformation> saveObjects(
}

final ValidatedTypedObject rep = reports.get(wo);
saveobjs.add(wo.resolve(rep, refs, provrefs, extractedIDs));
saveobjs.add(wo.resolve(rwsi, rep, refs, provrefs, extractedIDs));
ttlObjSize += rep.calculateRelabeledSize();
if (rep.getRelabeledSize() > rescfg.getMaxObjectSize()) {
throw new IllegalArgumentException(String.format(
Expand Down
1 change: 1 addition & 0 deletions src/us/kbase/workspace/database/WorkspaceObjectData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import us.kbase.typedobj.core.SubsetSelection;
import us.kbase.typedobj.idref.IdReferenceType;
import us.kbase.workspace.database.ByteArrayFileCacheManager.ByteArrayFileCache;
import us.kbase.workspace.database.provenance.Provenance;

/** A package containing (optionally) a workspace object's data along with provenance and
* information about the object.
Expand Down
21 changes: 18 additions & 3 deletions src/us/kbase/workspace/database/WorkspaceSaveObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
import us.kbase.typedobj.core.ValidatedTypedObject;
import us.kbase.typedobj.idref.IdReferenceType;
import us.kbase.typedobj.idref.RemappedId;
import us.kbase.workspace.database.provenance.Provenance;

public class WorkspaceSaveObject {

// TODO TEST unit tests
// TODO JAVADOC

// Unfortunately UObjects aren't necessarily immutable so there's no way to make this truly
// immutable - although maybe could check that the UObject wraps a JsonTokenStream in which
// case it should be immutable (double check)

private final ObjectIDNoWSNoVer id;
private final UObject data;
private final TypeDefId type;
Expand Down Expand Up @@ -69,13 +77,20 @@ public boolean isHidden() {

// provrefs *must* be in the same ordering as they are in the provenance actions
public ResolvedSaveObject resolve(
final ResolvedWorkspaceID wsid,
final ValidatedTypedObject rep,
final Set<Reference> references,
final List<Reference> provenancerefs,
final Map<IdReferenceType, Set<RemappedId>> extractedIDs) {
return new ResolvedSaveObject(this.id, this.userMeta,
this.provenance, this.hidden, rep, references,
provenancerefs, extractedIDs);
return new ResolvedSaveObject(
this.id,
this.userMeta,
this.provenance.updateWorkspaceID(wsid.getID()),
this.hidden,
rep,
references,
provenancerefs,
extractedIDs);
}

@Override
Expand Down
26 changes: 16 additions & 10 deletions src/us/kbase/workspace/database/mongo/MongoWorkspaceDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import us.kbase.workspace.database.Permission;
import us.kbase.workspace.database.PermissionSet;
import us.kbase.workspace.database.PermissionSet.Builder;
import us.kbase.workspace.database.Provenance;
import us.kbase.workspace.database.provenance.Provenance;
import us.kbase.workspace.database.Reference;
import us.kbase.workspace.database.ResolvedObjectID;
import us.kbase.workspace.database.ResolvedObjectIDNoVer;
Expand Down Expand Up @@ -2013,8 +2013,14 @@ private Map<String, String> emptyToNull(final Map<String, String> map) {
private Map<String, Object> toDocument(final Provenance p) {
final Map<String, Object> ret = new HashMap<>();
ret.put(Fields.PROV_USER, p.getUser().getUser());
ret.put(Fields.PROV_DATE, p.getDate());
ret.put(Fields.PROV_WS_ID, p.getWorkspaceID());
// this is a bit of a hack, but the JSON representation of an Instant is ~30 chars bigger
// than a Date and not always the same length, which screws with the size calculations in
// tests when writing this map to JSON (which is also a bit of a hack, but doesn't need to
// be byte level accurate). Both instants and dates are accepted by Mongo so just use a
// Date here.
// Might make more sense to write BSON vs. JSON for the size calcs.
ret.put(Fields.PROV_DATE, Date.from(p.getDate()));
ret.put(Fields.PROV_WS_ID, p.getWorkspaceID().orElse(null));
final List<Map<String, Object>> actions = new LinkedList<>();
ret.put(Fields.PROV_ACTIONS, actions);
for (final ProvenanceAction pa: p.getActions()) {
Expand All @@ -2032,7 +2038,7 @@ private Map<String, Object> toDocument(final Provenance p) {
paret.put(Fields.PROV_ACTION_SCRIPT_VER, pa.getScriptVersion().orElse(null));
paret.put(Fields.PROV_ACTION_SERVICE, pa.getServiceName().orElse(null));
paret.put(Fields.PROV_ACTION_SERVICE_VER, pa.getServiceVersion().orElse(null));
paret.put(Fields.PROV_ACTION_TIME, pa.getTime().orElse(null));
paret.put(Fields.PROV_ACTION_TIME, pa.getTime().map(t -> Date.from(t)).orElse(null));
paret.put(Fields.PROV_ACTION_WS_OBJS, emptyToNull(pa.getWorkspaceObjects()));

final List<Map<String, Object>> extdata = new LinkedList<>();
Expand Down Expand Up @@ -2745,11 +2751,11 @@ private Provenance toProvenance(
final List<String> resolvedRefs) {
// make a copy we can mutate
final List<String> rrcopy = new LinkedList<>(resolvedRefs);
final Provenance ret = new Provenance(
final Provenance.Builder ret = Provenance.getBuilder(
new WorkspaceUser(p.getString(Fields.PROV_USER)),
p.getDate(Fields.PROV_DATE));
// objects saved before version 0.4.1 will have null workspace IDs
ret.setWorkspaceID(p.getLong(Fields.PROV_WS_ID));
p.getDate(Fields.PROV_DATE).toInstant())
// objects saved before version 0.4.1 will have null workspace IDs
.withWorkspaceID(p.getLong(Fields.PROV_WS_ID));
for (final Document pa: p.getList(Fields.PROV_ACTIONS, Document.class)) {
@SuppressWarnings("unchecked")
final Map<String, String> precustom =
Expand All @@ -2764,7 +2770,7 @@ private Provenance toProvenance(
final List<String> actionRefs = new LinkedList<>(rrcopy.subList(0, refcnt));
rrcopy.subList(0, refcnt).clear();

ret.addAction(ProvenanceAction.getBuilder()
ret.withAction(ProvenanceAction.getBuilder()
.withExternalData(toExternalData(pa.getList(
Fields.PROV_ACTION_EXTERNAL_DATA, Document.class)))
.withSubActions(toSubAction(pa.getList(
Expand All @@ -2787,7 +2793,7 @@ private Provenance toProvenance(
.build()
);
}
return ret;
return ret.build();
}

private List<Object> getParamList(final Document provaction) {
Expand Down
4 changes: 4 additions & 0 deletions src/us/kbase/workspace/database/provenance/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ static <T> List<T> immutable(final List<T> list) {
*/
return Collections.unmodifiableList(new ArrayList<>(list));
}

static <T> List<T> getList(final List<T> list) {
return list == null ? Collections.emptyList() : list;
}
}
Loading

0 comments on commit 9c0bfb4

Please sign in to comment.