From e0e098a17830c331f812acb6e7e15ee8b8363963 Mon Sep 17 00:00:00 2001 From: d-kunin Date: Mon, 5 Aug 2013 12:47:24 +0300 Subject: [PATCH] [core] Storage.hpp as facade for core. --- storage/article.hpp | 6 ------ storage/article_info.hpp | 28 ++++++++++++++++++++++++ storage/article_info_storage.cpp | 25 +++++++++++++++++++++ storage/article_info_storage.hpp | 18 ++++++++++++++++ storage/article_storage.cpp | 10 --------- storage/article_storage.hpp | 30 -------------------------- storage/index_storage.cpp | 15 +++++++++++++ storage/index_storage.hpp | 21 ++++++++++++++++++ storage/storage.cpp | 21 ++++++++++++++++++ storage/storage.hpp | 21 ++++++++++++++++++ storage/storage.pro | 13 +++++++---- storage/storage_common.hpp | 9 ++++++++ storage/tests/article_storage_test.cpp | 15 ------------- storage/tests/storage_test.cpp | 22 +++++++++++++++++++ 14 files changed, 189 insertions(+), 65 deletions(-) delete mode 100644 storage/article.hpp create mode 100644 storage/article_info.hpp create mode 100644 storage/article_info_storage.cpp create mode 100644 storage/article_info_storage.hpp delete mode 100644 storage/article_storage.cpp delete mode 100644 storage/article_storage.hpp create mode 100644 storage/index_storage.cpp create mode 100644 storage/index_storage.hpp create mode 100644 storage/storage.cpp create mode 100644 storage/storage.hpp create mode 100644 storage/storage_common.hpp delete mode 100644 storage/tests/article_storage_test.cpp create mode 100644 storage/tests/storage_test.cpp diff --git a/storage/article.hpp b/storage/article.hpp deleted file mode 100644 index 034f74b..0000000 --- a/storage/article.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -class Article -{ - -}; diff --git a/storage/article_info.hpp b/storage/article_info.hpp new file mode 100644 index 0000000..419dbb7 --- /dev/null +++ b/storage/article_info.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "storage_common.hpp" +#include + +struct ArticleInfo +{ + ArticleInfo() {} + ArticleInfo(string const & url, string const & title, string const & thumbnailUrl) + : m_url(url), m_title(title), m_thumbnailUrl(thumbnailUrl) {} + + string m_url; + string m_title; + string m_thumbnailUrl; +}; + +inline bool operator == (ArticleInfo const & a1, ArticleInfo const & a2) +{ + return a1.m_url == a2.m_url && a1.m_title == a2.m_title && a1.m_thumbnailUrl == a2.m_thumbnailUrl; +} + +// It's important that PrintTo() is defined in the SAME +// namespace that defines Bar. C++'s look-up rules rely on that. +inline void PrintTo(ArticleInfo const & artInfo, ::std::ostream* os) { + *os << "ArticleInfi {" << artInfo.m_url << ", " + << artInfo.m_title << ", " + << artInfo.m_thumbnailUrl << "}"; +} diff --git a/storage/article_info_storage.cpp b/storage/article_info_storage.cpp new file mode 100644 index 0000000..2005101 --- /dev/null +++ b/storage/article_info_storage.cpp @@ -0,0 +1,25 @@ +#include "article_info_storage.hpp" + +bool ArticleInfoStorageMock::GetArticleInfoById(ArticleInfo & out, ArticleInfoId const & id) +{ + if (id > 2) + return false; + + out.m_thumbnailUrl = "stub_url.png"; + switch (id) + { + case 0: + out.m_url = "London"; + out.m_title = "London"; + return true; + case 1: + out.m_url = "Lancaster"; + out.m_title = "Lancaster"; + return true; + case 2: + out.m_url = "Great_Britain"; + out.m_title = "Great Britain"; + return true; + } + return false; +} diff --git a/storage/article_info_storage.hpp b/storage/article_info_storage.hpp new file mode 100644 index 0000000..ee6f0d6 --- /dev/null +++ b/storage/article_info_storage.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "../std/string.hpp" +#include "article_info.hpp" +#include "storage_common.hpp" + +class ArticleInfoStorage +{ +public: + virtual ~ArticleInfoStorage() {} + virtual bool GetArticleInfoById(ArticleInfo & out, ArticleInfoId const & id) = 0; +}; + +class ArticleInfoStorageMock: public ArticleInfoStorage +{ +public: + virtual bool GetArticleInfoById(ArticleInfo & out, ArticleInfoId const & id); +}; diff --git a/storage/article_storage.cpp b/storage/article_storage.cpp deleted file mode 100644 index 948dcc0..0000000 --- a/storage/article_storage.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "article_storage.hpp" - -bool ArticleStorageMock::GetArticleById(string const & id, Article & out) const -{ - ArticleMap::const_iterator it = m_articles.find(id); - if (it == m_articles.end()) - return false; - out = it->second; - return true; -} diff --git a/storage/article_storage.hpp b/storage/article_storage.hpp deleted file mode 100644 index d76c86e..0000000 --- a/storage/article_storage.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include "article.hpp" - -#include "../std/map.hpp" -#include "../std/string.hpp" - -class ArticleStorage -{ -public: - virtual ~ArticleStorage() {} - virtual bool GetArticleById(string const & id, Article & out) const = 0; -}; - -class ArticleStorageMock : public ArticleStorage -{ -public: - ArticleStorageMock(map articles) : m_articles(articles) {} - virtual bool GetArticleById(string const & id, Article & out) const; -private: - typedef map ArticleMap; - ArticleMap m_articles; -}; - -class ArticleStorageTable : public ArticleStorage -{ -public: - virtual bool GetArticleById(string const & id, Article & out) const; -}; - diff --git a/storage/index_storage.cpp b/storage/index_storage.cpp new file mode 100644 index 0000000..62fb803 --- /dev/null +++ b/storage/index_storage.cpp @@ -0,0 +1,15 @@ +#include "index_storage.hpp" + +void IndexStorageMock::QueryArticleInfos(vector & out, string const & prefix, + double /*lat*/, double /*lon*/) const +{ + out.clear(); + if (prefix.empty()) + for (int i = 0; i < 3; ++i) + out.push_back(i); + else if (prefix == "L") + for (int i = 0; i < 2; ++i) + out.push_back(i); + else if (prefix.size() <= 6 && string("London").substr(prefix.size()) == prefix) + out.push_back(1); +} diff --git a/storage/index_storage.hpp b/storage/index_storage.hpp new file mode 100644 index 0000000..dcea3cb --- /dev/null +++ b/storage/index_storage.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "article_info.hpp" + +#include "../std/vector.hpp" +#include "../std/string.hpp" + +class IndexStorage +{ +public: + virtual ~IndexStorage() {} + virtual void QueryArticleInfos(vector & out, string const & prefix, + double lat = INVALID_LAT, double lon = INVALID_LON) const = 0; +}; + +class IndexStorageMock : public IndexStorage +{ +public: + virtual void QueryArticleInfos(vector & out, string const & prefix, + double lat = INVALID_LAT, double lon = INVALID_LON) const; +}; diff --git a/storage/storage.cpp b/storage/storage.cpp new file mode 100644 index 0000000..7ecfd6d --- /dev/null +++ b/storage/storage.cpp @@ -0,0 +1,21 @@ +#include "storage.hpp" +#include "../std/vector.hpp" + + +Storage::Storage(ArticleInfoStorage * artInfoStorage, IndexStorage * indexStorage) +{ + m_articleInfoStorage.reset(artInfoStorage); + m_indexStorage.reset(indexStorage); +} + +void Storage::QueryArticleInfos(vector & out, string const & prefix, + double lat, double lon) const +{ + vector ids; + m_indexStorage->QueryArticleInfos(ids, prefix, lat, lon); + out.clear(); + out.resize(ids.size()); + for (size_t i = 0; i < ids.size(); ++i) + m_articleInfoStorage->GetArticleInfoById(out[i], ids[i]); +} + diff --git a/storage/storage.hpp b/storage/storage.hpp new file mode 100644 index 0000000..cc598b7 --- /dev/null +++ b/storage/storage.hpp @@ -0,0 +1,21 @@ +# pragma once + +#include "article_info_storage.hpp" +#include "index_storage.hpp" + +#include "../std/scoped_ptr.hpp" +#include "../std/vector.hpp" + +class Storage +{ +public: + /// @note takes ownership of @link(ArticleInfoStorage) and @link(IndexStorage) + Storage(ArticleInfoStorage * artInfoStorage, IndexStorage * indexStorage); + + void QueryArticleInfos(vector & out, string const & prefix, + double lat = INVALID_LAT, double lon = INVALID_LON) const; + +private: + scoped_ptr m_articleInfoStorage; + scoped_ptr m_indexStorage; +}; diff --git a/storage/storage.pro b/storage/storage.pro index 6b79829..c9e658f 100644 --- a/storage/storage.pro +++ b/storage/storage.pro @@ -6,14 +6,19 @@ CONFIG -= app_bundle INCLUDEPATH += ../3rdparty/boost ../3rdparty/googletest/include HEADERS += \ - article.hpp \ - article_storage.hpp \ + storage.hpp \ + article_info_storage.hpp \ + article_info.hpp \ + index_storage.hpp \ + storage_common.hpp \ SOURCES += \ - article_storage.cpp + article_info_storage.cpp \ + tests/storage_test.cpp \ + index_storage.cpp \ + storage.cpp \ # unit tests SOURCES += \ ../3rdparty/googletest/src/gtest-all.cc \ ../3rdparty/googletest/src/gtest_main.cc \ - tests/article_storage_test.cpp \ diff --git a/storage/storage_common.hpp b/storage/storage_common.hpp new file mode 100644 index 0000000..4e35604 --- /dev/null +++ b/storage/storage_common.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "../std/string.hpp" +#include "../std/stdint.hpp" + +typedef uint32_t ArticleInfoId; + +#define INVALID_LAT -720.0 +#define INVALID_LON -720.0 diff --git a/storage/tests/article_storage_test.cpp b/storage/tests/article_storage_test.cpp deleted file mode 100644 index e06971e..0000000 --- a/storage/tests/article_storage_test.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "../std/utility.hpp" - -#include "../article_storage.hpp" -#include "../std/scoped_ptr.hpp" - -TEST(ArticleStorageMock, ReturnsIfAnArticleExists) -{ - map articles; - articles.insert(make_pair("SomeId", Article())); - scoped_ptr storage(new ArticleStorageMock(articles)); - Article article; - EXPECT_TRUE(storage->GetArticleById("SomeId", article)); - EXPECT_FALSE(storage->GetArticleById("SomeIdThatDoesNotExist", article)); -} diff --git a/storage/tests/storage_test.cpp b/storage/tests/storage_test.cpp new file mode 100644 index 0000000..a6a5b1f --- /dev/null +++ b/storage/tests/storage_test.cpp @@ -0,0 +1,22 @@ +#include + +#include "storage.hpp" +#include "article_info_storage.hpp" +#include "index_storage.hpp" + +TEST(Storage, Smoke) +{ + Storage storage(new ArticleInfoStorageMock(), new IndexStorageMock()); + + vector artInfos; + storage.QueryArticleInfos(artInfos, ""); + + ArticleInfo expected[] = + { + ArticleInfo("London", "London", "stub_url.png"), + ArticleInfo("Lancaster", "Lancaster", "stub_url.png"), + ArticleInfo("Great_Britain", "Great Britain", "stub_url.png"), + }; + + EXPECT_EQ(vector(&expected[0], &expected[0] + 3), artInfos); +}