commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl JDBMStorage.java
Date Sun, 24 Mar 2002 09:12:33 GMT
baliuka     02/03/24 01:12:33

  Added:       simplestore/src/java/org/apache/commons/simplestore/persistence/impl
                        JDBMStorage.java
  Log:
  added JDBM storage
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/JDBMStorage.java
  
  Index: JDBMStorage.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.simplestore.persistence.impl;
  
  import org.apache.commons.simplestore.cache.Cache;
  import org.apache.commons.simplestore.persistence.InternalTransaction;
  import org.apache.commons.simplestore.persistence.MetaObject;
  import org.apache.commons.simplestore.persistence.MetaClass;
  import org.apache.commons.simplestore.persistence.ObjectNotFound;
  import org.apache.commons.simplestore.persistence.Persistent;
  import org.apache.commons.simplestore.persistence.StorageException;
  import org.apache.commons.simplestore.persistence.Context;
  import org.apache.commons.simplestore.persistence.IllegalTransactionStateException;
  
  import jdbm.JDBMEnumeration;
  import jdbm.JDBMHashtable;
  import jdbm.recman.RecordManager;
  import java.io.IOException;
  import jdbm.helper.Tuple;
  import jdbm.helper.TupleBrowser;
  import jdbm.helper.MRU;
  import jdbm.helper.ObjectCache;
  import jdbm.helper.StringComparator;
  import jdbm.recman.RecordManager;
  import jdbm.btree.BTree;
  import jdbm.helper.LongComparator;
  
  
  import java.lang.reflect.Method;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
  import java.util.ResourceBundle;
  import java.util.Set;
  
  
  
  /**
   *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   *@version    $Id: JDBMStorage.java,v 1.1 2002/03/24 09:12:33 baliuka Exp $
   */
  
  public class JDBMStorage extends AbstractStorage implements org.apache.commons.simplestore.tools.Constants
{
      
      Context context;
      RecordManager recman;
      java.util.Map openTables = new java.util.Hashtable();
      ObjectCache cache;
      jdbm.helper.Comparator comparator;
      /** Creates a new instance of JDBMStorageManager */
      public JDBMStorage(RecordManager recman, ObjectCache cache, jdbm.helper.Comparator comparator)
{
          this.recman = recman;
          this.cache  = cache;
          this.comparator = comparator;
      }
      private void clear(){
         Thread thred = Thread.currentThread();
         openTables.remove(thred);
              
      }
      public void close() throws StorageException {
          try{
              recman.close();
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      BTree getTree(String name)throws StorageException{
          try{
              Thread thred = Thread.currentThread();
              Map table   =  (Map)openTables.get(thred);
              if( table == null ){
                  table = new java.util.HashMap();
                  openTables.put(thred,table);
              }
              BTree tree = (BTree)table.get(name);
              if(tree != null)
                  return tree;
              long  recid = recman.getNamedObject( name );
              if ( recid != 0 ) {
                  tree = BTree.load( recman, cache, recid );
              } else {
                  tree = new BTree( recman, cache, comparator );
                  recman.setNamedObject( name, tree.getRecid() );
                  
              }
              table.put(name,tree);
              return tree;
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
          
      }
      protected void createObject(MetaObject properties) throws StorageException {
          try{
              BTree hashtable = getTree(
              properties.getPersistentClass().getName()
              );
              hashtable.insert(properties.getOID(),properties.getProperties(),false);
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      
      protected void internalBegin() throws StorageException {
           clear();
      }
      
      protected void internalCommit() throws StorageException {
          try{
              recman.commit();
              clear();
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      
      protected void internalRollback() throws StorageException {
          try{
              recman.rollback();
              clear();
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      
      protected void removeObject(MetaObject properties) throws StorageException {
          try{
              BTree hashtable = getTree(
              properties.getPersistentClass().getName()
              );
              hashtable.remove(properties.getOID());
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      
      public java.util.Collection retrieve(Class clasz, int index, Object value) throws StorageException
{
          try{
              BTree tree = getTree(
              clasz.getName()
              );
              Set objects = new HashSet();
              MetaClass mClass =  context.getMetaClass(clasz);
              java.util.Collection tObjects = context.getTransactionManager().getTransaction().getTransactionalObjects();
              for( java.util.Iterator i = tObjects.iterator(); i.hasNext(); ){
                  MetaObject mo = (MetaObject)i.next();
                  if( mo.getPersistentClass().equals(clasz) ){
                    if( (value == null) && (mo.getProperty(index) == null) ){  
                      objects.add( mo.getObject() );
                      continue;
                    }
                   if( value != null && value.equals(mo.getProperty(index))  ){
                      objects.add( mo.getObject() );
                   } 
                     
                  }
              }
              
              Tuple  tuple = new Tuple();
              TupleBrowser browser = tree.browse();
              while ( browser.getNext( tuple ) ) {
                  Object id = tuple.getKey();
                  Persistent pc = (Persistent)retrieveObject(clasz,id);
                  MetaObject mo = pc.getMetaObject();
                 if( (value == null) && (mo.getProperty(index) == null) ){  
                      objects.add( mo.getObject() );
                      continue;
                    }
                   if( value != null && value.equals(mo.getProperty(index))  ){
                      objects.add( mo.getObject() );
                   } 
                  
              }
              
              return objects;
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
          
      }
      
      public Set retrieveAll(Class clasz) throws StorageException {
          try{
              BTree tree = getTree(
              clasz.getName()
              );
              MetaClass mClass =  context.getMetaClass(clasz);
              Set objects = new HashSet();
              java.util.Collection tObjects = context.getTransactionManager().getTransaction().getTransactionalObjects();
              for( java.util.Iterator i = tObjects.iterator(); i.hasNext(); ){
                  MetaObject mo = (MetaObject)i.next();
                  if( mo.getPersistentClass().equals(clasz) ){
                      objects.add( mo.getObject() );
                  }
              }
              
              Tuple  tuple = new Tuple();
              TupleBrowser browser = tree.browse();
              while ( browser.getNext( tuple ) ) {
                  Object id = tuple.getKey();
                  Persistent pc = (Persistent)retrieveObject(clasz,id);
                  objects.add( pc );
              }
              return objects;
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      
      public Object retrieveObject(Class clasz, Object id) throws StorageException {
          try{
              Persistent result = (Persistent) context.getCache().get(id);
              
              if (result != null) {
                  return result;
              }
              BTree tree = getTree(clasz.getName());
              
              Object obj = tree.find(id);
              if( obj == null ){
                  throw new ObjectNotFound();
              }
              MetaClass mClass =  context.getMetaClass(clasz);
              result = (Persistent) mClass.newInstance( id );
              MetaObject pc = result.getMetaObject();
              Object props[] = pc.getProperties();
              System.arraycopy(obj,0,props,0,props.length);
              context.getCache().put(id,result);
              return result;
          }catch(java.io.IOException ioe){
              throw new StorageException(ioe.getMessage(),ioe);
          }
      }
      
      public void setContext(Context context) {
          this.context = context;
      }
      
      public void storeObject(MetaObject properties) throws StorageException {
          createObject(properties);
      }
      
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message