Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plate cache updates and DB serialization clean up. #5162

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions assay/api-src/org/labkey/api/assay/plate/Plate.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public interface Plate extends PropertySet, Identifiable

boolean isTemplate();

@NotNull PlateType getPlateTypeObject();
@NotNull PlateType getPlateType();
labkey-nicka marked this conversation as resolved.
Show resolved Hide resolved

@Nullable PlateSet getPlateSetObject();
@Nullable PlateSet getPlateSet();

@NotNull String getPlateId();

/**
* Returns an existing well, or creates a new well if one
Expand Down
16 changes: 16 additions & 0 deletions assay/api-src/org/labkey/api/assay/plate/PlateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ static PlateService get()
*/
@Nullable Plate getPlate(Container container, int rowId);

/**
* Gets a plate instance object by plate id.
* @param container The plate's container.
* @param plateId The plate id of the plate.
* @return The requested plate, or null if no plate exists with the specified plate id.
*/
@Nullable Plate getPlate(Container container, String plateId);

/**
* Gets a plate instance object by row id.
* @param cf The container filter to find the plate
Expand All @@ -146,6 +154,14 @@ static PlateService get()
*/
@Nullable Plate getPlate(ContainerFilter cf, Lsid lsid);

/**
* Gets a plate instance object by plate id.
* @param cf The container filter to find the plate
* @param plateId The plate id of the plate.
* @return The requested plate, or null if no plate exists with the specified plate id.
*/
@Nullable Plate getPlate(ContainerFilter cf, String plateId);

