Skip to content

Commit

Permalink
Final Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunsk committed Dec 17, 2022
1 parent c3f4d76 commit bf35a46
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 170 deletions.
4 changes: 2 additions & 2 deletions docs/diagrams/D_CalciteQueryEngine.puml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ package MetadataMgr_TableCatalog
package RW_HeapRecordScan


CalciteEnumerator <...> MetadataMgr_TableCatalog
CalciteEnumerator <...> RW_HeapRecordScan
CalciteEnumerator <..> MetadataMgr_TableCatalog
CalciteEnumerator <..> RW_HeapRecordScan
CalciteEnumerator ---> CalciteTable

MetadataMgr_TableCatalog -> RW_HeapRecordScan
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.utdallas.tiny_db.server.b_query_engine.common.catalog;

import edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.StatMgr;
import edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.StatMgr;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.index.IndexInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.index.IndexMgr;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TableMgr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.sql.Types.INTEGER;

import edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.domain.StatInfo;
import edu.utdallas.tiny_db.server.d_storage_engine.common.transaction.Transaction;
import edu.utdallas.tiny_db.server.d_storage_engine.RWIndexScan;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TablePhysicalLayout;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
package edu.utdallas.tiny_db.server.b_query_engine.common.catalog.index;

import edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.StatMgr;
import edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.StatMgr;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TableDefinition;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TableMgr;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TablePhysicalLayout;
import edu.utdallas.tiny_db.server.d_storage_engine.common.transaction.Transaction;
import edu.utdallas.tiny_db.server.d_storage_engine.impl.data.heap.HeapRWRecordScan;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TablePhysicalLayout;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TableDefinition;

import java.util.HashMap;
import java.util.Map;

