vgritsenko    2003/08/14 20:08:36

  Modified:    java/src/org/apache/xindice/core FaultCodes.java
               java/src/org/apache/xindice/core/filer BTree.java
                        BTreeFiler.java FSFiler.java
               java/tests/src/org/apache/xindice/core/filer
                        FilerTestCase.java FilerTestSetup.java
  Log:
  Improve exception handling in Filers.
  Add fault code DBE_CANNOT_READ in addition to _DROP and _CREATE.
  Use these fault codes consistently in all filers.
  
  Revision  Changes    Path
  1.16      +14 -2     
xml-xindice/java/src/org/apache/xindice/core/FaultCodes.java
  
  Index: FaultCodes.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/FaultCodes.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- FaultCodes.java   9 Aug 2003 18:56:19 -0000       1.15
  +++ FaultCodes.java   15 Aug 2003 03:08:36 -0000      1.16
  @@ -85,10 +85,12 @@
       public static final int SEC = (int) (700l);
       public static final int URI = (int) (800l);
       public static final int JAVA = (int) (2000l);
  +
       public static final int GEN_UNKNOWN = (int) (0l);
       public static final int GEN_GENERAL_ERROR = (int) (40l);
       public static final int GEN_CRITICAL_ERROR = (int) (70l);
       public static final int GEN_FATAL_ERROR = (int) (90l);
  +
       public static final int COL_COLLECTION_NOT_FOUND = (int) (200l);
       public static final int COL_DOCUMENT_NOT_FOUND = (int) (201l);
       public static final int COL_DUPLICATE_COLLECTION = (int) (240l);
  @@ -103,6 +105,7 @@
       public static final int COL_CANNOT_CREATE = (int) (270l);
       public static final int COL_CANNOT_DROP = (int) (271l);
       public static final int COL_INVALID_RESULT = (int) (277l);
  +
       public static final int IDX_VALUE_NOT_FOUND = (int) (300l);
       public static final int IDX_INDEX_NOT_FOUND = (int) (301l);
       public static final int IDX_MATCHES_NOT_FOUND = (int) (340l);
  @@ -111,27 +114,35 @@
       public static final int IDX_STYLE_NOT_FOUND = (int) (371l);
       public static final int IDX_CORRUPTED = (int) (372l);
       public static final int IDX_CANNOT_CREATE = (int) (373l);
  +
       public static final int TRX_DOC_LOCKED = (int) (400l);
       public static final int TRX_NO_CONTEXT = (int) (440l);
       public static final int TRX_NOT_ACTIVE = (int) (441l);
       public static final int TRX_NOT_SUPPORTED = (int) (470l);
  +
       public static final int DBE_NO_PARENT = (int) (500l);
       public static final int DBE_CANNOT_DROP = (int) (570l);
       public static final int DBE_CANNOT_CREATE = (int) (571l);
  +    public static final int DBE_CANNOT_READ = (int) (572l);
  +
       public static final int QRY_NULL_RESULT = (int) (600l);
       public static final int QRY_COMPILATION_ERROR = (int) (640l);
       public static final int QRY_PROCESSING_ERROR = (int) (641l);
       public static final int QRY_NOT_SUPPORTED = (int) (670l);
       public static final int QRY_STYLE_NOT_FOUND = (int) (671l);
  +
       public static final int SEC_INVALID_USER = (int) (770l);
       public static final int SEC_INVALID_GROUP = (int) (771l);
       public static final int SEC_INVALID_ACCESS = (int) (772l);
       public static final int SEC_INVALID_CREDENTIALS = (int) (773l);
  +
       public static final int URI_EMPTY = (int) (800l);
       public static final int URI_NULL = (int) (801l);
       public static final int URI_PARSE_ERROR = (int) (820l);
  +
       public static final int JAVA_RUNTIME_ERROR = (int) (2070l);
   
  +
       private FaultCodes() {
       }
   
  @@ -178,6 +189,7 @@
           putCodeMessage(DBE_NO_PARENT, "Database No Parent");
           putCodeMessage(DBE_CANNOT_DROP, "Database Cannot Drop");
           putCodeMessage(DBE_CANNOT_CREATE, "Database Cannot Create");
  +        putCodeMessage(DBE_CANNOT_READ, "Database Cannot Read");
   
           // Query-related errors 600 series
           putCodeMessage(QRY_NULL_RESULT, "Query Null Result");
  
  
  
  1.20      +11 -8     
xml-xindice/java/src/org/apache/xindice/core/filer/BTree.java
  
  Index: BTree.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/filer/BTree.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- BTree.java        12 Aug 2003 02:57:29 -0000      1.19
  +++ BTree.java        15 Aug 2003 03:08:36 -0000      1.20
  @@ -595,15 +595,16 @@
   
           public synchronized long removeValue(Value value) throws 
