2014-10-17 10:37:13 -04:00
|
|
|
/**
|
|
|
|
* @file
|
2014-03-12 20:23:34 -04:00
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_INDEX_H
|
|
|
|
#define OCARINA_CORE_INDEX_H
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/database.h>
|
2014-03-12 20:23:34 -04:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* The IndexEntry class is used to associate a specific key with a set of
|
|
|
|
* integer identifiers. This lets us use a Database as an inverted index.
|
2014-10-17 10:37:13 -04:00
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
class IndexEntry : public DatabaseEntry {
|
|
|
|
public:
|
2014-11-04 08:21:14 -05:00
|
|
|
std::string _key; /**< The term stored by this IndexEntry. */
|
|
|
|
std::set<unsigned int> _values; /**< Integers representing strings that
|
|
|
|
contain this term. */
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-11-04 08:21:14 -05:00
|
|
|
IndexEntry(); /**< Create an empty IndexEntry. */
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Create an IndexEntry with a specific key.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param key The key associated with this IndexEntry.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
IndexEntry(const std::string &);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Access the key stored by this IndexEntry.
|
|
|
|
*
|
|
|
|
* @return IndexEntry::_key.
|
2014-10-17 10:37:13 -04:00
|
|
|
*/
|
2014-03-25 17:51:37 -04:00
|
|
|
const std::string primary_key() const;
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Add a new value to this IndexEntry.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param value The new value to add.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void insert(unsigned int);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Remove a value from this IndexEntry.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param value The value to remove.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void remove(unsigned int);
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Write an IndexEntry to file.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param file The file to use when writing data.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void write(File &);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Read an IndexEntry from file.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param file The file read from.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void read(File &);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
|
|
|
* A special Database for index access.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
class Index : public Database<IndexEntry> {
|
|
|
|
public:
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
|
|
|
* Index constructor.
|
|
|
|
* @param filepath Where to store the file on disk.
|
|
|
|
* @param autosave True if changes should automatically be saved.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
Index(const std::string &, bool);
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
|
|
|
* Add a new (key, value) pair to the index.
|
|
|
|
* @param key String to store in the index.
|
|
|
|
* @param value Integer value associated with the key.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void insert(const std::string &, unsigned int);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a (key, value) pair from the index.
|
|
|
|
* @param key Key associated with the value to be removed.
|
|
|
|
* @param value Value to remove from the index.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void remove(const std::string &, unsigned int);
|
|
|
|
};
|
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_DATABASE_H */
|