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.TreeMap;
   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   
   36   final public class GenreMDB extends MDB {
   37   
   38     public GenreMDB(String fileName)
   39       throws FileNotFoundException, IOException, DatabaseCorruptException {
   40   
   41       super(fileName);
   42   
   43       loadExistingDatabase();
   44     }
   45       
   46   
   47     public GenreMDB(String fileName, boolean createEmpty)
   48       throws FileNotFoundException, IOException, DatabaseCorruptException {
   49   
   50       super(fileName);
   51   
   52       if (createEmpty) {
   53         createEmptyDatabase();
   54       } else {
   55         loadExistingDatabase();
   56       }
   57     }
   58     
   59       
   60     private void loadExistingDatabase()
   61       throws FileNotFoundException, IOException, DatabaseCorruptException {
   62       
   63       File file = new File(fileName);
   64       // TODO: check length is longer that max int
   65       int fileLengthInBytes = (int) file.length();
   66       int fileLengthInWords = fileLengthInBytes/2;
   67   
   68       FileInputStream fileStream = new FileInputStream(fileName);
   69   
   70       byte[] bytes = new byte[fileLengthInBytes];
   71   
   72       fileStream.read(bytes);
   73   
   74       parseMDBHeader(bytes);
   75   
   76       // Start parsing the data records
   77       int wordOffset = recordStart.getValue();
   78   
   79       int key = nextKey++;
   80       NullRecord nr = new NullRecord(key);
   81       WordPointer ptr = new WordPointer(wordOffset);
   82       keysToMDBRecordsMap.put(new Integer(key), nr);
   83       keysToWordPointersMap.put(new Integer(key), ptr);
   84       wordOffset += nr.getRecordLength();
   85   
   86       // parse data records
   87       while (wordOffset < fileLengthInWords) {
   88         key = nextKey++;
   89         GenreRecord gr = new GenreRecord(key, bytes, wordOffset,
   90                                          numAccessKeys(), numExtraInfo());
   91         ptr = new WordPointer(wordOffset);
   92         keysToMDBRecordsMap.put(new Integer(key), gr);
   93         keysToWordPointersMap.put(new Integer(key), ptr);
   94         wordOffset += gr.getRecordLength();
   95       }
   96     }
   97   
   98     
   99     private void createEmptyDatabase()
  100       throws DatabaseCorruptException {
  101         
  102       attributes = new Word(0x0000);
  103       status = new Word(0x0000);
  104       numOfKeys = new Word(0x0001);
  105       numOfFieldsPerRecord = new Word(0x0001);
  106       recordStart = new WordPointer(0x0000002a);
  107       pXIM = new WordPointer(0x00000000);
  108       db_ID = new Word(0x0000);
  109       pDB_Name = new WordPointerToDisplayData(0x00000018);
  110       pParentDBFilename = new WordPointerToWordString(0x0000001d);
  111       pRules = new Rule[1];
  112       pRules[0] = new Rule(new WordPointerToDisplayData(0x00000023),
  113                            new WordPointerToWordString(0x00000000),
  114                            new DisplayData("Genres"), null);
  115       db_Name = new DisplayData("Genre");
  116       parentDBFilename = new WordString("audio.mdb");
  117       ximData = null;
  118       
  119       // add null record
  120       int key = nextKey++;
  121       keysToMDBRecordsMap.put(new Integer(key), new NullRecord(key));
  122       keysToWordPointersMap.put(new Integer(key), recordStart);
  123     }
  124   
  125     
  126     void write() throws IOException, DatabaseInternalCorruptionException {
  127       File file = new File(fileName);
  128       File parentDir = new File(file.getParent());
  129       parentDir.mkdirs();
  130       NIOFile fileOutput = new NIOFile(fileName);
  131       super.write(fileOutput);
  132       fileOutput.write();
  133     }
  134   
  135     
  136     int addNewTrack(NewTrack newTrack, GenreSAI genreSAI, PAI genrePAI,
  137                     int audioKey)
  138       throws DatabaseInternalCorruptionException {
  139   
  140       if (newTrack.neurosGenre == null) {
  141         return 1;  // null record
  142       }
  143   
  144       keysToWordPointersMap = null;
  145   
  146       int keyFound = 0;
  147       for (Iterator iter = keysToMDBRecordsMap.keySet().iterator();
  148            iter.hasNext() ;) {
  149         Integer key = (Integer)iter.next();
  150         MDBRecord rec = (MDBRecord)keysToMDBRecordsMap.get(key);
  151         if (rec instanceof GenreRecord) {
  152           GenreRecord genreRec = (GenreRecord)rec;
  153           if (genreRec.getGenre().toString().equals(newTrack.neurosGenre)) {
  154             keyFound = key.intValue();
  155           }
  156         }
  157       }
  158   
  159       int returnKey = 0;
  160       // new Genre
  161       if (keyFound == 0) {
  162         returnKey = nextKey++;
  163         GenreRecord rec = new GenreRecord(returnKey, newTrack.neurosGenre);
  164         keysToMDBRecordsMap.put(new Integer(returnKey), rec);
  165         int paiKey = genrePAI.addNewRecord(audioKey);
  166         genreSAI.addNewRecord(returnKey, paiKey);
  167       } else {
  168         returnKey = keyFound;
  169         int paiKey = genreSAI.getPAIKey(returnKey);
  170         genrePAI.addNewRecord(audioKey, paiKey);
  171       }
  172       return returnKey;
  173     }
  174   
  175   
  176     String getGenre(int key) {
  177       MDBRecord baseRec =
  178         (MDBRecord) keysToMDBRecordsMap.get(new Integer(key));
  179       if (baseRec == null) {
  180         return null;
  181       }
  182       if (!(baseRec instanceof GenreRecord)) {
  183         return null;
  184       }
  185       GenreRecord rec = (GenreRecord) baseRec;
  186       return rec.getGenre().toString();
  187     }
  188   }
  189   

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