summaryrefslogtreecommitdiff
path: root/include/mbgl/storage
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-09-15 17:26:44 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-09-24 16:14:09 +0200
commitd9fc7708a2dfb6e2506a5d10d896a813557c056d (patch)
tree50b2dba9e0a8766c88f7c276a8f71742a06a1d67 /include/mbgl/storage
parent062e911c6d570a794431023f9f0cb0b02cd85667 (diff)
downloadqtlocation-mapboxgl-d9fc7708a2dfb6e2506a5d10d896a813557c056d.tar.gz
do 304 requests and cache them in sqlite
Diffstat (limited to 'include/mbgl/storage')
-rw-r--r--include/mbgl/storage/base_request.hpp46
-rw-r--r--include/mbgl/storage/file_request.hpp32
-rw-r--r--include/mbgl/storage/file_source.hpp54
-rw-r--r--include/mbgl/storage/http_request.hpp38
-rw-r--r--include/mbgl/storage/http_request_baton.hpp28
-rw-r--r--include/mbgl/storage/request.hpp39
-rw-r--r--include/mbgl/storage/resource_type.hpp18
-rw-r--r--include/mbgl/storage/response.hpp23
-rw-r--r--include/mbgl/storage/sqlite_store.hpp47
9 files changed, 325 insertions, 0 deletions
diff --git a/include/mbgl/storage/base_request.hpp b/include/mbgl/storage/base_request.hpp
new file mode 100644
index 0000000000..b8ebd368e4
--- /dev/null
+++ b/include/mbgl/storage/base_request.hpp
@@ -0,0 +1,46 @@
+#ifndef MBGL_STORAGE_BASE_REQUEST
+#define MBGL_STORAGE_BASE_REQUEST
+
+#include <string>
+#include <forward_list>
+#include <memory>
+#include <functional>
+
+typedef struct uv_loop_s uv_loop_t;
+typedef struct uv_async_s uv_async_t;
+
+namespace mbgl {
+
+class Response;
+class Request;
+using Callback = std::function<void(const Response &)>;
+
+
+class BaseRequest {
+private:
+ // Make noncopyable and immovable
+ BaseRequest(const BaseRequest &) = delete;
+ BaseRequest(BaseRequest &&) = delete;
+ BaseRequest& operator=(const BaseRequest &) = delete;
+ BaseRequest& operator=(BaseRequest &&) = delete;
+
+public:
+ BaseRequest();
+ virtual ~BaseRequest();
+
+ Callback *add(Callback &&callback, const std::shared_ptr<BaseRequest> &request);
+ void remove(Callback *callback);
+ void notify();
+
+public:
+ const unsigned long thread_id;
+ std::unique_ptr<Response> response;
+
+private:
+ std::shared_ptr<BaseRequest> self;
+ std::forward_list<std::unique_ptr<Callback>> callbacks;
+};
+
+}
+
+#endif
diff --git a/include/mbgl/storage/file_request.hpp b/include/mbgl/storage/file_request.hpp
new file mode 100644
index 0000000000..156fd6dfe7
--- /dev/null
+++ b/include/mbgl/storage/file_request.hpp
@@ -0,0 +1,32 @@
+#ifndef MBGL_STORAGE_FILE_REQUEST
+#define MBGL_STORAGE_FILE_REQUEST
+
+
+#include <string>
+#include <memory>
+#include <cassert>
+
+#include <mbgl/storage/base_request.hpp>
+
+namespace mbgl {
+
+typedef struct uv_loop_s uv_loop_t;
+
+struct FileRequestBaton;
+
+class FileRequest : public BaseRequest {
+public:
+ FileRequest(const std::string &path, uv_loop_t *loop);
+ ~FileRequest();
+
+private:
+ const std::string path;
+ const unsigned long thread_id;
+ FileRequestBaton *ptr = nullptr;
+
+ friend struct FileRequestBaton;
+};
+
+}
+
+#endif \ No newline at end of file
diff --git a/include/mbgl/storage/file_source.hpp b/include/mbgl/storage/file_source.hpp
new file mode 100644
index 0000000000..4cc95ae24e
--- /dev/null
+++ b/include/mbgl/storage/file_source.hpp
@@ -0,0 +1,54 @@
+#ifndef MBGL_STORAGE_FILE_SOURCE
+#define MBGL_STORAGE_FILE_SOURCE
+
+#include <mbgl/storage/resource_type.hpp>
+#include <mbgl/storage/request.hpp>
+
+#include <string>
+#include <memory>
+#include <unordered_map>
+#include <functional>
+
+typedef struct uv_loop_s uv_loop_t;
+typedef struct uv_messenger_s uv_messenger_t;
+
+namespace mbgl {
+
+class BaseRequest;
+class SQLiteStore;
+
+class FileSource {
+private:
+ FileSource(const FileSource &) = delete;
+ FileSource(FileSource &&) = delete;
+ FileSource& operator=(const FileSource &) = delete;
+ FileSource& operator=(FileSource &&) = delete;
+
+public:
+ FileSource(uv_loop_t *loop);
+ ~FileSource();
+
+public:
+ // Stores and retrieves the base path/URL for relative requests
+ void setBase(const std::string &value);
+ const std::string &getBase() const;
+
+ std::unique_ptr<Request> request(ResourceType type, const std::string &url);
+
+ void prepare(std::function<void()> fn);
+
+private:
+ const unsigned long thread_id;
+
+ // Stores a URL that is used as a base for loading resources with relative path.
+ std::string base;
+
+ std::unordered_map<std::string, std::weak_ptr<BaseRequest>> pending;
+ std::shared_ptr<SQLiteStore> store;
+ uv_loop_t *loop = nullptr;
+ uv_messenger_t *queue = nullptr;
+};
+
+}
+
+#endif
diff --git a/include/mbgl/storage/http_request.hpp b/include/mbgl/storage/http_request.hpp
new file mode 100644
index 0000000000..30f7b3aa6d
--- /dev/null
+++ b/include/mbgl/storage/http_request.hpp
@@ -0,0 +1,38 @@
+#ifndef MBGL_STORAGE_HTTP_REQUEST
+#define MBGL_STORAGE_HTTP_REQUEST
+
+#include <mbgl/storage/resource_type.hpp>
+#include <mbgl/storage/base_request.hpp>
+
+#include <string>
+#include <memory>
+#include <cassert>
+
+typedef struct uv_loop_s uv_loop_t;
+
+namespace mbgl {
+
+struct CacheRequestBaton;
+struct HTTPRequestBaton;
+struct CacheEntry;
+class SQLiteStore;
+
+class HTTPRequest : public BaseRequest {
+public:
+ HTTPRequest(ResourceType type, const std::string &path, uv_loop_t *loop, std::shared_ptr<SQLiteStore> store);
+ ~HTTPRequest();
+
+private:
+ void loadedCacheEntry(std::unique_ptr<Response> &&response);
+
+private:
+ const unsigned long thread_id;
+ CacheRequestBaton *cache_baton = nullptr;
+ HTTPRequestBaton *http_baton = nullptr;
+ std::shared_ptr<SQLiteStore> store;
+ const ResourceType type;
+};
+
+}
+
+#endif \ No newline at end of file
diff --git a/include/mbgl/storage/http_request_baton.hpp b/include/mbgl/storage/http_request_baton.hpp
new file mode 100644
index 0000000000..5f06a68cd1
--- /dev/null
+++ b/include/mbgl/storage/http_request_baton.hpp
@@ -0,0 +1,28 @@
+#ifndef MBGL_STORAGE_HTTP_REQUEST_BATON
+#define MBGL_STORAGE_HTTP_REQUEST_BATON
+
+#include <mbgl/storage/response.hpp>
+
+#include <string>
+
+typedef struct uv_async_s uv_async_t;
+
+namespace mbgl {
+
+class HTTPRequest;
+
+struct HTTPRequestBaton {
+ HTTPRequest *request = nullptr;
+ std::string path;
+ uv_async_t *async = nullptr;
+ std::unique_ptr<Response> response;
+ void *ptr = nullptr;
+ bool not_modified = false;
+
+ void start();
+ void cancel();
+};
+
+}
+
+#endif
diff --git a/include/mbgl/storage/request.hpp b/include/mbgl/storage/request.hpp
new file mode 100644
index 0000000000..fa27fbc781
--- /dev/null
+++ b/include/mbgl/storage/request.hpp
@@ -0,0 +1,39 @@
+#ifndef MBGL_STORAGE_REQUEST
+#define MBGL_STORAGE_REQUEST
+
+#include <mbgl/storage/response.hpp>
+
+#include <memory>
+#include <functional>
+#include <forward_list>
+
+typedef struct uv_loop_s uv_loop_t;
+
+namespace mbgl {
+
+class BaseRequest;
+using Callback = std::function<void(const Response &)>;
+
+class Request {
+private:
+ Request(const Request &) = delete;
+ Request(Request &&) = delete;
+ Request& operator=(const Request &) = delete;
+ Request& operator=(Request &&) = delete;
+
+public:
+ Request(const std::shared_ptr<BaseRequest> &base);
+ ~Request();
+
+ void onload(Callback cb);
+ void cancel();
+
+private:
+ const unsigned long thread_id;
+ std::shared_ptr<BaseRequest> base;
+ std::forward_list<Callback *> callbacks;
+};
+
+}
+
+#endif \ No newline at end of file
diff --git a/include/mbgl/storage/resource_type.hpp b/include/mbgl/storage/resource_type.hpp
new file mode 100644
index 0000000000..b7204a9fa1
--- /dev/null
+++ b/include/mbgl/storage/resource_type.hpp
@@ -0,0 +1,18 @@
+#ifndef MBGL_STORAGE_RESOURCE_TYPE
+#define MBGL_STORAGE_RESOURCE_TYPE
+
+#include <cstdint>
+
+namespace mbgl {
+
+enum class ResourceType : uint8_t {
+ Unknown = 0,
+ Tile = 1,
+ Glyphs = 2,
+ Image = 3,
+ JSON = 4
+};
+
+}
+
+#endif
diff --git a/include/mbgl/storage/response.hpp b/include/mbgl/storage/response.hpp
new file mode 100644
index 0000000000..4960173f9e
--- /dev/null
+++ b/include/mbgl/storage/response.hpp
@@ -0,0 +1,23 @@
+#ifndef MBGL_STORAGE_RESPONSE
+#define MBGL_STORAGE_RESPONSE
+
+#include <string>
+#include <ctime>
+
+namespace mbgl {
+
+class Response {
+public:
+ long code = 0;
+ int64_t modified = 0;
+ int64_t expires = 0;
+ std::string data;
+
+ std::string message;
+
+ static int64_t parseCacheControl(const char *value);
+};
+
+}
+
+#endif \ No newline at end of file
diff --git a/include/mbgl/storage/sqlite_store.hpp b/include/mbgl/storage/sqlite_store.hpp
new file mode 100644
index 0000000000..e03e6cf2bc
--- /dev/null
+++ b/include/mbgl/storage/sqlite_store.hpp
@@ -0,0 +1,47 @@
+#ifndef MBGL_STORAGE_SQLITE_STORE
+#define MBGL_STORAGE_SQLITE_STORE
+
+#include <mbgl/storage/file_source.hpp>
+#include <mbgl/storage/response.hpp>
+
+#include <uv.h>
+
+#include <string>
+
+typedef struct uv_worker_s uv_worker_t;
+
+namespace mapbox {
+namespace sqlite {
+class Database;
+}
+}
+
+namespace mbgl {
+
+class SQLiteStore {
+public:
+ SQLiteStore(uv_loop_t *loop, const std::string &path);
+ ~SQLiteStore();
+
+ typedef void (*GetCallback)(std::unique_ptr<Response> &&entry, void *ptr);
+
+ void get(const std::string &path, GetCallback cb, void *ptr);
+ void put(const std::string &path, ResourceType type, const Response &entry);
+ void updateExpiration(const std::string &path, int64_t expires);
+
+private:
+ void createSchema();
+ void closeDatabase();
+ static void runGet(uv_work_t *req);
+ static void runPut(uv_work_t *req);
+ static void deliverResult(uv_work_t *req, int status);
+
+private:
+ const unsigned long thread_id;
+ std::shared_ptr<mapbox::sqlite::Database> db;
+ uv_worker_t *worker = nullptr;
+};
+
+}
+
+#endif