Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]
    1   //
    2   //  Neuros Database Manipulator
    3   //  Copyright (C) 2003  Neuros Database Manipulator
    4   //
    5   //  This program is free software; you can redistribute it and/or modify
    6   //  it under the terms of the GNU General Public License as published by
    7   //  the Free Software Foundation; either version 2 of the License, or
    8   //  (at your option) any later version.
    9   //
   10   //  This program is distributed in the hope that it will be useful,
   11   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13   //  GNU General Public License for more details.
   14   //
   15   //  You should have received a copy of the GNU General Public License
   16   //  along with this program; if not, write to the Free Software
   17   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18   //
   19   //  For information about Neuros Database Manipulator and its authors,
   20   //  please contact the Neuros Database Manipulator Web Site at
   21   //  http://neurosdbm.sourceforge.net
   22   //
   23   //
   24   
   25   package net.sourceforge.neurosdbm.db;
   26   
   27   
   28   import java.util.ArrayList;
   29   import java.util.Iterator;
   30   import java.io.FileNotFoundException;
   31   import java.io.IOException;
   32   import java.io.File;
   33   import java.io.FileInputStream;
   34   import java.io.FileOutputStream;
   35   import net.sourceforge.neurosdbm.Logging;
   36   
   37   public abstract class SAI {
   38   
   39     protected byte[] bytes;
   40     protected Word numEntries;
   41     
   42     protected ArrayList saiRecordsArrayList;
   43     private String fileName;
   44   
   45     protected int nextKey;
   46   
   47     private MDB mdb;
   48     private PAI pai;
   49     
   50    
   51     protected SAI(String fileName, MDB mdb, PAI pai)
   52       throws FileNotFoundException, IOException, DatabaseCorruptException {
   53   
   54       this.fileName = fileName;
   55       this.nextKey = 2;
   56       this.mdb = mdb;
   57       this.pai = pai;
   58       
   59       loadExistingDatabase(mdb, pai);
   60     }
   61   
   62     protected SAI(String fileName, boolean createEmpty, MDB mdb,
   63                   PAI pai)
   64       throws FileNotFoundException, IOException, DatabaseCorruptException {
   65       
   66       this.fileName = fileName;
   67       this.nextKey = 2;
   68       this.mdb = mdb;
   69       this.pai = pai;
   70   
   71       if (createEmpty) {
   72         // header is 12 words long
   73         createEmptyDatabase(mdb, pai);
   74       } else {
   75         loadExistingDatabase(mdb, pai);
   76       }
   77     }
   78   
   79     
   80     private void loadExistingDatabase(MDB mdb, PAI pai)
   81       throws FileNotFoundException, IOException, DatabaseCorruptException {
   82       
   83       File file = new File(fileName);
   84       // TODO: check length is longer that max int
   85       int fileLengthInBytes = (int) file.length();
   86       int fileLengthInWords = fileLengthInBytes/2;
   87   
   88       FileInputStream fileStream = new FileInputStream(fileName);
   89   
   90       bytes = new byte[fileLengthInBytes];
   91   
   92       fileStream.read(bytes);
   93   
   94       int wordOffset = parseSAIHeader(bytes);
   95       
   96       saiRecordsArrayList = new ArrayList();
   97       for (int cnt=0; cnt < numEntries.getValue(); cnt++) {
   98         try {
   99           SAIRecord sr = new SAIRecord(nextKey++, bytes, wordOffset, mdb, pai);
  100           saiRecordsArrayList.add(sr);
  101           wordOffset += sr.getRecordLength();
  102         } catch (Exception exp) {
  103           // For bug 784561
  104           Logging.fatal("wordOffset = " + wordOffset);
  105           Logging.fatal("length of bytes = " + bytes.length);
  106           Logging.fatal("fileName = " + fileName);
  107           Logging.fatal("numEntries = " + numEntries.getValue());
  108           Logging.fatal("mdb = " + mdb);
  109           Logging.fatal("pai = " + pai);
  110         }
  111       }
  112     }
  113   
  114   
  115     abstract void createEmptyDatabase(MDB mdb, PAI pai);
  116   
  117     
  118     protected int parseSAIHeader(byte[] bytes)
  119       throws DatabaseCorruptException {
  120       
  121       int wordOffset = 0;
  122       
  123       Word signature1 = new Word(bytes, wordOffset);
  124       wordOffset++;
  125       if (signature1.getValue() != 0x0518) {
  126         throw new DatabaseCorruptException("Invalid signature on SAI ("
  127                                            + fileName + ")");
  128       }
  129       Word signature2 = new Word(bytes, wordOffset);
  130       wordOffset++;
  131       if (signature2.getValue() != 0x1971) {
  132         throw new DatabaseCorruptException("Invalid signature on SAI ("
  133                                            + fileName + ")");
  134       }
  135   
  136       // Skip over reserved words
  137       wordOffset += 2;
  138   
  139       numEntries = new Word(bytes, wordOffset);
  140       wordOffset++;
  141   
  142       // Skip over reserved words
  143       wordOffset += 3;
  144   
  145       // Empty record
  146       wordOffset += 4;
  147   
  148       return wordOffset;
  149     }
  150   
  151   
  152     public Iterator getEntriesIterator() {
  153       return saiRecordsArrayList.iterator();
  154     }
  155   
  156   
  157     void reorder(ArrayList list) {
  158       ArrayList newSaiRecordsArrayList = new ArrayList();
  159       
  160       // Add null record
  161       newSaiRecordsArrayList.add(saiRecordsArrayList.get(0));
  162       
  163       for (Iterator iter = list.iterator(); iter.hasNext(); ) {
  164         DisplayIndex index = (DisplayIndex)iter.next();
  165         for (Iterator iter2 = saiRecordsArrayList.iterator();
  166              iter2.hasNext() ;) {
  167           SAIRecord rec = (SAIRecord)iter2.next();
  168           if (rec.getMDBKey() == index.getKey()) {
  169             newSaiRecordsArrayList.add(rec);
  170           }
  171         }
  172       }
  173       saiRecordsArrayList = newSaiRecordsArrayList;
  174     }
  175   
  176   
  177     void write() throws IOException {
  178       File fileToCreate = new File(fileName);
  179       File parentDir = new File(fileToCreate.getParent());
  180       parentDir.mkdirs();
  181       NIOFile file = new NIOFile(fileName);
  182       
  183       // signature
  184       file.append(new Word(0x0518));
  185       file.append(new Word(0x1971));
  186   
  187       // reserved
  188       file.append(new Word(0x0000));
  189       file.append(new Word(0x0000));
  190   
  191       numEntries = new Word(saiRecordsArrayList.size());
  192       file.append(numEntries);
  193   
  194       // reserved
  195       file.append(new Word(0x0000));
  196       file.append(new Word(0x0000));
  197       file.append(new Word(0x0000));
  198   
  199       // empty record
  200       file.append(new WordPointer(0x00000000));
  201       file.append(new WordPointer(0x00000000));
  202   
  203       for (int cnt=0; cnt<saiRecordsArrayList.size(); cnt++) {
  204         SAIRecord rec = (SAIRecord)saiRecordsArrayList.get(cnt);
  205         rec.write(file);
  206       }
  207       
  208       // empty record
  209       file.append(new WordPointer(0x00000000));
  210       file.append(new WordPointer(0x00000000));
  211       
  212       file.write();
  213     }
  214   
  215   
  216     int getPAIKey(int mdbKey) {
  217       for (Iterator iter = saiRecordsArrayList.iterator(); iter.hasNext() ;) {
  218         SAIRecord rec = (SAIRecord) iter.next();
  219         if (rec.getMDBKey() == mdbKey) {
  220           return rec.getPAIKey();
  221         }
  222       }
  223       return 0;
  224     }
  225   
  226   
  227     int addNewRecord(int mdbKey, int paiKey) {
  228       int key = nextKey++;
  229       SAIRecord rec = new SAIRecord(key, mdbKey, paiKey, mdb, pai);
  230       saiRecordsArrayList.add(rec);
  231       return key;
  232     }
  233   
  234   
  235     void deleteRecord(int mdbKey) {
  236       for (Iterator iter = saiRecordsArrayList.iterator(); iter.hasNext() ;) {
  237         SAIRecord rec = (SAIRecord)iter.next();
  238         if (rec.getMDBKey() == mdbKey) {
  239           iter.remove();
  240           return;
  241         }
  242       }
  243     }
  244   
  245   
  246     public String toString() {
  247       String returnString = new String();
  248       returnString += "---- SAI(" + fileName + ") Start ----\n";
  249       for (Iterator iter = saiRecordsArrayList.iterator(); iter.hasNext() ;) {
  250         returnString += iter.next() + "\n";
  251       }
  252       returnString += "---- SAI(" + fileName + ") End ----";
  253       return returnString;
  254     }
  255   }

Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]