package database;
import cell.*;
/**
This class represents a table. Its contents are automatically loaded from disk.
When a part of the table is modified, it is immediately written back to disk.
The table does not have fixed data types, but can return both floating point values and character strings.
<p>
File Format:
I have created my own file format. It is Unicode and Random Access. It is basically, padded CSV.
<p>
File Location: Please consult {@link database.Tables} for directory information.
*/
public final class DB
{
CellBase c ;
/**
This loads a file from disk, and into memory, where it can be accessed by you.
@param tableName
Please consult {@link database.Tables} for more information.
@throws IllegalArgumentException If your table's name is unacceptable.
@throws IOException Never. If there is such an exception, the program just calls System.exit() pronto.
*/
public DB(String tableName)
{
c = new CellFinal(tableName);
}
/**
Retrieve a count of added records.
@return The number of populated rows in this table.
*/
public int length()
{
return c.max()+1;
}
/**
Apply a new value to this particular cell.
<p>
Then write this value to disk, before returning.
@param x Column >= 0
@param y Row >= 0
@param value The new value, in floating point format.
*/
public void setFloat(int x, int y, float value)
{
setString(x, y, ""+value);
}
/**
Modify the value of a particular cell.
<p>
The data is then writtn to disk, straight away.
@param x The column in question. You can set 0, and then 60, whatever.
@param y Row offset.
@param value The new value you want it to be, expressed as a character string.
*/
public void setString(int x, int y, String value)
{
c.so((byte)x, (short)y, value);
}
/**
Return the contents of this cell, as a float.
@param x Column.
@param y row >= 0
@return The last value set to this cell.
If the contents are not a float, or if ese coordinates are out of range, zero is returned.
*/
public float getFloat(int x, int y)
{
return c.af((byte)x, (short)y);
}
/**
Get the data for a cell.
@param x Column offset >= 0
@param y Row offset.
@returns The data in this cell.
If the coordinates are out of range, "" is returned.
*/
public String getString(int x, int y)
{
return ""+c.go((byte)x, (short)y);
}
/**
Delete a row, with one caveat:
<p>
The row is not deleted on disk, only in memory.
So you would have to delete this row every time you load from file.
This means that you can only use this method within the constructor of your class.
@param y The offset of the row. Zero-based.
@throws Exception If you try to delete a non-existent row.
*/
public void delete(int y)
{
c.ramDelete((short)y);
}
/**
Appends a new row to the bottom of this table.
<p>
It is written to disk immediately.
@param values An array of either Float or String objects, although probably anything will work.
These will be the contents of the new row, on disk, for example:
values[0] == getFloat(0, length()-1)
*/
public void add(Object[] values)
{
int y = length();
for (int x = 0; x < values.length; x++)
setString(x, y, values[x]+"");
}
/**
Get the value of this cell.
@param x Column >= 0
@param y Row >= 0
@return the value of this cell, integrated into a long-type variable. If the cell has not yet been initialized, zero is returned.
*/
public long getLong(int x, int y)
{
return c.al((byte)x, (short)y);
}
/**
Set the value of this cell.
@param x Column.
@param y Row.
@param value The new value, integrated into a longparameter of -type.
*/
public void setLong(int x, int y, long value)
{
setString(x, y, ""+value);
}
/**
Return the value of this cell.
<p>
The cell must contain a value in accordance with {@link System.currentTimeMillis
}
@x Column >= 0
@y Row >= 0
@return The value of this cell, formatted into a nice human-readable bit of text. If the cell has not yet been initialized, the string "Timeless." is returned. That string is also returned if the contents of the cell are nonsensical.
*/
public String getDateTime(int x, int y)
{
return c.ad((byte)x, (short)y);
}
/** Convert an arbitrary long value into a human readable string, representing a date.
<p>
This has no relevance to any table or database, that is why it is declared static. It is just a utility function.
@param millis The result of {@link System.currentTimeMillis} at some point, possibly in the past.
@return A humand readable date and time, for us in the current (local) timezone.
*/
public static String getDateTime(long millis)
{
return DTFormatter.shift(millis);
}
/**
Test case.
*/
public static void main(String[] dummy)
{
String name = "TST";
DB db = new DB(name);
float val = (float)Math.random();
db.setFloat(0, 0, val);
assert (new DB(name).getFloat(0, 0) == val);
}
}