22
33import edu .utdallas .tiny_db .server .d_storage_engine .common .transaction .Transaction ;
44import edu .utdallas .tiny_db .server .d_storage_engine .impl .data .heap .HeapRWRecordScan ;
5-
65import java .util .HashMap ;
76import java .util .Map ;
87
98/**
10- * The table manager.
11- * There are methods to create a table, save the metadata
12- * in the catalog, and obtain the metadata of a
13- * previously-created table.
9+ * The table manager. There are methods to create a table, save the metadata in the catalog, and
10+ * obtain the metadata of a previously-created table.
1411 *
1512 * @author Edward Sciore
1613 */
1714public class TableMgr {
18- // The max characters a tablename or fieldname can have.
19- public static final int MAX_NAME = 16 ;
20- private TablePhysicalLayout tcatRecordValueLayout , fcatRecordValueLayout ;
2115
22- /**
23- * Create a new catalog manager for the database system.
24- * If the database is new, the two catalog tables
25- * are created.
26- *
27- * @param isNew has the value true if the database is new
28- * @param tx the startup transaction
29- */
30- public TableMgr (boolean isNew , Transaction tx ) {
31- TableDefinition tcatTableDefinition = new TableDefinition ();
32- tcatTableDefinition .addStringField ("tblname" , MAX_NAME );
33- tcatTableDefinition .addIntField ("slotsize" );
34- tcatRecordValueLayout = new TablePhysicalLayout (tcatTableDefinition );
16+ // The max characters a tablename or fieldname can have.
17+ public static final int MAX_NAME = 16 ;
18+ private TablePhysicalLayout tcatRecordValueLayout , fcatRecordValueLayout ;
19+
20+ /**
21+ * Create a new catalog manager for the database system. If the database is new, the two catalog
22+ * tables are created.
23+ *
24+ * @param isNew has the value true if the database is new
25+ * @param tx the startup transaction
26+ */
27+ public TableMgr (boolean isNew , Transaction tx ) {
28+ TableDefinition tcatTableDefinition = new TableDefinition ();
29+ tcatTableDefinition .addStringField ("tblname" , MAX_NAME );
30+ tcatTableDefinition .addIntField ("slotsize" );
31+ tcatRecordValueLayout = new TablePhysicalLayout (tcatTableDefinition );
3532
36- TableDefinition fcatTableDefinition = new TableDefinition ();
37- fcatTableDefinition .addStringField ("tblname" , MAX_NAME );
38- fcatTableDefinition .addStringField ("fldname" , MAX_NAME );
39- fcatTableDefinition .addIntField ("type" );
40- fcatTableDefinition .addIntField ("length" );
41- fcatTableDefinition .addIntField ("offset" );
42- fcatRecordValueLayout = new TablePhysicalLayout (fcatTableDefinition );
33+ TableDefinition fcatTableDefinition = new TableDefinition ();
34+ fcatTableDefinition .addStringField ("tblname" , MAX_NAME );
35+ fcatTableDefinition .addStringField ("fldname" , MAX_NAME );
36+ fcatTableDefinition .addIntField ("type" );
37+ fcatTableDefinition .addIntField ("length" );
38+ fcatTableDefinition .addIntField ("offset" );
39+ fcatRecordValueLayout = new TablePhysicalLayout (fcatTableDefinition );
4340
44- if (isNew ) {
45- createTable ("tinydb_tables" , tcatTableDefinition , tx );
46- createTable ("tinydb_columns" , fcatTableDefinition , tx );
47- }
41+ if (isNew ) {
42+ createTable ("tinydb_tables" , tcatTableDefinition , tx );
43+ createTable ("tinydb_columns" , fcatTableDefinition , tx );
4844 }
45+ }
4946
50- /**
51- * Create a new table having the specified name and schema.
52- *
53- * @param tblname the name of the new table
54- * @param sch the table's schema
55- * @param tx the transaction creating the table
56- */
57- public void createTable (String tblname , TableDefinition sch , Transaction tx ) {
58- TablePhysicalLayout recordValueLayout = new TablePhysicalLayout (sch );
47+ /**
48+ * Create a new table having the specified name and schema.
49+ *
50+ * @param tblname the name of the new table
51+ * @param sch the table's schema
52+ * @param tx the transaction creating the table
53+ */
54+ public void createTable (String tblname , TableDefinition sch , Transaction tx ) {
55+ TablePhysicalLayout recordValueLayout = new TablePhysicalLayout (sch );
5956
60- HeapRWRecordScan tcat = new HeapRWRecordScan (tx , "tinydb_tables" , tcatRecordValueLayout );
61- tcat .seekToInsertStart ();
62- tcat .setString ("tblname" , tblname );
63- tcat .setInt ("slotsize" , recordValueLayout .slotSize ());
64- tcat .close ();
57+ HeapRWRecordScan tcat = new HeapRWRecordScan (tx , "tinydb_tables" , tcatRecordValueLayout );
58+ tcat .seekToInsertStart ();
59+ tcat .setString ("tblname" , tblname );
60+ tcat .setInt ("slotsize" , recordValueLayout .slotSize ());
61+ tcat .close ();
6562
66- HeapRWRecordScan fcat = new HeapRWRecordScan (tx , "tinydb_columns" , fcatRecordValueLayout );
67- for (String fldname : sch .fields ()) {
68- fcat .seekToInsertStart ();
69- fcat .setString ("tblname" , tblname );
70- fcat .setString ("fldname" , fldname );
71- fcat .setInt ("type" , sch .type (fldname ));
72- fcat .setInt ("length" , sch .length (fldname ));
73- fcat .setInt ("offset" , recordValueLayout .offset (fldname ));
74- }
75- fcat .close ();
63+ HeapRWRecordScan fcat = new HeapRWRecordScan (tx , "tinydb_columns" , fcatRecordValueLayout );
64+ for (String fldname : sch .fields ()) {
65+ fcat .seekToInsertStart ();
66+ fcat .setString ("tblname" , tblname );
67+ fcat .setString ("fldname" , fldname );
68+ fcat .setInt ("type" , sch .type (fldname ));
69+ fcat .setInt ("length" , sch .length (fldname ));
70+ fcat .setInt ("offset" , recordValueLayout .offset (fldname ));
7671 }
72+ fcat .close ();
73+ }
7774
78- /**
79- * Retrieve the layout of the specified table
80- * from the catalog.
81- *
82- * @param tblname the name of the table
83- * @param tx the transaction
84- * @return the table's stored metadata
85- */
86- public TablePhysicalLayout getLayout (String tblname , Transaction tx ) {
87- int size = -1 ;
88- HeapRWRecordScan tcat = new HeapRWRecordScan (tx , "tinydb_tables" , tcatRecordValueLayout );
89- while (tcat .next ()) if (tcat .getString ("tblname" ).equals (tblname )) {
90- size = tcat .getInt ("slotsize" );
91- break ;
92- }
93- tcat .close ();
75+ /**
76+ * Retrieve the layout of the specified table from the catalog.
77+ *
78+ * @param tblname the name of the table
79+ * @param tx the transaction
80+ * @return the table's stored metadata
81+ */
82+ public TablePhysicalLayout getLayout (String tblname , Transaction tx ) {
83+ int size = -1 ;
84+ HeapRWRecordScan tcat = new HeapRWRecordScan (tx , "tinydb_tables" , tcatRecordValueLayout );
85+ // [tableName1, slotsize1] | [tableName2, slotsize2] | [tableName3, slotsize3]
86+ while (tcat .next ()) {
87+ if (tcat .getString ("tblname" ).equals (tblname )) {
88+ size = tcat .getInt ("slotsize" );
89+ break ;
90+ }
91+ }
92+ tcat .close ();
9493
95- TableDefinition sch = new TableDefinition ();
96- Map <String , Integer > offsets = new HashMap <String , Integer >();
97- HeapRWRecordScan fcat = new HeapRWRecordScan (tx , "tinydb_columns" , fcatRecordValueLayout );
98- while ( fcat . next ()) if ( fcat . getString ( "tblname" ). equals ( tblname )) {
99- String fldname = fcat .getString ( "fldname" );
100- int fldtype = fcat .getInt ( "type" );
101- int fldlen = fcat .getInt ( "length " );
102- int offset = fcat .getInt ("offset " );
103- offsets . put ( fldname , offset );
104- sch . addField ( fldname , fldtype , fldlen );
105- }
106- fcat . close ( );
107- return new TablePhysicalLayout ( sch , offsets , size );
94+ TableDefinition sch = new TableDefinition ();
95+ Map <String , Integer > offsets = new HashMap <String , Integer >();
96+ HeapRWRecordScan fcat = new HeapRWRecordScan (tx , "tinydb_columns" , fcatRecordValueLayout );
97+ // [tableName1, A, int, 4, offset] | [tableName1, B, varchar, 9, offset] | [tableName3, fldname, type, length offset]
98+ while ( fcat .next ()) {
99+ if ( fcat .getString ( "tblname" ). equals ( tblname )) {
100+ String fldname = fcat .getString ( "fldname " );
101+ int fldtype = fcat .getInt ("type " );
102+ int fldlen = fcat . getInt ( "length" );
103+ int offset = fcat . getInt ( "offset" );
104+ offsets . put ( fldname , offset );
105+ sch . addField ( fldname , fldtype , fldlen );
106+ }
108107 }
108+ fcat .close ();
109+ return new TablePhysicalLayout (sch , offsets , size );
110+ }
109111}
0 commit comments