IOException, BTreeException {
               int idx = Arrays.binarySearch(values, value);
  +
               switch (ph.getStatus()) {
                   case BRANCH:
                       idx = idx < 0 ? -(idx + 1) : idx + 1;
                       return getChildNode(idx).removeValue(value);
   
                   case LEAF:
  -                    if (idx < 0)
  -                        throw new BTreeNotFoundException("Value '" + 
value.toString() + "' doesn't exist");
  -                    else {
  +                    if (idx < 0) {
  +                        throw new BTreeNotFoundException("Value '" + value + 
"' doesn't exist");
  +                    } else {
                           long oldPtr = ptrs[idx];
   
                           setValues(deleteArrayValue(values, idx));
  @@ -614,7 +615,8 @@
                       }
   
                   default :
  -                    throw new BTreeCorruptException("Invalid Page Type In 
removeValue");
  +                    throw new BTreeCorruptException("Invalid page type '" + 
ph.getStatus() +
  +                                                    "' in removeValue");
               }
           }
   
  @@ -781,13 +783,14 @@
   
                   case LEAF:
                       if (idx < 0) {
  -                        throw new BTreeNotFoundException("Value '" + 
value.toString() + "' doesn't exist");
  +                        throw new BTreeNotFoundException("Value '" + value + 
"' doesn't exist");
                       } else {
                           return ptrs[idx];
                       }
   
                   default :
  -                    throw new BTreeCorruptException("Invalid Page Type In 
findValue");
  +                    throw new BTreeCorruptException("Invalid page type '" + 
ph.getStatus() +
  +                                                    "' in findValue");
               }
           }
   
  
  
  
  1.18      +26 -30    
xml-xindice/java/src/org/apache/xindice/core/filer/BTreeFiler.java
  
  Index: BTreeFiler.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/filer/BTreeFiler.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- BTreeFiler.java   12 Aug 2003 02:57:29 -0000      1.17
  +++ BTreeFiler.java   15 Aug 2003 03:08:36 -0000      1.18
  @@ -139,6 +139,7 @@
           if (key == null || key.equals("")) {
               return null;
           }
  +
           checkOpened();
           try {
               long pos = findValue(key);
  @@ -146,32 +147,32 @@
               Value v = readValue(startPage);
               BTreeFilerPageHeader sph = (BTreeFilerPageHeader) 
startPage.getPageHeader();
   
  -            HashMap meta = new HashMap(2);
  +            HashMap meta = new HashMap(2, 1.5F);
               meta.put(Record.CREATED, new Long(sph.getCreated()));
               meta.put(Record.MODIFIED, new Long(sph.getModified()));
   
               return new Record(key, v, meta);
           } catch (BTreeNotFoundException e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  -        } catch (BTreeException e) {
  -            throw e;
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  +            if (log.isDebugEnabled()) {
  +                log.debug("Record '" + key + "' not found: " + e);
               }
  +        } catch (IOException e) {
  +            throw new FilerException(FaultCodes.DBE_CANNOT_READ,
  +                                     "Can't read record '" + key + "': " + 
e.getMessage(), e);
           }
           return null;
       }
   
       public boolean writeRecord(Key key, Value value) throws DBException {
           if (key == null || key.equals("")) {
  -            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid 
key: '" + key + "'");
  +            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE,
  +                                     "Invalid key: null or empty");
           }
           if (value == null) {
  -            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid 
null value");
  +            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE,
  +                                     "Invalid value: null");
           }
  +
           checkOpened();
           try {
               Page p;
  @@ -189,22 +190,17 @@
               if (ph.getStatus() == UNUSED) {
                   ph.setCreated(t);
               }
  -
               ph.setModified(t);
               ph.setStatus(RECORD);
   
               writeValue(p, value);
   
               flush();
  -        } catch (DBException e) {
  -            throw e;
           } catch (IOException e) {
  -            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, 
e.getMessage(), e);
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  +            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE,
  +                                     "Can't write record '" + key + "': " + 
e.getMessage(), e);
           }
  +
           return true;
       }
   
  @@ -212,6 +208,7 @@
           if (key == null || key.equals("")) {
               return false;
           }
  +
           checkOpened();
           try {
               long pos = findValue(key);
  @@ -226,16 +223,14 @@
   
               return true;
           } catch (BTreeNotFoundException e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  -        } catch (BTreeException e) {
  -            throw e;
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  +            if (log.isDebugEnabled()) {
  +                log.debug("Record '" + key + "' not found (" + e + ")");
               }
  +        } catch (IOException e) {
  +            throw new FilerException(FaultCodes.DBE_CANNOT_DROP,
  +                                     "Can't delete record '" + key + "': " + 
e.getMessage(), e);
           }
  +
           return false;
       }
   
  @@ -266,7 +261,8 @@
                   query(null, this);
                   enum = keys.iterator();
               } catch (IOException e) {
  -                throw new FilerException(FaultCodes.GEN_CRITICAL_ERROR, 
"Error generating RecordSet", e);
  +                throw new FilerException(FaultCodes.GEN_CRITICAL_ERROR,
  +                                         "Error generating RecordSet", e);
               }
           }
   
  
  
  
  1.15      +17 -19    