/**
* The index manager.
* The index manager has similar functionality to the table manager.
* The index manager. The index manager has similar functionality to the table manager.
*
* @author Edward Sciore
*/
public class IndexMgr {
private TablePhysicalLayout recordValueLayout;
private TableMgr tblmgr;
private StatMgr statmgr;

/**
* Create the index manager.
* This constructor is called during system startup.
* If the database is new, then the <i>idxcat</i> table is created.
*
* @param isnew indicates whether this is a new database
* @param tx the system startup transaction
*/
public IndexMgr(boolean isnew, TableMgr tblmgr, StatMgr statmgr, Transaction tx) {
if (isnew) {
TableDefinition sch = new TableDefinition();
sch.addStringField("indexname", TableMgr.MAX_NAME);
sch.addStringField("tablename", TableMgr.MAX_NAME);
sch.addStringField("fieldname", TableMgr.MAX_NAME);
tblmgr.createTable("idxcat", sch, tx);
}
this.tblmgr = tblmgr;
this.statmgr = statmgr;
recordValueLayout = tblmgr.getLayout("idxcat", tx);
}
private TablePhysicalLayout recordValueLayout;
private TableMgr tblmgr;
private StatMgr statmgr;

/**
* Create the index manager. This constructor is called during system startup. If the database is
* new, then the <i>idxcat</i> table is created.
*
* @param isnew indicates whether this is a new database
* @param tx the system startup transaction
*/
public IndexMgr(boolean isnew, TableMgr tblmgr, StatMgr statmgr, Transaction tx) {
if (isnew) {
TableDefinition sch = new TableDefinition();
sch.addStringField("indexname", TableMgr.MAX_NAME);
sch.addStringField("tablename", TableMgr.MAX_NAME);
sch.addStringField("fieldname", TableMgr.MAX_NAME);

/**
* Create an index of the specified type for the specified field.
* A unique ID is assigned to this index, and its information
* is stored in the idxcat table.
*
* @param idxname the name of the index
* @param tblname the name of the indexed table
* @param fldname the name of the indexed field
* @param tx the calling transaction
*/
public void createIndex(String idxname, String tblname, String fldname, Transaction tx) {
HeapRWRecordScan ts = new HeapRWRecordScan(tx, "idxcat", recordValueLayout);
ts.seekToInsertStart();
ts.setString("indexname", idxname);
ts.setString("tablename", tblname);
ts.setString("fieldname", fldname);
ts.close();
tblmgr.createTable("idxcat", sch, tx);
}
this.tblmgr = tblmgr;
this.statmgr = statmgr;
recordValueLayout = tblmgr.getLayout("idxcat", tx);
}

/**
* Create an index of the specified type for the specified field. A unique ID is assigned to this
* index, and its information is stored in the idxcat table.
*
* @param idxname the name of the index
* @param tblname the name of the indexed table
* @param fldname the name of the indexed field
* @param tx the calling transaction
*/
public void createIndex(String idxname, String tblname, String fldname, Transaction tx) {
HeapRWRecordScan ts = new HeapRWRecordScan(tx, "idxcat", recordValueLayout);
ts.seekToInsertStart();
ts.setString("indexname", idxname);
ts.setString("tablename", tblname);
ts.setString("fieldname", fldname);
ts.close();
}

/**
* Return a map containing the index info for all indexes on the specified table.
*
* @param tblname the name of the table
* @param tx the calling transaction
* @return a map of IndexInfo objects, keyed by their field names
*/
public Map<String, IndexInfo> getIndexInfo(String tblname, Transaction tx) {
Map<String, IndexInfo> result = new HashMap<String, IndexInfo>();
HeapRWRecordScan ts = new HeapRWRecordScan(tx, "idxcat", recordValueLayout);
while (ts.next()) {
if (ts.getString("tablename").equals(tblname)) {
String idxname = ts.getString("indexname");
String fldname = ts.getString("fieldname");
TablePhysicalLayout tblRecordValueLayout = tblmgr.getLayout(tblname, tx);
StatInfo tblsi = statmgr.getStatInfo(tblname, tblRecordValueLayout, tx);

/**
* Return a map containing the index info for all indexes
* on the specified table.
*
* @param tblname the name of the table
* @param tx the calling transaction
* @return a map of IndexInfo objects, keyed by their field names
*/
public Map<String, IndexInfo> getIndexInfo(String tblname, Transaction tx) {
Map<String, IndexInfo> result = new HashMap<String, IndexInfo>();
HeapRWRecordScan ts = new HeapRWRecordScan(tx, "idxcat", recordValueLayout);
while (ts.next()) if (ts.getString("tablename").equals(tblname)) {
String idxname = ts.getString("indexname");
String fldname = ts.getString("fieldname");
TablePhysicalLayout tblRecordValueLayout = tblmgr.getLayout(tblname, tx);
StatInfo tblsi = statmgr.getStatInfo(tblname, tblRecordValueLayout, tx);
IndexInfo ii = new IndexInfo(idxname, fldname, tblRecordValueLayout.schema(), tx, tblsi);
result.put(fldname, ii);
}
ts.close();
return result;
IndexInfo ii = new IndexInfo(idxname, fldname, tblRecordValueLayout.schema(), tx, tblsi);
result.put(fldname, ii);
}
}
ts.close();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager;
package edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats;

import edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.domain.StatInfo;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TableMgr;
import edu.utdallas.tiny_db.server.d_storage_engine.common.transaction.Transaction;
import edu.utdallas.tiny_db.server.b_query_engine.common.catalog.table.TablePhysicalLayout;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.utdallas.tiny_db.server.b_query_engine.impl.basic.b_stats_manager.domain;
package edu.utdallas.tiny_db.server.b_query_engine.common.catalog.stats.domain;

/**
* A StatInfo object holds three pieces of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,110 @@

import edu.utdallas.tiny_db.server.d_storage_engine.common.transaction.Transaction;
import edu.utdallas.tiny_db.server.d_storage_engine.impl.data.heap.HeapRWRecordScan;

import java.util.HashMap;
import java.util.Map;

/**
* The table manager.
* There are methods to create a table, save the metadata
* in the catalog, and obtain the metadata of a
* previously-created table.
* The table manager. There are methods to create a table, save the metadata in the catalog, and
* obtain the metadata of a previously-created table.
*
* @author Edward Sciore
*/
public class TableMgr {
// The max characters a tablename or fieldname can have.
public static final int MAX_NAME = 16;
private TablePhysicalLayout tcatRecordValueLayout, fcatRecordValueLayout;

/**
* Create a new catalog manager for the database system.
* If the database is new, the two catalog tables
* are created.
*
* @param isNew has the value true if the database is new
* @param tx the startup transaction
*/
public TableMgr(boolean isNew, Transaction tx) {
TableDefinition tcatTableDefinition = new TableDefinition();
tcatTableDefinition.addStringField("tblname", MAX_NAME);
tcatTableDefinition.addIntField("slotsize");
tcatRecordValueLayout = new TablePhysicalLayout(tcatTableDefinition);
// The max characters a tablename or fieldname can have.
public static final int MAX_NAME = 16;
private TablePhysicalLayout tcatRecordValueLayout, fcatRecordValueLayout;

/**
* Create a new catalog manager for the database system. If the database is new, the two catalog
* tables are created.
*
* @param isNew has the value true if the database is new
* @param tx the startup transaction
*/
public TableMgr(boolean isNew, Transaction tx) {
TableDefinition tcatTableDefinition = new TableDefinition();
tcatTableDefinition.addStringField("tblname", MAX_NAME);
tcatTableDefinition.addIntField("slotsize");
tcatRecordValueLayout = new TablePhysicalLayout(tcatTableDefinition);

TableDefinition fcatTableDefinition = new TableDefinition();
fcatTableDefinition.addStringField("tblname", MAX_NAME);
fcatTableDefinition.addStringField("fldname", MAX_NAME);
fcatTableDefinition.addIntField("type");
fcatTableDefinition.addIntField("length");
fcatTableDefinition.addIntField("offset");
fcatRecordValueLayout = new TablePhysicalLayout(fcatTableDefinition);
TableDefinition fcatTableDefinition = new TableDefinition();
fcatTableDefinition.addStringField("tblname", MAX_NAME);
fcatTableDefinition.addStringField("fldname", MAX_NAME);
fcatTableDefinition.addIntField("type");
fcatTableDefinition.addIntField("length");
fcatTableDefinition.addIntField("offset");
fcatRecordValueLayout = new TablePhysicalLayout(fcatTableDefinition);

if (isNew) {
createTable("tinydb_tables", tcatTableDefinition, tx);
createTable("tinydb_columns", fcatTableDefinition, tx);
}
if (isNew) {
createTable("tinydb_tables", tcatTableDefinition, tx);
createTable("tinydb_columns", fcatTableDefinition, tx);
}
}

/**
* Create a new table having the specified name and schema.
*
* @param tblname the name of the new table
* @param sch the table's schema
* @param tx the transaction creating the table
*/
public void createTable(String tblname, TableDefinition sch, Transaction tx) {
TablePhysicalLayout recordValueLayout = new TablePhysicalLayout(sch);
/**
* Create a new table having the specified name and schema.
*
* @param tblname the name of the new table
* @param sch the table's schema
* @param tx the transaction creating the table
*/
public void createTable(String tblname, TableDefinition sch, Transaction tx) {
TablePhysicalLayout recordValueLayout = new TablePhysicalLayout(sch);

HeapRWRecordScan tcat = new HeapRWRecordScan(tx, "tinydb_tables", tcatRecordValueLayout);
tcat.seekToInsertStart();
tcat.setString("tblname", tblname);
tcat.setInt("slotsize", recordValueLayout.slotSize());
tcat.close();
HeapRWRecordScan tcat = new HeapRWRecordScan(tx, "tinydb_tables", tcatRecordValueLayout);
tcat.seekToInsertStart();
tcat.setString("tblname", tblname);
tcat.setInt("slotsize", recordValueLayout.slotSize());
tcat.close();

HeapRWRecordScan fcat = new HeapRWRecordScan(tx, "tinydb_columns", fcatRecordValueLayout);
for (String fldname : sch.fields()) {
fcat.seekToInsertStart();
fcat.setString("tblname", tblname);
fcat.setString("fldname", fldname);
fcat.setInt("type", sch.type(fldname));
fcat.setInt("length", sch.length(fldname));
fcat.setInt("offset", recordValueLayout.offset(fldname));
}
fcat.close();
HeapRWRecordScan fcat = new HeapRWRecordScan(tx, "tinydb_columns", fcatRecordValueLayout);
for (String fldname : sch.fields()) {
fcat.seekToInsertStart();
fcat.setString("tblname", tblname);
fcat.setString("fldname", fldname);
fcat.setInt("type", sch.type(fldname));
fcat.setInt("length", sch.length(fldname));
fcat.setInt("offset", recordValueLayout.offset(fldname));
}
fcat.close();
}

/**
* Retrieve the layout of the specified table
* from the catalog.
*
* @param tblname the name of the table
* @param tx the transaction
* @return the table's stored metadata
*/
public TablePhysicalLayout getLayout(String tblname, Transaction tx) {
int size = -1;
HeapRWRecordScan tcat = new HeapRWRecordScan(tx, "tinydb_tables", tcatRecordValueLayout);
while (tcat.next()) if (tcat.getString("tblname").equals(tblname)) {
size = tcat.getInt("slotsize");
break;
}
tcat.close();
/**
* Retrieve the layout of the specified table from the catalog.
*
* @param tblname the name of the table
* @param tx the transaction
* @return the table's stored metadata
*/
public TablePhysicalLayout getLayout(String tblname, Transaction tx) {
int size = -1;
HeapRWRecordScan tcat = new HeapRWRecordScan(tx, "tinydb_tables", tcatRecordValueLayout);
// [tableName1, slotsize1] | [tableName2, slotsize2] | [tableName3, slotsize3]
while (tcat.next()) {
if (tcat.getString("tblname").equals(tblname)) {
size = tcat.getInt("slotsize");
break;
}
}
tcat.close();

TableDefinition sch = new TableDefinition();
Map<String, Integer> offsets = new HashMap<String, Integer>();
HeapRWRecordScan fcat = new HeapRWRecordScan(tx, "tinydb_columns", fcatRecordValueLayout);
while (fcat.next()) if (fcat.getString("tblname").equals(tblname)) {
String fldname = fcat.getString("fldname");
int fldtype = fcat.getInt("type");
int fldlen = fcat.getInt("length");
int offset = fcat.getInt("offset");
offsets.put(fldname, offset);
sch.addField(fldname, fldtype, fldlen);
}
fcat.close();
return new TablePhysicalLayout(sch, offsets, size);
TableDefinition sch = new TableDefinition();
Map<String, Integer> offsets = new HashMap<String, Integer>();
HeapRWRecordScan fcat = new HeapRWRecordScan(tx, "tinydb_columns", fcatRecordValueLayout);
// [tableName1, A, int, 4, offset] | [tableName1, B, varchar, 9, offset] | [tableName3, fldname, type, length offset]
while (fcat.next()) {
if (fcat.getString("tblname").equals(tblname)) {
String fldname = fcat.getString("fldname");
int fldtype = fcat.getInt("type");
int fldlen = fcat.getInt("length");
int offset = fcat.getInt("offset");
offsets.put(fldname, offset);
sch.addField(fldname, fldtype, fldlen);
}
}
fcat.close();
return new TablePhysicalLayout(sch, offsets, size);
}
}
Loading

0 comments on commit bf35a46

Please sign in to comment.