Package com.db4o

Examples of com.db4o.ObjectContainer


    notifyNewEntity(newResponse);
  }

  private WebResponse createWebResponse(List<NameValuePair> parameters, String mimeType) {
    WebResponse response = new WebResponse(eventManager, database, this, parameters, mimeType);
    ObjectContainer database = getDatabase();
    response.setDatabase(database);
    database.store(response);
    return response;
  }
View Full Code Here


  private ObjectContainer openDatabase(final File databaseFile) {
    try {
      final String databasePath = databaseFile.getAbsolutePath();
      final boolean exists = databaseFile.exists(); // possible race condition 
      final ObjectContainer db = configurationFactory.openContainer(databasePath);
      loadModelVersion(db, !exists);
      tagModel = new TagModel(db);
      webModel = new WebModel(db);
      variableModel = new VariableModel(db);
      httpMacroModel = new HttpMacroModel(db);
View Full Code Here

   * @param destination
   * @param useLargeBlockSize
   * @param monitor
   */
  public final static void copyDatabase(File source, File destination, boolean useLargeBlockSize, IProgressMonitor monitor) {
    ObjectContainer sourceDb = null;
    ObjectContainer destinationDb = null;
    try {

      /* Open Source DB */
      sourceDb = Db4o.openFile(createConfiguration(true), source.getAbsolutePath());

      Configuration destinationDbConfiguration = createConfiguration(true);
      if (useLargeBlockSize)
        destinationDbConfiguration.blockSize(LARGE_DB_BLOCK_SIZE);

      /* Open Destination DB */
      destinationDb = Db4o.openFile(destinationDbConfiguration, destination.getAbsolutePath());

      /* Copy (Defragment) */
      internalCopyDatabase(sourceDb, destinationDb, useLargeBlockSize, monitor);
    } finally {
      if (sourceDb != null)
        sourceDb.close();

      if (destinationDb != null)
        destinationDb.close();
    }
  }
View Full Code Here

  public MigrationResult migrate(ConfigurationFactory configFactory, String dbFileName, IProgressMonitor progressMonitor) {
    final int totalProgress = 100;
    int totalProgressIncremented = 0;
    progressMonitor.beginTask(Messages.Migration2To5_MIGRATING_DATA, totalProgress);

    ObjectContainer oc = Db4o.openFile(configFactory.createConfiguration(), dbFileName);

    totalProgressIncremented = Migration2To3.migrate(progressMonitor, totalProgress, totalProgressIncremented, oc);
    oc.commit();
    oc.close();

    File dbLastBackUpFile = DBManager.getDefault().getDBLastBackUpFile();
    dbLastBackUpFile.delete();

    progressMonitor.worked(totalProgress - totalProgressIncremented);
View Full Code Here

  public MigrationResult migrate(ConfigurationFactory configFactory, String dbFileName, IProgressMonitor progressMonitor) {
    final int totalProgress = 100;
    int totalProgressIncremented = 0;
    progressMonitor.beginTask(Messages.Migration2To3_MIGRATING_DATA, totalProgress);

    ObjectContainer oc = Db4o.openFile(configFactory.createConfiguration(), dbFileName);

    totalProgressIncremented = migrate(progressMonitor, totalProgress, totalProgressIncremented, oc);
    oc.commit();
    oc.close();

    progressMonitor.worked(totalProgress - totalProgressIncremented);

    return new MigrationResult(false, false, false);
  }
View Full Code Here

  public MigrationResult migrate(ConfigurationFactory configFactory, String dbFileName, IProgressMonitor progressMonitor) {
    final int totalProgress = 100;
    int totalProgressIncremented = 0;
    progressMonitor.beginTask(Messages.Migration3To4_MIGRATING_DATA, totalProgress);

    ObjectContainer oc = Db4o.openFile(configFactory.createConfiguration(), dbFileName);

    List<NewsBin> newsBins = oc.query(NewsBin.class);

    for (INewsBin newsBin : newsBins) {
      oc.activate(newsBin, Integer.MAX_VALUE);
      for (NewsReference newsRef : newsBin.getNewsRefs()) {
        Query query = oc.query();
        query.constrain(News.class);
        query.descend("fId").constrain(newsRef.getId()); //$NON-NLS-1$
        News news = (News) query.execute().iterator().next();
        oc.activate(news, Integer.MAX_VALUE);
        String parentIdFieldName = "fParentId"; //$NON-NLS-1$
        MigrationHelper.setField(news, parentIdFieldName, newsBin.getId().longValue());
        oc.ext().set(news, Integer.MAX_VALUE);
      }
    }

    oc.commit();
    oc.close();

    progressMonitor.worked(totalProgress - totalProgressIncremented);

    return new MigrationResult(true, false, true);
  }
View Full Code Here

      defragmentedDbFile.delete();

    DBManager.copyDatabase(originDbFile, defragmentedDbFile, useLargeBlocksize, new NullProgressMonitor());

    System.gc();
    ObjectContainer db = Db4o.openFile(DBManager.createConfiguration(false), originDbFile.getAbsolutePath());
    ObjectContainer defragmentedDb = Db4o.openFile(DBManager.createConfiguration(false), defragmentedDbFile.getAbsolutePath());

    try {

      /* Assert Number of Entities */
      List<IEntity> entities = db.query(IEntity.class);
      assertEquals(entities.size(), defragmentedDb.query(IEntity.class).size());
      for (IEntity entity : entities) {
        Query query = defragmentedDb.query();
        query.constrain(entity.getClass());
        query.descend("fId").constrain(Long.valueOf(entity.getId())); //$NON-NLS-1$

        List<?> result = query.execute();
        assertEquals(1, result.size());
        assertEquals(entity, result.get(0));

        if (entity instanceof Attachment)
          assertTrue(((Attachment) entity).isIdentical((Attachment) result.get(0)));
        else if (entity instanceof BookMark)
          assertTrue(((BookMark) entity).isIdentical((BookMark) result.get(0)));
        else if (entity instanceof Category)
          assertTrue(((Category) entity).isIdentical((Category) result.get(0)));
        else if (entity instanceof Feed)
          assertTrue(((Feed) entity).isIdentical((Feed) result.get(0)));
        else if (entity instanceof Folder)
          assertTrue(((Folder) entity).isIdentical((Folder) result.get(0)));
        else if (entity instanceof Label)
          assertTrue(((Label) entity).isIdentical((Label) result.get(0)));
        else if (entity instanceof News)
          assertTrue(((News) entity).isIdentical((News) result.get(0)));
        else if (entity instanceof Person)
          assertTrue(((Person) entity).isIdentical((Person) result.get(0)));
        else if (entity instanceof SearchCondition)
          assertTrue(((SearchCondition) entity).isIdentical((SearchCondition) result.get(0)));
        else if (entity instanceof SearchMark)
          assertTrue(((SearchMark) entity).isIdentical((SearchMark) result.get(0)));
      }

      /* Assert News */
      List<INews> newsList = db.query(INews.class);
      assertEquals(newsList.size(), defragmentedDb.query(INews.class).size());
      for (INews news : newsList) {
        Query query = defragmentedDb.query();
        query.constrain(news.getClass());
        query.descend("fId").constrain(Long.valueOf(news.getId())); //$NON-NLS-1$

        List<INews> result = query.execute();
        assertEquals(1, result.size());
        assertEquals(news.getTitle(), result.get(0).getTitle());
      }

      /* Assert Description */
      List<Description> descriptions = db.query(Description.class);
      assertEquals(descriptions.size(), defragmentedDb.query(Description.class).size());
      for (Description description : descriptions) {
        Query query = defragmentedDb.query();
        query.constrain(description.getClass());
        query.descend("fNewsId").constrain(Long.valueOf(description.getNews().getId())); //$NON-NLS-1$

        List<Description> result = query.execute();
        assertEquals(1, result.size());
        assertEquals(description.getValue(), result.get(0).getValue());
      }

      /* Assert News Bins */
      List<INewsBin> newsBins = db.query(INewsBin.class);
      assertEquals(newsBins.size(), defragmentedDb.query(INewsBin.class).size());
      for (INewsBin newsBin : newsBins) {
        Query query = defragmentedDb.query();
        query.constrain(newsBin.getClass());
        query.descend("fId").constrain(Long.valueOf(newsBin.getId())); //$NON-NLS-1$

        List<INewsBin> result = query.execute();
        assertEquals(1, result.size());
        assertEquals(newsBin.getNews(), result.get(0).getNews());
      }

      /* Assert Folders, Bookmarks and Searchmarks */
      List<IFolder> folders = db.query(IFolder.class);
      assertEquals(folders.size(), defragmentedDb.query(IFolder.class).size());
      for (IFolder folder : folders) {
        Query query = defragmentedDb.query();
        query.constrain(folder.getClass());
        query.descend("fId").constrain(Long.valueOf(folder.getId())); //$NON-NLS-1$

        List<IFolder> result = query.execute();
        assertEquals(1, result.size());

        IFolder otherFolder = result.get(0);
        assertTrue(folder.getName().equals(otherFolder.getName()));
        assertTrue(folder.getProperties().equals(otherFolder.getProperties()));

        IBookMark bm = null;
        ISearchMark sm = null;
        List<IFolderChild> children = folder.getChildren();
        for (IFolderChild child : children) {
          if (child instanceof IBookMark)
            bm = (IBookMark) child;
          else if (child instanceof ISearchMark)
            sm = (ISearchMark) child;
        }

        IBookMark otherBM = null;
        ISearchMark otherSM = null;
        List<IFolderChild> otherChildren = otherFolder.getChildren();
        for (IFolderChild otherChild : otherChildren) {
          if (otherChild instanceof IBookMark)
            otherBM = (IBookMark) otherChild;
          else if (otherChild instanceof ISearchMark)
            otherSM = (ISearchMark) otherChild;
        }

        assertNotNull(bm);
        assertNotNull(sm);
        assertNotNull(otherBM);
        assertNotNull(otherSM);

        assertTrue(bm.getName().equals(otherBM.getName()));
        assertTrue(bm.getProperties().equals(otherBM.getProperties()));

        assertTrue(sm.getSearchConditions().size() == otherSM.getSearchConditions().size());
      }

      /* Assert Preference */
      List<IPreference> preferences = db.query(IPreference.class);
      assertEquals(preferences.size(), defragmentedDb.query(IPreference.class).size());
      for (IPreference preference : preferences) {
        Query query = defragmentedDb.query();
        query.constrain(preference.getClass());
        query.descend("fId").constrain(Long.valueOf(preference.getId())); //$NON-NLS-1$

        List<IPreference> result = query.execute();
        assertEquals(1, result.size());

        IPreference otherPreference = result.get(0);

        assertEquals(preference.getKey(), otherPreference.getKey());
        if ("string".equals(preference.getKey()))
          assertEquals(preference.getString(), otherPreference.getString());

        if ("strings".equals(preference.getKey()))
          assertTrue(Arrays.equals(preference.getStrings(), otherPreference.getStrings()));

        if ("boolean".equals(preference.getKey()))
          assertEquals(preference.getBoolean(), otherPreference.getBoolean());

        if ("booleans".equals(preference.getKey()))
          assertTrue(Arrays.equals(preference.getBooleans(), otherPreference.getBooleans()));

        if ("integer".equals(preference.getKey()))
          assertEquals(preference.getInteger(), otherPreference.getInteger());

        if ("integers".equals(preference.getKey()))
          assertTrue(Arrays.equals(preference.getIntegers(), otherPreference.getIntegers()));

        if ("long".equals(preference.getKey()))
          assertEquals(preference.getLong(), otherPreference.getLong());

        if ("longs".equals(preference.getKey()))
          assertTrue(Arrays.equals(preference.getLongs(), otherPreference.getLongs()));
      }

      /* Assert Label */
      List<ILabel> labels = db.query(ILabel.class);
      assertEquals(labels.size(), defragmentedDb.query(ILabel.class).size());
      for (ILabel label : labels) {
        Query query = defragmentedDb.query();
        query.constrain(label.getClass());
        query.descend("fId").constrain(Long.valueOf(label.getId())); //$NON-NLS-1$

        List<INewsBin> result = query.execute();
        assertEquals(1, result.size());
        assertTrue(((Label) label).isIdentical((ILabel) result.get(0)));
      }

      /* Assert Counter */
      assertEquals(db.query(Counter.class).get(0).getValue(), defragmentedDb.query(Counter.class).get(0).getValue());

      /* Assert Search Filter */
      List<ISearchFilter> filters = db.query(ISearchFilter.class);
      assertEquals(filters.size(), defragmentedDb.query(ISearchFilter.class).size());
      for (ISearchFilter filter : filters) {
        Query query = defragmentedDb.query();
        query.constrain(filter.getClass());
        query.descend("fId").constrain(Long.valueOf(filter.getId())); //$NON-NLS-1$

        List<INewsBin> result = query.execute();
        assertEquals(1, result.size());

        ISearchFilter otherFilter = (ISearchFilter) result.get(0);
        assertTrue(filter.getName().equals(otherFilter.getName()));
        assertEquals(filter.getActions().size(), otherFilter.getActions().size());

        ISearch search = filter.getSearch();
        ISearch otherSearch = otherFilter.getSearch();

        assertEquals(search.getSearchConditions().size(), otherSearch.getSearchConditions().size());
      }

      /* Assert Conditional Get */
      List<IConditionalGet> condGets = db.query(IConditionalGet.class);
      assertEquals(condGets.size(), defragmentedDb.query(IConditionalGet.class).size());
      for (IConditionalGet condGet : condGets) {
        Query query = defragmentedDb.query();
        query.constrain(condGet.getClass());
        query.descend("fLink").constrain(condGet.getLink().toString()); //$NON-NLS-1$

        List<IConditionalGet> result = query.execute();
        assertEquals(1, result.size());

        IConditionalGet otherCondGet = result.get(0);
        assertEquals(condGet.getIfModifiedSince(), otherCondGet.getIfModifiedSince());
        assertEquals(condGet.getIfNoneMatch(), otherCondGet.getIfNoneMatch());
      }

      /* Assert EntityIdsByEventType */
      EntityIdsByEventType eventType = db.query(EntityIdsByEventType.class).get(0);
      EntityIdsByEventType otherEventType = defragmentedDb.query(EntityIdsByEventType.class).get(0);
      assertNotNull(eventType);
      assertNotNull(otherEventType);
      assertEquals(eventType, otherEventType);

      /* Assert NewsCounter / NewsCounterItem */
      NewsCounter newsCounter = db.query(NewsCounter.class).get(0);
      db.activate(newsCounter, Integer.MAX_VALUE);
      NewsCounter otherNewsCounter = defragmentedDb.query(NewsCounter.class).get(0);
      defragmentedDb.activate(otherNewsCounter, Integer.MAX_VALUE);
      assertNotNull(newsCounter);
      assertNotNull(otherNewsCounter);

      NewsCounterItem item = otherNewsCounter.get("http://www.rssowl.org");
      assertEquals(1, item.getNewCounter());
      assertEquals(2, item.getUnreadCounter());
      assertEquals(3, item.getStickyCounter());
    } finally {
      db.close();
      defragmentedDb.close();
    }
  }
View Full Code Here

TOP

Related Classes of com.db4o.ObjectContainer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.