Hello,
we have questions about the file size of ObjectDB database on disk.
Can you tell us something about how the deleting of entities affects the size of *.odb files on disk?
-Will the released memory always used by new entities?
-Will be the database file automatically reduced?
We have created a small test, in which we create and delete entities. It looks like that the released memory are reused but the the file not resized. The database is at the end empty again, but larger than the initial size.
import java.util.ArrayList; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.FlushModeType; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Persistence; public final class createAndDelete { static int idCounter = 1; public static void main(String[] args) { EntityManagerFactory emf; emf = Persistence.createEntityManagerFactory("objectdb:$objectdb/db/test.tmp;drop"); EntityManager em = emf.createEntityManager(); em.setFlushMode(FlushModeType.AUTO); int objects = 1000; int loops = 3; for (int iteration = 0; iteration < loops; iteration++) { List<Integer> entityAIds = new ArrayList<>(objects); System.out.println("create"); for (int i = 0; i < objects; i++) { em.getTransaction().begin(); EntityA entity = new EntityA(); entity.value = i; entity.strValue = "longStirng" + i + "longStirnglongStirnglongStirng of entity: " + entity.id; em.persist(entity); entityAIds.add(entity.id); em.getTransaction().commit(); } System.out.println("references"); // add references for (int i = 0; i < objects; i++) { em.getTransaction().begin(); EntityB entityB = new EntityB(); entityB.value = i; entityB.strValue = "longStirng" + i + "longStirnglongStirnglongStirng of entity: " + entityB.id; EntityA entity = em.find(EntityA.class, entityAIds.get(i)); entity.entityB = entityB; em.getTransaction().commit(); } em.flush(); System.out.println("remove"); // remove for (int i = 0; i < objects; i++) { em.getTransaction().begin(); EntityA entity = em.find(EntityA.class, entityAIds.get(i)); em.remove(entity); em.getTransaction().commit(); } } em.close(); emf.close(); } @Entity public static class EntityA { public EntityA() { id = idCounter++; } @Id int id; @Basic Integer value; @Basic String strValue; @OneToOne(cascade = CascadeType.ALL) EntityB entityB = null; } @Entity public static class EntityB { public EntityB() { id = idCounter++; } @Id int id; @Basic String strValue; @Basic Integer value; } }
Is there a configuration that can we enable? Or do you have other tips for us to keep the database file as small as possible.