BACK

feedback / send your comments
mail to luc peuvrier
chat channel
joafip users mailing list
forums

En Français

Auto save feature

This features is a major enhancement of future release 4.0.0

Auto save is usable through exclusive data access session only. (see also save meaning)

The auto save feature avoid to have call to save() to be inserted in the code to free memory.

The save is automaticly done when a number of object in memory threshold is reached.

Some additional features:

Explanation by example:
1) out of memory problem
2) save in session (how to avoid out of memory adding save call)
3) auto save (how to simplify code to avoid out of memory)
4) unloadable instance (save memory used by new object instance)

1) Out of memory problem

Below an "out of memory" can occurs

final FilePersistenceBuilder builder = new FilePersistenceBuilder();
...
builder.set....
...
FilePersistence
filePersistence=builder.build();
IExclusiveDataAccessSession session= filePersistence.createExclusiveDataAccessSession();
session.open();
List<Integer> bigList=new PLinkedList<Integer>(); // bigList instance is in memory
for(int count=0;count<max;count++) {
    final int value=aValue(count);
    bigList.add(value); // this make bigList growing in memory
}
session.setObject("myBigList",bigList);
bigList=null; // no more reference the big list
session.save(); // after this the garbage collector can free memory of the big list
bigList=(
List<Integer>)session.getObject("myBigList"); // witchery of lazy load :
                                                       // all the big list is not loaded in memory
for(int value:bigList) {
// iteration make bigList growing in memory
    doSomething(value);
}

2) save in session

Below to avoid "out of memory", saving at each object access can take a long time

final FilePersistenceBuilder builder = new FilePersistenceBuilder();
...
builder.set....
...
FilePersistence
filePersistence=builder.build();
IExclusiveDataAccessSession session= filePersistence.createExclusiveDataAccessSession();
session.open();
session.setObject("myBigList",new PLinkedList<Integer>());
session.save();
List<Integer> bigList=(List<Integer>)session.getObject("myBigList"); //obtains a persisted big list
                                                                     //a proxy in unloaded state

for(int count=0;count<max;count++) {
    final int value=aValue(count);
    bigList.add(value); // grow in memory but only of the number of added objects
    session.save(); // save one by one will take a long time. only added element are writed
}
for(int value:bigList) {
    doSomething(value);
    session.save();
// save one by one will take a long time
}

3) auto save

Setting up a memory threshold speed up program by avoiding saving one by one.

final FilePersistenceBuilder builder = new FilePersistenceBuilder();
...
builder.set....
...
FilePersistence
filePersistence=builder.build();

filePersistence.autoSaveSetup(100); // set maximum persistetd objects in memory threshold to 100 objets
filePersistence.setAutoSaveEnabled(true); // enable auto save, will be done according to previous setup
filePersistence.setAutoSaveEnabled(this); // if want to control and/or listen saving
filePersistence.maintainInMemorySetup(50); // set maximum of persisted objets maintains
                                           //in memory after a save
filePersistence.setMaintainedInMemoryEnabled(true); // enable maintenance of persisted objects in memory
IExclusiveDataAccessSession session= filePersistence.createExclusiveDataAccessSession();
session.open();
session.setObject("myBigList",new PLinkedList<Integer>());
session.save(); //force save
List<Integer> bigList=(List<Integer>)session.getObject("myBigList"); //obtains a persisted big list
                                                                     //a proxy in unloaded state

for(int count=0;count<max;count++) {
    final int value=aValue(count);
    bigList.add(value); // grow in memory but only of the number of added objects up to "in memory threshold"
    // session.save(); no more needed
}
for(int value:bigList) {
    doSomething(value);
    // session.save(); no more needed
}

4) unloadable instance

It is possible to create new object as proxy instance that can be persisted and unload before be attached to root persisted object.
This is done using:
IInstanceFactory instanceFactory = session.getInstanceFactory();
List<Integer> bigList=(List<Integer>)PLinkedList.newInstance(instanceFactory);

4.1) sample with PLinkedList

final FilePersistenceBuilder builder = new FilePersistenceBuilder();
...
builder.set....
...
FilePersistence
filePersistence=builder.build();

filePersistence.autoSaveSetup(100); // set maximum persistetd objects in memory threshold to 100 objets
filePersistence.setAutoSaveEnabled(true); // enable auto save, will be done according to previous setup
filePersistence.setAutoSaveEnabled(this); // if want to control and/or listen saving
filePersistence.maintainInMemorySetup(50); // set maximum of persisted objets maintains
                                           //in memory after a save
filePersistence.setMaintainedInMemoryEnabled(true); // enable maintenance of persisted objects in memory
IExclusiveDataAccessSession session= filePersistence.createExclusiveDataAccessSession();
session.open();
IInstanceFactory instanceFactory = session.getInstanceFactory();
List<Integer> bigList=(List<Integer>)PLinkedList.newInstance(instanceFactory); // this list can be unloaded

for(int count=0;count<max;count++) {
    final int value=aValue(count);
    bigList.add(value); // grow in memory but only of the number of added objects up to "in memory threshold"
    // session.save(); no more needed
}
session.setObject("myBigList",bigList);//add to allow retrieval
session.save(); //force save
for(int value:bigList) {
    doSomething(value);
    // session.save(); no more needed
}

4.2) add a proxy factory to your POJO

public class MyPojo {

  // keep a reference to instance factory, null if not used
  private final IInstanceFactory instanceFactory;

  public MyPojo() {
    this(null);
  }

  public MyPojo(final IInstanceFactory instanceFactory) {
    super();
    this.instanceFactory=instanceFactory;
  }

  // factory method
  public static MyPojo newInstance(final IInstanceFactory instanceFactory) {
    final MyPojo newInstance;
    if( instanceFactory==null)  {
      newInstance=new MyPojo();
    } else {
      newInstance=(MyPojo)instanceFactory.newInstance(MyPojo.class,new CLass[]{IInstanceFactory.class},
         new Object[]{instanceFactory});
    }
    return newInstance;
  }

  public XXX asXXX() {
    // below replace "new XXX(this);"
    return XXX.newInstance(instanceFactory,this);
  }
}



feedback / send your comments
mail to luc peuvrier
chat channel
joafip users mailing list
forums

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
© 2007-2012, joafip