Due: 12 September 2021, 11:59pm EDT (No late submissions will be accepted)
Points: 100 public
We will be implementing a simple database using Ruby data structures to store the data. A database can contain an arbitrary number of tables. Each table will contain tuples of size n, where n is the number of columns in the table. Through a series of discussion exercises each week, we will improve upon our simple database. This week we will create a database representation that will implement some basic functionality. The class Tuple
represents and entry in a table. The class Table
represents a collection of tuples.
You will submit this project to Gradescope. You may only submit the disc1.rb file. To test locally, run ruby test/public/public.rb
.
A Tuple
represents a single entry, in a table. The methods below will be implemented in the Tuple
class in disc1.rb.
- Type:
(Array) -> _
- Description: Given an array of values for the tuple, store them in the
Tuple
object in any way you would like. You should perform any initialization steps for theTuple
instance here. The return value of this function does not matter. - Examples:
t = Tuple.new(["a", 1, "b", 2]) t = Tuple.new([]) # Tuples may be empty
- Type:
() -> Integer
- Description: Return the number of entries in the
Tuple
. - Examples:
t = Tuple.new(["a", 1, "b", 2]) t.getSize() # Returns 4 u = Tuple.new([]) u.getSize() # Returns 0
- Type:
(Integer) -> Object
- Description: Return the data at a particular index of a
Tuple
(indexed starting at 0). If the provided index exceeds the largest index in the tuple, returnnil
. - Assumptions:
index
is non-negative. - Examples:
t = Tuple.new(["a", 1, "b", 2]) t.getData(0) # Returns "a" t.getData(4) # Returns nil
- Type:
(Integer) -> Integer
- Description: Return the number of
Tuple
s of sizen
that have ever been created. Hint: you should use a static variable to keep track of this. - Examples:
Tuple.getNumTuples(3) # Returns 0 t = Tuple.new(["a", 1, "b"]) t = Tuple.new(["a", 1, "b"]) Tuple.getNumTuples(3) # Returns 2 t = Tuple.new([3]) Tuple.getNumTuples(3) # Returns 2
A Table
represents a collection of tuples. The methods below will be implemented in the Table
class in disc1.rb.
- Type:
(Array) -> _
- Description: Given an array of column names for the
Table
, store them in the object in any way you would like. You should perform any initialization steps for theTable
instance here. The return value of this function does not matter. - Assumptions: The elements in
column_names
will be unique. - Examples:
t = Table.new(["c0", "c1", "c2"]) t = Table.new([])
- Type:
(Tuple) -> boolean
- Description: Insert a
Tuple
into theTable
. Note that the number of entries in theTuple
must match the number of columns in theTable
. If this is not the case, make no changes to theTable
and returnfalse
. If the sizes match, insert theTuple
and returntrue
. - Examples:
table = Table.new(["a", "b"]) x = Tuple.new([0, 1]) y = Tuple.new([3, "y"]) z = Tuple.new([1, 2, 3]) table.insertTuple(x) # Returns true table.insertTuple(y) # Returns true table.insertTuple(z) # Returns false (sizes do not match)
- Type:
() -> Integer
- Description: Return the number of
Tuple
s in the table. - Examples:
table = Table.new(["a", "b"]) table.getSize() # Returns 0 x = Tuple.new([0, 1]) table.insertTuple(x) table.getSize() # Returns 1
- Type:
(Array) -> Table
- Description: Return a new
Table
instance which is the same as the existing one, but only with the requested columns. Hint: to find the index of an element in an array, usearr.index(element)
. This function should NOT modify the current table; rather, create a new one. - Assumptions:
column_names
does not include any column names which are not present in the table (i.e. all queries are valid). - Examples:
table = Table.new(["a", "b", "c"]) x = Tuple.new([0, 1, 2]) y = Tuple.new(["x", "y", "z"]) table.insertTuple(x) table.insertTuple(y) # The table currently looks like this: # a | b | c # -----+-----+----- # 0 | 1 | 2 # "x" | "y" | "z" table2 = table.selectTuples(["a", "c"]) # The call above should return a NEW table that looks like this: # a | c # -----+----- # 0 | 2 # "x" | "z"
- Type:
() -> Array
- Description: Return an array of the
Tuple
s present in theTable
. - Examples:
table = Table.new(["a", "b", "c"]) table.getTuples() # Returns [] x = Tuple.new([0, 1, 2]) table.insertTuple(x) # Returns true y = Tuple.new(["x", "y", "z"]) table.insertTuple(y) # Returns true table.getTuples() # Returns [[0, 1, 2], ["x", "y", "z"]]