Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 1998, 1999, 2000, 2001,
  *
  * Arjuna Solutions Limited,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id: HashedStore.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.internal.arjuna.objectstore;
 
 import java.io.File;
 
 
 /*
  * Should be derived from FragmentedStore, but we currently
  * don't have such an implementation in Java.
  */

The basic shadowing store implementations store the object states in a separate file within the same directory in the object store, determined by the object's type. However, as the number of file entries within the directory increases, so does the search time for finding a specific file. The HashStore implementation hashes object states over many different sub-directories to attempt to keep the number of files in a given directory low, thus improving performance as the number of object states grows.

Author(s):
Mark Little (mark@arjuna.com)
Version:
$Id: HashedStore.java 2342 2006-03-30 13:06:17Z $
Since:
JTS 2.0.
 
 
 public class HashedStore extends ShadowNoFileLockStore
 {
    
Given a type name initialise state to contains all of the Uids of objects of that type
 
 
     public boolean allObjUids (String tNameInputObjectState stateint matchthrows ObjectStoreException
     {
         if (..isTraceEnabled()) {
             ..trace("HashedStore.allObjUids(" + tName + ", " + state + ", " + match + ")");
         }
 
         /*
          * Directory ALWAYS has a trailing '/'
          */
 
         String directory = locateStore(getStoreName());
         OutputObjectState store = new OutputObjectState();
 
         /* Does typename start with a '/' if so skip over */
 
         if ((tName != null) && (tName.length() > 0) && (tName.charAt(0) == .))
             directory = directory + tName.substring(1, tName.length());
         else
             directory = directory + tName;
 
         if (!directory.endsWith(.))
             directory = directory + .;
 
         File f = new File(directory);
         String[] entry = f.list();
        if ((entry != null) && (entry.length > 0))
        {
            for (int i = 0; i < entry.lengthi++)
            {
                if ( Character.isDigit(entry[i].charAt(1)) || entry[i].startsWith() )
                {
                    File dir = new File(directory + entry[i]);
                    if (dir.isDirectory())
                    {
                        String[] dirEnt = dir.list();
                        for (int j = 0; j < dirEnt.lengthj++)
                        {
                            try
                            {
                                Uid aUid = new Uid(dirEnt[j], true);
                                if (!aUid.valid() || (aUid.equals(Uid.nullUid())))
                                {
                                    String revealed = revealedId(dirEnt[j]);
                                    // don't want to give the same id twice.
                                    if (present(revealeddirEnt))
                                        aUid = null;
                                    else
                                        aUid = new Uid(revealed);
                                }
                                if ((aUid.notEquals(Uid.nullUid())) && ((match == .) ||
                                    (isType(aUidtNamematch))))
                                {
                                    if( || new File(dirdirEnt[j]).length() > 0) {
                                        UidHelper.packInto(aUidstore);
                                    }
                                }
                            }
                            catch (NumberFormatException e)
                            {
                                /*
                                 * Not a number at start of file.
                                 */
                            }
                            catch (IOException e)
                            {
                                throw new ObjectStoreException(..get_objectstore_HashedStore_5(), e);
                            }
                        }
                    }
                }
                else
                {
                    // ignore
                }
            }
        }
        /* terminate list */
        try
        {
            UidHelper.packInto(Uid.nullUid(), store);
        }
        catch (IOException e)
        {
            throw new ObjectStoreException(..get_objectstore_HashedStore_6(), e);
        }
        state.setBuffer(store.buffer());
        store = null;
        return true;
    }
    public HashedStore (ObjectStoreEnvironmentBean objectStoreEnvironmentBeanthrows ObjectStoreException
    {
        super(objectStoreEnvironmentBean);
    }
    protected String truncate (String value)
    {
        int lastIndex = value.lastIndexOf(.);
        String toReturn = value;
        if (lastIndex != -1)
        {
            int nextIndex = value.lastIndexOf(.lastIndex - 1);
            if (nextIndex != -1)
            {
                char[] bitInbetween = new char[lastIndex - nextIndex - 1];
                boolean isDigit = true;
                value.getChars(nextIndex + 1, lastIndexbitInbetween, 0);
                for (int i = 0; (i < bitInbetween.length) && isDigiti++)
                {
                    if (!Character.isDigit(bitInbetween[i]))
                    {
                        isDigit = false;
                    }
                }
                if (isDigit)
                    toReturn = value.substring(lastIndex + 1);
            }
        }
        return toReturn;
    }

    

Returns:
the file name for the state of the object identified by the Uid and TypeName.
    protected String genPathName (Uid objUidString tNameint otypethrows ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("HashedStore.genPathName(" + objUid + ", " + tName + ", " + StateType.stateTypeString(otype) + ")");
        }
        String storeName = locateStore(getStoreName());
        String fname = null;
        String cPtr = null;
        int uidHash = objUid.hashCode();
        String os = objUid.fileStringForm();
        String hashDir = . + uidHash % . + . + .;  // make sure hash value is unique in the string
        if ((tName == null) || (tName.length() == 0))
            cPtr = "";
        else
        {
            cPtr = tName;
            /*
             * Convert Unix separators to 'other', i.e., Windows!
             */
            if (. && (cPtr.indexOf(.) != -1))
            {
                cPtr = cPtr.replace(..);
            }
        }
        /*
         * storeName always ends in '/' so we can remove any
         * at the start of the type name.
         */
        if (cPtr.charAt(0) == .)
            cPtr = cPtr.substring(1, cPtr.length());
        if (cPtr.charAt(cPtr.length() -1) != .)
            fname = storeName + cPtr + . + hashDir + os;
        else
            fname = storeName + cPtr + hashDir + os;
        /*
         * Make sure we don't end in a '/'.
         */
        if (fname.charAt(fname.length() -1) == .)
            fname = fname.substring(0, fname.length() -2);
        // mark the shadow copy distinctly
        if (otype == .)
            fname = fname + ;
        return fname;
    }
    public static final char SHADOWCHAR = '!';
    private final boolean present (String idString[] list)
    {
        for (int i = 0; i < list.lengthi++)
        {
            if (list[i].equals(id))
                return true;
        }
        return false;
    }
    public static final int DEFAULT_NUMBER_DIRECTORIES = 255;
    private static final String HASH_SEPARATOR = "#";
    private static int NUMBEROFDIRECTORIES = ;
    static
    {
        if ( <= 0) {
        }
    }
New to GrepCode? Check out our FAQ X