    1   /* PersistOnlineProcessor.java
    2    *
    3    * Created on Feb 18, 2005
    4    *
    5    * Copyright (C) 2007 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22   */
    23  package org.archive.crawler.processor.recrawl;
    24
    25  import org.archive.util.bdbje.EnhancedEnvironment;
    26
    27  import st.ata.util.AList;
    28
    29  import com.sleepycat.bind.serial.SerialBinding;
    30  import com.sleepycat.bind.serial.StoredClassCatalog;
    31  import com.sleepycat.bind.tuple.StringBinding;
    32  import com.sleepycat.collections.StoredSortedMap;
    33  import com.sleepycat.je.Database;
    34  import com.sleepycat.je.DatabaseConfig;
    35  import com.sleepycat.je.DatabaseException;
    36
    37  /***
    38   * Common superclass for persisting Processors which directly store/load
    39   * to persistence (as opposed to logging for batch load later).
    40   * @author gojomo
    41   */
    42  public abstract class PersistOnlineProcessor extends PersistProcessor {
    43      private static final long serialVersionUID = -666479480942267268L;
    44
    45      protected StoredSortedMap store;
    46      protected Database historyDb;
    47
    48      /***
    49       * Usual constructor
    50       *
    51       * @param name
    52       * @param string
    53       */
    54      public PersistOnlineProcessor(String name, String string) {
    55          super(name, string);
    56      }
    57
    58      protected void initialTasks() {
    59          // TODO: share single store instance between Load and Store processors
    60          // (shared context? EnhancedEnvironment?)
    61          store = initStore();
    62      }
    63
    64      protected StoredSortedMap initStore() {
    65          StoredSortedMap historyMap;
    66          try {
    67              EnhancedEnvironment env = getController().getBdbEnvironment();
    68              StoredClassCatalog classCatalog = env.getClassCatalog();
    69              DatabaseConfig dbConfig = historyDatabaseConfig();
    70              historyDb = env.openDatabase(null, URI_HISTORY_DBNAME, dbConfig);
    71              historyMap = new StoredSortedMap(historyDb,
    72                      new StringBinding(), new SerialBinding(classCatalog,
    73                              AList.class), true);
    74          } catch (DatabaseException e) {
    75              throw new RuntimeException(e);
    76          }
    77          return historyMap;
    78      }
    79
    80      @Override
    81      protected void finalTasks() {
    82              try {
    83                  historyDb.sync();
    84                  historyDb.close();
    85              } catch (DatabaseException e) {
    86                  // TODO Auto-generated catch block
    87                  throw new RuntimeException(e);
    88              }
    89          }
    90
    91  }