Bugfixes in memory storage model. QBE200206120100-BLD200206141345
authortzezula@netbeans.org
Mon, 10 Jun 2002 15:10:42 +0000
changeset 8710ced445f854f
parent 870 a58b4539f628
child 872 8636c5887916
Bugfixes in memory storage model.
mdr/src/org/netbeans/mdr/persistence/memoryimpl/StorageFactoryImpl.java
mdr/src/org/netbeans/mdr/persistence/memoryimpl/StorageImpl.java
     1.1 --- a/mdr/src/org/netbeans/mdr/persistence/memoryimpl/StorageFactoryImpl.java	Mon Jun 10 08:16:13 2002 +0000
     1.2 +++ b/mdr/src/org/netbeans/mdr/persistence/memoryimpl/StorageFactoryImpl.java	Mon Jun 10 15:10:42 2002 +0000
     1.3 @@ -23,6 +23,9 @@
     1.4  public class StorageFactoryImpl extends Object implements StorageFactory {
     1.5      
     1.6      public static final String STORAGE_NAME = "org.netbeans.mdr.persistence.memoryimpl.name";
     1.7 +    public static final String PERSISTENT_NAME = "org.netbeans.mdr.persistence.memoryimpl.persistent";
     1.8 +    
     1.9 +    private static Sequencer sequencer = null;
    1.10  
    1.11      /** Creates new StorageFactoryImpl */
    1.12      public StorageFactoryImpl() {
    1.13 @@ -33,9 +36,38 @@
    1.14       */
    1.15      public Storage createStorage(Map properties) throws StorageException {
    1.16          String name = (String) properties.get (STORAGE_NAME);  // Not mandatory
    1.17 -        return new StorageImpl (name);
    1.18 +        boolean persistent = true;
    1.19 +        Boolean persistFlag = (Boolean) properties.get (PERSISTENT_NAME);
    1.20 +        if (persistFlag != null)
    1.21 +            persistent = persistFlag.booleanValue ();
    1.22 +        return new StorageImpl (getSequencer ().nextValue (), name, persistent);
    1.23      }
    1.24      public String createNullMOFID() throws StorageException {
    1.25 -        return "NULLmodif";
    1.26 +        return org.netbeans.mdr.persistence.memoryimpl.MOFID.getNullMOFID ().toString();
    1.27      }
    1.28 +    
    1.29 +    protected static Sequencer getSequencer () {
    1.30 +        if (sequencer == null) {
    1.31 +            sequencer = new Sequencer (org.netbeans.mdr.persistence.memoryimpl.MOFID.NULL_STORAGE + 1);
    1.32 +        }
    1.33 +        return sequencer;
    1.34 +    }
    1.35 +    
    1.36 +    private static class Sequencer {
    1.37 +        
    1.38 +        private int current;
    1.39 +        
    1.40 +        public Sequencer () {
    1.41 +            this.current = 0;
    1.42 +        }
    1.43 +        
    1.44 +        public Sequencer (int initialValue) {
    1.45 +            this.current = initialValue;
    1.46 +        }
    1.47 +        
    1.48 +        public synchronized int nextValue () {
    1.49 +            return this.current++;
    1.50 +        }
    1.51 +    }
    1.52 +    
    1.53  }
     2.1 --- a/mdr/src/org/netbeans/mdr/persistence/memoryimpl/StorageImpl.java	Mon Jun 10 08:16:13 2002 +0000
     2.2 +++ b/mdr/src/org/netbeans/mdr/persistence/memoryimpl/StorageImpl.java	Mon Jun 10 15:10:42 2002 +0000
     2.3 @@ -1,11 +1,11 @@
     2.4  /*
     2.5   *                 Sun Public License Notice
     2.6 - * 
     2.7 + *
     2.8   * The contents of this file are subject to the Sun Public License
     2.9   * Version 1.0 (the "License"). You may not use this file except in
    2.10   * compliance with the License. A copy of the License is available at
    2.11   * http://www.sun.com/
    2.12 - * 
    2.13 + *
    2.14   * The Original Code is NetBeans. The Initial Developer of the Original
    2.15   * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
    2.16   * Microsystems, Inc. All Rights Reserved.
    2.17 @@ -26,35 +26,39 @@
    2.18   * @version
    2.19   */
    2.20  public class StorageImpl extends Object implements Storage {
    2.21 -
    2.22 +    
    2.23      private static final String PRIMARY_INDEX_NAME = "_primaryIndex";
    2.24      private static final String INDENT = "  ";
    2.25      private static final String XMI_HEADER = "<?xml version = '1.0' ?><MDRStorage>";
    2.26      private static final String DEFAULT_NAME = "storage.xml";
    2.27 -
    2.28 +    
    2.29      private static int lastMofId = 0;
    2.30      
    2.31 -    private TreeMap maps = new TreeMap ();
    2.32 +    private TreeMap maps = new TreeMap();
    2.33      private String name;
    2.34 +    private int memStorageNumber;
    2.35      private SinglevaluedIndex primaryIndex;
    2.36 -
    2.37 +    private boolean persistent;
    2.38 +    
    2.39      /** Creates new MdrCacheImpl */
    2.40 -    public StorageImpl(String name) {
    2.41 +    public StorageImpl(int memStorageNumber, String name, boolean persistent) {
    2.42          if (name == null) {
    2.43              this.name = DEFAULT_NAME;
    2.44          } else {
    2.45              this.name = name;
    2.46          }
    2.47 +        this.memStorageNumber = memStorageNumber;
    2.48 +        this.persistent = persistent;
    2.49      }
    2.50 -
    2.51 +    
    2.52      public String generateMOFID() throws StorageException {
    2.53 -        return (lastMofId++)+"mofid";
    2.54 +        return new org.netbeans.mdr.persistence.memoryimpl.MOFID(this.memStorageNumber, this.lastMofId++).toString();
    2.55      }
    2.56 -
    2.57 +    
    2.58      // does nothing in this implementation
    2.59      public void create(boolean replace, ObjectResolver resolver) throws StorageException {
    2.60          if (!replace && exists()) {
    2.61 -            throw new StorageBadRequestException ("Storage already exists");
    2.62 +            throw new StorageBadRequestException("Storage already exists");
    2.63          }
    2.64          createPrimaryIndex();
    2.65      }
    2.66 @@ -64,68 +68,69 @@
    2.67      }
    2.68      
    2.69      public boolean delete() throws StorageException {
    2.70 -        return new File (getName()).delete();
    2.71 +        return new File(getName()).delete();
    2.72      }
    2.73      
    2.74      public String getName() {
    2.75          return this.name;
    2.76      }
    2.77      
    2.78 -    public String getStorageId () {
    2.79 +    public String getStorageId() {
    2.80          return null;
    2.81      }
    2.82      
    2.83      public boolean exists() throws StorageException {
    2.84 -        return new File (getName()).exists();
    2.85 +        return new File(getName()).exists();
    2.86      }
    2.87      
    2.88      public void open(boolean createOnNoExist, ObjectResolver resolver) throws StorageException {
    2.89          Log.out.println("Reading storage from XML document ...");
    2.90          Log.out.indent();
    2.91          String className = null;
    2.92 -        
    2.93 -        try {
    2.94 -            InputStream is = new BufferedInputStream (new FileInputStream ( name ));
    2.95 -            is.skip(XMI_HEADER.length());
    2.96 -            while (true) { // ends with EOFException
    2.97 -                // read name of class
    2.98 -                // create instance and call read on it
    2.99 -                String str = XmlUtils.readTagStart (is); // <Index>
   2.100 -                if (str.equals("/MDRStorage")) break;
   2.101 -                className = XmlUtils.readTextTag(is); // <class>
   2.102 -                Log.out.println("Index:"+className);
   2.103 -                Log.out.indent();
   2.104 -
   2.105 -                Streamable object = null;
   2.106 -                try {
   2.107 -                    Class cls = Class.forName(className);
   2.108 -                    object = (Streamable) cls.newInstance();
   2.109 -                    object.read (is);
   2.110 -                    maps.put (((Index)object).getName(),object);
   2.111 -                    if (((Index)object).getName().equals(PRIMARY_INDEX_NAME)) {
   2.112 -                        primaryIndex = (SinglevaluedIndex) object;
   2.113 +        if (this.persistent) {
   2.114 +            try {
   2.115 +                InputStream is = new BufferedInputStream(new FileInputStream( name ));
   2.116 +                is.skip(XMI_HEADER.length());
   2.117 +                while (true) { // ends with EOFException
   2.118 +                    // read name of class
   2.119 +                    // create instance and call read on it
   2.120 +                    String str = XmlUtils.readTagStart(is); // <Index>
   2.121 +                    if (str.equals("/MDRStorage")) break;
   2.122 +                    className = XmlUtils.readTextTag(is); // <class>
   2.123 +                    Log.out.println("Index:"+className);
   2.124 +                    Log.out.indent();
   2.125 +                    
   2.126 +                    Streamable object = null;
   2.127 +                    try {
   2.128 +                        Class cls = Class.forName(className);
   2.129 +                        object = (Streamable) cls.newInstance();
   2.130 +                        object.read(is);
   2.131 +                        maps.put(((Index)object).getName(),object);
   2.132 +                        if (((Index)object).getName().equals(PRIMARY_INDEX_NAME)) {
   2.133 +                            primaryIndex = (SinglevaluedIndex) object;
   2.134 +                        }
   2.135 +                    } catch (ClassNotFoundException e) {
   2.136 +                        e.printStackTrace(Log.out);
   2.137 +                        throw new StoragePersistentDataException(e.getMessage());
   2.138 +                    } catch (InstantiationException e) {
   2.139 +                        e.printStackTrace(Log.out);
   2.140 +                        throw new StoragePersistentDataException(e.getMessage());
   2.141 +                    } catch (IllegalAccessException e) {
   2.142 +                        e.printStackTrace(Log.out);
   2.143 +                        throw new StoragePersistentDataException(e.getMessage());
   2.144                      }
   2.145 -                } catch (ClassNotFoundException e) {
   2.146 -                    e.printStackTrace(Log.out);
   2.147 -                    throw new StoragePersistentDataException (e.getMessage());
   2.148 -                } catch (InstantiationException e) {
   2.149 -                    e.printStackTrace(Log.out);
   2.150 -                    throw new StoragePersistentDataException (e.getMessage());
   2.151 -                } catch (IllegalAccessException e) {
   2.152 -                    e.printStackTrace(Log.out);
   2.153 -                    throw new StoragePersistentDataException (e.getMessage());
   2.154 +                    XmlUtils.skipTagEnd(is); // </Index>
   2.155 +                    Log.out.unindent();
   2.156                  }
   2.157 -                XmlUtils.skipTagEnd(is); // </Index>
   2.158 -                Log.out.unindent();
   2.159 +            } catch ( java.io.IOException e ) {
   2.160 +                e.printStackTrace(Log.out);
   2.161 +                throw new StoragePersistentDataException(e.getMessage());
   2.162              }
   2.163 -        } catch ( java.io.IOException e ) {
   2.164 -            e.printStackTrace(Log.out);
   2.165 -            throw new StoragePersistentDataException (e.getMessage());
   2.166          }
   2.167          Log.out.unindent();
   2.168          Log.out.println("Finished reading document.");
   2.169      }
   2.170 -
   2.171 +    
   2.172      public void objectStateChanged(Object key) throws StorageException {
   2.173      }
   2.174      
   2.175 @@ -134,51 +139,53 @@
   2.176      
   2.177      public void shutDown() throws StorageException {
   2.178      }
   2.179 -
   2.180 +    
   2.181      public void commitChanges() throws StorageException {
   2.182 -        try {
   2.183 -            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream ( getName()));
   2.184 -            String str = XMI_HEADER;
   2.185 -            outputStream.write(str.getBytes());
   2.186 -            for (Iterator it = maps.values().iterator(); it.hasNext();){
   2.187 -                Object index = it.next();
   2.188 -                String className = index.getClass().getName();
   2.189 -                str = "<Index><class>"+className+"</class>";
   2.190 +        if (this.persistent) {
   2.191 +            try {
   2.192 +                OutputStream outputStream = new BufferedOutputStream(new FileOutputStream( getName()));
   2.193 +                String str = XMI_HEADER;
   2.194                  outputStream.write(str.getBytes());
   2.195 -                Log.out.println("Save index:" + ((Index)index).getName() );
   2.196 -                ((Streamable) index).write (outputStream);
   2.197 -                //Log.out.println("...saved");
   2.198 -                str = "</Index>";
   2.199 +                for (Iterator it = maps.values().iterator(); it.hasNext();){
   2.200 +                    Object index = it.next();
   2.201 +                    String className = index.getClass().getName();
   2.202 +                    str = "<Index><class>"+className+"</class>";
   2.203 +                    outputStream.write(str.getBytes());
   2.204 +                    Log.out.println("Save index:" + ((Index)index).getName() );
   2.205 +                    ((Streamable) index).write(outputStream);
   2.206 +                    //Log.out.println("...saved");
   2.207 +                    str = "</Index>";
   2.208 +                    outputStream.write(str.getBytes());
   2.209 +                }
   2.210 +                str = "</MDRStorage>";
   2.211                  outputStream.write(str.getBytes());
   2.212 +                outputStream.close();
   2.213 +            } catch (java.io.IOException e) {
   2.214 +                e.printStackTrace();
   2.215              }
   2.216 -            str = "</MDRStorage>";
   2.217 -            outputStream.write(str.getBytes());
   2.218 -            outputStream.close();
   2.219 -        } catch (java.io.IOException e) {
   2.220 -            e.printStackTrace();
   2.221          }
   2.222      }
   2.223 -
   2.224 +    
   2.225      public SinglevaluedIndex getSinglevaluedIndex(String name) throws StorageException {
   2.226          return (SinglevaluedIndex) getIndex(name);
   2.227      }
   2.228 -
   2.229 +    
   2.230      public MultivaluedIndex getMultivaluedIndex(String name) throws StorageException {
   2.231          return (MultivaluedIndex) getIndex(name);
   2.232      }
   2.233 -
   2.234 +    
   2.235      public MultivaluedOrderedIndex getMultivaluedOrderedIndex(String name) throws StorageException {
   2.236          return (MultivaluedOrderedIndex) getIndex(name);
   2.237      }
   2.238 -
   2.239 +    
   2.240      public void dropIndex(String name) throws StorageException {
   2.241          maps.remove(name);
   2.242      }
   2.243 -
   2.244 +    
   2.245      public boolean supportsMultipleStorableIndexes() throws StorageException {
   2.246          return true;
   2.247      }
   2.248 -
   2.249 +    
   2.250      /** Creates SinglevaluedIndex.
   2.251       * @param name Singlevalued name of the index
   2.252       * @param type type of indexes values
   2.253 @@ -186,32 +193,32 @@
   2.254       */
   2.255      public SinglevaluedIndex createSinglevaluedIndex(String name,EntryType keyType,EntryType valueType) throws StorageException {
   2.256          if (valueType.equals(EntryType.STREAMABLE)) {
   2.257 -            throw new StorageBadRequestException ("Cannot create another primary index with STREAMABLE value type.");
   2.258 +            throw new StorageBadRequestException("Cannot create another primary index with STREAMABLE value type.");
   2.259          }
   2.260 -        SinglevaluedIndex sm = new SinglevaluedIndexImpl (name, keyType, valueType);
   2.261 -        maps.put (name, sm);
   2.262 +        SinglevaluedIndex sm = new SinglevaluedIndexImpl(name, keyType, valueType);
   2.263 +        maps.put(name, sm);
   2.264          return sm;
   2.265      }
   2.266 -
   2.267 +    
   2.268      /** Creates MultivaluedOrderedIndex.
   2.269       * @param name
   2.270       * @param type
   2.271       * @return
   2.272       */
   2.273      public MultivaluedOrderedIndex createMultivaluedOrderedIndex(String name,EntryType keyType,EntryType valueType,boolean unique) throws StorageException {
   2.274 -        MultivaluedOrderedIndex sm = new MultivaluedOrderedIndexImpl (name, keyType, valueType, unique);
   2.275 -        maps.put (name, sm);
   2.276 +        MultivaluedOrderedIndex sm = new MultivaluedOrderedIndexImpl(name, keyType, valueType, unique);
   2.277 +        maps.put(name, sm);
   2.278          return sm;
   2.279      }
   2.280 -
   2.281 +    
   2.282      /** Creates MultivaluedIndex.
   2.283       * @param name
   2.284       * @param type type of indexes values
   2.285       * @return created index
   2.286       */
   2.287      public MultivaluedIndex createMultivaluedIndex(String name,EntryType keyType,EntryType valueType,boolean unique) throws StorageException {
   2.288 -        MultivaluedIndex sm = new MultivaluedIndexImpl (name, keyType, valueType, unique);
   2.289 -        maps.put (name, sm);
   2.290 +        MultivaluedIndex sm = new MultivaluedIndexImpl(name, keyType, valueType, unique);
   2.291 +        maps.put(name, sm);
   2.292          return sm;
   2.293      }
   2.294      
   2.295 @@ -225,14 +232,14 @@
   2.296      /** Creates primary index. Primary index is SinglevaluedIndex with STREAMABLE valueType.
   2.297       */
   2.298      private void createPrimaryIndex() throws StorageException {
   2.299 -        this.primaryIndex = new SinglevaluedIndexImpl (PRIMARY_INDEX_NAME, EntryType.MOFID, EntryType.STREAMABLE);
   2.300 -        maps.put (PRIMARY_INDEX_NAME, this.primaryIndex);
   2.301 +        this.primaryIndex = new SinglevaluedIndexImpl(PRIMARY_INDEX_NAME, EntryType.MOFID, EntryType.STREAMABLE);
   2.302 +        maps.put(PRIMARY_INDEX_NAME, this.primaryIndex);
   2.303      }
   2.304      
   2.305      /** Retrieve index by name.
   2.306       * @param name name of the index
   2.307       * @return index of the specified name
   2.308 - */
   2.309 +     */
   2.310      public Index getIndex(String name) throws StorageException {
   2.311          return (Index) maps.get(name);
   2.312      }