Merge lp:~jamesh/thumbnailer/bug-1467751 into lp:thumbnailer
- bug-1467751
- Merge into trunk
Proposed by
James Henstridge
Status: | Superseded | ||||
---|---|---|---|---|---|
Proposed branch: | lp:~jamesh/thumbnailer/bug-1467751 | ||||
Merge into: | lp:thumbnailer | ||||
Diff against target: |
30692 lines (+25137/-3830) 204 files modified
CMakeLists.txt (+107/-31) COPYING.GPL (+674/-0) CTestCustom.cmake.in (+17/-0) HACKING (+121/-0) cmake/XEnableCoverageReport.cmake (+203/-0) cmake/coverage.cmake (+0/-34) data/CMakeLists.txt (+0/-1) data/com.canonical.Unity.Thumbnailer.gschema.xml (+67/-0) debian/changelog (+8/-0) debian/control (+12/-28) debian/copyright (+22/-2) debian/libthumbnailer-dev.install (+0/-3) debian/libthumbnailer0.install (+0/-2) debian/libthumbnailer0.symbols (+0/-7) debian/rules (+3/-0) debian/thumbnailer-service.install (+2/-0) include/CMakeLists.txt (+0/-11) include/core/cache_codec.h (+50/-0) include/core/cache_discard_policy.h (+41/-0) include/core/cache_events.h (+103/-0) include/core/internal/cache_event_indexes.h (+46/-0) include/core/internal/persistent_string_cache_impl.h (+191/-0) include/core/internal/persistent_string_cache_stats.h (+237/-0) include/core/optional.h (+35/-0) include/core/persistent_cache.h (+3104/-0) include/core/persistent_cache_stats.h (+230/-0) include/core/persistent_string_cache.h (+726/-0) include/internal/artdownloader.h (+44/-16) include/internal/artreply.h (+60/-0) include/internal/audioimageextractor.h (+0/-41) include/internal/check_access.h (+42/-0) include/internal/config.h.in (+4/-7) include/internal/file_io.h (+51/-0) include/internal/gobj_memory.h (+170/-65) include/internal/httpdownloader.h (+25/-13) include/internal/image.h (+82/-0) include/internal/imageextractor.h (+59/-22) include/internal/imagescaler.h (+0/-55) include/internal/lastfmdownloader.h (+0/-45) include/internal/make_directories.h (+44/-0) include/internal/mediaartcache.h (+0/-61) include/internal/raii.h (+55/-0) include/internal/safe_strerror.h (+38/-0) include/internal/settings.h (+72/-0) include/internal/soupdownloader.h (+0/-40) include/internal/thumbnailcache.h (+0/-50) include/internal/thumbnailer.h (+128/-47) include/internal/trace.h (+45/-0) include/internal/ubuntuserverdownloader.h (+49/-21) include/service/dbus_names.h (+40/-0) include/thumbnailer.pc.in (+0/-10) plugins/Ubuntu/Thumbnailer.0.1/CMakeLists.txt (+11/-2) plugins/Ubuntu/Thumbnailer.0.1/albumartgenerator.cpp (+46/-52) plugins/Ubuntu/Thumbnailer.0.1/albumartgenerator.h (+15/-8) plugins/Ubuntu/Thumbnailer.0.1/artgeneratorcommon.cpp (+45/-19) plugins/Ubuntu/Thumbnailer.0.1/artgeneratorcommon.h (+12/-6) plugins/Ubuntu/Thumbnailer.0.1/artistartgenerator.cpp (+46/-52) plugins/Ubuntu/Thumbnailer.0.1/artistartgenerator.h (+18/-11) plugins/Ubuntu/Thumbnailer.0.1/plugin.cpp (+65/-24) plugins/Ubuntu/Thumbnailer.0.1/plugin.h (+14/-7) plugins/Ubuntu/Thumbnailer.0.1/thumbnailerimageresponse.cpp (+117/-0) plugins/Ubuntu/Thumbnailer.0.1/thumbnailerimageresponse.h (+68/-0) plugins/Ubuntu/Thumbnailer.0.1/thumbnailgenerator.cpp (+67/-77) plugins/Ubuntu/Thumbnailer.0.1/thumbnailgenerator.h (+25/-16) src/CMakeLists.txt (+43/-36) src/artdownloader.cpp (+40/-0) src/audioimageextractor.cpp (+0/-58) src/check_access.cpp (+83/-0) src/core/CMakeLists.txt (+13/-0) src/core/internal/CMakeLists.txt (+5/-0) src/core/internal/persistent_string_cache_impl.cpp (+1646/-0) src/core/persistent_cache_stats.cpp (+219/-0) src/core/persistent_string_cache.cpp (+239/-0) src/file_io.cpp (+194/-0) src/image.cpp (+484/-0) src/imageextractor.cpp (+149/-89) src/imagescaler.cpp (+0/-163) src/lastfmdownloader.cpp (+0/-95) src/libthumbnailer.map (+0/-15) src/make_directories.cpp (+51/-0) src/mediaartcache.cpp (+0/-225) src/safe_strerror.cpp (+72/-0) src/service/CMakeLists.txt (+20/-17) src/service/admininterface.cpp (+141/-0) src/service/admininterface.h (+65/-0) src/service/admininterface.xml (+41/-0) src/service/bus.xml (+9/-0) src/service/com.canonical.Thumbnailer.service.in (+0/-1) src/service/credentialscache.cpp (+187/-0) src/service/credentialscache.h (+81/-0) src/service/dbusinterface.cpp (+246/-237) src/service/dbusinterface.h (+71/-27) src/service/dbusinterface.xml (+14/-11) src/service/handler.cpp (+443/-0) src/service/handler.h (+91/-0) src/service/inactivityhandler.cpp (+63/-0) src/service/inactivityhandler.h (+49/-0) src/service/main.cpp (+80/-43) src/service/ratelimiter.cpp (+69/-0) src/service/ratelimiter.h (+63/-0) src/service/stats.cpp (+105/-0) src/service/stats.h (+75/-0) src/settings.cpp (+170/-0) src/soupdownloader.cpp (+0/-46) src/thumbnailcache.cpp (+0/-371) src/thumbnailer-admin/CMakeLists.txt (+33/-0) src/thumbnailer-admin/action.h (+66/-0) src/thumbnailer-admin/clear.cpp (+115/-0) src/thumbnailer-admin/clear.h (+50/-0) src/thumbnailer-admin/dbus_connection.cpp (+58/-0) src/thumbnailer-admin/dbus_connection.h (+57/-0) src/thumbnailer-admin/get_local_thumbnail.cpp (+113/-0) src/thumbnailer-admin/get_local_thumbnail.h (+52/-0) src/thumbnailer-admin/get_remote_thumbnail.cpp (+129/-0) src/thumbnailer-admin/get_remote_thumbnail.h (+53/-0) src/thumbnailer-admin/parse_size.cpp (+62/-0) src/thumbnailer-admin/parse_size.h (+39/-0) src/thumbnailer-admin/show_stats.cpp (+223/-0) src/thumbnailer-admin/show_stats.h (+55/-0) src/thumbnailer-admin/shutdown.cpp (+74/-0) src/thumbnailer-admin/shutdown.h (+47/-0) src/thumbnailer-admin/thumbnailer-admin.cpp (+143/-0) src/thumbnailer-admin/util.cpp (+79/-0) src/thumbnailer-admin/util.h (+49/-0) src/thumbnailer.cpp (+735/-303) src/trace.cpp (+103/-0) src/ubuntuserverdownloader.cpp (+240/-74) src/vs-thumb/CMakeLists.txt (+13/-0) src/vs-thumb/thumbnailextractor.cpp (+430/-0) src/vs-thumb/thumbnailextractor.h (+56/-0) src/vs-thumb/vs-thumb.cpp (+90/-207) tests/CMakeLists.txt (+52/-30) tests/basic.cpp (+0/-212) tests/check_access/CMakeLists.txt (+4/-0) tests/check_access/check_access_test.cpp (+64/-0) tests/copyright/CMakeLists.txt (+6/-0) tests/copyright/check_copyright.sh (+53/-0) tests/core/CMakeLists.txt (+8/-0) tests/core/internal/CMakeLists.txt (+1/-0) tests/core/internal/persistent_string_cache_impl/CMakeLists.txt (+5/-0) tests/core/internal/persistent_string_cache_impl/persistent_string_cache_impl_test.cpp (+1980/-0) tests/core/persistent_cache/CMakeLists.txt (+5/-0) tests/core/persistent_cache/persistent_cache_test.cpp (+1413/-0) tests/core/persistent_string_cache/CMakeLists.txt (+11/-0) tests/core/persistent_string_cache/persistent_string_cache_test.cpp (+469/-0) tests/core/persistent_string_cache/speed_test.cpp (+212/-0) tests/dbus/CMakeLists.txt (+12/-0) tests/dbus/dbus_test.cpp (+548/-0) tests/download/CMakeLists.txt (+11/-0) tests/download/download_test.cpp (+216/-135) tests/file_io/CMakeLists.txt (+4/-0) tests/file_io/file_io_test.cpp (+155/-0) tests/gobj_ptr/CMakeLists.txt (+3/-0) tests/gobj_ptr/gobj_ptr_test.cpp (+166/-129) tests/headers/CMakeLists.txt (+45/-0) tests/headers/compile_headers.py (+169/-0) tests/image/CMakeLists.txt (+4/-0) tests/image/image_test.cpp (+368/-0) tests/media/bad_image.jpg (+1/-0) tests/mediaartcachetest.cpp (+0/-169) tests/qml/CMakeLists.txt (+11/-0) tests/qml/Fixture.qml (+109/-0) tests/qml/qml_test.cpp (+78/-0) tests/qml/tst_albumart.qml (+41/-66) tests/qml/tst_embedded_albumart.qml (+10/-0) tests/qml/tst_photo.qml (+61/-0) tests/safe_strerror/CMakeLists.txt (+4/-0) tests/safe_strerror/safe_strerror_test.cpp (+29/-0) tests/server/images/artist_album.png (+1/-0) tests/server/images/sia_fear.txt (+1/-0) tests/server/images/sia_fear_album.txt (+1/-0) tests/server/server.py (+96/-0) tests/settings/CMakeLists.txt (+4/-0) tests/settings/settings_test.cpp (+146/-0) tests/slow-vs-thumb/CMakeLists.txt (+11/-0) tests/slow-vs-thumb/slow-vs-thumb_test.cpp (+63/-0) tests/slow-vs-thumb/slow/vs-thumb (+3/-0) tests/testsetup.h.in (+29/-3) tests/thumbnailer-admin/CMakeLists.txt (+16/-0) tests/thumbnailer-admin/thumbnailer-admin_test.cpp (+616/-0) tests/thumbnailer/CMakeLists.txt (+14/-0) tests/thumbnailer/thumbnailer_test.cpp (+903/-0) tests/thumbnailer/vs-thumb-crash/vs-thumb (+3/-0) tests/thumbnailer/vs-thumb-exit-1/vs-thumb (+3/-0) tests/thumbnailer/vs-thumb-exit-2/vs-thumb (+3/-0) tests/thumbnailer/vs-thumb-exit-99/vs-thumb (+3/-0) tests/utils/CMakeLists.txt (+34/-0) tests/utils/artserver.cpp (+59/-0) tests/utils/artserver.h (+34/-0) tests/utils/dbusserver.cpp (+64/-0) tests/utils/dbusserver.h (+48/-0) tests/vs-thumb/CMakeLists.txt (+9/-0) tests/vs-thumb/vs-thumb_test.cpp (+223/-0) tests/whitespace/CMakeLists.txt (+9/-0) tests/whitespace/check_whitespace.py (+114/-0) tools/CMakeLists.txt (+0/-14) tools/cachetool.cpp (+0/-37) tools/imdump.cpp (+0/-34) tools/lastfmtest.cpp (+0/-31) tools/parse-settings.py (+123/-0) tools/scaler.cpp (+0/-38) tools/ubuntuservertest.cpp (+0/-28) tsan-suppress (+2/-0) valgrind-suppress (+133/-0) |
||||
To merge this branch: | bzr merge lp:~jamesh/thumbnailer/bug-1467751 | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
PS Jenkins bot (community) | continuous-integration | Approve | |
Unity Team | Pending | ||
Review via email: mp+262659@code.launchpad.net |
This proposal has been superseded by a proposal from 2015-06-23.
Commit message
Set finish_time within the Handler:
Description of the change
Set finish_time within the Handler:
To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : | # |
review:
Approve
(continuous-integration)
- 233. By James Henstridge
-
Merge from devel, fixing conflicts
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'CMakeLists.txt' |
2 | --- CMakeLists.txt 2014-09-25 09:47:05 +0000 |
3 | +++ CMakeLists.txt 2015-06-23 06:06:30 +0000 |
4 | @@ -1,27 +1,84 @@ |
5 | project(thumbnailer C CXX) |
6 | -cmake_minimum_required(VERSION 2.8.7) |
7 | -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra") |
8 | - |
9 | -include(CheckCXXCompilerFlag) |
10 | -check_cxx_compiler_flag(-std=c++11 HAS_C11) |
11 | -# Precise's GCC is too old to have -std=c++11 |
12 | -if(HAS_C11) |
13 | -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") |
14 | -else() |
15 | -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Dfinal=") |
16 | -endif() |
17 | - |
18 | -set(SO_VERSION_MAJOR "1") |
19 | -set(SO_VERSION_MINOR "1") |
20 | -set(SO_VERSION_PATCH "0") |
21 | - |
22 | -set(SO_VERSION "${SO_VERSION_MAJOR}.${SO_VERSION_MINOR}.${SO_VERSION_PATCH}") |
23 | - |
24 | -# Increment this manually whenever breaking ABI. |
25 | -# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN135 |
26 | -set(ABI_VERSION 0) |
27 | - |
28 | -include(cmake/coverage.cmake) |
29 | + |
30 | +set(CMAKE_INCLUDE_CURRENT_DIR ON) |
31 | + |
32 | +cmake_minimum_required(VERSION 2.8.11) |
33 | + |
34 | +# Suppress complaints by cmake about COMPILE_DEFINITIONS_<Config> caused by qt5 cmake macros. |
35 | +cmake_policy(SET CMP0043 OLD) |
36 | + |
37 | +string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_lower) # Build types should always be lower case |
38 | + |
39 | +set(ACCEPTED_BUILD_TYPES "" none release debug relwithdebinfo coverage) |
40 | +list(FIND ACCEPTED_BUILD_TYPES "${cmake_build_type_lower}" IS_BUILD_TYPE_ACCEPTED) |
41 | +if(${IS_BUILD_TYPE_ACCEPTED} EQUAL -1) |
42 | + message(FATAL_ERROR "Invalid CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}\nValid types are: ${ACCEPTED_BUILD_TYPES}") |
43 | +endif() |
44 | + |
45 | +set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) |
46 | + |
47 | +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -Wall -pedantic -Wextra") |
48 | + |
49 | +# Some additional warnings not included by the general flags set above. |
50 | +set(EXTRA_C_WARNINGS "-Wcast-align -Wcast-qual -Wformat -Wredundant-decls -Wswitch-default") |
51 | +set(EXTRA_CXX_WARNINGS "-Wnon-virtual-dtor -Wctor-dtor-privacy -Wold-style-cast") |
52 | +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_C_WARNINGS} ${EXTRA_CXX_WARNINGS}") |
53 | + |
54 | +# By default, for release builds, warnings become hard errors. |
55 | +if ("${cmake_build_type_lower}" STREQUAL "release" OR "${cmake_build_type_lower}" STREQUAL "relwithdebinfo") |
56 | + option(Werror "Treat warnings as errors" ON) |
57 | +else() |
58 | + option(Werror "Treat warnings as errors" OFF) |
59 | +endif() |
60 | + |
61 | +# If warnings are errors, don't error on deprecated declarations. |
62 | +if (${Werror}) |
63 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") |
64 | + if ("${cmake_build_type_lower}" STREQUAL "release" OR "${cmake_build_type_lower}" STREQUAL "relwithdebinfo") |
65 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-declarations") |
66 | + endif() |
67 | +endif() |
68 | + |
69 | +add_definitions(-DQT_NO_KEYWORDS) |
70 | + |
71 | +# Flags for thread/address sanitizer |
72 | +set(SANITIZER "" CACHE STRING "Build with -fsanitize=<value> (legal values: thread, address)") |
73 | + |
74 | +if ("${SANITIZER}" STREQUAL "") |
75 | + # Do nothing |
76 | +elseif (${SANITIZER} STREQUAL "thread") |
77 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -g") |
78 | +elseif (${SANITIZER} STREQUAL "address") |
79 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -g") |
80 | +else() |
81 | + message(FATAL_ERROR "Invalid SANITIZER setting: ${SANITIZER}") |
82 | +endif() |
83 | + |
84 | +# Some tests are slow, so make it possible not to run them |
85 | +# during day-to-day development. |
86 | +option(slowtests "Run slow tests" ON) |
87 | + |
88 | +# Definitions for testing with valgrind. |
89 | + |
90 | +configure_file(CTestCustom.cmake.in CTestCustom.cmake) # Tests in CTestCustom.cmake are skipped for valgrind |
91 | + |
92 | +find_program(MEMORYCHECK_COMMAND NAMES valgrind) |
93 | +if (MEMORYCHECK_COMMAND) |
94 | + set(MEMORYCHECK_COMMAND_OPTIONS |
95 | + "--suppressions=${CMAKE_SOURCE_DIR}/valgrind-suppress --errors-for-leak-kinds=definite --show-leak-kinds=definite --leak-check=full --num-callers=50 --error-exitcode=3" |
96 | + ) |
97 | + add_custom_target(valgrind DEPENDS NightlyMemCheck) |
98 | +else() |
99 | + message(WARNING "Cannot find valgrind: valgrind target will not be available") |
100 | +endif() |
101 | + |
102 | +include(CTest) |
103 | +enable_testing() |
104 | + |
105 | +# TODO: replace this with include(EnableCoverageReport) once the updated module makes it into the archives. |
106 | +# See https://code.launchpad.net/~michihenning/cmake-extras/cmake-extras-coverage/+merge/258021 |
107 | +include(XEnableCoverageReport) |
108 | + |
109 | include(cmake/UseGSettings.cmake) |
110 | |
111 | include(GNUInstallDirs) |
112 | @@ -29,27 +86,46 @@ |
113 | set(SHARE_PRIV_DIR ${CMAKE_INSTALL_LIBDIR}/thumbnailer) |
114 | set(SHARE_PRIV_ABS ${CMAKE_INSTALL_PREFIX}/${SHARE_PRIV_DIR}) |
115 | |
116 | +find_package(Boost COMPONENTS filesystem system REQUIRED) |
117 | find_package(Threads REQUIRED) |
118 | find_package(Qt5Core REQUIRED) |
119 | +find_package(Qt5DBus REQUIRED) |
120 | + |
121 | include(FindPkgConfig) |
122 | -pkg_check_modules(GST_DEPS REQUIRED gstreamer-1.0 gstreamer-plugins-base-1.0 gstreamer-pbutils-1.0 gstreamer-app-1.0) |
123 | +pkg_check_modules(GST_DEPS REQUIRED gstreamer-1.0 gstreamer-plugins-base-1.0 gstreamer-tag-1.0) |
124 | +pkg_check_modules(GOBJ_DEPS REQUIRED gobject-2.0) |
125 | pkg_check_modules(GIO_DEPS REQUIRED gio-2.0 gio-unix-2.0) |
126 | pkg_check_modules(IMG_DEPS REQUIRED gdk-pixbuf-2.0 libexif) |
127 | -pkg_check_modules(SOUP_DEPS REQUIRED libsoup-2.4) |
128 | -pkg_check_modules(XML_DEPS REQUIRED libxml-2.0) |
129 | +pkg_check_modules(UNITY_API_DEPS REQUIRED libunity-api) |
130 | +pkg_check_modules(APPARMOR_DEPS REQUIRED libapparmor) |
131 | |
132 | include_directories(${GST_DEPS_INCLUDE_DIRS}) |
133 | +include_directories(${GOBJ_DEPS_INCLUDE_DIRS}) |
134 | include_directories(${GIO_DEPS_INCLUDE_DIRS}) |
135 | include_directories(${IMG_DEPS_INCLUDE_DIRS}) |
136 | -include_directories(${SOUP_DEPS_INCLUDE_DIRS}) |
137 | -include_directories(${XML_DEPS_INCLUDE_DIRS}) |
138 | +include_directories(${UNITY_API_DEPS_INCLUDE_DIRS}) |
139 | +include_directories(${APPARMOR_DEPS_INCLUDE_DIRS}) |
140 | include_directories(include) |
141 | include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) |
142 | |
143 | -enable_testing() |
144 | add_subdirectory(src) |
145 | add_subdirectory(data) |
146 | -add_subdirectory(plugins/Ubuntu/Thumbnailer) |
147 | +add_subdirectory(plugins/Ubuntu/Thumbnailer.0.1) |
148 | add_subdirectory(tests) |
149 | -add_subdirectory(tools) |
150 | add_subdirectory(include) |
151 | + |
152 | +enable_coverage_report( |
153 | + TARGETS |
154 | + thumbnailer |
155 | + thumbnailer-admin |
156 | + thumbnailer-service |
157 | + thumbnailer-qml |
158 | + vs-thumb |
159 | + testutils |
160 | + FILTER |
161 | + ${CMAKE_SOURCE_DIR}/include/core/* |
162 | + ${CMAKE_SOURCE_DIR}/tests/* |
163 | + ${CMAKE_BINARY_DIR}/* |
164 | + TESTS |
165 | + ${UNIT_TEST_TARGETS} |
166 | +) |
167 | |
168 | === added file 'COPYING.GPL' |
169 | --- COPYING.GPL 1970-01-01 00:00:00 +0000 |
170 | +++ COPYING.GPL 2015-06-23 06:06:30 +0000 |
171 | @@ -0,0 +1,674 @@ |
172 | + GNU GENERAL PUBLIC LICENSE |
173 | + Version 3, 29 June 2007 |
174 | + |
175 | + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> |
176 | + Everyone is permitted to copy and distribute verbatim copies |
177 | + of this license document, but changing it is not allowed. |
178 | + |
179 | + Preamble |
180 | + |
181 | + The GNU General Public License is a free, copyleft license for |
182 | +software and other kinds of works. |
183 | + |
184 | + The licenses for most software and other practical works are designed |
185 | +to take away your freedom to share and change the works. By contrast, |
186 | +the GNU General Public License is intended to guarantee your freedom to |
187 | +share and change all versions of a program--to make sure it remains free |
188 | +software for all its users. We, the Free Software Foundation, use the |
189 | +GNU General Public License for most of our software; it applies also to |
190 | +any other work released this way by its authors. You can apply it to |
191 | +your programs, too. |
192 | + |
193 | + When we speak of free software, we are referring to freedom, not |
194 | +price. Our General Public Licenses are designed to make sure that you |
195 | +have the freedom to distribute copies of free software (and charge for |
196 | +them if you wish), that you receive source code or can get it if you |
197 | +want it, that you can change the software or use pieces of it in new |
198 | +free programs, and that you know you can do these things. |
199 | + |
200 | + To protect your rights, we need to prevent others from denying you |
201 | +these rights or asking you to surrender the rights. Therefore, you have |
202 | +certain responsibilities if you distribute copies of the software, or if |
203 | +you modify it: responsibilities to respect the freedom of others. |
204 | + |
205 | + For example, if you distribute copies of such a program, whether |
206 | +gratis or for a fee, you must pass on to the recipients the same |
207 | +freedoms that you received. You must make sure that they, too, receive |
208 | +or can get the source code. And you must show them these terms so they |
209 | +know their rights. |
210 | + |
211 | + Developers that use the GNU GPL protect your rights with two steps: |
212 | +(1) assert copyright on the software, and (2) offer you this License |
213 | +giving you legal permission to copy, distribute and/or modify it. |
214 | + |
215 | + For the developers' and authors' protection, the GPL clearly explains |
216 | +that there is no warranty for this free software. For both users' and |
217 | +authors' sake, the GPL requires that modified versions be marked as |
218 | +changed, so that their problems will not be attributed erroneously to |
219 | +authors of previous versions. |
220 | + |
221 | + Some devices are designed to deny users access to install or run |
222 | +modified versions of the software inside them, although the manufacturer |
223 | +can do so. This is fundamentally incompatible with the aim of |
224 | +protecting users' freedom to change the software. The systematic |
225 | +pattern of such abuse occurs in the area of products for individuals to |
226 | +use, which is precisely where it is most unacceptable. Therefore, we |
227 | +have designed this version of the GPL to prohibit the practice for those |
228 | +products. If such problems arise substantially in other domains, we |
229 | +stand ready to extend this provision to those domains in future versions |
230 | +of the GPL, as needed to protect the freedom of users. |
231 | + |
232 | + Finally, every program is threatened constantly by software patents. |
233 | +States should not allow patents to restrict development and use of |
234 | +software on general-purpose computers, but in those that do, we wish to |
235 | +avoid the special danger that patents applied to a free program could |
236 | +make it effectively proprietary. To prevent this, the GPL assures that |
237 | +patents cannot be used to render the program non-free. |
238 | + |
239 | + The precise terms and conditions for copying, distribution and |
240 | +modification follow. |
241 | + |
242 | + TERMS AND CONDITIONS |
243 | + |
244 | + 0. Definitions. |
245 | + |
246 | + "This License" refers to version 3 of the GNU General Public License. |
247 | + |
248 | + "Copyright" also means copyright-like laws that apply to other kinds of |
249 | +works, such as semiconductor masks. |
250 | + |
251 | + "The Program" refers to any copyrightable work licensed under this |
252 | +License. Each licensee is addressed as "you". "Licensees" and |
253 | +"recipients" may be individuals or organizations. |
254 | + |
255 | + To "modify" a work means to copy from or adapt all or part of the work |
256 | +in a fashion requiring copyright permission, other than the making of an |
257 | +exact copy. The resulting work is called a "modified version" of the |
258 | +earlier work or a work "based on" the earlier work. |
259 | + |
260 | + A "covered work" means either the unmodified Program or a work based |
261 | +on the Program. |
262 | + |
263 | + To "propagate" a work means to do anything with it that, without |
264 | +permission, would make you directly or secondarily liable for |
265 | +infringement under applicable copyright law, except executing it on a |
266 | +computer or modifying a private copy. Propagation includes copying, |
267 | +distribution (with or without modification), making available to the |
268 | +public, and in some countries other activities as well. |
269 | + |
270 | + To "convey" a work means any kind of propagation that enables other |
271 | +parties to make or receive copies. Mere interaction with a user through |
272 | +a computer network, with no transfer of a copy, is not conveying. |
273 | + |
274 | + An interactive user interface displays "Appropriate Legal Notices" |
275 | +to the extent that it includes a convenient and prominently visible |
276 | +feature that (1) displays an appropriate copyright notice, and (2) |
277 | +tells the user that there is no warranty for the work (except to the |
278 | +extent that warranties are provided), that licensees may convey the |
279 | +work under this License, and how to view a copy of this License. If |
280 | +the interface presents a list of user commands or options, such as a |
281 | +menu, a prominent item in the list meets this criterion. |
282 | + |
283 | + 1. Source Code. |
284 | + |
285 | + The "source code" for a work means the preferred form of the work |
286 | +for making modifications to it. "Object code" means any non-source |
287 | +form of a work. |
288 | + |
289 | + A "Standard Interface" means an interface that either is an official |
290 | +standard defined by a recognized standards body, or, in the case of |
291 | +interfaces specified for a particular programming language, one that |
292 | +is widely used among developers working in that language. |
293 | + |
294 | + The "System Libraries" of an executable work include anything, other |
295 | +than the work as a whole, that (a) is included in the normal form of |
296 | +packaging a Major Component, but which is not part of that Major |
297 | +Component, and (b) serves only to enable use of the work with that |
298 | +Major Component, or to implement a Standard Interface for which an |
299 | +implementation is available to the public in source code form. A |
300 | +"Major Component", in this context, means a major essential component |
301 | +(kernel, window system, and so on) of the specific operating system |
302 | +(if any) on which the executable work runs, or a compiler used to |
303 | +produce the work, or an object code interpreter used to run it. |
304 | + |
305 | + The "Corresponding Source" for a work in object code form means all |
306 | +the source code needed to generate, install, and (for an executable |
307 | +work) run the object code and to modify the work, including scripts to |
308 | +control those activities. However, it does not include the work's |
309 | +System Libraries, or general-purpose tools or generally available free |
310 | +programs which are used unmodified in performing those activities but |
311 | +which are not part of the work. For example, Corresponding Source |
312 | +includes interface definition files associated with source files for |
313 | +the work, and the source code for shared libraries and dynamically |
314 | +linked subprograms that the work is specifically designed to require, |
315 | +such as by intimate data communication or control flow between those |
316 | +subprograms and other parts of the work. |
317 | + |
318 | + The Corresponding Source need not include anything that users |
319 | +can regenerate automatically from other parts of the Corresponding |
320 | +Source. |
321 | + |
322 | + The Corresponding Source for a work in source code form is that |
323 | +same work. |
324 | + |
325 | + 2. Basic Permissions. |
326 | + |
327 | + All rights granted under this License are granted for the term of |
328 | +copyright on the Program, and are irrevocable provided the stated |
329 | +conditions are met. This License explicitly affirms your unlimited |
330 | +permission to run the unmodified Program. The output from running a |
331 | +covered work is covered by this License only if the output, given its |
332 | +content, constitutes a covered work. This License acknowledges your |
333 | +rights of fair use or other equivalent, as provided by copyright law. |
334 | + |
335 | + You may make, run and propagate covered works that you do not |
336 | +convey, without conditions so long as your license otherwise remains |
337 | +in force. You may convey covered works to others for the sole purpose |
338 | +of having them make modifications exclusively for you, or provide you |
339 | +with facilities for running those works, provided that you comply with |
340 | +the terms of this License in conveying all material for which you do |
341 | +not control copyright. Those thus making or running the covered works |
342 | +for you must do so exclusively on your behalf, under your direction |
343 | +and control, on terms that prohibit them from making any copies of |
344 | +your copyrighted material outside their relationship with you. |
345 | + |
346 | + Conveying under any other circumstances is permitted solely under |
347 | +the conditions stated below. Sublicensing is not allowed; section 10 |
348 | +makes it unnecessary. |
349 | + |
350 | + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. |
351 | + |
352 | + No covered work shall be deemed part of an effective technological |
353 | +measure under any applicable law fulfilling obligations under article |
354 | +11 of the WIPO copyright treaty adopted on 20 December 1996, or |
355 | +similar laws prohibiting or restricting circumvention of such |
356 | +measures. |
357 | + |
358 | + When you convey a covered work, you waive any legal power to forbid |
359 | +circumvention of technological measures to the extent such circumvention |
360 | +is effected by exercising rights under this License with respect to |
361 | +the covered work, and you disclaim any intention to limit operation or |
362 | +modification of the work as a means of enforcing, against the work's |
363 | +users, your or third parties' legal rights to forbid circumvention of |
364 | +technological measures. |
365 | + |
366 | + 4. Conveying Verbatim Copies. |
367 | + |
368 | + You may convey verbatim copies of the Program's source code as you |
369 | +receive it, in any medium, provided that you conspicuously and |
370 | +appropriately publish on each copy an appropriate copyright notice; |
371 | +keep intact all notices stating that this License and any |
372 | +non-permissive terms added in accord with section 7 apply to the code; |
373 | +keep intact all notices of the absence of any warranty; and give all |
374 | +recipients a copy of this License along with the Program. |
375 | + |
376 | + You may charge any price or no price for each copy that you convey, |
377 | +and you may offer support or warranty protection for a fee. |
378 | + |
379 | + 5. Conveying Modified Source Versions. |
380 | + |
381 | + You may convey a work based on the Program, or the modifications to |
382 | +produce it from the Program, in the form of source code under the |
383 | +terms of section 4, provided that you also meet all of these conditions: |
384 | + |
385 | + a) The work must carry prominent notices stating that you modified |
386 | + it, and giving a relevant date. |
387 | + |
388 | + b) The work must carry prominent notices stating that it is |
389 | + released under this License and any conditions added under section |
390 | + 7. This requirement modifies the requirement in section 4 to |
391 | + "keep intact all notices". |
392 | + |
393 | + c) You must license the entire work, as a whole, under this |
394 | + License to anyone who comes into possession of a copy. This |
395 | + License will therefore apply, along with any applicable section 7 |
396 | + additional terms, to the whole of the work, and all its parts, |
397 | + regardless of how they are packaged. This License gives no |
398 | + permission to license the work in any other way, but it does not |
399 | + invalidate such permission if you have separately received it. |
400 | + |
401 | + d) If the work has interactive user interfaces, each must display |
402 | + Appropriate Legal Notices; however, if the Program has interactive |
403 | + interfaces that do not display Appropriate Legal Notices, your |
404 | + work need not make them do so. |
405 | + |
406 | + A compilation of a covered work with other separate and independent |
407 | +works, which are not by their nature extensions of the covered work, |
408 | +and which are not combined with it such as to form a larger program, |
409 | +in or on a volume of a storage or distribution medium, is called an |
410 | +"aggregate" if the compilation and its resulting copyright are not |
411 | +used to limit the access or legal rights of the compilation's users |
412 | +beyond what the individual works permit. Inclusion of a covered work |
413 | +in an aggregate does not cause this License to apply to the other |
414 | +parts of the aggregate. |
415 | + |
416 | + 6. Conveying Non-Source Forms. |
417 | + |
418 | + You may convey a covered work in object code form under the terms |
419 | +of sections 4 and 5, provided that you also convey the |
420 | +machine-readable Corresponding Source under the terms of this License, |
421 | +in one of these ways: |
422 | + |
423 | + a) Convey the object code in, or embodied in, a physical product |
424 | + (including a physical distribution medium), accompanied by the |
425 | + Corresponding Source fixed on a durable physical medium |
426 | + customarily used for software interchange. |
427 | + |
428 | + b) Convey the object code in, or embodied in, a physical product |
429 | + (including a physical distribution medium), accompanied by a |
430 | + written offer, valid for at least three years and valid for as |
431 | + long as you offer spare parts or customer support for that product |
432 | + model, to give anyone who possesses the object code either (1) a |
433 | + copy of the Corresponding Source for all the software in the |
434 | + product that is covered by this License, on a durable physical |
435 | + medium customarily used for software interchange, for a price no |
436 | + more than your reasonable cost of physically performing this |
437 | + conveying of source, or (2) access to copy the |
438 | + Corresponding Source from a network server at no charge. |
439 | + |
440 | + c) Convey individual copies of the object code with a copy of the |
441 | + written offer to provide the Corresponding Source. This |
442 | + alternative is allowed only occasionally and noncommercially, and |
443 | + only if you received the object code with such an offer, in accord |
444 | + with subsection 6b. |
445 | + |
446 | + d) Convey the object code by offering access from a designated |
447 | + place (gratis or for a charge), and offer equivalent access to the |
448 | + Corresponding Source in the same way through the same place at no |
449 | + further charge. You need not require recipients to copy the |
450 | + Corresponding Source along with the object code. If the place to |
451 | + copy the object code is a network server, the Corresponding Source |
452 | + may be on a different server (operated by you or a third party) |
453 | + that supports equivalent copying facilities, provided you maintain |
454 | + clear directions next to the object code saying where to find the |
455 | + Corresponding Source. Regardless of what server hosts the |
456 | + Corresponding Source, you remain obligated to ensure that it is |
457 | + available for as long as needed to satisfy these requirements. |
458 | + |
459 | + e) Convey the object code using peer-to-peer transmission, provided |
460 | + you inform other peers where the object code and Corresponding |
461 | + Source of the work are being offered to the general public at no |
462 | + charge under subsection 6d. |
463 | + |
464 | + A separable portion of the object code, whose source code is excluded |
465 | +from the Corresponding Source as a System Library, need not be |
466 | +included in conveying the object code work. |
467 | + |
468 | + A "User Product" is either (1) a "consumer product", which means any |
469 | +tangible personal property which is normally used for personal, family, |
470 | +or household purposes, or (2) anything designed or sold for incorporation |
471 | +into a dwelling. In determining whether a product is a consumer product, |
472 | +doubtful cases shall be resolved in favor of coverage. For a particular |
473 | +product received by a particular user, "normally used" refers to a |
474 | +typical or common use of that class of product, regardless of the status |
475 | +of the particular user or of the way in which the particular user |
476 | +actually uses, or expects or is expected to use, the product. A product |
477 | +is a consumer product regardless of whether the product has substantial |
478 | +commercial, industrial or non-consumer uses, unless such uses represent |
479 | +the only significant mode of use of the product. |
480 | + |
481 | + "Installation Information" for a User Product means any methods, |
482 | +procedures, authorization keys, or other information required to install |
483 | +and execute modified versions of a covered work in that User Product from |
484 | +a modified version of its Corresponding Source. The information must |
485 | +suffice to ensure that the continued functioning of the modified object |
486 | +code is in no case prevented or interfered with solely because |
487 | +modification has been made. |
488 | + |
489 | + If you convey an object code work under this section in, or with, or |
490 | +specifically for use in, a User Product, and the conveying occurs as |
491 | +part of a transaction in which the right of possession and use of the |
492 | +User Product is transferred to the recipient in perpetuity or for a |
493 | +fixed term (regardless of how the transaction is characterized), the |
494 | +Corresponding Source conveyed under this section must be accompanied |
495 | +by the Installation Information. But this requirement does not apply |
496 | +if neither you nor any third party retains the ability to install |
497 | +modified object code on the User Product (for example, the work has |
498 | +been installed in ROM). |
499 | + |
500 | + The requirement to provide Installation Information does not include a |
501 | +requirement to continue to provide support service, warranty, or updates |
502 | +for a work that has been modified or installed by the recipient, or for |
503 | +the User Product in which it has been modified or installed. Access to a |
504 | +network may be denied when the modification itself materially and |
505 | +adversely affects the operation of the network or violates the rules and |
506 | +protocols for communication across the network. |
507 | + |
508 | + Corresponding Source conveyed, and Installation Information provided, |
509 | +in accord with this section must be in a format that is publicly |
510 | +documented (and with an implementation available to the public in |
511 | +source code form), and must require no special password or key for |
512 | +unpacking, reading or copying. |
513 | + |
514 | + 7. Additional Terms. |
515 | + |
516 | + "Additional permissions" are terms that supplement the terms of this |
517 | +License by making exceptions from one or more of its conditions. |
518 | +Additional permissions that are applicable to the entire Program shall |
519 | +be treated as though they were included in this License, to the extent |
520 | +that they are valid under applicable law. If additional permissions |
521 | +apply only to part of the Program, that part may be used separately |
522 | +under those permissions, but the entire Program remains governed by |
523 | +this License without regard to the additional permissions. |
524 | + |
525 | + When you convey a copy of a covered work, you may at your option |
526 | +remove any additional permissions from that copy, or from any part of |
527 | +it. (Additional permissions may be written to require their own |
528 | +removal in certain cases when you modify the work.) You may place |
529 | +additional permissions on material, added by you to a covered work, |
530 | +for which you have or can give appropriate copyright permission. |
531 | + |
532 | + Notwithstanding any other provision of this License, for material you |
533 | +add to a covered work, you may (if authorized by the copyright holders of |
534 | +that material) supplement the terms of this License with terms: |
535 | + |
536 | + a) Disclaiming warranty or limiting liability differently from the |
537 | + terms of sections 15 and 16 of this License; or |
538 | + |
539 | + b) Requiring preservation of specified reasonable legal notices or |
540 | + author attributions in that material or in the Appropriate Legal |
541 | + Notices displayed by works containing it; or |
542 | + |
543 | + c) Prohibiting misrepresentation of the origin of that material, or |
544 | + requiring that modified versions of such material be marked in |
545 | + reasonable ways as different from the original version; or |
546 | + |
547 | + d) Limiting the use for publicity purposes of names of licensors or |
548 | + authors of the material; or |
549 | + |
550 | + e) Declining to grant rights under trademark law for use of some |
551 | + trade names, trademarks, or service marks; or |
552 | + |
553 | + f) Requiring indemnification of licensors and authors of that |
554 | + material by anyone who conveys the material (or modified versions of |
555 | + it) with contractual assumptions of liability to the recipient, for |
556 | + any liability that these contractual assumptions directly impose on |
557 | + those licensors and authors. |
558 | + |
559 | + All other non-permissive additional terms are considered "further |
560 | +restrictions" within the meaning of section 10. If the Program as you |
561 | +received it, or any part of it, contains a notice stating that it is |
562 | +governed by this License along with a term that is a further |
563 | +restriction, you may remove that term. If a license document contains |
564 | +a further restriction but permits relicensing or conveying under this |
565 | +License, you may add to a covered work material governed by the terms |
566 | +of that license document, provided that the further restriction does |
567 | +not survive such relicensing or conveying. |
568 | + |
569 | + If you add terms to a covered work in accord with this section, you |
570 | +must place, in the relevant source files, a statement of the |
571 | +additional terms that apply to those files, or a notice indicating |
572 | +where to find the applicable terms. |
573 | + |
574 | + Additional terms, permissive or non-permissive, may be stated in the |
575 | +form of a separately written license, or stated as exceptions; |
576 | +the above requirements apply either way. |
577 | + |
578 | + 8. Termination. |
579 | + |
580 | + You may not propagate or modify a covered work except as expressly |
581 | +provided under this License. Any attempt otherwise to propagate or |
582 | +modify it is void, and will automatically terminate your rights under |
583 | +this License (including any patent licenses granted under the third |
584 | +paragraph of section 11). |
585 | + |
586 | + However, if you cease all violation of this License, then your |
587 | +license from a particular copyright holder is reinstated (a) |
588 | +provisionally, unless and until the copyright holder explicitly and |
589 | +finally terminates your license, and (b) permanently, if the copyright |
590 | +holder fails to notify you of the violation by some reasonable means |
591 | +prior to 60 days after the cessation. |
592 | + |
593 | + Moreover, your license from a particular copyright holder is |
594 | +reinstated permanently if the copyright holder notifies you of the |
595 | +violation by some reasonable means, this is the first time you have |
596 | +received notice of violation of this License (for any work) from that |
597 | +copyright holder, and you cure the violation prior to 30 days after |
598 | +your receipt of the notice. |
599 | + |
600 | + Termination of your rights under this section does not terminate the |
601 | +licenses of parties who have received copies or rights from you under |
602 | +this License. If your rights have been terminated and not permanently |
603 | +reinstated, you do not qualify to receive new licenses for the same |
604 | +material under section 10. |
605 | + |
606 | + 9. Acceptance Not Required for Having Copies. |
607 | + |
608 | + You are not required to accept this License in order to receive or |
609 | +run a copy of the Program. Ancillary propagation of a covered work |
610 | +occurring solely as a consequence of using peer-to-peer transmission |
611 | +to receive a copy likewise does not require acceptance. However, |
612 | +nothing other than this License grants you permission to propagate or |
613 | +modify any covered work. These actions infringe copyright if you do |
614 | +not accept this License. Therefore, by modifying or propagating a |
615 | +covered work, you indicate your acceptance of this License to do so. |
616 | + |
617 | + 10. Automatic Licensing of Downstream Recipients. |
618 | + |
619 | + Each time you convey a covered work, the recipient automatically |
620 | +receives a license from the original licensors, to run, modify and |
621 | +propagate that work, subject to this License. You are not responsible |
622 | +for enforcing compliance by third parties with this License. |
623 | + |
624 | + An "entity transaction" is a transaction transferring control of an |
625 | +organization, or substantially all assets of one, or subdividing an |
626 | +organization, or merging organizations. If propagation of a covered |
627 | +work results from an entity transaction, each party to that |
628 | +transaction who receives a copy of the work also receives whatever |
629 | +licenses to the work the party's predecessor in interest had or could |
630 | +give under the previous paragraph, plus a right to possession of the |
631 | +Corresponding Source of the work from the predecessor in interest, if |
632 | +the predecessor has it or can get it with reasonable efforts. |
633 | + |
634 | + You may not impose any further restrictions on the exercise of the |
635 | +rights granted or affirmed under this License. For example, you may |
636 | +not impose a license fee, royalty, or other charge for exercise of |
637 | +rights granted under this License, and you may not initiate litigation |
638 | +(including a cross-claim or counterclaim in a lawsuit) alleging that |
639 | +any patent claim is infringed by making, using, selling, offering for |
640 | +sale, or importing the Program or any portion of it. |
641 | + |
642 | + 11. Patents. |
643 | + |
644 | + A "contributor" is a copyright holder who authorizes use under this |
645 | +License of the Program or a work on which the Program is based. The |
646 | +work thus licensed is called the contributor's "contributor version". |
647 | + |
648 | + A contributor's "essential patent claims" are all patent claims |
649 | +owned or controlled by the contributor, whether already acquired or |
650 | +hereafter acquired, that would be infringed by some manner, permitted |
651 | +by this License, of making, using, or selling its contributor version, |
652 | +but do not include claims that would be infringed only as a |
653 | +consequence of further modification of the contributor version. For |
654 | +purposes of this definition, "control" includes the right to grant |
655 | +patent sublicenses in a manner consistent with the requirements of |
656 | +this License. |
657 | + |
658 | + Each contributor grants you a non-exclusive, worldwide, royalty-free |
659 | +patent license under the contributor's essential patent claims, to |
660 | +make, use, sell, offer for sale, import and otherwise run, modify and |
661 | +propagate the contents of its contributor version. |
662 | + |
663 | + In the following three paragraphs, a "patent license" is any express |
664 | +agreement or commitment, however denominated, not to enforce a patent |
665 | +(such as an express permission to practice a patent or covenant not to |
666 | +sue for patent infringement). To "grant" such a patent license to a |
667 | +party means to make such an agreement or commitment not to enforce a |
668 | +patent against the party. |
669 | + |
670 | + If you convey a covered work, knowingly relying on a patent license, |
671 | +and the Corresponding Source of the work is not available for anyone |
672 | +to copy, free of charge and under the terms of this License, through a |
673 | +publicly available network server or other readily accessible means, |
674 | +then you must either (1) cause the Corresponding Source to be so |
675 | +available, or (2) arrange to deprive yourself of the benefit of the |
676 | +patent license for this particular work, or (3) arrange, in a manner |
677 | +consistent with the requirements of this License, to extend the patent |
678 | +license to downstream recipients. "Knowingly relying" means you have |
679 | +actual knowledge that, but for the patent license, your conveying the |
680 | +covered work in a country, or your recipient's use of the covered work |
681 | +in a country, would infringe one or more identifiable patents in that |
682 | +country that you have reason to believe are valid. |
683 | + |
684 | + If, pursuant to or in connection with a single transaction or |
685 | +arrangement, you convey, or propagate by procuring conveyance of, a |
686 | +covered work, and grant a patent license to some of the parties |
687 | +receiving the covered work authorizing them to use, propagate, modify |
688 | +or convey a specific copy of the covered work, then the patent license |
689 | +you grant is automatically extended to all recipients of the covered |
690 | +work and works based on it. |
691 | + |
692 | + A patent license is "discriminatory" if it does not include within |
693 | +the scope of its coverage, prohibits the exercise of, or is |
694 | +conditioned on the non-exercise of one or more of the rights that are |
695 | +specifically granted under this License. You may not convey a covered |
696 | +work if you are a party to an arrangement with a third party that is |
697 | +in the business of distributing software, under which you make payment |
698 | +to the third party based on the extent of your activity of conveying |
699 | +the work, and under which the third party grants, to any of the |
700 | +parties who would receive the covered work from you, a discriminatory |
701 | +patent license (a) in connection with copies of the covered work |
702 | +conveyed by you (or copies made from those copies), or (b) primarily |
703 | +for and in connection with specific products or compilations that |
704 | +contain the covered work, unless you entered into that arrangement, |
705 | +or that patent license was granted, prior to 28 March 2007. |
706 | + |
707 | + Nothing in this License shall be construed as excluding or limiting |
708 | +any implied license or other defenses to infringement that may |
709 | +otherwise be available to you under applicable patent law. |
710 | + |
711 | + 12. No Surrender of Others' Freedom. |
712 | + |
713 | + If conditions are imposed on you (whether by court order, agreement or |
714 | +otherwise) that contradict the conditions of this License, they do not |
715 | +excuse you from the conditions of this License. If you cannot convey a |
716 | +covered work so as to satisfy simultaneously your obligations under this |
717 | +License and any other pertinent obligations, then as a consequence you may |
718 | +not convey it at all. For example, if you agree to terms that obligate you |
719 | +to collect a royalty for further conveying from those to whom you convey |
720 | +the Program, the only way you could satisfy both those terms and this |
721 | +License would be to refrain entirely from conveying the Program. |
722 | + |
723 | + 13. Use with the GNU Affero General Public License. |
724 | + |
725 | + Notwithstanding any other provision of this License, you have |
726 | +permission to link or combine any covered work with a work licensed |
727 | +under version 3 of the GNU Affero General Public License into a single |
728 | +combined work, and to convey the resulting work. The terms of this |
729 | +License will continue to apply to the part which is the covered work, |
730 | +but the special requirements of the GNU Affero General Public License, |
731 | +section 13, concerning interaction through a network will apply to the |
732 | +combination as such. |
733 | + |
734 | + 14. Revised Versions of this License. |
735 | + |
736 | + The Free Software Foundation may publish revised and/or new versions of |
737 | +the GNU General Public License from time to time. Such new versions will |
738 | +be similar in spirit to the present version, but may differ in detail to |
739 | +address new problems or concerns. |
740 | + |
741 | + Each version is given a distinguishing version number. If the |
742 | +Program specifies that a certain numbered version of the GNU General |
743 | +Public License "or any later version" applies to it, you have the |
744 | +option of following the terms and conditions either of that numbered |
745 | +version or of any later version published by the Free Software |
746 | +Foundation. If the Program does not specify a version number of the |
747 | +GNU General Public License, you may choose any version ever published |
748 | +by the Free Software Foundation. |
749 | + |
750 | + If the Program specifies that a proxy can decide which future |
751 | +versions of the GNU General Public License can be used, that proxy's |
752 | +public statement of acceptance of a version permanently authorizes you |
753 | +to choose that version for the Program. |
754 | + |
755 | + Later license versions may give you additional or different |
756 | +permissions. However, no additional obligations are imposed on any |
757 | +author or copyright holder as a result of your choosing to follow a |
758 | +later version. |
759 | + |
760 | + 15. Disclaimer of Warranty. |
761 | + |
762 | + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY |
763 | +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT |
764 | +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY |
765 | +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, |
766 | +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
767 | +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM |
768 | +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF |
769 | +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
770 | + |
771 | + 16. Limitation of Liability. |
772 | + |
773 | + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
774 | +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS |
775 | +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY |
776 | +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE |
777 | +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF |
778 | +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD |
779 | +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), |
780 | +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
781 | +SUCH DAMAGES. |
782 | + |
783 | + 17. Interpretation of Sections 15 and 16. |
784 | + |
785 | + If the disclaimer of warranty and limitation of liability provided |
786 | +above cannot be given local legal effect according to their terms, |
787 | +reviewing courts shall apply local law that most closely approximates |
788 | +an absolute waiver of all civil liability in connection with the |
789 | +Program, unless a warranty or assumption of liability accompanies a |
790 | +copy of the Program in return for a fee. |
791 | + |
792 | + END OF TERMS AND CONDITIONS |
793 | + |
794 | + How to Apply These Terms to Your New Programs |
795 | + |
796 | + If you develop a new program, and you want it to be of the greatest |
797 | +possible use to the public, the best way to achieve this is to make it |
798 | +free software which everyone can redistribute and change under these terms. |
799 | + |
800 | + To do so, attach the following notices to the program. It is safest |
801 | +to attach them to the start of each source file to most effectively |
802 | +state the exclusion of warranty; and each file should have at least |
803 | +the "copyright" line and a pointer to where the full notice is found. |
804 | + |
805 | + <one line to give the program's name and a brief idea of what it does.> |
806 | + Copyright (C) <year> <name of author> |
807 | + |
808 | + This program is free software: you can redistribute it and/or modify |
809 | + it under the terms of the GNU General Public License as published by |
810 | + the Free Software Foundation, either version 3 of the License, or |
811 | + (at your option) any later version. |
812 | + |
813 | + This program is distributed in the hope that it will be useful, |
814 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
815 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
816 | + GNU General Public License for more details. |
817 | + |
818 | + You should have received a copy of the GNU General Public License |
819 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
820 | + |
821 | +Also add information on how to contact you by electronic and paper mail. |
822 | + |
823 | + If the program does terminal interaction, make it output a short |
824 | +notice like this when it starts in an interactive mode: |
825 | + |
826 | + <program> Copyright (C) <year> <name of author> |
827 | + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. |
828 | + This is free software, and you are welcome to redistribute it |
829 | + under certain conditions; type `show c' for details. |
830 | + |
831 | +The hypothetical commands `show w' and `show c' should show the appropriate |
832 | +parts of the General Public License. Of course, your program's commands |
833 | +might be different; for a GUI interface, you would use an "about box". |
834 | + |
835 | + You should also get your employer (if you work as a programmer) or school, |
836 | +if any, to sign a "copyright disclaimer" for the program, if necessary. |
837 | +For more information on this, and how to apply and follow the GNU GPL, see |
838 | +<http://www.gnu.org/licenses/>. |
839 | + |
840 | + The GNU General Public License does not permit incorporating your program |
841 | +into proprietary programs. If your program is a subroutine library, you |
842 | +may consider it more useful to permit linking proprietary applications with |
843 | +the library. If this is what you want to do, use the GNU Lesser General |
844 | +Public License instead of this License. But first, please read |
845 | +<http://www.gnu.org/philosophy/why-not-lgpl.html>. |
846 | |
847 | === renamed file 'COPYING' => 'COPYING.LGPL' |
848 | === added file 'CTestCustom.cmake.in' |
849 | --- CTestCustom.cmake.in 1970-01-01 00:00:00 +0000 |
850 | +++ CTestCustom.cmake.in 2015-06-23 06:06:30 +0000 |
851 | @@ -0,0 +1,17 @@ |
852 | +# |
853 | +# Tests listed here will not be run by the valgrind target, |
854 | +# either because there is not point (we don't want to |
855 | +# test that a python script doesn't leak), or because, |
856 | +# under valgrind, the test runs too slowly to meet |
857 | +# its timing constraints (or crashes valgrind). |
858 | +# |
859 | + |
860 | +SET(CTEST_CUSTOM_MEMCHECK_IGNORE |
861 | + test_qml # Too many bugs in QML to suppress them selectively |
862 | + speed |
863 | + copyright |
864 | + stand-alone-internal-headers |
865 | + stand-alone-core-headers |
866 | + stand-alone-core-internal-headers |
867 | + whitespace |
868 | +) |
869 | |
870 | === added file 'HACKING' |
871 | --- HACKING 1970-01-01 00:00:00 +0000 |
872 | +++ HACKING 2015-06-23 06:06:30 +0000 |
873 | @@ -0,0 +1,121 @@ |
874 | +Building the code |
875 | +----------------- |
876 | + |
877 | +By default, the code is built in release mode. To build a debug version, use |
878 | + |
879 | + $ mkdir builddebug |
880 | + $ cd builddebug |
881 | + $ cmake -DCMAKE_BUILD_TYPE=debug .. |
882 | + $ make |
883 | + |
884 | +For a release version, use -DCMAKE_BUILD_TYPE=release |
885 | + |
886 | +Running the tests |
887 | +----------------- |
888 | + |
889 | + $ make |
890 | + $ make test |
891 | + |
892 | +Note that "make test" alone is dangerous because it does not rebuild |
893 | +any tests if either the library or the test files themselves need |
894 | +rebuilding. It's not possible to fix this with cmake because cmake cannot |
895 | +add build dependencies to built-in targets. To make sure that everything |
896 | +is up-to-date, run "make" before running "make test"! |
897 | + |
898 | +To run the tests with valgrind: |
899 | + |
900 | + $ make valgrind |
901 | + |
902 | +It doesn't make sense for some tests to run them with valgrind. For |
903 | +example, the header compilation tests don't need valgrind because |
904 | +we'd just be testing that Python doesn't leak. There are also some |
905 | +tests that run too slow and time out under valgrind and, occasionally, |
906 | +valgrind crashes for a particular test. |
907 | + |
908 | +There are two ways to suppress tests: |
909 | + |
910 | +You can add a test name to CTestCustom.cmake.in to suppress that |
911 | +test completely. That makes sense for the header compilation tests, |
912 | +for example. |
913 | + |
914 | +If a specific test case in a test program causes a valgrind problem, |
915 | +you can selectively disable a section of code like this: |
916 | + |
917 | + #include <valgrind/valgrind.h> |
918 | + |
919 | + if (!RUNNING_ON_VALGRIND) |
920 | + { |
921 | + // Code here crashes valgrind... |
922 | + } |
923 | + |
924 | +That way, the test will still be run as part of the normal "make test" |
925 | +target, but will be ommitted when running "make valgrind". |
926 | + |
927 | +Coverage |
928 | +-------- |
929 | + |
930 | +To build with the flags for coverage testing enabled and get coverage: |
931 | + |
932 | + $ mkdir buildcoverage |
933 | + $ cd buildcoverage |
934 | + $ cmake -DCMAKE_BUILD_TYPE=coverage |
935 | + $ make |
936 | + $ make test |
937 | + $ make coverage |
938 | + |
939 | +Unfortunately, it is not possible to get 100% coverage for some files, |
940 | +mainly due to gcc's generation of two destructors for dynamic and non- |
941 | +dynamic instances. For abstract base classes and for classes that |
942 | +prevent stack and static allocation, this causes one of the destructors |
943 | +to be reported as uncovered. |
944 | + |
945 | +There are also issues with some functions in header files that are |
946 | +incorrectly reported as uncovered due to inlining, as well as |
947 | +the impossibility of covering defensive assert(false) statements, |
948 | +such as an assert in the default branch of a switch, where the |
949 | +switch is meant to handle all possible cases explicitly. |
950 | + |
951 | +If you run a binary and get lots of warnings about a "merge mismatch for summaries", |
952 | +this is caused by having made changes to the source that add or remove code |
953 | +that was previously run, so the new coverage output cannot sensibly be merged |
954 | +into the old coverage output. You can get rid of this problem by running |
955 | + |
956 | + $ make clean-coverage |
957 | + |
958 | +This deletes all the .gcda files, allowing the merge to (sometimes) succeed again. |
959 | +If this doesn't work either, the only remedy is to do a clean build. |
960 | + |
961 | +Code style |
962 | +---------- |
963 | + |
964 | +Please maintain the existing coding style. For details |
965 | +on the style, see lp:canonical-client-development-guidelines. |
966 | + |
967 | +We use a format tool that fixes a whole lot of issues |
968 | +regarding code style. See the HACKING file of lp:unity-scopes-api |
969 | +for details on the tool. |
970 | + |
971 | +Thread and address sanitizer |
972 | +---------------------------- |
973 | + |
974 | +Set SANITIZER to "thread" or "address" to build with the |
975 | +corresponding sanitizer enabled. |
976 | + |
977 | +Some libraries cause a cause a large number of warnings from |
978 | +thread sanitizer. The tsan-suppress file supresses the benign |
979 | +race conditions we currently know about. To run the tests |
980 | +(from the build directory) with these suppressions |
981 | +enabled, use: |
982 | + |
983 | + $ TSAN_OPTIONS="suppressions=../tsan-suppress" make test |
984 | + |
985 | +If a test runs too slowly under address sanitizer, you can |
986 | +hide a section of code from address sanitzer with: |
987 | + |
988 | +#if defined(__has_feature) |
989 | + #if !__has_feature(address_sanitizer) |
990 | + |
991 | + // Code here takes forever under address sanitizer... |
992 | + |
993 | + #endif |
994 | +#endif |
995 | |
996 | === added file 'cmake/XEnableCoverageReport.cmake' |
997 | --- cmake/XEnableCoverageReport.cmake 1970-01-01 00:00:00 +0000 |
998 | +++ cmake/XEnableCoverageReport.cmake 2015-06-23 06:06:30 +0000 |
999 | @@ -0,0 +1,203 @@ |
1000 | +# - Creates a special coverage build type and target on GCC. |
1001 | +# |
1002 | +# Defines a function ENABLE_COVERAGE_REPORT which generates the coverage target |
1003 | +# for selected targets. Optional arguments to this function are used to filter |
1004 | +# unwanted results using globbing expressions. Moreover targets with tests for |
1005 | +# the source code can be specified to trigger regenerating the report if the |
1006 | +# test has changed. |
1007 | +# |
1008 | +# ENABLE_COVERAGE_REPORT(TARGETS target... [FILTER filter...] [TESTS test targets...]) |
1009 | +# |
1010 | +# To generate a coverage report first build the project with |
1011 | +# CMAKE_BUILD_TYPE=coverage, then call make test and afterwards make coverage. |
1012 | +# |
1013 | +# The coverage report is based on gcov. Depending on the availability of lcov |
1014 | +# a HTML report will be generated and/or an XML report of gcovr is found. |
1015 | +# The generated coverage target executes all found solutions. Special targets |
1016 | +# exist to create e.g. only the xml report: coverage-xml. |
1017 | +# |
1018 | +# Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de> |
1019 | +# |
1020 | +# This file may be licensed under the terms of the |
1021 | +# GNU Lesser General Public License Version 3 (the ``LGPL''), |
1022 | +# or (at your option) any later version. |
1023 | +# |
1024 | +# Software distributed under the License is distributed |
1025 | +# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either |
1026 | +# express or implied. See the LGPL for the specific language |
1027 | +# governing rights and limitations. |
1028 | +# |
1029 | +# You should have received a copy of the LGPL along with this |
1030 | +# program. If not, go to http://www.gnu.org/licenses/lgpl.html |
1031 | +# or write to the Free Software Foundation, Inc., |
1032 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
1033 | +# |
1034 | +# The development of this software was supported by: |
1035 | +# CoR-Lab, Research Institute for Cognition and Robotics |
1036 | +# Bielefeld University |
1037 | + |
1038 | +FIND_PACKAGE(Lcov) |
1039 | +FIND_PACKAGE(gcovr) |
1040 | + |
1041 | +FUNCTION(SET_GCC_VERSION) |
1042 | + EXECUTE_PROCESS(COMMAND gcc -dumpversion OUTPUT_VARIABLE GCC_VERSION) |
1043 | + STRING(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION}) |
1044 | + LIST(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR) |
1045 | + LIST(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR) |
1046 | + STRING(LENGTH ${GCC_MAJOR} MINOR_LEN) |
1047 | + IF(${MINOR_LEN} EQUAL 1) |
1048 | + SET(GCC_MINOR "0${GCC_MINOR}") |
1049 | + ENDIF() |
1050 | + SET(GCC_VERSION "${GCC_MAJOR}${GCC_MINOR}" PARENT_SCOPE) |
1051 | +ENDFUNCTION() |
1052 | + |
1053 | +FUNCTION(ENABLE_COVERAGE_REPORT) |
1054 | + # argument parsing |
1055 | + SET(MULTI_VALUE_ARGS FILTER TARGETS TESTS) |
1056 | + CMAKE_PARSE_ARGUMENTS(ENABLE_COVERAGE_REPORT "" "" "${MULTI_VALUE_ARGS}" ${ARGN}) |
1057 | + |
1058 | + SET(COVERAGE_RAW_FILE "${CMAKE_BINARY_DIR}/coverage.raw.info") |
1059 | + SET(COVERAGE_FILTERED_FILE "${CMAKE_BINARY_DIR}/coverage.info") |
1060 | + SET(COVERAGE_REPORT_DIR "${CMAKE_BINARY_DIR}/coveragereport") |
1061 | + SET(COVERAGE_XML_FILE "${CMAKE_BINARY_DIR}/coverage.xml") |
1062 | + |
1063 | + # decide if there is any tool to create coverage data |
1064 | + SET(TOOL_FOUND FALSE) |
1065 | + IF(LCOV_FOUND OR GCOVR_FOUND) |
1066 | + SET(TOOL_FOUND TRUE) |
1067 | + ENDIF() |
1068 | + IF(NOT TOOL_FOUND) |
1069 | + MESSAGE(STATUS "Cannot enable coverage targets because neither lcov nor gcovr are found.") |
1070 | + ENDIF() |
1071 | + |
1072 | + STRING(TOLOWER "${CMAKE_BUILD_TYPE}" LOWER_CMAKE_BUILD_TYPE) |
1073 | + IF(TOOL_FOUND AND "${LOWER_CMAKE_BUILD_TYPE}" STREQUAL "coverage") |
1074 | + |
1075 | + MESSAGE(STATUS "Coverage support enabled for targets: ${ENABLE_COVERAGE_REPORT_TARGETS}") |
1076 | + |
1077 | + # create coverage build type |
1078 | + SET(CMAKE_CXX_FLAGS_COVERAGE ${CMAKE_CXX_FLAGS_DEBUG} PARENT_SCOPE) |
1079 | + SET(CMAKE_C_FLAGS_COVERAGE ${CMAKE_C_FLAGS_DEBUG} PARENT_SCOPE) |
1080 | + SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES} coverage PARENT_SCOPE) |
1081 | + |
1082 | + # instrument targets |
1083 | + |
1084 | + # If we are using Clang, tell it to generate coverage data suitable for gcovr. |
1085 | + IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
1086 | + SET_GCC_VERSION() |
1087 | + set(CLANG_COVERAGE_ARGS "-Xclang -coverage-cfg-checksum -Xclang -coverage-no-function-names-in-data -Xclang -coverage-version='${GCC_VERSION}*' ") |
1088 | + ENDIF() |
1089 | + |
1090 | + FOREACH(T ${ENABLE_COVERAGE_REPORT_TARGETS}) |
1091 | + SET_PROPERTY(TARGET ${T} APPEND_STRING PROPERTY COMPILE_FLAGS "-g --coverage ") |
1092 | + SET_PROPERTY(TARGET ${T} APPEND_STRING PROPERTY LINK_FLAGS "-g --coverage ") |
1093 | + SET_PROPERTY(TARGET ${T} APPEND_STRING PROPERTY COMPILE_FLAGS ${CLANG_COVERAGE_ARGS}) |
1094 | + ENDFOREACH() |
1095 | + |
1096 | + FOREACH(T ${ENABLE_COVERAGE_REPORT_TESTS}) |
1097 | + SET_PROPERTY(TARGET ${T} APPEND_STRING PROPERTY LINK_FLAGS "-g --coverage ") |
1098 | + ENDFOREACH() |
1099 | + |
1100 | + # html report |
1101 | + IF (LCOV_FOUND) |
1102 | + |
1103 | + MESSAGE(STATUS "Enabling HTML coverage report") |
1104 | + |
1105 | + # set up coverage target |
1106 | + |
1107 | + ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_RAW_FILE} |
1108 | + COMMAND ${LCOV_EXECUTABLE} -c -d ${CMAKE_BINARY_DIR} -o ${COVERAGE_RAW_FILE} |
1109 | + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
1110 | + COMMENT "Collecting coverage data" |
1111 | + DEPENDS ${ENABLE_COVERAGE_REPORT_TARGETS} ${ENABLE_COVERAGE_REPORT_TESTS} |
1112 | + VERBATIM) |
1113 | + |
1114 | + # filter unwanted stuff |
1115 | + LIST(LENGTH ENABLE_COVERAGE_REPORT_FILTER FILTER_LENGTH) |
1116 | + IF(${FILTER_LENGTH} GREATER 0) |
1117 | + SET(FILTER COMMAND ${LCOV_EXECUTABLE} -r ${COVERAGE_FILTERED_FILE} ${ENABLE_COVERAGE_REPORT_FILTER} -o ${COVERAGE_FILTERED_FILE}) |
1118 | + ELSE() |
1119 | + SET(FILTER "") |
1120 | + ENDIF() |
1121 | + |
1122 | + ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_FILTERED_FILE} |
1123 | + COMMAND ${LCOV_EXECUTABLE} -e ${COVERAGE_RAW_FILE} "${CMAKE_SOURCE_DIR}*" -o ${COVERAGE_FILTERED_FILE} |
1124 | + ${FILTER} |
1125 | + DEPENDS ${COVERAGE_RAW_FILE} |
1126 | + COMMENT "Filtering recorded coverage data for project-relevant entries" |
1127 | + VERBATIM) |
1128 | + ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_REPORT_DIR} |
1129 | + COMMAND ${CMAKE_COMMAND} -E make_directory ${COVERAGE_REPORT_DIR} |
1130 | + COMMAND ${GENHTML_EXECUTABLE} --legend --show-details -t "${PROJECT_NAME} test coverage" -o ${COVERAGE_REPORT_DIR} ${COVERAGE_FILTERED_FILE} |
1131 | + DEPENDS ${COVERAGE_FILTERED_FILE} |
1132 | + COMMENT "Generating HTML coverage report in ${COVERAGE_REPORT_DIR}" |
1133 | + VERBATIM) |
1134 | + |
1135 | + ADD_CUSTOM_TARGET(coverage-html |
1136 | + DEPENDS ${COVERAGE_REPORT_DIR}) |
1137 | + |
1138 | + ENDIF() |
1139 | + |
1140 | + # xml coverage report |
1141 | + IF(GCOVR_FOUND) |
1142 | + |
1143 | + MESSAGE(STATUS "Enabling XML coverage report") |
1144 | + |
1145 | + # filter unwanted stuff |
1146 | + SET(GCOVR_FILTER "") |
1147 | + FOREACH(F ${ENABLE_COVERAGE_REPORT_FILTER}) |
1148 | + # Convert the pattern to a regexp using the same rules |
1149 | + # as lcov's transform_pattern() function. |
1150 | + STRING(REPLACE "\\" "\\\\" F ${F}) |
1151 | + STRING(REPLACE "/" "\\/" F ${F}) |
1152 | + STRING(REPLACE "^" "\\^" F ${F}) |
1153 | + STRING(REPLACE "\$" "\\\$" F ${F}) |
1154 | + STRING(REPLACE "(" "\\(" F ${F}) |
1155 | + STRING(REPLACE ")" "\\)" F ${F}) |
1156 | + STRING(REPLACE "[" "\\[" F ${F}) |
1157 | + STRING(REPLACE "]" "\\]" F ${F}) |
1158 | + STRING(REPLACE "{" "\\{" F ${F}) |
1159 | + STRING(REPLACE "}" "\\}" F ${F}) |
1160 | + STRING(REPLACE "." "\\." F ${F}) |
1161 | + STRING(REPLACE "," "\\," F ${F}) |
1162 | + STRING(REPLACE "|" "\\|" F ${F}) |
1163 | + STRING(REPLACE "+" "\\+" F ${F}) |
1164 | + STRING(REPLACE "!" "\\!" F ${F}) |
1165 | + |
1166 | + STRING(REPLACE "*" ".*" F ${F}) |
1167 | + STRING(REPLACE "?" "." F ${F}) |
1168 | + |
1169 | + SET(GCOVR_FILTER ${GCOVR_FILTER} -e "^${F}\$") |
1170 | + ENDFOREACH() |
1171 | + |
1172 | + ADD_CUSTOM_COMMAND(OUTPUT ${COVERAGE_XML_FILE} |
1173 | + COMMAND "${GCOVR_EXECUTABLE}" -x -r "${CMAKE_SOURCE_DIR}" ${GCOVR_FILTER} -o "${COVERAGE_XML_FILE}" |
1174 | + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" |
1175 | + COMMENT "Generating coverage XML report" |
1176 | + VERBATIM) |
1177 | + |
1178 | + ADD_CUSTOM_TARGET(coverage-xml |
1179 | + DEPENDS ${COVERAGE_XML_FILE}) |
1180 | + |
1181 | + ENDIF() |
1182 | + |
1183 | + # provide a global coverage target executing both steps if available |
1184 | + SET(GLOBAL_DEPENDS "") |
1185 | + IF(LCOV_FOUND) |
1186 | + LIST(APPEND GLOBAL_DEPENDS ${COVERAGE_REPORT_DIR}) |
1187 | + ENDIF() |
1188 | + IF(GCOVR_FOUND) |
1189 | + LIST(APPEND GLOBAL_DEPENDS ${COVERAGE_XML_FILE}) |
1190 | + ENDIF() |
1191 | + IF(LCOV_FOUND OR GCOVR_FOUND) |
1192 | + ADD_CUSTOM_TARGET(coverage |
1193 | + DEPENDS ${GLOBAL_DEPENDS}) |
1194 | + ENDIF() |
1195 | + |
1196 | + ENDIF() |
1197 | + |
1198 | + # This gets rid of any stale .gcda files. Run this if a running a binary causes lots of |
1199 | + # messages about about a "merge mismatch for summaries". |
1200 | + ADD_CUSTOM_TARGET(clean-coverage COMMAND find ${CMAKE_BINARY_DIR} -name '*.gcda' | xargs rm -f) |
1201 | + |
1202 | +ENDFUNCTION() |
1203 | |
1204 | === removed file 'cmake/coverage.cmake' |
1205 | --- cmake/coverage.cmake 2013-09-30 12:17:23 +0000 |
1206 | +++ cmake/coverage.cmake 1970-01-01 00:00:00 +0000 |
1207 | @@ -1,34 +0,0 @@ |
1208 | -if (CMAKE_BUILD_TYPE MATCHES coverage) |
1209 | - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage") |
1210 | - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") |
1211 | - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage") |
1212 | - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") |
1213 | - |
1214 | - find_program(GCOVR_EXECUTABLE gcovr HINTS ${GCOVR_ROOT} "${GCOVR_ROOT}/bin") |
1215 | - if (NOT GCOVR_EXECUTABLE) |
1216 | - message(STATUS "Gcovr binary was not found, can not generate XML coverage info.") |
1217 | - else () |
1218 | - message(STATUS "Gcovr found, can generate XML coverage info.") |
1219 | - add_custom_target (coverage-xml |
1220 | - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
1221 | - COMMAND "${GCOVR_EXECUTABLE}" --exclude="test.*" -x -r "${CMAKE_SOURCE_DIR}" |
1222 | - --object-directory=${CMAKE_BINARY_DIR} -o coverage.xml) |
1223 | - endif() |
1224 | - |
1225 | - find_program(LCOV_EXECUTABLE lcov HINTS ${LCOV_ROOT} "${GCOVR_ROOT}/bin") |
1226 | - find_program(GENHTML_EXECUTABLE genhtml HINTS ${GENHTML_ROOT}) |
1227 | - if (NOT LCOV_EXECUTABLE) |
1228 | - message(STATUS "Lcov binary was not found, can not generate HTML coverage info.") |
1229 | - else () |
1230 | - if(NOT GENHTML_EXECUTABLE) |
1231 | - message(STATUS "Genthml binary not found, can not generate HTML coverage info.") |
1232 | - else() |
1233 | - message(STATUS "Lcov and genhtml found, can generate HTML coverage info.") |
1234 | - add_custom_target (coverage-html |
1235 | - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
1236 | - COMMAND "${LCOV_EXECUTABLE}" --directory ${CMAKE_BINARY_DIR} --capture --output-file coverage.info --no-checksum |
1237 | - COMMAND "${GENHTML_EXECUTABLE}" --prefix ${CMAKE_BINARY_DIR} --output-directory coveragereport --title "Code Coverage" --legend --show-details coverage.info |
1238 | - ) |
1239 | - endif() |
1240 | - endif() |
1241 | -endif() |
1242 | |
1243 | === modified file 'data/CMakeLists.txt' |
1244 | --- data/CMakeLists.txt 2014-09-26 11:24:27 +0000 |
1245 | +++ data/CMakeLists.txt 2015-06-23 06:06:30 +0000 |
1246 | @@ -22,4 +22,3 @@ |
1247 | "${CMAKE_CURRENT_BINARY_DIR}/thumbnailer" |
1248 | DESTINATION "${CMAKE_INSTALL_SYSCONFDIR}/apport/blacklist.d" |
1249 | ) |
1250 | - |
1251 | |
1252 | === modified file 'data/com.canonical.Unity.Thumbnailer.gschema.xml' |
1253 | --- data/com.canonical.Unity.Thumbnailer.gschema.xml 2014-09-16 13:00:17 +0000 |
1254 | +++ data/com.canonical.Unity.Thumbnailer.gschema.xml 2015-06-23 06:06:30 +0000 |
1255 | @@ -6,5 +6,72 @@ |
1256 | <summary>API key for dash.ubuntu.com service</summary> |
1257 | <description>API key for dash.ubuntu.com service</description> |
1258 | </key> |
1259 | + |
1260 | + <key type="i" name="full-size-cache-size"> |
1261 | + <default>50</default> |
1262 | + <summary>Size of the full-size image cache in megabytes</summary> |
1263 | + <description>The full-size cache is used to store full-size extracted media artwork and downloaded album art.</description> |
1264 | + </key> |
1265 | + |
1266 | + <key type="i" name="thumbnail-cache-size"> |
1267 | + <default>100</default> |
1268 | + <summary>Size of the thumbnail image cache in megabytes</summary> |
1269 | + <description>The thumbnail image cache stores scaled thumbnail images.</description> |
1270 | + </key> |
1271 | + |
1272 | + <key type="i" name="failure-cache-size"> |
1273 | + <default>2</default> |
1274 | + <summary>Size of the failure cache in megabytes</summary> |
1275 | + <description>The failure cache records information about failed downloads or failed thumbnail extractions.</description> |
1276 | + </key> |
1277 | + |
1278 | + <key type="i" name="max-thumbnail-size"> |
1279 | + <default>1920</default> |
1280 | + <summary>Maximum size in pixels for a thumbnail</summary> |
1281 | + <description> |
1282 | + Requests for thumbnails larger than this will automatically reduce the thumbnail to max-thumbnail-size in the larger dimension. An exception are requests for local thumbnails with size 0x0, which are returned in their original size. |
1283 | + </description> |
1284 | + </key> |
1285 | + |
1286 | + <key type="i" name="retry-not-found-hours"> |
1287 | + <default>168</default> |
1288 | + <summary>Time to wait before re-trying for remote artwork that did not exist</summary> |
1289 | + <description> |
1290 | + If artwork cannot be retrieved because the remote server authoritavely confirmed that no artwork exists for an artist and album, this parameter defines how long the thumbnailer waits before trying to download the same image again. The default is 168 hours (one week). |
1291 | + </description> |
1292 | + </key> |
1293 | + |
1294 | + <key type="i" name="retry-error-hours"> |
1295 | + <default>2</default> |
1296 | + <summary>Time to wait before re-trying remote artwork that failed to download</summary> |
1297 | + <description> |
1298 | + If artwork cannot be retrieved from the remote server due to an unexpected error (such as the server not responding), this parameter defines how long the thumbnailer waits before trying to download the same image again. The default is two hours. |
1299 | + </description> |
1300 | + </key> |
1301 | + |
1302 | + <key type="i" name="max-downloads"> |
1303 | + <default>2</default> |
1304 | + <summary>Maximum number of concurrent downloads</summary> |
1305 | + <description> |
1306 | + This parameter sets the maximum number of concurrent downloads for remote artwork. |
1307 | + </description> |
1308 | + </key> |
1309 | + |
1310 | + <key type="i" name="max-extractions"> |
1311 | + <default>0</default> |
1312 | + <summary>Maximum number of concurrent image extractions</summary> |
1313 | + <description> |
1314 | + This parameter sets the maximum number of concurrent image extractions from local media files. The default value is zero, which sets the value according to the number of CPU cores. |
1315 | + </description> |
1316 | + </key> |
1317 | + |
1318 | + <key type="i" name="extraction-timeout"> |
1319 | + <default>10</default> |
1320 | + <summary>Maximum amount of time to wait for an image extraction or download (in seconds)</summary> |
1321 | + <description> |
1322 | + This parameter sets the amount of time (in seconds) to wait for a remote image download or |
1323 | + a thumbnail extraction before giving up. |
1324 | + </description> |
1325 | + </key> |
1326 | </schema> |
1327 | </schemalist> |
1328 | |
1329 | === modified file 'debian/changelog' |
1330 | --- debian/changelog 2015-03-12 08:38:44 +0000 |
1331 | +++ debian/changelog 2015-06-23 06:06:30 +0000 |
1332 | @@ -1,3 +1,11 @@ |
1333 | +thumbnailer (1.4-0ubuntu0) UNRELEASED; urgency=medium |
1334 | + |
1335 | + * Switch to new cache implementation |
1336 | + * Remove the libthumbnailer0 and libthumbnailer-dev packages, since |
1337 | + that code is now statically linked into the D-Bus service. |
1338 | + |
1339 | + -- James Henstridge <james.henstridge@canonical.com> Tue, 21 Apr 2015 14:17:52 +0800 |
1340 | + |
1341 | thumbnailer (1.3+15.04.20150312-0ubuntu1) vivid; urgency=medium |
1342 | |
1343 | [ Albert Astals Cid ] |
1344 | |
1345 | === modified file 'debian/control' |
1346 | --- debian/control 2014-09-25 07:23:54 +0000 |
1347 | +++ debian/control 2015-06-23 06:06:30 +0000 |
1348 | @@ -4,57 +4,42 @@ |
1349 | Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com> |
1350 | Standards-Version: 3.9.4 |
1351 | Build-Depends: cmake, |
1352 | + cmake-extras, |
1353 | debhelper (>= 9), |
1354 | gstreamer1.0-plugins-good, |
1355 | + libapparmor-dev, |
1356 | + libboost-filesystem-dev, |
1357 | libexif-dev, |
1358 | libgdk-pixbuf2.0-dev, |
1359 | libgstreamer1.0-dev, |
1360 | libgstreamer-plugins-base1.0-dev, |
1361 | libgtest-dev, |
1362 | - libsoup2.4-dev, |
1363 | - libxml2-dev, |
1364 | + libleveldb-dev, |
1365 | + libqtdbustest1-dev, |
1366 | + libunity-api-dev, |
1367 | + python3-tornado, |
1368 | + qml-module-qtquick2, |
1369 | + qml-module-qttest, |
1370 | qt5-default, |
1371 | qtbase5-dev, |
1372 | qtbase5-dev-tools, |
1373 | qtdeclarative5-dev, |
1374 | shared-mime-info, |
1375 | + xvfb, |
1376 | Homepage: https://launchpad.net/thumbnailer |
1377 | # if you don't have have commit access to this branch but would like to upload |
1378 | # directly to Ubuntu, don't worry: your changes will be merged back into the |
1379 | # upstream branch |
1380 | Vcs-Bzr: lp:thumbnailer |
1381 | |
1382 | -Package: libthumbnailer0 |
1383 | -Architecture: any |
1384 | -Multi-Arch: same |
1385 | -Pre-Depends: ${misc:Pre-Depends}, |
1386 | -Depends: ${misc:Depends}, |
1387 | - ${shlibs:Depends}, |
1388 | -Description: generate thumbnails from files |
1389 | - Thumbnailer is a shared library that generates |
1390 | - thumbnail images for multiple file types such |
1391 | - as video and audio files. |
1392 | - |
1393 | -Package: libthumbnailer-dev |
1394 | -Architecture: any |
1395 | -Multi-Arch: same |
1396 | -Pre-Depends: ${misc:Pre-Depends}, |
1397 | -Depends: ${misc:Depends}, |
1398 | - ${shlibs:Depends}, |
1399 | - gstreamer1.0-plugins-base, |
1400 | - gstreamer1.0-plugins-good, |
1401 | - libthumbnailer0 (= ${binary:Version}), |
1402 | -Description: development files for thumbnailer |
1403 | - This package contains development files |
1404 | - for the thumbnailer package. |
1405 | - |
1406 | Package: thumbnailer-service |
1407 | Architecture: any |
1408 | Multi-Arch: foreign |
1409 | Pre-Depends: ${misc:Pre-Depends}, |
1410 | Depends: ${misc:Depends}, |
1411 | ${shlibs:Depends}, |
1412 | - libthumbnailer0 (= ${binary:Version}), |
1413 | +Breaks: libthumbnailer0 (<< 1.4~) |
1414 | +Replaces: libthumbnailer0 (<< 1.4~) |
1415 | Description: D-Bus service for out of process thumbnailing |
1416 | This package provides a D-Bus service that can provide thumbnails on |
1417 | behalf of another process. |
1418 | @@ -66,7 +51,6 @@ |
1419 | Depends: ${misc:Depends}, |
1420 | ${shlibs:Depends}, |
1421 | thumbnailer-common (= ${binary:Version}), |
1422 | - libthumbnailer0 (= ${binary:Version}), |
1423 | Provides: ubuntu-thumbnailer-impl, |
1424 | ubuntu-thumbnailer-impl-0, |
1425 | Recommends: thumbnailer-service (= ${binary:Version}), |
1426 | |
1427 | === modified file 'debian/copyright' |
1428 | --- debian/copyright 2013-09-30 15:55:07 +0000 |
1429 | +++ debian/copyright 2015-06-23 06:06:30 +0000 |
1430 | @@ -3,7 +3,27 @@ |
1431 | Source: https://launchpad.net/thumbnailer |
1432 | |
1433 | Files: * |
1434 | -Copyright: 2013 Canonical ltd |
1435 | +Copyright: 2015 Canonical Ltd. |
1436 | +License: GPL-3 |
1437 | + This program is free software: you can redistribute it and/or modify |
1438 | + it under the terms of the GNU General Public License version 3 as |
1439 | + published by the Free Software Foundation. |
1440 | + . |
1441 | + This program is distributed in the hope that it will be useful, |
1442 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
1443 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1444 | + GNU General Public License for more details. |
1445 | + . |
1446 | + On Debian/Ubuntu systems, the full text of the GPL v3 can be found in |
1447 | + `/usr/share/common-licenses/GPL-3' |
1448 | + |
1449 | +Files: plugins/* |
1450 | + src/core/* |
1451 | + include/core/* |
1452 | + tests/core/* include/internal/gobj_memory.h |
1453 | + include/internal/safe_strerror.h |
1454 | + src/safe_strerror.cpp |
1455 | +Copyright: 2015 Canonical ltd |
1456 | License: LGPL-3 |
1457 | This program is free software: you can redistribute it and/or modify |
1458 | it under the terms of version 3 of the GNU Lesser General Public |
1459 | @@ -12,7 +32,7 @@ |
1460 | This program is distributed in the hope that it will be useful, |
1461 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
1462 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1463 | - GNU General Public License for more details. |
1464 | + GNU Lesser General Public License for more details. |
1465 | . |
1466 | On Debian systems, the full text of the GNU Lesser General Public |
1467 | License version 3 can be found in the file |
1468 | |
1469 | === removed file 'debian/libthumbnailer-dev.install' |
1470 | --- debian/libthumbnailer-dev.install 2013-09-25 12:45:32 +0000 |
1471 | +++ debian/libthumbnailer-dev.install 1970-01-01 00:00:00 +0000 |
1472 | @@ -1,3 +0,0 @@ |
1473 | -usr/include/* |
1474 | -usr/lib/*/lib*.so |
1475 | -usr/lib/*/pkgconfig/* |
1476 | |
1477 | === removed file 'debian/libthumbnailer0.install' |
1478 | --- debian/libthumbnailer0.install 2014-09-26 11:24:27 +0000 |
1479 | +++ debian/libthumbnailer0.install 1970-01-01 00:00:00 +0000 |
1480 | @@ -1,2 +0,0 @@ |
1481 | -usr/lib/*/libthumb*.so.* |
1482 | -usr/lib/*/thumbnailer/vs-thumb |
1483 | |
1484 | === removed file 'debian/libthumbnailer0.symbols' |
1485 | --- debian/libthumbnailer0.symbols 2014-08-11 14:20:31 +0000 |
1486 | +++ debian/libthumbnailer0.symbols 1970-01-01 00:00:00 +0000 |
1487 | @@ -1,7 +0,0 @@ |
1488 | -libthumbnailer.so.0 libthumbnailer0 #MINVER# |
1489 | - (c++)"Thumbnailer::get_thumbnail(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ThumbnailSize)@Base" 1.0 |
1490 | - (c++)"Thumbnailer::get_thumbnail(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ThumbnailSize, ThumbnailPolicy)@Base" 1.0 |
1491 | - (c++)"Thumbnailer::get_album_art(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ThumbnailSize, ThumbnailPolicy)@Base" 1.0 |
1492 | - (c++)"Thumbnailer::Thumbnailer()@Base" 1.0 |
1493 | - (c++)"Thumbnailer::~Thumbnailer()@Base" 1.0 |
1494 | - (c++)"Thumbnailer::get_artist_art(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, ThumbnailSize, ThumbnailPolicy)@Base" 1.1+14.10.20140811 |
1495 | |
1496 | === modified file 'debian/rules' |
1497 | --- debian/rules 2014-09-25 08:54:10 +0000 |
1498 | +++ debian/rules 2015-06-23 06:06:30 +0000 |
1499 | @@ -15,3 +15,6 @@ |
1500 | override_dh_auto_configure: |
1501 | dh_auto_configure -- -DCMAKE_INSTALL_SYSCONFDIR=/etc |
1502 | |
1503 | +# Tests are not written to be run in parallel. |
1504 | +override_dh_auto_test: |
1505 | + dh_auto_test --max-parallel=1 -- ARGS="--verbose" |
1506 | |
1507 | === modified file 'debian/thumbnailer-service.install' |
1508 | --- debian/thumbnailer-service.install 2014-09-16 12:12:21 +0000 |
1509 | +++ debian/thumbnailer-service.install 2015-06-23 06:06:30 +0000 |
1510 | @@ -1,3 +1,5 @@ |
1511 | +usr/bin/thumbnailer-admin |
1512 | usr/lib/*/thumbnailer/thumbnailer-service |
1513 | +usr/lib/*/thumbnailer/vs-thumb |
1514 | usr/share/dbus-1/services/com.canonical.Thumbnailer.service |
1515 | usr/share/glib-2.0/schemas/* |
1516 | |
1517 | === modified file 'include/CMakeLists.txt' |
1518 | --- include/CMakeLists.txt 2013-10-02 09:43:16 +0000 |
1519 | +++ include/CMakeLists.txt 2015-06-23 06:06:30 +0000 |
1520 | @@ -1,12 +1,1 @@ |
1521 | add_subdirectory(internal) |
1522 | - |
1523 | -install(FILES thumbnailer.h DESTINATION include) |
1524 | - |
1525 | -# Build and install a pkg-config file |
1526 | -set(prefix ${CMAKE_INSTALL_PREFIX}) |
1527 | -set(exec_prefix ${prefix}/bin) |
1528 | -set(libdir ${prefix}/${LIBDIR}) |
1529 | -set(includedir ${prefix}/include) |
1530 | -set(pkg-name "lib${COL_LIB_BASENAME}") |
1531 | -configure_file(thumbnailer.pc.in thumbnailer.pc @ONLY) |
1532 | -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/thumbnailer.pc DESTINATION ${LIBDIR}/pkgconfig) |
1533 | |
1534 | === added directory 'include/core' |
1535 | === added file 'include/core/CMakeLists.txt' |
1536 | === added file 'include/core/cache_codec.h' |
1537 | --- include/core/cache_codec.h 1970-01-01 00:00:00 +0000 |
1538 | +++ include/core/cache_codec.h 2015-06-23 06:06:30 +0000 |
1539 | @@ -0,0 +1,50 @@ |
1540 | +/* |
1541 | + * Copyright (C) 2015 Canonical Ltd. |
1542 | + * |
1543 | + * This program is free software: you can redistribute it and/or modify |
1544 | + * it under the terms of the GNU Lesser General Public License version 3 as |
1545 | + * published by the Free Software Foundation. |
1546 | + * |
1547 | + * This program is distributed in the hope that it will be useful, |
1548 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1549 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1550 | + * GNU Lesser General Public License for more details. |
1551 | + * |
1552 | + * You should have received a copy of the GNU Lesser General Public License |
1553 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1554 | + * |
1555 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
1556 | + */ |
1557 | + |
1558 | +#pragma once |
1559 | + |
1560 | +#include <string> |
1561 | + |
1562 | +namespace core |
1563 | +{ |
1564 | + |
1565 | +/** |
1566 | +Traits for serialization and deserialization of cache custom types. |
1567 | +To use custom types, specialize this template |
1568 | +for each custom type (other than string) in the `core` namespace. |
1569 | + |
1570 | +\warning Do _not_ specialize this struct for `std::string`! |
1571 | +Doing so has no effect. |
1572 | + |
1573 | +\see PersistentCache |
1574 | +*/ |
1575 | + |
1576 | +template <typename T> |
1577 | +struct CacheCodec |
1578 | +{ |
1579 | + /** |
1580 | + \brief Converts a value of custom type T into a string. |
1581 | + */ |
1582 | + static std::string encode(T const& value); |
1583 | + /** |
1584 | + \brief Converts a string into a value of custom type T. |
1585 | + */ |
1586 | + static T decode(std::string const& s); |
1587 | +}; |
1588 | + |
1589 | +} // namespace core |
1590 | |
1591 | === added file 'include/core/cache_discard_policy.h' |
1592 | --- include/core/cache_discard_policy.h 1970-01-01 00:00:00 +0000 |
1593 | +++ include/core/cache_discard_policy.h 2015-06-23 06:06:30 +0000 |
1594 | @@ -0,0 +1,41 @@ |
1595 | +/* |
1596 | + * Copyright (C) 2015 Canonical Ltd. |
1597 | + * |
1598 | + * This program is free software: you can redistribute it and/or modify |
1599 | + * it under the terms of the GNU Lesser General Public License version 3 as |
1600 | + * published by the Free Software Foundation. |
1601 | + * |
1602 | + * This program is distributed in the hope that it will be useful, |
1603 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1604 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1605 | + * GNU Lesser General Public License for more details. |
1606 | + * |
1607 | + * You should have received a copy of the GNU Lesser General Public License |
1608 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1609 | + * |
1610 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
1611 | + */ |
1612 | + |
1613 | +#pragma once |
1614 | + |
1615 | +namespace core |
1616 | +{ |
1617 | + |
1618 | +/** |
1619 | +\brief Indicates the discard policy to make room for entries when the cache is full. |
1620 | + |
1621 | +Once the cache is full and another entry is added, |
1622 | +`lru_ttl` unconditionally deletes all entries that have expired and then, |
1623 | +if deleting these entries did not create sufficient free space, deletes entries |
1624 | +in least-recently-used order until enough space is available. |
1625 | + |
1626 | +If the discard policy is set to `lru_only`, entries do not maintain an expiry time and |
1627 | +are therefore discarded strictly in LRU order. |
1628 | +*/ |
1629 | +enum class CacheDiscardPolicy |
1630 | +{ |
1631 | + lru_ttl, ///< Evict expired entries first, followed by eviction in LRU order |
1632 | + lru_only ///< Evict in LRU order |
1633 | +}; |
1634 | + |
1635 | +} // namespace core |
1636 | |
1637 | === added file 'include/core/cache_events.h' |
1638 | --- include/core/cache_events.h 1970-01-01 00:00:00 +0000 |
1639 | +++ include/core/cache_events.h 2015-06-23 06:06:30 +0000 |
1640 | @@ -0,0 +1,103 @@ |
1641 | +/* |
1642 | + * Copyright (C) 2015 Canonical Ltd. |
1643 | + * |
1644 | + * This program is free software: you can redistribute it and/or modify |
1645 | + * it under the terms of the GNU Lesser General Public License version 3 as |
1646 | + * published by the Free Software Foundation. |
1647 | + * |
1648 | + * This program is distributed in the hope that it will be useful, |
1649 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1650 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1651 | + * GNU Lesser General Public License for more details. |
1652 | + * |
1653 | + * You should have received a copy of the GNU Lesser General Public License |
1654 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1655 | + * |
1656 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
1657 | + */ |
1658 | + |
1659 | +#pragma once |
1660 | + |
1661 | +#include <cstdint> |
1662 | +#include <type_traits> |
1663 | + |
1664 | +/** |
1665 | +\brief Top-level namespace for core functionality. |
1666 | +*/ |
1667 | + |
1668 | +namespace core |
1669 | +{ |
1670 | + |
1671 | +/** |
1672 | +\brief Event types that can be monitored. |
1673 | +*/ |
1674 | + |
1675 | +// Note: Any change here must have a corresponding change to |
1676 | +// CacheEventIndex in core/internal/cache_event_indexes.h! |
1677 | + |
1678 | +enum class CacheEvent : uint32_t |
1679 | +{ |
1680 | + get = 1 << 0, ///< An entry was returned by a call to `get()`, `get_or_put()`, `take()`, or `take_data()`. |
1681 | + put = 1 << 1, ///< An entry was added by a call to `put()` or `get_or_put()`. |
1682 | + invalidate = 1 << 2, ///< An entry was removed by a call to `invalidate()`, `take()`, or `take_data()`. |
1683 | + touch = 1 << 3, ///< An entry was refreshed by a call to `touch()`. |
1684 | + miss = 1 << 4, ///< A call to `get()`, `get_or_put()`, `take()`, or `take_data()` failed to return an entry. |
1685 | + evict_ttl = 1 << 5, ///< An expired entry was evicted due to a call to `put()`, |
1686 | + ///< `get_or_put()`, `trim_to()`, or `resize()`. |
1687 | + evict_lru = 1 << 6, ///< The oldest entry was evicted due to a call to `put()`, |
1688 | + ///< `get_or_put()`, `trim_to()`, or `resize()`. |
1689 | + END_ = 1 << 7 ///< End marker |
1690 | +}; |
1691 | + |
1692 | +/** |
1693 | +\brief Returns the bitwise OR of two event types. |
1694 | +*/ |
1695 | +inline CacheEvent operator|(CacheEvent left, CacheEvent right) |
1696 | +{ |
1697 | + auto l = std::underlying_type<CacheEvent>::type(left); |
1698 | + auto r = std::underlying_type<CacheEvent>::type(right); |
1699 | + return CacheEvent(l | r); |
1700 | +} |
1701 | + |
1702 | +/** |
1703 | +\brief Assigns the bitwise OR of `left` and `right` to `left`. |
1704 | +*/ |
1705 | +inline CacheEvent& operator|=(CacheEvent& left, CacheEvent right) |
1706 | +{ |
1707 | + return left = left | right; |
1708 | +} |
1709 | + |
1710 | +/** |
1711 | +\brief Returns the bitwise AND of two event types. |
1712 | +*/ |
1713 | +inline CacheEvent operator&(CacheEvent left, CacheEvent right) |
1714 | +{ |
1715 | + auto l = std::underlying_type<CacheEvent>::type(left); |
1716 | + auto r = std::underlying_type<CacheEvent>::type(right); |
1717 | + return CacheEvent(l & r); |
1718 | +} |
1719 | + |
1720 | +/** |
1721 | +\brief Assigns the bitwise AND of `left` and `right` to `left`. |
1722 | +*/ |
1723 | +inline CacheEvent& operator&=(CacheEvent& left, CacheEvent right) |
1724 | +{ |
1725 | + return left = left & right; |
1726 | +} |
1727 | + |
1728 | +/** |
1729 | +\brief Returns the bitwise NOT of `ev`. Unused bits are set to zero. |
1730 | +*/ |
1731 | +inline CacheEvent operator~(CacheEvent ev) |
1732 | +{ |
1733 | + auto mask = std::underlying_type<CacheEvent>::type(CacheEvent::END_) - 1; |
1734 | + auto event = std::underlying_type<CacheEvent>::type(ev); |
1735 | + return CacheEvent(~event & mask); |
1736 | +} |
1737 | + |
1738 | +/** |
1739 | +\brief Convenience definition for all event types. |
1740 | +*/ |
1741 | +static constexpr auto AllCacheEvents = CacheEvent(std::underlying_type<CacheEvent>::type(CacheEvent::END_) - 1); |
1742 | + |
1743 | +} // namespace core |
1744 | |
1745 | === added directory 'include/core/internal' |
1746 | === added file 'include/core/internal/cache_event_indexes.h' |
1747 | --- include/core/internal/cache_event_indexes.h 1970-01-01 00:00:00 +0000 |
1748 | +++ include/core/internal/cache_event_indexes.h 2015-06-23 06:06:30 +0000 |
1749 | @@ -0,0 +1,46 @@ |
1750 | +/* |
1751 | + * Copyright (C) 2015 Canonical Ltd. |
1752 | + * |
1753 | + * This program is free software: you can redistribute it and/or modify |
1754 | + * it under the terms of the GNU Lesser General Public License version 3 as |
1755 | + * published by the Free Software Foundation. |
1756 | + * |
1757 | + * This program is distributed in the hope that it will be useful, |
1758 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1759 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1760 | + * GNU Lesser General Public License for more details. |
1761 | + * |
1762 | + * You should have received a copy of the GNU Lesser General Public License |
1763 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1764 | + * |
1765 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
1766 | + */ |
1767 | + |
1768 | +#pragma once |
1769 | + |
1770 | +#include <cstdint> |
1771 | + |
1772 | +namespace core |
1773 | +{ |
1774 | + |
1775 | +namespace internal |
1776 | +{ |
1777 | + |
1778 | +// Note: Any change here must have a corresponding change to |
1779 | +// CacheEvent in core/cache_events.h! |
1780 | + |
1781 | +enum class CacheEventIndex : uint32_t |
1782 | +{ |
1783 | + get = 0, |
1784 | + put = 1, |
1785 | + invalidate = 2, |
1786 | + touch = 3, |
1787 | + miss = 4, |
1788 | + evict_ttl = 5, |
1789 | + evict_lru = 6, |
1790 | + END_ = 7 |
1791 | +}; |
1792 | + |
1793 | +} // namespace internal |
1794 | + |
1795 | +} // namespace core |
1796 | |
1797 | === added file 'include/core/internal/persistent_string_cache_impl.h' |
1798 | --- include/core/internal/persistent_string_cache_impl.h 1970-01-01 00:00:00 +0000 |
1799 | +++ include/core/internal/persistent_string_cache_impl.h 2015-06-23 06:06:30 +0000 |
1800 | @@ -0,0 +1,191 @@ |
1801 | +/* |
1802 | + * Copyright (C) 2015 Canonical Ltd. |
1803 | + * |
1804 | + * This program is free software: you can redistribute it and/or modify |
1805 | + * it under the terms of the GNU Lesser General Public License version 3 as |
1806 | + * published by the Free Software Foundation. |
1807 | + * |
1808 | + * This program is distributed in the hope that it will be useful, |
1809 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1810 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1811 | + * GNU Lesser General Public License for more details. |
1812 | + * |
1813 | + * You should have received a copy of the GNU Lesser General Public License |
1814 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1815 | + * |
1816 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
1817 | + */ |
1818 | + |
1819 | +#pragma once |
1820 | + |
1821 | +#include <core/internal/cache_event_indexes.h> |
1822 | +#include <core/persistent_string_cache.h> |
1823 | + |
1824 | +#include <leveldb/db.h> |
1825 | + |
1826 | +#include <mutex> |
1827 | +#include <sstream> |
1828 | + |
1829 | +namespace core |
1830 | +{ |
1831 | + |
1832 | +namespace internal |
1833 | +{ |
1834 | + |
1835 | +class PersistentStringCacheStats; |
1836 | + |
1837 | +class PersistentStringCacheImpl |
1838 | +{ |
1839 | +public: |
1840 | + PersistentStringCacheImpl(std::string const& cache_path, |
1841 | + int64_t max_size_in_bytes, |
1842 | + core::CacheDiscardPolicy policy, |
1843 | + PersistentStringCache* pimpl = nullptr); |
1844 | + PersistentStringCacheImpl(std::string const& cache_path, PersistentStringCache* pimpl = nullptr); |
1845 | + |
1846 | + PersistentStringCacheImpl(PersistentStringCacheImpl const&) = delete; |
1847 | + PersistentStringCacheImpl& operator=(PersistentStringCacheImpl const&) = delete; |
1848 | + |
1849 | + PersistentStringCacheImpl(PersistentStringCacheImpl&&) = delete; |
1850 | + PersistentStringCacheImpl& operator=(PersistentStringCacheImpl&&) = delete; |
1851 | + |
1852 | + ~PersistentStringCacheImpl(); |
1853 | + |
1854 | + bool get(std::string const& key, std::string& value) const; |
1855 | + bool get(std::string const& key, std::string& value, std::string* metadata) const; |
1856 | + bool get_metadata(std::string const& key, std::string& metadata) const; |
1857 | + bool contains_key(std::string const& key) const; |
1858 | + int64_t size() const noexcept; |
1859 | + int64_t size_in_bytes() const noexcept; |
1860 | + int64_t max_size_in_bytes() const noexcept; |
1861 | + int64_t disk_size_in_bytes() const; |
1862 | + CacheDiscardPolicy discard_policy() const noexcept; |
1863 | + core::PersistentCacheStats stats() const; |
1864 | + |
1865 | + bool put(std::string const& key, |
1866 | + std::string const& value, |
1867 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
1868 | + bool put(std::string const& key, |
1869 | + char const* value, |
1870 | + int64_t size, |
1871 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
1872 | + bool put(std::string const& key, |
1873 | + std::string const& value, |
1874 | + std::string const* metadata, |
1875 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
1876 | + bool put(std::string const& key, |
1877 | + char const* value_data, |
1878 | + int64_t value_size, |
1879 | + char const* metadata_data, |
1880 | + int64_t metadata_size, |
1881 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
1882 | + bool get_or_put(std::string const& key, std::string& value, PersistentStringCache::Loader load_func); |
1883 | + bool get_or_put(std::string const& key, |
1884 | + std::string& value, |
1885 | + std::string* metadata, |
1886 | + PersistentStringCache::Loader load_func); |
1887 | + bool put_metadata(std::string const& key, std::string const& metadata); |
1888 | + bool put_metadata(std::string const& key, char const* metadata, int64_t metadata_size); |
1889 | + bool take(std::string const& key, std::string& value); |
1890 | + bool take(std::string const& key, std::string& value, std::string* metadata); |
1891 | + bool invalidate(std::string const& key); |
1892 | + void invalidate(std::vector<std::string> const& keys); |
1893 | + template<typename It> |
1894 | + void invalidate(It begin, It end); |
1895 | + void invalidate(std::initializer_list<std::string> const& keys); |
1896 | + void invalidate(); |
1897 | + bool touch( |
1898 | + std::string const& key, |
1899 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
1900 | + void clear_stats() noexcept; |
1901 | + void resize(int64_t size_in_bytes); |
1902 | + void trim_to(int64_t used_size_in_bytes); |
1903 | + void compact(); |
1904 | + void set_handler(CacheEvent events, PersistentStringCache::EventCallback cb); |
1905 | + |
1906 | +private: |
1907 | + // Simple struct to serialize/deserialize a data tuple. |
1908 | + // For the stringified representation, fields are separated |
1909 | + // by a space. |
1910 | + |
1911 | + struct DataTuple |
1912 | + { |
1913 | + int64_t atime; // Last access time, msec since the epoch |
1914 | + int64_t etime; // Expiry time, msec since the epoch |
1915 | + int64_t size; // Size in bytes |
1916 | + |
1917 | + DataTuple(int64_t at, int64_t et, int64_t s) noexcept |
1918 | + : atime(at) |
1919 | + , etime(et) |
1920 | + , size(s) |
1921 | + { |
1922 | + } |
1923 | + |
1924 | + DataTuple() noexcept |
1925 | + : DataTuple(0, 0, 0) |
1926 | + { |
1927 | + } |
1928 | + |
1929 | + DataTuple(std::string const& s) noexcept |
1930 | + { |
1931 | + std::istringstream is(s); |
1932 | + is >> atime >> etime >> size; |
1933 | + assert(!is.bad()); |
1934 | + } |
1935 | + |
1936 | + DataTuple(DataTuple const&) = default; |
1937 | + DataTuple(DataTuple&&) = default; |
1938 | + |
1939 | + DataTuple& operator=(DataTuple const&) = default; |
1940 | + DataTuple& operator=(DataTuple&&) = default; |
1941 | + |
1942 | + std::string to_string() const |
1943 | + { |
1944 | + std::ostringstream os; |
1945 | + os << atime << " " << etime << " " << size; |
1946 | + return os.str(); |
1947 | + } |
1948 | + }; |
1949 | + |
1950 | + void init_stats(); |
1951 | + void init_db(leveldb::Options options); |
1952 | + bool cache_is_new() const; |
1953 | + void write_version(); |
1954 | + void check_version(); |
1955 | + void read_settings(); |
1956 | + void write_settings(); |
1957 | + void read_stats(); |
1958 | + void write_stats(); |
1959 | + bool read_dirty_flag() const; |
1960 | + void write_dirty_flag(bool is_dirty); |
1961 | + DataTuple get_data(std::string const& key, bool& found) const; |
1962 | + bool get_value_and_metadata(std::string const& key, |
1963 | + DataTuple& data, |
1964 | + std::string& value, |
1965 | + std::string* metadata) const; |
1966 | + void batch_delete(std::string const& key, DataTuple const& data, leveldb::WriteBatch& batch); |
1967 | + void delete_entry(std::string const& key, DataTuple const& data); |
1968 | + void delete_at_least(int64_t bytes_needed, std::string const& skip_key = ""); |
1969 | + void call_handler(std::string const& key, core::internal::CacheEventIndex event) const; |
1970 | + |
1971 | + std::string make_message(leveldb::Status const& s, std::string const& msg) const; |
1972 | + std::string make_message(std::string const& msg) const; |
1973 | + void throw_if_error(leveldb::Status const& s, std::string const& msg) const; |
1974 | + void throw_logic_error(std::string const& msg) const; |
1975 | + void throw_invalid_argument(std::string const& msg) const; |
1976 | + void throw_corrupt_error(std::string const& msg) const; |
1977 | + |
1978 | + PersistentStringCache* pimpl_; // Back-pointer to owning pimpl. |
1979 | + std::unique_ptr<leveldb::Cache> block_cache_; // Must be defined *before* db_! |
1980 | + std::unique_ptr<leveldb::DB> db_; |
1981 | + std::shared_ptr<PersistentStringCacheStats> stats_; |
1982 | + |
1983 | + std::array<PersistentStringCache::EventCallback, static_cast<unsigned>(CacheEventIndex::END_)> |
1984 | + handlers_; |
1985 | + |
1986 | + mutable std::recursive_mutex mutex_; |
1987 | +}; |
1988 | + |
1989 | +} // namespace internal |
1990 | + |
1991 | +} // namespace core |
1992 | |
1993 | === added file 'include/core/internal/persistent_string_cache_stats.h' |
1994 | --- include/core/internal/persistent_string_cache_stats.h 1970-01-01 00:00:00 +0000 |
1995 | +++ include/core/internal/persistent_string_cache_stats.h 2015-06-23 06:06:30 +0000 |
1996 | @@ -0,0 +1,237 @@ |
1997 | +/* |
1998 | + * Copyright (C) 2015 Canonical Ltd. |
1999 | + * |
2000 | + * This program is free software: you can redistribute it and/or modify |
2001 | + * it under the terms of the GNU Lesser General Public License version 3 as |
2002 | + * published by the Free Software Foundation. |
2003 | + * |
2004 | + * This program is distributed in the hope that it will be useful, |
2005 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
2006 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2007 | + * GNU Lesser General Public License for more details. |
2008 | + * |
2009 | + * You should have received a copy of the GNU Lesser General Public License |
2010 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
2011 | + * |
2012 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
2013 | + */ |
2014 | + |
2015 | +#pragma once |
2016 | + |
2017 | +#include <core/cache_discard_policy.h> |
2018 | +#include <core/persistent_cache_stats.h> |
2019 | + |
2020 | +#include <cassert> |
2021 | +#include <cmath> |
2022 | +#include <cstring> |
2023 | +#include <sstream> |
2024 | + |
2025 | +namespace core |
2026 | +{ |
2027 | + |
2028 | +namespace internal |
2029 | +{ |
2030 | + |
2031 | +// Simple stats class to keep track of accesses. |
2032 | + |
2033 | +class PersistentStringCacheStats |
2034 | +{ |
2035 | +public: |
2036 | + PersistentStringCacheStats() noexcept |
2037 | + : policy_(CacheDiscardPolicy::lru_only) |
2038 | + , max_cache_size_(0) |
2039 | + , num_entries_(0) |
2040 | + , cache_size_(0) |
2041 | + , state_(Initialized) |
2042 | + { |
2043 | + clear(); |
2044 | + hist_.resize(PersistentCacheStats::NUM_BINS, 0); |
2045 | + } |
2046 | + |
2047 | + PersistentStringCacheStats(PersistentStringCacheStats const&) = default; |
2048 | + PersistentStringCacheStats(PersistentStringCacheStats&&) = default; |
2049 | + PersistentStringCacheStats& operator=(PersistentStringCacheStats const&) = default; |
2050 | + PersistentStringCacheStats& operator=(PersistentStringCacheStats&&) = default; |
2051 | + |
2052 | + std::string cache_path_; // Immutable |
2053 | + core::CacheDiscardPolicy policy_; // Immutable |
2054 | + int64_t max_cache_size_; |
2055 | + |
2056 | + int64_t num_entries_; |
2057 | + int64_t cache_size_; |
2058 | + PersistentCacheStats::Histogram hist_; |
2059 | + |
2060 | + // Values below are reset by a call to clear(). |
2061 | + int64_t hits_; |
2062 | + int64_t misses_; |
2063 | + int64_t hits_since_last_miss_; |
2064 | + int64_t misses_since_last_hit_; |
2065 | + int64_t longest_hit_run_; |
2066 | + int64_t longest_miss_run_; |
2067 | + int64_t ttl_evictions_; |
2068 | + int64_t lru_evictions_; |
2069 | + std::chrono::system_clock::time_point most_recent_hit_time_; |
2070 | + std::chrono::system_clock::time_point most_recent_miss_time_; |
2071 | + std::chrono::system_clock::time_point longest_hit_run_time_; |
2072 | + std::chrono::system_clock::time_point longest_miss_run_time_; |
2073 | + |
2074 | + enum State |
2075 | + { |
2076 | + Initialized, |
2077 | + LastAccessWasHit, |
2078 | + LastAccessWasMiss |
2079 | + }; |
2080 | + State state_; |
2081 | + |
2082 | + void inc_hits() noexcept |
2083 | + { |
2084 | + ++hits_since_last_miss_; |
2085 | + ++hits_; |
2086 | + most_recent_hit_time_ = std::chrono::system_clock::now(); |
2087 | + misses_since_last_hit_ = 0; |
2088 | + if (state_ != LastAccessWasHit) |
2089 | + { |
2090 | + state_ = LastAccessWasHit; |
2091 | + misses_since_last_hit_ = 0; |
2092 | + } |
2093 | + if (state_ != LastAccessWasMiss && hits_since_last_miss_ > longest_hit_run_) |
2094 | + { |
2095 | + longest_hit_run_ = hits_since_last_miss_; |
2096 | + longest_hit_run_time_ = most_recent_hit_time_; |
2097 | + } |
2098 | + } |
2099 | + |
2100 | + void inc_misses() noexcept |
2101 | + { |
2102 | + ++misses_since_last_hit_; |
2103 | + ++misses_; |
2104 | + most_recent_miss_time_ = std::chrono::system_clock::now(); |
2105 | + hits_since_last_miss_ = 0; |
2106 | + if (state_ != LastAccessWasMiss) |
2107 | + { |
2108 | + state_ = LastAccessWasMiss; |
2109 | + hits_since_last_miss_ = 0; |
2110 | + } |
2111 | + if (state_ != LastAccessWasHit && misses_since_last_hit_ > longest_miss_run_) |
2112 | + { |
2113 | + longest_miss_run_ = misses_since_last_hit_; |
2114 | + longest_miss_run_time_ = most_recent_miss_time_; |
2115 | + } |
2116 | + } |
2117 | + |
2118 | + void hist_decrement(int64_t size) noexcept |
2119 | + { |
2120 | + assert(size > 0); |
2121 | + --hist_[size_to_index(size)]; |
2122 | + } |
2123 | + |
2124 | + void hist_increment(int64_t size) noexcept |
2125 | + { |
2126 | + assert(size > 0); |
2127 | + ++hist_[size_to_index(size)]; |
2128 | + } |
2129 | + |
2130 | + void hist_clear() noexcept |
2131 | + { |
2132 | + memset(&hist_[0], 0, hist_.size() * sizeof(PersistentCacheStats::Histogram::value_type)); |
2133 | + } |
2134 | + |
2135 | + void clear() noexcept |
2136 | + { |
2137 | + hits_ = 0; |
2138 | + misses_ = 0; |
2139 | + hits_since_last_miss_ = 0; |
2140 | + misses_since_last_hit_ = 0; |
2141 | + longest_hit_run_ = 0; |
2142 | + longest_miss_run_ = 0; |
2143 | + ttl_evictions_ = 0; |
2144 | + lru_evictions_ = 0; |
2145 | + most_recent_hit_time_ = std::chrono::system_clock::time_point(); |
2146 | + most_recent_miss_time_ = std::chrono::system_clock::time_point(); |
2147 | + longest_hit_run_time_ = std::chrono::system_clock::time_point(); |
2148 | + longest_miss_run_time_ = std::chrono::system_clock::time_point(); |
2149 | + } |
2150 | + |
2151 | + // Serialize the stats. |
2152 | + |
2153 | + std::string serialize() const |
2154 | + { |
2155 | + using namespace std; |
2156 | + using namespace std::chrono; |
2157 | + |
2158 | + ostringstream os; |
2159 | + os << num_entries_ << " " |
2160 | + << cache_size_ << " " |
2161 | + << hits_ << " " |
2162 | + << misses_ << " " |
2163 | + << hits_since_last_miss_ << " " |
2164 | + << misses_since_last_hit_ << " " |
2165 | + << longest_hit_run_ << " " |
2166 | + << longest_miss_run_ << " " |
2167 | + << ttl_evictions_ << " " |
2168 | + << lru_evictions_ << " " |
2169 | + << duration_cast<milliseconds>(most_recent_hit_time_.time_since_epoch()).count() << " " |
2170 | + << duration_cast<milliseconds>(most_recent_miss_time_.time_since_epoch()).count() << " " |
2171 | + << duration_cast<milliseconds>(longest_hit_run_time_.time_since_epoch()).count() << " " |
2172 | + << duration_cast<milliseconds>(longest_miss_run_time_.time_since_epoch()).count(); |
2173 | + for (auto d : hist_) |
2174 | + { |
2175 | + os << " " << d; |
2176 | + } |
2177 | + return os.str(); |
2178 | + } |
2179 | + |
2180 | + // De-serialize the stats. |
2181 | + |
2182 | + void deserialize(const std::string& s) noexcept |
2183 | + { |
2184 | + using namespace std; |
2185 | + using namespace std::chrono; |
2186 | + |
2187 | + istringstream is(s); |
2188 | + int64_t mrht; |
2189 | + int64_t mrmt; |
2190 | + int64_t lhrt; |
2191 | + int64_t lmrt; |
2192 | + is >> num_entries_ |
2193 | + >> cache_size_ |
2194 | + >> hits_ |
2195 | + >> misses_ |
2196 | + >> hits_since_last_miss_ |
2197 | + >> misses_since_last_hit_ |
2198 | + >> longest_hit_run_ |
2199 | + >> longest_miss_run_ |
2200 | + >> ttl_evictions_ |
2201 | + >> lru_evictions_ |
2202 | + >> mrht |
2203 | + >> mrmt |
2204 | + >> lhrt |
2205 | + >> lmrt; |
2206 | + for (unsigned i = 0; i < PersistentCacheStats::NUM_BINS; ++i) |
2207 | + { |
2208 | + is >> hist_[i]; |
2209 | + } |
2210 | + assert(!is.bad()); |
2211 | + most_recent_hit_time_ = system_clock::time_point(milliseconds(mrht)); |
2212 | + most_recent_miss_time_ = system_clock::time_point(milliseconds(mrmt)); |
2213 | + longest_hit_run_time_ = system_clock::time_point(milliseconds(lhrt)); |
2214 | + longest_miss_run_time_ = system_clock::time_point(milliseconds(lmrt)); |
2215 | + } |
2216 | + |
2217 | +private: |
2218 | + unsigned size_to_index(int64_t size) const noexcept |
2219 | + { |
2220 | + using namespace std; |
2221 | + assert(size > 0); |
2222 | + unsigned log = floor(log10(size)); // 0..9 = 0, 10..99 = 1, 100..199 = 2, etc. |
2223 | + unsigned exp = pow(10, log); // 0..9 = 1, 10..99 = 10, 100..199 = 100, etc. |
2224 | + unsigned div = size / exp; // Extracts first decimal digit of size. |
2225 | + int index = log * 10 + div - log - 1; // Partition each power of 10 into 9 bins. |
2226 | + index -= 8; // Sizes < 10 all go into bin 0; |
2227 | + return index < 0 ? 0 : (index > int(hist_.size()) - 1 ? hist_.size() - 1 : index); |
2228 | + } |
2229 | +}; |
2230 | + |
2231 | +} // namespace internal |
2232 | + |
2233 | +} // namespace core |
2234 | |
2235 | === added file 'include/core/optional.h' |
2236 | --- include/core/optional.h 1970-01-01 00:00:00 +0000 |
2237 | +++ include/core/optional.h 2015-06-23 06:06:30 +0000 |
2238 | @@ -0,0 +1,35 @@ |
2239 | +/* |
2240 | + * Copyright (C) 2015 Canonical Ltd. |
2241 | + * |
2242 | + * This program is free software: you can redistribute it and/or modify |
2243 | + * it under the terms of the GNU Lesser General Public License version 3 as |
2244 | + * published by the Free Software Foundation. |
2245 | + * |
2246 | + * This program is distributed in the hope that it will be useful, |
2247 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
2248 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2249 | + * GNU Lesser General Public License for more details. |
2250 | + * |
2251 | + * You should have received a copy of the GNU Lesser General Public License |
2252 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
2253 | + * |
2254 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
2255 | + */ |
2256 | + |
2257 | +#pragma once |
2258 | + |
2259 | +#include <boost/optional.hpp> |
2260 | + |
2261 | +namespace core |
2262 | +{ |
2263 | + |
2264 | +/** |
2265 | +\brief Convenience typedef for nullable values. |
2266 | + |
2267 | +\note You should use `core::Optional` in preference to `boost::optional` |
2268 | +in your code. This will ease an eventual transition to `std::optional`. |
2269 | +*/ |
2270 | +template <typename T> |
2271 | +using Optional = boost::optional<T>; |
2272 | + |
2273 | +} // namespace core |
2274 | |
2275 | === added file 'include/core/persistent_cache.h' |
2276 | --- include/core/persistent_cache.h 1970-01-01 00:00:00 +0000 |
2277 | +++ include/core/persistent_cache.h 2015-06-23 06:06:30 +0000 |
2278 | @@ -0,0 +1,3104 @@ |
2279 | +/* |
2280 | + * Copyright (C) 2015 Canonical Ltd. |
2281 | + * |
2282 | + * This program is free software: you can redistribute it and/or modify |
2283 | + * it under the terms of the GNU Lesser General Public License version 3 as |
2284 | + * published by the Free Software Foundation. |
2285 | + * |
2286 | + * This program is distributed in the hope that it will be useful, |
2287 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
2288 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2289 | + * GNU Lesser General Public License for more details. |
2290 | + * |
2291 | + * You should have received a copy of the GNU Lesser General Public License |
2292 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
2293 | + * |
2294 | + * Authored by: Michi Henning <michi.henning@canonical.com> |
2295 | + */ |
2296 | + |
2297 | +#pragma once |
2298 | + |
2299 | +#include <core/cache_codec.h> |
2300 | +#include <core/persistent_string_cache.h> |
2301 | + |
2302 | +namespace core |
2303 | +{ |
2304 | + |
2305 | +/** |
2306 | +\brief A persistent cache of key-value pairs and metadata of user-defined type. |
2307 | + |
2308 | +`K`, `V`, and `M` are the key type, value type, and metadata type, respectively. |
2309 | + |
2310 | +\note This template is a simple type adapter that forwards to |
2311 | +core::PersistentStringCache. See the documentation there for details |
2312 | +on cache operations and semantics. |
2313 | + |
2314 | +In order to use the cache with custom types (other than `std::string`), |
2315 | +you must provide methods to encode the type to `string`, and decode |
2316 | +from `string` back to the type. |
2317 | + |
2318 | +For example, suppose we have the following structure that we want to use |
2319 | +as the key type of the cache: |
2320 | + |
2321 | +\code{.cpp} |
2322 | +struct Person |
2323 | +{ |
2324 | + string name; |
2325 | + int age; |
2326 | +}; |
2327 | +\endcode |
2328 | + |
2329 | +In order to use the cache with the `Person` struct as the key, you must specialize |
2330 | +the CacheCodec struct in namespace `core`: |
2331 | + |
2332 | +\code{.cpp} |
2333 | +namespace core // Specializations must be placed into namespace core. |
2334 | +{ |
2335 | + |
2336 | +template <> |
2337 | +string CacheCodec<Person>::encode(Person const& p) |
2338 | +{ |
2339 | + ostringstream s; |
2340 | + s << p.age << ' ' << p.name; |
2341 | + return s.str(); |
2342 | +} |
2343 | + |
2344 | +template <> |
2345 | +Person CacheCodec<Person>::decode(string const& str) |
2346 | +{ |
2347 | + istringstream s; |
2348 | + Person p; |
2349 | + s >> p.age >> p.name; |
2350 | + return p; |
2351 | +} |
2352 | + |
2353 | +} // namespace core |
2354 | +\endcode |
2355 | + |
2356 | +For this example, it is convenient to stream the age first because this |
2357 | +guarantees that `decode()` will work correctly even if the name contains |
2358 | +a space. The order in which you stream the fields does not matter, only that |
2359 | +(for custom _key_ types) the string representation of each value is unique. |
2360 | + |
2361 | +With these two methods defined, we can now use the cache with `Person` instances |
2362 | +as the key. For example: |
2363 | + |
2364 | +\code{.cpp} |
2365 | +// Custom cache using Person as the key, and string as the value and metadata. |
2366 | +using PersonCache = core::PersistentCache<Person, string>; |
2367 | + |
2368 | +auto c = PersonCache::open("my_cache", 1024 * 1024 * 1024, CacheDiscardPolicy::LRU_only); |
2369 | + |
2370 | +Person bjarne{"Bjarne Stroustrup", 65}; |
2371 | +c->put(bjarne, "C++ inventor"); |
2372 | +auto value = c->get(bjarne); |
2373 | +if (value) |
2374 | +{ |
2375 | + cout << bjarne.name << ": " << *value << endl; |
2376 | +} |
2377 | +Person person{"no such person", 0}; |
2378 | +value = c->get(person); |
2379 | +assert(!value); |
2380 | +\endcode |
2381 | + |
2382 | +Running this code produces the output: |
2383 | + |
2384 | +\code |
2385 | +Bjarne Stroustrup: C++ inventor |
2386 | +\endcode |
2387 | + |
2388 | +You can use a custom type for the cache's value and metadata as well by simply providing |
2389 | +CacheCodec specializations as needed. |
2390 | + |
2391 | +\see core::CacheCodec |
2392 | +\see core::PersistentStringCache |
2393 | +*/ |
2394 | + |
2395 | +template <typename K, typename V, typename M = std::string> |
2396 | +class PersistentCache |
2397 | +{ |
2398 | +public: |
2399 | + /** |
2400 | + Convenience typedef for the return type of open(). |
2401 | + */ |
2402 | + typedef std::unique_ptr<PersistentCache<K, V, M>> UPtr; |
2403 | + |
2404 | + /** |
2405 | + \brief Simple pair of value and metadata. |
2406 | + */ |
2407 | + struct Data |
2408 | + { |
2409 | + /** |
2410 | + \brief Stores the value of an entry. |
2411 | + */ |
2412 | + V value; |
2413 | + |
2414 | + /** |
2415 | + \brief Stores the metadata of an entry. If no metadata exists for an entry, |
2416 | + `metadata` is returned as the empty string when it is retrieved. |
2417 | + */ |
2418 | + M metadata; |
2419 | + }; |
2420 | + |
2421 | + /** @name Typedefs for nullable keys, values, and metadata. |
2422 | + */ |
2423 | + |
2424 | + //{@ |
2425 | + |
2426 | + /** |
2427 | + \brief Convenience typedefs for returning nullable values. |
2428 | + |
2429 | + \note You should use `OptionalKey`, `OptionalValue`, and `OptionalMetadata` in |
2430 | + your code in preference to `boost::optional`. This will ease an eventual |
2431 | + transition to `std::optional`. |
2432 | + */ |
2433 | + typedef Optional<K> OptionalKey; |
2434 | + typedef Optional<V> OptionalValue; |
2435 | + typedef Optional<M> OptionalMetadata; |
2436 | + typedef Optional<Data> OptionalData; |
2437 | + |
2438 | + //@} |
2439 | + |
2440 | + /** @name Copy and Assignment |
2441 | + */ |
2442 | + |
2443 | + //{@ |
2444 | + PersistentCache(PersistentCache const&) = delete; |
2445 | + PersistentCache& operator=(PersistentCache const&) = delete; |
2446 | + |
2447 | + PersistentCache(PersistentCache&&) = default; |
2448 | + PersistentCache& operator=(PersistentCache&&) = default; |
2449 | + //@} |
2450 | + |
2451 | + /** |
2452 | + Destroys the instance. |
2453 | + |
2454 | + The destructor compacts the database. This ensures that, while a cache is |
2455 | + not in use, it comsumes as little disk space as possible. |
2456 | + */ |
2457 | + ~PersistentCache() = default; |
2458 | + |
2459 | + /** @name Creation Methods |
2460 | + */ |
2461 | + |
2462 | + //{@ |
2463 | + |
2464 | + /** |
2465 | + \brief Creates or opens a PersistentCache. |
2466 | + */ |
2467 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
2468 | + |
2469 | + /** |
2470 | + \brief Opens an existing PersistentCache. |
2471 | + */ |
2472 | + static UPtr open(std::string const& cache_path); |
2473 | + |
2474 | + //@} |
2475 | + |
2476 | + /** @name Accessors |
2477 | + */ |
2478 | + |
2479 | + //{@ |
2480 | + |
2481 | + /** |
2482 | + \brief Returns the value of an entry in the cache, provided the entry has not expired. |
2483 | + */ |
2484 | + OptionalValue get(K const& key) const; |
2485 | + |
2486 | + /** |
2487 | + \brief Returns the data for an entry in the cache, provided the entry has not expired. |
2488 | + */ |
2489 | + OptionalData get_data(K const& key) const; |
2490 | + |
2491 | + /** |
2492 | + \brief Returns the metadata for an entry in the cache, provided the entry has not expired. |
2493 | + */ |
2494 | + OptionalMetadata get_metadata(K const& key) const; |
2495 | + |
2496 | + /** |
2497 | + \brief Tests if an (unexpired) entry is in the cache. |
2498 | + */ |
2499 | + bool contains_key(K const& key) const; |
2500 | + |
2501 | + /** |
2502 | + \brief Returns the number of entries in the cache. |
2503 | + */ |
2504 | + int64_t size() const noexcept; |
2505 | + |
2506 | + /** |
2507 | + \brief Returns the number of bytes consumed by entries in the cache. |
2508 | + */ |
2509 | + int64_t size_in_bytes() const noexcept; |
2510 | + |
2511 | + /** |
2512 | + \brief Returns the maximum size of the cache in bytes. |
2513 | + */ |
2514 | + int64_t max_size_in_bytes() const noexcept; |
2515 | + |
2516 | + /** |
2517 | + \brief Returns an estimate of the disk space consumed by the cache. |
2518 | + */ |
2519 | + int64_t disk_size_in_bytes() const; |
2520 | + |
2521 | + /** |
2522 | + \brief Returns the discard policy of the cache. |
2523 | + */ |
2524 | + CacheDiscardPolicy discard_policy() const noexcept; |
2525 | + |
2526 | + /** |
2527 | + \brief Returns statistics for the cache. |
2528 | + */ |
2529 | + PersistentCacheStats stats() const; |
2530 | + |
2531 | + //@} |
2532 | + |
2533 | + /** @name Modifiers |
2534 | + */ |
2535 | + |
2536 | + //{@ |
2537 | + |
2538 | + /** |
2539 | + \brief Adds or updates an entry. If `V` = `std::string`, the method is also overloaded to |
2540 | + to accept `char const*` and `size`. |
2541 | + */ |
2542 | + bool put(K const& key, |
2543 | + V const& value, |
2544 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
2545 | + |
2546 | + /** |
2547 | + \brief Adds or updates an entry and its metadata. If 'V' or `M` = `std::string`, |
2548 | + the method is also overloaded to accept `char const*` and `size`. |
2549 | + */ |
2550 | + bool put(K const& key, |
2551 | + V const& value, |
2552 | + M const& metadata, |
2553 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
2554 | + |
2555 | + /** |
2556 | + \brief Function called by the cache to load an entry after a cache miss. |
2557 | + */ |
2558 | + typedef std::function<void(K const& key, PersistentCache<K, V, M>& cache)> Loader; |
2559 | + |
2560 | + /** |
2561 | + \brief Atomically retrieves or stores a cache entry. |
2562 | + */ |
2563 | + OptionalValue get_or_put(K const& key, Loader const& load_func); |
2564 | + |
2565 | + /** |
2566 | + \brief Atomically retrieves or stores a cache entry. |
2567 | + */ |
2568 | + OptionalData get_or_put_data(K const& key, Loader const& load_func); |
2569 | + |
2570 | + /** |
2571 | + \brief Adds or replaces the metadata for an entry. If `M` = `std::string`, an overload that accepts |
2572 | + `const char*` and `size` is provided as well. |
2573 | + */ |
2574 | + bool put_metadata(K const& key, M const& metadata); |
2575 | + |
2576 | + /** |
2577 | + \brief Removes an entry and returns its value. |
2578 | + */ |
2579 | + OptionalValue take(K const& key); |
2580 | + |
2581 | + /** |
2582 | + \brief Removes an entry and returns its value and metadata. |
2583 | + */ |
2584 | + OptionalData take_data(K const& key); |
2585 | + |
2586 | + /** |
2587 | + \brief Removes an entry and its associated metadata (if any). |
2588 | + */ |
2589 | + bool invalidate(K const& key); |
2590 | + |
2591 | + /** |
2592 | + \brief Atomically removes the specified entries from the cache. |
2593 | + */ |
2594 | + void invalidate(std::vector<K> const& keys); |
2595 | + |
2596 | + /** |
2597 | + \brief Atomically removes the specified entries from the cache. |
2598 | + */ |
2599 | + template <typename It> |
2600 | + void invalidate(It begin, It end); |
2601 | + |
2602 | + /** |
2603 | + \brief Atomically removes the specified entries from the cache. |
2604 | + */ |
2605 | + void invalidate(std::initializer_list<K> const& keys); |
2606 | + |
2607 | + /** |
2608 | + \brief Deletes all entries from the cache. |
2609 | + */ |
2610 | + void invalidate(); |
2611 | + |
2612 | + /** |
2613 | + \brief Updates the access time of an entry. |
2614 | + */ |
2615 | + bool touch( |
2616 | + K const& key, |
2617 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
2618 | + |
2619 | + /** |
2620 | + \brief Resets all statistics counters. |
2621 | + */ |
2622 | + void clear_stats(); |
2623 | + |
2624 | + /** |
2625 | + \brief Changes the maximum size of the cache. |
2626 | + */ |
2627 | + void resize(int64_t size_in_bytes); |
2628 | + |
2629 | + /** |
2630 | + \brief Expires entries. |
2631 | + */ |
2632 | + void trim_to(int64_t used_size_in_bytes); |
2633 | + |
2634 | + /** |
2635 | + \brief Compacts the database. |
2636 | + */ |
2637 | + void compact(); |
2638 | + |
2639 | + //@} |
2640 | + |
2641 | + /** @name Monitoring cache activity |
2642 | + */ |
2643 | + |
2644 | + //{@ |
2645 | + |
2646 | + /** |
2647 | + \brief The type of a handler function. |
2648 | + */ |
2649 | + typedef std::function<void(K const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
2650 | + |
2651 | + /** |
2652 | + \brief Installs a handler for one or more events. |
2653 | + */ |
2654 | + void set_handler(CacheEvent events, EventCallback cb); |
2655 | + |
2656 | + //@} |
2657 | + |
2658 | +private: |
2659 | + // @cond |
2660 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
2661 | + PersistentCache(std::string const& cache_path); |
2662 | + |
2663 | + std::unique_ptr<PersistentStringCache> p_; |
2664 | + // @endcond |
2665 | +}; |
2666 | + |
2667 | +// @cond |
2668 | + |
2669 | +// Method implementations. Out-of-line because, otherwise, things become completely unreadable. |
2670 | + |
2671 | +template <typename K, typename V, typename M> |
2672 | +PersistentCache<K, V, M>::PersistentCache(std::string const& cache_path, |
2673 | + int64_t max_size_in_bytes, |
2674 | + CacheDiscardPolicy policy) |
2675 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
2676 | +{ |
2677 | +} |
2678 | + |
2679 | +template <typename K, typename V, typename M> |
2680 | +PersistentCache<K, V, M>::PersistentCache(std::string const& cache_path) |
2681 | + : p_(PersistentStringCache::open(cache_path)) |
2682 | +{ |
2683 | +} |
2684 | + |
2685 | +template <typename K, typename V, typename M> |
2686 | +typename PersistentCache<K, V, M>::UPtr PersistentCache<K, V, M>::open(std::string const& cache_path, |
2687 | + int64_t max_size_in_bytes, |
2688 | + CacheDiscardPolicy policy) |
2689 | +{ |
2690 | + return PersistentCache<K, V, M>::UPtr(new PersistentCache<K, V, M>(cache_path, max_size_in_bytes, policy)); |
2691 | +} |
2692 | + |
2693 | +template <typename K, typename V, typename M> |
2694 | +typename PersistentCache<K, V, M>::UPtr PersistentCache<K, V, M>::open(std::string const& cache_path) |
2695 | +{ |
2696 | + return PersistentCache<K, V, M>::UPtr(new PersistentCache<K, V, M>(cache_path)); |
2697 | +} |
2698 | + |
2699 | +template <typename K, typename V, typename M> |
2700 | +typename PersistentCache<K, V, M>::OptionalValue PersistentCache<K, V, M>::get(K const& key) const |
2701 | +{ |
2702 | + auto const& svalue = p_->get(CacheCodec<K>::encode(key)); |
2703 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
2704 | +} |
2705 | + |
2706 | +template <typename K, typename V, typename M> |
2707 | +typename PersistentCache<K, V, M>::OptionalData PersistentCache<K, V, M>::get_data(K const& key) const |
2708 | +{ |
2709 | + auto sdata = p_->get_data(CacheCodec<K>::encode(key)); |
2710 | + if (!sdata) |
2711 | + { |
2712 | + return OptionalData(); |
2713 | + } |
2714 | + return OptionalData({CacheCodec<V>::decode(sdata->value), CacheCodec<M>::decode(sdata->metadata)}); |
2715 | +} |
2716 | + |
2717 | +template <typename K, typename V, typename M> |
2718 | +typename PersistentCache<K, V, M>::OptionalMetadata PersistentCache<K, V, M>::get_metadata(K const& key) const |
2719 | +{ |
2720 | + auto smeta = p_->get_metadata(CacheCodec<K>::encode(key)); |
2721 | + return smeta ? OptionalMetadata(CacheCodec<M>::decode(*smeta)) : OptionalMetadata(); |
2722 | +} |
2723 | + |
2724 | +template <typename K, typename V, typename M> |
2725 | +bool PersistentCache<K, V, M>::contains_key(K const& key) const |
2726 | +{ |
2727 | + return p_->contains_key(CacheCodec<K>::encode(key)); |
2728 | +} |
2729 | + |
2730 | +template <typename K, typename V, typename M> |
2731 | +int64_t PersistentCache<K, V, M>::size() const noexcept |
2732 | +{ |
2733 | + return p_->size(); |
2734 | +} |
2735 | + |
2736 | +template <typename K, typename V, typename M> |
2737 | +int64_t PersistentCache<K, V, M>::size_in_bytes() const noexcept |
2738 | +{ |
2739 | + return p_->size_in_bytes(); |
2740 | +} |
2741 | + |
2742 | +template <typename K, typename V, typename M> |
2743 | +int64_t PersistentCache<K, V, M>::max_size_in_bytes() const noexcept |
2744 | +{ |
2745 | + return p_->max_size_in_bytes(); |
2746 | +} |
2747 | + |
2748 | +template <typename K, typename V, typename M> |
2749 | +int64_t PersistentCache<K, V, M>::disk_size_in_bytes() const |
2750 | +{ |
2751 | + return p_->disk_size_in_bytes(); |
2752 | +} |
2753 | + |
2754 | +template <typename K, typename V, typename M> |
2755 | +CacheDiscardPolicy PersistentCache<K, V, M>::discard_policy() const noexcept |
2756 | +{ |
2757 | + return p_->discard_policy(); |
2758 | +} |
2759 | + |
2760 | +template <typename K, typename V, typename M> |
2761 | +PersistentCacheStats PersistentCache<K, V, M>::stats() const |
2762 | +{ |
2763 | + return p_->stats(); |
2764 | +} |
2765 | + |
2766 | +template <typename K, typename V, typename M> |
2767 | +bool PersistentCache<K, V, M>::put(K const& key, |
2768 | + V const& value, |
2769 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
2770 | +{ |
2771 | + return p_->put(CacheCodec<K>::encode(key), CacheCodec<V>::encode(value), expiry_time); |
2772 | +} |
2773 | + |
2774 | +template <typename K, typename V, typename M> |
2775 | +bool PersistentCache<K, V, M>::put(K const& key, |
2776 | + V const& value, |
2777 | + M const& metadata, |
2778 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
2779 | +{ |
2780 | + return p_->put(CacheCodec<K>::encode(key), CacheCodec<V>::encode(value), CacheCodec<M>::encode(metadata), |
2781 | + expiry_time); |
2782 | +} |
2783 | + |
2784 | +template <typename K, typename V, typename M> |
2785 | +typename PersistentCache<K, V, M>::OptionalValue PersistentCache<K, V, M>::get_or_put( |
2786 | + K const& key, PersistentCache<K, V, M>::Loader const& load_func) |
2787 | +{ |
2788 | + std::string const& skey = CacheCodec<K>::encode(key); |
2789 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
2790 | + { |
2791 | + load_func(key, *this); |
2792 | + }; |
2793 | + auto svalue = p_->get_or_put(skey, sload_func); |
2794 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
2795 | +} |
2796 | + |
2797 | +template <typename K, typename V, typename M> |
2798 | +typename PersistentCache<K, V, M>::OptionalData PersistentCache<K, V, M>::get_or_put_data( |
2799 | + K const& key, PersistentCache<K, V, M>::Loader const& load_func) |
2800 | +{ |
2801 | + std::string const& skey = CacheCodec<K>::encode(key); |
2802 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
2803 | + { |
2804 | + load_func(key, *this); |
2805 | + }; |
2806 | + auto sdata = p_->get_or_put_data(skey, sload_func); |
2807 | + if (!sdata) |
2808 | + { |
2809 | + return OptionalData(); |
2810 | + } |
2811 | + return OptionalData({CacheCodec<V>::decode(sdata->value), CacheCodec<M>::decode(sdata->metadata)}); |
2812 | +} |
2813 | + |
2814 | +template <typename K, typename V, typename M> |
2815 | +bool PersistentCache<K, V, M>::put_metadata(K const& key, M const& metadata) |
2816 | +{ |
2817 | + return p_->put_metadata(CacheCodec<K>::encode(key), CacheCodec<M>::encode(metadata)); |
2818 | +} |
2819 | + |
2820 | +template <typename K, typename V, typename M> |
2821 | +typename PersistentCache<K, V, M>::OptionalValue PersistentCache<K, V, M>::take(K const& key) |
2822 | +{ |
2823 | + auto svalue = p_->take(CacheCodec<K>::encode(key)); |
2824 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
2825 | +} |
2826 | + |
2827 | +template <typename K, typename V, typename M> |
2828 | +typename PersistentCache<K, V, M>::OptionalData PersistentCache<K, V, M>::take_data(K const& key) |
2829 | +{ |
2830 | + auto sdata = p_->take_data(CacheCodec<K>::encode(key)); |
2831 | + if (!sdata) |
2832 | + { |
2833 | + return OptionalData(); |
2834 | + } |
2835 | + return OptionalData({CacheCodec<V>::decode(sdata->value), CacheCodec<M>::decode(sdata->metadata)}); |
2836 | +} |
2837 | + |
2838 | +template <typename K, typename V, typename M> |
2839 | +bool PersistentCache<K, V, M>::invalidate(K const& key) |
2840 | +{ |
2841 | + return p_->invalidate(CacheCodec<K>::encode(key)); |
2842 | +} |
2843 | + |
2844 | +template <typename K, typename V, typename M> |
2845 | +void PersistentCache<K, V, M>::invalidate(std::vector<K> const& keys) |
2846 | +{ |
2847 | + invalidate(keys.begin(), keys.end()); |
2848 | +} |
2849 | + |
2850 | +template <typename K, typename V, typename M> |
2851 | +template <typename It> |
2852 | +void PersistentCache<K, V, M>::invalidate(It begin, It end) |
2853 | +{ |
2854 | + std::vector<std::string> skeys; |
2855 | + for (auto&& it = begin; it < end; ++it) |
2856 | + { |
2857 | + skeys.push_back(CacheCodec<K>::encode(*it)); |
2858 | + } |
2859 | + p_->invalidate(skeys.begin(), skeys.end()); |
2860 | +} |
2861 | + |
2862 | +template <typename K, typename V, typename M> |
2863 | +void PersistentCache<K, V, M>::invalidate(std::initializer_list<K> const& keys) |
2864 | +{ |
2865 | + invalidate(keys.begin(), keys.end()); |
2866 | +} |
2867 | + |
2868 | +template <typename K, typename V, typename M> |
2869 | +void PersistentCache<K, V, M>::invalidate() |
2870 | +{ |
2871 | + p_->invalidate(); |
2872 | +} |
2873 | + |
2874 | +template <typename K, typename V, typename M> |
2875 | +bool PersistentCache<K, V, M>::touch(K const& key, std::chrono::time_point<std::chrono::system_clock> expiry_time) |
2876 | +{ |
2877 | + return p_->touch(CacheCodec<K>::encode(key), expiry_time); |
2878 | +} |
2879 | + |
2880 | +template <typename K, typename V, typename M> |
2881 | +void PersistentCache<K, V, M>::clear_stats() |
2882 | +{ |
2883 | + p_->clear_stats(); |
2884 | +} |
2885 | + |
2886 | +template <typename K, typename V, typename M> |
2887 | +void PersistentCache<K, V, M>::resize(int64_t size_in_bytes) |
2888 | +{ |
2889 | + p_->resize(size_in_bytes); |
2890 | +} |
2891 | + |
2892 | +template <typename K, typename V, typename M> |
2893 | +void PersistentCache<K, V, M>::trim_to(int64_t used_size_in_bytes) |
2894 | +{ |
2895 | + p_->trim_to(used_size_in_bytes); |
2896 | +} |
2897 | + |
2898 | +template <typename K, typename V, typename M> |
2899 | +void PersistentCache<K, V, M>::compact() |
2900 | +{ |
2901 | + p_->compact(); |
2902 | +} |
2903 | + |
2904 | +template <typename K, typename V, typename M> |
2905 | +void PersistentCache<K, V, M>::set_handler(CacheEvent events, EventCallback cb) |
2906 | +{ |
2907 | + auto scb = [cb](std::string const& key, CacheEvent ev, PersistentCacheStats const& c) |
2908 | + { |
2909 | + cb(CacheCodec<K>::decode(key), ev, c); |
2910 | + }; |
2911 | + p_->set_handler(events, scb); |
2912 | +} |
2913 | + |
2914 | +// Below are specializations for the various combinations of one or more of K, V, and M |
2915 | +// being of type std::string. Without this, we would end up calling through a string-to-string |
2916 | +// codec, which would force a copy for everything of type string, which is brutally inefficient. |
2917 | +// |
2918 | +// This is verbose because we must specialize the class template in order to avoid |
2919 | +// calling the CacheCodec methods for type string. The combinations that follow are |
2920 | +// <s,V,M>, <K,s,M>, <K,V,s>, <s,s,M>, <s,V,s>, <K,s,s>, and <s,s,s>. |
2921 | +// The code is identical, except that, where we know that K, V, or M are std::string, |
2922 | +// it avoids calling the encode()/decode() methods. |
2923 | + |
2924 | +// Specialization for K = std::string. |
2925 | + |
2926 | +template <typename V, typename M> |
2927 | +class PersistentCache<std::string, V, M> |
2928 | +{ |
2929 | +public: |
2930 | + typedef std::unique_ptr<PersistentCache<std::string, V, M>> UPtr; |
2931 | + |
2932 | + typedef Optional<std::string> OptionalKey; |
2933 | + typedef Optional<V> OptionalValue; |
2934 | + typedef Optional<M> OptionalMetadata; |
2935 | + |
2936 | + struct Data |
2937 | + { |
2938 | + V value; |
2939 | + M metadata; |
2940 | + }; |
2941 | + typedef Optional<Data> OptionalData; |
2942 | + |
2943 | + PersistentCache(PersistentCache const&) = delete; |
2944 | + PersistentCache& operator=(PersistentCache const&) = delete; |
2945 | + |
2946 | + PersistentCache(PersistentCache&&) = default; |
2947 | + PersistentCache& operator=(PersistentCache&&) = default; |
2948 | + |
2949 | + ~PersistentCache() = default; |
2950 | + |
2951 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
2952 | + static UPtr open(std::string const& cache_path); |
2953 | + |
2954 | + OptionalValue get(std::string const& key) const; |
2955 | + OptionalData get_data(std::string const& key) const; |
2956 | + OptionalMetadata get_metadata(std::string const& key) const; |
2957 | + bool contains_key(std::string const& key) const; |
2958 | + int64_t size() const noexcept; |
2959 | + int64_t size_in_bytes() const noexcept; |
2960 | + int64_t max_size_in_bytes() const noexcept; |
2961 | + int64_t disk_size_in_bytes() const; |
2962 | + CacheDiscardPolicy discard_policy() const noexcept; |
2963 | + PersistentCacheStats stats() const; |
2964 | + |
2965 | + bool put(std::string const& key, |
2966 | + V const& value, |
2967 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
2968 | + bool put(std::string const& key, |
2969 | + V const& value, |
2970 | + M const& metadata, |
2971 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
2972 | + |
2973 | + typedef std::function<void(std::string const& key, PersistentCache<std::string, V, M>& cache)> Loader; |
2974 | + |
2975 | + OptionalValue get_or_put(std::string const& key, Loader const& load_func); |
2976 | + OptionalData get_or_put_data(std::string const& key, Loader const& load_func); |
2977 | + |
2978 | + bool put_metadata(std::string const& key, M const& metadata); |
2979 | + OptionalValue take(std::string const& key); |
2980 | + OptionalData take_data(std::string const& key); |
2981 | + bool invalidate(std::string const& key); |
2982 | + void invalidate(std::vector<std::string> const& keys); |
2983 | + template <typename It> |
2984 | + void invalidate(It begin, It end); |
2985 | + void invalidate(std::initializer_list<std::string> const& keys); |
2986 | + void invalidate(); |
2987 | + bool touch( |
2988 | + std::string const& key, |
2989 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
2990 | + void clear_stats(); |
2991 | + void resize(int64_t size_in_bytes); |
2992 | + void trim_to(int64_t used_size_in_bytes); |
2993 | + void compact(); |
2994 | + |
2995 | + typedef std::function<void(std::string const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
2996 | + |
2997 | + void set_handler(CacheEvent events, EventCallback cb); |
2998 | + |
2999 | +private: |
3000 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
3001 | + PersistentCache(std::string const& cache_path); |
3002 | + |
3003 | + std::unique_ptr<PersistentStringCache> p_; |
3004 | +}; |
3005 | + |
3006 | +template <typename V, typename M> |
3007 | +PersistentCache<std::string, V, M>::PersistentCache(std::string const& cache_path, |
3008 | + int64_t max_size_in_bytes, |
3009 | + CacheDiscardPolicy policy) |
3010 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
3011 | +{ |
3012 | +} |
3013 | + |
3014 | +template <typename V, typename M> |
3015 | +PersistentCache<std::string, V, M>::PersistentCache(std::string const& cache_path) |
3016 | + : p_(PersistentStringCache::open(cache_path)) |
3017 | +{ |
3018 | +} |
3019 | + |
3020 | +template <typename V, typename M> |
3021 | +typename PersistentCache<std::string, V, M>::UPtr PersistentCache<std::string, V, M>::open( |
3022 | + std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy) |
3023 | +{ |
3024 | + return PersistentCache<std::string, V, M>::UPtr( |
3025 | + new PersistentCache<std::string, V, M>(cache_path, max_size_in_bytes, policy)); |
3026 | +} |
3027 | + |
3028 | +template <typename V, typename M> |
3029 | +typename PersistentCache<std::string, V, M>::UPtr PersistentCache<std::string, V, M>::open( |
3030 | + std::string const& cache_path) |
3031 | +{ |
3032 | + return PersistentCache<std::string, V, M>::UPtr(new PersistentCache<std::string, V, M>(cache_path)); |
3033 | +} |
3034 | + |
3035 | +template <typename V, typename M> |
3036 | +typename PersistentCache<std::string, V, M>::OptionalValue PersistentCache<std::string, V, M>::get( |
3037 | + std::string const& key) const |
3038 | +{ |
3039 | + auto const& svalue = p_->get(key); |
3040 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
3041 | +} |
3042 | + |
3043 | +template <typename V, typename M> |
3044 | +typename PersistentCache<std::string, V, M>::OptionalData PersistentCache<std::string, V, M>::get_data( |
3045 | + std::string const& key) const |
3046 | +{ |
3047 | + auto sdata = p_->get_data(key); |
3048 | + if (!sdata) |
3049 | + { |
3050 | + return OptionalData(); |
3051 | + } |
3052 | + return OptionalData({CacheCodec<V>::decode(sdata->value), CacheCodec<M>::decode(sdata->metadata)}); |
3053 | +} |
3054 | + |
3055 | +template <typename V, typename M> |
3056 | +typename PersistentCache<std::string, V, M>::OptionalMetadata PersistentCache<std::string, V, M>::get_metadata( |
3057 | + std::string const& key) const |
3058 | +{ |
3059 | + auto smeta = p_->get_metadata(key); |
3060 | + return smeta ? OptionalMetadata(CacheCodec<M>::decode(*smeta)) : OptionalMetadata(); |
3061 | +} |
3062 | + |
3063 | +template <typename V, typename M> |
3064 | +bool PersistentCache<std::string, V, M>::contains_key(std::string const& key) const |
3065 | +{ |
3066 | + return p_->contains_key(key); |
3067 | +} |
3068 | + |
3069 | +template <typename V, typename M> |
3070 | +int64_t PersistentCache<std::string, V, M>::size() const noexcept |
3071 | +{ |
3072 | + return p_->size(); |
3073 | +} |
3074 | + |
3075 | +template <typename V, typename M> |
3076 | +int64_t PersistentCache<std::string, V, M>::size_in_bytes() const noexcept |
3077 | +{ |
3078 | + return p_->size_in_bytes(); |
3079 | +} |
3080 | + |
3081 | +template <typename V, typename M> |
3082 | +int64_t PersistentCache<std::string, V, M>::max_size_in_bytes() const noexcept |
3083 | +{ |
3084 | + return p_->max_size_in_bytes(); |
3085 | +} |
3086 | + |
3087 | +template <typename V, typename M> |
3088 | +int64_t PersistentCache<std::string, V, M>::disk_size_in_bytes() const |
3089 | +{ |
3090 | + return p_->disk_size_in_bytes(); |
3091 | +} |
3092 | + |
3093 | +template <typename V, typename M> |
3094 | +CacheDiscardPolicy PersistentCache<std::string, V, M>::discard_policy() const noexcept |
3095 | +{ |
3096 | + return p_->discard_policy(); |
3097 | +} |
3098 | + |
3099 | +template <typename V, typename M> |
3100 | +PersistentCacheStats PersistentCache<std::string, V, M>::stats() const |
3101 | +{ |
3102 | + return p_->stats(); |
3103 | +} |
3104 | + |
3105 | +template <typename V, typename M> |
3106 | +bool PersistentCache<std::string, V, M>::put(std::string const& key, |
3107 | + V const& value, |
3108 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3109 | +{ |
3110 | + return p_->put(key, CacheCodec<V>::encode(value), expiry_time); |
3111 | +} |
3112 | + |
3113 | +template <typename V, typename M> |
3114 | +bool PersistentCache<std::string, V, M>::put(std::string const& key, |
3115 | + V const& value, |
3116 | + M const& metadata, |
3117 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3118 | +{ |
3119 | + return p_->put(key, CacheCodec<V>::encode(value), CacheCodec<M>::encode(metadata), expiry_time); |
3120 | +} |
3121 | + |
3122 | +template <typename V, typename M> |
3123 | +typename PersistentCache<std::string, V, M>::OptionalValue PersistentCache<std::string, V, M>::get_or_put( |
3124 | + std::string const& key, PersistentCache<std::string, V, M>::Loader const& load_func) |
3125 | +{ |
3126 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
3127 | + { |
3128 | + load_func(key, *this); |
3129 | + }; |
3130 | + auto svalue = p_->get_or_put(key, sload_func); |
3131 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
3132 | +} |
3133 | + |
3134 | +template <typename V, typename M> |
3135 | +typename PersistentCache<std::string, V, M>::OptionalData PersistentCache<std::string, V, M>::get_or_put_data( |
3136 | + std::string const& key, PersistentCache<std::string, V, M>::Loader const& load_func) |
3137 | +{ |
3138 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
3139 | + { |
3140 | + load_func(key, *this); |
3141 | + }; |
3142 | + auto sdata = p_->get_or_put_data(key, sload_func); |
3143 | + if (!sdata) |
3144 | + { |
3145 | + return OptionalData(); |
3146 | + } |
3147 | + return OptionalData({CacheCodec<V>::decode(sdata->value), CacheCodec<M>::decode(sdata->metadata)}); |
3148 | +} |
3149 | + |
3150 | +template <typename V, typename M> |
3151 | +bool PersistentCache<std::string, V, M>::put_metadata(std::string const& key, M const& metadata) |
3152 | +{ |
3153 | + return p_->put_metadata(key, CacheCodec<M>::encode(metadata)); |
3154 | +} |
3155 | + |
3156 | +template <typename V, typename M> |
3157 | +typename PersistentCache<std::string, V, M>::OptionalValue PersistentCache<std::string, V, M>::take( |
3158 | + std::string const& key) |
3159 | +{ |
3160 | + auto svalue = p_->take(key); |
3161 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
3162 | +} |
3163 | + |
3164 | +template <typename V, typename M> |
3165 | +typename PersistentCache<std::string, V, M>::OptionalData PersistentCache<std::string, V, M>::take_data( |
3166 | + std::string const& key) |
3167 | +{ |
3168 | + auto sdata = p_->take_data(key); |
3169 | + if (!sdata) |
3170 | + { |
3171 | + return OptionalData(); |
3172 | + } |
3173 | + return OptionalData({CacheCodec<V>::decode(sdata->value), CacheCodec<M>::decode(sdata->metadata)}); |
3174 | +} |
3175 | + |
3176 | +template <typename V, typename M> |
3177 | +bool PersistentCache<std::string, V, M>::invalidate(std::string const& key) |
3178 | +{ |
3179 | + return p_->invalidate(key); |
3180 | +} |
3181 | + |
3182 | +template <typename V, typename M> |
3183 | +void PersistentCache<std::string, V, M>::invalidate(std::vector<std::string> const& keys) |
3184 | +{ |
3185 | + invalidate(keys.begin(), keys.end()); |
3186 | +} |
3187 | + |
3188 | +template <typename V, typename M> |
3189 | +template <typename It> |
3190 | +void PersistentCache<std::string, V, M>::invalidate(It begin, It end) |
3191 | +{ |
3192 | + std::vector<std::string> skeys; |
3193 | + for (auto&& it = begin; it < end; ++it) |
3194 | + { |
3195 | + skeys.push_back(*it); |
3196 | + } |
3197 | + p_->invalidate(skeys.begin(), skeys.end()); |
3198 | +} |
3199 | + |
3200 | +template <typename V, typename M> |
3201 | +void PersistentCache<std::string, V, M>::invalidate(std::initializer_list<std::string> const& keys) |
3202 | +{ |
3203 | + invalidate(keys.begin(), keys.end()); |
3204 | +} |
3205 | + |
3206 | +template <typename V, typename M> |
3207 | +void PersistentCache<std::string, V, M>::invalidate() |
3208 | +{ |
3209 | + p_->invalidate(); |
3210 | +} |
3211 | + |
3212 | +template <typename V, typename M> |
3213 | +bool PersistentCache<std::string, V, M>::touch(std::string const& key, |
3214 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3215 | +{ |
3216 | + return p_->touch(key, expiry_time); |
3217 | +} |
3218 | + |
3219 | +template <typename V, typename M> |
3220 | +void PersistentCache<std::string, V, M>::clear_stats() |
3221 | +{ |
3222 | + p_->clear_stats(); |
3223 | +} |
3224 | + |
3225 | +template <typename V, typename M> |
3226 | +void PersistentCache<std::string, V, M>::resize(int64_t size_in_bytes) |
3227 | +{ |
3228 | + p_->resize(size_in_bytes); |
3229 | +} |
3230 | + |
3231 | +template <typename V, typename M> |
3232 | +void PersistentCache<std::string, V, M>::trim_to(int64_t used_size_in_bytes) |
3233 | +{ |
3234 | + p_->trim_to(used_size_in_bytes); |
3235 | +} |
3236 | + |
3237 | +template <typename V, typename M> |
3238 | +void PersistentCache<std::string, V, M>::compact() |
3239 | +{ |
3240 | + p_->compact(); |
3241 | +} |
3242 | + |
3243 | +template <typename V, typename M> |
3244 | +void PersistentCache<std::string, V, M>::set_handler(CacheEvent events, EventCallback cb) |
3245 | +{ |
3246 | + p_->set_handler(events, cb); |
3247 | +} |
3248 | + |
3249 | +// Specialization for V = std::string. |
3250 | + |
3251 | +template <typename K, typename M> |
3252 | +class PersistentCache<K, std::string, M> |
3253 | +{ |
3254 | +public: |
3255 | + typedef std::unique_ptr<PersistentCache<K, std::string, M>> UPtr; |
3256 | + |
3257 | + typedef Optional<K> OptionalKey; |
3258 | + typedef Optional<std::string> OptionalValue; |
3259 | + typedef Optional<M> OptionalMetadata; |
3260 | + |
3261 | + struct Data |
3262 | + { |
3263 | + std::string value; |
3264 | + M metadata; |
3265 | + }; |
3266 | + typedef Optional<Data> OptionalData; |
3267 | + |
3268 | + PersistentCache(PersistentCache const&) = delete; |
3269 | + PersistentCache& operator=(PersistentCache const&) = delete; |
3270 | + |
3271 | + PersistentCache(PersistentCache&&) = default; |
3272 | + PersistentCache& operator=(PersistentCache&&) = default; |
3273 | + |
3274 | + ~PersistentCache() = default; |
3275 | + |
3276 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
3277 | + static UPtr open(std::string const& cache_path); |
3278 | + |
3279 | + OptionalValue get(K const& key) const; |
3280 | + OptionalData get_data(K const& key) const; |
3281 | + OptionalMetadata get_metadata(K const& key) const; |
3282 | + bool contains_key(K const& key) const; |
3283 | + int64_t size() const noexcept; |
3284 | + int64_t size_in_bytes() const noexcept; |
3285 | + int64_t max_size_in_bytes() const noexcept; |
3286 | + int64_t disk_size_in_bytes() const; |
3287 | + CacheDiscardPolicy discard_policy() const noexcept; |
3288 | + PersistentCacheStats stats() const; |
3289 | + |
3290 | + bool put(K const& key, |
3291 | + std::string const& value, |
3292 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3293 | + bool put(K const& key, |
3294 | + char const* value, |
3295 | + int64_t size, |
3296 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3297 | + bool put(K const& key, |
3298 | + std::string const& value, |
3299 | + M const& metadata, |
3300 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3301 | + bool put(K const& key, |
3302 | + char const* value, |
3303 | + int64_t value_size, |
3304 | + M const& metadata, |
3305 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3306 | + |
3307 | + typedef std::function<void(K const& key, PersistentCache<K, std::string, M>& cache)> Loader; |
3308 | + |
3309 | + OptionalValue get_or_put(K const& key, Loader const& load_func); |
3310 | + OptionalData get_or_put_data(K const& key, Loader const& load_func); |
3311 | + |
3312 | + bool put_metadata(K const& key, M const& metadata); |
3313 | + OptionalValue take(K const& key); |
3314 | + OptionalData take_data(K const& key); |
3315 | + bool invalidate(K const& key); |
3316 | + void invalidate(std::vector<K> const& keys); |
3317 | + template <typename It> |
3318 | + void invalidate(It begin, It end); |
3319 | + void invalidate(std::initializer_list<K> const& keys); |
3320 | + void invalidate(); |
3321 | + bool touch( |
3322 | + K const& key, |
3323 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3324 | + void clear_stats(); |
3325 | + void resize(int64_t size_in_bytes); |
3326 | + void trim_to(int64_t used_size_in_bytes); |
3327 | + void compact(); |
3328 | + |
3329 | + typedef std::function<void(K const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
3330 | + |
3331 | + void set_handler(CacheEvent events, EventCallback cb); |
3332 | + |
3333 | +private: |
3334 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
3335 | + PersistentCache(std::string const& cache_path); |
3336 | + |
3337 | + std::unique_ptr<PersistentStringCache> p_; |
3338 | +}; |
3339 | + |
3340 | +template <typename K, typename M> |
3341 | +PersistentCache<K, std::string, M>::PersistentCache(std::string const& cache_path, |
3342 | + int64_t max_size_in_bytes, |
3343 | + CacheDiscardPolicy policy) |
3344 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
3345 | +{ |
3346 | +} |
3347 | + |
3348 | +template <typename K, typename M> |
3349 | +PersistentCache<K, std::string, M>::PersistentCache(std::string const& cache_path) |
3350 | + : p_(PersistentStringCache::open(cache_path)) |
3351 | +{ |
3352 | +} |
3353 | + |
3354 | +template <typename K, typename M> |
3355 | +typename PersistentCache<K, std::string, M>::UPtr PersistentCache<K, std::string, M>::open( |
3356 | + std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy) |
3357 | +{ |
3358 | + return PersistentCache<K, std::string, M>::UPtr( |
3359 | + new PersistentCache<K, std::string, M>(cache_path, max_size_in_bytes, policy)); |
3360 | +} |
3361 | + |
3362 | +template <typename K, typename M> |
3363 | +typename PersistentCache<K, std::string, M>::UPtr PersistentCache<K, std::string, M>::open( |
3364 | + std::string const& cache_path) |
3365 | +{ |
3366 | + return PersistentCache<K, std::string, M>::UPtr(new PersistentCache<K, std::string, M>(cache_path)); |
3367 | +} |
3368 | + |
3369 | +template <typename K, typename M> |
3370 | +typename PersistentCache<K, std::string, M>::OptionalValue PersistentCache<K, std::string, M>::get(K const& key) const |
3371 | +{ |
3372 | + auto const& svalue = p_->get(CacheCodec<K>::encode(key)); |
3373 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
3374 | +} |
3375 | + |
3376 | +template <typename K, typename M> |
3377 | +typename PersistentCache<K, std::string, M>::OptionalData PersistentCache<K, std::string, M>::get_data( |
3378 | + K const& key) const |
3379 | +{ |
3380 | + auto sdata = p_->get_data(CacheCodec<K>::encode(key)); |
3381 | + if (!sdata) |
3382 | + { |
3383 | + return OptionalData(); |
3384 | + } |
3385 | + return OptionalData({sdata->value, CacheCodec<M>::decode(sdata->metadata)}); |
3386 | +} |
3387 | + |
3388 | +template <typename K, typename M> |
3389 | +typename PersistentCache<K, std::string, M>::OptionalMetadata PersistentCache<K, std::string, M>::get_metadata( |
3390 | + K const& key) const |
3391 | +{ |
3392 | + auto smeta = p_->get_metadata(CacheCodec<K>::encode(key)); |
3393 | + return smeta ? OptionalMetadata(CacheCodec<M>::decode(*smeta)) : OptionalMetadata(); |
3394 | +} |
3395 | + |
3396 | +template <typename K, typename M> |
3397 | +bool PersistentCache<K, std::string, M>::contains_key(K const& key) const |
3398 | +{ |
3399 | + return p_->contains_key(CacheCodec<K>::encode(key)); |
3400 | +} |
3401 | + |
3402 | +template <typename K, typename M> |
3403 | +int64_t PersistentCache<K, std::string, M>::size() const noexcept |
3404 | +{ |
3405 | + return p_->size(); |
3406 | +} |
3407 | + |
3408 | +template <typename K, typename M> |
3409 | +int64_t PersistentCache<K, std::string, M>::size_in_bytes() const noexcept |
3410 | +{ |
3411 | + return p_->size_in_bytes(); |
3412 | +} |
3413 | + |
3414 | +template <typename K, typename M> |
3415 | +int64_t PersistentCache<K, std::string, M>::max_size_in_bytes() const noexcept |
3416 | +{ |
3417 | + return p_->max_size_in_bytes(); |
3418 | +} |
3419 | + |
3420 | +template <typename K, typename M> |
3421 | +int64_t PersistentCache<K, std::string, M>::disk_size_in_bytes() const |
3422 | +{ |
3423 | + return p_->disk_size_in_bytes(); |
3424 | +} |
3425 | + |
3426 | +template <typename K, typename M> |
3427 | +CacheDiscardPolicy PersistentCache<K, std::string, M>::discard_policy() const noexcept |
3428 | +{ |
3429 | + return p_->discard_policy(); |
3430 | +} |
3431 | + |
3432 | +template <typename K, typename M> |
3433 | +PersistentCacheStats PersistentCache<K, std::string, M>::stats() const |
3434 | +{ |
3435 | + return p_->stats(); |
3436 | +} |
3437 | + |
3438 | +template <typename K, typename M> |
3439 | +bool PersistentCache<K, std::string, M>::put(K const& key, |
3440 | + std::string const& value, |
3441 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3442 | +{ |
3443 | + return p_->put(CacheCodec<K>::encode(key), value, expiry_time); |
3444 | +} |
3445 | + |
3446 | +template <typename K, typename M> |
3447 | +bool PersistentCache<K, std::string, M>::put(K const& key, |
3448 | + char const* value, |
3449 | + int64_t size, |
3450 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3451 | +{ |
3452 | + return p_->put(CacheCodec<K>::encode(key), value, size, expiry_time); |
3453 | +} |
3454 | + |
3455 | +template <typename K, typename M> |
3456 | +bool PersistentCache<K, std::string, M>::put(K const& key, |
3457 | + std::string const& value, |
3458 | + M const& metadata, |
3459 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3460 | +{ |
3461 | + return p_->put(CacheCodec<K>::encode(key), value, CacheCodec<M>::encode(metadata), expiry_time); |
3462 | +} |
3463 | + |
3464 | +template <typename K, typename M> |
3465 | +bool PersistentCache<K, std::string, M>::put(K const& key, |
3466 | + char const* value, |
3467 | + int64_t size, |
3468 | + M const& metadata, |
3469 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3470 | +{ |
3471 | + std::string md = CacheCodec<M>::encode(metadata); |
3472 | + return p_->put(CacheCodec<K>::encode(key), value, size, md.data(), md.size(), expiry_time); |
3473 | +} |
3474 | + |
3475 | +template <typename K, typename M> |
3476 | +typename PersistentCache<K, std::string, M>::OptionalValue PersistentCache<K, std::string, M>::get_or_put( |
3477 | + K const& key, PersistentCache<K, std::string, M>::Loader const& load_func) |
3478 | +{ |
3479 | + std::string const& skey = CacheCodec<K>::encode(key); |
3480 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
3481 | + { |
3482 | + load_func(key, *this); |
3483 | + }; |
3484 | + auto svalue = p_->get_or_put(skey, sload_func); |
3485 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
3486 | +} |
3487 | + |
3488 | +template <typename K, typename M> |
3489 | +typename PersistentCache<K, std::string, M>::OptionalData PersistentCache<K, std::string, M>::get_or_put_data( |
3490 | + K const& key, PersistentCache<K, std::string, M>::Loader const& load_func) |
3491 | +{ |
3492 | + std::string const& skey = CacheCodec<K>::encode(key); |
3493 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
3494 | + { |
3495 | + load_func(key, *this); |
3496 | + }; |
3497 | + auto sdata = p_->get_or_put_data(skey, sload_func); |
3498 | + if (!sdata) |
3499 | + { |
3500 | + return OptionalData(); |
3501 | + } |
3502 | + return OptionalData({sdata->value, CacheCodec<M>::decode(sdata->metadata)}); |
3503 | +} |
3504 | + |
3505 | +template <typename K, typename M> |
3506 | +bool PersistentCache<K, std::string, M>::put_metadata(K const& key, M const& metadata) |
3507 | +{ |
3508 | + return p_->put_metadata(CacheCodec<K>::encode(key), CacheCodec<M>::encode(metadata)); |
3509 | +} |
3510 | + |
3511 | +template <typename K, typename M> |
3512 | +typename PersistentCache<K, std::string, M>::OptionalValue PersistentCache<K, std::string, M>::take(K const& key) |
3513 | +{ |
3514 | + auto svalue = p_->take(CacheCodec<K>::encode(key)); |
3515 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
3516 | +} |
3517 | + |
3518 | +template <typename K, typename M> |
3519 | +typename PersistentCache<K, std::string, M>::OptionalData PersistentCache<K, std::string, M>::take_data( |
3520 | + K const& key) |
3521 | +{ |
3522 | + auto sdata = p_->take_data(CacheCodec<K>::encode(key)); |
3523 | + if (!sdata) |
3524 | + { |
3525 | + return OptionalData(); |
3526 | + } |
3527 | + return OptionalData({sdata->value, CacheCodec<M>::decode(sdata->metadata)}); |
3528 | +} |
3529 | + |
3530 | +template <typename K, typename M> |
3531 | +bool PersistentCache<K, std::string, M>::invalidate(K const& key) |
3532 | +{ |
3533 | + return p_->invalidate(CacheCodec<K>::encode(key)); |
3534 | +} |
3535 | + |
3536 | +template <typename K, typename M> |
3537 | +void PersistentCache<K, std::string, M>::invalidate(std::vector<K> const& keys) |
3538 | +{ |
3539 | + invalidate(keys.begin(), keys.end()); |
3540 | +} |
3541 | + |
3542 | +template <typename K, typename M> |
3543 | +template <typename It> |
3544 | +void PersistentCache<K, std::string, M>::invalidate(It begin, It end) |
3545 | +{ |
3546 | + std::vector<std::string> skeys; |
3547 | + for (auto&& it = begin; it < end; ++it) |
3548 | + { |
3549 | + skeys.push_back(CacheCodec<K>::encode(*it)); |
3550 | + } |
3551 | + p_->invalidate(skeys.begin(), skeys.end()); |
3552 | +} |
3553 | + |
3554 | +template <typename K, typename M> |
3555 | +void PersistentCache<K, std::string, M>::invalidate(std::initializer_list<K> const& keys) |
3556 | +{ |
3557 | + invalidate(keys.begin(), keys.end()); |
3558 | +} |
3559 | + |
3560 | +template <typename K, typename M> |
3561 | +void PersistentCache<K, std::string, M>::invalidate() |
3562 | +{ |
3563 | + p_->invalidate(); |
3564 | +} |
3565 | + |
3566 | +template <typename K, typename M> |
3567 | +bool PersistentCache<K, std::string, M>::touch(K const& key, |
3568 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3569 | +{ |
3570 | + return p_->touch(CacheCodec<K>::encode(key), expiry_time); |
3571 | +} |
3572 | + |
3573 | +template <typename K, typename M> |
3574 | +void PersistentCache<K, std::string, M>::clear_stats() |
3575 | +{ |
3576 | + p_->clear_stats(); |
3577 | +} |
3578 | + |
3579 | +template <typename K, typename M> |
3580 | +void PersistentCache<K, std::string, M>::resize(int64_t size_in_bytes) |
3581 | +{ |
3582 | + p_->resize(size_in_bytes); |
3583 | +} |
3584 | + |
3585 | +template <typename K, typename M> |
3586 | +void PersistentCache<K, std::string, M>::trim_to(int64_t used_size_in_bytes) |
3587 | +{ |
3588 | + p_->trim_to(used_size_in_bytes); |
3589 | +} |
3590 | + |
3591 | +template <typename K, typename M> |
3592 | +void PersistentCache<K, std::string, M>::compact() |
3593 | +{ |
3594 | + p_->compact(); |
3595 | +} |
3596 | + |
3597 | +template <typename K, typename M> |
3598 | +void PersistentCache<K, std::string, M>::set_handler(CacheEvent events, EventCallback cb) |
3599 | +{ |
3600 | + auto scb = [cb](std::string const& key, CacheEvent ev, PersistentCacheStats const& c) |
3601 | + { |
3602 | + cb(CacheCodec<K>::decode(key), ev, c); |
3603 | + }; |
3604 | + p_->set_handler(events, scb); |
3605 | +} |
3606 | + |
3607 | +// Specialization for M = std::string. |
3608 | + |
3609 | +template <typename K, typename V> |
3610 | +class PersistentCache<K, V, std::string> |
3611 | +{ |
3612 | +public: |
3613 | + typedef std::unique_ptr<PersistentCache<K, V, std::string>> UPtr; |
3614 | + |
3615 | + typedef Optional<K> OptionalKey; |
3616 | + typedef Optional<V> OptionalValue; |
3617 | + typedef Optional<std::string> OptionalMetadata; |
3618 | + |
3619 | + struct Data |
3620 | + { |
3621 | + V value; |
3622 | + std::string metadata; |
3623 | + }; |
3624 | + typedef Optional<Data> OptionalData; |
3625 | + |
3626 | + PersistentCache(PersistentCache const&) = delete; |
3627 | + PersistentCache& operator=(PersistentCache const&) = delete; |
3628 | + |
3629 | + PersistentCache(PersistentCache&&) = default; |
3630 | + PersistentCache& operator=(PersistentCache&&) = default; |
3631 | + |
3632 | + ~PersistentCache() = default; |
3633 | + |
3634 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
3635 | + static UPtr open(std::string const& cache_path); |
3636 | + |
3637 | + OptionalValue get(K const& key) const; |
3638 | + OptionalData get_data(K const& key) const; |
3639 | + OptionalMetadata get_metadata(K const& key) const; |
3640 | + bool contains_key(K const& key) const; |
3641 | + int64_t size() const noexcept; |
3642 | + int64_t size_in_bytes() const noexcept; |
3643 | + int64_t max_size_in_bytes() const noexcept; |
3644 | + int64_t disk_size_in_bytes() const; |
3645 | + CacheDiscardPolicy discard_policy() const noexcept; |
3646 | + PersistentCacheStats stats() const; |
3647 | + |
3648 | + bool put(K const& key, |
3649 | + V const& value, |
3650 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3651 | + bool put(K const& key, |
3652 | + V const& value, |
3653 | + std::string const& metadata, |
3654 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3655 | + bool put(K const& key, |
3656 | + V const& value, |
3657 | + char const* metadata, |
3658 | + int64_t size, |
3659 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3660 | + |
3661 | + typedef std::function<void(K const& key, PersistentCache<K, V, std::string>& cache)> Loader; |
3662 | + |
3663 | + OptionalValue get_or_put(K const& key, Loader const& load_func); |
3664 | + OptionalData get_or_put_data(K const& key, Loader const& load_func); |
3665 | + |
3666 | + bool put_metadata(K const& key, std::string const& metadata); |
3667 | + bool put_metadata(K const& key, char const* metadata, int64_t size); |
3668 | + OptionalValue take(K const& key); |
3669 | + OptionalData take_data(K const& key); |
3670 | + bool invalidate(K const& key); |
3671 | + void invalidate(std::vector<K> const& keys); |
3672 | + template <typename It> |
3673 | + void invalidate(It begin, It end); |
3674 | + void invalidate(std::initializer_list<K> const& keys); |
3675 | + void invalidate(); |
3676 | + bool touch( |
3677 | + K const& key, |
3678 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
3679 | + void clear_stats(); |
3680 | + void resize(int64_t size_in_bytes); |
3681 | + void trim_to(int64_t used_size_in_bytes); |
3682 | + void compact(); |
3683 | + |
3684 | + typedef std::function<void(K const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
3685 | + |
3686 | + void set_handler(CacheEvent events, EventCallback cb); |
3687 | + |
3688 | +private: |
3689 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
3690 | + PersistentCache(std::string const& cache_path); |
3691 | + |
3692 | + std::unique_ptr<PersistentStringCache> p_; |
3693 | +}; |
3694 | + |
3695 | +template <typename K, typename V> |
3696 | +PersistentCache<K, V, std::string>::PersistentCache(std::string const& cache_path, |
3697 | + int64_t max_size_in_bytes, |
3698 | + CacheDiscardPolicy policy) |
3699 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
3700 | +{ |
3701 | +} |
3702 | + |
3703 | +template <typename K, typename V> |
3704 | +PersistentCache<K, V, std::string>::PersistentCache(std::string const& cache_path) |
3705 | + : p_(PersistentStringCache::open(cache_path)) |
3706 | +{ |
3707 | +} |
3708 | + |
3709 | +template <typename K, typename V> |
3710 | +typename PersistentCache<K, V, std::string>::UPtr PersistentCache<K, V, std::string>::open( |
3711 | + std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy) |
3712 | +{ |
3713 | + return PersistentCache<K, V, std::string>::UPtr( |
3714 | + new PersistentCache<K, V, std::string>(cache_path, max_size_in_bytes, policy)); |
3715 | +} |
3716 | + |
3717 | +template <typename K, typename V> |
3718 | +typename PersistentCache<K, V, std::string>::UPtr PersistentCache<K, V, std::string>::open( |
3719 | + std::string const& cache_path) |
3720 | +{ |
3721 | + return PersistentCache<K, V, std::string>::UPtr(new PersistentCache<K, V, std::string>(cache_path)); |
3722 | +} |
3723 | + |
3724 | +template <typename K, typename V> |
3725 | +typename PersistentCache<K, V, std::string>::OptionalValue PersistentCache<K, V, std::string>::get(K const& key) const |
3726 | +{ |
3727 | + auto const& svalue = p_->get(CacheCodec<K>::encode(key)); |
3728 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
3729 | +} |
3730 | + |
3731 | +template <typename K, typename V> |
3732 | +typename PersistentCache<K, V, std::string>::OptionalData PersistentCache<K, V, std::string>::get_data( |
3733 | + K const& key) const |
3734 | +{ |
3735 | + auto sdata = p_->get_data(CacheCodec<K>::encode(key)); |
3736 | + if (!sdata) |
3737 | + { |
3738 | + return OptionalData(); |
3739 | + } |
3740 | + return OptionalData({CacheCodec<V>::decode(sdata->value), sdata->metadata}); |
3741 | +} |
3742 | + |
3743 | +template <typename K, typename V> |
3744 | +typename PersistentCache<K, V, std::string>::OptionalMetadata PersistentCache<K, V, std::string>::get_metadata( |
3745 | + K const& key) const |
3746 | +{ |
3747 | + auto smeta = p_->get_metadata(CacheCodec<K>::encode(key)); |
3748 | + return smeta ? OptionalMetadata(*smeta) : OptionalMetadata(); |
3749 | +} |
3750 | + |
3751 | +template <typename K, typename V> |
3752 | +bool PersistentCache<K, V, std::string>::contains_key(K const& key) const |
3753 | +{ |
3754 | + return p_->contains_key(CacheCodec<K>::encode(key)); |
3755 | +} |
3756 | + |
3757 | +template <typename K, typename V> |
3758 | +int64_t PersistentCache<K, V, std::string>::size() const noexcept |
3759 | +{ |
3760 | + return p_->size(); |
3761 | +} |
3762 | + |
3763 | +template <typename K, typename V> |
3764 | +int64_t PersistentCache<K, V, std::string>::size_in_bytes() const noexcept |
3765 | +{ |
3766 | + return p_->size_in_bytes(); |
3767 | +} |
3768 | + |
3769 | +template <typename K, typename V> |
3770 | +int64_t PersistentCache<K, V, std::string>::max_size_in_bytes() const noexcept |
3771 | +{ |
3772 | + return p_->max_size_in_bytes(); |
3773 | +} |
3774 | + |
3775 | +template <typename K, typename V> |
3776 | +int64_t PersistentCache<K, V, std::string>::disk_size_in_bytes() const |
3777 | +{ |
3778 | + return p_->disk_size_in_bytes(); |
3779 | +} |
3780 | + |
3781 | +template <typename K, typename V> |
3782 | +CacheDiscardPolicy PersistentCache<K, V, std::string>::discard_policy() const noexcept |
3783 | +{ |
3784 | + return p_->discard_policy(); |
3785 | +} |
3786 | + |
3787 | +template <typename K, typename V> |
3788 | +PersistentCacheStats PersistentCache<K, V, std::string>::stats() const |
3789 | +{ |
3790 | + return p_->stats(); |
3791 | +} |
3792 | + |
3793 | +template <typename K, typename V> |
3794 | +bool PersistentCache<K, V, std::string>::put(K const& key, |
3795 | + V const& value, |
3796 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3797 | +{ |
3798 | + return p_->put(CacheCodec<K>::encode(key), CacheCodec<V>::encode(value), expiry_time); |
3799 | +} |
3800 | + |
3801 | +template <typename K, typename V> |
3802 | +bool PersistentCache<K, V, std::string>::put(K const& key, |
3803 | + V const& value, |
3804 | + std::string const& metadata, |
3805 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3806 | +{ |
3807 | + std::string v = CacheCodec<V>::encode(value); |
3808 | + return p_->put(CacheCodec<K>::encode(key), v.data(), v.size(), metadata.data(), metadata.size(), expiry_time); |
3809 | +} |
3810 | + |
3811 | +template <typename K, typename V> |
3812 | +bool PersistentCache<K, V, std::string>::put(K const& key, |
3813 | + V const& value, |
3814 | + char const* metadata, |
3815 | + int64_t size, |
3816 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3817 | +{ |
3818 | + std::string v = CacheCodec<V>::encode(value); |
3819 | + return p_->put(CacheCodec<K>::encode(key), v.data(), v.size(), metadata, size, expiry_time); |
3820 | +} |
3821 | + |
3822 | +template <typename K, typename V> |
3823 | +typename PersistentCache<K, V, std::string>::OptionalValue PersistentCache<K, V, std::string>::get_or_put( |
3824 | + K const& key, PersistentCache<K, V, std::string>::Loader const& load_func) |
3825 | +{ |
3826 | + std::string const& skey = CacheCodec<K>::encode(key); |
3827 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
3828 | + { |
3829 | + load_func(key, *this); |
3830 | + }; |
3831 | + auto svalue = p_->get_or_put(skey, sload_func); |
3832 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
3833 | +} |
3834 | + |
3835 | +template <typename K, typename V> |
3836 | +typename PersistentCache<K, V, std::string>::OptionalData PersistentCache<K, V, std::string>::get_or_put_data( |
3837 | + K const& key, PersistentCache<K, V, std::string>::Loader const& load_func) |
3838 | +{ |
3839 | + std::string const& skey = CacheCodec<K>::encode(key); |
3840 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
3841 | + { |
3842 | + load_func(key, *this); |
3843 | + }; |
3844 | + auto sdata = p_->get_or_put_data(skey, sload_func); |
3845 | + if (!sdata) |
3846 | + { |
3847 | + return OptionalData(); |
3848 | + } |
3849 | + return OptionalData({CacheCodec<V>::decode(sdata->value), sdata->metadata}); |
3850 | +} |
3851 | + |
3852 | +template <typename K, typename V> |
3853 | +bool PersistentCache<K, V, std::string>::put_metadata(K const& key, std::string const& metadata) |
3854 | +{ |
3855 | + return p_->put_metadata(CacheCodec<K>::encode(key), metadata); |
3856 | +} |
3857 | + |
3858 | +template <typename K, typename V> |
3859 | +bool PersistentCache<K, V, std::string>::put_metadata(K const& key, char const* metadata, int64_t size) |
3860 | +{ |
3861 | + return p_->put_metadata(CacheCodec<K>::encode(key), metadata, size); |
3862 | +} |
3863 | + |
3864 | +template <typename K, typename V> |
3865 | +typename PersistentCache<K, V, std::string>::OptionalValue PersistentCache<K, V, std::string>::take(K const& key) |
3866 | +{ |
3867 | + auto svalue = p_->take(CacheCodec<K>::encode(key)); |
3868 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
3869 | +} |
3870 | + |
3871 | +template <typename K, typename V> |
3872 | +typename PersistentCache<K, V, std::string>::OptionalData PersistentCache<K, V, std::string>::take_data( |
3873 | + K const& key) |
3874 | +{ |
3875 | + auto sdata = p_->take_data(CacheCodec<K>::encode(key)); |
3876 | + if (!sdata) |
3877 | + { |
3878 | + return OptionalData(); |
3879 | + } |
3880 | + return OptionalData({CacheCodec<V>::decode(sdata->value), sdata->metadata}); |
3881 | +} |
3882 | + |
3883 | +template <typename K, typename V> |
3884 | +bool PersistentCache<K, V, std::string>::invalidate(K const& key) |
3885 | +{ |
3886 | + return p_->invalidate(CacheCodec<K>::encode(key)); |
3887 | +} |
3888 | + |
3889 | +template <typename K, typename V> |
3890 | +void PersistentCache<K, V, std::string>::invalidate(std::vector<K> const& keys) |
3891 | +{ |
3892 | + invalidate(keys.begin(), keys.end()); |
3893 | +} |
3894 | + |
3895 | +template <typename K, typename V> |
3896 | +template <typename It> |
3897 | +void PersistentCache<K, V, std::string>::invalidate(It begin, It end) |
3898 | +{ |
3899 | + std::vector<std::string> skeys; |
3900 | + for (auto&& it = begin; it < end; ++it) |
3901 | + { |
3902 | + skeys.push_back(CacheCodec<K>::encode(*it)); |
3903 | + } |
3904 | + p_->invalidate(skeys.begin(), skeys.end()); |
3905 | +} |
3906 | + |
3907 | +template <typename K, typename V> |
3908 | +void PersistentCache<K, V, std::string>::invalidate(std::initializer_list<K> const& keys) |
3909 | +{ |
3910 | + invalidate(keys.begin(), keys.end()); |
3911 | +} |
3912 | + |
3913 | +template <typename K, typename V> |
3914 | +void PersistentCache<K, V, std::string>::invalidate() |
3915 | +{ |
3916 | + p_->invalidate(); |
3917 | +} |
3918 | + |
3919 | +template <typename K, typename V> |
3920 | +bool PersistentCache<K, V, std::string>::touch(K const& key, |
3921 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
3922 | +{ |
3923 | + return p_->touch(CacheCodec<K>::encode(key), expiry_time); |
3924 | +} |
3925 | + |
3926 | +template <typename K, typename V> |
3927 | +void PersistentCache<K, V, std::string>::clear_stats() |
3928 | +{ |
3929 | + p_->clear_stats(); |
3930 | +} |
3931 | + |
3932 | +template <typename K, typename V> |
3933 | +void PersistentCache<K, V, std::string>::resize(int64_t size_in_bytes) |
3934 | +{ |
3935 | + p_->resize(size_in_bytes); |
3936 | +} |
3937 | + |
3938 | +template <typename K, typename V> |
3939 | +void PersistentCache<K, V, std::string>::trim_to(int64_t used_size_in_bytes) |
3940 | +{ |
3941 | + p_->trim_to(used_size_in_bytes); |
3942 | +} |
3943 | + |
3944 | +template <typename K, typename V> |
3945 | +void PersistentCache<K, V, std::string>::compact() |
3946 | +{ |
3947 | + p_->compact(); |
3948 | +} |
3949 | + |
3950 | +template <typename K, typename V> |
3951 | +void PersistentCache<K, V, std::string>::set_handler(CacheEvent events, EventCallback cb) |
3952 | +{ |
3953 | + auto scb = [cb](std::string const& key, CacheEvent ev, PersistentCacheStats const& c) |
3954 | + { |
3955 | + cb(CacheCodec<K>::decode(key), ev, c); |
3956 | + }; |
3957 | + p_->set_handler(events, scb); |
3958 | +} |
3959 | + |
3960 | +// Specialization for K and V = std::string. |
3961 | + |
3962 | +template <typename M> |
3963 | +class PersistentCache<std::string, std::string, M> |
3964 | +{ |
3965 | +public: |
3966 | + typedef std::unique_ptr<PersistentCache<std::string, std::string, M>> UPtr; |
3967 | + |
3968 | + typedef Optional<std::string> OptionalKey; |
3969 | + typedef Optional<std::string> OptionalValue; |
3970 | + typedef Optional<M> OptionalMetadata; |
3971 | + |
3972 | + struct Data |
3973 | + { |
3974 | + std::string value; |
3975 | + M metadata; |
3976 | + }; |
3977 | + typedef Optional<Data> OptionalData; |
3978 | + |
3979 | + PersistentCache(PersistentCache const&) = delete; |
3980 | + PersistentCache& operator=(PersistentCache const&) = delete; |
3981 | + |
3982 | + PersistentCache(PersistentCache&&) = default; |
3983 | + PersistentCache& operator=(PersistentCache&&) = default; |
3984 | + |
3985 | + ~PersistentCache() = default; |
3986 | + |
3987 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
3988 | + static UPtr open(std::string const& cache_path); |
3989 | + |
3990 | + OptionalValue get(std::string const& key) const; |
3991 | + OptionalData get_data(std::string const& key) const; |
3992 | + OptionalMetadata get_metadata(std::string const& key) const; |
3993 | + bool contains_key(std::string const& key) const; |
3994 | + int64_t size() const noexcept; |
3995 | + int64_t size_in_bytes() const noexcept; |
3996 | + int64_t max_size_in_bytes() const noexcept; |
3997 | + int64_t disk_size_in_bytes() const; |
3998 | + CacheDiscardPolicy discard_policy() const noexcept; |
3999 | + PersistentCacheStats stats() const; |
4000 | + |
4001 | + bool put(std::string const& key, |
4002 | + std::string const& value, |
4003 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4004 | + bool put(std::string const& key, |
4005 | + char const* value, |
4006 | + int64_t size, |
4007 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4008 | + bool put(std::string const& key, |
4009 | + std::string const& value, |
4010 | + M const& metadata, |
4011 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4012 | + bool put(std::string const& key, |
4013 | + char const* value, |
4014 | + int64_t value_size, |
4015 | + M const& metadata, |
4016 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4017 | + |
4018 | + typedef std::function<void(std::string const& key, PersistentCache<std::string, std::string, M>& cache)> Loader; |
4019 | + |
4020 | + OptionalValue get_or_put(std::string const& key, Loader const& load_func); |
4021 | + OptionalData get_or_put_data(std::string const& key, Loader const& load_func); |
4022 | + |
4023 | + bool put_metadata(std::string const& key, M const& metadata); |
4024 | + OptionalValue take(std::string const& key); |
4025 | + OptionalData take_data(std::string const& key); |
4026 | + bool invalidate(std::string const& key); |
4027 | + void invalidate(std::vector<std::string> const& keys); |
4028 | + template <typename It> |
4029 | + void invalidate(It begin, It end); |
4030 | + void invalidate(std::initializer_list<std::string> const& keys); |
4031 | + void invalidate(); |
4032 | + bool touch( |
4033 | + std::string const& key, |
4034 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4035 | + void clear_stats(); |
4036 | + void resize(int64_t size_in_bytes); |
4037 | + void trim_to(int64_t used_size_in_bytes); |
4038 | + void compact(); |
4039 | + |
4040 | + typedef std::function<void(std::string const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
4041 | + |
4042 | + void set_handler(CacheEvent events, EventCallback cb); |
4043 | + |
4044 | +private: |
4045 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
4046 | + PersistentCache(std::string const& cache_path); |
4047 | + |
4048 | + std::unique_ptr<PersistentStringCache> p_; |
4049 | +}; |
4050 | + |
4051 | +template <typename M> |
4052 | +PersistentCache<std::string, std::string, M>::PersistentCache(std::string const& cache_path, |
4053 | + int64_t max_size_in_bytes, |
4054 | + CacheDiscardPolicy policy) |
4055 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
4056 | +{ |
4057 | +} |
4058 | + |
4059 | +template <typename M> |
4060 | +PersistentCache<std::string, std::string, M>::PersistentCache(std::string const& cache_path) |
4061 | + : p_(PersistentStringCache::open(cache_path)) |
4062 | +{ |
4063 | +} |
4064 | + |
4065 | +template <typename M> |
4066 | +typename PersistentCache<std::string, std::string, M>::UPtr PersistentCache<std::string, std::string, M>::open( |
4067 | + std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy) |
4068 | +{ |
4069 | + return PersistentCache<std::string, std::string, M>::UPtr( |
4070 | + new PersistentCache<std::string, std::string, M>(cache_path, max_size_in_bytes, policy)); |
4071 | +} |
4072 | + |
4073 | +template <typename M> |
4074 | +typename PersistentCache<std::string, std::string, M>::UPtr PersistentCache<std::string, std::string, M>::open( |
4075 | + std::string const& cache_path) |
4076 | +{ |
4077 | + return PersistentCache<std::string, std::string, M>::UPtr( |
4078 | + new PersistentCache<std::string, std::string, M>(cache_path)); |
4079 | +} |
4080 | + |
4081 | +template <typename M> |
4082 | +typename PersistentCache<std::string, std::string, M>::OptionalValue PersistentCache<std::string, std::string, M>::get( |
4083 | + std::string const& key) const |
4084 | +{ |
4085 | + auto const& svalue = p_->get(key); |
4086 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
4087 | +} |
4088 | + |
4089 | +template <typename M> |
4090 | +typename PersistentCache<std::string, std::string, M>::OptionalData |
4091 | + PersistentCache<std::string, std::string, M>::get_data(std::string const& key) const |
4092 | +{ |
4093 | + auto sdata = p_->get_data(key); |
4094 | + if (!sdata) |
4095 | + { |
4096 | + return OptionalData(); |
4097 | + } |
4098 | + return OptionalData({sdata->value, CacheCodec<M>::decode(sdata->metadata)}); |
4099 | +} |
4100 | + |
4101 | +template <typename M> |
4102 | +typename PersistentCache<std::string, std::string, M>::OptionalMetadata |
4103 | + PersistentCache<std::string, std::string, M>::get_metadata(std::string const& key) const |
4104 | +{ |
4105 | + auto smeta = p_->get_metadata(key); |
4106 | + return smeta ? OptionalMetadata(CacheCodec<M>::decode(*smeta)) : OptionalMetadata(); |
4107 | +} |
4108 | + |
4109 | +template <typename M> |
4110 | +bool PersistentCache<std::string, std::string, M>::contains_key(std::string const& key) const |
4111 | +{ |
4112 | + return p_->contains_key(key); |
4113 | +} |
4114 | + |
4115 | +template <typename M> |
4116 | +int64_t PersistentCache<std::string, std::string, M>::size() const noexcept |
4117 | +{ |
4118 | + return p_->size(); |
4119 | +} |
4120 | + |
4121 | +template <typename M> |
4122 | +int64_t PersistentCache<std::string, std::string, M>::size_in_bytes() const noexcept |
4123 | +{ |
4124 | + return p_->size_in_bytes(); |
4125 | +} |
4126 | + |
4127 | +template <typename M> |
4128 | +int64_t PersistentCache<std::string, std::string, M>::max_size_in_bytes() const noexcept |
4129 | +{ |
4130 | + return p_->max_size_in_bytes(); |
4131 | +} |
4132 | + |
4133 | +template <typename M> |
4134 | +int64_t PersistentCache<std::string, std::string, M>::disk_size_in_bytes() const |
4135 | +{ |
4136 | + return p_->disk_size_in_bytes(); |
4137 | +} |
4138 | + |
4139 | +template <typename M> |
4140 | +CacheDiscardPolicy PersistentCache<std::string, std::string, M>::discard_policy() const noexcept |
4141 | +{ |
4142 | + return p_->discard_policy(); |
4143 | +} |
4144 | + |
4145 | +template <typename M> |
4146 | +PersistentCacheStats PersistentCache<std::string, std::string, M>::stats() const |
4147 | +{ |
4148 | + return p_->stats(); |
4149 | +} |
4150 | + |
4151 | +template <typename M> |
4152 | +bool PersistentCache<std::string, std::string, M>::put(std::string const& key, |
4153 | + std::string const& value, |
4154 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4155 | +{ |
4156 | + return p_->put(key, value, expiry_time); |
4157 | +} |
4158 | + |
4159 | +template <typename M> |
4160 | +bool PersistentCache<std::string, std::string, M>::put(std::string const& key, |
4161 | + char const* value, |
4162 | + int64_t size, |
4163 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4164 | +{ |
4165 | + return p_->put(key, value, size, nullptr, 0, expiry_time); |
4166 | +} |
4167 | + |
4168 | +template <typename M> |
4169 | +bool PersistentCache<std::string, std::string, M>::put(std::string const& key, |
4170 | + std::string const& value, |
4171 | + M const& metadata, |
4172 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4173 | +{ |
4174 | + return p_->put(key, value, CacheCodec<M>::encode(metadata), expiry_time); |
4175 | +} |
4176 | + |
4177 | +template <typename M> |
4178 | +bool PersistentCache<std::string, std::string, M>::put(std::string const& key, |
4179 | + char const* value, |
4180 | + int64_t size, |
4181 | + M const& metadata, |
4182 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4183 | +{ |
4184 | + std::string md = CacheCodec<M>::encode(metadata); |
4185 | + return p_->put(key, value, size, md.data(), md.size(), expiry_time); |
4186 | +} |
4187 | + |
4188 | +template <typename M> |
4189 | +typename PersistentCache<std::string, std::string, M>::OptionalValue |
4190 | + PersistentCache<std::string, std::string, M>::get_or_put( |
4191 | + std::string const& key, PersistentCache<std::string, std::string, M>::Loader const& load_func) |
4192 | +{ |
4193 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
4194 | + { |
4195 | + load_func(key, *this); |
4196 | + }; |
4197 | + auto svalue = p_->get_or_put(key, sload_func); |
4198 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
4199 | +} |
4200 | + |
4201 | +template <typename M> |
4202 | +typename PersistentCache<std::string, std::string, M>::OptionalData |
4203 | + PersistentCache<std::string, std::string, M>::get_or_put_data( |
4204 | + std::string const& key, PersistentCache<std::string, std::string, M>::Loader const& load_func) |
4205 | +{ |
4206 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
4207 | + { |
4208 | + load_func(key, *this); |
4209 | + }; |
4210 | + auto sdata = p_->get_or_put_data(key, sload_func); |
4211 | + if (!sdata) |
4212 | + { |
4213 | + return OptionalData(); |
4214 | + } |
4215 | + return OptionalData({sdata->value, CacheCodec<M>::decode(sdata->metadata)}); |
4216 | +} |
4217 | + |
4218 | +template <typename M> |
4219 | +bool PersistentCache<std::string, std::string, M>::put_metadata(std::string const& key, M const& metadata) |
4220 | +{ |
4221 | + return p_->put_metadata(key, CacheCodec<M>::encode(metadata)); |
4222 | +} |
4223 | + |
4224 | +template <typename M> |
4225 | +typename PersistentCache<std::string, std::string, M>::OptionalValue PersistentCache<std::string, std::string, M>::take( |
4226 | + std::string const& key) |
4227 | +{ |
4228 | + auto svalue = p_->take(key); |
4229 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
4230 | +} |
4231 | + |
4232 | +template <typename M> |
4233 | +typename PersistentCache<std::string, std::string, M>::OptionalData |
4234 | + PersistentCache<std::string, std::string, M>::take_data(std::string const& key) |
4235 | +{ |
4236 | + auto sdata = p_->take_data(key); |
4237 | + if (!sdata) |
4238 | + { |
4239 | + return OptionalData(); |
4240 | + } |
4241 | + return OptionalData({sdata->value, CacheCodec<M>::decode(sdata->metadata)}); |
4242 | +} |
4243 | + |
4244 | +template <typename M> |
4245 | +bool PersistentCache<std::string, std::string, M>::invalidate(std::string const& key) |
4246 | +{ |
4247 | + return p_->invalidate(key); |
4248 | +} |
4249 | + |
4250 | +template <typename M> |
4251 | +void PersistentCache<std::string, std::string, M>::invalidate(std::vector<std::string> const& keys) |
4252 | +{ |
4253 | + invalidate(keys.begin(), keys.end()); |
4254 | +} |
4255 | + |
4256 | +template <typename M> |
4257 | +template <typename It> |
4258 | +void PersistentCache<std::string, std::string, M>::invalidate(It begin, It end) |
4259 | +{ |
4260 | + std::vector<std::string> skeys; |
4261 | + for (auto&& it = begin; it < end; ++it) |
4262 | + { |
4263 | + skeys.push_back(*it); |
4264 | + } |
4265 | + p_->invalidate(skeys.begin(), skeys.end()); |
4266 | +} |
4267 | + |
4268 | +template <typename M> |
4269 | +void PersistentCache<std::string, std::string, M>::invalidate(std::initializer_list<std::string> const& keys) |
4270 | +{ |
4271 | + invalidate(keys.begin(), keys.end()); |
4272 | +} |
4273 | + |
4274 | +template <typename M> |
4275 | +void PersistentCache<std::string, std::string, M>::invalidate() |
4276 | +{ |
4277 | + p_->invalidate(); |
4278 | +} |
4279 | + |
4280 | +template <typename M> |
4281 | +bool PersistentCache<std::string, std::string, M>::touch(std::string const& key, |
4282 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4283 | +{ |
4284 | + return p_->touch(key, expiry_time); |
4285 | +} |
4286 | + |
4287 | +template <typename M> |
4288 | +void PersistentCache<std::string, std::string, M>::clear_stats() |
4289 | +{ |
4290 | + p_->clear_stats(); |
4291 | +} |
4292 | + |
4293 | +template <typename M> |
4294 | +void PersistentCache<std::string, std::string, M>::resize(int64_t size_in_bytes) |
4295 | +{ |
4296 | + p_->resize(size_in_bytes); |
4297 | +} |
4298 | + |
4299 | +template <typename M> |
4300 | +void PersistentCache<std::string, std::string, M>::trim_to(int64_t used_size_in_bytes) |
4301 | +{ |
4302 | + p_->trim_to(used_size_in_bytes); |
4303 | +} |
4304 | + |
4305 | +template <typename M> |
4306 | +void PersistentCache<std::string, std::string, M>::compact() |
4307 | +{ |
4308 | + p_->compact(); |
4309 | +} |
4310 | + |
4311 | +template <typename M> |
4312 | +void PersistentCache<std::string, std::string, M>::set_handler(CacheEvent events, EventCallback cb) |
4313 | +{ |
4314 | + p_->set_handler(events, cb); |
4315 | +} |
4316 | + |
4317 | +// Specialization for K and M = std::string. |
4318 | + |
4319 | +template <typename V> |
4320 | +class PersistentCache<std::string, V, std::string> |
4321 | +{ |
4322 | +public: |
4323 | + typedef std::unique_ptr<PersistentCache<std::string, V, std::string>> UPtr; |
4324 | + |
4325 | + typedef Optional<std::string> OptionalKey; |
4326 | + typedef Optional<V> OptionalValue; |
4327 | + typedef Optional<std::string> OptionalMetadata; |
4328 | + |
4329 | + struct Data |
4330 | + { |
4331 | + V value; |
4332 | + std::string metadata; |
4333 | + }; |
4334 | + typedef Optional<Data> OptionalData; |
4335 | + |
4336 | + PersistentCache(PersistentCache const&) = delete; |
4337 | + PersistentCache& operator=(PersistentCache const&) = delete; |
4338 | + |
4339 | + PersistentCache(PersistentCache&&) = default; |
4340 | + PersistentCache& operator=(PersistentCache&&) = default; |
4341 | + |
4342 | + ~PersistentCache() = default; |
4343 | + |
4344 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
4345 | + static UPtr open(std::string const& cache_path); |
4346 | + |
4347 | + OptionalValue get(std::string const& key) const; |
4348 | + OptionalData get_data(std::string const& key) const; |
4349 | + OptionalMetadata get_metadata(std::string const& key) const; |
4350 | + bool contains_key(std::string const& key) const; |
4351 | + int64_t size() const noexcept; |
4352 | + int64_t size_in_bytes() const noexcept; |
4353 | + int64_t max_size_in_bytes() const noexcept; |
4354 | + int64_t disk_size_in_bytes() const; |
4355 | + CacheDiscardPolicy discard_policy() const noexcept; |
4356 | + PersistentCacheStats stats() const; |
4357 | + |
4358 | + bool put(std::string const& key, |
4359 | + V const& value, |
4360 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4361 | + bool put(std::string const& key, |
4362 | + V const& value, |
4363 | + std::string const& metadata, |
4364 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4365 | + bool put(std::string const& key, |
4366 | + V const& value, |
4367 | + char const* metadata, |
4368 | + int64_t size, |
4369 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4370 | + |
4371 | + typedef std::function<void(std::string const& key, PersistentCache<std::string, V, std::string>& cache)> Loader; |
4372 | + |
4373 | + OptionalValue get_or_put(std::string const& key, Loader const& load_func); |
4374 | + OptionalData get_or_put_data(std::string const& key, Loader const& load_func); |
4375 | + |
4376 | + bool put_metadata(std::string const& key, std::string const& metadata); |
4377 | + bool put_metadata(std::string const& key, char const* metadata, int64_t size); |
4378 | + OptionalValue take(std::string const& key); |
4379 | + OptionalData take_data(std::string const& key); |
4380 | + bool invalidate(std::string const& key); |
4381 | + void invalidate(std::vector<std::string> const& keys); |
4382 | + template <typename It> |
4383 | + void invalidate(It begin, It end); |
4384 | + void invalidate(std::initializer_list<std::string> const& keys); |
4385 | + void invalidate(); |
4386 | + bool touch( |
4387 | + std::string const& key, |
4388 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4389 | + void clear_stats(); |
4390 | + void resize(int64_t size_in_bytes); |
4391 | + void trim_to(int64_t used_size_in_bytes); |
4392 | + void compact(); |
4393 | + |
4394 | + typedef std::function<void(std::string const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
4395 | + |
4396 | + void set_handler(CacheEvent events, EventCallback cb); |
4397 | + |
4398 | +private: |
4399 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
4400 | + PersistentCache(std::string const& cache_path); |
4401 | + |
4402 | + std::unique_ptr<PersistentStringCache> p_; |
4403 | +}; |
4404 | + |
4405 | +template <typename V> |
4406 | +PersistentCache<std::string, V, std::string>::PersistentCache(std::string const& cache_path, |
4407 | + int64_t max_size_in_bytes, |
4408 | + CacheDiscardPolicy policy) |
4409 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
4410 | +{ |
4411 | +} |
4412 | + |
4413 | +template <typename V> |
4414 | +PersistentCache<std::string, V, std::string>::PersistentCache(std::string const& cache_path) |
4415 | + : p_(PersistentStringCache::open(cache_path)) |
4416 | +{ |
4417 | +} |
4418 | + |
4419 | +template <typename V> |
4420 | +typename PersistentCache<std::string, V, std::string>::UPtr PersistentCache<std::string, V, std::string>::open( |
4421 | + std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy) |
4422 | +{ |
4423 | + return PersistentCache<std::string, V, std::string>::UPtr( |
4424 | + new PersistentCache<std::string, V, std::string>(cache_path, max_size_in_bytes, policy)); |
4425 | +} |
4426 | + |
4427 | +template <typename V> |
4428 | +typename PersistentCache<std::string, V, std::string>::UPtr PersistentCache<std::string, V, std::string>::open( |
4429 | + std::string const& cache_path) |
4430 | +{ |
4431 | + return PersistentCache<std::string, V, std::string>::UPtr( |
4432 | + new PersistentCache<std::string, V, std::string>(cache_path)); |
4433 | +} |
4434 | + |
4435 | +template <typename V> |
4436 | +typename PersistentCache<std::string, V, std::string>::OptionalValue PersistentCache<std::string, V, std::string>::get( |
4437 | + std::string const& key) const |
4438 | +{ |
4439 | + auto const& svalue = p_->get(key); |
4440 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
4441 | +} |
4442 | + |
4443 | +template <typename V> |
4444 | +typename PersistentCache<std::string, V, std::string>::OptionalData |
4445 | + PersistentCache<std::string, V, std::string>::get_data(std::string const& key) const |
4446 | +{ |
4447 | + auto sdata = p_->get_data(key); |
4448 | + if (!sdata) |
4449 | + { |
4450 | + return OptionalData(); |
4451 | + } |
4452 | + return OptionalData({CacheCodec<V>::decode(sdata->value), sdata->metadata}); |
4453 | +} |
4454 | + |
4455 | +template <typename V> |
4456 | +typename PersistentCache<std::string, V, std::string>::OptionalMetadata |
4457 | + PersistentCache<std::string, V, std::string>::get_metadata(std::string const& key) const |
4458 | +{ |
4459 | + auto smeta = p_->get_metadata(key); |
4460 | + return smeta ? OptionalMetadata(*smeta) : OptionalMetadata(); |
4461 | +} |
4462 | + |
4463 | +template <typename V> |
4464 | +bool PersistentCache<std::string, V, std::string>::contains_key(std::string const& key) const |
4465 | +{ |
4466 | + return p_->contains_key(key); |
4467 | +} |
4468 | + |
4469 | +template <typename V> |
4470 | +int64_t PersistentCache<std::string, V, std::string>::size() const noexcept |
4471 | +{ |
4472 | + return p_->size(); |
4473 | +} |
4474 | + |
4475 | +template <typename V> |
4476 | +int64_t PersistentCache<std::string, V, std::string>::size_in_bytes() const noexcept |
4477 | +{ |
4478 | + return p_->size_in_bytes(); |
4479 | +} |
4480 | + |
4481 | +template <typename V> |
4482 | +int64_t PersistentCache<std::string, V, std::string>::max_size_in_bytes() const noexcept |
4483 | +{ |
4484 | + return p_->max_size_in_bytes(); |
4485 | +} |
4486 | + |
4487 | +template <typename V> |
4488 | +int64_t PersistentCache<std::string, V, std::string>::disk_size_in_bytes() const |
4489 | +{ |
4490 | + return p_->disk_size_in_bytes(); |
4491 | +} |
4492 | + |
4493 | +template <typename V> |
4494 | +CacheDiscardPolicy PersistentCache<std::string, V, std::string>::discard_policy() const noexcept |
4495 | +{ |
4496 | + return p_->discard_policy(); |
4497 | +} |
4498 | + |
4499 | +template <typename V> |
4500 | +PersistentCacheStats PersistentCache<std::string, V, std::string>::stats() const |
4501 | +{ |
4502 | + return p_->stats(); |
4503 | +} |
4504 | + |
4505 | +template <typename V> |
4506 | +bool PersistentCache<std::string, V, std::string>::put(std::string const& key, |
4507 | + V const& value, |
4508 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4509 | +{ |
4510 | + return p_->put(key, CacheCodec<V>::encode(value), expiry_time); |
4511 | +} |
4512 | + |
4513 | +template <typename V> |
4514 | +bool PersistentCache<std::string, V, std::string>::put(std::string const& key, |
4515 | + V const& value, |
4516 | + std::string const& metadata, |
4517 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4518 | +{ |
4519 | + std::string v = CacheCodec<V>::encode(value); |
4520 | + return p_->put(key, v.data(), v.size(), metadata.data(), metadata.size(), expiry_time); |
4521 | +} |
4522 | + |
4523 | +template <typename V> |
4524 | +bool PersistentCache<std::string, V, std::string>::put(std::string const& key, |
4525 | + V const& value, |
4526 | + char const* metadata, |
4527 | + int64_t size, |
4528 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4529 | +{ |
4530 | + std::string v = CacheCodec<V>::encode(value); |
4531 | + return p_->put(key, v.data(), v.size(), metadata, size, expiry_time); |
4532 | +} |
4533 | + |
4534 | +template <typename V> |
4535 | +typename PersistentCache<std::string, V, std::string>::OptionalValue |
4536 | + PersistentCache<std::string, V, std::string>::get_or_put( |
4537 | + std::string const& key, PersistentCache<std::string, V, std::string>::Loader const& load_func) |
4538 | +{ |
4539 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
4540 | + { |
4541 | + load_func(key, *this); |
4542 | + }; |
4543 | + auto svalue = p_->get_or_put(key, sload_func); |
4544 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
4545 | +} |
4546 | + |
4547 | +template <typename V> |
4548 | +typename PersistentCache<std::string, V, std::string>::OptionalData |
4549 | + PersistentCache<std::string, V, std::string>::get_or_put_data( |
4550 | + std::string const& key, PersistentCache<std::string, V, std::string>::Loader const& load_func) |
4551 | +{ |
4552 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
4553 | + { |
4554 | + load_func(key, *this); |
4555 | + }; |
4556 | + auto sdata = p_->get_or_put_data(key, sload_func); |
4557 | + if (!sdata) |
4558 | + { |
4559 | + return OptionalData(); |
4560 | + } |
4561 | + return OptionalData({CacheCodec<V>::decode(sdata->value), sdata->metadata}); |
4562 | +} |
4563 | + |
4564 | +template <typename V> |
4565 | +bool PersistentCache<std::string, V, std::string>::put_metadata(std::string const& key, std::string const& metadata) |
4566 | +{ |
4567 | + return p_->put_metadata(key, metadata); |
4568 | +} |
4569 | + |
4570 | +template <typename V> |
4571 | +bool PersistentCache<std::string, V, std::string>::put_metadata(std::string const& key, |
4572 | + char const* metadata, |
4573 | + int64_t size) |
4574 | +{ |
4575 | + return p_->put_metadata(key, metadata, size); |
4576 | +} |
4577 | + |
4578 | +template <typename V> |
4579 | +typename PersistentCache<std::string, V, std::string>::OptionalValue PersistentCache<std::string, V, std::string>::take( |
4580 | + std::string const& key) |
4581 | +{ |
4582 | + auto svalue = p_->take(key); |
4583 | + return svalue ? OptionalValue(CacheCodec<V>::decode(*svalue)) : OptionalValue(); |
4584 | +} |
4585 | + |
4586 | +template <typename V> |
4587 | +typename PersistentCache<std::string, V, std::string>::OptionalData |
4588 | + PersistentCache<std::string, V, std::string>::take_data(std::string const& key) |
4589 | +{ |
4590 | + auto sdata = p_->take_data(key); |
4591 | + if (!sdata) |
4592 | + { |
4593 | + return OptionalData(); |
4594 | + } |
4595 | + return OptionalData({CacheCodec<V>::decode(sdata->value), sdata->metadata}); |
4596 | +} |
4597 | + |
4598 | +template <typename V> |
4599 | +bool PersistentCache<std::string, V, std::string>::invalidate(std::string const& key) |
4600 | +{ |
4601 | + return p_->invalidate(key); |
4602 | +} |
4603 | + |
4604 | +template <typename V> |
4605 | +void PersistentCache<std::string, V, std::string>::invalidate(std::vector<std::string> const& keys) |
4606 | +{ |
4607 | + invalidate(keys.begin(), keys.end()); |
4608 | +} |
4609 | + |
4610 | +template <typename V> |
4611 | +template <typename It> |
4612 | +void PersistentCache<std::string, V, std::string>::invalidate(It begin, It end) |
4613 | +{ |
4614 | + std::vector<std::string> skeys; |
4615 | + for (auto&& it = begin; it < end; ++it) |
4616 | + { |
4617 | + skeys.push_back(*it); |
4618 | + } |
4619 | + p_->invalidate(skeys.begin(), skeys.end()); |
4620 | +} |
4621 | + |
4622 | +template <typename V> |
4623 | +void PersistentCache<std::string, V, std::string>::invalidate(std::initializer_list<std::string> const& keys) |
4624 | +{ |
4625 | + invalidate(keys.begin(), keys.end()); |
4626 | +} |
4627 | + |
4628 | +template <typename V> |
4629 | +void PersistentCache<std::string, V, std::string>::invalidate() |
4630 | +{ |
4631 | + p_->invalidate(); |
4632 | +} |
4633 | + |
4634 | +template <typename V> |
4635 | +bool PersistentCache<std::string, V, std::string>::touch(std::string const& key, |
4636 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4637 | +{ |
4638 | + return p_->touch(key, expiry_time); |
4639 | +} |
4640 | + |
4641 | +template <typename V> |
4642 | +void PersistentCache<std::string, V, std::string>::clear_stats() |
4643 | +{ |
4644 | + p_->clear_stats(); |
4645 | +} |
4646 | + |
4647 | +template <typename V> |
4648 | +void PersistentCache<std::string, V, std::string>::resize(int64_t size_in_bytes) |
4649 | +{ |
4650 | + p_->resize(size_in_bytes); |
4651 | +} |
4652 | + |
4653 | +template <typename V> |
4654 | +void PersistentCache<std::string, V, std::string>::trim_to(int64_t used_size_in_bytes) |
4655 | +{ |
4656 | + p_->trim_to(used_size_in_bytes); |
4657 | +} |
4658 | + |
4659 | +template <typename V> |
4660 | +void PersistentCache<std::string, V, std::string>::compact() |
4661 | +{ |
4662 | + p_->compact(); |
4663 | +} |
4664 | + |
4665 | +template <typename V> |
4666 | +void PersistentCache<std::string, V, std::string>::set_handler(CacheEvent events, EventCallback cb) |
4667 | +{ |
4668 | + p_->set_handler(events, cb); |
4669 | +} |
4670 | + |
4671 | +// Specialization for V and M = std::string. |
4672 | + |
4673 | +template <typename K> |
4674 | +class PersistentCache<K, std::string, std::string> |
4675 | +{ |
4676 | +public: |
4677 | + typedef std::unique_ptr<PersistentCache<K, std::string, std::string>> UPtr; |
4678 | + |
4679 | + typedef Optional<K> OptionalKey; |
4680 | + typedef Optional<std::string> OptionalValue; |
4681 | + typedef Optional<std::string> OptionalMetadata; |
4682 | + |
4683 | + struct Data |
4684 | + { |
4685 | + std::string value; |
4686 | + std::string metadata; |
4687 | + }; |
4688 | + typedef Optional<Data> OptionalData; |
4689 | + |
4690 | + PersistentCache(PersistentCache const&) = delete; |
4691 | + PersistentCache& operator=(PersistentCache const&) = delete; |
4692 | + |
4693 | + PersistentCache(PersistentCache&&) = default; |
4694 | + PersistentCache& operator=(PersistentCache&&) = default; |
4695 | + |
4696 | + ~PersistentCache() = default; |
4697 | + |
4698 | + static UPtr open(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
4699 | + static UPtr open(std::string const& cache_path); |
4700 | + |
4701 | + OptionalValue get(K const& key) const; |
4702 | + OptionalData get_data(K const& key) const; |
4703 | + OptionalMetadata get_metadata(K const& key) const; |
4704 | + bool contains_key(K const& key) const; |
4705 | + int64_t size() const noexcept; |
4706 | + int64_t size_in_bytes() const noexcept; |
4707 | + int64_t max_size_in_bytes() const noexcept; |
4708 | + int64_t disk_size_in_bytes() const; |
4709 | + CacheDiscardPolicy discard_policy() const noexcept; |
4710 | + PersistentCacheStats stats() const; |
4711 | + |
4712 | + bool put(K const& key, |
4713 | + std::string const& value, |
4714 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4715 | + bool put(K const& key, |
4716 | + char const* value, |
4717 | + int64_t size, |
4718 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4719 | + bool put(K const& key, |
4720 | + std::string const& value, |
4721 | + std::string const& metadata, |
4722 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4723 | + bool put(K const& key, |
4724 | + char const* value, |
4725 | + int64_t value_size, |
4726 | + char const* metadata, |
4727 | + int64_t metadata_size, |
4728 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4729 | + |
4730 | + typedef std::function<void(K const& key, PersistentCache<K, std::string, std::string>& cache)> Loader; |
4731 | + |
4732 | + OptionalValue get_or_put(K const& key, Loader const& load_func); |
4733 | + OptionalData get_or_put_data(K const& key, Loader const& load_func); |
4734 | + |
4735 | + bool put_metadata(K const& key, std::string const& metadata); |
4736 | + bool put_metadata(K const& key, char const* metadata, int64_t size); |
4737 | + OptionalValue take(K const& key); |
4738 | + OptionalData take_data(K const& key); |
4739 | + bool invalidate(K const& key); |
4740 | + void invalidate(std::vector<K> const& keys); |
4741 | + template <typename It> |
4742 | + void invalidate(It begin, It end); |
4743 | + void invalidate(std::initializer_list<K> const& keys); |
4744 | + void invalidate(); |
4745 | + bool touch( |
4746 | + K const& key, |
4747 | + std::chrono::time_point<std::chrono::system_clock> expiry_time = std::chrono::system_clock::time_point()); |
4748 | + void clear_stats(); |
4749 | + void resize(int64_t size_in_bytes); |
4750 | + void trim_to(int64_t used_size_in_bytes); |
4751 | + void compact(); |
4752 | + |
4753 | + typedef std::function<void(K const& key, CacheEvent ev, PersistentCacheStats const& stats)> EventCallback; |
4754 | + |
4755 | + void set_handler(CacheEvent events, EventCallback cb); |
4756 | + |
4757 | +private: |
4758 | + PersistentCache(std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy); |
4759 | + PersistentCache(std::string const& cache_path); |
4760 | + |
4761 | + std::unique_ptr<PersistentStringCache> p_; |
4762 | +}; |
4763 | + |
4764 | +template <typename K> |
4765 | +PersistentCache<K, std::string, std::string>::PersistentCache(std::string const& cache_path, |
4766 | + int64_t max_size_in_bytes, |
4767 | + CacheDiscardPolicy policy) |
4768 | + : p_(PersistentStringCache::open(cache_path, max_size_in_bytes, policy)) |
4769 | +{ |
4770 | +} |
4771 | + |
4772 | +template <typename K> |
4773 | +PersistentCache<K, std::string, std::string>::PersistentCache(std::string const& cache_path) |
4774 | + : p_(PersistentStringCache::open(cache_path)) |
4775 | +{ |
4776 | +} |
4777 | + |
4778 | +template <typename K> |
4779 | +typename PersistentCache<K, std::string, std::string>::UPtr PersistentCache<K, std::string, std::string>::open( |
4780 | + std::string const& cache_path, int64_t max_size_in_bytes, CacheDiscardPolicy policy) |
4781 | +{ |
4782 | + return PersistentCache<K, std::string, std::string>::UPtr( |
4783 | + new PersistentCache<K, std::string, std::string>(cache_path, max_size_in_bytes, policy)); |
4784 | +} |
4785 | + |
4786 | +template <typename K> |
4787 | +typename PersistentCache<K, std::string, std::string>::UPtr PersistentCache<K, std::string, std::string>::open( |
4788 | + std::string const& cache_path) |
4789 | +{ |
4790 | + return PersistentCache<K, std::string, std::string>::UPtr( |
4791 | + new PersistentCache<K, std::string, std::string>(cache_path)); |
4792 | +} |
4793 | + |
4794 | +template <typename K> |
4795 | +typename PersistentCache<K, std::string, std::string>::OptionalValue PersistentCache<K, std::string, std::string>::get( |
4796 | + K const& key) const |
4797 | +{ |
4798 | + auto const& svalue = p_->get(CacheCodec<K>::encode(key)); |
4799 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
4800 | +} |
4801 | + |
4802 | +template <typename K> |
4803 | +typename PersistentCache<K, std::string, std::string>::OptionalData |
4804 | + PersistentCache<K, std::string, std::string>::get_data(K const& key) const |
4805 | +{ |
4806 | + auto sdata = p_->get_data(CacheCodec<K>::encode(key)); |
4807 | + if (!sdata) |
4808 | + { |
4809 | + return OptionalData(); |
4810 | + } |
4811 | + return OptionalData({sdata->value, sdata->metadata}); |
4812 | +} |
4813 | + |
4814 | +template <typename K> |
4815 | +typename PersistentCache<K, std::string, std::string>::OptionalMetadata |
4816 | + PersistentCache<K, std::string, std::string>::get_metadata(K const& key) const |
4817 | +{ |
4818 | + auto smeta = p_->get_metadata(CacheCodec<K>::encode(key)); |
4819 | + return smeta ? OptionalMetadata(*smeta) : OptionalMetadata(); |
4820 | +} |
4821 | + |
4822 | +template <typename K> |
4823 | +bool PersistentCache<K, std::string, std::string>::contains_key(K const& key) const |
4824 | +{ |
4825 | + return p_->contains_key(CacheCodec<K>::encode(key)); |
4826 | +} |
4827 | + |
4828 | +template <typename K> |
4829 | +int64_t PersistentCache<K, std::string, std::string>::size() const noexcept |
4830 | +{ |
4831 | + return p_->size(); |
4832 | +} |
4833 | + |
4834 | +template <typename K> |
4835 | +int64_t PersistentCache<K, std::string, std::string>::size_in_bytes() const noexcept |
4836 | +{ |
4837 | + return p_->size_in_bytes(); |
4838 | +} |
4839 | + |
4840 | +template <typename K> |
4841 | +int64_t PersistentCache<K, std::string, std::string>::max_size_in_bytes() const noexcept |
4842 | +{ |
4843 | + return p_->max_size_in_bytes(); |
4844 | +} |
4845 | + |
4846 | +template <typename K> |
4847 | +int64_t PersistentCache<K, std::string, std::string>::disk_size_in_bytes() const |
4848 | +{ |
4849 | + return p_->disk_size_in_bytes(); |
4850 | +} |
4851 | + |
4852 | +template <typename K> |
4853 | +CacheDiscardPolicy PersistentCache<K, std::string, std::string>::discard_policy() const noexcept |
4854 | +{ |
4855 | + return p_->discard_policy(); |
4856 | +} |
4857 | + |
4858 | +template <typename K> |
4859 | +PersistentCacheStats PersistentCache<K, std::string, std::string>::stats() const |
4860 | +{ |
4861 | + return p_->stats(); |
4862 | +} |
4863 | + |
4864 | +template <typename K> |
4865 | +bool PersistentCache<K, std::string, std::string>::put(K const& key, |
4866 | + std::string const& value, |
4867 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4868 | +{ |
4869 | + return p_->put(CacheCodec<K>::encode(key), value, expiry_time); |
4870 | +} |
4871 | + |
4872 | +template <typename K> |
4873 | +bool PersistentCache<K, std::string, std::string>::put(K const& key, |
4874 | + char const* value, |
4875 | + int64_t size, |
4876 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4877 | +{ |
4878 | + return p_->put(CacheCodec<K>::encode(key), value, size, expiry_time); |
4879 | +} |
4880 | + |
4881 | +template <typename K> |
4882 | +bool PersistentCache<K, std::string, std::string>::put(K const& key, |
4883 | + std::string const& value, |
4884 | + std::string const& metadata, |
4885 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4886 | +{ |
4887 | + return p_->put(CacheCodec<K>::encode(key), value, metadata, expiry_time); |
4888 | +} |
4889 | + |
4890 | +template <typename K> |
4891 | +bool PersistentCache<K, std::string, std::string>::put(K const& key, |
4892 | + char const* value, |
4893 | + int64_t value_size, |
4894 | + char const* metadata, |
4895 | + int64_t metadata_size, |
4896 | + std::chrono::time_point<std::chrono::system_clock> expiry_time) |
4897 | +{ |
4898 | + return p_->put(CacheCodec<K>::encode(key), value, value_size, metadata, metadata_size, expiry_time); |
4899 | +} |
4900 | + |
4901 | +template <typename K> |
4902 | +typename PersistentCache<K, std::string, std::string>::OptionalValue |
4903 | + PersistentCache<K, std::string, std::string>::get_or_put( |
4904 | + K const& key, PersistentCache<K, std::string, std::string>::Loader const& load_func) |
4905 | +{ |
4906 | + auto skey = CacheCodec<K>::encode(key); |
4907 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
4908 | + { |
4909 | + load_func(key, *this); |
4910 | + }; |
4911 | + auto svalue = p_->get_or_put(skey, sload_func); |
4912 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
4913 | +} |
4914 | + |
4915 | +template <typename K> |
4916 | +typename PersistentCache<K, std::string, std::string>::OptionalData |
4917 | + PersistentCache<K, std::string, std::string>::get_or_put_data( |
4918 | + K const& key, PersistentCache<K, std::string, std::string>::Loader const& load_func) |
4919 | +{ |
4920 | + auto skey = CacheCodec<K>::encode(key); |
4921 | + auto sload_func = [&](std::string const&, PersistentStringCache const&) |
4922 | + { |
4923 | + load_func(key, *this); |
4924 | + }; |
4925 | + auto sdata = p_->get_or_put_data(skey, sload_func); |
4926 | + if (!sdata) |
4927 | + { |
4928 | + return OptionalData(); |
4929 | + } |
4930 | + return OptionalData({sdata->value, sdata->metadata}); |
4931 | +} |
4932 | + |
4933 | +template <typename K> |
4934 | +bool PersistentCache<K, std::string, std::string>::put_metadata(K const& key, std::string const& metadata) |
4935 | +{ |
4936 | + return p_->put_metadata(CacheCodec<K>::encode(key), metadata); |
4937 | +} |
4938 | + |
4939 | +template <typename K> |
4940 | +bool PersistentCache<K, std::string, std::string>::put_metadata(K const& key, char const* metadata, int64_t size) |
4941 | +{ |
4942 | + return p_->put_metadata(CacheCodec<K>::encode(key), metadata, size); |
4943 | +} |
4944 | + |
4945 | +template <typename K> |
4946 | +typename PersistentCache<K, std::string, std::string>::OptionalValue PersistentCache<K, std::string, std::string>::take( |
4947 | + K const& key) |
4948 | +{ |
4949 | + auto svalue = p_->take(CacheCodec<K>::encode(key)); |
4950 | + return svalue ? OptionalValue(*svalue) : OptionalValue(); |
4951 | +} |
4952 | + |
4953 | +template <typename K> |
4954 | +typename PersistentCache<K, std::string, std::string>::OptionalData |
4955 | + PersistentCache<K, std::string, std::string>::take_data(K const& key) |
4956 | +{ |
4957 | + auto sdata = p_->take_data(CacheCodec<K>::encode(key)); |
4958 | + if (!sdata) |
4959 | + { |
4960 | + return OptionalData(); |
4961 | + } |
4962 | + return OptionalData({sdata->value, sdata->metadata}); |
4963 | +} |
4964 | + |
4965 | +template <typename K> |
4966 | +bool PersistentCache<K, std::string, std::string>::invalidate(K const& key) |
4967 | +{ |
4968 | + return p_->invalidate(CacheCodec<K>::encode(key)); |
4969 | +} |
4970 | + |
4971 | +template <typename K> |
4972 | +void PersistentCache<K, std::string, std::string>::invalidate(std::vector<K> const& keys) |
4973 | +{ |
4974 | + invalidate(keys.begin(), keys.end()); |
4975 | +} |
4976 | + |
4977 | +template <typename K> |
4978 | +template <typename It> |
4979 | +void PersistentCache<K, std::string, std::string>::invalidate(It begin, It end) |
4980 | +{ |
4981 | + std::vector<std::string> skeys; |
4982 | + for (auto&& it = begin; it < end; ++it) |
4983 | + { |
4984 | + skeys.push_back(CacheCodec<K>::encode(*it)); |
4985 | + } |
4986 | + p_->invalidate(skeys.begin(), skeys.end()); |
4987 | +} |
4988 | + |
4989 | +template <typename K> |
4990 | +void PersistentCache<K, std::string, std::string>::invalidate(std::initializer_list<K> const& keys) |
4991 | +{ |
4992 | + invalidate(keys.begin(), keys.end()); |
4993 | +} |
4994 | + |
4995 | +template <typename K> |
4996 | +void PersistentCache<K, std::string, std::string>::invalidate() |
4997 | +{ |
4998 | + p_->invalidate(); |
4999 | +} |
5000 | + |
The diff has been truncated for viewing.
PASSED: Continuous integration, rev:232 jenkins. qa.ubuntu. com/job/ thumbnailer- ci/172/ jenkins. qa.ubuntu. com/job/ thumbnailer- wily-amd64- ci/4 jenkins. qa.ubuntu. com/job/ thumbnailer- wily-armhf- ci/4 jenkins. qa.ubuntu. com/job/ thumbnailer- wily-armhf- ci/4/artifact/ work/output/ *zip*/output. zip jenkins. qa.ubuntu. com/job/ thumbnailer- wily-i386- ci/4
http://
Executed test runs:
SUCCESS: http://
SUCCESS: http://
deb: http://
SUCCESS: http://
Click here to trigger a rebuild: s-jenkins. ubuntu- ci:8080/ job/thumbnailer -ci/172/ rebuild
http://