Skip to content

Commit 26ed4d2

Browse files
Optimisation for bulk loading
1 parent c6c58fa commit 26ed4d2

File tree

15 files changed

+777
-200
lines changed

15 files changed

+777
-200
lines changed

record-api-migration/src/main/java/eu/europeana/api/record/migration/MigrationHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import dev.morphia.query.filters.Filters;
44
import eu.europeana.api.edm.EDM;
55
import eu.europeana.api.format.RdfFormat;
6-
import eu.europeana.api.record.db.repository.RecordRepository;
6+
import eu.europeana.api.record.db.repository.OptimisedRecordRepositoryCopy;
77
import eu.europeana.api.record.io.FormatHandlerRegistry;
88
import eu.europeana.api.record.migration.RecordDomProcessor.Result;
99
import eu.europeana.api.record.model.*;
@@ -68,14 +68,14 @@ public class MigrationHandler {
6868

6969
private final FormatHandlerRegistry registry;
7070

71-
private final RecordRepository migrationRepository;
71+
private final OptimisedRecordRepositoryCopy migrationRepository;
7272

7373
private final TemplateLibrary library;
7474

7575
@Autowired
7676
public MigrationHandler(MigrationSettings settings
7777
, FormatHandlerRegistry registry
78-
, RecordRepository migrationRepository
78+
, OptimisedRecordRepositoryCopy migrationRepository
7979
, @Qualifier(BEAN_RECORD_TEMPLATE_LIBRARY) TemplateLibrary library) {
8080
this.settings = settings;
8181
this.registry = registry;

record-api-model/src/main/java/eu/europeana/api/record/io/jena/ObjectReferenceCodec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.morphia.mapping.codec.references.MorphiaProxy;
44
import eu.europeana.api.record.model.EDMClass;
55
import eu.europeana.api.record.model.data.ObjectReference;
6+
import eu.europeana.api.record.model.data.SharedObject;
67
import eu.europeana.jena.encoder.JenaObjectDecoder.DecoderContext;
78
import eu.europeana.jena.encoder.JenaObjectEncoder.EncoderContext;
89
import eu.europeana.jena.encoder.codec.JenaCodec;
@@ -32,7 +33,7 @@ public void encode(Model m, ObjectReference ref, EncoderContext context) {
3233
//Good example to test this is: /221/URN_NBN_SI_DOC_0CEKTP4N
3334
if ( ref.isDereferenced() ) {
3435
//TODO: review how to best work with lazy loading
35-
EDMClass o = ref.getDereferencedObject();
36+
SharedObject o = ref.getDereferencedObject();
3637
// the object may be null when it has not been stored before and
3738
// should be covered on the dereferenced
3839
if ( o != null ) {

record-api-model/src/main/java/eu/europeana/api/record/model/data/LocalReference.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class LocalReference implements ObjectReference
2525

2626
@JsonUnwrapped
2727
@Property(ModelConstants.object)
28-
protected EDMClass object;
28+
protected SharedObject object;
2929

3030
public LocalReference() {}
3131

@@ -48,7 +48,7 @@ public LocalReference(EDMClass object)
4848

4949
public boolean isDereferenced() { return this.object != null; }
5050

51-
public EDMClass getDereferencedObject() { return this.object; }
51+
public SharedObject getDereferencedObject() { return this.object; }
5252

5353
public String toString() { return ("<" + id + ">"); }
5454

@@ -61,4 +61,9 @@ public void postPersist(Document doc) {
6161
public void postLoad(Document doc) {
6262
if ( this.object != null ) { this.id = this.object.getID(); }
6363
}
64+
65+
@Override
66+
public void setDereferencedObject(SharedObject obj) {
67+
this.object = obj;
68+
}
6469
}

record-api-model/src/main/java/eu/europeana/api/record/model/data/ObjectReference.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import dev.morphia.annotations.Entity;
44
import eu.europeana.api.edm.RDF;
5-
import eu.europeana.api.record.model.EDMClass;
65

76
@Entity(discriminatorKey = RDF.type)
87
public interface ObjectReference extends DataValue {
8+
99
public String getID();
1010

1111
public boolean isLocal();
1212

1313
public boolean isDereferenced();
1414

15-
public EDMClass getDereferencedObject();
15+
public SharedObject getDereferencedObject();
16+
17+
public void setDereferencedObject(SharedObject obj);
1618
}

record-api-model/src/main/java/eu/europeana/api/record/model/data/ObjectRepository.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

record-api-model/src/main/java/eu/europeana/api/record/model/data/SharedObject.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
*/
44
package eu.europeana.api.record.model.data;
55

6+
import dev.morphia.annotations.Entity;
7+
import eu.europeana.api.edm.RDF;
8+
69
/**
710
* @author Hugo
811
* @since 17 Jun 2024
912
*/
13+
@Entity(discriminatorKey = RDF.type)
1014
public interface SharedObject
1115
{
1216
public String getID();

record-api-model/src/main/java/eu/europeana/api/record/model/data/SharedReference.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public class SharedReference implements ObjectReference {
2424

2525
private static final Logger LOGGER = LogManager.getLogger(SharedReference.class);
2626

27-
public static ThreadLocal<ObjectRepository> repo = new ThreadLocal<>();
27+
public static ThreadLocal<SharedReferenceHandler> handler = new ThreadLocal<>();
2828

2929
@Property(ModelConstants.id)
3030
protected String id;
3131

3232
@Reference(value = ModelConstants.object, lazy = true)
33-
protected EDMClass object;
33+
protected SharedObject object;
3434

3535

3636
public SharedReference() {}
@@ -58,11 +58,12 @@ public boolean isLocal() {
5858
*/
5959
public boolean isDereferenced() { return (this.object != null); }
6060

61-
public EDMClass getDereferencedObject() {
62-
EDMClass obj = this.object;
61+
public SharedObject getDereferencedObject() {
62+
SharedObject obj = this.object;
63+
//explain why there is a if with MorphiaProxy
6364
if ( obj instanceof MorphiaProxy ) {
6465
try {
65-
obj = (EDMClass)((MorphiaProxy)obj).unwrap();
66+
obj = (SharedObject)((MorphiaProxy)obj).unwrap();
6667
}
6768
catch (ReferenceException e) {
6869
LOGGER.warn("Could not dereference: " + this.id);
@@ -72,28 +73,30 @@ public EDMClass getDereferencedObject() {
7273
return obj;
7374
}
7475

76+
public void setDereferencedObject(SharedObject obj) {
77+
this.object = obj;
78+
}
79+
7580
public String toString() { return ("<" + id + ">"); }
7681

7782
@PrePersist
7883
public void prePersist(Document doc, Datastore ds) {
7984
if ( this.object != null ) {
80-
ObjectRepository repo = this.repo.get();
81-
repo.save(this.object);
85+
SharedReferenceHandler handler = this.handler.get();
86+
if ( handler != null ) { handler.saveShared(this.object); }
8287
}
8388
}
8489

8590
@PostLoad
86-
public void postLoad(Document document, Datastore ds)
87-
{
91+
public void postLoad(Document document, Datastore ds) {
92+
93+
if ( this.object != null ) {
94+
SharedReferenceHandler handler = this.handler.get();
95+
if ( handler != null ) { handler.loadShared(this); }
96+
}
8897
// TODO:
8998
// Consider again removing the id field since the reference is now based
9099
// on that. The challenge will be getting the id from the reference without
91100
// forcing a load from the db.
92-
93-
//OUTDATED:
94-
// when lazy loading the object still comes with an "artificial" value, for which,
95-
// the check against null passes and the call to getID() forces the effective load of the object
96-
// which effectively breaks the lazy loading. We need to find a better way to do this!
97-
//if ( this.object != null ) { this.id = this.object.getID(); }
98101
}
99102
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*
3+
*/
4+
package eu.europeana.api.record.model.data;
5+
6+
/**
7+
* @author Hugo
8+
* @since 12 Jun 2025
9+
*/
10+
public interface SharedReferenceHandler {
11+
12+
public void saveShared(SharedObject obj);
13+
14+
public void loadShared(SharedReference ref);
15+
16+
}

0 commit comments

Comments
 (0)