View Javadoc

1   package uba.db.impl.filesystem;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import uba.db.DatabaseBehavior;
7   import uba.db.DatabaseInitializationException;
8   import uba.db.IdGenerator;
9   import uba.db.SimpleNumberSequenceIdGenerator;
10  import uba.db.SystemTableSchemas;
11  import uba.db.TableCreationException;
12  import uba.db.table.Row;
13  import uba.db.table.Table;
14  import uba.db.table.TableSchema;
15  import uba.db.table.io.TableReader;
16  
17  /***
18   * Implementación de la base de datos, utilizando el file system.
19   * 
20   * @version $Revision: 1.4 $
21   */
22  public class FileSystemDatabase extends DatabaseBehavior {
23      private File directory;
24      private SimpleNumberSequenceIdGenerator idGenerator;
25  
26      public FileSystemDatabase(File directory) throws DatabaseInitializationException {
27          super(directory);
28      }
29  
30      /***
31       * @see uba.db.DatabaseBehavior#configureDatabaseUsing(java.lang.Object)
32       */
33      protected void configureDatabaseUsing(Object configuration)
34              throws DatabaseInitializationException {
35          directory = (File) configuration;
36  
37          if (!directory.exists()) {
38              throw new DatabaseInitializationException("El directorio " + directory
39                      + " no existe.");
40          }
41  
42          if (!directory.isDirectory()) {
43              throw new DatabaseInitializationException("El archivo " + directory
44                      + " no es un directorio.");
45          }
46      }
47  
48      /***
49       * @see uba.db.DatabaseBehavior#createTablesTable()
50       */
51      protected Table createTablesTable() throws Exception {
52          File dataFile = FileSystemTable.dataFileFor(directory,
53                                                      SystemTableSchemas.TABLES_SCHEMA);
54  
55          if (!dataFile.exists()) {
56              FileSystemTable.createDataFile(directory, SystemTableSchemas.TABLES_SCHEMA);
57          }
58  
59          Table tablesTable = new FileSystemTable(SystemTableSchemas.TABLES_SCHEMA,
60                  dataFile);
61  
62          return tablesTable;
63      }
64  
65      /***
66       * @see uba.db.DatabaseBehavior#initializeUserTables()
67       */
68      protected void initializeUserTables() throws DatabaseInitializationException {
69          try {
70              int maxTableId = readTablesFromDisk();
71              idGenerator = new SimpleNumberSequenceIdGenerator(maxTableId);
72          } catch (Exception e) {
73              throw new DatabaseInitializationException(e);
74          }
75      }
76  
77      private int readTablesFromDisk() throws Exception {
78          int maxTableId = SimpleNumberSequenceIdGenerator.DEFAULT_SEQUENCE_START;
79  
80          TableReader reader = tablesTable().reader();
81          while (reader.hasMoreRows()) {
82              Row row = reader.fetchRow();
83              Integer tableId = (Integer) row.valueAt(0);
84              String tableName = (String) row.valueAt(1);
85              
86              if (tableId.intValue() > maxTableId) {
87                  maxTableId = tableId.intValue();
88              }
89              
90              Table table = FileSystemTable.createFrom(new File(directory, tableName
91                      + FileSystemTable.SCHEMA_FILE_SUFFIX));
92  
93              addToUserTablesMap(table);
94              addToColumnsTable(tableId, table);
95          }
96          
97          return maxTableId;
98      }
99  
100     /***
101      * @see uba.db.DatabaseBehavior#basicCreateTable(uba.db.table.TableSchema)
102      */
103     protected Table basicCreateTable(TableSchema tableSchema)
104             throws TableCreationException {
105         try {
106             return FileSystemTable.createNew(directory, tableSchema);
107         } catch (IOException e) {
108             throw new TableCreationException(this, tableSchema, e);
109         }
110     }
111 
112     /***
113      * @see uba.db.DatabaseBehavior#idGenerator()
114      */
115     protected IdGenerator idGenerator() {
116         return idGenerator;
117     }
118 }