ocarina/design/database.txt

110 lines
3.1 KiB
Plaintext

== Files ==
ocarina/include/
database.h
database.hpp
ocarina/lib/
database.cpp
== Depends ==
file
Database: (lib/database.cpp)
Ocarina 5.x created a different save file format for each type of
data that needed to be stored (preferences, library paths, playlists).
I intend to unify everything into a generic file format that can be
accessed through a generic database interface. The database code will
be in charge of printing the "valid" bit for each DatabaseEntry so that
child classes do not need to call into the parent class. If valid ==
true, the DatabaseEntry will be streamed out followed by a newline. If
valid == false the database will print the next entry in the vector.
Modules should inherit from the DatabasEntry class and implement their
own read() and write() functions. The "valid" field will be stored
before these functions are called, and the entry will be skipped if
valid is set to false.
The Database class is a templated class, so code could potentially
get messy. Normal class declarations can still exist in the file
include/database.h and member functions can be written in the file
include/database.hpp, which will be included by database.h. Any
function not relying on a template can be written in lib/database.cpp.
- DatabaseEntry:
class DatabaseEntry { /* let database modify valid flag */
private:
bool valid;
public:
virtual File &operator>>(File &) = 0;
virtual File &operator<<(File &) = 0;
friend class Database;
};
File << <CHILD_CLASS_DATA>
- Database:
template <class T>
class Database {
private:
unsigned int _size; /* Number of valid rows */
File filename;
vector<T> db;
public:
Database::Database(filename);
void load();
void save();
unsigned int insert(T);
void delete(unsigned int);
unsigned int size();
unsigned int num_rows();
unsigned int first();
unsigned int last();
unsigned int next();
T &operator[](unsigned int);
};
File << db.size() << endl
File << INDEX_0 << db[INDEX_0].valid << db[INDEX_0] << endl;
File << Index_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
...
- API:
Database : Database(filename);
Initializes database to use ~/.ocarina{-debug}/filename.
void Database : load();
Reads data from file.
void Database : save();
Saves data to file.
template <class T>
unsigned int Database : insert(T &);
Adds a new item to the db, returns the id of the item.
void Database : delete(unsigned int index);
Mark db[index] as invalid (quick deletion).
unsigned int Database : size();
Returns number of valid rows in the database.
unsigned int Database : num_rows();
Return db.size().
unsigned int Database : first();
Return the id to the first valid row or return db.size()
if there are no valid rows.
unsigned int Database : last();
Return the id of the last valid row or return db.size()
if there are no valid rows.
unsigned int Database : next(unsigned int &id)
Return the id of the next valid row or return db.size()
if there are no remaining valid rows.
template <class T>
T &Database : operator[unsigned int index]
Return a reference to db[index].