开发者

db4o : ActivationDepth seems to have no effect (?)

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);
  }

}


My best bet is that you are missing db4o-xxx-nqopt.jar which contains required classes for NQ optimization.

When a NQ query fails the criteria to run optimized db4o still runs your query, but this time as an evaluation whence it needs to activate all candidates.

Follows the output of DiagnoticToConsole() on such cases:

Test02$1@235dd910 :: Native Query Predicate could not be run optimized.

This Native Query was run by instantiating all objects of the candidate class.

Consider simplifying the expression in the Native Query method. If you feel that the Native Query processor should understand your code better, you are invited to post yout query code to db4o forums at http://developer.db4o.com/forums

To fix this, make sure that at least db4o-xxx-core-java5.jar, db4o-xxx-nqopt-java5.jar, db4o-xxx-instrumentation-java5.jar and bloat-1.0.jar are in your classpath. Alternatively you can replace all these jars with db4o-xxx-all-java5.jar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