【问题标题】:db4o : ActivationDepth seems to have no effect (?)db4o:ActivationDepth 似乎没有效果(?)
【发布时间】:2011-09-23 18:32:22
【问题描述】:

谁能解释一下为什么设置 激活深度似乎没有效果 在下面的代码示例中?

这个示例创建了“块”对象,它们都具有 一个数字和一个子块。

由于 ActivationDepth 设置为“2”,我希望 只有块 01 和 02 将从 数据库,但可以遍历子块 一直到块 05 (?)。

我猜这意味着整个对象图 加载而不是仅级别 01 和 02,即 正是我试图通过设置激活深度来避免的。

这是完整的代码示例:

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

}

【问题讨论】:

    标签: java db4o


    【解决方案1】:

    我最好的选择是您缺少 db4o-xxx-nqopt.jar,其中包含 NQ 优化所需的类。

    当 NQ 查询未能满足运行优化的条件时,db4o 仍会运行您的查询,但这一次作为评估,它需要激活所有候选者。

    在这种情况下遵循 DiagnoticToConsole() 的输出:

    Test02$1@235dd910 :: Native Query Predicate 无法优化运行。

    本机查询是通过实例化候选类的所有对象来运行的。

    考虑简化 Native Query 方法中的表达式。如果您觉得 Native Query 处理器应该更好地理解您的代码,请您将您的查询代码发布到 http://developer.db4o.com/forums 的 db4o 论坛

    要解决此问题,请确保至少 db4o-xxx-core-java5.jardb4o-xxx-nqopt-java5.jardb4o-xxx-instrumentation-java5.jarbloat-1.0.jar 在您的类路径中。或者,您可以将所有这些 jar 替换为 db4o-xxx-all-java5.jar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 2018-05-18
      • 1970-01-01
      • 2010-12-24
      • 2020-07-04
      • 2014-11-06
      • 2020-04-11
      相关资源
      最近更新 更多