xml-xindice/java/src/org/apache/xindice/core/filer/FSFiler.java
  
  Index: FSFiler.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/filer/FSFiler.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- FSFiler.java      12 Aug 2003 02:57:29 -0000      1.14
  +++ FSFiler.java      15 Aug 2003 03:08:36 -0000      1.15
  @@ -76,6 +76,7 @@
   import java.io.File;
   import java.io.FileFilter;
   import java.io.FileOutputStream;
  +import java.io.IOException;
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Set;
  @@ -132,13 +133,15 @@
   
       private void checkOpened() throws DBException {
           if (!opened) {
  -            throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED, 
"Filer is closed");
  +            throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED,
  +                                     "Filer is closed");
           }
       }
   
       private void checkReadOnly() throws DBException {
           if (readOnly) {
  -            throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY, 
"Filer is read-only");
  +            throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY,
  +                                     "Filer is read-only");
           }
       }
   
  @@ -180,6 +183,7 @@
           if (key == null || key.equals("")) {
               return null;
           }
  +
           checkOpened();
   
           String fname = key.toString();
  @@ -191,17 +195,16 @@
           try {
               locks.acquireSharedLock(file);
   
  -            HashMap meta = new HashMap(2);
  +            HashMap meta = new HashMap(2, 1.5F);
               meta.put(Record.MODIFIED, new Long(file.lastModified()));
   
               byte[] valueData = cache.getFile(file);
               if (valueData != null) {
                   return new Record(key, new Value(valueData), meta);
               }
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  +        } catch (IOException e) {
  +            throw new FilerException(FaultCodes.DBE_CANNOT_READ,
  +                                     "Can't read record '" + key + "': " + 
e.getMessage(), e);
           } finally {
               locks.releaseSharedLock(file);
           }
  @@ -215,6 +218,7 @@
           if (value == null) {
               throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid 
null value");
           }
  +
           checkOpened();
           checkReadOnly();
   
  @@ -230,14 +234,12 @@
               value.streamTo(fos);
               fos.close();
               return true;
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  +        } catch (IOException e) {
  +            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE,
  +                                     "Can't write record '" + key + "': " + 
e.getMessage(), e);
           } finally {
               locks.releaseExclusiveLock(file);
           }
  -        return true;
       }
   
       public boolean deleteRecord(Key key) throws DBException {
  @@ -255,15 +257,11 @@
           File file = new File(dir, fname);
           try {
               locks.acquireExclusiveLock(file);
  +            // TODO: Should Exception (SecurityException) be catched here or 
not?
               return file.delete();
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
           } finally {
               locks.releaseExclusiveLock(file);
           }
  -        return true;
       }
   
       public long getRecordCount() throws DBException {
  
  
  
  1.8       +11 -15    
xml-xindice/java/tests/src/org/apache/xindice/core/filer/FilerTestCase.java
  
  Index: FilerTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/tests/src/org/apache/xindice/core/filer/FilerTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FilerTestCase.java        7 Aug 2003 20:13:26 -0000       1.7
  +++ FilerTestCase.java        15 Aug 2003 03:08:36 -0000      1.8
  @@ -123,31 +123,28 @@
       public void testFailWriteRecordNullKey() throws Exception {
           try {
               filer.writeRecord(null, TEST_VALUE);
  +            fail("Expecting FilerException");
  +        } catch (FilerException e) {
  +            // Ok
           }
  -        catch (FilerException e) {
  -            return;
  -        }
  -        fail("Expecting FilerException");
       }
   
       public void testFailWriteRecordNullValue() throws Exception {
           try {
               filer.writeRecord(TEST_KEY, null);
  +            fail("Expecting FilerException");
  +        } catch (FilerException e) {
  +            // Ok
           }
  -        catch (FilerException e) {
  -            return;
  -        }
  -        fail("Expecting FilerException");
       }
   
       public void testFailWriteRecordNullValueKey() throws Exception {
           try {
               filer.writeRecord(null, null);
  +            fail("Expecting FilerException");
  +        } catch (FilerException e) {
  +            // Ok
           }
  -        catch (FilerException e) {
  -            return;
  -        }
  -        fail("Expecting FilerException");
       }
   
       public void testReadRecord() throws Exception {
  @@ -284,5 +281,4 @@
   
           assertTrue(filer.getRecordCount() == 0);
       }
  -
   }
  
  
  
  1.5       +2 -3      
xml-xindice/java/tests/src/org/apache/xindice/core/filer/FilerTestSetup.java
  
  Index: FilerTestSetup.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/tests/src/org/apache/xindice/core/filer/FilerTestSetup.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FilerTestSetup.java       7 Aug 2003 20:13:26 -0000       1.4
  +++ FilerTestSetup.java       15 Aug 2003 03:08:36 -0000      1.5
  @@ -108,5 +108,4 @@
               filer.close();
           }
       }
  -
   }
  
  
  

Reply via email to