Compare commits

...

No commits in common. "main" and "master" have entirely different histories.
main ... master

54 changed files with 10585 additions and 234 deletions

79
.gitea/workflows/aur.yml Normal file
View File

@ -0,0 +1,79 @@
name: Release to AUR
on:
workflow_call:
inputs:
package-name:
required: true
type: string
version:
required: true
type: string
description:
required: true
type: string
depends:
required: true
type: string
workflow_dispatch:
inputs:
package-name:
description: "The name under which the package is going to be packaged to AUR"
type: string
default: "lmdbal"
version:
description: "The version of the published package"
type: string
default: "1.0.0"
description:
description: "The description of the package"
type: string
default: "LMDB Abstraction Layer"
depends:
description: "Additional dependencies"
type: string
default: ""
jobs:
aur:
name: Release ${{ inputs.package-name }} to AUR
runs-on: archlinux
steps:
- name: Download the release tarball
run: curl -sL ${{ gitea.server_url }}/${{ gitea.repository }}/archive/${{ gitea.event.release.tag_name }}.tar.gz --output tarball.tar.gz
- name: Calculate SHA256 for the tarball
run: echo "tbSum=$(sha256sum tarball.tar.gz | cut -d ' ' -f 1)" >> $GITHUB_ENV
- name: Unarchive tarball
run: tar -xvzf tarball.tar.gz
- name: Clone the AUR repository
run: |
echo "${{ secrets.DEPLOY_TO_AUR_PRIVATE_KEY }}" > key
chmod 600 key
GIT_SSH_COMMAND="ssh -i key -o 'IdentitiesOnly yes' -o 'StrictHostKeyChecking no'" git clone ssh://aur@aur.archlinux.org/${{ inputs.package-name }}.git aur
chmod 777 -R aur
cd aur
git config user.name ${{ secrets.DEPLOY_TO_AUR_USER_NAME }}
git config user.email ${{ secrets.DEPLOY_TO_AUR_EMAIL }}
- name: Copy PKGBUILD to the directory
run: cp lmdbal/packaging/Archlinux/PKGBUILD aur/
- name: Patch PKGBUILD file, and generate .SRCINFO
working-directory: aur
run: |
sed -i "/sha256sums=/c\sha256sums=('${{ env.tbSum }}')" PKGBUILD
sed -i "/pkgname=lmdbal/c\pkgname=(${{ inputs.package-name }})" PKGBUILD
sed -i "/pkgver=1.0.0/c\pkgver=(${{ inputs.version }})" PKGBUILD
sed -i "/pkgdesc=\"LMDB Abstraction Layer\"/c\pkgdesc=(\"${{ inputs.description }}\")" PKGBUILD
sed -i "/depends=( 'lmdb' )/c\depends=( 'lmdb' ${{ inputs.depends }} ))" PKGBUILD
sudo -u build makepkg --printsrcinfo > .SRCINFO
- name: Commit package to aur
working-directory: aur
run: |
git add PKGBUILD .SRCINFO
git commit -m "${{ gitea.event.release.body }}"
GIT_SSH_COMMAND="ssh -i ../key -o 'IdentitiesOnly yes' -o 'StrictHostKeyChecking no'" git push

39
.gitea/workflows/main.yml Normal file
View File

@ -0,0 +1,39 @@
name: Main LMDBAL workfow
run-name: ${{ gitea.actor }} is running LMDBAL main workflow
on:
push:
branches:
- master
jobs:
Archlinux:
runs-on: archlinux
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Make a build directory
run: mkdir build
- name: Configure
working-directory: ./build
run: cmake .. -D BUILD_TESTS=True -D BUILD_DOC_HTML=True -D BUILD_DOC_XML=True -D BUILD_DOC_MAN=True -D BUILD_DOXYGEN_AWESOME=True -D QT_VERSION_MAJOR=5
- name: Build
working-directory: ./build
run: cmake --build .
- name: Run tests
working-directory: ./build/test
run: ./runUnitTests
- name: Copy docs via scp
uses: appleboy/scp-action@master
# working-directory: ./build/doc //doesn't work
with:
host: ${{ secrets.DOMAIN_ROOT }}
username: ${{ secrets.DEPLOY_USER_NAME }}
key: ${{ secrets.DEPLOY_PRIVATE_KEY }}
source: "build/doc/html/*,build/doc/xml/*,build/doc/man/*"
target: "/srv/lmdbal/doc"
strip_components: 2

View File

@ -0,0 +1,46 @@
name: LMDBAL Release workflow
run-name: ${{ gitea.actor }} is running LMDBAL Release workflow on release ${{ gitea.event.release.tag_name }}
on:
release:
types: [published]
jobs:
Archlinux:
runs-on: archlinux
steps:
- name: Download the release tarball
run: curl -sL ${{ gitea.server_url }}/${{ gitea.repository }}/archive/${{ gitea.event.release.tag_name }}.tar.gz --output tarball.tar.gz
- name: Calculate SHA256 for the tarball
run: echo "tbSum=$(sha256sum tarball.tar.gz | cut -d ' ' -f 1)" >> $GITHUB_ENV
- name: Unarchive tarball
run: tar -xvzf tarball.tar.gz
- name: Clone the AUR repository
run: |
echo "${{ secrets.DEPLOY_TO_AUR_PRIVATE_KEY }}" > key
chmod 600 key
GIT_SSH_COMMAND="ssh -i key -o 'IdentitiesOnly yes' -o 'StrictHostKeyChecking no'" git clone ssh://aur@aur.archlinux.org/lmdbal.git aur
chmod 777 -R aur
cd aur
git config user.name ${{ secrets.DEPLOY_TO_AUR_USER_NAME }}
git config user.email ${{ secrets.DEPLOY_TO_AUR_EMAIL }}
- name: Copy PKGBUILD to the directory
run: cp lmdbal/packaging/Archlinux/PKGBUILD aur/
- name: Put SHA256 sum to PKGBUILD file, and generate .SRCINFO
working-directory: aur
run: |
sed -i "/sha256sums=/c\sha256sums=('${{ env.tbSum }}')" PKGBUILD
sudo -u build makepkg --printsrcinfo > .SRCINFO
- name: Commit package to aur
working-directory: aur
run: |
git add PKGBUILD .SRCINFO
git commit -m "${{ gitea.event.release.body }}"
GIT_SSH_COMMAND="ssh -i ../key -o 'IdentitiesOnly yes' -o 'StrictHostKeyChecking no'" git push

62
CHANGELOG.md Normal file
View File

@ -0,0 +1,62 @@
# Changelog
# LMDBAL 0.5.3 (November 14, 2023)
### Improvements
- Now you don't need to link agains lmdb, just linking against LMDBAL is enough
### Bug fixes
- transaction error in LMDBAL::Cache::readAll
## LMDBAL 0.5.2 (November 01, 2023)
### Improvements
- RAII cursors
- operation set for cursors
### Bug fixes
- error beginning transaction is now correctly handled and doesn't segfault
## LMDBAL 0.5.1 (October 21, 2023)
### Improvements
- RAII transactions
- reduced overhead for private transaction functions
### Bug fixes
- bug fix with cache fallthough
## LMDBAL 0.5.0 (October 15, 2023)
### New Features
- duplicates support (only for table)
### Improvements
- some more documentation
- more tests
## LMDBAL 0.4.0 (August 13, 2023)
### New Features
- read only cursors
### Bug fixes
- possible cache unsync
- doxygen-awesome build bix
### Improvements
- some more documentation
- more tests
## LMDBAL 0.3.1 (April 14, 2023)
### Bug fixes
- build with qt5 now is possible again
### Improvements
- exception documentation
## LMDBAL 0.3.0 (April 12, 2023)
### New features
- transaction functions
### Improvements
- initial documentation
- cache unit testing
- transactions unit testing
- serialization unit testing

130
CMakeLists.txt Normal file
View File

@ -0,0 +1,130 @@
cmake_minimum_required(VERSION 3.16)
project(LMDBAL
VERSION 0.5.4
DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer"
LANGUAGES CXX
)
string(TOLOWER ${PROJECT_NAME} PROJECT_LOW)
cmake_policy(SET CMP0076 NEW)
cmake_policy(SET CMP0079 NEW)
option(BUILD_STATIC "Builds library as static library" OFF)
option(BUILD_TESTS "Builds tests" OFF)
option(BUILD_DOC_MAN "Builds man page documentation" OFF)
option(BUILD_DOC_HTML "Builds html documentation" OFF)
option(BUILD_DOC_XML "Builds xml documentation" OFF)
option(BUILD_DOXYGEN_AWESOME "Builds documentation alternative style" OFF)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
if (NOT DEFINED QT_VERSION_MAJOR)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
endif()
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
find_package(LMDB REQUIRED)
# Build type
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif ()
if (BUILD_STATIC)
add_library(${PROJECT_NAME} STATIC)
else ()
add_library(${PROJECT_NAME} SHARED)
endif()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
list(APPEND COMPILE_OPTIONS -O3)
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND COMPILE_OPTIONS -g)
list(APPEND COMPILE_OPTIONS -Wall)
list(APPEND COMPILE_OPTIONS -Wextra)
endif()
message("Compilation options: " ${COMPILE_OPTIONS})
target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS})
set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${version})
set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION 1)
set_property(TARGET ${PROJECT_NAME} PROPERTY EXPORT_NAME ${PROJECT_NAME})
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERFACE_${PROJECT_NAME}_MAJOR_VERSION 1)
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY
COMPATIBLE_INTERFACE_STRING ${PROJECT_NAME}_MAJOR_VERSION
)
if (UNIX)
set_property(TARGET ${PROJECT_NAME} PROPERTY INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/${PROJECT_LOW}")
set_property(TARGET ${PROJECT_NAME} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)
set_property(TARGET ${PROJECT_NAME} PROPERTY SKIP_BUILD_RPATH FALSE)
set_property(TARGET ${PROJECT_NAME} PROPERTY BUILD_WITH_INSTALL_RPATH FALSE)
endif()
add_subdirectory(src)
if (BUILD_DOC_MAN OR BUILD_DOC_HTML OR BUILD_DOC_XML)
find_package(Doxygen)
if (DOXYGEN_FOUND)
add_subdirectory(doc)
else()
message("Was trying to build documentation, but Doxygen was not found, skipping documentation")
endif()
endif()
if (BUILD_TESTS)
add_subdirectory(test)
endif ()
target_include_directories(
${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/serializer>
)
target_include_directories(${PROJECT_NAME} PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
target_link_libraries(
${PROJECT_NAME}
Qt${QT_VERSION_MAJOR}::Core
lmdb
)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_LOW}Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_LOW}
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_LOW}ConfigVersion.cmake"
VERSION "${version}"
COMPATIBILITY AnyNewerVersion
)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_LOW}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}
)
install(EXPORT ${PROJECT_LOW}Targets
FILE ${PROJECT_LOW}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_LOW}
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_LOW}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_LOW}ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_LOW}
)

232
LICENSE
View File

@ -1,232 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for software and other kinds of works.
The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions.
Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and modification follow.
TERMS AND CONDITIONS
0. Definitions.
“This License” refers to version 3 of the GNU General Public License.
“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations.
To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work.
A “covered work” means either the unmodified Program or a work based on the Program.
To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
1. Source Code.
The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.
A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.
The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”.
c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
“Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
7. Additional Terms.
“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
11. Patents.
A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”.
A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.
If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”.
You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see <http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read <http://www.gnu.org/philosophy/why-not-lgpl.html>.

676
LICENSE.md Normal file
View File

