Can someone please explain to me why setting the Activation Depth seems to have no effect in the code-sample below ?
This sample creates 'Block'-objects which all have a number and a child-block.
Since the ActivationDepth is set to '2', I would expect that only Block 01 and 02 would be retrieved from the database, but it's possible to traverse child-blocks all the way down to block 05 (?).
I guess this means that the whole graph of objects is loaded instead of only level 01 and 02 and that is just what I try to avoid by setting the Activation Depth.
Here is the complete code-sample :
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.CommonConfiguration;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Predicate;
public class Test02
{
private final String DATABASE_NAME = "MyDatabase";
public static void main(String[] args)
{
new Test02().test();
}
public void test()
{
storeContent();
exploreContent();
}
private void storeContent()
{
Block block01 = new Block(1);
Block block02 = new Block(2);
Block block03 = new Block(3);
Block block04 = new Block(4);
Block block05 = new Block(5);
Block block06 = new Block(6);
block01.setChild(block02);
block02.setChild(block03);
block03.setChild(block04);
block04.setChild(block05);
block05.setChild(block06);
ObjectContainer container = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), DATABASE_NAME);
try
{
container.store(block01);
container.store(block02);
container.store(block03);
container.store(block04);
container.store(block05);
container.store(block06);
}
finally
{
container.close();
}
}
private void exploreContent()
{
EmbeddedConfiguration configEmbedded = Db4oEmbedded.newConfiguration();
CommonConfiguration configCommon = configEmbedded.common();
configCommon.activationDepth(2); // Global activation depth.
ObjectContainer container = Db4oEmbedded.openFile(configEmbedded, DATABASE_NAME);
try
{
int targetNumber = 1;
ObjectSet blocks = getBlocksFromDatabase(container, targetNumber);
System.out.println(String.format("activationDepth : %s ", configEmbedded.common().activationDepth()));
System.out.println(String.format("Blocks found : %s ", blocks.size()));
Block block = blocks.get(0);
System.out.println(block); // Block 01
System.out.println(block.child); // Block 02
System.out.println(block.child.child); // Block 03 // Why are these
System.out.println(block.child.child.child); // Block 04 // blocks available
System.out.println(block.child.child.child.child); // Block 05 // as well ??
// System.out.println(block.child.child.child.child.child); // Block 06
}
finally
{
container.close();
}
}
private ObjectSet getBlocksFromDatabase(ObjectContainer db, final int number)
{
ObjectSet results = db.query
(
new Predicate()
{
@Override
public boolean match(Block block)
{
return block.number == number;
}
}
);
return results;
}
}
class Block
{
public Block child;
public int number;
public Block(int i)
{
number = i;
}
public void setChild(Block ch)
{
child = ch;
}
@Override
public String toString()
{
return String.format("Block number %s / child = %s ", number, child.number);
}
}