database: Add funcions for database iteration

A database may have invalid rows.  If this is the case, then the next()
function needs to return the id of the next valid row.  I'm aware that
this could get horribly inefficient on large DBs with many invalid rows.
I don't expect people to delete large chunks of music all at once, but a
defragment tool is on my "todo" list for a future Ocarina version.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-08-11 20:18:51 -04:00 committed by Anna Schumaker
parent b3f5363445
commit 50a7c3dae6
5 changed files with 126 additions and 17 deletions

View File

@ -52,9 +52,15 @@ Database: (lib/database.cpp)
Database::Database(filename);
void load();
void save();
unsigned int insert(T);
void delete(unsigned int);
const unsigned int &size();
unsigned int size();
unsigned int num_rows();
unsigned int first();
unsigned int last();
unsigned int next();
T &operator[](unsigned int);
};
@ -75,14 +81,29 @@ Database: (lib/database.cpp)
template <class T>
unsigned int Database : insert(T &);
Adds a new item to the db, returns the id of the item
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)
Mark db[index] as invalid (quick deletion).
unsigned Database : size();
Returns number of valid rows in the database
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]
Return a reference to db[index].

View File

@ -8,6 +8,15 @@
#include <vector>
class DatabaseEntry {
public:
bool valid;
DatabaseEntry();
};
template <class T>
class Database {
private:
@ -20,7 +29,13 @@ public:
~Database();
unsigned int insert(T);
const unsigned int &size();
unsigned int size();
unsigned int num_rows();
unsigned int first();
unsigned int last();
unsigned int next(unsigned int);
T &operator[](unsigned int);
};
#include "database.hpp"

View File

@ -28,9 +28,54 @@ unsigned int Database<T> :: insert(T val)
}
template <class T>
const unsigned int &Database<T> :: size()
unsigned int Database<T> :: size()
{
return _size;
}
template <class T>
unsigned int Database<T> :: num_rows()
{
return db.size();
}
template <class T>
unsigned int Database<T> :: first()
{
for (unsigned int i = 0; i < db.size(); i++) {
if (db[i].valid == true)
return i;
}
return db.size();
}
template <class T>
unsigned int Database<T> :: last()
{
for (unsigned int i = db.size() - 1; i >= 0; i--) {
if (db[i].valid == true)
return i;
}
return db.size();
}
template <class T>
unsigned int Database<T> :: next(unsigned int id)
{
for (unsigned int i = id + 1; i < db.size(); i++) {
if (db[i].valid == true)
return i;
}
return db.size();
}
template <class T>
T &Database<T> :: operator[](unsigned int id)
{
return db[id];
}
#endif /* OCARINA_DATABASE_HPP */

View File

@ -3,3 +3,7 @@
*/
#include <database.h>
DatabaseEntry :: DatabaseEntry()
: valid(false)
{
}

View File

@ -4,24 +4,48 @@
#include <database.h>
#include <print.h>
void test_insertion(Database<int> &db)
class DBTest : public DatabaseEntry {
public:
unsigned int value;
DBTest(unsigned int);
};
DBTest :: DBTest(unsigned int val)
{
unsigned int id;
value = val;
valid = true;
}
print("\n");
for (unsigned int i = 1; i <= 10; i++) {
id = db.insert(i);
print("db[%u] = %u\n", id, i);
}
print("Database size: %u\n", db.size());
void print_db(Database<DBTest> &db)
{
print("\nDatabase size: %u\n", db.size());
print("Num rows: %u\n", db.num_rows());
print("First: %u\n", db.first());
print("Last: %u\n", db.last());
for (unsigned int i = db.first(); i != db.num_rows(); i = db.next(i))
print("db[%u] = %u\n", i, db[i].value);
}
void test_insertion(Database<DBTest> &db)
{
for (unsigned int i = 1; i <= 10; i++)
db.insert(DBTest(i));
print_db(db);
}
void test_deletion(Database<DBTest> &db)
{
}
int main(int argc, char **argv)
{
Database<int> db("test.db");
Database<DBTest> db("test.db");
test_insertion(db);
test_deletion(db);
return 0;
}