diff --git a/design/index.txt b/design/index.txt index 3bc007d8..20f34a18 100644 --- a/design/index.txt +++ b/design/index.txt @@ -30,6 +30,7 @@ Index: (lib/index.cpp) const set &operator[](string); }; + File << keys.size() << endl; File << key << endl; File << map[key].size() << int_0 << int_1 << ... << int_n << endl; diff --git a/include/index.h b/include/index.h index 52047ad1..39c657b4 100644 --- a/include/index.h +++ b/include/index.h @@ -18,6 +18,8 @@ private: public: Index(std::string); + void save(); + void load(); void insert(std::string, unsigned int); void remove(std::string); diff --git a/lib/index.cpp b/lib/index.cpp index 15ab0ffd..5d844fbc 100644 --- a/lib/index.cpp +++ b/lib/index.cpp @@ -10,6 +10,48 @@ Index :: Index(std::string filepath) } +void Index :: save() +{ + std::set::iterator k_it; + std::set::iterator u_it; + + if (file.open(OPEN_WRITE) == false) + return; + + file << keys.size(); + for (k_it = keys.begin(); k_it != keys.end(); k_it++) { + file << *k_it << std::endl; + file << index[*k_it].size() << " "; + + for (u_it = index[*k_it].begin(); u_it != index[*k_it].end(); u_it++) + file << *u_it << " "; + + file << std::endl; + } + + file.close(); +} + +void Index :: load() +{ + std::string key; + unsigned int num_keys, num_values, value; + + if (file.open(OPEN_READ) == false) + return; + + file >> num_keys; + for (unsigned int i = 0; i < num_keys; i++) { + file >> key >> num_values; + for (unsigned int j = 0; j < num_values; j++) { + file >> value; + insert(key, value); + } + } + + file.close(); +} + void Index :: insert(std::string key, unsigned int value) { if (index.find(key) == index.end()) { diff --git a/tests/index/index.cpp b/tests/index/index.cpp index c54be50a..0b3f0f8f 100644 --- a/tests/index/index.cpp +++ b/tests/index/index.cpp @@ -113,10 +113,37 @@ void test_2() print_index(index); } +/* + * Save the database to disk + */ +void test_3() +{ + print("Test 3\n"); + Index index("test.idx"); + + populate_index(index); + remove_values(index); + index.save(); +} + +/* + * Reload the database from disk + */ +void test_4() +{ + print("Test 4\n"); + Index index("test.idx"); + + index.load(); + print_index(index); +} + int main(int argc, char **argv) { test_0(); test_1(); test_2(); + test_3(); + test_4(); return 0; } diff --git a/tests/index/index.good b/tests/index/index.good index 5da5317d..db47f157 100644 --- a/tests/index/index.good +++ b/tests/index/index.good @@ -73,3 +73,32 @@ index[x] = {1 2 3 4 5 6 7 8 9} index[y] = {2 3 4 5 6 7 8 9} index[z] = {3 4 5 6 7 8 9} +Test 3 +Test 4 +=== Printing index === +Found keys: a b c d e f g h i j l m n o p q r s t u w x y z +index[a] = {0 1 2 3 4 5 6 7 8 9} +index[b] = {1 2 3 4 5 6 7 8 9} +index[c] = {2 3 4 5 6 7 8 9} +index[d] = {3 4 5 6 7 8 9} +index[e] = {4 5 6 7 8 9} +index[f] = {5 6 7 8 9} +index[g] = {6 7 8 9} +index[h] = {7 8 9} +index[i] = {8 9} +index[j] = {9} +index[l] = {0 1 2 3 4 5 6 7 8 9} +index[m] = {1 2 3 4 5 6 7 8 9} +index[n] = {2 3 4 5 6 7 8 9} +index[o] = {3 4 5 6 7 8 9} +index[p] = {4 5 6 7 8 9} +index[q] = {5 6 7 8 9} +index[r] = {6 7 8 9} +index[s] = {7 8 9} +index[t] = {8 9} +index[u] = {9} +index[w] = {0 1 2 3 4 5 6 7 8 9} +index[x] = {1 2 3 4 5 6 7 8 9} +index[y] = {2 3 4 5 6 7 8 9} +index[z] = {3 4 5 6 7 8 9} +