/**
* Gets all plate templates for the specified container. Plate templates are Plate instances
* which have their template field set to TRUE.
Expand Down
5 changes: 1 addition & 4 deletions assay/src/org/labkey/assay/AssayDomainServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.labkey.api.assay.AssayDomainService;
import org.labkey.api.assay.AssayProvider;
import org.labkey.api.assay.AssayQCService;
import org.labkey.api.assay.AssaySchema;
import org.labkey.api.assay.AssayService;
import org.labkey.api.assay.DetectionMethodAssayProvider;
import org.labkey.api.assay.plate.Plate;
Expand Down Expand Up @@ -58,9 +57,7 @@
import org.labkey.api.gwt.client.model.GWTContainer;
import org.labkey.api.gwt.client.model.GWTDomain;
import org.labkey.api.gwt.client.model.GWTPropertyDescriptor;
import org.labkey.api.query.QueryChangeListener;
import org.labkey.api.query.QueryService;
import org.labkey.api.query.SchemaKey;
import org.labkey.api.query.ValidationException;
import org.labkey.api.security.SecurityPolicy;
import org.labkey.api.security.User;
Expand Down Expand Up @@ -484,7 +481,7 @@ public GWTProtocol saveChanges(GWTProtocol assay, boolean replaceIfExisting) thr
if (provider instanceof PlateBasedAssayProvider && assay.getSelectedPlateTemplate() != null)
{
PlateBasedAssayProvider plateProvider = (PlateBasedAssayProvider)provider;
Plate plate = PlateManager.get().getPlate(getContainer(), assay.getSelectedPlateTemplate());
Plate plate = PlateManager.get().getPlateByName(getContainer(), assay.getSelectedPlateTemplate());
if (plate != null)
plateProvider.setPlate(getContainer(), protocol, plate);
else
Expand Down
2 changes: 1 addition & 1 deletion assay/src/org/labkey/assay/AssayUpgradeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private static Plate getPlate(ExpProtocol protocol)
{
// resolve plate by the legacy deprecated plate name method
ObjectProperty prop = protocol.getObjectProperties().get(protocol.getLSID() + AbstractPlateBasedAssayProvider.PLATE_TEMPLATE_SUFFIX);
return prop != null ? PlateManager.get().getPlate(protocol.getContainer(), prop.getStringValue()) : null;
return prop != null ? PlateManager.get().getPlateByName(protocol.getContainer(), prop.getStringValue()) : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, did you verify these changes through the upgrade(s)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I ran the upgrade manually, but you have to revert the protocol property to use the plate name unless you upgrade from an older version. Did you see an error on your end?

}

/**
Expand Down
3 changes: 3 additions & 0 deletions assay/src/org/labkey/assay/PlateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ public static class CopyTemplateBean

public CopyTemplateBean(final Container container, final User user, final Integer plateId, final String selectedDestination)
{
if (plateId == null)
throw new IllegalArgumentException("Plate ID cannot be null");

_plate = PlateService.get().getPlate(container, plateId);
if (_plate == null)
throw new IllegalStateException("Could not resolve the plate with ID : " + plateId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.labkey.api.security.User;
import org.labkey.api.util.JsonUtil;
import org.labkey.assay.TSVProtocolSchema;
import org.labkey.assay.plate.model.Well;
import org.labkey.assay.plate.model.WellBean;
import org.labkey.assay.query.AssayDbSchema;

import java.io.File;
Expand Down Expand Up @@ -297,11 +297,11 @@ public List<Map<String, Object>> mergePlateMetadata(
if (!plate.getCustomFields().isEmpty())
{
// create the map of well locations to the well
Map<Position, Well> positionToWell = new HashMap<>();
Map<Position, WellBean> positionToWell = new HashMap<>();

SimpleFilter filter = SimpleFilter.createContainerFilter(plate.getContainer());
filter.addCondition(FieldKey.fromParts("PlateId"), plate.getRowId());
for (Well well : new TableSelector(AssayDbSchema.getInstance().getTableInfoWell(), filter, null).getArrayList(Well.class))
for (WellBean well : new TableSelector(AssayDbSchema.getInstance().getTableInfoWell(), filter, null).getArrayList(WellBean.class))
positionToWell.put(new PositionImpl(plate.getContainer(), well.getRow(), well.getCol()), well);

for (Map<String, Object> row : mergedRows)
Expand Down Expand Up @@ -502,7 +502,7 @@ public OntologyManager.UpdateableTableImportHelper getImportHelper(

SimpleFilter filter = SimpleFilter.createContainerFilter(plate.getContainer());
filter.addCondition(FieldKey.fromParts("PlateId"), plate.getRowId());
for (Well well : new TableSelector(AssayDbSchema.getInstance().getTableInfoWell(), filter, null).getArrayList(Well.class))
for (WellBean well : new TableSelector(AssayDbSchema.getInstance().getTableInfoWell(), filter, null).getArrayList(WellBean.class))
positionToWellLsid.put(new PositionImpl(plate.getContainer(), well.getRow(), well.getCol()), Lsid.parse(well.getLsid()));

return new PlateMetadataImportHelper(plate.getContainer(), data, Collections.unmodifiableMap(positionToWellLsid));
Expand Down
44 changes: 27 additions & 17 deletions assay/src/org/labkey/assay/plate/PlateCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.labkey.api.data.TableSelector;
import org.labkey.api.exp.Lsid;
import org.labkey.api.query.FieldKey;
import org.labkey.assay.plate.model.PlateBean;
import org.labkey.assay.query.AssayDbSchema;

import java.util.ArrayList;
Expand Down Expand Up @@ -46,13 +47,14 @@ public Plate load(@NotNull String key, @Nullable Object argument)
SimpleFilter filter = SimpleFilter.createContainerFilter(cacheKey._container);
filter.addCondition(FieldKey.fromParts(cacheKey._type.name()), cacheKey._identifier);

List<PlateImpl> plates = new TableSelector(AssayDbSchema.getInstance().getTableInfoPlate(), filter, null).getArrayList(PlateImpl.class);
List<PlateBean> plates = new TableSelector(AssayDbSchema.getInstance().getTableInfoPlate(), filter, null).getArrayList(PlateBean.class);
assert plates.size() <= 1;

if (plates.size() == 1)
{
PlateImpl plate = plates.get(0);
PlateManager.get().populatePlate(plate);
PlateBean bean = plates.get(0);

Plate plate = PlateManager.get().populatePlate(bean);
LOG.info(String.format("Caching plate \"%s\" for folder %s", plate.getName(), cacheKey._container.getPath()));

// add all cache keys for this plate
Expand All @@ -66,8 +68,8 @@ private void addCacheKeys(PlateCacheKey cacheKey, Plate plate)
{
if (plate != null)
{
if (plate.getName() == null)
throw new IllegalArgumentException("Plate cannot be cached, name is null");
if (plate.getPlateId() == null)
throw new IllegalArgumentException("Plate cannot be cached, plateId is null");
if (plate.getRowId() == null)
throw new IllegalArgumentException("Plate cannot be cached, rowId is null");
if (plate.getLSID() == null)
Expand All @@ -78,8 +80,8 @@ private void addCacheKeys(PlateCacheKey cacheKey, Plate plate)
PLATE_CACHE.put(PlateCacheKey.getCacheKey(plate.getContainer(), plate.getRowId()), plate);
if (cacheKey._type != PlateCacheKey.Type.lsid)
PLATE_CACHE.put(PlateCacheKey.getCacheKey(plate.getContainer(), Lsid.parse(plate.getLSID())), plate);
if (cacheKey._type != PlateCacheKey.Type.name)
PLATE_CACHE.put(PlateCacheKey.getCacheKey(plate.getContainer(), plate.getName()), plate);
if (cacheKey._type != PlateCacheKey.Type.plateId)
PLATE_CACHE.put(PlateCacheKey.getCacheKey(plate.getContainer(), plate.getPlateId()), plate);

_containerPlateMap.computeIfAbsent(cacheKey._container, k -> new ArrayList<>()).add(plate);
}
Expand Down Expand Up @@ -110,6 +112,14 @@ private void addCacheKeys(PlateCacheKey cacheKey, Plate plate)
return c != null ? PLATE_CACHE.get(PlateCacheKey.getCacheKey(c, lsid)) : null;
}

public static @Nullable Plate getPlate(ContainerFilter cf, String plateId)
{
SimpleFilter filter = new SimpleFilter(FieldKey.fromParts("plateId"), plateId);
Container c = getContainerWithIdentifier(cf, filter);

return c != null ? PLATE_CACHE.get(PlateCacheKey.getCacheKey(c, plateId)) : null;
}

private static @Nullable Container getContainerWithIdentifier(ContainerFilter cf, SimpleFilter filter)
{
filter.addClause(cf.createFilterClause(AssayDbSchema.getInstance().getSchema(), FieldKey.fromParts("Container")));
Expand All @@ -127,9 +137,9 @@ private void addCacheKeys(PlateCacheKey cacheKey, Plate plate)
return null;
}

public static @Nullable Plate getPlate(Container c, String name)
public static @Nullable Plate getPlate(Container c, String plateId)
{
Plate plate = PLATE_CACHE.get(PlateCacheKey.getCacheKey(c, name));
Plate plate = PLATE_CACHE.get(PlateCacheKey.getCacheKey(c, plateId));
return plate != null ? plate.copy() : null;
}

Expand Down Expand Up @@ -185,14 +195,14 @@ public static void uncache(Container c, Plate plate)
{
LOG.info(String.format("Un-caching plate \"%s\" for folder %s", plate.getName(), c.getPath()));

if (plate.getName() == null)
throw new IllegalArgumentException("Plate cannot be uncached, name is null");
if (plate.getPlateId() == null)
throw new IllegalArgumentException("Plate cannot be uncached, plateId is null");
if (plate.getRowId() == null)
throw new IllegalArgumentException("Plate cannot be uncached, rowId is null");
if (plate.getLSID() == null)
throw new IllegalArgumentException("Plate cannot be uncached, LSID is null");

PLATE_CACHE.remove(PlateCacheKey.getCacheKey(c, plate.getName()));
PLATE_CACHE.remove(PlateCacheKey.getCacheKey(c, plate.getPlateId()));
PLATE_CACHE.remove(PlateCacheKey.getCacheKey(c, plate.getRowId()));
PLATE_CACHE.remove(PlateCacheKey.getCacheKey(c, Lsid.parse(plate.getLSID())));

Expand All @@ -210,7 +220,7 @@ private static class PlateCacheKey
enum Type
{
rowId,
name,
plateId,
lsid,
}
private Type _type;
Expand All @@ -226,14 +236,14 @@ enum Type
_identifier = json.get("identifier");
}

public static String getCacheKey(Container c, String name)
public static String getCacheKey(Container c, String plateId)
{
return _getCacheKey(c, Type.name, name);
return _getCacheKey(c, Type.plateId, plateId);
}

public static String getCacheKey(Container c, Integer plateId)
public static String getCacheKey(Container c, Integer rowId)
{
return _getCacheKey(c, Type.rowId, plateId);
return _getCacheKey(c, Type.rowId, rowId);
}

public static String getCacheKey(Container c, Lsid lsid)
Expand Down
2 changes: 1 addition & 1 deletion assay/src/org/labkey/assay/plate/PlateDataServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public int saveChanges(GWTPlate gwtPlate, boolean replaceIfExisting) throws Exce
else
{
// check another plate of the same name doesn't already exist
Plate other = PlateManager.get().getPlate(getContainer(), gwtPlate.getName());
Plate other = PlateManager.get().getPlateByName(getContainer(), gwtPlate.getName());
if (other != null)
{
if (!replaceIfExisting)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static WebdavResource createDocument(@NotNull Plate plate)

StringBuilder body = new StringBuilder();

PlateType plateType = plate.getPlateTypeObject();
PlateType plateType = plate.getPlateType();
if (plateType != null)
append(body, plateType.getDescription());

Expand Down
Loading