class ProjectPlan(Plan): """ The Plan class corresponding to the project relational algebra operator. """ def __init__(self, p: Plan, fieldlist: list): self._schema = Schema() self._p = p for fldname in fieldlist: self._schema.add(fldname, p.schema()) def open(self): """ Creates a project scan for this query. """ s = self._p.open() return ProjectScan(s, self._schema.fields()) def blocks_accessed(self): """ Estimates the number of block accesses in the projection, which is the same as in the underlying query. """ return self._p.blocks_accessed() def records_output(self): """ Estimates the number of output records in the projection, which is the same as in the underlying query. """ return self._p.records_output() def distinct_values(self, fldname): """ Estimates the number of distinct field values in the projection, which is the same as in the underlying query. """ return self._p.distinct_values(fldname) def schema(self): """ Returns the schema of the projection, which is taken from the field list. """ return self._schema
def __init__(self, idxname, leafsch, tx): """ Opens a B-tree index for the specified index. The method determines the appropriate files for the leaf and directory records, creating them if they did not exist. :param idxname: the name of the index :param leafsch: the schema of the leaf index records :param tx: the calling transaction """ assert isinstance(leafsch, Schema) assert isinstance(tx, Transaction) self._tx = tx self._leaf = None # deal with the leaves leaftbl = idxname + "leaf" self._leaf_ti = TableInfo(leaftbl, leafsch) if tx.size(self._leaf_ti.file_name() == 0): tx.append(self._leaf_ti.file_name(), BTPageFormatter(self._leaf_ti, -1)) # deal with the directory dirsch = Schema() dirsch.add("block", leafsch) dirsch.add("dataval", leafsch) dirtbl = idxname + "dir" self._dir_ti = TableInfo(dirtbl, dirsch) self._rootblk = Block(self._dir_ti.file_name(), 0) if tx.size(self._dir_ti.file_name() == 0): # create new root block tx.append(self._dir_ti.file_name(), BTPageFormatter(self._dir_ti, 0)) page = BTreePage(self._rootblk, self._dir_ti, tx) if page.get_num_recs() == 0: # insert initial directory entry fldtype = dirsch.type("dataval") minval = IntConstant(-sys.maxsize) if fldtype == INTEGER else StringConstant("") page.insert_dir(0, minval, 0) page.close()