@ -0,0 +1,676 @@
# GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
<https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
## Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom
to share and change all versions of a program--to make sure it remains
free software for all its users. We, the Free Software Foundation, use
the GNU General Public License for most of our software; it applies
also to any other work released this way by its authors. You can apply
it to your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you
have certain responsibilities if you distribute copies of the
software, or if you modify it: responsibilities to respect the freedom
of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the
manufacturer can do so. This is fundamentally incompatible with the
aim of protecting users' freedom to change the software. The
systematic pattern of such abuse occurs in the area of products for
individuals to use, which is precisely where it is most unacceptable.
Therefore, we have designed this version of the GPL to prohibit the
practice for those products. If such problems arise substantially in
other domains, we stand ready to extend this provision to those
domains in future versions of the GPL, as needed to protect the
freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish
to avoid the special danger that patents applied to a free program
could make it effectively proprietary. To prevent this, the GPL
assures that patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
## TERMS AND CONDITIONS
### 0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds
of works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of
an exact copy. The resulting work is called a "modified version" of
the earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user
through a computer network, with no transfer of a copy, is not
conveying.
An interactive user interface displays "Appropriate Legal Notices" to
the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
### 1. Source Code.
The "source code" for a work means the preferred form of the work for
making modifications to it. "Object code" means any non-source form of
a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users can
regenerate automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same
work.
### 2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey,
without conditions so long as your license otherwise remains in force.
You may convey covered works to others for the sole purpose of having
them make modifications exclusively for you, or provide you with
facilities for running those works, provided that you comply with the
terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for
you must do so exclusively on your behalf, under your direction and
control, on terms that prohibit them from making any copies of your
copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under the
conditions stated below. Sublicensing is not allowed; section 10 makes
it unnecessary.
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such
circumvention is effected by exercising rights under this License with
respect to the covered work, and you disclaim any intention to limit
operation or modification of the work as a means of enforcing, against
the work's users, your or third parties' legal rights to forbid
circumvention of technological measures.
### 4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
### 5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these
conditions:
- a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
- b) The work must carry prominent notices stating that it is
released under this License and any conditions added under
section 7. This requirement modifies the requirement in section 4
to "keep intact all notices".
- c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
- d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
### 6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms of
sections 4 and 5, provided that you also convey the machine-readable
Corresponding Source under the terms of this License, in one of these
ways:
- a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
- b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the Corresponding
Source from a network server at no charge.
- c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
- d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
- e) Convey the object code using peer-to-peer transmission,
provided you inform other peers where the object code and
Corresponding Source of the work are being offered to the general
public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal,
family, or household purposes, or (2) anything designed or sold for
incorporation into a dwelling. In determining whether a product is a
consumer product, doubtful cases shall be resolved in favor of
coverage. For a particular product received by a particular user,
"normally used" refers to a typical or common use of that class of
product, regardless of the status of the particular user or of the way
in which the particular user actually uses, or expects or is expected
to use, the product. A product is a consumer product regardless of
whether the product has substantial commercial, industrial or
non-consumer uses, unless such uses represent the only significant
mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to
install and execute modified versions of a covered work in that User
Product from a modified version of its Corresponding Source. The
information must suffice to ensure that the continued functioning of
the modified object code is in no case prevented or interfered with
solely because modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or
updates for a work that has been modified or installed by the
recipient, or for the User Product in which it has been modified or
installed. Access to a network may be denied when the modification
itself materially and adversely affects the operation of the network
or violates the rules and protocols for communication across the
network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
### 7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders
of that material) supplement the terms of this License with terms:
- a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
- b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
- c) Prohibiting misrepresentation of the origin of that material,
or requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
- d) Limiting the use for publicity purposes of names of licensors
or authors of the material; or
- e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
- f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions
of it) with contractual assumptions of liability to the recipient,
for any liability that these contractual assumptions directly
impose on those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions; the
above requirements apply either way.
### 8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your license
from a particular copyright holder is reinstated (a) provisionally,
unless and until the copyright holder explicitly and finally
terminates your license, and (b) permanently, if the copyright holder
fails to notify you of the violation by some reasonable means prior to
60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
### 9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or run
a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
### 10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
### 11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims owned
or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on
the non-exercise of one or more of the rights that are specifically
granted under this License. You may not convey a covered work if you
are a party to an arrangement with a third party that is in the
business of distributing software, under which you make payment to the
third party based on the extent of your activity of conveying the
work, and under which the third party grants, to any of the parties
who would receive the covered work from you, a discriminatory patent
license (a) in connection with copies of the covered work conveyed by
you (or copies made from those copies), or (b) primarily for and in
connection with specific products or compilations that contain the
covered work, unless you entered into that arrangement, or that patent
license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
### 12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under
this License and any other pertinent obligations, then as a
consequence you may not convey it at all. For example, if you agree to
terms that obligate you to collect a royalty for further conveying
from those to whom you convey the Program, the only way you could
satisfy both those terms and this License would be to refrain entirely
from conveying the Program.
### 13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
### 14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions
of the GNU General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in
detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies that a certain numbered version of the GNU General Public
License "or any later version" applies to it, you have the option of
following the terms and conditions either of that numbered version or
of any later version published by the Free Software Foundation. If the
Program does not specify a version number of the GNU General Public
License, you may choose any version ever published by the Free
Software Foundation.
If the Program specifies that a proxy can decide which future versions
of the GNU General Public License can be used, that proxy's public
statement of acceptance of a version permanently authorizes you to
choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
### 15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION.
### 16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
### 17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
## How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively state
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper
mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands \`show w' and \`show c' should show the
appropriate parts of the General Public License. Of course, your
program's commands might be different; for a GUI interface, you would
use an "about box".
You should also get your employer (if you work as a programmer) or
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. For more information on this, and how to apply and follow
the GNU GPL, see <https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your
program into proprietary programs. If your program is a subroutine
library, you may consider it more useful to permit linking proprietary
applications with the library. If this is what you want to do, use the
GNU Lesser General Public License instead of this License. But first,
please read <https://www.gnu.org/licenses/why-not-lgpl.html>.

101
README.md
View File

@ -1,3 +1,100 @@
# storage
# LMDBAL - Lightning Memory Data Base Abstraction Level
Just a temp experiment project for storing stuff
[![AUR license](https://img.shields.io/aur/license/lmdbal?style=flat-square)](https://git.macaw.me/blue/lmdbal/raw/branch/master/LICENSE.md)
[![AUR version](https://img.shields.io/aur/version/lmdbal?style=flat-square)](https://aur.archlinux.org/packages/lmdbal/)
[![Liberapay patrons](https://img.shields.io/liberapay/patrons/macaw.me?logo=liberapay&style=flat-square)](https://liberapay.com/macaw.me)
[![Documentation](https://img.shields.io/badge/Documentation-HTML-green)](https://macaw.me/lmdbal/doc/html)
### Prerequisites
- a compiler (c++ would do)
- Qt 5 or higher (qt5-base would do)
- lmdb
- CMake 3.16 or higher
- Doxygen (optional, for documentation)
- gtest (optional, for tests)
### Using with CMake
#### As a system library
If you're using LMDBAL as a system library you probably have no control over it's build options. The easiest way to include the project is to add following
```
find_package(lmdbal)
if (LMDBAL_FOUND)
target_include_directories(yourTarget PRIVATE ${LMDBAL_INCLUDE_DIRS})
target_link_libraries(yourTarget PRIVATE LMDBAL::LMDBAL)
endif()
```
#### As an embeded subproject
If you're using LMDBAL as a embeded library you might want to control it's build options, for example you can run
```
set(BUILD_STATIC ON)
```
before including the library in your project. This will set the library to be build in a static mode.
Then you want to run something like this
```
add_subdirectory(pathTo/yourEmbedded/libraries/lmdbal)
add_library(LMDBAL::LMDBAL ALIAS LMDBAL)
...
target_link_libraries(yourTarget PRIVATE LMDBAL::LMDBAL)
```
The headers are added as `PUBLIC` so you might not even need to `target_link_libraries` them
### Building
LMDBAL uses CMake as a build system.
Please check the prerequisites and install them before building.
Here is an easy way to build a project
```
$ git clone https://git.macaw.me/blue/lmdbal
$ cd lmdbal
$ mkdir build
$ cd build
$ cmake .. [ *optional keys* ]
$ cmake --build .
$ cmake --install . --prefix install
```
This way will create you a `lmdbal/build` directory with temporary files, and `lmdbal/build/install` with all the export files for installation to the system.
After `cmake ..` you can specify keys to alter the building process. In this context building keys are transfered like so
```
cmake .. -D KEY1=VALUE1 -D KEY2=VALUE2 ...
```
#### List of keys
Here is the list of keys you can pass to configuration phase of `cmake ..`:
- `CMAKE_BUILD_TYPE` - `Debug` just builds showing all warnings, `Release` builds with no warnings and applies optimizations (default is `Debug`);
- `BUILD_STATIC` - `True` builds project as a static library, `False` builds as dynamic (default is `False`);
- `BUILD_TESTS` - `True` build unit tests, `False` does not (default is `False`);
- `BUILD_DOC` - `True` build doxygen documentation, `False` does not (default is `False`);
- `BUILD_DOXYGEN_AWESOME` - `True` build doxygen awesome theme if `BUILD_DOC` is also `True` (default is `False`);
- `QT_VERSION_MAJOR` - `5` links against Qt5, `6` links agains Qt6, there is no default, so, if you didn't specify it the project will chose automatically;
#### Running tests
If you built the library with `-D BUILD_TESTS=True`, then there will be `lmdbal/build/tests/runUnitTests` executable file. You can simply run it as
```
./runUnitTests
```
if you're in the same directory with it
## License
This project is available under the GPL-3.0 License or any later version. The file [cmake/FindLMDB.cmake](cmake/FindLMDB.cmake) is available under the GPL-3.0 License only, - see the [LICENSE.md](LICENSE.md) file for details

5
cmake/Config.cmake.in Normal file
View File

@ -0,0 +1,5 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/lmdbalTargets.cmake")
check_required_components(lmdbal)

52
cmake/FindLMDB.cmake Normal file
View File

@ -0,0 +1,52 @@
#This file is taken from here https://gitlab.ralph.or.at/causal-rt/causal-cpp/, it was GPLv3 license
#Thank you so much, mr. Ralph Alexander Bariz, I hope you don't mind me using your code
# Try to find LMDB headers and library.
#
# Usage of this module as follows:
#
# find_package(LMDB)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
# LMDB_ROOT_DIR Set this variable to the root installation of
# LMDB if the module has problems finding the
# proper installation path.
#
# Variables defined by this module:
#
# LMDB_FOUND System has LMDB library/headers.
# LMDB_LIBRARIES The LMDB library.
# LMDB_INCLUDE_DIRS The location of LMDB headers.
find_path(LMDB_ROOT_DIR
NAMES include/lmdb.h
)
find_library(LMDB_LIBRARIES
NAMES liblmdb.a liblmdb.so liblmdb.so.a liblmdb.dll.a # We want lmdb to be static, if possible
HINTS ${LMDB_ROOT_DIR}/lib
)
add_library(lmdb UNKNOWN IMPORTED)
set_target_properties(lmdb PROPERTIES
IMPORTED_LOCATION ${LMDB_LIBRARIES}
)
find_path(LMDB_INCLUDE_DIRS
NAMES lmdb.h
HINTS ${LMDB_ROOT_DIR}/include
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LMDB DEFAULT_MSG
LMDB_LIBRARIES
LMDB_INCLUDE_DIRS
)
mark_as_advanced(
LMDB_ROOT_DIR
LMDB_LIBRARIES
LMDB_INCLUDE_DIRS
)

66
doc/CMakeLists.txt Normal file
View File

@ -0,0 +1,66 @@
if (BUILD_DOC_HTML)
set(DOXYGEN_GENERATE_HTML YES)
endif()
if (BUILD_DOC_MAN)
set(DOXYGEN_GENERATE_MAN YES)
endif()
if (BUILD_DOC_XML)
set(DOXYGEN_GENERATE_XML YES)
endif()
if (BUILD_DOXYGEN_AWESOME)
include(ExternalProject)
ExternalProject_Add(doxygen-awesome-css
GIT_REPOSITORY https://github.com/jothepro/doxygen-awesome-css.git
GIT_TAG "v2.2.1"
CONFIGURE_COMMAND ""
BUILD_COMMAND make
BUILD_IN_SOURCE TRUE
INSTALL_COMMAND make PREFIX=${CMAKE_CURRENT_BINARY_DIR}/doxygen-awesome-css install
)
set (DOXYGEN_GENERATE_TREEVIEW YES)
set (DOXYGEN_DISABLE_INDEX NO)
set (DOXYGEN_FULL_SIDEBAR NO)
set (DOXYGEN_HTML_EXTRA_STYLESHEET
${CMAKE_CURRENT_BINARY_DIR}/doxygen-awesome-css/share/doxygen-awesome-css/doxygen-awesome.css
${CMAKE_CURRENT_BINARY_DIR}/doxygen-awesome-css/share/doxygen-awesome-css/doxygen-awesome-sidebar-only.css
${CMAKE_CURRENT_BINARY_DIR}/doxygen-awesome-css/share/doxygen-awesome-css/doxygen-awesome-sidebar-only-darkmode-toggle.css
custom.css
)
set (DOXYGEN_HTML_EXTRA_FILES
${CMAKE_CURRENT_BINARY_DIR}/doxygen-awesome-css/share/doxygen-awesome-css/doxygen-awesome-darkmode-toggle.js
)
set (DOXYGEN_FULL_SIDEBAR NO)
set (DOXYGEN_HTML_COLORSTYLE "LIGHT")
set (DOXYGEN_HTML_HEADER header.html)
endif()
doxygen_add_docs(
documentation
${PROJECT_SOURCE_DIR}/src
mainpage.dox
ALL
COMMENT "Generate man and html pages"
)
if (BUILD_DOC_MAN)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/man
TYPE DOC
)
endif()
if (BUILD_DOC_HTML)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/html
TYPE DOC
)
endif()
if (BUILD_DOC_XML)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/xml
TYPE DOC
)
endif()
if (BUILD_DOXYGEN_AWESOME)
add_dependencies(documentation doxygen-awesome-css)
endif()

3
doc/custom.css Normal file
View File

@ -0,0 +1,3 @@
html {
--top-height: 150px;
}

78
doc/header.html Normal file
View File

@ -0,0 +1,78 @@
<!-- HTML header for doxygen 1.9.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="$langISO">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen $doxygenversion"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!--BEGIN PROJECT_NAME--><title>$projectname: $title</title><!--END PROJECT_NAME-->
<!--BEGIN !PROJECT_NAME--><title>$title</title><!--END !PROJECT_NAME-->
<link href="$relpath^tabs.css" rel="stylesheet" type="text/css"/>
<!--BEGIN DISABLE_INDEX-->
<!--BEGIN FULL_SIDEBAR-->
<script type="text/javascript">var page_layout=1;</script>
<!--END FULL_SIDEBAR-->
<!--END DISABLE_INDEX-->
<script type="text/javascript" src="$relpath^jquery.js"></script>
<script type="text/javascript" src="$relpath^dynsections.js"></script>
$treeview
$search
$mathjax
$darkmode
<link href="$relpath^$stylesheet" rel="stylesheet" type="text/css" />
$extrastylesheet
<script type="text/javascript" src="$relpath^doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
</head>
<body>
<!--BEGIN DISABLE_INDEX-->
<!--BEGIN FULL_SIDEBAR-->
<div id="side-nav" class="ui-resizable side-nav-resizable"><!-- do not remove this div, it is closed by doxygen! -->
<!--END FULL_SIDEBAR-->
<!--END DISABLE_INDEX-->
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<!--BEGIN TITLEAREA-->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<!--BEGIN PROJECT_LOGO-->
<td id="projectlogo"><img alt="Logo" src="$relpath^$projectlogo"/></td>
<!--END PROJECT_LOGO-->
<!--BEGIN PROJECT_NAME-->
<td id="projectalign">
<div id="projectname">$projectname<!--BEGIN PROJECT_NUMBER--><span id="projectnumber">&#160;$projectnumber</span><!--END PROJECT_NUMBER-->
</div>
<!--BEGIN PROJECT_BRIEF--><div id="projectbrief">$projectbrief</div><!--END PROJECT_BRIEF-->
</td>
<!--END PROJECT_NAME-->
<!--BEGIN !PROJECT_NAME-->
<!--BEGIN PROJECT_BRIEF-->
<td>
<div id="projectbrief">$projectbrief</div>
</td>
<!--END PROJECT_BRIEF-->
<!--END !PROJECT_NAME-->
<!--BEGIN DISABLE_INDEX-->
<!--BEGIN SEARCHENGINE-->
<!--BEGIN !FULL_SIDEBAR-->
<td>$searchbox</td>
<!--END !FULL_SIDEBAR-->
<!--END SEARCHENGINE-->
<!--END DISABLE_INDEX-->
</tr>
<!--BEGIN SEARCHENGINE-->
<!--BEGIN FULL_SIDEBAR-->
<tr><td colspan="2">$searchbox</td></tr>
<!--END FULL_SIDEBAR-->
<!--END SEARCHENGINE-->
</tbody>
</table>
</div>
<!--END TITLEAREA-->
<!-- end header part -->

78
doc/mainpage.dox Normal file
View File

@ -0,0 +1,78 @@
/*! \mainpage Getting Started
*
* Everything begins with a data base represented by the class LMDBAL::Base.
* It repesents a collection of key-value storages that are going to be stored in a sigle data base directory.
* To create a LMDBAL::Base you need to pick up a name of a directory that is going to be created on your machine.
*
* @code{.cpp}
*
* #include "base.h"
*
* //...
*
* LMDBAL::Base base("myDataBase");
* @endcode
*
* LMDBAL::Base creates or opens existing directory with the given name in the location acquired with
* <a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#writableLocation">writableLocation</a>(<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#StandardLocation-enum">CacheLocation</a>)
* so, the file system destination of your data depends on the
* <a class="el" href="https://doc.qt.io/qt-6/qcoreapplication.html">QCoreApplication</a> configuration of your app.
*
* After you have created a LMDBAL::Base you probably want to obtain storage handlers.
* Now there are only two available types of them: LMDBAL::Storage and LMDBAL::Cache.
* The only difference between them is that LMDBAL::Cache additionally stores elements in a
* <a class="el" href="https://en.cppreference.com/w/cpp/container/map">std::map</a>
* to speed up the access.
*
* @code{.cpp}
*
* #include "storage.h"
* #include "cache.h"
*
* //...
*
* LMDBAL::Storage<uint32_t, uint32_t> storage = base.addStorage<uint32_t, uint32_t>("storage");
* LMDBAL::Cache<int8_t, std::string> cache = base.addCache<int8_t, std::string>("cache");
*
* @endcode
*
* You can obtain handlers by calling LMDBAL::Base::addStorage() or LMDBAL::Base::addCache().
* Note that the handlers still belong to the LMDBAL::Base and it's his responsibility to destroy them.
* You are not obliged to save those handlers,
* you can obtain them at any time later using methods LMDBAL::Base::getStorage() or LMDBAL::Base::getCache()
* calling them with the same template types and names.
*
* @code{.cpp}
*
* //...
*
* base.open();
*
* @endcode
*
* After you have added all the storages you wanted it's time to open the data base with LMDBAL::Base::open().
* At this point you are not allowed to add any more storages, otherwise LMDBAL::Opened exception will be thrown.
* It's currently the limitation of this little library and I might solve it in the future.
* Database will throw no exception if you will try to close the closed LMDBAL::Base or open again already opened one.
* Also it will automatically close itself if you'll try to destoroy onpened LMDBAL::Base.
*
* @code{.cpp}
*
* //...
*
* storage->addRecord(54, 75);
* cache->addRecord(9, "my value");
*
* uint32_t value1 = storage->getRecord(54); //75
* std::string value2 = cache->getRecord(9); //"myValue"
*
* uint32_t count1 = storage->count(); //1
* uint32_t count2 = cache->count(); //1
*
* storage->removeRecord(54);
* cache->removeRecord(9);
*
* @endcode
*
* To discover how to store read and modify data take a look at LMDBAL::Storage and LMDBAL::Cache classes.
*/

View File

@ -0,0 +1,23 @@
# Maintainer: Yury Gubich <blue@macaw.me>
pkgname=lmdbal
pkgver=1.0.0
pkgrel=1
pkgdesc="LMDB Abstraction Layer"
arch=('i686' 'x86_64')
url="https://git.macaw.me/blue/lmdbal"
license=('GPL3')
depends=( 'lmdb' )
makedepends=('cmake>=3.16' 'gcc')
optdepends=()
source=("$pkgname-$pkgver.tar.gz::https://git.macaw.me/blue/$pkgname/archive/$pkgver.tar.gz")
sha256sums=('SKIP')
build() {
cd "$srcdir/$pkgname"
cmake . -D CMAKE_INSTALL_PREFIX=/usr -D CMAKE_BUILD_TYPE=Release -D QT_VERSION_MAJOR=5
cmake --build .
}
package() {
cd "$srcdir/$pkgname"
DESTDIR="$pkgdir/" cmake --install .
}

25
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,25 @@
set(SOURCES
exceptions.cpp
storage.cpp
base.cpp
transaction.cpp
)
set(HEADERS
base.h
exceptions.h
storage.h
storage.hpp
cursor.h
cursor.hpp
cache.h
cache.hpp
operators.hpp
transaction.h
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
add_subdirectory(serializer)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW})

465
src/base.cpp Normal file
View File

@ -0,0 +1,465 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base.h"
#include "exceptions.h"
#include "storage.h"
#include "transaction.h"
#define UNUSED(x) (void)(x)
/**
* \class LMDBAL::Base
* \brief Database abstraction
*
* This is a basic class that represents the database as a collection of storages.
* Storages is something key-value database has instead of tables in classic SQL databases.
*/
/**
* \brief Creates the database
*
* \param[in] _name - name of the database, it is going to affect folder name that is created to store data
* \param[in] _mapSize - LMDB map size (MiB), multiplied by 1024^2 and passed to <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gaa2506ec8dab3d969b0e609cd82e619e5">mdb_env_set_mapsize</a> during the call of LMDBAL::Base::open()
*/
LMDBAL::Base::Base(const QString& _name, uint16_t _mapSize):
name(_name.toStdString()),
opened(false),
size(_mapSize),
environment(),
storages(),
transactions(new Transactions())
{}
/**
* \brief Destroys the database
*/
LMDBAL::Base::~Base() {
close();
delete transactions;
for (const std::pair<const std::string, iStorage*>& pair : storages)
delete pair.second;
}
/**
* \brief Closes the database
*
* Closes all lmdb handles, aborts all public transactions.
* This function will do nothing on closed database
*
* \exception LMDBAL::Unknown - thrown if something went wrong aborting transactions
*/
void LMDBAL::Base::close() {
if (opened) {
for (const LMDBAL::TransactionID id : *transactions)
abortTransaction(id, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages)
pair.second->close();
mdb_env_close(environment);
transactions->clear();
opened = false;
}
}
/**
* \brief Opens the database
*
* Almost every LMDBAL::Base require it to be opened, this function does it.
* It laso creates the directory for the database if it was an initial launch.
* This function will do nothing on opened database
*
* \exception LMDBAL::Unknown - thrown if something went wrong opening storages and caches
*/
void LMDBAL::Base::open() {
if (!opened) {
mdb_env_create(&environment);
QString path = createDirectory();
mdb_env_set_maxdbs(environment, storages.size());
mdb_env_set_mapsize(environment, size * 1024UL * 1024UL);
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
TransactionID txn = beginPrivateTransaction(emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages) {
iStorage* storage = pair.second;
int rc = storage->open(txn);
if (rc)
throw Unknown(name, mdb_strerror(rc));
}
commitPrivateTransaction(txn, emptyName);
opened = true;
}
}
/**
* \brief Removes database directory
*
* \returns true if removal was successfull of if no directory was created where it's expected to be, false otherwise
*
* \exception LMDBAL::Opened - thrown if this function was called on opened database
*/
bool LMDBAL::Base::removeDirectory() {
if (opened)
throw Opened(name, "remove database directory");
QString path = getPath();
QDir cache(path);
if (cache.exists())
return cache.removeRecursively();
else
return true;
}
/**
* \brief Creates database directory
*
* Creates or opens existing directory with the given name in the location acquired with
* <a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#writableLocation">writableLocation</a>(<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#StandardLocation-enum">CacheLocation</a>)
* so, the file system destination of your data would depend on the
* <a class="el" href="https://doc.qt.io/qt-6/qcoreapplication.html">QCoreApplication</a> configuration of your app.
* This function does nothing if the directory was already created
*
* \returns the path of the created directory
*
* \exception LMDBAL::Opened - thrown if called on opened database
* \exception LMDBAL::Directory - if the database couldn't create the folder
*/
QString LMDBAL::Base::createDirectory() {
if (opened)
throw Opened(name, "create database directory");
QString path = getPath();
QDir cache(path);
if (!cache.exists()) {
bool res = cache.mkpath(path);
if (!res)
throw Directory(path.toStdString());
}
return path;
}
/**
* \brief Returns database name
*
* \returns database name
*/
QString LMDBAL::Base::getName() const {
return QString::fromStdString(name);}
/**
* \brief Returns database name
*
* \returns database path
*/
QString LMDBAL::Base::getPath() const {
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
return path;
}
/**
* \brief Returns database state
*
* \returns true if the database is opened and ready for work, false otherwise
*/
bool LMDBAL::Base::ready() const {
return opened;}
/**
* \brief Drops the database
*
* Clears all caches and storages of the database
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happend
*/
void LMDBAL::Base::drop() {
if (!opened)
throw Closed("drop", name);
TransactionID txn = beginPrivateTransaction(emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages) {
int rc = pair.second->drop(txn);
if (rc != MDB_SUCCESS) {
abortPrivateTransaction(txn, emptyName);
throw Unknown(name, mdb_strerror(rc), pair.first);
}
}
commitPrivateTransaction(txn, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages)
pair.second->handleDrop();
}
/**
* \brief Begins read-only transaction
*
* This is the legitimate way to retrieve LMDBAL::Transaction.
* LMDBAL::Transaction is considered runnig right after creation by this method.
* You can terminate transaction manually calling LMDBAL::Transaction::terminate
* but it's not required, because transaction will be terminated automatically
* (if it was not terminated manually) upon the call of the destructor.
*
* You can not use termitated transaction any more.
*
* \returns read-only transaction
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::Transaction LMDBAL::Base::beginReadOnlyTransaction() const {
TransactionID id = beginReadOnlyTransaction(emptyName);
return Transaction(id, this);
}
/**
* \brief Begins writable transaction
*
* This is the legitimate way to retrieve LMDBAL::WriteTransaction.
* LMDBAL::WriteTransaction is considered runnig right after creation by this method.
* You can commit all the changes made by this transaction calling LMDBAL::WriteTransaction::commit.
* You can cancel any changes made bu this transaction calling LMDBAL::WriteTransaction::abort
* (or LMDBAL::Transaction::terminate which LMDBAL::WriteTransaction inherits),
* but it's not required, because transaction will be aborted automatically
* (if it was not terminated (committed <b>OR</b> aborted) manually) upon the call of the destructor.
*
* You can not use termitated transaction any more.
*
* \returns writable transaction
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::WriteTransaction LMDBAL::Base::beginTransaction() {
TransactionID id = beginTransaction(emptyName);
return WriteTransaction(id, this);
}
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* This is an optimal way to terminate read-only transactions
*
* \param[in] id - transaction ID you want to abort
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const {
return abortTransaction(id, emptyName);}
/**
* \brief Commits transaction
*
* Terminates transaction applying changes.
*
* \param[in] id - transaction ID you want to commit
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) {
return commitTransaction(id, emptyName);}
/**
* \brief Begins read-only transaction
*
* This function is intended to be called from subordinate storage or cache
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns read-only transaction ID
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const {
if (!opened)
throw Closed("beginReadOnlyTransaction", name, storageName);
TransactionID txn = beginPrivateReadOnlyTransaction(storageName);
transactions->emplace(txn);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionStarted(txn, true);
return txn;
}
/**
* \brief Begins writable transaction
*
* This function is intended to be called from subordinate storage or cache
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns writable transaction ID
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageName) const {
if (!opened)
throw Closed("beginTransaction", name, storageName);
TransactionID txn = beginPrivateTransaction(storageName);
transactions->emplace(txn);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionStarted(txn, false);
return txn;
}
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* This is an optimal way to terminate read-only transactions.
* This function is intended to be called from subordinate storage or cache
*
* \param[in] id - transaction ID you want to abort
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
if (!opened)
throw Closed("abortTransaction", name, storageName);
Transactions::iterator itr = transactions->find(id);
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
throw Unknown(name, "unable to abort transaction: transaction was not found", storageName);
abortPrivateTransaction(id, storageName);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionAborted(id);
transactions->erase(itr);
}
/**
* \brief Commits transaction
*
* Terminates transaction applying changes.
* This function is intended to be called from subordinate storage or cache
*
* \param[in] id - transaction ID you want to commit
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string& storageName) {
if (!opened)
throw Closed("abortTransaction", name, storageName);
Transactions::iterator itr = transactions->find(id);
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
throw Unknown(name, "unable to commit transaction: transaction was not found", storageName);
commitPrivateTransaction(id, storageName);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionCommited(id);
transactions->erase(itr);
}
/**
* \brief Begins read-only transaction
*
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns read-only transaction ID
*
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::string& storageName) const {
MDB_txn* txn;
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
if (rc != MDB_SUCCESS)
throw Unknown(name, mdb_strerror(rc), storageName);
return txn;
}
/**
* \brief Begins writable transaction
*
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns writable transaction ID
*
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginPrivateTransaction(const std::string& storageName) const {
MDB_txn* txn;
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
if (rc != MDB_SUCCESS) {
mdb_txn_abort(txn);
throw Unknown(name, mdb_strerror(rc), storageName);
}
return txn;
}
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* This is an optimal way to terminate read-only transactions.
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] id - transaction ID you want to abort
* \param[in] storageName - name of the storage/cache that you begin transaction from, unused here
*/
void LMDBAL::Base::abortPrivateTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
UNUSED(storageName);
mdb_txn_abort(id);
}
/**
* \brief Commits transaction
*
* Terminates transaction applying changes.
* This function is intended to be called from subordinate storage or cache
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] id - transaction ID you want to commit
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::commitPrivateTransaction(LMDBAL::TransactionID id, const std::string& storageName) {
int rc = mdb_txn_commit(id);
if (rc != MDB_SUCCESS)
throw Unknown(name, mdb_strerror(rc), storageName);
}

219
src/base.h Normal file
View File

@ -0,0 +1,219 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_BASE_H
#define LMDBAL_BASE_H
#include <map>
#include <set>
#include <string>
#include <optional>
#include <limits>
#include <QString>
#include <QStandardPaths>
#include <QDir>
#include <lmdb.h>
#include "exceptions.h"
namespace LMDBAL {
class iStorage;
class Transaction;
class WriteTransaction;
template<class T>
class Serializer;
template <class K, class V>
class Storage;
template <class K, class V>
class Cache;
typedef MDB_txn* TransactionID; /**<\brief I'm going to use transaction pointers as transaction IDs*/
typedef uint32_t SizeType; /**<\brief All LMDBAL sizes are uint32_t*/
class Base {
friend class iStorage;
friend class Transaction;
friend class WriteTransaction;
public:
Base(const QString& name, uint16_t mapSize = 10);
~Base();
void open();
void close();
bool ready() const;
bool removeDirectory();
QString createDirectory();
QString getName() const;
QString getPath() const;
void drop();
Transaction beginReadOnlyTransaction() const;
WriteTransaction beginTransaction();
template <class K, class V>
LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
template <class K, class V>
LMDBAL::Cache<K, V>* addCache(const std::string& storageName);
template <class K, class V>
LMDBAL::Storage<K, V>* getStorage(const std::string& storageName);
template <class K, class V>
LMDBAL::Cache<K, V>* getCache(const std::string& storageName);
private:
typedef std::map<std::string, LMDBAL::iStorage*> Storages; /**<\brief Storage and Cache pointers are saved in the std::map*/
typedef std::set<TransactionID> Transactions; /**<\brief Piblic transaction IDs are saved in the std::set*/
void commitTransaction(TransactionID id);
void abortTransaction(TransactionID id) const;
TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
TransactionID beginTransaction(const std::string& storageName) const;
void commitTransaction(TransactionID id, const std::string& storageName);
void abortTransaction(TransactionID id, const std::string& storageName) const;
TransactionID beginPrivateReadOnlyTransaction(const std::string& storageName) const;
TransactionID beginPrivateTransaction(const std::string& storageName) const;
void commitPrivateTransaction(TransactionID id, const std::string& storageName);
void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
private:
std::string name; /**<\brief Name of this database*/
bool opened; /**<\brief State of this database*/
uint16_t size; /**<\brief lmdb map size in MiB*/
MDB_env* environment; /**<\brief lmdb environment handle*/
Storages storages; /**<\brief Registered storages and caches*/
Transactions* transactions; /**<\brief Active public transactions*/
inline static const std::string emptyName = ""; /**<\brief Empty string for general fallback purposes*/
};
}
#include "operators.hpp"
/**
* \brief Adds LMDBAL::Storage to the database
*
* Defines that the database is going to have the following storage.
* The LMDBAL::Base must be closed
*
* \param[in] storageName - storage name
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
*
* \tparam K - key type of the storage
* \tparam V - value type of the storage
*
* \exception LMDBAL::Opened thrown if this method is called on the opened database
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add storage with repeating name
*/
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
if (opened)
throw Opened(name, "add storage " + storageName);
Storage<K, V>* storage = new Storage<K, V>(this, storageName, duplicates);
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)storage));
if (!pair.second)
throw StorageDuplicate(name, storageName);
return storage;
}
/**
* \brief Adds LMDBAL::Cache to the database
*
* Defines that the database is going to have the following cache.
* The LMDBAL::Base must be closed
*
* \param[in] storageName - cache name
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it
*
* \tparam K - key type of the cache
* \tparam V - value type of the cahce
*
* \exception LMDBAL::Opened thrown if this method is called on the opened database
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add cache with repeating name
*/
template<class K, class V>
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
if (opened)
throw Opened(name, "add cache " + storageName);
Cache<K, V>* cache = new Cache<K, V>(this, storageName, false);
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)cache));
if (!pair.second)
throw StorageDuplicate(name, storageName);
return cache;
}
/**
* \brief Returns LMDBAL::Storage handle
*
* Requested storage must have been added before opening database
* Note that template parameters is user responsibility zone!
* If user, for instance, had added storage <int, int> but calling
* this method with template parameters <std::string, std::string>
* on the same name of the previously added storage, or calling it on cache - the behaviour is undefined
*
* \param[in] storageName - storage name
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
*
* \tparam K - key type of the storage
* \tparam V - value type of the storage
*
* \exception std::out_of_range thrown if storage with the given name was not found
*/
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& storageName) {
return static_cast<Storage<K, V>*>(storages.at(storageName));
}
/**
* \brief Returns LMDBAL::Cache handle
*
* Requested cache must have been added before opening database
* Note that template parameters is user responsibility zone!
* If user, for instance, had added cache <int, int> but calling
* this method with template parameters <std::string, std::string>
* on the same name of the previously added cache, or calling it on storage - the behaviour is undefined
*
* \param[in] storageName - cache name
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it
*
* \tparam K - key type of the cache
* \tparam V - value type of the cahce
*
* \exception std::out_of_range thrown if cache with the given name was not found
*/
template <class K, class V>
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& storageName) {
return static_cast<Cache<K, V>*>(storages.at(storageName));
}
#endif //LMDBAL_BASE_H

138
src/cache.h Normal file
View File

@ -0,0 +1,138 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_CACHE_H
#define LMDBAL_CACHE_H
#include <map>
#include <set>
#include <tuple>
#include "storage.h"
namespace LMDBAL {
template <class K, class V>
class Cache : public Storage<K, V> {
friend class Base;
enum class Mode { /**<it's a cache state when we:*/
nothing, /**< - know nothing about records in database on disk*/
size, /**< - know just an amount of records*/
full /**< - shure that our cache is equal to the database on disk*/
};
enum class Operation { /**<Operation type, used in pcrocessing cache modification after writable transaction is commited*/
add,
remove,
change,
force,
drop,
replace,
addMany
};
typedef std::pair<Operation, void*> Entry;
typedef std::list<Entry> Queue;
typedef std::map<TransactionID, Queue> TransactionCache;
protected:
Cache(Base* parent, const std::string& name, bool duplicates = false);
~Cache() override;
virtual void handleDrop() override;
virtual void transactionStarted(TransactionID txn, bool readOnly) const override;
virtual void transactionCommited(TransactionID txn) override;
virtual void transactionAborted(TransactionID txn) const override;
virtual void discoveredRecord(const K& key, const V& value) const override;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const override;
virtual SizeType count(TransactionID txn) const override;
virtual void addRecord(const K& key, const V& value, TransactionID txn) override;
virtual bool forceRecord(const K& key, const V& value, TransactionID txn) override;
virtual void changeRecord(const K& key, const V& value, TransactionID txn) override;
virtual void removeRecord(const K& key, TransactionID txn) override;
virtual bool checkRecord(const K& key, TransactionID txn) const override;
virtual void getRecord(const K& key, V& out, TransactionID txn) const override;
virtual V getRecord(const K& key, TransactionID txn) const override;
virtual std::map<K, V> readAll(TransactionID txn) const override;
virtual void readAll(std::map<K, V>& out, TransactionID txn) const override;
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn) override;
virtual SizeType addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false) override;
private:
void handleMode() const;
void handleTransactionEntry(const Entry& entry);
void destroyTransactionEntry(const Entry& entry) const;
void handleAddRecord(const K& key, const V& value);
void handleRemoveRecord(const K& key);
void handleChangeRecord(const K& key, const V& value);
void handleForceRecord(const K& key, const V& value, bool added);
void handleReplaceAll(std::map<K, V>* data);
void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize);
void appendToCache(const K& key, const V& value) const;
public:
using Storage<K, V>::drop;
using Storage<K, V>::addRecord;
using Storage<K, V>::forceRecord;
using Storage<K, V>::changeRecord;
using Storage<K, V>::removeRecord;
using Storage<K, V>::checkRecord;
using Storage<K, V>::getRecord;
using Storage<K, V>::readAll;
using Storage<K, V>::replaceAll;
using Storage<K, V>::addRecords;
using Storage<K, V>::count;
virtual int drop(const WriteTransaction& transaction) override;
virtual void addRecord(const K& key, const V& value) override;
virtual bool forceRecord(const K& key, const V& value) override;
virtual void changeRecord(const K& key, const V& value) override;
virtual void removeRecord(const K& key) override;
virtual bool checkRecord(const K& key) const override;
virtual void getRecord(const K& key, V& out) const override;
virtual V getRecord(const K& key) const override;
virtual SizeType count() const override;
virtual std::map<K, V> readAll() const override;
virtual void readAll(std::map<K, V>& out) const override;
virtual void replaceAll(const std::map<K, V>& data) override;
virtual SizeType addRecords(const std::map<K, V>& data, bool overwrite = false) override;
protected:
/**
* \brief Cache mode
*
* Sometimes we have a complete information about the content of the database,
* and we don't even need to call lmdb to fetch or check for data.
* This member actually shows what is the current state, more about them you can read in Enum descriptions
*/
mutable Mode mode;
std::map<K, V>* cache; /**<\brief Cached data*/
std::set<K>* abscent; /**<\brief Set of keys that are definitely not in the cache*/
mutable SizeType sizeDifference; /**<\brief Difference of size between cached data and amount of records in the lmdb storage*/
TransactionCache* transactionCache; /**<\brief All changes made under under uncommited transactions*/
};
}
#include "cache.hpp"
#endif // LMDBAL_CACHE_H

902
src/cache.hpp Normal file
View File

@ -0,0 +1,902 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_CACHE_HPP
#define LMDBAL_CACHE_HPP
#include "cache.h"
#include "exceptions.h"
/**
* \class LMDBAL::Cache
* \brief Storage with additional caching in std::map
*
* \tparam K type of the keys of the cache
* \tparam V type of the values of the cache
*
* You can receive an instance of this class calling LMDBAL::Base::addCache(const std::string&)
* if the database is yet closed and you're defining the storages you're going to need.
* Or you can call LMDBAL::Base::getCache(const std::string&) if you didn't save a pointer to the cache at first
*
* You are not supposed to instantiate or destory instances of this class yourself!
*/
/**
* \brief Creates a cache
*
* \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] name - the name of the storage
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*/
template<class K, class V>
LMDBAL::Cache<K, V>::Cache(Base* parent, const std::string& name, bool duplicates):
Storage<K, V>(parent, name, duplicates),
mode(Mode::nothing),
cache(new std::map<K, V>()),
abscent(new std::set<K>()),
sizeDifference(0),
transactionCache(new TransactionCache) {}
/**
* \brief Destroys a cache
*/
template<class K, class V>
LMDBAL::Cache<K, V>::~Cache() {
delete transactionCache;
delete cache;
delete abscent;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::addRecordMethodName);
if (cache->count(key) > 0)
iStorage::throwDuplicate(iStorage::toString(key));
Storage<K, V>::addRecord(key, value);
handleAddRecord(key, value);
}
template<class K, class V>
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
if (cache->count(key) > 0)
iStorage::throwDuplicate(iStorage::toString(key));
Storage<K, V>::addRecord(key, value, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::pair<K, V>* pair = new std::pair<K, V>(key, value);
tc->second.emplace_back(Operation::add, pair);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleAddRecord(const K& key, const V& value) {
cache->insert(std::make_pair(key, value));
if (mode != Mode::full)
abscent->erase(key);
}
template<class K, class V>
bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::forceRecordMethodName);
bool added = Storage<K, V>::forceRecord(key, value);
handleForceRecord(key, value, added);
return added;
}
template<class K, class V>
bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
bool added = Storage<K, V>::forceRecord(key, value, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::tuple<bool, K, V>* t = new std::tuple<bool, K, V>(added, key, value);
tc->second.emplace_back(Operation::force, t);
}
return added;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleForceRecord(const K& key, const V& value, bool added) {
if (mode == Mode::full) {
(*cache)[key] = value;
} else {
if (added)
abscent->erase(key);
std::pair<typename std::map<K, V>::iterator, bool> result =
cache->insert(std::make_pair(key, value));
if (!result.second)
result.first->second = value;
else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference
handleMode();
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::changeRecordMethodName);
if (mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::changeRecord(key, value);
itr->second = value;
} else {
if (abscent->count(key) > 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::changeRecord(key, value);
typename std::pair<typename std::map<K, V>::iterator, bool> res =
cache->insert(std::make_pair(key, value));
if (!res.second)
res.first->second = value;
else
handleMode();
} catch (const NotFound& error) {
abscent->insert(key);
throw error;
}
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
if (mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::changeRecord(key, value, txn);
} else {
if (abscent->count(key) > 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::changeRecord(key, value, txn);
} catch (const NotFound& error) {
abscent->insert(key);
throw error;
}
}
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::pair<K, V>* pair = new std::pair<K, V>(key, value);
tc->second.emplace_back(Operation::add, pair);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleChangeRecord(const K& key, const V& value) {
if (mode == Mode::full) {
cache->at(key) = value;
} else {
typename std::pair<typename std::map<K, V>::iterator, bool> res =
cache->insert(std::make_pair(key, value));
if (!res.second)
res.first->second = value;
else
handleMode();
}
}
template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
V value;
Cache<K, V>::getRecord(key, value);
return value;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end()) {
out = itr->second;
return;
}
if (mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::getRecord(key, out);
appendToCache(key, out);
return;
} catch (const NotFound& error) {
if (mode != Mode::full)
abscent->insert(key);
throw error;
}
}
template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const {
V value;
Cache<K, V>::getRecord(key, value, txn);
return value;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) const {
//if there are any changes made within this transaction
//I will be able to see them among pending changes
//so, I'm going to go through them in reverse order
//and check every key. If it has anything to do this requested key
//there is a way to tell...
bool currentTransaction = false;
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
currentTransaction = true;
const Queue& queue = tc->second;
for (typename Queue::const_reverse_iterator i = queue.rbegin(), end = queue.rend(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key) {
out = static_cast<std::pair<K, V>*>(entry.second)->second;
return;
}
break;
case Operation::remove:
iStorage::throwNotFound(iStorage::toString(key));
break;
case Operation::change:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key) {
out = static_cast<std::pair<K, V>*>(entry.second)->second;
return;
}
break;
case Operation::force:
if (std::get<1>(*static_cast<std::tuple<bool, K, V>*>(entry.second)) == key) {
out = std::get<2>(*static_cast<std::tuple<bool, K, V>*>(entry.second));
return;
}
break;
case Operation::drop:
iStorage::throwNotFound(iStorage::toString(key));
break;
case Operation::replace: {
std::map<K, V>* newMap = static_cast<std::map<K, V>*>(entry.second);
typename std::map<K, V>::const_iterator vitr = newMap->find(key);
if (vitr != newMap->end()) {
out = vitr->second;
return;
} else {
iStorage::throwNotFound(iStorage::toString(key));
}
}
break;
case Operation::addMany: {
const std::tuple<bool, SizeType, std::map<K, V>>& tuple =
*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
const std::map<K, V>& newElements = std::get<2>(tuple);
typename std::map<K, V>::const_iterator vitr = newElements.find(key);
if (vitr != newElements.end()) {
out = vitr->second;
return;
}
}
break;
}
}
}
//... but if nothing was found or if the transaction is not the one
//which caused the changes i just need to check it among local cache
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end()) {
out = itr->second;
return;
}
if (mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::getRecord(key, out, txn);
if (!currentTransaction)
appendToCache(key, out);
return;
} catch (const NotFound& error) {
if (!currentTransaction && mode != Mode::full)
abscent->insert(key);
throw error;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::discoveredRecord(const K& key, const V& value) const {
appendToCache(key, value);
}
template<class K, class V>
void LMDBAL::Cache<K, V>::discoveredRecord(const K& key, const V& value, TransactionID txn) const {
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc == transactionCache->end()) //there is a way to look though all the records in transaction log and cache the new pair
discoveredRecord(key, value); //if there is nothing in transaction log about it, but it seems like too much for a small gain
}
template<class K, class V>
bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
iStorage::ensureOpened(iStorage::checkRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return true;
if (mode == Mode::full || abscent->count(key) != 0)
return false;
try {
V value = Storage<K, V>::getRecord(key);
appendToCache(key, value);
return true;
} catch (const NotFound& error) {
if (mode != Mode::full)
abscent->insert(key);
return false;
}
}
template<class K, class V>
bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
//if there are any changes made within this transaction
//I will be able to see them among pending changes
//so, I'm going to go through them in reverse order
//and check every key. If it has anything to do this requested key
//there is a way to tell...
bool currentTransaction = false;
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
currentTransaction = true;
const Queue& queue = tc->second;
for (typename Queue::const_reverse_iterator i = queue.rbegin(), end = queue.rend(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key)
return true;
break;
case Operation::remove:
if (*static_cast<K*>(entry.second) == key)
return false;
break;
case Operation::change:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key)
return true;
break;
case Operation::force:
if (std::get<1>(*static_cast<std::tuple<bool, K, V>*>(entry.second)) == key)
return true;
break;
case Operation::drop:
return false;
break;
case Operation::replace:
if (static_cast<std::map<K, V>*>(entry.second)->count(key) > 0)
return true;
else
return false;
break;
case Operation::addMany:
if (std::get<2>(
*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second)
).count(key) > 0)
return true;
break;
}
}
}
//... but if nothing was found or if the transaction is not the one
//which caused the changes i just need to check it among local cache
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return true;
if (mode == Mode::full || abscent->count(key) != 0)
return false;
try {
V value = Storage<K, V>::getRecord(key, txn);
if (!currentTransaction)
appendToCache(key, value);
return true;
} catch (const NotFound& error) {
if (!currentTransaction && mode != Mode::full)
abscent->insert(key);
return false;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::appendToCache(const K& key, const V& value) const {
typename std::pair<typename std::map<K, V>::const_iterator, bool> pair = cache->insert(std::make_pair(key, value));
if (pair.second)
handleMode();
}
template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll() const {
iStorage::ensureOpened(iStorage::readAllMethodName);
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
*cache = Storage<K, V>::readAll(); //that are missing in the cache
abscent->clear();
sizeDifference = 0;
}
return *cache;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out) const {
iStorage::ensureOpened(iStorage::readAllMethodName);
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
Storage<K, V>::readAll(out); //that are missing in the cache
*cache = out;
abscent->clear();
sizeDifference = 0;
}
}
template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const {
std::map<K, V> out;
readAll(out, txn);
return out;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const {
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
Queue& queue = tc->second;
if (mode != Mode::full) {
out = *cache;
for (typename Queue::const_iterator i = queue.begin(), end = queue.end(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
out.insert(*static_cast<std::pair<K, V>*>(entry.second));
break;
case Operation::remove:
out.erase(*static_cast<K*>(entry.second));
break;
case Operation::change: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
out.at(pair->first) = pair->second;
}
break;
case Operation::force:{
const std::tuple<bool, K, V>& tuple =
*static_cast<std::tuple<bool, K, V>*>(entry.second);
out[std::get<1>(tuple)] = std::get<2>(tuple);
}
break;
case Operation::drop:
out.clear();
break;
case Operation::replace:
out = *static_cast<std::map<K, V>*>(entry.second);
break;
case Operation::addMany: {
const std::tuple<bool, SizeType, std::map<K, V>>& t =
*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
const std::map<K, V>& added = std::get<2>(t);
bool overwrite = std::get<0>(t);
for (const std::pair<const K, V>& pair : added) {
if (overwrite)
out[pair.first] = pair.second;
else
out.insert(pair);
}
}
break;
}
}
} else {
Storage<K, V>::readAll(out, txn);
//queue.clear(); //since I'm getting a complete state of the database
queue.emplace_back(Operation::replace, new std::map<K, V>(out)); //I can as well erase all previous cache entries
}
} else {
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
Storage<K, V>::readAll(out, txn); //that are missing in the cache
*cache = out;
abscent->clear();
sizeDifference = 0;
}
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
Storage<K, V>::replaceAll(data);
*cache = data;
if (mode != Mode::full) {
mode = Mode::full;
abscent->clear();
sizeDifference = 0;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data, TransactionID txn) {
Storage<K, V>::replaceAll(data, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
//queue.clear();
std::map<K, V>* map = new std::map<K, V>(data); //since I'm getting a complete state of the database
tc->second.emplace_back(Operation::replace, map); //I can as well erase all previous cache entries
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleReplaceAll(std::map<K, V>* data) {
delete cache;
cache = data;
if (mode != Mode::full) {
mode = Mode::full;
abscent->clear();
sizeDifference = 0;
}
}
template<class K, class V>
LMDBAL::SizeType LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
SizeType newSize = Storage<K, V>::addRecords(data, overwrite);
handleAddRecords(data, overwrite, newSize);
return newSize;
}
template<class K, class V>
LMDBAL::SizeType LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite) {
SizeType newSize = Storage<K, V>::addRecords(data, txn, overwrite);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::tuple<bool, SizeType, std::map<K, V>>* tuple =
new std::tuple<bool, SizeType, std::map<K, V>>(overwrite, newSize, data);
tc->second.emplace_back(Operation::addMany, tuple);
}
return newSize;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize) {
if (mode == Mode::nothing)
mode = Mode::size;
std::map<K, V>& c = *cache;
std::set<K>& a = *abscent;
for (const std::pair<const K, V>& pair : data) {
std::pair<typename std::map<K, V>::iterator, bool> res = c.insert(pair);
if (!res.second) {
if (overwrite)
res.first->second = pair.second;
} else if (mode != Mode::full) {
a.erase(pair.first);
}
}
if (mode != Mode::full) {
sizeDifference = newSize - c.size();
if (sizeDifference == 0) {
mode = Mode::full;
abscent->clear();
}
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::removeRecord(const K& key) {
iStorage::ensureOpened(iStorage::removeRecordMethodName);
bool noKey = false;
if (mode != Mode::full)
noKey = cache->count(key) == 0;
else
noKey = abscent->count(key) > 0;
if (noKey)
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::removeRecord(key);
handleRemoveRecord(key);
}
template<class K, class V>
void LMDBAL::Cache<K, V>::removeRecord(const K& key, TransactionID txn) {
bool noKey = false;
if (mode != Mode::full)
noKey = cache->count(key) == 0;
else
noKey = abscent->count(key) > 0;
if (noKey)
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::removeRecord(key, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end())
tc->second.emplace_back(Operation::remove, new K(key));
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleRemoveRecord(const K& key) {
if (cache->erase(key) == 0) //if it was not cached and we are now in size mode then the sizeDifference would decrease
handleMode();
if (mode != Mode::full)
abscent->insert(key);
}
template<class K, class V>
uint32_t LMDBAL::Cache<K, V>::count() const {
switch (mode) {
case Mode::nothing:
{
uint32_t sz = Storage<K, V>::count();
sizeDifference = sz - cache->size();
if (sz == 0) {
mode = Mode::full;
abscent->clear();
} else {
mode = Mode::size;
}
return sz;
}
case Mode::size:
return cache->size() + sizeDifference;
case Mode::full:
return cache->size();
default:
return 0; //unreachable, no such state, just to suppress the waring
}
}
template<class K, class V>
uint32_t LMDBAL::Cache<K, V>::count(TransactionID txn) const {
int32_t diff = 0;
bool currentTransaction = false;
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
currentTransaction = true;
const Queue& queue = tc->second;
for (typename Queue::const_reverse_iterator i = queue.rbegin(), end = queue.rend(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
++diff;
break;
case Operation::remove:
--diff;
break;
case Operation::change:
break;
case Operation::force:
if (std::get<0>(*static_cast<std::tuple<bool, K, V>*>(entry.second)))
++diff;
break;
case Operation::drop:
return false;
break;
case Operation::replace:
return static_cast<std::map<K, V>*>(entry.second)->size() + diff;
break;
case Operation::addMany:
return std::get<1>(*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second)) + diff;
break;
}
}
}
switch (mode) {
case Mode::nothing: {
uint32_t sz = Storage<K, V>::count(txn);
if (!currentTransaction) {
sizeDifference = sz - cache->size();
if (sz == 0) {
mode = Mode::full;
abscent->clear();
} else {
mode = Mode::size;
}
}
return sz;
}
case Mode::size:
return cache->size() + sizeDifference + diff;
case Mode::full:
return cache->size() + diff;
default:
return 0; //unreachable, no such state, just to suppress the waring
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleMode() const {
if (mode == Mode::size) {
--sizeDifference;
if (sizeDifference == 0) {
mode = Mode::full;
abscent->clear();
}
}
}
template<class K, class V>
int LMDBAL::Cache<K, V>::drop(const WriteTransaction& transaction) {
iStorage::ensureOpened(iStorage::dropMethodName);
TransactionID txn = iStorage::extractTransactionId(transaction, iStorage::dropMethodName);
int res = Storage<K, V>::drop(txn);
if (res != MDB_SUCCESS)
return res;
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end())
tc->second.emplace_back(Operation::drop, nullptr);
return res;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleDrop() {
cache->clear();
abscent->clear();
mode = Mode::full;
sizeDifference = 0;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::transactionStarted(TransactionID txn, bool readOnly) const {
if (!readOnly)
transactionCache->emplace(txn, Queue());
}
template<class K, class V>
void LMDBAL::Cache<K, V>::transactionCommited(TransactionID txn) {
typename TransactionCache::iterator itr = transactionCache->find(txn);
if (itr != transactionCache->end()) {
Queue& queue = itr->second;
for (const Entry& entry : queue)
handleTransactionEntry(entry);
transactionCache->erase(itr);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::transactionAborted(TransactionID txn) const {
typename TransactionCache::iterator itr = transactionCache->find(txn);
if (itr != transactionCache->end()) {
Queue& queue = itr->second;
for (const Entry& entry : queue)
destroyTransactionEntry(entry);
transactionCache->erase(itr);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleTransactionEntry(const Entry& entry) {
switch (entry.first) {
case Operation::add: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
handleAddRecord(pair->first, pair->second);
delete pair;
} break;
case Operation::remove: {
K* key = static_cast<K*>(entry.second);
handleRemoveRecord(*key);
delete key;
} break;
case Operation::change: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
handleChangeRecord(pair->first, pair->second);
delete pair;
} break;
case Operation::force: {
std::tuple<bool, K, V>* tuple = static_cast<std::tuple<bool, K, V>*>(entry.second);
const std::tuple<bool, K, V>& t = *tuple;
handleForceRecord(std::get<1>(t), std::get<2>(t), std::get<0>(t));
delete tuple;
} break;
case Operation::drop:
handleDrop();
break;
case Operation::replace:
handleReplaceAll(static_cast<std::map<K, V>*>(entry.second)); //I take ownership, no need to delete
break;
case Operation::addMany: {
std::tuple<bool, SizeType, std::map<K, V>>* tuple = static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
const std::tuple<bool, SizeType, std::map<K, V>>& t = * tuple;
handleAddRecords(std::get<2>(t), std::get<0>(t), std::get<1>(t));
delete tuple;
} break;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::destroyTransactionEntry(const Entry& entry) const {
switch (entry.first) {
case Operation::add:
delete static_cast<std::pair<K, V>*>(entry.second);
break;
case Operation::remove:
delete static_cast<K*>(entry.second);
break;
case Operation::change:
delete static_cast<std::pair<K, V>*>(entry.second);
break;
case Operation::force:
delete static_cast<std::tuple<bool, K, V>*>(entry.second);
break;
case Operation::drop:
break;
case Operation::replace:
delete static_cast<std::map<K, V>*>(entry.second);
break;
case Operation::addMany:
delete static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
break;
}
}
#endif //LMDBAL_CACHE_HPP

109
src/cursor.h Normal file
View File

@ -0,0 +1,109 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_CURSOR_H
#define LMDBAL_CURSOR_H
#include <string>
#include "lmdb.h"
#include "base.h"
#include "storage.h"
#include "transaction.h"
namespace LMDBAL {
template <class K, class V>
class Cursor {
friend class Storage<K, V>;
private:
enum State { /**<Cursor state:*/
closed, /**< - closed*/
openedPublic, /**< - opened with public transaction, all storages will be notified about it after it's done*/
openedPrivate /**< - opened with private transaction, only current storage will be notified when cursor is closed*/
};
public:
Cursor();
Cursor(Storage<K, V>* parent);
Cursor(const Cursor& other) = delete;
Cursor(Cursor&& other);
~Cursor();
Cursor& operator = (const Cursor& other) = delete;
Cursor& operator = (Cursor&& other);
void open();
void open(const Transaction& transaction);
void renew();
void renew(const Transaction& transaction);
void close();
bool opened() const;
bool empty() const;
void drop();
std::pair<K, V> first();
std::pair<K, V> last();
std::pair<K, V> next();
std::pair<K, V> prev();
std::pair<K, V> current() const;
bool set(const K& target);
void first(K& key, V& value);
void last(K& key, V& value);
void next(K& key, V& value);
void prev(K& key, V& value);
void current(K& key, V& value) const;
private:
void dropped();
void freed();
void terminated();
void operateCursorRead(K& key, V& value, MDB_cursor_op operation, const std::string& methodName, const std::string& operationName) const;
private:
Storage<K, V>* storage;
MDB_cursor* cursor;
State state;
uint32_t id;
inline static const std::string openCursorMethodName = "Cursor::open"; /**<\brief member function name, just for exceptions*/
inline static const std::string closeCursorMethodName = "Cursor::close"; /**<\brief member function name, just for exceptions*/
inline static const std::string renewCursorMethodName = "Cursor::renew"; /**<\brief member function name, just for exceptions*/
inline static const std::string firstMethodName = "first"; /**<\brief member function name, just for exceptions*/
inline static const std::string lastMethodName = "last"; /**<\brief member function name, just for exceptions*/
inline static const std::string nextMethodName = "next"; /**<\brief member function name, just for exceptions*/
inline static const std::string prevMethodName = "prev"; /**<\brief member function name, just for exceptions*/
inline static const std::string currentMethodName = "current"; /**<\brief member function name, just for exceptions*/
inline static const std::string setMethodName = "set"; /**<\brief member function name, just for exceptions*/
inline static const std::string firstOperationName = "Cursor::first"; /**<\brief member function name, just for exceptions*/
inline static const std::string lastOperationName = "Cursor::last"; /**<\brief member function name, just for exceptions*/
inline static const std::string nextOperationName = "Cursor::next"; /**<\brief member function name, just for exceptions*/
inline static const std::string prevOperationName = "Cursor::prev"; /**<\brief member function name, just for exceptions*/
inline static const std::string currentOperationName = "Cursor::current"; /**<\brief member function name, just for exceptions*/
};
};
#include "cursor.hpp"
#endif //LMDBAL_CURSOR_H

661
src/cursor.hpp Normal file
View File

@ -0,0 +1,661 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_CURSOR_HPP
#define LMDBAL_CURSOR_HPP
#include "cursor.h"
#include <iostream>
/**
* \class LMDBAL::Cursor
* \brief An object to iterate storages.
*
* \tparam K type of the keys in the storage that this cursor would iterate
* \tparam V type of the values in the storage that this cursor would iterate
*
* Cursor allowes you to perform sequential querries, navigate from one element to another at reduced operation price.
* For now Cursors are read only but in the future you might also be able to write to the storage with them.
*
* Cursors are owned by the storage, they die with the storage.
* They also get closed if the storage is closed (if you close by the database for example)
*
* You can obtain an instance of this class calling LMDBAL::Storage::createCursor()
* and destory it calling LMDBAL::Storage::destoryCursor() at any time, LMDBAL::Base doesn't necessarily need to be opened.
*
* You are not supposed to instantiate or destory instances of this class yourself!
*/
static uint32_t idCounter = 0;
/**
* \brief Creates a cursor
*
* \param[in] parent a storage that created this cursor
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
storage(parent),
cursor(nullptr),
state(closed),
id(++idCounter)
{
storage->cursors[id] = this;
}
/**
* \brief Creates an empty cursor.
*
* It's not usable, but can exist just to be a target of moves
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor():
storage(nullptr),
cursor(nullptr),
state(closed),
id(0)
{}
/**
* \brief Moves from another cursor
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Cursor&& other):
storage(other.storage),
cursor(other.cursor),
state(other.state),
id(other.id)
{
other.terminated();
if (id != 0)
storage->cursors[id] = this;
other.freed();
}
/**
* \brief A private function that turns cursor into an empty one
*
* This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid.
* Those cursors will become empty, and can't be used anymore
*/
template<class K, class V>
LMDBAL::Cursor<K, V>& LMDBAL::Cursor<K, V>::operator = (Cursor&& other) {
terminated();
if (id != 0)
storage->cursors.erase(id);
storage = other.storage;
cursor = other.cursor;
state = other.state;
id = other.id;
if (id != 0) {
other.freed();
other.state = closed;
storage->cursors[id] = this;
}
return *this;
}
/**
* \brief Destroys a cursor
*
* If the cursor wasn't properly closed - it's going to be upon destruction
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::~Cursor () {
close();
if (id != 0)
storage->cursors.erase(id);
}
/**
* \brief Turns cursor into an empty one, releasing resources
*
* This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid.
* Those cursors will become empty, and can't be used anymore
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::drop () {
close();
if (id != 0)
storage->cursors.erase(id);
freed();
}
/**
* \brief A private method that turns cursor into an empty one
*
* This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid cursors.
* Those cursors will become empty, and can't be used anymore
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::dropped () {
terminated();
freed();
}
/**
* \brief A private method that turns cursor into an empty one (submethod)
*
* This function is called from LMDBAL::Storage, when the cursor is getting destoryed.
* Those cursors will become empty, and can't be used anymore
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::freed () {
cursor = nullptr;
storage = nullptr;
id = 0;
}
/**
* \brief Returns true if the cursor is empty
*
* Empty cursors can't be used, they can be only targets of move operations
*/
template<class K, class V>
bool LMDBAL::Cursor<K, V>::empty () const {
return id == 0;
}
/**
* \brief A private function the storage owning this cursor will call to inform this cursor that the thansaction needs to be aborted
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::terminated () {
close(); //for now it's the same, but if I ever going to make writable cursor - here is where it's gonna be different
}
/**
* \brief Opens the cursor for operations.
*
* This is a normal way to start the sequence of operations with the cursor.
* This variant of the function creates a read only transaction just for this cursor
*
* This function should be called when the LMDBAL::Storage is already opened and before any query with this cursor!
* It will do nothing to a cursor that was already opened (no matter what way).
*
* \exception LMDBAL::Closed thrown if you try to open the cursor on a closed database
* \exception LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb, or to begin a transaction
* \exception LMDBAL::CursorEmpty thrown if the cursor was empty
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::open () {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(openCursorMethodName);
switch (state) {
case closed: {
TransactionID txn = storage->beginReadOnlyTransaction();
int result = storage->_mdbCursorOpen(txn, &cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn);
storage->transactionStarted(txn, true);
state = openedPrivate;
} break;
default:
break;
}
}
/**
* \brief Opens the cursor for operations.
*
* This is a normal way to start the sequence of operations with the cursor.
* This variant of the function uses for queries a transaction you have obtained somewhere else.
*
* This function should be called when the LMDBAL::Storage is already opened and before any query with this cursor!
* It will do nothing to a cursor that was already opened (no matter what way).
*
* \param[in] transaction - a transaction, can be read only
*
* \exception LMDBAL::Closed thrown if you try to open the cursor on a closed database
* \exception LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
* \exception LMDBAL::CursorEmpty thrown if the cursor was empty
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::open (const Transaction& transaction) {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(openCursorMethodName);
TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName);
switch (state) {
case closed: {
int result = storage->_mdbCursorOpen(txn, &cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result);
state = openedPublic;
} break;
default:
break;
}
}
/**
* \brief Renews a cursor
*
* This function aborts current transaction if the cursor was opened with it's own transaction
* (does not mess up if the transaction was public),
* creates new private transaction and rebinds this cursor to it.
*
* Theoretically you could call this method if your public transaction was aborted (or commited)
* but you wish to continue to keep working with your cursor.
* Or if you just want to rebind your cursor to a new private transaction.
*
* This function does nothing if the cursor is closed
*
* \exception LMDBAL::Closed thrown if you try to renew the cursor on a closed database
* \exception LMDBAL::Unknown thrown if there was a problem beginning new transaction or if there was a problem renewing the cursor by lmdb
* \exception LMDBAL::CursorEmpty thrown if the cursor was empty
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew () {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(renewCursorMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->abortTransaction(txn);
storage->transactionAborted(txn);
[[fallthrough]];
}
case openedPublic: {
TransactionID txn = storage->beginReadOnlyTransaction();
int result = storage->_mdbCursorRenew(txn, cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn);
storage->transactionStarted(txn, true);
state = openedPrivate;
} break;
default:
break;
}
}
/**
* \brief Renews a cursor
*
* This function aborts current transaction if the cursor was opened with it's own transaction
* (does not mess up if the transaction was public),
* and rebinds this cursor to a passed new transaction.
*
* Theoretically you could call this method if your previous public transaction was aborted (or commited)
* but you wish to continue to keep working with your cursor.
* Or if you just want to rebind your cursor to another public transaction.
*
* This function does nothing if the cursor is closed
*
* \param[in] transaction - a transaction you wish this cursor to be bound to
*
* \exception LMDBAL::Closed thrown if you try to renew the cursor on a closed database
* \exception LMDBAL::Unknown thrown if there was a problem renewing the cursor by lmdb
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
* \exception LMDBAL::CursorEmpty thrown if the cursor was empty
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew (const Transaction& transaction) {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(renewCursorMethodName);
TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->abortTransaction(txn);
storage->transactionAborted(txn);
[[fallthrough]];
}
case openedPublic: {
int result = storage->_mdbCursorRenew(txn, cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result);
state = openedPublic;
} break;
default:
break;
}
}
/**
* \brief Termiates a sequence of operations with the cursor
*
* This is a normal way to tell that you're done with the cursor and don't want to continue the sequence of queries.
* The state of the cursor is lost after calling this method, some inner resorce is freed.
*
* If the cursor was opened with the private transaction - the owner storage will be notified of the aborted transaction.
*
* This function does nothing on a closed cursor.
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::close () {
switch (state) {
case openedPublic: {
storage->_mdbCursorClose(cursor);
state = closed;
} break;
case openedPrivate: {
TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->_mdbCursorClose(cursor);
storage->abortTransaction(txn);
storage->transactionAborted(txn);
state = closed;
} break;
default:
break;
}
}
/**
* \brief Tells if the cursor is open
*/
template<class K, class V>
bool LMDBAL::Cursor<K, V>::opened () const {
return state != closed;
}
/**
* \brief Queries the first element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::first (K& key, V& value) {
operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
}
/**
* \brief Queries the last element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::last (K& key, V& value) {
operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
}
/**
* \brief Queries the next element from the storage
*
* If there was no operation before this method positions the cursor on the first element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::first(K key, V value).
*
* It will also throw LMDBAL::NotFound if you call this method being on the last element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::next (K& key, V& value) {
operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
}
/**
* \brief Queries the previous element from the storage
*
* If there was no operation before this method positions the cursor on the last element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::last(K key, V value).
*
* It will also throw LMDBAL::NotFound if you call this method being on the first element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::prev (K& key, V& value) {
operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
}
/**
* \brief Returns current cursor element from the storage
*
* If there was no operation before this method throws LMDBAL::Unknown for some reason
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
operateCursorRead(key, value, MDB_GET_CURRENT, currentMethodName, currentOperationName);
}
/**
* \brief Queries the first element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::first () {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
return result;
}
/**
* \brief Queries the last element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::last () {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
return result;
}
/**
* \brief Queries the next element from the storage
*
* If there was no operation before this method positions the cursor on the first element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::first().
*
* It will also throw LMDBAL::NotFound if you call this method being on the last element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::next () {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
return result;
}
/**
* \brief Queries the previous element from the storage
*
* If there was no operation before this method positions the cursor on the last element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::last().
*
* It will also throw LMDBAL::NotFound if you call this method being on the first element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
* \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::prev () {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
return result;
}
/**
* \brief Returns current cursor element from the storage
*
* If there was no operation before this method throws LMDBAL::Unknown for some reason
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
* \exception LMDBAL::Unknown thrown if there was no positioning operation before of if there was some unexpected problem with lmdb
*/
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_GET_CURRENT, currentMethodName, currentOperationName);
return result;
}
/**
* \brief Sets cursors to the defined position
*
* If exactly the same element wasn't found it sets the cursor to the first
* element greater then the key and returns false
*
* \param[in] key a key of the element you would like the cursor to be
* \returns true if the exact value was found, false otherwise
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::Unknown thrown if there was some unexpected problem
*/
template<class K, class V>
bool LMDBAL::Cursor<K, V>::set (const K& key) {
if (state == closed)
storage->throwCursorNotReady(setMethodName);
MDB_val mdbKey = storage->keySerializer.setData(key);
int result = storage->_mdbCursorSet(cursor, mdbKey);
if (result == MDB_SUCCESS)
return true;
else if (result == MDB_NOTFOUND)
return false;
storage->throwUnknown(result);
return false; //unreachable, just to suppress the warning
}
/**
* \brief a private mothod that actually doing all the reading
*
* Queries the storage, deserializes the output, notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
* \param[in] operation LMDB cursor <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#ga1206b2af8b95e7f6b0ef6b28708c9127">operation code</a>
* \param[in] methodName a name of the method you called it from, just for the exception message if something goes not as expected
* \param[in] operationName a name of the opeartion, just for the exception message if something goes not as expected
*
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \exception LMDBAL::NotFound mostly thrown if the query wasn't found
* \exception LMDBAL::Unknown mostly thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::operateCursorRead(
K& key,
V& value,
MDB_cursor_op operation,
const std::string& methodName,
const std::string& operationName
) const {
if (state == closed)
storage->throwCursorNotReady(methodName);
MDB_val mdbKey, mdbValue;
int result = storage->_mdbCursorGet(cursor, mdbKey, mdbValue, operation);
if (result != MDB_SUCCESS)
storage->throwNotFoundOrUnknown(result, operationName);
storage->keySerializer.deserialize(mdbKey, key);
storage->valueSerializer.deserialize(mdbValue, value);
if (state == openedPrivate)
storage->discoveredRecord(key, value);
else
storage->discoveredRecord(key, value, storage->_mdbCursorTxn(cursor));
}
#endif //LMDBAL_CURSOR_HPP

180
src/exceptions.cpp Normal file
View File

@ -0,0 +1,180 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "exceptions.h"
LMDBAL::Exception::Exception():
std::exception()
{}
LMDBAL::Exception::~Exception() {}
const char* LMDBAL::Exception::what() const noexcept( true ) {
std::string* msg = new std::string(getMessage());
return msg->c_str();
}
LMDBAL::Directory::Directory(const std::string& p_path):
Exception(),
path(p_path) {}
std::string LMDBAL::Directory::getMessage() const {
return "Can't create directory for database at " + path;}
LMDBAL::Closed::Closed(
const std::string& p_operation,
const std::string& p_dbName,
const std::optional<std::string>& p_tableName
):
Exception(),
operation(p_operation),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::Closed::getMessage() const {
std::string msg = "An attempt to perform operation " + operation
+ " on closed database " + dbName;
if (tableName.has_value())
msg += + " on table " + tableName.value();
return msg;
}
LMDBAL::CursorNotReady::CursorNotReady(
const std::string& p_operation,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
operation(p_operation),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::CursorNotReady::getMessage() const {
std::string msg = "An attempt to perform operation " + operation
+ " on closed cursor that belongs to the table " + tableName
+ " within database " + dbName;
return msg;
}
LMDBAL::CursorEmpty::CursorEmpty(const std::string & operation):
Exception(),
operation(operation) {}
std::string LMDBAL::CursorEmpty::getMessage() const {
return "An attempt to perform an operation \"" + operation + "\" on an empty cursor";
}
LMDBAL::Opened::Opened(const std::string& p_dbName, const std::string& p_action):
Exception(),
dbName(p_dbName),
action(p_action) {}
std::string LMDBAL::Opened::getMessage() const {
return "An attempt to " + action
+ " (the database " + dbName
+ ") but it's can't be done because the Base is already opened";
}
LMDBAL::NotFound::NotFound(
const std::string& p_key,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
key(p_key),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::NotFound::getMessage() const {
return "Element for id " + key + " wasn't found "
+ " in database " + dbName
+ " in table " + tableName;}
LMDBAL::StorageDuplicate::StorageDuplicate(
const std::string& p_dbName,
const std::string& p_tableName
):
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::StorageDuplicate::getMessage() const {
return "An attempt to add a storage (or cache) " + tableName
+ " to database " + dbName
+ " but the database already has a storage with given name";
}
LMDBAL::Exist::Exist(
const std::string& p_key,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
key(p_key),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::Exist::getMessage() const {
return "An attempt to insert element with key " + key
+ " to database " + dbName
+ " to table " + tableName
+ " but it already has an element with given id";
}
LMDBAL::TransactionTerminated::TransactionTerminated(
const std::string& dbName,
const std::string& tableName,
const std::string& action
) :
dbName(dbName),
tableName(tableName),
action(action)
{}
std::string LMDBAL::TransactionTerminated::getMessage() const {
std::string result = "Error";
if (!action.empty())
result += " perfoming action " + action;
result += " in database " + dbName;
result += ", table " + tableName;
result += ". The transaction is already terminated";
return result;
}
LMDBAL::Unknown::Unknown(
const std::string& p_dbName,
const std::string& message,
const std::optional<std::string>& p_tableName
):
Exception(),
dbName(p_dbName),
tableName(p_tableName),
msg(message) {}
std::string LMDBAL::Unknown::getMessage() const {
std::string result = "Unknown error in database " + dbName;
if (tableName.has_value())
result += " in table " + tableName.value();
result += ": " + msg;
return result;
}

241
src/exceptions.h Normal file
View File

@ -0,0 +1,241 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_EXCEPTIONS_H
#define LMDBAL_EXCEPTIONS_H
#include <stdexcept>
#include <string>
#include <optional>
namespace LMDBAL {
/**
* \brief Parent abstract class for all LMDBAL exceptions
*/
class Exception : public std::exception {
public:
Exception();
virtual ~Exception();
virtual std::string getMessage() const = 0; /**<\brief returns exception message*/
const char* what() const noexcept( true ) override; /**<\brief system exception method that is actually called to show the message*/
};
/**
* \brief Thrown if LMDBAL had issues creating or opening database directory
*/
class Directory: public Exception {
public:
/**
* \brief Creates exception
*
* \param path - path of the directory that was supposed to be used to store the database
*/
Directory(const std::string& path);
std::string getMessage() const;
private:
std::string path;
};
/**
* \brief Thrown if something in the database was called on closed state and it is not supported
*/
class Closed : public Exception {
public:
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on closed database
* \param dbName - name of the database
* \param tableName - name of the storage which called that method, abscent if it's untracable or if it's thrown by the database
*/
Closed(const std::string& operation, const std::string& dbName, const std::optional<std::string>& tableName = std::nullopt);
std::string getMessage() const;
private:
std::string operation;
std::string dbName;
std::optional<std::string> tableName;
};
/**
* \brief Thrown if the cursor was operated in closed state
*/
class CursorNotReady : public Exception {
public:
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on closed cursor
* \param dbName - name of the database
* \param tableName - name of the storage owning the cursor
*/
CursorNotReady(const std::string& operation, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string operation;
std::string dbName;
std::string tableName;
};
/**
* \brief Thrown if an empty cursor was somehow operated
*/
class CursorEmpty : public Exception {
public:
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on an empty cursor
*/
CursorEmpty(const std::string& operation);
std::string getMessage() const;
private:
std::string operation;
};
/**
* \brief Thrown if something in the database was called on opened state and it is not supported
*/
class Opened : Exception {
public:
/**
* \brief Creates exception
*
* \param action - text name of the method that was called on opened database
* \param dbName - name of the database
*/
Opened(const std::string& dbName, const std::string& action);
std::string getMessage() const;
private:
std::string dbName;
std::string action;
};
/**
* \brief Thrown if something in the database was not found
*/
class NotFound : public Exception {
public:
/**
* \brief Creates exception
*
* \param key - record key that was not found
* \param dbName - name of the database
* \param tableName - name of the storage that was looked for a record
*/
NotFound(const std::string& key, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string key;
std::string dbName;
std::string tableName;
};
/**
* \brief Thrown if there was attempt to define storages with conflicting names
*/
class StorageDuplicate : public Exception {
public:
/**
* \brief Creates exception
*
* \param dbName - name of the database
* \param tableName - that name that was conflicting
*/
StorageDuplicate(const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string dbName;
std::string tableName;
};
/**
* \brief Thrown if there was a key conflict in one of the storages
*/
class Exist : public Exception {
public:
/**
* \brief Creates exception
*
* \param key - record key that caused the conflict
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with
*/
Exist(const std::string& key, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string key;
std::string dbName;
std::string tableName;
};
/**
* Thrown if there was an attempt to perform action using terminated transaction
*/
class TransactionTerminated : public Exception {
public:
/**
* \brief Creates exception
*
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with
* \param action - optional action, just to enrich the exception message
*/
TransactionTerminated(const std::string& dbName, const std::string& tableName, const std::string& action = "");
std::string getMessage() const;
private:
std::string dbName;
std::string tableName;
std::string action;
};
/**
* \brief Thrown if something unexpected happened
*/
class Unknown : public Exception {
public:
/**
* \brief Creates exception
*
* \param message - text description of the error, most of the times contains the result of <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#ga569e66c1e3edc1a6016b86719ee3d098">mdb_strerror</a>
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with, abscent if the operation was with the database itself
*/
Unknown(const std::string& dbName, const std::string& message, const std::optional<std::string>& tableName = std::nullopt);
std::string getMessage() const;
private:
std::string dbName;
std::optional<std::string> tableName;
std::string msg;
};
}
#endif //LMDBAL_EXCEPTIONS_H

213
src/operators.hpp Normal file
View File

@ -0,0 +1,213 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_OPERATORS_HPP
#define LMDBAL_OPERATORS_HPP
#include <map>
#include <set>
#include <vector>
#include <list>
#include <deque>
#include <QDataStream>
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::map<K, V>& container) {
uint32_t size = container.size();
out << size;
for (const std::pair<const K, V>& pair : container) {
out << pair;
}
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::map<K, V>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
in >> pair;
container.insert(pair);
}
return in;
}
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::multimap<K, V>& container) {
uint32_t size = container.size();
out << size;
for (const std::pair<const K, V>& pair : container) {
out << pair;
}
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::multimap<K, V>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
in >> pair;
container.insert(pair);
}
return in;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) {
out << pair.first;
out << pair.second;
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) {
in >> container.first;
in >> container.second;
return in;
}
#endif
template <class K>
QDataStream& operator << (QDataStream &out, const std::set<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::set<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
K value;
in >> value;
container.insert(value);
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::multiset<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::multiset<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
K value;
in >> value;
container.insert(value);
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::vector<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::vector<K>& container) {
uint32_t size;
in >> size;
container.resize(size);
for (uint32_t i = 0; i < size; ++i) {
in >> container[i];
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::deque<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::deque<K>& container) {
uint32_t size;
in >> size;
container.resize(size);
for (uint32_t i = 0; i < size; ++i) {
in >> container[i];
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::list<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::list<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
typename std::list<K>::iterator itr = container.emplace(container.end());
in >> *itr;
}
return in;
}
#endif //LMDBAL_OPERATORS_HPP

View File

@ -0,0 +1,19 @@
set(HEADERS
serializer.h
serializer.hpp
serializer_uint8.hpp
serializer_uint16.hpp
serializer_uint32.hpp
serializer_uint64.hpp
serializer_int8.hpp
serializer_int16.hpp
serializer_int32.hpp
serializer_int64.hpp
serializer_float.hpp
serializer_double.hpp
serializer_stdstring.hpp
serializer_qstring.hpp
serializer_qbytearray.hpp
)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW})

View File

@ -0,0 +1,72 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_H
#define LMDBAL_SERIALIZER_H
#include <cstring>
#include <QByteArray>
#include <QBuffer>
#include <QDataStream>
#include <lmdb.h>
namespace LMDBAL {
template<class T>
class Serializer {
public:
Serializer();
Serializer(const T& value);
~Serializer();
T deserialize(const MDB_val& value);
void deserialize(const MDB_val& value, T& result);
MDB_val setData(const T& value);
MDB_val getData();
void clear();
private:
void _setData(const T& value);
private:
QByteArray bytes;
QBuffer buffer;
QDataStream stream;
};
}
#include "serializer.hpp"
#include "serializer_uint64.hpp"
#include "serializer_uint32.hpp"
#include "serializer_uint16.hpp"
#include "serializer_uint8.hpp"
#include "serializer_int64.hpp"
#include "serializer_int32.hpp"
#include "serializer_int16.hpp"
#include "serializer_int8.hpp"
#include "serializer_float.hpp"
#include "serializer_double.hpp"
#include "serializer_stdstring.hpp"
#include "serializer_qstring.hpp"
#include "serializer_qbytearray.hpp"
#endif // LMDBAL_SERIALIZER_H

View File

@ -0,0 +1,163 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_HPP
#define LMDBAL_SERIALIZER_HPP
#include "serializer.h"
/**
* \class LMDBAL::Serializer
* \brief A class handling serialization/deserialization
*
* A class that is constructed in every LMDBAL::Storage
* to serialize or deserialize keys and values.
*
* It serializes to and deserializes from <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#structMDB__val">MDB_val</a>
*
* \tparam K type of the keys of the storage
* \tparam V type of the values of the storage
*/
/**
* \brief Creates an empty Serializer
*/
template<class T>
LMDBAL::Serializer<T>::Serializer() :
bytes(),
buffer(&bytes),
stream(&buffer)
{
buffer.open(QIODevice::ReadWrite);
}
/**
* \brief Creates a Serializer with some data in it
*
* The data automatically gets serialized
*
* \param[in] value - a value that is assigned to the serializer
*/
template<class T>
LMDBAL::Serializer<T>::Serializer(const T& value) :
bytes(),
buffer(&bytes),
stream(&buffer)
{
buffer.open(QIODevice::ReadWrite);
_setData(value);
}
/**
* \brief Destoys the serializer
*/
template<class T>
LMDBAL::Serializer<T>::~Serializer() {
buffer.close();
}
/**
* \brief Sets the data to the seriazer
*
* This is a normal way to seriaze value
*
* \param[in] value - a value you want to serialize
*
* \returns serialized value
*/
template<class T>
MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
clear();
_setData(value);
return getData();
}
/**
* \brief Deserializes value
*
* This is a normal way to deseriaze value
*
* \param[in] value - a value you want to deserialize
*
* \returns deserialized value
*/
template<class T>
T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
T result;
deserialize(value, result);
return result;
}
/**
* \brief Deserializes value
*
* This is a normal way to deseriaze value
*
* \param[in] value - a value you want to deserialize
* \param[out] result - deserialized value
*/
template<class T>
void LMDBAL::Serializer<T>::deserialize(const MDB_val& value, T& result) {
clear();
bytes.setRawData((char*)value.mv_data, value.mv_size);
stream >> result;
}
/**
* \brief Private function that handles serialization
*
* \param[in] value - a value you want to serialize
*/
template<class T>
void LMDBAL::Serializer<T>::_setData(const T& value) {
stream << value;
}
/**
* \brief Clears the state of serializer
*
* Normally you don't need to call this function
*/
template<class T>
void LMDBAL::Serializer<T>::clear() {
if (buffer.pos() > 0) {
buffer.seek(0);
}
}
/**
* \brief Returns the data if it already was serialized
*
* Normally you don't need to call this function
*
* This may be usefull if you called LMDBAL::Serilizer::setData() but lost the result
*
* \returns Serialized data
*/
template<class T>
MDB_val LMDBAL::Serializer<T>::getData() {
MDB_val val;
val.mv_size = buffer.pos();
val.mv_data = (char*)bytes.data();
return val;
}
#endif //LMDBAL_SERIALIZER_HPP

View File

@ -0,0 +1,59 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_DOUBLE_HPP
#define LMDBAL_SERIALIZER_DOUBLE_HPP
namespace LMDBAL {
template<>
class Serializer<double> {
public:
Serializer():value(0) {};
Serializer(const double& p_value):value(p_value) {};
~Serializer() {};
double deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, double& result) {
std::memcpy(&result, data.mv_data, 8);
}
MDB_val setData(const double& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 8;
return result;
};
void clear() {}; //not possible;
private:
double value;
};
}
#endif //LMDBAL_SERIALIZER_DOUBLE_HPP

View File

@ -0,0 +1,59 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_FLOAT_HPP
#define LMDBAL_SERIALIZER_FLOAT_HPP
namespace LMDBAL {
template<>
class Serializer<float> {
public:
Serializer():value(0) {};
Serializer(const float& p_value):value(p_value) {};
~Serializer() {};
float deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, float& result) {
std::memcpy(&result, data.mv_data, 4);
}
MDB_val setData(const float& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 4;
return result;
};
void clear() {}; //not possible;
private:
float value;
};
}
#endif //LMDBAL_SERIALIZER_FLOAT_HPP

View File

@ -0,0 +1,58 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_INT16_HPP
#define LMDBAL_SERIALIZER_INT16_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int16_t> {
public:
Serializer():value(0) {};
Serializer(const int16_t& p_value):value(p_value) {};
~Serializer() {};
int16_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, int16_t& result) {
std::memcpy(&result, data.mv_data, 2);
}
MDB_val setData(const int16_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 2;
return result;
};
void clear() {}; //not possible;
private:
int16_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT16_HPP

View File

@ -0,0 +1,61 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_INT32_HPP
#define LMDBAL_SERIALIZER_INT32_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int32_t> {
public:
Serializer():value(0) {};
Serializer(const int32_t& p_value):value(p_value) {};
~Serializer() {};
int32_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, int32_t& result) {
std::memcpy(&result, data.mv_data, 4);
}
MDB_val setData(const int32_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 4;
return result;
};
void clear() {}; //not possible;
private:
int32_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT32_HPP

View File

@ -0,0 +1,61 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_INT64_HPP
#define LMDBAL_SERIALIZER_INT64_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int64_t> {
public:
Serializer():value(0) {};
Serializer(const int64_t& p_value):value(p_value) {};
~Serializer() {};
int64_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, int64_t& result) {
std::memcpy(&result, data.mv_data, 8);
}
MDB_val setData(const int64_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 8;
return result;
};
void clear() {}; //not possible;
private:
int64_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT64_HPP

View File

@ -0,0 +1,61 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_INT8_HPP
#define LMDBAL_SERIALIZER_INT8_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int8_t> {
public:
Serializer():value(0) {};
Serializer(const int8_t& p_value):value(p_value) {};
~Serializer() {};
int8_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, int8_t& result) {
std::memcpy(&result, data.mv_data, 1);
}
MDB_val setData(const int8_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 1;
return result;
};
void clear() {}; //not possible;
private:
int8_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT8_HPP

View File

@ -0,0 +1,64 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_QBYTEARRAY_HPP
#define LMDBAL_SERIALIZER_QBYTEARRAY_HPP
#include <QByteArray>
namespace LMDBAL {
template<>
class Serializer<QByteArray> {
public:
Serializer():value() {};
Serializer(const QByteArray& p_value):value(p_value) {};
~Serializer() {};
QByteArray deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, QByteArray& result) {
result.setRawData((char*)data.mv_data, data.mv_size);
}
MDB_val setData(const QByteArray& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = value.data();
result.mv_size = value.size();
return result;
};
void clear() {
value.clear();
};
private:
QByteArray value;
};
}
#endif //LMDBAL_SERIALIZER_QBYTEARRAY_HPP

View File

@ -0,0 +1,65 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_QSTRING_HPP
#define LMDBAL_SERIALIZER_QSTRING_HPP
#include <QString>
#include <QByteArray>
namespace LMDBAL {
template<>
class Serializer<QString> {
public:
Serializer():value() {};
Serializer(const QString& p_value):value(p_value.toUtf8()) {};
~Serializer() {};
QString deserialize(const MDB_val& data) {
value = QByteArray((char*)data.mv_data, data.mv_size);
return QString::fromUtf8(value);
};
void deserialize(const MDB_val& data, QString& result) {
value = QByteArray((char*)data.mv_data, data.mv_size);
result = QString::fromUtf8(value);
}
MDB_val setData(const QString& data) {
value = data.toUtf8();
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = value.data();
result.mv_size = value.size();
return result;
};
void clear() {}; //not possible;
private:
QByteArray value;
};
}
#endif //LMDBAL_SERIALIZER_QSTRING_HPP

View File

@ -0,0 +1,62 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_STDSTRING_HPP
#define LMDBAL_SERIALIZER_STDSTRING_HPP
#include <string>
namespace LMDBAL {
template<>
class Serializer<std::string> {
public:
Serializer():value() {};
Serializer(const std::string& p_value):value(p_value) {};
~Serializer() {};
std::string deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, std::string& result) {
result.assign((char*)data.mv_data, data.mv_size);
}
MDB_val setData(const std::string& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = (char*)value.c_str();
result.mv_size = value.size();
return result;
};
void clear() {}; //not possible;
private:
std::string value;
};
}
#endif //LMDBAL_SERIALIZER_STDSTRING_HPP

View File

@ -0,0 +1,60 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_UINT16_HPP
#define LMDBAL_SERIALIZER_UINT16_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint16_t> {
public:
Serializer():value(0) {};
Serializer(const uint16_t& p_value):value(p_value) {};
~Serializer() {};
uint16_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, uint16_t& result) {
std::memcpy(&result, data.mv_data, 2);
}
MDB_val setData(const uint16_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 2;
return result;
};
void clear() {}; //not possible;
private:
uint16_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT16_HPP

View File

@ -0,0 +1,58 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_UINT32_HPP
#define LMDBAL_SERIALIZER_UINT32_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint32_t> {
public:
Serializer():value(0) {};
Serializer(const uint32_t& p_value):value(p_value) {};
~Serializer() {};
uint32_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, uint32_t& result) {
std::memcpy(&result, data.mv_data, 4);
}
MDB_val setData(const uint32_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 4;
return result;
};
void clear() {}; //not possible;
private:
uint32_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT32_HPP

View File

@ -0,0 +1,59 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_UINT64_HPP
#define LMDBAL_SERIALIZER_UINT64_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint64_t> {
public:
Serializer():value(0) {};
Serializer(const uint64_t& p_value):value(p_value) {};
~Serializer() {};
uint64_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, uint64_t& result) {
std::memcpy(&result, data.mv_data, 8);
}
MDB_val setData(const uint64_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 8;
return result;
};
void clear() {}; //not possible;
private:
uint64_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT64_HPP

View File

@ -0,0 +1,61 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_SERIALIZER_UINT8_HPP
#define LMDBAL_SERIALIZER_UINT8_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint8_t> {
public:
Serializer():value(0) {};
Serializer(const uint8_t& p_value):value(p_value) {};
~Serializer() {};
uint8_t deserialize(const MDB_val& data) {
deserialize(data, value);
return value;
};
void deserialize(const MDB_val& data, uint8_t& result) {
std::memcpy(&result, data.mv_data, 1);
}
MDB_val setData(const uint8_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 1;
return result;
};
void clear() {}; //not possible;
private:
uint8_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT8_HPP

509
src/storage.cpp Normal file
View File

@ -0,0 +1,509 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "storage.h"
#define UNUSED(x) (void)(x)
/**
* \class LMDBAL::iStorage
*
* \brief Storage interface
*
* This is a interface-like class, it's designed to be an inner database interface to
* be used as a polymorphic entity, and provide protected interaction with the database
* from the heirs code
*/
/**
* \brief Constructs a storage interface
*
* \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] name - the name of the storage
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*/
LMDBAL::iStorage::iStorage(Base* parent, const std::string& name, bool duplicates):
dbi(),
db(parent),
name(name),
duplicates(duplicates)
{}
/**
* \brief Destroys a storage interface
*/
LMDBAL::iStorage::~iStorage() {}
/**
* \brief A private virtual function I need to close each storage in the database
*/
void LMDBAL::iStorage::close() {
mdb_dbi_close(db->environment, dbi);
}
/**
* \brief Checks if the transaction is still active, returns inner TransactionID
*
* This method is for internal usage only
*
* \param[in] txn - a transaction, you want to extract ID from
* \param[in] action - a description of what you're going to do, in case of exception the error description will be verbose
* \returns - Transaction inner TransactionID
*
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
*/
LMDBAL::TransactionID LMDBAL::iStorage::extractTransactionId(const Transaction& txn, const std::string& action) const {
if (!txn.isActive())
throw TransactionTerminated(db->name, name, action);
return txn.txn;
}
/**
* \brief Drops content of a storage interface
*
* Designed to drop storage content
*
* \exception LMDBAL::Closed thrown if the database was closed
* \exception LMDBAL::Unknown thrown if something unexpected happened
*/
void LMDBAL::iStorage::drop() {
ensureOpened(dropMethodName);
TransactionID txn = beginTransaction();
int rc = iStorage::drop(txn);
if (rc != MDB_SUCCESS) {
abortTransaction(txn);
throw Unknown(db->name, mdb_strerror(rc), name);
}
db->commitTransaction(txn);
handleDrop();
}
/**
* \brief Drops content of a storage interface (transaction variant)
*
* Just performs content drop
*
* \param[in] transaction - transaction ID, must be writable transaction!
* \returns MDB_SUCCESS if everything went fine, MDB_<error> code otherwise
*/
int LMDBAL::iStorage::drop(TransactionID transaction) {
return mdb_drop(transaction, dbi, 0);
}
/**
* \brief Drops content of a storage interface (public transaction variant)
*
* Just performs content drop
*
* \param[in] txn - transaction ID, must be writable transaction!
* \returns MDB_SUCCESS if everything went fine, MDB_<error> code otherwise
*
* \exception LMDBAL::TransactionTerminated thrown if the transaction was not active
*/
int LMDBAL::iStorage::drop(const WriteTransaction& txn) {
ensureOpened(dropMethodName);
return drop(extractTransactionId(txn, dropMethodName));
}
/**
* \brief Helper function, thows exception if the database is not opened
*
* \param[in] methodName - name of the method this function is called from, just for display in std::exception::what() message
*
* \exception LMDBAL::Closed thrown if the database was closed
*/
void LMDBAL::iStorage::ensureOpened(const std::string& methodName) const {
if (!isDBOpened())
throw Closed(methodName, db->name, name);
}
/**
* \brief Storage size
*
* \returns amount of records in the storage
*
* \exception LMDBAL::Closed thrown if the database was closed
* \exception LMDBAL::Unknown thrown if something unexpected happened
*/
LMDBAL::SizeType LMDBAL::iStorage::count() const {
ensureOpened(countMethodName);
TransactionID txn = beginReadOnlyTransaction();
SizeType amount;
try {
amount = count(txn);
} catch (...) {
abortTransaction(txn);
throw;
}
abortTransaction(txn);
return amount;
}
/**
* \brief Storage size (private transaction variant)
*
* \param[in] txn - transaction ID, can be read-only transaction
* \returns amount of records in the storage
*
* \exception LMDBAL::Unknown thrown if something unexpected happened
*/
LMDBAL::SizeType LMDBAL::iStorage::count(TransactionID txn) const {
MDB_stat stat;
int rc = mdb_stat(txn, dbi, &stat);
if (rc != MDB_SUCCESS)
throw Unknown(db->name, mdb_strerror(rc), name);
return stat.ms_entries;
}
/**
* \brief Storage size (public transaction variant)
*
* \param[in] txn - transaction, can be read-only transaction
* \returns amount of records in the storage
*
* \exception LMDBAL::Closed thrown if the database was closed
* \exception LMDBAL::Unknown thrown if something unexpected happened
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
*/
LMDBAL::SizeType LMDBAL::iStorage::count(const Transaction& txn) const {
ensureOpened(countMethodName);
return count(extractTransactionId(txn, countMethodName));
}
/**
* \brief Throws LMDBAL::Exist or LMDBAL::Unknown (transaction vairiant)
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] rc - result of lmdb low level operation
* \param[in] txn - transaction ID to be aborted, any transaction
* \param[in] key - requested key string representation, just to show in std::exception::what() message
*
* \exception LMDBAL::Exist thrown if rc == MDB_KEYEXIST
* \exception LMDBAL::Unknown thrown if rc != MDB_KEYEXIST
*/
void LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
abortTransaction(txn);
throwDuplicateOrUnknown(rc, key);
}
/**
* \brief Throws LMDBAL::NotFound or LMDBAL::Unknown (transaction vairiant)
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] rc - result of lmdb low level operation
* \param[in] txn - transaction ID to be aborted, any transaction
* \param[in] key - requested key string representation, just to show in std::exception::what() message
*
* \exception LMDBAL::NotFound thrown if rc == MDB_NOTFOUND
* \exception LMDBAL::Unknown thrown if rc != MDB_NOTFOUND
*/
void LMDBAL::iStorage::throwNotFoundOrUnknown(int rc, LMDBAL::TransactionID txn, const std::string& key) const {
abortTransaction(txn);
throwNotFoundOrUnknown(rc, key);
}
/**
* \brief Throws LMDBAL::Exist or LMDBAL::Unknown
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] rc - result of lmdb low level operation
* \param[in] key - requested key string representation, just to show in std::exception::what() message
*
* \exception LMDBAL::Exist thrown if rc == MDB_KEYEXIST
* \exception LMDBAL::Unknown thrown if rc != MDB_KEYEXIST
*/
void LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, const std::string& key) const {
if (rc == MDB_KEYEXIST)
throwDuplicate(key);
else
throwUnknown(rc);
}
/**
* \brief Throws LMDBAL::NotFound or LMDBAL::Unknown (transaction variant)
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] rc - result of lmdb low level operation
* \param[in] key - requested key string representation, just to show in std::exception::what() message
*
* \exception LMDBAL::NotFound thrown if rc == MDB_NOTFOUND
* \exception LMDBAL::Unknown thrown if rc != MDB_NOTFOUND
*/
void LMDBAL::iStorage::throwNotFoundOrUnknown(int rc, const std::string& key) const {
if (rc == MDB_NOTFOUND)
throwNotFound(key);
else
throwUnknown(rc);
}
/**
* \brief Throws LMDBAL::Unknown (transaction vairiant)
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] rc - result of lmdb low level operation
* \param[in] txn - transaction ID to be aborted, any transaction
*
* \exception LMDBAL::Unknown thrown everytime
*/
void LMDBAL::iStorage::throwUnknown(int rc, LMDBAL::TransactionID txn) const {
abortTransaction(txn);
throwUnknown(rc);
}
/**
* \brief Database name
*
* Ment to be used in heirs, to provide some sort of interface to acces to some of the database information
*
* \returns database name
*/
const std::string & LMDBAL::iStorage::dbName() const {
return db->name;}
/**
* \brief Is database opened
*
* Ment to be used in heirs, to provide some sort of interface to acces to some of the database information
*
* \returns true if database is ipened, false otherwise
*/
bool LMDBAL::iStorage::isDBOpened() const {
return db->opened;}
/**
* \brief Throws LMDBAL::Unknown
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] rc - result of lmdb low level operation
*
* \exception LMDBAL::Unknown thrown everytime
*/
void LMDBAL::iStorage::throwUnknown(int rc) const {
throw Unknown(db->name, mdb_strerror(rc), name);}
/**
* \brief Throws LMDBAL::Unknown
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] message - a message you wish to appear in the exception reason
*
* \exception LMDBAL::Unknown thrown everytime
*/
void LMDBAL::iStorage::throwUnknown(const std::string& message) const {
throw Unknown(db->name, message, name);}
/**
* \brief Throws LMDBAL::Exist
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] key - requested key string representation, just to show in std::exception::what() message
*
* \exception LMDBAL::Exist thrown everytime
*/
void LMDBAL::iStorage::throwDuplicate(const std::string& key) const {
throw Exist(key, db->name, name);}
/**
* \brief Throws LMDBAL::NotFound
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] key - requested key string representation, just to show in std::exception::what() message
*
* \exception LMDBAL::NotFound thrown everytime
*/
void LMDBAL::iStorage::throwNotFound(const std::string& key) const {
throw NotFound(key, db->name, name);}
/**
* \brief Throws LMDBAL::CursorNotReady
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] method - called cursor method name, just to show in std::exception::what() message
*
* \exception LMDBAL::CursorNotReady thrown everytime
*/
void LMDBAL::iStorage::throwCursorNotReady(const std::string& method) const {
throw CursorNotReady(method, db->name, name);}
/**
* \brief Begins read-only transaction
*
* Ment to be called from heirs, name is reported to the database just to be displayed in std::exception::what() message
*
* \returns read only transaction
*/
LMDBAL::TransactionID LMDBAL::iStorage::beginReadOnlyTransaction() const {
return db->beginPrivateReadOnlyTransaction(name);}
/**
* \brief Begins writable transaction
*
* Ment to be called from heirs, name is reported to the database just to be displayed in std::exception::what() message
*
* \returns read only transaction
*/
LMDBAL::TransactionID LMDBAL::iStorage::beginTransaction() const {
return db->beginPrivateTransaction(name);}
/**
* \brief Aborts transaction
*
* Ment to be called from heirs, name is reported to the database just to be displayed in std::exception::what() message
*/
void LMDBAL::iStorage::abortTransaction(LMDBAL::TransactionID id) const {
db->abortPrivateTransaction(id, name);}
/**
* \brief Commits transaction
*
* Ment to be called from heirs, name is reported to the database just to be displayed in std::exception::what() message
*
* \exception LMDBAL::Unknown thrown if something unexpected happened
*/
void LMDBAL::iStorage::commitTransaction(LMDBAL::TransactionID id) {
db->commitPrivateTransaction(id, name);}
/**
* \brief called on beginning of public transaction
*
* This function is called on every storage of the database
* when user calls LMDBAL::Base::beginTransaction() or LMDBAL::Base::beginReadOnlyTransaction()
*
* This function is met to be reimplemented in heirs
* if the heir code requires some transaction custom handling
*
* \param[in] txn - ID of started transaction
* \param[in] readOnly - true if transaction is read-only, false otherwise
*/
void LMDBAL::iStorage::transactionStarted(LMDBAL::TransactionID txn, bool readOnly) const {
UNUSED(txn);
UNUSED(readOnly);
}
/**
* \brief called on commitment of public transaction
*
* This function is called on every storage of the database
* when user calls LMDBAL::Base::commitTransaction(LMDBAL::TransactionID)
*
* This function is met to be reimplemented in heirs
* if the heir code requires some transaction custom handling
*
* \param[in] txn - ID of started transaction
*/
void LMDBAL::iStorage::transactionCommited(LMDBAL::TransactionID txn) {
UNUSED(txn);}
/**
* \brief called on abortion of public transaction
*
* This function is called on every storage of the database
* when user calls LMDBAL::Base::abortTransaction(LMDBAL::TransactionID)
*
* This function is met to be reimplemented in heirs
* if the heir code requires some transaction custom handling
*
* \param[in] txn - ID of started transaction
*/
void LMDBAL::iStorage::transactionAborted(LMDBAL::TransactionID txn) const {
UNUSED(txn);}
/**
* \brief A method where database additionally handles drop
*
* It's a protected method that is called to optimise drop process
* after the transaction is commited. Used just for optimisations.
*/
void LMDBAL::iStorage::handleDrop() {}
int LMDBAL::iStorage::_mdbOpen(MDB_txn *txn, unsigned int flags) {
return mdb_dbi_open(txn, name.c_str(), flags, &dbi);
}
int LMDBAL::iStorage::_mdbPut(MDB_txn* txn, MDB_val& key, MDB_val& data, unsigned int flags) {
return mdb_put(txn, dbi, &key, &data, flags);
}
int LMDBAL::iStorage::_mdbGet(MDB_txn* txn, MDB_val& key, MDB_val& data) const {
return mdb_get(txn, dbi, &key, &data);
}
int LMDBAL::iStorage::_mdbDel(MDB_txn* txn, MDB_val& key) {
return mdb_del(txn, dbi, &key, NULL);
}
int LMDBAL::iStorage::_mdbDel(MDB_txn* txn, MDB_val& key, MDB_val& data) {
return mdb_del(txn, dbi, &key, &data);
}
int LMDBAL::iStorage::_mdbStat(MDB_txn* txn, MDB_stat& stat) const {
return mdb_stat(txn, dbi, &stat);
}
int LMDBAL::iStorage::_mdbFlags(MDB_txn* txn, uint32_t& flags) const {
return mdb_dbi_flags(txn, dbi, &flags);
}
int LMDBAL::iStorage::_mdbCursorOpen(MDB_txn* txn, MDB_cursor** cursor) const {
return mdb_cursor_open(txn, dbi, cursor);
}
void LMDBAL::iStorage::_mdbCursorClose(MDB_cursor *cursor) const {
mdb_cursor_close(cursor);
}
int LMDBAL::iStorage::_mdbCursorGet(MDB_cursor* cursor, MDB_val& key, MDB_val& data, MDB_cursor_op operation) const {
return mdb_cursor_get(cursor, &key, &data, operation);
}
int LMDBAL::iStorage::_mdbCursorSet(MDB_cursor* cursor, MDB_val& key) const {
return mdb_cursor_get(cursor, &key, NULL, MDB_SET);
}
int LMDBAL::iStorage::_mdbCursorDel(MDB_cursor* cursor, unsigned int flags) {
return mdb_cursor_del(cursor, flags);
}
int LMDBAL::iStorage::_mdbCursorPut(MDB_cursor* cursor, MDB_val& key, MDB_val& data, unsigned int flags) {
return mdb_cursor_put(cursor, &key, &data, flags);
}
int LMDBAL::iStorage::_mdbCursorRenew(MDB_txn* txn, MDB_cursor* cursor) const {
return mdb_cursor_renew(txn, cursor);
}
MDB_txn* LMDBAL::iStorage::_mdbCursorTxn(MDB_cursor* cursor) const {
return mdb_cursor_txn(cursor);
}

203
src/storage.h Normal file
View File

@ -0,0 +1,203 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_STORAGE_H
#define LMDBAL_STORAGE_H
#include <type_traits>
#include <cstring>
#include "base.h"
#include "serializer.h"
#include "cursor.h"
#include "transaction.h"
class BaseTest;
class DuplicatesTest;
class CacheCursorTest;
class StorageCursorTest;
namespace LMDBAL {
class iStorage {
friend class Base;
public:
protected:
iStorage(Base* parent, const std::string& name, bool duplicates = false);
virtual ~iStorage();
/**
* \brief A private virtual function I need to open each storage in the database
*
* \param[in] transaction - lmdb transaction to call <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a">mdb_dbi_open</a>
* \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code
*/
virtual int open(MDB_txn * transaction) = 0;
virtual void close();
virtual void handleDrop();
bool isDBOpened() const;
const std::string& dbName() const;
void ensureOpened(const std::string& methodName) const;
void throwDuplicateOrUnknown(int rc, const std::string& key) const;
void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const;
void throwNotFoundOrUnknown(int rc, const std::string& key) const;
void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const;
void throwUnknown(int rc, TransactionID txn) const;
void throwUnknown(int rc) const;
void throwUnknown(const std::string& message) const;
void throwDuplicate(const std::string& key) const;
void throwNotFound(const std::string& key) const;
void throwCursorNotReady(const std::string& method) const;
TransactionID extractTransactionId(const Transaction& txn, const std::string& action = "") const;
TransactionID beginReadOnlyTransaction() const;
TransactionID beginTransaction() const;
void commitTransaction(TransactionID id);
void abortTransaction(TransactionID id) const;
virtual void transactionStarted(TransactionID txn, bool readOnly) const;
virtual void transactionCommited(TransactionID txn);
virtual void transactionAborted(TransactionID txn) const;
virtual int drop(TransactionID transaction);
virtual SizeType count(TransactionID txn) const;
int _mdbOpen(MDB_txn* txn, unsigned int flags = 0);
int _mdbPut(MDB_txn* txn, MDB_val& key, MDB_val& data, unsigned int flags = 0);
int _mdbGet(MDB_txn* txn, MDB_val& key, MDB_val& data) const;
int _mdbDel(MDB_txn* txn, MDB_val& key);
int _mdbDel(MDB_txn* txn, MDB_val& key, MDB_val& data);
int _mdbStat(MDB_txn* txn, MDB_stat& stat) const;
int _mdbFlags(MDB_txn* txn, uint32_t& flags) const;
int _mdbCursorOpen(MDB_txn* txn, MDB_cursor** cursor) const;
void _mdbCursorClose(MDB_cursor* cursor) const;
int _mdbCursorGet(MDB_cursor* cursor, MDB_val& key, MDB_val& data, MDB_cursor_op operation) const;
int _mdbCursorSet(MDB_cursor* cursor, MDB_val& key) const;
int _mdbCursorDel(MDB_cursor* cursor, unsigned int flags = 0);
int _mdbCursorPut(MDB_cursor* cursor, MDB_val& key, MDB_val& data, unsigned int flags = 0);
int _mdbCursorRenew(MDB_txn* txn, MDB_cursor* cursor) const;
MDB_txn* _mdbCursorTxn(MDB_cursor* cursor) const;
public:
virtual void drop();
virtual int drop(const WriteTransaction& txn);
virtual SizeType count() const;
virtual SizeType count(const Transaction& txn) const;
protected:
MDB_dbi dbi; /**<\brief lmdb storage handle*/
Base* db; /**<\brief parent database pointer (borrowed)*/
const std::string name; /**<\brief this storage name*/
const bool duplicates; /**<\brief true if storage supports duplicates*/
inline static const std::string dropMethodName = "drop"; /**<\brief member function name, just for exceptions*/
inline static const std::string countMethodName = "count"; /**<\brief member function name, just for exceptions*/
inline static const std::string flagsMethodName = "flags"; /**<\brief member function name, just for exceptions*/
inline static const std::string addRecordMethodName = "addRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string forceRecordMethodName = "forceRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string changeRecordMethodName = "changeRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string removeRecordMethodName = "removeRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string checkRecordMethodName = "checkRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string getRecordMethodName = "getRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string readAllMethodName = "readAllRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string replaceAllMethodName = "replaceAll"; /**<\brief member function name, just for exceptions*/
inline static const std::string addRecordsMethodName = "addRecords"; /**<\brief member function name, just for exceptions*/
protected:
template <class K, class V>
int makeStorage(MDB_txn* transaction, bool duplicates = false);
template <class T>
static std::string toString(const T& value);
};
template <class K, class V>
class Storage : public iStorage {
friend class ::BaseTest;
friend class ::DuplicatesTest;
friend class ::CacheCursorTest;
friend class ::StorageCursorTest;
friend class Base;
friend class Cursor<K, V>;
protected:
Storage(Base* parent, const std::string& name, bool duplicates = false);
~Storage() override;
virtual void discoveredRecord(const K& key, const V& value) const;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const;
uint32_t flags() const;
virtual void addRecord(const K& key, const V& value, TransactionID txn);
virtual bool forceRecord(const K& key, const V& value, TransactionID txn);
virtual void changeRecord(const K& key, const V& value, TransactionID txn);
virtual void removeRecord(const K& key, TransactionID txn);
virtual bool checkRecord(const K& key, TransactionID txn) const;
virtual void getRecord(const K& key, V& value, TransactionID txn) const;
virtual V getRecord(const K& key, TransactionID txn) const;
virtual std::map<K, V> readAll(TransactionID txn) const;
virtual void readAll(std::map<K, V>& result, TransactionID txn) const;
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn);
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
public:
using iStorage::drop;
virtual void addRecord(const K& key, const V& value);
virtual void addRecord(const K& key, const V& value, const WriteTransaction& txn);
virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change
virtual bool forceRecord(const K& key, const V& value, const WriteTransaction& txn);
virtual void changeRecord(const K& key, const V& value);
virtual void changeRecord(const K& key, const V& value, const WriteTransaction& txn);
virtual void removeRecord(const K& key);
virtual void removeRecord(const K& key, const WriteTransaction& txn);
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
virtual bool checkRecord(const K& key, const Transaction& txn) const;
virtual void getRecord(const K& key, V& value) const;
virtual void getRecord(const K& key, V& value, const Transaction& txn) const;
virtual V getRecord(const K& key) const;
virtual V getRecord(const K& key, const Transaction& txn) const;
virtual std::map<K, V> readAll() const;
virtual std::map<K, V> readAll(const Transaction& txn) const;
virtual void readAll(std::map<K, V>& result) const;
virtual void readAll(std::map<K, V>& result, const Transaction& txn) const;
virtual void replaceAll(const std::map<K, V>& data);
virtual void replaceAll(const std::map<K, V>& data, const WriteTransaction& txn);
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
virtual uint32_t addRecords(const std::map<K, V>& data, const WriteTransaction& txn, bool overwrite = false);
Cursor<K, V> createCursor();
void destroyCursor(Cursor<K, V>& cursor);
protected:
mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/
mutable Serializer<V> valueSerializer; /**<\brief internal object that would serialize and deserialize values*/
std::map<uint32_t, Cursor<K, V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
int open(MDB_txn* transaction) override;
void close() override;
};
}
#include "storage.hpp"
#endif //LMDBAL_STORAGE_H

1186
src/storage.hpp Normal file

File diff suppressed because it is too large Load Diff

154
src/transaction.cpp Normal file
View File

@ -0,0 +1,154 @@
#include "transaction.h"
/**
* \class LMDBAL::Transaction
* \brief Public read only transaction
*
* This class provides read only transactions you can use
* to speed to your queries keeping them thread safe.
* LMDBAL::Transaction is <b>NOT COPYABLE</b> but <b>MOVABLE</b>.
* Transaction can be in one of two states: active or terminated.
* The way to receive an active LMDBAL::Transaction is to call LMDBAL::Base::beginReadOnlyTransaction.
*
* Active transactions become terminated upon the call of LMDBAL::Transaction::terminate.
* Active transactions automaticaly terminate themselves upon destruction.
*
* You <b>CAN NOT</b> use inactive transactions for any query.
*/
/**
* \brief Constructs inactive transaction
*/
LMDBAL::Transaction::Transaction():
txn(nullptr),
active(false),
parent(nullptr)
{}
/**
* \brief Constructs an active transaction
*/
LMDBAL::Transaction::Transaction(TransactionID txn, const Base* parent) :
txn(txn),
active(true),
parent(parent)
{}
/**
* \brief Moves transaction to a new object
*/
LMDBAL::Transaction::Transaction(Transaction&& other):
txn(other.txn),
active(other.active),
parent(other.parent)
{
other.active = false;
}
/**
* \brief Destroys transaction
*/
LMDBAL::Transaction::~Transaction() {
terminate();
}
/**
* \brief Move-assigns transaction to the new object
*/
LMDBAL::Transaction& LMDBAL::Transaction::operator=(Transaction&& other) {
terminate();
txn = other.txn;
active = other.active;
parent = other.parent;
other.active = false;
return *this;
}
/**
* \brief Terminates transaction if it was active
*
* Transaction becomes terminated after calling this method
*/
void LMDBAL::Transaction::terminate() {
if (active) {
parent->abortTransaction(txn);
active = false;
}
}
/**
* \brief Returns transaction states
*
* \returns true if the transaction is active, false otherwise
*/
bool LMDBAL::Transaction::isActive() const {
return active; //todo may be it's better if I query it from DB?
}
/**
* \class LMDBAL::WriteTransaction
* \brief Public writable transaction
*
* This class provides writable transactions you can use
* to speed to your queries and modifications keeping them thread safe.
* LMDBAL::WriteTransaction is <b>NOT COPYABLE</b> but <b>MOVABLE</b>.
* Transaction can be in one of two states: active or terminated.
* The way to receive an active LMDBAL::WriteTransaction is to call LMDBAL::Base::beginTransaction.
* You can use LMDBAL::WriteTransaction for everything instead of LMDBAL::Transaction
*
* Active transactions become terminated upon the call of
* LMDBAL::WriteTransaction::abort or LMDBAL::WriteTransaction::commit.
* Calling LMDBAL::Transaction::terminate on LMDBAL::WriteTransaction
* is exactly the same as calling LMDBAL::WriteTransaction::abort.
*
* Active transactions automaticaly terminate themselves upon destruction.
* <b>For LMDBAL::WriteTransaction default behaviour upon destruction is to abort.</b>
*
* You <b>CAN NOT</b> use inactive transactions for any query.
*/
/**
* \brief Constructs active write transaction
*/
LMDBAL::WriteTransaction::WriteTransaction(TransactionID txn, Base* parent):
Transaction(txn, parent)
{}
/**
* \brief Constructs inactive write transaction
*/
LMDBAL::WriteTransaction::WriteTransaction():
Transaction()
{}
/**
* \brief Moves transaction to the new object
*/
LMDBAL::WriteTransaction::WriteTransaction(WriteTransaction&& other):
Transaction(std::move(other))
{}
/**
* \brief Aborts transaction cancelling all changes
*
* Transaction becomes terminated after calling this method
*/
void LMDBAL::WriteTransaction::abort() {
terminate();
}
/**
* \brief Commits transaction submitting all changes
*
* Transaction becomes terminated after calling this method
*/
void LMDBAL::WriteTransaction::commit() {
if (active) {
const_cast<Base*>(parent)->commitTransaction(txn);
active = false;
}
}

46
src/transaction.h Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include "base.h"
namespace LMDBAL {
class iStorage;
class Transaction {
friend class Base;
friend class iStorage;
public:
explicit Transaction();
explicit Transaction(Transaction&& other);
Transaction(const Transaction& other) = delete;
Transaction& operator = (const Transaction& other) = delete;
Transaction& operator = (Transaction&& other);
virtual ~Transaction();
void terminate();
bool isActive() const;
protected:
Transaction(TransactionID txn, const Base* parent);
protected:
TransactionID txn; /**<\brief Transaction inner handler*/
bool active; /**<\brief Transaction state*/
const Base* parent; /**<\brief Pointer to the database this transaction belongs to*/
};
class WriteTransaction : public Transaction {
friend class Base;
public:
explicit WriteTransaction();
explicit WriteTransaction(WriteTransaction&& other);
WriteTransaction(const WriteTransaction& other) = delete;
WriteTransaction& operator = (const WriteTransaction& other) = delete;
void commit();
void abort();
protected:
WriteTransaction(TransactionID txn, Base* parent);
};
}

27
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,27 @@
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
add_executable(runUnitTests
basic.cpp
serialization.cpp
storagetransaction.cpp
cachetransaction.cpp
storagecursor.cpp
cachecursor.cpp
duplicates.cpp
)
target_compile_options(runUnitTests PRIVATE -fPIC -Wall -Wextra -O0)
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
target_link_libraries(
runUnitTests
GTest::gtest_main
${PROJECT_NAME}
Qt${QT_VERSION_MAJOR}::Core
)
include(GoogleTest)
gtest_discover_tests(runUnitTests)

576
test/basic.cpp Normal file
View File

@ -0,0 +1,576 @@
#include <gtest/gtest.h>
#include "base.h"
#include "storage.h"
#include "cache.h"
#include <QString>
#include <QVariant>
class BaseTest : public ::testing::Test {
protected:
BaseTest():
::testing::Test(),
t1(db->getStorage<uint32_t, uint32_t>("table1")),
t2(db->getStorage<QString, QString>("table2")),
c1(db->getCache<int8_t, std::string>("cache1")),
c2(db->getCache<std::string, QVariant>("cache2")) {}
~BaseTest() {}
uint32_t getT1Flags() const {return t1->flags();}
uint32_t getT2Flags() const {return t2->flags();}
uint32_t getC1Flags() const {return c1->flags();}
uint32_t getC2Flags() const {return c2->flags();}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("testBase");
db->addStorage<uint32_t, uint32_t>("table1");
db->addStorage<QString, QString>("table2");
db->addCache<int8_t, std::string>("cache1");
db->addCache<std::string, QVariant>("cache2");
}
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
LMDBAL::Storage<uint32_t, uint32_t>* t1;
LMDBAL::Storage<QString, QString>* t2;
LMDBAL::Cache<int8_t, std::string>* c1;
LMDBAL::Cache<std::string, QVariant>* c2;
};
LMDBAL::Base* BaseTest::db = nullptr;
TEST_F(BaseTest, RemovingDirectory) {
EXPECT_EQ(db->removeDirectory(), true);
}
TEST_F(BaseTest, OpeningClosingDatabase) {
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
db->close();
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
}
TEST_F(BaseTest, Flags) {
uint32_t t1Flags = getT1Flags();
uint32_t t2Flags = getT2Flags();
uint32_t c1Flags = getC1Flags();
uint32_t c2Flags = getC2Flags();
EXPECT_TRUE(t1Flags & MDB_INTEGERKEY);
EXPECT_FALSE(t1Flags & MDB_DUPSORT);
EXPECT_FALSE(t1Flags & MDB_DUPFIXED);
EXPECT_FALSE(t1Flags & MDB_INTEGERDUP);
EXPECT_FALSE(t2Flags & MDB_INTEGERKEY);
EXPECT_FALSE(t2Flags & MDB_DUPSORT);
EXPECT_FALSE(t2Flags & MDB_DUPFIXED);
EXPECT_FALSE(t2Flags & MDB_INTEGERDUP);
EXPECT_TRUE(c1Flags & MDB_INTEGERKEY);
EXPECT_FALSE(c1Flags & MDB_DUPSORT);
EXPECT_FALSE(c1Flags & MDB_DUPFIXED);
EXPECT_FALSE(c1Flags & MDB_INTEGERDUP);
EXPECT_FALSE(c2Flags & MDB_INTEGERKEY);
EXPECT_FALSE(c2Flags & MDB_DUPSORT);
EXPECT_FALSE(c2Flags & MDB_DUPFIXED);
EXPECT_FALSE(c2Flags & MDB_INTEGERDUP);
}
TEST_F(BaseTest, AddingIntegerKey) {
EXPECT_EQ(db->ready(), true);
t1->addRecord(1, 2);
t1->addRecord(2, 2);
t1->addRecord(3, 15);
EXPECT_EQ(t1->getRecord(1), 2);
}
TEST_F(BaseTest, AddingQStringKey) {
EXPECT_EQ(db->ready(), true);
t2->addRecord("hello", "world");
t2->addRecord("aaa", "gagdfsdf");
t2->addRecord("sdfhga", "DSFFDG");
t2->addRecord("sdfsda", "shgsdgfa");
EXPECT_EQ(t2->getRecord("hello"), "world");
}
TEST_F(BaseTest, AddingKeysToCache) {
EXPECT_EQ(db->ready(), true);
c1->addRecord(2, "blah balah");
c1->addRecord(-4, "testing goes brrr");
c1->addRecord(40, "whatever");
c1->addRecord(-37, "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(40), "whatever");
}
TEST_F(BaseTest, AddingKeysToVariableCache) {
EXPECT_EQ(db->ready(), true);
c2->addRecord("regrets", "blah balah");
c2->addRecord("fossil fingers", 842);
c2->addRecord("preloaded cut", 539.75);
c2->addRecord("dihotomy", false);
EXPECT_EQ(c2->getRecord("regrets"), "blah balah");
EXPECT_EQ(c2->getRecord("fossil fingers"), 842);
EXPECT_EQ(c2->getRecord("preloaded cut"), 539.75);
EXPECT_EQ(c2->getRecord("dihotomy"), false);
}
TEST_F(BaseTest, AddingRepeatingKey) {
EXPECT_EQ(db->ready(), true);
EXPECT_THROW(t1->addRecord(3, 24), LMDBAL::Exist);
EXPECT_EQ(t1->getRecord(3), 15);
EXPECT_THROW(t2->addRecord("sdfhga", "world"), LMDBAL::Exist);
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
EXPECT_THROW(c1->addRecord(-4, "world"), LMDBAL::Exist);
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_THROW(c2->addRecord("dihotomy", "pired"), LMDBAL::Exist);
EXPECT_EQ(c2->getRecord("dihotomy"), false);
}
TEST_F(BaseTest, GettingNotExistingKeys) {
EXPECT_EQ(db->ready(), true);
EXPECT_THROW(t2->getRecord("almonds"), LMDBAL::NotFound);
EXPECT_THROW(t1->getRecord(64), LMDBAL::NotFound);
EXPECT_THROW(c1->getRecord(21), LMDBAL::NotFound);
EXPECT_THROW(c2->getRecord("praising"), LMDBAL::NotFound);
}
TEST_F(BaseTest, Persistence) {
EXPECT_EQ(db->ready(), true);
uint32_t t1Size = t1->count();
uint32_t t2Size = t2->count();
uint32_t c1Size = c1->count();
uint32_t c2Size = c2->count();
db->close();
delete db;
db = new LMDBAL::Base("testBase");
t1 = db->addStorage<uint32_t, uint32_t>("table1");
t2 = db->addStorage<QString, QString>("table2");
c1 = db->addCache<int8_t, std::string>("cache1");
c2 = db->addCache<std::string, QVariant>("cache2");
db->open();
EXPECT_EQ(t1->count(), t1Size);
EXPECT_EQ(t1->getRecord(3), 15);
EXPECT_EQ(t1->getRecord(1), 2);
EXPECT_EQ(t1->getRecord(2), 2);
EXPECT_EQ(t1->count(), t1Size);
EXPECT_EQ(t2->count(), t2Size);
EXPECT_EQ(t2->getRecord("hello"), "world");
EXPECT_EQ(t2->getRecord("aaa"), "gagdfsdf");
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
EXPECT_EQ(t2->getRecord("sdfsda"), "shgsdgfa");
EXPECT_EQ(t2->count(), t2Size);
EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c1->checkRecord(40), true);
EXPECT_EQ(c1->getRecord(40), "whatever");
EXPECT_EQ(c1->checkRecord(-4), true);
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
EXPECT_EQ(c1->checkRecord(-4), true);
EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c1->getRecord(-37), "aaaaa tss tsss tsss tsss aaaaaaa");
EXPECT_EQ(c1->getRecord(2), "blah balah");
EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c2->count(), c2Size);
EXPECT_EQ(c2->getRecord("regrets"), "blah balah");
EXPECT_EQ(c2->getRecord("fossil fingers"), 842);
EXPECT_EQ(c2->getRecord("preloaded cut"), 539.75);
EXPECT_EQ(c2->getRecord("dihotomy"), false);
EXPECT_THROW(t2->getRecord("cats"), LMDBAL::NotFound);
EXPECT_THROW(t1->getRecord(7893), LMDBAL::NotFound);
EXPECT_THROW(c1->getRecord(89), LMDBAL::NotFound);
EXPECT_THROW(c2->getRecord("marathons"), LMDBAL::NotFound);
}
TEST_F(BaseTest, CountAndDrop) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 4);
EXPECT_EQ(c1->count(), 4);
EXPECT_EQ(c2->count(), 4);
db->drop();
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 0);
t1->addRecord(2, 2);
t2->addRecord("sdfhga", "world");
c1->addRecord(15, "world");
c1->addRecord(12, "grr grr");
c2->addRecord("blues", -749);
EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1);
EXPECT_EQ(c1->count(), 2);
EXPECT_EQ(c2->count(), 1);
}
TEST_F(BaseTest, Change) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1);
EXPECT_EQ(c1->count(), 2);
EXPECT_EQ(c2->count(), 1);
EXPECT_EQ(t1->getRecord(2), 2);
EXPECT_EQ(t2->getRecord("sdfhga"), "world");
EXPECT_EQ(c1->getRecord(15), "world");
EXPECT_EQ(c1->getRecord(12), "grr grr");
EXPECT_EQ(c2->getRecord("blues"), -749);
t1->addRecord(58, 39);
t2->addRecord("lawfirm", "stumble");
c1->addRecord(89, "answer");
c2->addRecord("wielders", QStringList({"calcium", "eagles"}));
t1->changeRecord(2, 49);
t2->changeRecord("sdfhga", "void");
c1->changeRecord(15, "recording");
c1->changeRecord(12, "thermal");
c2->changeRecord("blues", true);
EXPECT_THROW(t1->changeRecord(37, 49), LMDBAL::NotFound);
EXPECT_THROW(t2->changeRecord("precision", "cryoplastics"), LMDBAL::NotFound);
EXPECT_THROW(c1->changeRecord(-62, "sharks"), LMDBAL::NotFound);
EXPECT_THROW(c2->changeRecord("visions", 90), LMDBAL::NotFound);
EXPECT_EQ(t1->getRecord(2), 49);
EXPECT_EQ(t2->getRecord("sdfhga"), "void");
EXPECT_EQ(c1->getRecord(15), "recording");
EXPECT_EQ(c1->getRecord(12), "thermal");
EXPECT_EQ(c2->getRecord("blues"), true);
EXPECT_EQ(t1->getRecord(58), 39);
EXPECT_EQ(t2->getRecord("lawfirm"), "stumble");
EXPECT_EQ(c1->getRecord(89), "answer");
EXPECT_EQ(c2->getRecord("wielders"), QStringList({"calcium", "eagles"}));
EXPECT_EQ(t1->count(), 2);
EXPECT_EQ(t2->count(), 2);
EXPECT_EQ(c1->count(), 3);
EXPECT_EQ(c2->count(), 2);
}
TEST_F(BaseTest, Force) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->forceRecord(58, 35), false); //changing
EXPECT_EQ(t1->forceRecord(68, 36), true); //adding
EXPECT_EQ(t2->forceRecord("prophecy", "dumpling"), true); //adding
EXPECT_EQ(t2->forceRecord("lawfirm", "paracetamol"), false); //changing
EXPECT_EQ(c1->forceRecord(89, "canine"), false); //changing
EXPECT_EQ(c1->forceRecord(98, "duration"), true); //adding
EXPECT_EQ(c2->forceRecord("wielders", "charm"), false); //changing
EXPECT_EQ(c2->forceRecord("tedious", 5732.89), true); //adding
EXPECT_EQ(t1->getRecord(2), 49);
EXPECT_EQ(t1->getRecord(58), 35);
EXPECT_EQ(t1->getRecord(68), 36);
EXPECT_EQ(t2->getRecord("sdfhga"), "void");
EXPECT_EQ(t2->getRecord("prophecy"), "dumpling");
EXPECT_EQ(t2->getRecord("lawfirm"), "paracetamol");
EXPECT_EQ(c1->getRecord(15), "recording");
EXPECT_EQ(c1->getRecord(12), "thermal");
EXPECT_EQ(c1->getRecord(89), "canine");
EXPECT_EQ(c1->getRecord(98), "duration");
EXPECT_EQ(c2->getRecord("blues"), true);
EXPECT_EQ(c2->getRecord("tedious"), 5732.89);
EXPECT_EQ(c2->getRecord("wielders"), "charm");
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 3);
EXPECT_EQ(c1->count(), 4);
EXPECT_EQ(c2->count(), 3);
}
TEST_F(BaseTest, ReadAll) {
EXPECT_EQ(db->ready(), true);
std::map<uint32_t, uint32_t> m1 = t1->readAll();
std::map<QString, QString> m2 = t2->readAll();
std::map<int8_t, std::string> m3 = c1->readAll();
std::map<std::string, QVariant> m4 = c2->readAll();
EXPECT_EQ(m1.at(2), 49);
EXPECT_EQ(m1.at(58), 35);
EXPECT_EQ(m1.at(68), 36);
EXPECT_EQ(m2.at("sdfhga"), "void");
EXPECT_EQ(m2.at("prophecy"), "dumpling");
EXPECT_EQ(m2.at("lawfirm"), "paracetamol");
EXPECT_EQ(m3.at(15), "recording");
EXPECT_EQ(m3.at(12), "thermal");
EXPECT_EQ(m3.at(89), "canine");
EXPECT_EQ(m3.at(98), "duration");
EXPECT_EQ(m4.at("blues"), true);
EXPECT_EQ(m4.at("tedious"), 5732.89);
EXPECT_EQ(m4.at("wielders"), "charm");
EXPECT_EQ(m1.size(), 3);
EXPECT_EQ(m2.size(), 3);
EXPECT_EQ(m3.size(), 4);
EXPECT_EQ(m4.size(), 3);
}
TEST_F(BaseTest, ReplaceAll) {
EXPECT_EQ(db->ready(), true);
t1->replaceAll({
{7, 48},
{194, 582},
{857, 39},
{9717, 8}
});
t2->replaceAll({
{"bringin", "keyboard"},
{"cluster", "throttle"},
{"ronin", "cheese"}
});
c1->replaceAll({});
c2->replaceAll({
{"kind", 73}
});
EXPECT_EQ(t1->count(), 4);
EXPECT_EQ(t2->count(), 3);
EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 1);
EXPECT_FALSE(t1->checkRecord(2));
EXPECT_FALSE(t1->checkRecord(58));
EXPECT_FALSE(t1->checkRecord(68));
EXPECT_FALSE(t2->checkRecord("sdfhga"));
EXPECT_FALSE(t2->checkRecord("prophecy"));
EXPECT_FALSE(t2->checkRecord("lawfirm"));
EXPECT_FALSE(c1->checkRecord(15));
EXPECT_FALSE(c1->checkRecord(12));
EXPECT_FALSE(c1->checkRecord(89));
EXPECT_FALSE(c1->checkRecord(98));
EXPECT_FALSE(c2->checkRecord("blues"));
EXPECT_FALSE(c2->checkRecord("tedious"));
EXPECT_FALSE(c2->checkRecord("wielders"));
EXPECT_EQ(t1->getRecord(7), 48);
EXPECT_EQ(t1->getRecord(194), 582);
EXPECT_EQ(t1->getRecord(857), 39);
EXPECT_EQ(t1->getRecord(9717), 8);
EXPECT_EQ(t2->getRecord("bringin"), "keyboard");
EXPECT_EQ(t2->getRecord("cluster"), "throttle");
EXPECT_EQ(t2->getRecord("ronin"), "cheese");
EXPECT_EQ(c2->getRecord("kind"), 73);
c1->replaceAll({
{68, "quality"},
{31, "ridgid body"},
{16, "fermentation on your kind"},
{22, "pseudo"},
{-117, "lance of Michael"},
});
EXPECT_EQ(c1->count(), 5);
EXPECT_EQ(c1->getRecord(68), "quality");
EXPECT_EQ(c1->getRecord(31), "ridgid body");
EXPECT_EQ(c1->getRecord(16), "fermentation on your kind");
EXPECT_EQ(c1->getRecord(22), "pseudo");
EXPECT_EQ(c1->getRecord(-117), "lance of Michael");
}
TEST_F(BaseTest, AddRecords) {
EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType s1 = t1->addRecords({
{5, 3},
{800, 9}
});
EXPECT_EQ(s1, 6);
EXPECT_EQ(t1->getRecord(7), 48);
EXPECT_EQ(t1->getRecord(194), 582);
EXPECT_EQ(t1->getRecord(857), 39);
EXPECT_EQ(t1->getRecord(9717), 8);
EXPECT_EQ(t1->getRecord(5), 3);
EXPECT_EQ(t1->getRecord(800), 9);
s1 = t1->addRecords({
{194, 371},
{808, 487},
{807, 0}
}, true);
EXPECT_EQ(s1, 8);
EXPECT_EQ(t1->count(), 8);
EXPECT_EQ(t1->getRecord(7), 48);
EXPECT_EQ(t1->getRecord(194), 371);
EXPECT_EQ(t1->getRecord(857), 39);
EXPECT_EQ(t1->getRecord(9717), 8);
EXPECT_EQ(t1->getRecord(5), 3);
EXPECT_EQ(t1->getRecord(800), 9);
EXPECT_EQ(t1->getRecord(808), 487);
EXPECT_EQ(t1->getRecord(807), 0);
EXPECT_THROW(
s1 = t1->addRecords({
{194, 371},
{808, 487},
{807, 0}
}), LMDBAL::Exist
);
EXPECT_EQ(t1->count(), 8);
LMDBAL::SizeType s2 = t2->addRecords({
{"lama", "not quite"},
{"by the shadow", "leech"},
{"summertime", "curses"}
});
EXPECT_EQ(s2, 6);
EXPECT_EQ(t2->count(), 6);
EXPECT_EQ(t2->getRecord("bringin"), "keyboard");
EXPECT_EQ(t2->getRecord("cluster"), "throttle");
EXPECT_EQ(t2->getRecord("ronin"), "cheese");
EXPECT_EQ(t2->getRecord("lama"), "not quite");
EXPECT_EQ(t2->getRecord("by the shadow"), "leech");
EXPECT_EQ(t2->getRecord("summertime"), "curses");
s2 = t2->addRecords({
{"worry not", "for shall you"},
{"by the shadow", "face the inevitable"},
{"cluster", "sobing over those"}
}, true);
EXPECT_EQ(s2, 7);
EXPECT_EQ(t2->count(), 7);
EXPECT_EQ(t2->getRecord("bringin"), "keyboard");
EXPECT_EQ(t2->getRecord("cluster"), "sobing over those");
EXPECT_EQ(t2->getRecord("ronin"), "cheese");
EXPECT_EQ(t2->getRecord("lama"), "not quite");
EXPECT_EQ(t2->getRecord("by the shadow"), "face the inevitable");
EXPECT_EQ(t2->getRecord("summertime"), "curses");
EXPECT_EQ(t2->getRecord("worry not"), "for shall you");
EXPECT_THROW(
s2 = t2->addRecords({
{"within reasonable limits", "occasion"},
{"ronin", "crest of violence"},
{"permanent", "of your kind"}
}), LMDBAL::Exist
);
EXPECT_EQ(t2->count(), 7);
LMDBAL::SizeType s3 = c1->addRecords({
{19, "menace"},
{-7, "failure driven sorrow"},
{82, "lungache"},
{4, "drowsy"},
{44, "pressure"},
});
EXPECT_EQ(c1->count(), 10);
EXPECT_EQ(s3, 10);
EXPECT_EQ(c1->getRecord(68), "quality");
EXPECT_EQ(c1->getRecord(31), "ridgid body");
EXPECT_EQ(c1->getRecord(16), "fermentation on your kind");
EXPECT_EQ(c1->getRecord(22), "pseudo");
EXPECT_EQ(c1->getRecord(-117), "lance of Michael");
EXPECT_EQ(c1->getRecord(19), "menace");
EXPECT_EQ(c1->getRecord(-7), "failure driven sorrow");
EXPECT_EQ(c1->getRecord(82), "lungache");
EXPECT_EQ(c1->getRecord(4), "drowsy");
EXPECT_EQ(c1->getRecord(44), "pressure");
EXPECT_THROW(
s3 = c1->addRecords({
{-72, "amber"},
{-9, "going swinging of paleopathy"},
{82, "regret"}
}), LMDBAL::Exist
);
EXPECT_EQ(c1->count(), 10);
s3 = c1->addRecords({
{19, "to replicated being"},
{123, "horibly unforseen"},
{-32, "stitched"},
{31, "overall"}
}, true);
EXPECT_EQ(c1->count(), 12);
EXPECT_EQ(s3, 12);
EXPECT_EQ(c1->getRecord(68), "quality");
EXPECT_EQ(c1->getRecord(31), "overall");
EXPECT_EQ(c1->getRecord(16), "fermentation on your kind");
EXPECT_EQ(c1->getRecord(22), "pseudo");
EXPECT_EQ(c1->getRecord(-117), "lance of Michael");
EXPECT_EQ(c1->getRecord(19), "to replicated being");
EXPECT_EQ(c1->getRecord(-7), "failure driven sorrow");
EXPECT_EQ(c1->getRecord(82), "lungache");
EXPECT_EQ(c1->getRecord(4), "drowsy");
EXPECT_EQ(c1->getRecord(44), "pressure");
EXPECT_EQ(c1->getRecord(-32), "stitched");
EXPECT_EQ(c1->getRecord(123), "horibly unforseen");
LMDBAL::SizeType s4 = c2->addRecords({
{"and wholesome", "preaching"},
{"ginger", false}
});
EXPECT_EQ(c2->count(), 3);
EXPECT_EQ(s4, 3);
EXPECT_THROW(
s4 = c2->addRecords({
{"returning favor", 'c'},
{"ginger", 23},
{"frames", true}
}), LMDBAL::Exist
);
EXPECT_EQ(c2->count(), 3);
s4 = c2->addRecords({
{"and wholesome", -1},
{"orcid", 89.9}
}, true);
EXPECT_EQ(c2->count(), 4);
EXPECT_EQ(s4, 4);
EXPECT_EQ(c2->getRecord("and wholesome"), -1);
EXPECT_EQ(c2->getRecord("orcid"), 89.9);
EXPECT_EQ(c2->getRecord("ginger"), false);
EXPECT_EQ(c2->getRecord("kind"), 73);
}

440
test/cachecursor.cpp Normal file
View File

@ -0,0 +1,440 @@
#include <gtest/gtest.h>
#include "base.h"
#include "storage.h"
#include "cache.h"
#include "cursor.h"
class CacheCursorTest : public ::testing::Test {
protected:
CacheCursorTest():
::testing::Test(),
cache (db->getCache<uint64_t, std::string>("table1")),
emptyCache (db->getCache<uint64_t, std::string>("empty")) {}
~CacheCursorTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("testBase");
db->addCache<uint64_t, std::string>("table1");
db->addCache<uint64_t, std::string>("empty");
db->open();
}
}
int getCacheCursorsSize() const {
return cache->cursors.size();
}
static void TearDownTestSuite() {
cursor.drop();
transaction.terminate();
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string> cursor;
static LMDBAL::Transaction transaction;
LMDBAL::Cache<uint64_t, std::string>* cache;
LMDBAL::Cache<uint64_t, std::string>* emptyCache;
};
LMDBAL::Base* CacheCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string> CacheCursorTest::cursor;
LMDBAL::Transaction CacheCursorTest::transaction;
static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"},
{3458, "resilent pick forefront"},
{105190, "apportunity legal bat"},
{6510, "outside"},
{7438537, "damocles plush apparently rusty"},
{19373572, "local guidence"},
{138842, "forgetting tusks prepare"},
{981874, "butchered soaking pawn"},
{19302, "tanned inmate"},
{178239, "custody speaks neurotic"},
});
TEST_F(CacheCursorTest, PopulatingTheTable) {
uint32_t amount = cache->addRecords(data);
EXPECT_EQ(amount, data.size());
}
TEST_F(CacheCursorTest, Creation) {
EXPECT_EQ(getCacheCursorsSize(), 0);
cursor = cache->createCursor();
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
cursor.open();
}
TEST_F(CacheCursorTest, FirstPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, NextPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++;
for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, LastPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor.last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, PrevPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++;
for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, CurrentPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor.current();
EXPECT_EQ(cache->count(), data.size());
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
element = cursor.current();
++reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
cursor.next();
cursor.prev();
element = cursor.current();
++reference;
++reference;
--reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, SettingPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_FALSE(cursor.set(6684));
EXPECT_EQ(cursor.current().second, "tanned inmate");
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
std::advance(reference, 5);
EXPECT_TRUE(cursor.set(reference->first));
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
}
TEST_F(CacheCursorTest, Destruction) {
EXPECT_EQ(getCacheCursorsSize(), 1);
cursor.close();
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
cursor = LMDBAL::Cursor<uint64_t, std::string>();
EXPECT_EQ(getCacheCursorsSize(), 0);
cursor = cache->createCursor();
EXPECT_EQ(getCacheCursorsSize(), 1);
}
TEST_F(CacheCursorTest, FirstPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
transaction = db->beginTransaction();
cursor.open(transaction);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, NextPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++;
for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(CacheCursorTest, LastPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, PrevPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++;
for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, CurrentPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor.current();
EXPECT_EQ(cache->count(), data.size());
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
element = cursor.current();
++reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
cursor.next();
cursor.prev();
element = cursor.current();
++reference;
++reference;
--reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size());
}
TEST_F(CacheCursorTest, SettingPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_FALSE(cursor.set(557));
EXPECT_EQ(cursor.current().second, "resilent pick forefront");
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
std::advance(reference, 3);
EXPECT_TRUE(cursor.set(reference->first));
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
}
TEST_F(CacheCursorTest, CursorRAIIBehaviour) {
int initialiCursorsAmount = getCacheCursorsSize();
{
LMDBAL::Cursor<uint64_t, std::string> cur = cache->createCursor();
EXPECT_EQ(initialiCursorsAmount + 1, getCacheCursorsSize());
cur.open(transaction);
EXPECT_NO_THROW(cur.first());
}
EXPECT_EQ(initialiCursorsAmount, getCacheCursorsSize());
LMDBAL::Cursor<uint64_t, std::string> cur;
EXPECT_EQ(initialiCursorsAmount, getCacheCursorsSize());
EXPECT_EQ(cur.empty(), true);
cur = cache->createCursor();
EXPECT_EQ(cur.empty(), false);
EXPECT_EQ(initialiCursorsAmount + 1, getCacheCursorsSize());
EXPECT_THROW(emptyCache->destroyCursor(cur), LMDBAL::Unknown);
cache->destroyCursor(cur);
EXPECT_EQ(cur.empty(), true);
EXPECT_EQ(initialiCursorsAmount, getCacheCursorsSize());
cur = cache->createCursor();
EXPECT_EQ(initialiCursorsAmount + 1, getCacheCursorsSize());
EXPECT_EQ(cur.empty(), false);
cur.drop();
EXPECT_EQ(cur.empty(), true);
EXPECT_EQ(initialiCursorsAmount, getCacheCursorsSize());
EXPECT_NO_THROW(cur.drop());
EXPECT_THROW(cache->destroyCursor(cur), LMDBAL::Unknown);
}
TEST_F(CacheCursorTest, CornerCases) {
transaction.terminate();
EXPECT_THROW(cursor.current(), LMDBAL::Unknown);
cursor.close();
LMDBAL::Cursor<uint64_t, std::string> emptyCursor = emptyCache->createCursor();
emptyCursor.open();
EXPECT_THROW(emptyCursor.first(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.last(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.next(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.prev(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.current(), LMDBAL::Unknown);
emptyCursor.close();
cursor.open();
EXPECT_THROW(cursor.current(), LMDBAL::Unknown); //yeah, nice thing to write in the doc
std::map<uint64_t, std::string>::const_reverse_iterator breference = data.rbegin();
std::pair<uint64_t, std::string> element(cursor.prev());
EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again!
EXPECT_EQ(element.second, breference->second);
element = cursor.current();
EXPECT_EQ(element.first, breference->first);
EXPECT_EQ(element.second, breference->second);
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
cursor.close();
cursor.open();
element = cursor.next();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor.current();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
}

281
test/cachetransaction.cpp Normal file
View File

@ -0,0 +1,281 @@
#include <gtest/gtest.h>
#include <iostream>
#include "base.h"
#include "cache.h"
class CacheTransactionsTest : public testing::Test {
protected:
CacheTransactionsTest():
testing::Test(),
c1(db->getCache<int16_t, int64_t>("cache1")),
c2(db->getCache<std::string, float>("cache2")) {}
~CacheTransactionsTest() {}
int waitForChildFork(int pid) {
int status;
if (0 > waitpid(pid, &status, 0)) {
std::cerr << "[----------] Waitpid error!" << std::endl;
return (-1);
}
if (WIFEXITED(status)) {
const int exit_status = WEXITSTATUS(status);
if (exit_status != 0) {
std::cerr << "[----------] Non-zero exit status " << exit_status << " from test!" << std::endl;
}
return exit_status;
} else {
std::cerr << "[----------] Non-normal exit from child!" << std::endl;
return (-2);
}
}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("storageTrnansactionsTestBase");
db->addStorage<int16_t, int64_t>("cache1");
db->addStorage<std::string, float>("cache2");
}
db->open();
db->drop();
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
LMDBAL::Cache<int16_t, int64_t>* c1;
LMDBAL::Cache<std::string, float>* c2;
};
LMDBAL::Base* CacheTransactionsTest::db = nullptr;
TEST_F(CacheTransactionsTest, Adding) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 0);
LMDBAL::WriteTransaction txn = db->beginTransaction();
c1->addRecord(5, 13, txn);
c1->addRecord(-53, 782, txn);
c1->addRecord(5892, -37829, txn);
c2->addRecord("lorem", 481, txn);
c2->addRecord("decallence", 8532.48, txn);
c2->addRecord("prevent recovery", -64.64, txn);
EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 0);
txn.commit();
EXPECT_EQ(c1->count(), 3);
EXPECT_EQ(c1->getRecord(5), 13);
EXPECT_EQ(c1->getRecord(-53), 782);
EXPECT_EQ(c1->getRecord(5892), -37829);
EXPECT_EQ(c2->count(), 3);
EXPECT_FLOAT_EQ(c2->getRecord("lorem"), 481);
EXPECT_FLOAT_EQ(c2->getRecord("decallence"), 8532.48);
EXPECT_FLOAT_EQ(c2->getRecord("prevent recovery"), -64.64);
}
TEST_F(CacheTransactionsTest, Aborting) {
EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType s1 = c1->count();
LMDBAL::SizeType s2 = c2->count();
LMDBAL::WriteTransaction txn = db->beginTransaction();
c1->addRecord(18, 40, txn);
c1->addRecord(85, -4, txn);
c1->addRecord(-5, -3, txn);
c2->addRecord("tapestry", .053, txn);
c2->addRecord("pepper plants are beautifull", -7, txn);
c2->addRecord("horrots", -23.976, txn);
EXPECT_EQ(c1->count(), s1);
EXPECT_EQ(c2->count(), s2);
txn.abort();
EXPECT_EQ(c1->count(), s1);
EXPECT_EQ(c2->count(), s2);
}
TEST_F(CacheTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true);
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
EXPECT_EQ(c1->count(txn), 3);
EXPECT_EQ(c1->getRecord(5, txn), 13);
EXPECT_EQ(c1->getRecord(-53, txn), 782);
EXPECT_EQ(c1->getRecord(5892, txn), -37829);
EXPECT_EQ(c2->count(txn), 3);
EXPECT_FLOAT_EQ(c2->getRecord("lorem", txn), 481);
EXPECT_FLOAT_EQ(c2->getRecord("decallence", txn), 8532.48);
EXPECT_FLOAT_EQ(c2->getRecord("prevent recovery", txn), -64.64);
txn.terminate();
}
TEST_F(CacheTransactionsTest, ConcurentReading) {
EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType size = c1->count();
LMDBAL::WriteTransaction txn = db->beginTransaction();
EXPECT_EQ(c1->getRecord(5, txn), 13);
EXPECT_EQ(c1->getRecord(5), 13);
c1->removeRecord(5, txn);
EXPECT_FALSE(c1->checkRecord(5, txn));
EXPECT_EQ(c1->getRecord(5), 13);
c1->addRecord(5, 571, txn);
EXPECT_EQ(c1->getRecord(5, txn), 571);
EXPECT_EQ(c1->getRecord(5), 13);
c1->forceRecord(5, -472, txn);
EXPECT_EQ(c1->getRecord(5, txn), -472);
EXPECT_EQ(c1->getRecord(5), 13);
c1->replaceAll({
{1, 75}
}, txn);
EXPECT_FALSE(c1->checkRecord(5, txn));
EXPECT_EQ(c1->getRecord(5), 13);
EXPECT_EQ(c1->count(txn), 1);
EXPECT_EQ(c1->count(), size);
txn.commit();
EXPECT_FALSE(c1->checkRecord(5));
EXPECT_EQ(c1->count(), 1);
}
TEST_F(CacheTransactionsTest, ConcurentModification) {
EXPECT_EQ(db->ready(), true);
//if you start one writable transaction after another
//in a single thread like so:
//
//LMDBAL::TransactionID txn1 = db->beginTransaction();
//LMDBAL::TransactionID txn2 = db->beginTransaction();
//
//the execution should block on the second transaction
//so this test should preform in a sequence
//first the parent, then the child
int pid = fork();
if (pid == 0) { // I am the child
usleep(1);
std::cout << "beggining second transaction" << std::endl;
LMDBAL::WriteTransaction txn2 = db->beginTransaction(); //<--- this is where the execution should pause
//and wait for the first transaction to get finished
std::cout << "checking result of the first transaction value" << std::endl;
EXPECT_EQ(c1->getRecord(5, txn2), 812);
std::cout << "forcing second transaction value" << std::endl;
c1->forceRecord(5, -46, txn2);
std::cout << "checking second transaction value" << std::endl;
EXPECT_EQ(c1->getRecord(5, txn2), -46);
std::cout << "checking value independently" << std::endl;
EXPECT_EQ(c1->getRecord(5), 812);
std::cout << "commiting second transaction" << std::endl;
txn2.commit();
std::cout << "quitting child thread" << std::endl;
exit(testing::Test::HasFailure());
} else { // I am the parent
std::cout << "beggining first transaction" << std::endl;
LMDBAL::WriteTransaction txn1 = db->beginTransaction();
std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
usleep(5);
std::cout << "adding first transaction value" << std::endl;
c1->addRecord(5, 812, txn1);
std::cout << "checking first transaction value" << std::endl;
EXPECT_EQ(c1->getRecord(5, txn1), 812);
std::cout << "checking value independently" << std::endl;
EXPECT_FALSE(c1->checkRecord(5));
std::cout << "commiting first transaction" << std::endl;
txn1.commit();
std::cout << "waiting for the other thread to finish" << std::endl;
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
}
std::cout << "checking final result" << std::endl;
EXPECT_EQ(c1->getRecord(5), -46);
}
TEST_F(CacheTransactionsTest, RAIIResourceFree) {
EXPECT_EQ(db->ready(), true);
int pid = fork();
if (pid == 0) { // I am the child
usleep(1);
std::cout << "beggining child transaction" << std::endl;
LMDBAL::WriteTransaction txn2 = db->beginTransaction(); //<--- this is where the execution should pause
//and wait for the first transaction to get finished
std::cout << "checking result of the parent transaction value" << std::endl;
EXPECT_FALSE(c1->checkRecord(221, txn2));
std::cout << "performing modification from the child thread" << std::endl;
c1->addRecord(221, 14, txn2);
std::cout << "commiting child transaction" << std::endl;
txn2.commit();
std::cout << "quitting child thread, letting child transaction be destroyed after commit" << std::endl;
exit(testing::Test::HasFailure());
} else { // I am the parent
std::cout << "beggining parent transaction" << std::endl;
{
LMDBAL::WriteTransaction txn1 = db->beginTransaction();
std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
usleep(5);
std::cout << "parent thread woke up" << std::endl;
std::cout << "adding value from parent thread" << std::endl;
c1->addRecord(221, 320, txn1);
std::cout << "checking value from parent thread using transaction" << std::endl;
EXPECT_EQ(c1->getRecord(221, txn1), 320);
std::cout << "checking value independently from parent thread" << std::endl;
EXPECT_FALSE(c1->checkRecord(221));
std::cout << "implicitly aborting transaction by leaving the scope" << std::endl;
}
std::cout << "child thread should resume after this line" << std::endl;
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
}
std::cout << "checking the final result" << std::endl;
EXPECT_EQ(c1->getRecord(221), 14);
}

497
test/duplicates.cpp Normal file
View File

@ -0,0 +1,497 @@
#include <gtest/gtest.h>
#include <limits>
#include <map>
#include <set>
#include "base.h"
#include "storage.h"
#include "cursor.h"
class DuplicatesTest : public ::testing::Test {
protected:
DuplicatesTest():
::testing::Test(),
tu1(db->getStorage<int16_t, uint16_t>("sameSizeInts")),
tu2(db->getStorage<std::string, int8_t>("stringInt")),
tu3(db->getStorage<float, float>("floatFloat")),
tu4(db->getStorage<uint16_t, double>("intDouble")),
tu5(db->getStorage<float, int64_t>("floatLong")) {}
~DuplicatesTest() {}
uint32_t getTU1Flags() const {return tu1->flags();}
uint32_t getTU2Flags() const {return tu2->flags();}
uint32_t getTU3Flags() const {return tu3->flags();}
uint32_t getTU4Flags() const {return tu4->flags();}
uint32_t getTU5Flags() const {return tu5->flags();}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("testBase");
db->addStorage<int16_t, uint16_t>("sameSizeInts", true);
db->addStorage<std::string, int8_t>("stringInt", true);
db->addStorage<float, float>("floatFloat", true);
db->addStorage<uint16_t, double>("intDouble", true);
db->addStorage<float, int64_t>("floatLong", true);
db->open();
}
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
LMDBAL::Storage<int16_t, uint16_t>* tu1;
LMDBAL::Storage<std::string, int8_t>* tu2;
LMDBAL::Storage<float, float>* tu3;
LMDBAL::Storage<uint16_t, double>* tu4;
LMDBAL::Storage<float, int64_t>* tu5;
};
LMDBAL::Base* DuplicatesTest::db = nullptr;
TEST_F(DuplicatesTest, Flags) {
uint32_t tu1Flags = getTU1Flags();
uint32_t tu2Flags = getTU2Flags();
uint32_t tu3Flags = getTU3Flags();
uint32_t tu4Flags = getTU4Flags();
uint32_t tu5Flags = getTU5Flags();
EXPECT_TRUE(tu1Flags & MDB_INTEGERKEY);
EXPECT_TRUE(tu1Flags & MDB_DUPSORT);
EXPECT_TRUE(tu1Flags & MDB_DUPFIXED);
EXPECT_FALSE(tu1Flags & MDB_INTEGERDUP);
EXPECT_FALSE(tu2Flags & MDB_INTEGERKEY);
EXPECT_TRUE(tu2Flags & MDB_DUPSORT);
EXPECT_TRUE(tu2Flags & MDB_DUPFIXED);
EXPECT_FALSE(tu2Flags & MDB_INTEGERDUP);
EXPECT_FALSE(tu3Flags & MDB_INTEGERKEY);
EXPECT_TRUE(tu3Flags & MDB_DUPSORT);
EXPECT_TRUE(tu3Flags & MDB_DUPFIXED);
EXPECT_FALSE(tu3Flags & MDB_INTEGERDUP);
EXPECT_TRUE(tu4Flags & MDB_INTEGERKEY);
EXPECT_TRUE(tu4Flags & MDB_DUPSORT);
EXPECT_TRUE(tu4Flags & MDB_DUPFIXED);
EXPECT_FALSE(tu4Flags & MDB_INTEGERDUP);
EXPECT_FALSE(tu5Flags & MDB_INTEGERKEY);
EXPECT_TRUE(tu5Flags & MDB_DUPSORT);
EXPECT_TRUE(tu5Flags & MDB_DUPFIXED);
EXPECT_TRUE(tu5Flags & MDB_INTEGERDUP);
}
TEST_F(DuplicatesTest, Adding) {
tu1->addRecord(1, 1);
tu1->addRecord(2, 2);
tu1->addRecord(2, 1);
tu1->addRecord(1, 2);
EXPECT_THROW(tu1->addRecord(1, 1), LMDBAL::Exist);
EXPECT_THROW(tu1->addRecord(1, 2), LMDBAL::Exist);
EXPECT_THROW(tu1->addRecord(2, 2), LMDBAL::Exist);
EXPECT_EQ(tu1->count(), 4);
EXPECT_EQ(tu1->getRecord(1), 1);
EXPECT_EQ(tu1->getRecord(2), 1);
tu2->addRecord("brass boulers", -54);
tu2->addRecord("grief ", 61);
tu2->addRecord("grief ", 19);
tu2->addRecord("grief ", 32);
tu2->addRecord("miracles of a lunch", 44);
tu2->addRecord("miracles of a lunch", 102);
tu2->addRecord("miracles of a lunch", -72);
EXPECT_THROW(tu2->addRecord("grief ", 19), LMDBAL::Exist);
EXPECT_THROW(tu2->addRecord("brass boulers", -54), LMDBAL::Exist);
EXPECT_EQ(tu2->count(), 7);
EXPECT_EQ(tu2->getRecord("grief "), 19);
EXPECT_EQ(tu2->getRecord("miracles of a lunch"), 44); //apparently ints are compared as uints
tu3->addRecord(7.2, 697);
tu3->addRecord(5119, -998.53);
tu3->addRecord(7.2001, 4);
tu3->addRecord(7.2, -113);
tu3->addRecord(7.2, -53.5478);
float tu3ds = 0.432924;
tu3->addRecord(5119, tu3ds);
EXPECT_THROW(tu3->addRecord(5119, -998.53), LMDBAL::Exist);
EXPECT_THROW(tu3->addRecord(7.2001, 4), LMDBAL::Exist);
tu3->addRecord(7.20001, 4.00000001); //not sure how exactly, but it works
EXPECT_EQ(tu3->count(), 7);
std::set<float> res72({-113, -53.5478, 697, 4, 4.00000001});
EXPECT_EQ(res72.count(tu3->getRecord(7.2)), 1);
float tu3dd = tu3->getRecord(5119);
EXPECT_TRUE(tu3ds == tu3dd);
EXPECT_EQ(tu3ds, tu3dd);
tu4->addRecord(327, 463.28348);
tu4->addRecord(327, 79.624923);
tu4->addRecord(172, 0.00001);
tu4->addRecord(172, 0.00000001);
EXPECT_THROW(tu4->addRecord(172, 0.00000001), LMDBAL::Exist);
EXPECT_THROW(tu4->addRecord(172, 0.00001), LMDBAL::Exist);
EXPECT_THROW(tu4->addRecord(327, 79.624923), LMDBAL::Exist);
EXPECT_EQ(tu4->count(), 4);
std::set<double> res327({463.28348, 79.624923});
std::set<double> res172({0.00001, 0.00000001});
EXPECT_EQ(res172.count(tu4->getRecord(172)), 1);
EXPECT_EQ(res327.count(tu4->getRecord(327)), 1);
tu5->addRecord(-84.7, 45656753);
EXPECT_THROW(tu5->addRecord(-84.7, 45656753), LMDBAL::Exist);
tu5->addRecord(-84.7, 45656754);
int64_t intMax = std::numeric_limits<int32_t>::max();
int64_t intMin = std::numeric_limits<int32_t>::min();
int64_t longMax = std::numeric_limits<int64_t>::max();
int64_t longMin = std::numeric_limits<int64_t>::min();
tu5->addRecord(52.87, intMax);
EXPECT_THROW(tu5->addRecord(52.87, intMax), LMDBAL::Exist);
tu5->addRecord(52.87, intMin);
EXPECT_THROW(tu5->addRecord(52.87, intMin), LMDBAL::Exist);
tu5->addRecord(52.87, longMax);
EXPECT_THROW(tu5->addRecord(52.87, longMax), LMDBAL::Exist);
tu5->addRecord(52.87, longMin);
EXPECT_THROW(tu5->addRecord(52.87, longMin), LMDBAL::Exist);
EXPECT_EQ(tu5->count(), 6);
EXPECT_EQ(tu5->getRecord(-84.7), 45656753);
EXPECT_EQ(tu5->getRecord(52.87), intMax);
}
TEST_F(DuplicatesTest, Forcing) {
LMDBAL::SizeType tu1Size = tu1->count();
tu1->addRecord(-56, 71);
tu1->addRecord(-56, 274);
tu1->addRecord(-56, 732);
std::set<uint16_t> res56({71, 274, 732});
EXPECT_EQ(tu1->count(), tu1Size += 3);
EXPECT_TRUE(tu1->forceRecord(-56, 322));
res56.insert(322);
EXPECT_EQ(tu1->count(), tu1Size += 1);
EXPECT_EQ(res56.count(tu1->getRecord(-56)), 1);
res56.insert(14);
EXPECT_TRUE(tu1->forceRecord(-56, 14));
EXPECT_EQ(tu1->count(), tu1Size += 1);
EXPECT_EQ(res56.count(tu1->getRecord(-56)), 1);
EXPECT_FALSE(tu1->forceRecord(-56, 274));
EXPECT_EQ(tu1->count(), tu1Size);
LMDBAL::SizeType tu2Size = tu2->count();
tu2->addRecord("printable", -2);
tu2->addRecord("printable", 4);
EXPECT_EQ(tu2->count(), tu2Size += 2);
EXPECT_TRUE(tu2->forceRecord("printable", 18));
EXPECT_EQ(tu2->count(), tu2Size += 1);
EXPECT_EQ(tu2->getRecord("printable"), 4);
EXPECT_TRUE(tu2->forceRecord("printable", 3));
EXPECT_EQ(tu2->count(), tu2Size += 1);
EXPECT_EQ(tu2->getRecord("printable"), 3);
EXPECT_FALSE(tu2->forceRecord("printable", 4));
EXPECT_EQ(tu2->count(), tu2Size);
LMDBAL::SizeType tu3Size = tu3->count();
tu3->addRecord(17.3, 93.21);
tu3->addRecord(17.3, 6.6);
tu3->addRecord(17.3, 105.1);
std::set<float> res17({93.21, 6.6, 105.1});
EXPECT_EQ(tu3->count(), tu3Size += 3);
EXPECT_TRUE(tu3->forceRecord(17.3, 74.9));
res17.insert(74.9);
EXPECT_EQ(tu3->count(), tu3Size += 1);
EXPECT_EQ(res17.count(tu3->getRecord(17.3)), 1);
EXPECT_TRUE(tu3->forceRecord(17.3, 5.1));
res17.insert(5.1);
EXPECT_EQ(tu3->count(), tu3Size += 1);
EXPECT_EQ(res17.count(tu3->getRecord(17.3)), 1);
EXPECT_FALSE(tu3->forceRecord(17.3, 93.21));
EXPECT_EQ(tu3->count(), tu3Size);
LMDBAL::SizeType tu4Size = tu4->count();
tu4->addRecord(84, -359.109);
tu4->addRecord(84, 2879.654);
std::set<double> res84({-359.109, 2879.654});
EXPECT_EQ(tu4->count(), tu4Size += 2);
EXPECT_TRUE(tu4->forceRecord(84, 72.9));
res84.insert(72.9);
EXPECT_EQ(tu4->count(), tu4Size += 1);
EXPECT_EQ(res84.count(tu4->getRecord(84)), 1);
EXPECT_TRUE(tu4->forceRecord(84, 2679.5));
res84.insert(2679.5);
EXPECT_EQ(tu4->count(), tu4Size += 1);
EXPECT_EQ(res84.count(tu4->getRecord(84)), 1);
EXPECT_FALSE(tu4->forceRecord(84, -359.109));
EXPECT_EQ(tu4->count(), tu4Size);
LMDBAL::SizeType tu5Size = tu5->count();
tu5->addRecord(0.45, -85645);
tu5->addRecord(0.45, 10573);
tu5->addRecord(0.45, 573);
tu5->addRecord(0.45, 73285);
EXPECT_EQ(tu5->count(), tu5Size += 4);
EXPECT_TRUE(tu5->forceRecord(0.45, -473));
EXPECT_EQ(tu5->count(), tu5Size += 1);
EXPECT_EQ(tu5->getRecord(0.45), 573);
EXPECT_TRUE(tu5->forceRecord(0.45, 394));
EXPECT_EQ(tu5->count(), tu5Size += 1);
EXPECT_EQ(tu5->getRecord(0.45), 394);
EXPECT_FALSE(tu5->forceRecord(0.45, 10573));
EXPECT_EQ(tu5->count(), tu5Size);
}
TEST_F(DuplicatesTest, Changing) {
LMDBAL::SizeType tu1Size = tu1->count();
EXPECT_THROW(tu1->changeRecord(-31, 53), LMDBAL::NotFound);
EXPECT_EQ(tu1->count(), tu1Size);
tu1->addRecord(-31, 53);
EXPECT_EQ(tu1->count(), tu1Size += 1);
EXPECT_EQ(tu1->getRecord(-31), 53);
tu1->changeRecord(-31, 53); //should just do nothing usefull, but work normally
EXPECT_EQ(tu1->getRecord(-31), 53);
tu1->changeRecord(-31, 19);
EXPECT_EQ(tu1->count(), tu1Size);
EXPECT_EQ(tu1->getRecord(-31), 19);
tu1->addRecord(-31, 60);
EXPECT_EQ(tu1->count(), tu1Size += 1);
EXPECT_EQ(tu1->getRecord(-31), 19);
tu1->changeRecord(-31, 16);
EXPECT_EQ(tu1->count(), tu1Size);
EXPECT_EQ(tu1->getRecord(-31), 16);
tu1->changeRecord(-31, 203);
EXPECT_EQ(tu1->count(), tu1Size);
EXPECT_EQ(tu1->getRecord(-31), 60);
EXPECT_THROW(tu1->changeRecord(-31, 203), LMDBAL::Exist);
LMDBAL::SizeType tu2Size = tu2->count();
EXPECT_THROW(tu2->changeRecord("jeremy spins", -5), LMDBAL::NotFound);
EXPECT_EQ(tu2->count(), tu2Size);
tu2->addRecord("jeremy spins", -5);
EXPECT_EQ(tu2->count(), tu2Size += 1);
EXPECT_EQ(tu2->getRecord("jeremy spins"), -5);
tu2->changeRecord("jeremy spins", -5); //should just do nothing usefull, but work normally
EXPECT_EQ(tu2->getRecord("jeremy spins"), -5);
tu2->changeRecord("jeremy spins", 11);
EXPECT_EQ(tu2->count(), tu2Size);
EXPECT_EQ(tu2->getRecord("jeremy spins"), 11);
tu2->addRecord("jeremy spins", 24);
EXPECT_EQ(tu2->count(), tu2Size += 1);
EXPECT_EQ(tu2->getRecord("jeremy spins"), 11);
tu2->changeRecord("jeremy spins", 4);
EXPECT_EQ(tu2->count(), tu2Size);
EXPECT_EQ(tu2->getRecord("jeremy spins"), 4);
tu2->changeRecord("jeremy spins", -7);
EXPECT_EQ(tu2->count(), tu2Size);
EXPECT_EQ(tu2->getRecord("jeremy spins"), 24); //cuz it's compared as usigned down there
EXPECT_THROW(tu2->changeRecord("jeremy spins", -7), LMDBAL::Exist);
LMDBAL::SizeType tu3Size = tu3->count();
std::set<float> res26;
EXPECT_THROW(tu3->changeRecord(26.7, 68.22), LMDBAL::NotFound);
EXPECT_EQ(tu3->count(), tu3Size);
tu3->addRecord(26.7, 68.22);
EXPECT_EQ(tu3->count(), tu3Size += 1);
EXPECT_EQ(tu3->getRecord(26.7), 68.22f);
tu3->changeRecord(26.7, 68.22); //should just do nothing usefull, but work normally
EXPECT_EQ(tu3->getRecord(26.7), 68.22f);
tu3->changeRecord(26.7, 23.18);
res26.insert(23.18);
EXPECT_EQ(tu3->count(), tu3Size);
EXPECT_EQ(tu3->getRecord(26.7), 23.18f);
tu3->addRecord(26.7, 22.16);
res26.insert(22.16);
EXPECT_EQ(tu3->count(), tu3Size += 1);
EXPECT_EQ(res26.count(tu3->getRecord(26.7)), 1);
tu3->changeRecord(26.7, 21.7);
EXPECT_EQ(tu3->count(), tu3Size);
EXPECT_EQ(tu3->getRecord(26.7), 21.7f);
tu3->changeRecord(26.7, 54.33);
EXPECT_EQ(tu3->count(), tu3Size);
EXPECT_EQ(tu3->getRecord(26.7), 22.16f);
EXPECT_THROW(tu3->changeRecord(26.7, 54.33), LMDBAL::Exist);
LMDBAL::SizeType tu4Size = tu4->count();
EXPECT_THROW(tu4->changeRecord(852, 6795.349), LMDBAL::NotFound);
EXPECT_EQ(tu4->count(), tu4Size);
tu4->addRecord(852, 6795.349);
EXPECT_EQ(tu4->count(), tu4Size += 1);
EXPECT_EQ(tu4->getRecord(852), 6795.349);
tu4->changeRecord(852, 6795.349); //should just do nothing usefull, but work normally
EXPECT_EQ(tu4->getRecord(852), 6795.349);
tu4->changeRecord(852, 13.54);
std::set<double> res852({13.54});
EXPECT_EQ(tu4->count(), tu4Size);
EXPECT_EQ(tu4->getRecord(852), 13.54);
tu4->addRecord(852, 213.85);
res852.insert(213.85);
EXPECT_EQ(tu4->count(), tu4Size += 1);
EXPECT_EQ(res852.count(tu4->getRecord(852)), 1);
tu4->changeRecord(852, 236.21);
res852.insert(236.21);
EXPECT_EQ(tu4->count(), tu4Size);
EXPECT_EQ(tu4->getRecord(852), 236.21);
tu4->changeRecord(852, 46324.1135);
res852.insert(46324.1135);
EXPECT_EQ(tu4->count(), tu4Size);
EXPECT_EQ(res852.count(tu4->getRecord(852)), 1);
EXPECT_THROW(tu4->changeRecord(852, 46324.1135), LMDBAL::Exist);
}
TEST_F(DuplicatesTest, GettingAllRecords) {
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
bool cycle;
LMDBAL::SizeType iterations;
std::map<int16_t, uint16_t> m1;
std::set<int16_t> k1;
LMDBAL::Cursor<int16_t, uint16_t> c1 = tu1->createCursor();
tu1->readAll(m1, txn);
c1.open(txn);
cycle = false;
iterations = 0;
do {
try {
std::pair<int16_t, uint16_t> pair = c1.next();
cycle = true;
std::pair<std::set<int16_t>::const_iterator, bool> probe = k1.insert(pair.first);
if (probe.second) {
uint16_t valueAll = m1.at(pair.first);
uint16_t valueGet;
EXPECT_NO_THROW(tu1->getRecord(pair.first, valueGet, txn));
EXPECT_EQ(valueAll, valueGet);
}
++iterations;
} catch (const LMDBAL::NotFound& e) {
cycle = false;
}
} while (cycle);
EXPECT_EQ(iterations, tu1->count(txn));
EXPECT_EQ(k1.size(), m1.size());
EXPECT_NE(iterations, 0);
EXPECT_NE(k1.size(), 0);
c1.drop();
std::map<std::string, int8_t> m2;
std::set<std::string> k2;
LMDBAL::Cursor<std::string, int8_t> c2 = tu2->createCursor();
tu2->readAll(m2, txn);
c2.open(txn);
cycle = false;
iterations = 0;
do {
try {
std::pair<std::string, int8_t> pair = c2.next();
cycle = true;
std::pair<std::set<std::string>::const_iterator, bool> probe = k2.insert(pair.first);
if (probe.second) {
int8_t valueAll = m2.at(pair.first);
int8_t valueGet;
EXPECT_NO_THROW(tu2->getRecord(pair.first, valueGet, txn));
EXPECT_EQ(valueAll, valueGet);
}
++iterations;
} catch (const LMDBAL::NotFound& e) {
cycle = false;
}
} while (cycle);
EXPECT_EQ(iterations, tu2->count(txn));
EXPECT_EQ(k2.size(), m2.size());
EXPECT_NE(iterations, 0);
EXPECT_NE(k2.size(), 0);
c2.drop();
std::map<float, float> m3;
std::set<float> k3;
LMDBAL::Cursor<float, float> c3 = tu3->createCursor();
tu3->readAll(m3, txn);
c3.open(txn);
cycle = false;
iterations = 0;
do {
try {
std::pair<float, float> pair = c3.next();
cycle = true;
std::pair<std::set<float>::const_iterator, bool> probe = k3.insert(pair.first);
if (probe.second) {
float valueAll = m3.at(pair.first);
float valueGet;
EXPECT_NO_THROW(tu3->getRecord(pair.first, valueGet, txn));
EXPECT_EQ(valueAll, valueGet);
}
++iterations;
} catch (const LMDBAL::NotFound& e) {
cycle = false;
}
} while (cycle);
EXPECT_EQ(iterations, tu3->count(txn));
EXPECT_EQ(k3.size(), m3.size());
EXPECT_NE(iterations, 0);
EXPECT_NE(k3.size(), 0);
c3.drop();
std::map<uint16_t, double> m4;
std::set<uint16_t> k4;
LMDBAL::Cursor<uint16_t, double> c4 = tu4->createCursor();
tu4->readAll(m4, txn);
c4.open(txn);
cycle = false;
iterations = 0;
do {
try {
std::pair<uint16_t, double> pair = c4.next();
cycle = true;
std::pair<std::set<uint16_t>::const_iterator, bool> probe = k4.insert(pair.first);
if (probe.second) {
double valueAll = m4.at(pair.first);
double valueGet;
EXPECT_NO_THROW(tu4->getRecord(pair.first, valueGet, txn));
EXPECT_EQ(valueAll, valueGet);
}
++iterations;
} catch (const LMDBAL::NotFound& e) {
cycle = false;
}
} while (cycle);
c4.drop();
EXPECT_EQ(iterations, tu4->count(txn));
EXPECT_EQ(k4.size(), m4.size());
EXPECT_NE(iterations, 0);
EXPECT_NE(k4.size(), 0);
txn.terminate();
}

339
test/serialization.cpp Normal file
View File

@ -0,0 +1,339 @@
#include <gtest/gtest.h>
#include <serializer.h>
#include <operators.hpp>
TEST(Serialization, Double) {
double source1 = 5344.6542;
double source2 = 0.4329248;
LMDBAL::Serializer<double> serializer;
LMDBAL::Serializer<double> serializer2(source1);
LMDBAL::Serializer<double> deserializer;
serializer.setData(source1);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
double destination;
serializer.deserialize(data, destination);
EXPECT_DOUBLE_EQ(source1, destination);
double dest2 = serializer.deserialize(data);
EXPECT_DOUBLE_EQ(source1, dest2);
data = serializer.setData(source2);
serializer.deserialize(data, destination);
EXPECT_DOUBLE_EQ(source2, destination);
}
TEST(Serialization, Float) {
float source = 5.156;
LMDBAL::Serializer<float> serializer;
LMDBAL::Serializer<float> serializer2(source);
LMDBAL::Serializer<float> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
float destination;
serializer.deserialize(data, destination);
EXPECT_FLOAT_EQ(source, destination);
float dest2 = serializer.deserialize(data);
EXPECT_FLOAT_EQ(source, dest2);
}
TEST(Serialization, Int8) {
int8_t source = 38;
LMDBAL::Serializer<int8_t> serializer;
LMDBAL::Serializer<int8_t> serializer2(source);
LMDBAL::Serializer<int8_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int8_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int8_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int16) {
int16_t source = -3469;
LMDBAL::Serializer<int16_t> serializer;
LMDBAL::Serializer<int16_t> serializer2(source);
LMDBAL::Serializer<int16_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int16_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int16_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int32) {
int32_t source = 454832;
LMDBAL::Serializer<int32_t> serializer;
LMDBAL::Serializer<int32_t> serializer2(source);
LMDBAL::Serializer<int32_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int32_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int32_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, Int64) {
int64_t source = -875525478136;
LMDBAL::Serializer<int64_t> serializer;
LMDBAL::Serializer<int64_t> serializer2(source);
LMDBAL::Serializer<int64_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
int64_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
int64_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt8) {
uint8_t source = 196;
LMDBAL::Serializer<uint8_t> serializer;
LMDBAL::Serializer<uint8_t> serializer2(source);
LMDBAL::Serializer<uint8_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint8_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint8_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt16) {
uint16_t source = 8634;
LMDBAL::Serializer<uint16_t> serializer;
LMDBAL::Serializer<uint16_t> serializer2(source);
LMDBAL::Serializer<uint16_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint16_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint16_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt32) {
uint32_t source = 115469;
LMDBAL::Serializer<uint32_t> serializer;
LMDBAL::Serializer<uint32_t> serializer2(source);
LMDBAL::Serializer<uint32_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint32_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint32_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, UInt64) {
uint64_t source = 498763546873;
LMDBAL::Serializer<uint64_t> serializer;
LMDBAL::Serializer<uint64_t> serializer2(source);
LMDBAL::Serializer<uint64_t> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
uint64_t destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
uint64_t dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, StdString) {
std::string source("days just go by, some good and some are bad");
LMDBAL::Serializer<std::string> serializer;
LMDBAL::Serializer<std::string> serializer2(source);
LMDBAL::Serializer<std::string> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
std::string destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
std::string dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, QString) {
QString source("may be will find nothing new, may be I'll end up just just like you");
LMDBAL::Serializer<QString> serializer;
LMDBAL::Serializer<QString> serializer2(source);
LMDBAL::Serializer<QString> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
QString destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
QString dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, QByteArray) {
QByteArray source = QByteArray::fromHex("84 be 81 6c d3 5e c3 49 94 51 6f 8f a7 3b 0c d8 29 23 a3 21 4d b8 3f 73");
LMDBAL::Serializer<QByteArray> serializer;
LMDBAL::Serializer<QByteArray> serializer2(source);
LMDBAL::Serializer<QByteArray> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
QByteArray destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
QByteArray dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}
TEST(Serialization, StdMap) {
std::map<uint16_t, uint8_t> source({
{467, 123},
{1397, 8},
{551, -9},
{864, 114},
{9615, -85},
{32, 32}
});
LMDBAL::Serializer<std::map<uint16_t, uint8_t>> serializer;
LMDBAL::Serializer<std::map<uint16_t, uint8_t>> serializer2(source);
LMDBAL::Serializer<std::map<uint16_t, uint8_t>> deserializer;
serializer.setData(source);
MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData();
EXPECT_EQ(data.mv_size, data2.mv_size);
EXPECT_EQ(std::memcmp(data.mv_data, data2.mv_data, data.mv_size), 0);
std::map<uint16_t, uint8_t> destination;
serializer.deserialize(data, destination);
EXPECT_EQ(source, destination);
std::map<uint16_t, uint8_t> dest2 = serializer.deserialize(data);
EXPECT_EQ(source, dest2);
}

417
test/storagecursor.cpp Normal file
View File

@ -0,0 +1,417 @@
#include <gtest/gtest.h>
#include "base.h"
#include "storage.h"
#include "cursor.h"
class StorageCursorTest : public ::testing::Test {
protected:
StorageCursorTest():
::testing::Test(),
table (db->getStorage<uint64_t, std::string>("table1")),
emptyTable (db->getStorage<uint64_t, std::string>("empty")) {}
~StorageCursorTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("testBase");
db->addStorage<uint64_t, std::string>("table1");
db->addStorage<uint64_t, std::string>("empty");
db->open();
}
}
int getTableCursorsSize() const {
return table->cursors.size();
}
static void TearDownTestSuite() {
cursor.drop();
transaction.terminate();
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string> cursor;
static LMDBAL::Transaction transaction;
LMDBAL::Storage<uint64_t, std::string>* table;
LMDBAL::Storage<uint64_t, std::string>* emptyTable;
};
LMDBAL::Base* StorageCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string> StorageCursorTest::cursor;
LMDBAL::Transaction StorageCursorTest::transaction = LMDBAL::Transaction();
static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"},
{3458, "resilent pick forefront"},
{105190, "apportunity legal bat"},
{6510, "outside"},
{7438537, "damocles plush apparently rusty"},
{19373572, "local guidence"},
{138842, "forgetting tusks prepare"},
{981874, "butchered soaking pawn"},
{19302, "tanned inmate"},
{178239, "custody speaks neurotic"},
});
TEST_F(StorageCursorTest, PopulatingTheTable) {
uint32_t amount = table->addRecords(data);
EXPECT_EQ(amount, data.size());
}
TEST_F(StorageCursorTest, Creation) {
EXPECT_EQ(getTableCursorsSize(), 0);
cursor = table->createCursor();
EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
cursor.open();
}
TEST_F(StorageCursorTest, FirstPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, NextPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++;
for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, LastPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, PrevPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++;
for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, CurrentPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor.current();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
element = cursor.current();
++reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
cursor.next();
cursor.prev();
element = cursor.current();
++reference;
++reference;
--reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, SettingPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_FALSE(cursor.set(6684));
EXPECT_EQ(cursor.current().second, "tanned inmate");
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
std::advance(reference, 5);
EXPECT_TRUE(cursor.set(reference->first));
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
}
TEST_F(StorageCursorTest, Destruction) {
EXPECT_EQ(getTableCursorsSize(), 1);
cursor.close();
EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
cursor = LMDBAL::Cursor<uint64_t, std::string>();
EXPECT_EQ(getTableCursorsSize(), 0);
cursor = table->createCursor();
EXPECT_EQ(getTableCursorsSize(), 1);
}
TEST_F(StorageCursorTest, FirstPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
transaction = db->beginReadOnlyTransaction();
cursor.open(transaction);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, NextPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++;
for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, LastPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, PrevPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++;
for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, CurrentPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor.current();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
element = cursor.current();
++reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
cursor.next();
cursor.next();
cursor.prev();
element = cursor.current();
++reference;
++reference;
--reference;
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
}
TEST_F(StorageCursorTest, SettingPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_FALSE(cursor.set(557));
EXPECT_EQ(cursor.current().second, "resilent pick forefront");
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
std::advance(reference, 3);
EXPECT_TRUE(cursor.set(reference->first));
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
++reference;
cursor.next();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
--reference;
cursor.prev();
EXPECT_EQ(cursor.current().second, reference->second);
}
TEST_F(StorageCursorTest, CursorRAIIBehaviour) {
int initialiCursorsAmount = getTableCursorsSize();
{
LMDBAL::Cursor<uint64_t, std::string> cur = table->createCursor();
EXPECT_EQ(initialiCursorsAmount + 1, getTableCursorsSize());
cur.open(transaction);
EXPECT_NO_THROW(cur.first());
}
EXPECT_EQ(initialiCursorsAmount, getTableCursorsSize());
LMDBAL::Cursor<uint64_t, std::string> cur;
EXPECT_EQ(initialiCursorsAmount, getTableCursorsSize());
EXPECT_EQ(cur.empty(), true);
cur = table->createCursor();
EXPECT_EQ(cur.empty(), false);
EXPECT_EQ(initialiCursorsAmount + 1, getTableCursorsSize());
EXPECT_THROW(emptyTable->destroyCursor(cur), LMDBAL::Unknown);
table->destroyCursor(cur);
EXPECT_EQ(cur.empty(), true);
EXPECT_EQ(initialiCursorsAmount, getTableCursorsSize());
cur = table->createCursor();
EXPECT_EQ(initialiCursorsAmount + 1, getTableCursorsSize());
EXPECT_EQ(cur.empty(), false);
cur.drop();
EXPECT_EQ(cur.empty(), true);
EXPECT_EQ(initialiCursorsAmount, getTableCursorsSize());
EXPECT_NO_THROW(cur.drop());
EXPECT_THROW(table->destroyCursor(cur), LMDBAL::Unknown);
}
TEST_F(StorageCursorTest, CornerCases) {
EXPECT_EQ(getTableCursorsSize(), 1);
transaction.terminate();
EXPECT_THROW(cursor.current(), LMDBAL::Unknown);
cursor.close();
LMDBAL::Cursor<uint64_t, std::string> emptyCursor = emptyTable->createCursor();
emptyCursor.open();
EXPECT_THROW(emptyCursor.first(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.last(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.next(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.prev(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor.current(), LMDBAL::Unknown);
emptyCursor.close();
cursor.open();
EXPECT_THROW(cursor.current(), LMDBAL::Unknown); //yeah, nice thing to write in the doc
std::map<uint64_t, std::string>::const_reverse_iterator breference = data.rbegin();
std::pair<uint64_t, std::string> element(cursor.prev());
EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again!
EXPECT_EQ(element.second, breference->second);
element = cursor.current();
EXPECT_EQ(element.first, breference->first);
EXPECT_EQ(element.second, breference->second);
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
cursor.close();
cursor.open();
element = cursor.next();
std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
element = cursor.current();
EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second);
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
}

279
test/storagetransaction.cpp Normal file
View File

@ -0,0 +1,279 @@
#include <gtest/gtest.h>
#include <iostream>
#include "base.h"
#include "storage.h"
class StorageTransactionsTest : public testing::Test {
protected:
StorageTransactionsTest():
testing::Test(),
t1(db->getStorage<int16_t, int64_t>("table1")),
t2(db->getStorage<std::string, float>("table2")) {}
~StorageTransactionsTest() {}
int waitForChildFork(int pid) {
int status;
if (0 > waitpid(pid, &status, 0)) {
std::cerr << "[----------] Waitpid error!" << std::endl;
return (-1);
}
if (WIFEXITED(status)) {
const int exit_status = WEXITSTATUS(status);
if (exit_status != 0)
std::cerr << "[----------] Non-zero exit status " << exit_status << " from test!" << std::endl;
return exit_status;
} else {
std::cerr << "[----------] Non-normal exit from child!" << std::endl;
return (-2);
}
}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBAL::Base("storageTrnansactionsTestBase");
db->addStorage<int16_t, int64_t>("table1");
db->addStorage<std::string, float>("table2");
}
db->open();
db->drop();
}
static void TearDownTestSuite() {
db->close();
db->removeDirectory();
delete db;
db = nullptr;
}
static LMDBAL::Base* db;
LMDBAL::Storage<int16_t, int64_t>* t1;
LMDBAL::Storage<std::string, float>* t2;
};
LMDBAL::Base* StorageTransactionsTest::db = nullptr;
TEST_F(StorageTransactionsTest, Adding) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
LMDBAL::WriteTransaction txn = db->beginTransaction();
t1->addRecord(5, 13, txn);
t1->addRecord(-53, 782, txn);
t1->addRecord(5892, -37829, txn);
t2->addRecord("lorem", 481, txn);
t2->addRecord("decallence", 8532.48, txn);
t2->addRecord("prevent recovery", -64.64, txn);
EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0);
txn.commit();
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t1->getRecord(5), 13);
EXPECT_EQ(t1->getRecord(-53), 782);
EXPECT_EQ(t1->getRecord(5892), -37829);
EXPECT_EQ(t2->count(), 3);
EXPECT_FLOAT_EQ(t2->getRecord("lorem"), 481);
EXPECT_FLOAT_EQ(t2->getRecord("decallence"), 8532.48);
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery"), -64.64);
}
TEST_F(StorageTransactionsTest, Aborting) {
EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType s1 = t1->count();
LMDBAL::SizeType s2 = t2->count();
LMDBAL::WriteTransaction txn = db->beginTransaction();
t1->addRecord(18, 40, txn);
t1->addRecord(85, -4, txn);
t1->addRecord(-5, -3, txn);
t2->addRecord("tapestry", .053, txn);
t2->addRecord("pepper plants are beautifull", -7, txn);
t2->addRecord("horrots", -23.976, txn);
EXPECT_EQ(t1->count(), s1);
EXPECT_EQ(t2->count(), s2);
txn.abort();
EXPECT_EQ(t1->count(), s1);
EXPECT_EQ(t2->count(), s2);
}
TEST_F(StorageTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true);
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
EXPECT_EQ(t1->count(txn), 3);
EXPECT_EQ(t1->getRecord(5, txn), 13);
EXPECT_EQ(t1->getRecord(-53, txn), 782);
EXPECT_EQ(t1->getRecord(5892, txn), -37829);
EXPECT_EQ(t2->count(txn), 3);
EXPECT_FLOAT_EQ(t2->getRecord("lorem", txn), 481);
EXPECT_FLOAT_EQ(t2->getRecord("decallence", txn), 8532.48);
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery", txn), -64.64);
txn.terminate();
}
TEST_F(StorageTransactionsTest, ConcurentReading) {
EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType size = t1->count();
LMDBAL::WriteTransaction txn = db->beginTransaction();
EXPECT_EQ(t1->getRecord(5, txn), 13);
EXPECT_EQ(t1->getRecord(5), 13);
t1->removeRecord(5, txn);
EXPECT_FALSE(t1->checkRecord(5, txn));
EXPECT_EQ(t1->getRecord(5), 13);
t1->addRecord(5, 571, txn);
EXPECT_EQ(t1->getRecord(5, txn), 571);
EXPECT_EQ(t1->getRecord(5), 13);
t1->forceRecord(5, -472, txn);
EXPECT_EQ(t1->getRecord(5, txn), -472);
EXPECT_EQ(t1->getRecord(5), 13);
t1->replaceAll({
{1, 75}
}, txn);
EXPECT_FALSE(t1->checkRecord(5, txn));
EXPECT_EQ(t1->getRecord(5), 13);
EXPECT_EQ(t1->count(txn), 1);
EXPECT_EQ(t1->count(), size);
txn.commit();
EXPECT_FALSE(t1->checkRecord(5));
EXPECT_EQ(t1->count(), 1);
}
TEST_F(StorageTransactionsTest, ConcurentModification) {
EXPECT_EQ(db->ready(), true);
//if you start one writable transaction after another
//in a single thread like so:
//
//LMDBAL::TransactionID txn1 = db->beginTransaction();
//LMDBAL::TransactionID txn2 = db->beginTransaction();
//
//the execution should block on the second transaction
//so this test should preform in a sequence
//first the parent, then the child
int pid = fork();
if (pid == 0) { // I am the child
usleep(1);
std::cout << "beggining second transaction" << std::endl;
LMDBAL::WriteTransaction txn2 = db->beginTransaction(); //<--- this is where the execution should pause
//and wait for the first transaction to get finished
std::cout << "checking result of the first transaction value" << std::endl;
EXPECT_EQ(t1->getRecord(5, txn2), 812);
std::cout << "forcing second transaction value" << std::endl;
t1->forceRecord(5, -46, txn2);
std::cout << "checking second transaction value" << std::endl;
EXPECT_EQ(t1->getRecord(5, txn2), -46);
std::cout << "checking value independently" << std::endl;
EXPECT_EQ(t1->getRecord(5), 812);
std::cout << "commiting second transaction" << std::endl;
txn2.commit();
std::cout << "quitting child thread" << std::endl;
exit(testing::Test::HasFailure());
} else { // I am the parent
std::cout << "beggining first transaction" << std::endl;
LMDBAL::WriteTransaction txn1 = db->beginTransaction();
std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
usleep(5);
std::cout << "adding first transaction value" << std::endl;
t1->addRecord(5, 812, txn1);
std::cout << "checking first transaction value" << std::endl;
EXPECT_EQ(t1->getRecord(5, txn1), 812);
std::cout << "checking value independently" << std::endl;
EXPECT_FALSE(t1->checkRecord(5));
std::cout << "commiting first transaction" << std::endl;
txn1.commit();
std::cout << "waiting for the other thread to finish" << std::endl;
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
}
std::cout << "checking the final result" << std::endl;
EXPECT_EQ(t1->getRecord(5), -46);
}
TEST_F(StorageTransactionsTest, RAIIResourceFree) {
EXPECT_EQ(db->ready(), true);
int pid = fork();
if (pid == 0) { // I am the child
usleep(1);
std::cout << "beggining child transaction" << std::endl;
LMDBAL::WriteTransaction txn2 = db->beginTransaction(); //<--- this is where the execution should pause
//and wait for the first transaction to get finished
std::cout << "checking result of the parent transaction value" << std::endl;
EXPECT_FALSE(t1->checkRecord(221, txn2));
std::cout << "performing modification from the child thread" << std::endl;
t1->addRecord(221, 14, txn2);
std::cout << "commiting child transaction" << std::endl;
txn2.commit();
std::cout << "quitting child thread, letting child transaction be destroyed after commit" << std::endl;
exit(testing::Test::HasFailure());
} else { // I am the parent
std::cout << "beggining parent transaction" << std::endl;
{
LMDBAL::WriteTransaction txn1 = db->beginTransaction();
std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
usleep(5);
std::cout << "parent thread woke up" << std::endl;
std::cout << "adding value from parent thread" << std::endl;
t1->addRecord(221, 320, txn1);
std::cout << "checking value from parent thread using transaction" << std::endl;
EXPECT_EQ(t1->getRecord(221, txn1), 320);
std::cout << "checking value independently from parent thread" << std::endl;
EXPECT_FALSE(t1->checkRecord(221));
std::cout << "implicitly aborting transaction by leaving the scope" << std::endl;
}
std::cout << "child thread should resume after this line" << std::endl;
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
}
std::cout << "checking the final result" << std::endl;
EXPECT_EQ(t1->getRecord(221), 14);
}