You can tune up activation settings for specific classes with the following methods:
Java:configuration.objectClass("yourClass").minimumActivationDepth(minimumDepth)
configuration.objectClass("yourClass").maximumActivationDepth(maximumDepth)
Cascading the activation depth to member fields, the depth value is reduced by one for the field. If the depth exceeds the maximumDepth specified for the class of the object, it is reduced to the maximumDepth. If the depth value is lower than the minimumDepth it is raised to the minimumDepth.
01private static void testMaxActivate() { 02
storeSensorPanel(); 03
// note that the maximum is applied to the retrieved root object and limits activation 04
// further down the hierarchy 05
Configuration configuration = Db4o.newConfiguration(); 06
configuration.objectClass(SensorPanel.class).maximumActivationDepth(2); 07
08
ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 09
try { 10
System.out.println("Maximum activation depth = 2 (default = 5)"); 11
ObjectSet result = container.get(new SensorPanel(1)); 12
listResult(result); 13
if (result.size() > 0) { 14
SensorPanel sensor = (SensorPanel) result.get(0); 15
SensorPanel next = sensor.next; 16
while (next != null) { 17
System.out.println(next); 18
next = next.next; 19
} 20
} 21
} finally { 22
container.close(); 23
} 24
}
01private static void testMinActivate(){ 02
storeSensorPanel(); 03
// note that the minimum applies for *all* instances in the hierarchy 04
// the system ensures that every instantiated List object will have it's 05
// members set to a depth of 1 06
Configuration configuration = Db4o.newConfiguration(); 07
configuration.objectClass(SensorPanel.class).minimumActivationDepth(1); 08
ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 09
try { 10
System.out.println("Minimum activation depth = 1"); 11
ObjectSet result = container.get(new SensorPanel(1)); 12
listResult(result); 13
if (result.size() >0) { 14
SensorPanel sensor = (SensorPanel)result.get(0); 15
SensorPanel next = sensor.next; 16
while (next != null){ 17
System.out.println(next); 18
next = next.next; 19
} 20
} 21
} finally { 22
container.close(); 23
} 24
}
You can set up automatic activation for specific objects or fields:
Java: configuration.objectClass("yourClass").cascadeOnActivate (bool)
configuration.objectClass("yourClass").objectField("field").cascadeOnActivate(bool)
Cascade activation will retrieve the whole object graph, starting from the specified object(field). This setting can lead to increased memory consumption.
01private static void testCascadeActivate(){ 02
storeSensorPanel(); 03
Configuration configuration = Db4o.newConfiguration(); 04
configuration.objectClass(SensorPanel.class).cascadeOnActivate(true); 05
ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 06
try { 07
System.out.println("Cascade activation"); 08
ObjectSet result = container.get(new SensorPanel(1)); 09
listResult(result); 10
if (result.size() >0) { 11
SensorPanel sensor = (SensorPanel)result.get(0); 12
SensorPanel next = sensor.next; 13
while (next != null){ 14
System.out.println(next); 15
next = next.next; 16
} 17
} 18
} finally { 19
container.close(); 20
} 21
}
An alternative to cascade activation can be manual activation of objects:
Java: ObjectContainer#activate(object, activationDepth);
Manual deactivation may be used to save memory:
Java:ObjectContainer#deactivate(object, activationDepth);
These 2 methods give you an excellent control over object activation, but they obviously need more attention from the application side.
01private static void testActivateDeactivate(){ 02
storeSensorPanel(); 03
Configuration configuration = Db4o.newConfiguration(); 04
configuration.activationDepth(0); 05
ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 06
try { 07
System.out.println("Object container activation depth = 0" ); 08
ObjectSet result = container.get(new SensorPanel(1)); 09
System.out.println("Sensor1:"); 10
listResult(result); 11
SensorPanel sensor1 = (SensorPanel)result.get(0); 12
testActivated(sensor1); 13
14
System.out.println("Sensor1 activated:"); 15
container.activate(sensor1,4); 16
testActivated(sensor1); 17
18
System.out.println("Sensor5 activated:"); 19
result = container.get(new SensorPanel(5)); 20
SensorPanel sensor5 = (SensorPanel)result.get(0); 21
container.activate(sensor5,4); 22
listResult(result); 23
testActivated(sensor5); 24
25
System.out.println("Sensor1 deactivated:"); 26
container.deactivate(sensor1,5); 27
testActivated(sensor1); 28
29
// DANGER !!!. 30
// If you use deactivate with a higher value than 1 31
// make sure that you know whereto members might branch 32
// Deactivating list1 also deactivated list5 33
System.out.println("Sensor 5 AFTER DEACTIVATE OF Sensor1."); 34
testActivated(sensor5); 35
} finally { 36
container.close(); 37
} 38
}