Compare commits
No commits in common. "master" and "0.5.0" have entirely different histories.
@ -1,79 +0,0 @@
|
||||
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
|
@ -17,7 +17,7 @@ jobs:
|
||||
|
||||
- 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
|
||||
run: cmake .. -D BUILD_TESTS=True -D BUILD_DOC=True -D BUILD_DOXYGEN_AWESOME=True -D QT_VERSION_MAJOR=5
|
||||
|
||||
- name: Build
|
||||
working-directory: ./build
|
||||
@ -35,5 +35,5 @@ jobs:
|
||||
username: ${{ secrets.DEPLOY_USER_NAME }}
|
||||
key: ${{ secrets.DEPLOY_PRIVATE_KEY }}
|
||||
source: "build/doc/html/*,build/doc/xml/*,build/doc/man/*"
|
||||
target: "/srv/lmdbal/doc"
|
||||
target: ${{ secrets.LMDBAL_DOCS_DEPLOY_PATH }}
|
||||
strip_components: 2
|
||||
|
23
CHANGELOG.md
23
CHANGELOG.md
@ -1,28 +1,5 @@
|
||||
# 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)
|
||||
|
@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(LMDBAL
|
||||
VERSION 0.5.4
|
||||
VERSION 0.5.0
|
||||
DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer"
|
||||
LANGUAGES CXX
|
||||
)
|
||||
@ -12,9 +12,7 @@ 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_DOC "Builds documentation" OFF)
|
||||
option(BUILD_DOXYGEN_AWESOME "Builds documentation alternative style" OFF)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
@ -23,7 +21,7 @@ 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")
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
|
||||
|
||||
if (NOT DEFINED QT_VERSION_MAJOR)
|
||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
|
||||
@ -56,21 +54,14 @@ 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} 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)
|
||||
if (BUILD_DOC)
|
||||
find_package(Doxygen)
|
||||
if (DOXYGEN_FOUND)
|
||||
add_subdirectory(doc)
|
||||
@ -83,18 +74,14 @@ 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} PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}>"
|
||||
)
|
||||
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}
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
lmdb
|
||||
)
|
||||
@ -113,7 +100,8 @@ write_basic_package_version_file(
|
||||
|
||||
install(TARGETS ${PROJECT_NAME}
|
||||
EXPORT ${PROJECT_LOW}Targets
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_LOW}
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}
|
||||
)
|
||||
|
||||
|
45
LICENSE.md
45
LICENSE.md
@ -1,4 +1,4 @@
|
||||
# GNU GENERAL PUBLIC LICENSE
|
||||
### GNU GENERAL PUBLIC LICENSE
|
||||
|
||||
Version 3, 29 June 2007
|
||||
|
||||
@ -8,7 +8,7 @@ Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
Everyone is permitted to copy and distribute verbatim copies of this
|
||||
license document, but changing it is not allowed.
|
||||
|
||||
## Preamble
|
||||
### Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
@ -73,9 +73,9 @@ 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
|
||||
### TERMS AND CONDITIONS
|
||||
|
||||
### 0. Definitions.
|
||||
#### 0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
@ -115,7 +115,7 @@ 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.
|
||||
#### 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
|
||||
@ -156,7 +156,7 @@ 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.
|
||||
#### 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
|
||||
@ -181,7 +181,7 @@ 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.
|
||||
#### 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
|
||||
@ -197,7 +197,7 @@ 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.
|
||||
#### 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
|
||||
@ -210,7 +210,7 @@ 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.
|
||||
#### 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
|
||||
@ -245,7 +245,7 @@ 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.
|
||||
#### 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
|
||||
@ -341,7 +341,7 @@ 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.
|
||||
#### 7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
@ -400,7 +400,7 @@ 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.
|
||||
#### 8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
@ -428,7 +428,7 @@ 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.
|
||||
#### 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
|
||||
@ -439,7 +439,7 @@ 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.
|
||||
#### 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
|
||||
@ -464,7 +464,7 @@ rights granted under this License, and you may not initiate litigation
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
### 11. Patents.
|
||||
#### 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
|
||||
@ -533,7 +533,7 @@ 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.
|
||||
#### 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
|
||||
@ -546,7 +546,7 @@ 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.
|
||||
#### 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
|
||||
@ -557,7 +557,7 @@ 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.
|
||||
#### 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
|
||||
@ -583,7 +583,7 @@ 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.
|
||||
#### 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
|
||||
@ -595,7 +595,7 @@ 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.
|
||||
#### 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
|
||||
@ -607,7 +607,7 @@ 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.
|
||||
#### 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,
|
||||
@ -618,7 +618,7 @@ copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
## How to Apply These Terms to Your New Programs
|
||||
### 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
|
||||
@ -673,4 +673,3 @@ 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>.
|
||||
|
||||
|
@ -97,4 +97,4 @@ 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
|
||||
This project is licensed under the GPLv3 License - see the [LICENSE.md](LICENSE.md) file for details
|
||||
|
@ -1,12 +1,6 @@
|
||||
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()
|
||||
set(DOXYGEN_GENERATE_HTML YES)
|
||||
set(DOXYGEN_GENERATE_MAN YES)
|
||||
set(DOXYGEN_GENERATE_XML YES)
|
||||
|
||||
if (BUILD_DOXYGEN_AWESOME)
|
||||
include(ExternalProject)
|
||||
@ -42,24 +36,12 @@ doxygen_add_docs(
|
||||
ALL
|
||||
COMMENT "Generate man and html pages"
|
||||
)
|
||||
if (BUILD_DOC_MAN)
|
||||
install(DIRECTORY
|
||||
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)
|
||||
|
@ -1,21 +1,12 @@
|
||||
/*! \mainpage Getting Started
|
||||
*
|
||||
* Everything begins with a data base represented by the class LMDBAL::Base.
|
||||
* Everything begins with a data nase 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
|
||||
* 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.
|
||||
*
|
||||
* After you have created a LMDBAL::Base you probably want to obtain storage handlers.
|
||||
@ -24,55 +15,17 @@
|
||||
* <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().
|
||||
* You can obtain handlers by calling LMDBAL::Base::addStorage(const std::string&, bool) or LMDBAL::Base::addCache(const std::string& name).
|
||||
* 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()
|
||||
* you can obtain them at any time later using methods LMDBAL::Base::getStorage(const std::string&) or LMDBAL::Base::getCache(const std::string&)
|
||||
* 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.
|
||||
*/
|
||||
|
@ -1,17 +1,17 @@
|
||||
# Maintainer: Yury Gubich <blue@macaw.me>
|
||||
pkgname=lmdbal
|
||||
pkgver=1.0.0
|
||||
pkgver=0.5.0
|
||||
pkgrel=1
|
||||
pkgdesc="LMDB Abstraction Layer"
|
||||
pkgdesc="LMDB Abstraction Layer, qt5 version"
|
||||
arch=('i686' 'x86_64')
|
||||
url="https://git.macaw.me/blue/lmdbal"
|
||||
license=('GPL3')
|
||||
depends=( 'lmdb' )
|
||||
depends=( 'lmdb' 'qt5-base')
|
||||
makedepends=('cmake>=3.16' 'gcc')
|
||||
optdepends=()
|
||||
|
||||
source=("$pkgname-$pkgver.tar.gz::https://git.macaw.me/blue/$pkgname/archive/$pkgver.tar.gz")
|
||||
sha256sums=('SKIP')
|
||||
sha256sums=('df1a9687d81d609d160754285f2613d7e07fc6deb781d0fb0084e4857ea82e95')
|
||||
build() {
|
||||
cd "$srcdir/$pkgname"
|
||||
cmake . -D CMAKE_INSTALL_PREFIX=/usr -D CMAKE_BUILD_TYPE=Release -D QT_VERSION_MAJOR=5
|
||||
|
@ -2,7 +2,6 @@ set(SOURCES
|
||||
exceptions.cpp
|
||||
storage.cpp
|
||||
base.cpp
|
||||
transaction.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
@ -14,12 +13,27 @@ set(HEADERS
|
||||
cursor.hpp
|
||||
cache.h
|
||||
cache.hpp
|
||||
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
|
||||
operators.hpp
|
||||
transaction.h
|
||||
)
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
||||
add_subdirectory(serializer)
|
||||
target_sources(${PROJECT_NAME} PRIVATE
|
||||
${SOURCES}
|
||||
${HEADERS}
|
||||
)
|
||||
|
||||
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW})
|
||||
|
67
src/base.cpp
67
src/base.cpp
@ -19,7 +19,6 @@
|
||||
#include "base.h"
|
||||
#include "exceptions.h"
|
||||
#include "storage.h"
|
||||
#include "transaction.h"
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
@ -121,7 +120,8 @@ bool LMDBAL::Base::removeDirectory() {
|
||||
if (opened)
|
||||
throw Opened(name, "remove database directory");
|
||||
|
||||
QString path = getPath();
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + getName();
|
||||
QDir cache(path);
|
||||
|
||||
if (cache.exists())
|
||||
@ -148,8 +148,10 @@ QString LMDBAL::Base::createDirectory() {
|
||||
if (opened)
|
||||
throw Opened(name, "create database directory");
|
||||
|
||||
QString path = getPath();
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + getName();
|
||||
QDir cache(path);
|
||||
|
||||
if (!cache.exists()) {
|
||||
bool res = cache.mkpath(path);
|
||||
if (!res)
|
||||
@ -167,17 +169,6 @@ QString LMDBAL::Base::createDirectory() {
|
||||
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
|
||||
@ -199,63 +190,38 @@ void LMDBAL::Base::drop() {
|
||||
if (!opened)
|
||||
throw Closed("drop", name);
|
||||
|
||||
TransactionID txn = beginPrivateTransaction(emptyName);
|
||||
TransactionID txn = beginTransaction();
|
||||
for (const std::pair<const std::string, iStorage*>& pair : storages) {
|
||||
int rc = pair.second->drop(txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
abortPrivateTransaction(txn, emptyName);
|
||||
abortTransaction(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
commitPrivateTransaction(txn, emptyName);
|
||||
for (const std::pair<const std::string, iStorage*>& pair : storages)
|
||||
pair.second->handleDrop();
|
||||
commitTransaction(txn);
|
||||
}
|
||||
|
||||
/**
|
||||
* \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
|
||||
* \returns read-only transaction ID
|
||||
*
|
||||
* \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);
|
||||
}
|
||||
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const {
|
||||
return beginReadOnlyTransaction(emptyName);}
|
||||
|
||||
/**
|
||||
* \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
|
||||
* \returns writable transaction ID
|
||||
*
|
||||
* \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);
|
||||
}
|
||||
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const {
|
||||
return beginTransaction(emptyName);}
|
||||
|
||||
/**
|
||||
* \brief Aborts transaction
|
||||
@ -402,9 +368,10 @@ void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string
|
||||
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)
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
|
||||
}
|
||||
return txn;
|
||||
}
|
||||
|
||||
|
13
src/base.h
13
src/base.h
@ -36,8 +36,6 @@
|
||||
namespace LMDBAL {
|
||||
|
||||
class iStorage;
|
||||
class Transaction;
|
||||
class WriteTransaction;
|
||||
|
||||
template<class T>
|
||||
class Serializer;
|
||||
@ -53,8 +51,6 @@ typedef uint32_t SizeType; /**<\brief All LMDBAL si
|
||||
|
||||
class Base {
|
||||
friend class iStorage;
|
||||
friend class Transaction;
|
||||
friend class WriteTransaction;
|
||||
public:
|
||||
|
||||
Base(const QString& name, uint16_t mapSize = 10);
|
||||
@ -66,11 +62,12 @@ public:
|
||||
bool removeDirectory();
|
||||
QString createDirectory();
|
||||
QString getName() const;
|
||||
QString getPath() const;
|
||||
void drop();
|
||||
|
||||
Transaction beginReadOnlyTransaction() const;
|
||||
WriteTransaction beginTransaction();
|
||||
TransactionID beginReadOnlyTransaction() const;
|
||||
TransactionID beginTransaction() const;
|
||||
void commitTransaction(TransactionID id);
|
||||
void abortTransaction(TransactionID id) const;
|
||||
|
||||
template <class K, class V>
|
||||
LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
|
||||
@ -88,8 +85,6 @@ 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);
|
||||
|
40
src/cache.h
40
src/cache.h
@ -54,27 +54,12 @@ 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;
|
||||
|
||||
@ -87,34 +72,37 @@ private:
|
||||
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 handleDrop();
|
||||
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 int drop(TransactionID transaction) override;
|
||||
virtual void addRecord(const K& key, const V& value) override;
|
||||
virtual void addRecord(const K& key, const V& value, TransactionID txn) override;
|
||||
virtual bool forceRecord(const K& key, const V& value) override;
|
||||
virtual bool forceRecord(const K& key, const V& value, TransactionID txn) override;
|
||||
virtual void changeRecord(const K& key, const V& value) override;
|
||||
virtual void changeRecord(const K& key, const V& value, TransactionID txn) override;
|
||||
virtual void removeRecord(const K& key) override;
|
||||
virtual void removeRecord(const K& key, TransactionID txn) override;
|
||||
virtual bool checkRecord(const K& key) const override;
|
||||
virtual bool checkRecord(const K& key, TransactionID txn) const override;
|
||||
virtual void getRecord(const K& key, V& out) const override;
|
||||
virtual void getRecord(const K& key, V& out, TransactionID txn) const override;
|
||||
virtual V getRecord(const K& key) const override;
|
||||
virtual V getRecord(const K& key, TransactionID txn) const override;
|
||||
virtual SizeType count() const override;
|
||||
virtual SizeType count(TransactionID txn) const override;
|
||||
|
||||
virtual std::map<K, V> readAll() const override;
|
||||
virtual std::map<K, V> readAll(TransactionID txn) const override;
|
||||
virtual void readAll(std::map<K, V>& out) const override;
|
||||
virtual void readAll(std::map<K, V>& out, TransactionID txn) const override;
|
||||
virtual void replaceAll(const std::map<K, V>& data) override;
|
||||
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn) override;
|
||||
virtual SizeType addRecords(const std::map<K, V>& data, bool overwrite = false) override;
|
||||
virtual SizeType addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false) override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
|
@ -75,6 +75,8 @@ void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value) {
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
|
||||
iStorage::ensureOpened(iStorage::addRecordMethodName);
|
||||
|
||||
if (cache->count(key) > 0)
|
||||
iStorage::throwDuplicate(iStorage::toString(key));
|
||||
|
||||
@ -107,6 +109,8 @@ bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value) {
|
||||
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
|
||||
iStorage::ensureOpened(iStorage::forceRecordMethodName);
|
||||
|
||||
bool added = Storage<K, V>::forceRecord(key, value, txn);
|
||||
|
||||
typename TransactionCache::iterator tc = transactionCache->find(txn);
|
||||
@ -167,6 +171,8 @@ void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
|
||||
iStorage::ensureOpened(iStorage::changeRecordMethodName);
|
||||
|
||||
if (mode == Mode::full) {
|
||||
typename std::map<K, V>::iterator itr = cache->find(key);
|
||||
if (itr == cache->end())
|
||||
@ -242,6 +248,8 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
|
||||
|
||||
template<class K, class V>
|
||||
V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const {
|
||||
iStorage::ensureOpened(iStorage::getRecordMethodName);
|
||||
|
||||
V value;
|
||||
Cache<K, V>::getRecord(key, value, txn);
|
||||
return value;
|
||||
@ -249,6 +257,8 @@ V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const {
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) const {
|
||||
iStorage::ensureOpened(iStorage::getRecordMethodName);
|
||||
|
||||
//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
|
||||
@ -377,6 +387,8 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
|
||||
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
|
||||
iStorage::ensureOpened(iStorage::checkRecordMethodName);
|
||||
|
||||
//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
|
||||
@ -485,6 +497,8 @@ void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out) const {
|
||||
|
||||
template<class K, class V>
|
||||
std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const {
|
||||
iStorage::ensureOpened(iStorage::readAllMethodName);
|
||||
|
||||
std::map<K, V> out;
|
||||
readAll(out, txn);
|
||||
|
||||
@ -493,6 +507,8 @@ std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const {
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const {
|
||||
iStorage::ensureOpened(iStorage::readAllMethodName);
|
||||
|
||||
typename TransactionCache::iterator tc = transactionCache->find(txn);
|
||||
if (tc != transactionCache->end()) {
|
||||
Queue& queue = tc->second;
|
||||
@ -549,7 +565,7 @@ void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const
|
||||
} 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
|
||||
Storage<K, V>::readAll(out); //that are missing in the cache
|
||||
*cache = out;
|
||||
abscent->clear();
|
||||
sizeDifference = 0;
|
||||
@ -660,6 +676,8 @@ void LMDBAL::Cache<K, V>::removeRecord(const K& key) {
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cache<K, V>::removeRecord(const K& key, TransactionID txn) {
|
||||
iStorage::ensureOpened(iStorage::removeRecordMethodName);
|
||||
|
||||
bool noKey = false;
|
||||
if (mode != Mode::full)
|
||||
noKey = cache->count(key) == 0;
|
||||
@ -711,6 +729,7 @@ uint32_t LMDBAL::Cache<K, V>::count() const {
|
||||
|
||||
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);
|
||||
@ -781,15 +800,10 @@ void LMDBAL::Cache<K, V>::handleMode() const {
|
||||
}
|
||||
|
||||
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);
|
||||
int LMDBAL::Cache<K, V>::drop(TransactionID transaction) {
|
||||
int res = Storage<K, V>::drop(transaction);
|
||||
|
||||
if (res != MDB_SUCCESS)
|
||||
return res;
|
||||
|
||||
typename TransactionCache::iterator tc = transactionCache->find(txn);
|
||||
typename TransactionCache::iterator tc = transactionCache->find(transaction);
|
||||
if (tc != transactionCache->end())
|
||||
tc->second.emplace_back(Operation::drop, nullptr);
|
||||
|
||||
@ -841,23 +855,27 @@ void LMDBAL::Cache<K, V>::handleTransactionEntry(const Entry& entry) {
|
||||
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
|
||||
handleAddRecord(pair->first, pair->second);
|
||||
delete pair;
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
case Operation::remove: {
|
||||
K* key = static_cast<K*>(entry.second);
|
||||
handleRemoveRecord(*key);
|
||||
delete key;
|
||||
} break;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case Operation::drop:
|
||||
handleDrop();
|
||||
break;
|
||||
@ -869,7 +887,8 @@ void LMDBAL::Cache<K, V>::handleTransactionEntry(const Entry& entry) {
|
||||
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;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
49
src/cursor.h
49
src/cursor.h
@ -24,7 +24,6 @@
|
||||
#include "lmdb.h"
|
||||
#include "base.h"
|
||||
#include "storage.h"
|
||||
#include "transaction.h"
|
||||
|
||||
namespace LMDBAL {
|
||||
|
||||
@ -38,50 +37,37 @@ private:
|
||||
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();
|
||||
public:
|
||||
void open() const;
|
||||
void open(TransactionID txn) const;
|
||||
void renew() const;
|
||||
void renew(TransactionID txn) const;
|
||||
void close() const;
|
||||
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> first() const;
|
||||
std::pair<K, V> last() const;
|
||||
std::pair<K, V> next() const;
|
||||
std::pair<K, V> prev() const;
|
||||
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 first(K& key, V& value) const;
|
||||
void last(K& key, V& value) const;
|
||||
void next(K& key, V& value) const;
|
||||
void prev(K& key, V& value) const;
|
||||
void current(K& key, V& value) const;
|
||||
|
||||
private:
|
||||
void dropped();
|
||||
void freed();
|
||||
void terminated();
|
||||
void terminated() const;
|
||||
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;
|
||||
mutable MDB_cursor* cursor;
|
||||
mutable State state;
|
||||
|
||||
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*/
|
||||
@ -92,7 +78,6 @@ private:
|
||||
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*/
|
||||
|
304
src/cursor.hpp
304
src/cursor.hpp
@ -20,7 +20,6 @@
|
||||
#define LMDBAL_CURSOR_HPP
|
||||
|
||||
#include "cursor.h"
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* \class LMDBAL::Cursor
|
||||
@ -41,8 +40,6 @@
|
||||
* You are not supposed to instantiate or destory instances of this class yourself!
|
||||
*/
|
||||
|
||||
static uint32_t idCounter = 0;
|
||||
|
||||
/**
|
||||
* \brief Creates a cursor
|
||||
*
|
||||
@ -52,70 +49,9 @@ 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)
|
||||
state(closed)
|
||||
{}
|
||||
|
||||
/**
|
||||
* \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
|
||||
*
|
||||
@ -124,67 +60,13 @@ LMDBAL::Cursor<K, V>& LMDBAL::Cursor<K, V>::operator = (Cursor&& other) {
|
||||
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 () {
|
||||
void LMDBAL::Cursor<K, V>::terminated () const {
|
||||
close(); //for now it's the same, but if I ever going to make writable cursor - here is where it's gonna be different
|
||||
}
|
||||
|
||||
@ -197,20 +79,16 @@ void LMDBAL::Cursor<K, V>::terminated () {
|
||||
* 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
|
||||
* \throws LMDBAL::Closed thrown if you try to open the cursor on a closed database
|
||||
* \throws LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb, or to begin a transaction
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::open () {
|
||||
if (empty())
|
||||
throw CursorEmpty(openCursorMethodName);
|
||||
|
||||
void LMDBAL::Cursor<K, V>::open () const {
|
||||
storage->ensureOpened(openCursorMethodName);
|
||||
switch (state) {
|
||||
case closed: {
|
||||
TransactionID txn = storage->beginReadOnlyTransaction();
|
||||
int result = storage->_mdbCursorOpen(txn, &cursor);
|
||||
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
|
||||
if (result != MDB_SUCCESS)
|
||||
storage->throwUnknown(result, txn);
|
||||
|
||||
@ -231,23 +109,15 @@ void LMDBAL::Cursor<K, V>::open () {
|
||||
* 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
|
||||
* \throws LMDBAL::Closed thrown if you try to open the cursor on a closed database
|
||||
* \throws LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::open (const Transaction& transaction) {
|
||||
if (empty())
|
||||
throw CursorEmpty(openCursorMethodName);
|
||||
|
||||
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
|
||||
storage->ensureOpened(openCursorMethodName);
|
||||
TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName);
|
||||
switch (state) {
|
||||
case closed: {
|
||||
int result = storage->_mdbCursorOpen(txn, &cursor);
|
||||
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
|
||||
if (result != MDB_SUCCESS)
|
||||
storage->throwUnknown(result);
|
||||
|
||||
@ -271,26 +141,22 @@ void LMDBAL::Cursor<K, V>::open (const Transaction& 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
|
||||
* \throws LMDBAL::Closed thrown if you try to renew the cursor on a closed database
|
||||
* \throws LMDBAL::Unknown thrown if there was a problem beginning new transaction or if there was a problem renewing the cursor by lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::renew () {
|
||||
if (empty())
|
||||
throw CursorEmpty(openCursorMethodName);
|
||||
|
||||
void LMDBAL::Cursor<K, V>::renew () const {
|
||||
storage->ensureOpened(renewCursorMethodName);
|
||||
switch (state) {
|
||||
case openedPrivate: {
|
||||
TransactionID txn = storage->_mdbCursorTxn(cursor);
|
||||
TransactionID txn = mdb_cursor_txn(cursor);
|
||||
storage->abortTransaction(txn);
|
||||
storage->transactionAborted(txn);
|
||||
[[fallthrough]];
|
||||
}
|
||||
case openedPublic: {
|
||||
TransactionID txn = storage->beginReadOnlyTransaction();
|
||||
int result = storage->_mdbCursorRenew(txn, cursor);
|
||||
int result = mdb_cursor_renew(txn, cursor);
|
||||
if (result != MDB_SUCCESS)
|
||||
storage->throwUnknown(result, txn);
|
||||
|
||||
@ -315,29 +181,23 @@ void LMDBAL::Cursor<K, V>::renew () {
|
||||
*
|
||||
* This function does nothing if the cursor is closed
|
||||
*
|
||||
* \param[in] transaction - a transaction you wish this cursor to be bound to
|
||||
* \param[in] txn 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
|
||||
* \throws LMDBAL::Closed thrown if you try to renew the cursor on a closed database
|
||||
* \throws LMDBAL::Unknown thrown if there was a problem renewing the cursor by lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::renew (const Transaction& transaction) {
|
||||
if (empty())
|
||||
throw CursorEmpty(openCursorMethodName);
|
||||
|
||||
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
|
||||
storage->ensureOpened(renewCursorMethodName);
|
||||
TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName);
|
||||
switch (state) {
|
||||
case openedPrivate: {
|
||||
TransactionID txn = storage->_mdbCursorTxn(cursor);
|
||||
TransactionID txn = mdb_cursor_txn(cursor);
|
||||
storage->abortTransaction(txn);
|
||||
storage->transactionAborted(txn);
|
||||
[[fallthrough]];
|
||||
}
|
||||
case openedPublic: {
|
||||
int result = storage->_mdbCursorRenew(txn, cursor);
|
||||
int result = mdb_cursor_renew(txn, cursor);
|
||||
if (result != MDB_SUCCESS)
|
||||
storage->throwUnknown(result);
|
||||
|
||||
@ -359,16 +219,16 @@ void LMDBAL::Cursor<K, V>::renew (const Transaction& transaction) {
|
||||
* This function does nothing on a closed cursor.
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::close () {
|
||||
void LMDBAL::Cursor<K, V>::close () const {
|
||||
switch (state) {
|
||||
case openedPublic: {
|
||||
storage->_mdbCursorClose(cursor);
|
||||
mdb_cursor_close(cursor);
|
||||
|
||||
state = closed;
|
||||
} break;
|
||||
case openedPrivate: {
|
||||
TransactionID txn = storage->_mdbCursorTxn(cursor);
|
||||
storage->_mdbCursorClose(cursor);
|
||||
TransactionID txn = mdb_cursor_txn(cursor);
|
||||
mdb_cursor_close(cursor);
|
||||
storage->abortTransaction(txn);
|
||||
storage->transactionAborted(txn);
|
||||
|
||||
@ -395,12 +255,12 @@ bool LMDBAL::Cursor<K, V>::opened () const {
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
|
||||
* \throws 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) {
|
||||
void LMDBAL::Cursor<K, V>::first (K& key, V& value) const {
|
||||
operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
|
||||
}
|
||||
|
||||
@ -412,12 +272,12 @@ void LMDBAL::Cursor<K, V>::first (K& key, V& value) {
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
|
||||
* \throws 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) {
|
||||
void LMDBAL::Cursor<K, V>::last (K& key, V& value) const {
|
||||
operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
|
||||
}
|
||||
|
||||
@ -435,12 +295,12 @@ void LMDBAL::Cursor<K, V>::last (K& key, V& value) {
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
|
||||
* \throws 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) {
|
||||
void LMDBAL::Cursor<K, V>::next (K& key, V& value) const {
|
||||
operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
|
||||
}
|
||||
|
||||
@ -458,12 +318,12 @@ void LMDBAL::Cursor<K, V>::next (K& key, V& value) {
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
|
||||
* \throws 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) {
|
||||
void LMDBAL::Cursor<K, V>::prev (K& key, V& value) const {
|
||||
operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
|
||||
}
|
||||
|
||||
@ -477,9 +337,9 @@ void LMDBAL::Cursor<K, V>::prev (K& key, V& value) {
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
|
||||
* \throws 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 {
|
||||
@ -493,12 +353,12 @@ void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
|
||||
*
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
|
||||
* \throws 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> LMDBAL::Cursor<K, V>::first () const {
|
||||
std::pair<K, V> result;
|
||||
operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
|
||||
return result;
|
||||
@ -511,12 +371,12 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::first () {
|
||||
*
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
|
||||
* \throws 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> LMDBAL::Cursor<K, V>::last () const {
|
||||
std::pair<K, V> result;
|
||||
operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
|
||||
return result;
|
||||
@ -535,12 +395,12 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::last () {
|
||||
*
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
|
||||
* \throws 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> LMDBAL::Cursor<K, V>::next () const {
|
||||
std::pair<K, V> result;
|
||||
operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
|
||||
return result;
|
||||
@ -559,12 +419,12 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::next () {
|
||||
*
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
|
||||
* \throws 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> LMDBAL::Cursor<K, V>::prev () const {
|
||||
std::pair<K, V> result;
|
||||
operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
|
||||
return result;
|
||||
@ -579,9 +439,9 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::prev () {
|
||||
*
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
|
||||
* \throws 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 {
|
||||
@ -590,34 +450,6 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
|
||||
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
|
||||
*
|
||||
@ -629,9 +461,9 @@ bool LMDBAL::Cursor<K, V>::set (const K& key) {
|
||||
* \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
|
||||
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \throws LMDBAL::NotFound mostly thrown if the query wasn't found
|
||||
* \throws LMDBAL::Unknown mostly thrown if there was some unexpected problem with lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::operateCursorRead(
|
||||
@ -645,7 +477,7 @@ void LMDBAL::Cursor<K, V>::operateCursorRead(
|
||||
storage->throwCursorNotReady(methodName);
|
||||
|
||||
MDB_val mdbKey, mdbValue;
|
||||
int result = storage->_mdbCursorGet(cursor, mdbKey, mdbValue, operation);
|
||||
int result = mdb_cursor_get(cursor, &mdbKey, &mdbValue, operation);
|
||||
if (result != MDB_SUCCESS)
|
||||
storage->throwNotFoundOrUnknown(result, operationName);
|
||||
|
||||
@ -655,7 +487,7 @@ void LMDBAL::Cursor<K, V>::operateCursorRead(
|
||||
if (state == openedPrivate)
|
||||
storage->discoveredRecord(key, value);
|
||||
else
|
||||
storage->discoveredRecord(key, value, storage->_mdbCursorTxn(cursor));
|
||||
storage->discoveredRecord(key, value, mdb_cursor_txn(cursor));
|
||||
}
|
||||
|
||||
#endif //LMDBAL_CURSOR_HPP
|
||||
|
@ -72,14 +72,6 @@ std::string LMDBAL::CursorNotReady::getMessage() const {
|
||||
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),
|
||||
@ -120,6 +112,7 @@ std::string LMDBAL::StorageDuplicate::getMessage() const {
|
||||
+ " but the database already has a storage with given name";
|
||||
}
|
||||
|
||||
|
||||
LMDBAL::Exist::Exist(
|
||||
const std::string& p_key,
|
||||
const std::string& p_dbName,
|
||||
@ -137,29 +130,6 @@ std::string LMDBAL::Exist::getMessage() const {
|
||||
+ " 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,
|
||||
|
@ -97,23 +97,6 @@ private:
|
||||
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
|
||||
*/
|
||||
@ -194,27 +177,6 @@ private:
|
||||
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
|
||||
*/
|
||||
|
@ -1,19 +0,0 @@
|
||||
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})
|
118
src/storage.cpp
118
src/storage.cpp
@ -56,23 +56,6 @@ 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
|
||||
@ -85,7 +68,7 @@ LMDBAL::TransactionID LMDBAL::iStorage::extractTransactionId(const Transaction&
|
||||
void LMDBAL::iStorage::drop() {
|
||||
ensureOpened(dropMethodName);
|
||||
|
||||
TransactionID txn = beginTransaction();
|
||||
TransactionID txn = db->beginTransaction();
|
||||
int rc = iStorage::drop(txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
abortTransaction(txn);
|
||||
@ -93,7 +76,6 @@ void LMDBAL::iStorage::drop() {
|
||||
}
|
||||
|
||||
db->commitTransaction(txn);
|
||||
handleDrop();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,21 +90,6 @@ 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
|
||||
*
|
||||
@ -160,7 +127,7 @@ LMDBAL::SizeType LMDBAL::iStorage::count() const {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Storage size (private transaction variant)
|
||||
* \brief Storage size (transaction variant)
|
||||
*
|
||||
* \param[in] txn - transaction ID, can be read-only transaction
|
||||
* \returns amount of records in the storage
|
||||
@ -176,21 +143,6 @@ LMDBAL::SizeType LMDBAL::iStorage::count(TransactionID txn) const {
|
||||
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)
|
||||
*
|
||||
@ -439,71 +391,5 @@ void LMDBAL::iStorage::transactionCommited(LMDBAL::TransactionID txn) {
|
||||
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);
|
||||
}
|
||||
|
@ -25,12 +25,9 @@
|
||||
#include "base.h"
|
||||
#include "serializer.h"
|
||||
#include "cursor.h"
|
||||
#include "transaction.h"
|
||||
|
||||
class BaseTest;
|
||||
class DuplicatesTest;
|
||||
class CacheCursorTest;
|
||||
class StorageCursorTest;
|
||||
|
||||
namespace LMDBAL {
|
||||
|
||||
@ -49,7 +46,6 @@ protected:
|
||||
*/
|
||||
virtual int open(MDB_txn * transaction) = 0;
|
||||
virtual void close();
|
||||
virtual void handleDrop();
|
||||
|
||||
bool isDBOpened() const;
|
||||
const std::string& dbName() const;
|
||||
@ -66,7 +62,6 @@ protected:
|
||||
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);
|
||||
@ -74,34 +69,12 @@ protected:
|
||||
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 int drop(TransactionID transaction);
|
||||
virtual SizeType count() const;
|
||||
virtual SizeType count(const Transaction& txn) const;
|
||||
virtual SizeType count(TransactionID txn) const;
|
||||
|
||||
protected:
|
||||
MDB_dbi dbi; /**<\brief lmdb storage handle*/
|
||||
@ -135,8 +108,6 @@ 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:
|
||||
@ -147,50 +118,38 @@ protected:
|
||||
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 void addRecord(const K& key, const V& value, TransactionID 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 bool forceRecord(const K& key, const V& value, TransactionID txn);
|
||||
virtual void changeRecord(const K& key, const V& value);
|
||||
virtual void changeRecord(const K& key, const V& value, const WriteTransaction& txn);
|
||||
virtual void changeRecord(const K& key, const V& value, TransactionID txn);
|
||||
virtual void removeRecord(const K& key);
|
||||
virtual void removeRecord(const K& key, const WriteTransaction& txn);
|
||||
virtual void removeRecord(const K& key, TransactionID 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 bool checkRecord(const K& key, TransactionID txn) const;
|
||||
virtual void getRecord(const K& key, V& value) const;
|
||||
virtual void getRecord(const K& key, V& value, const Transaction& txn) const;
|
||||
virtual void getRecord(const K& key, V& value, TransactionID txn) const;
|
||||
virtual V getRecord(const K& key) const;
|
||||
virtual V getRecord(const K& key, const Transaction& txn) const;
|
||||
virtual V getRecord(const K& key, TransactionID txn) const;
|
||||
virtual std::map<K, V> readAll() const;
|
||||
virtual std::map<K, V> readAll(const Transaction& txn) const;
|
||||
virtual std::map<K, V> readAll(TransactionID 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 readAll(std::map<K, V>& result, TransactionID txn) const;
|
||||
virtual void replaceAll(const std::map<K, V>& data);
|
||||
virtual void replaceAll(const std::map<K, V>& data, const WriteTransaction& txn);
|
||||
virtual void replaceAll(const std::map<K, V>& data, TransactionID 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);
|
||||
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
|
||||
|
||||
Cursor<K, V> createCursor();
|
||||
void destroyCursor(Cursor<K, V>& cursor);
|
||||
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*/
|
||||
std::set<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;
|
||||
|
447
src/storage.hpp
447
src/storage.hpp
@ -58,8 +58,8 @@ LMDBAL::Storage<K, V>::Storage(Base* parent, const std::string& name, bool dupli
|
||||
*/
|
||||
template<class K, class V>
|
||||
LMDBAL::Storage<K, V>::~Storage() {
|
||||
for (const std::pair<const uint32_t, Cursor<K, V>*>& pair : cursors)
|
||||
pair.second->dropped();
|
||||
for (Cursor<K, V>* cursor : cursors)
|
||||
delete cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +91,10 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage (private transaction variant)
|
||||
* \brief Adds a key-value record to the storage (transaction variant)
|
||||
*
|
||||
* This method schedules an addition of a key-value record, but doesn't immidiately adds it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* Take a note that if the storage already had a record you want to add LMDBAL::Exist is thrown.
|
||||
* If your storage doesn't support duplicates LMDBAL::Exist is thrown if the record with the same key already exists in the database.
|
||||
@ -102,10 +105,13 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value) {
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
*
|
||||
* \exception LMDBAL::Exist thrown if the storage already has a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
|
||||
ensureOpened(addRecordMethodName);
|
||||
|
||||
MDB_val lmdbKey = keySerializer.setData(key);
|
||||
MDB_val lmdbData = valueSerializer.setData(value);
|
||||
|
||||
@ -115,36 +121,11 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionI
|
||||
else
|
||||
flags |= MDB_NOOVERWRITE;
|
||||
|
||||
int rc = _mdbPut(txn, lmdbKey, lmdbData, flags);
|
||||
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, flags);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwDuplicateOrUnknown(rc, toString(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage (public transaction variant)
|
||||
*
|
||||
* This method schedules an addition of a key-value record, but doesn't immidiately adds it.
|
||||
* You can obtain LMDBAL::WriteTransaction calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* Take a note that if the storage already had a record you want to add LMDBAL::Exist is thrown.
|
||||
* If your storage doesn't support duplicates LMDBAL::Exist is thrown if the record with the same key already exists in the database.
|
||||
* If your storage supports duplicates LMDBAL::Exist is thrown only if the record with the same key <b>AND</b> already exists in the database.
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value value of the record
|
||||
* \param[in] txn transaction
|
||||
*
|
||||
* \exception LMDBAL::Exist thrown if the storage already has a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, const WriteTransaction& txn) {
|
||||
ensureOpened(addRecordMethodName);
|
||||
addRecord(key, value, extractTransactionId(txn, addRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage, overwrites if it already exists
|
||||
*
|
||||
@ -179,9 +160,11 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage, overwrites if it already exists (private transaction variant)
|
||||
* \brief Adds a key-value record to the storage, overwrites if it already exists (transaction variant)
|
||||
*
|
||||
* This method schedules an addition of a key-value record, but doesn't immidiately add it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
* If the record did already exist in the database the actual overwrite will be done only after calling LMDBAL::Base::commitTransaction().
|
||||
*
|
||||
* This method is mostly useless in duplicates mode.
|
||||
* In this mode it basically does the same thing LMDBAL::Storage::addRecord() does,
|
||||
@ -193,10 +176,13 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value) {
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
* \returns true if the record was added, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
|
||||
ensureOpened(forceRecordMethodName);
|
||||
|
||||
bool added;
|
||||
if (duplicates) {
|
||||
try {
|
||||
@ -209,7 +195,7 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
|
||||
MDB_val lmdbKey = keySerializer.setData(key);
|
||||
MDB_val lmdbData;
|
||||
|
||||
int rc = _mdbGet(txn, lmdbKey, lmdbData);
|
||||
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
switch (rc) {
|
||||
case MDB_SUCCESS:
|
||||
added = false;
|
||||
@ -223,40 +209,13 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
|
||||
}
|
||||
|
||||
lmdbData = valueSerializer.setData(value);
|
||||
rc = _mdbPut(txn, lmdbKey, lmdbData);
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage, overwrites if it already exists (public transaction variant)
|
||||
*
|
||||
* This method schedules an addition of a key-value record, but doesn't immidiately add it.
|
||||
* You can obtain LMDBAL::WriteTransaction calling LMDBAL::Base::beginTransaction().
|
||||
* If the record did already exist in the database the actual overwrite will be done only after calling LMDBAL::WriteTransaction::commit().
|
||||
*
|
||||
* This method is mostly useless in duplicates mode.
|
||||
* In this mode it basically does the same thing LMDBAL::Storage::addRecord() does,
|
||||
* but suppresses LMDBAL::Exist exception if the record with the same key-value pair existed in the storage.
|
||||
* In this case just false is returned from the method.
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value value of the record
|
||||
* \param[in] txn transaction
|
||||
* \returns true if the record was added, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, const WriteTransaction& txn) {
|
||||
ensureOpened(forceRecordMethodName);
|
||||
return forceRecord(key, value, extractTransactionId(txn, forceRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Changes key-value record to the storage.
|
||||
*
|
||||
@ -292,9 +251,11 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Changes key-value record to the storage (private transaction variant)
|
||||
* \brief Changes key-value record to the storage (transaction variant)
|
||||
*
|
||||
* This method schedules a modification of a key-value record, but doesn't immidiately changes it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* If duplicates mode is enabled this function will find the first entry of the key
|
||||
@ -309,18 +270,21 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Exist thrown in duplicates mode when the given value matches some of existing values for the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
|
||||
ensureOpened(changeRecordMethodName);
|
||||
|
||||
MDB_cursor* cursor;
|
||||
int rc = _mdbCursorOpen(txn, &cursor);
|
||||
int rc = mdb_cursor_open(txn, dbi, &cursor);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
|
||||
MDB_val lmdbKey = keySerializer.setData(key);
|
||||
MDB_val lmdbData;
|
||||
rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_SET);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_SET);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwNotFoundOrUnknown(rc, toString(key));
|
||||
|
||||
@ -330,56 +294,25 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, Transacti
|
||||
if (sameSize) { //can compare only if they are the same size
|
||||
firstDifferentByte = memcmp(lmdbData.mv_data, lmdbNewData.mv_data, lmdbData.mv_size);
|
||||
if (firstDifferentByte == 0) { //old and new is the same, nothing to do
|
||||
_mdbCursorClose(cursor);
|
||||
mdb_cursor_close(cursor);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int flags = MDB_CURRENT;
|
||||
if (duplicates && (!sameSize || firstDifferentByte < 0)) { //if new value is greater than the old one
|
||||
rc = _mdbCursorDel(cursor); //we need to initiate duplicates sort, for it to be in the correct place
|
||||
rc = mdb_cursor_del(cursor, 0); //we need to initiate duplicates sort, for it to be in the correct place
|
||||
flags = MDB_NODUPDATA;
|
||||
}
|
||||
|
||||
if (rc == MDB_SUCCESS)
|
||||
rc = _mdbCursorPut(cursor, lmdbKey, lmdbNewData, flags);
|
||||
rc = mdb_cursor_put(cursor, &lmdbKey, &lmdbNewData, flags);
|
||||
|
||||
_mdbCursorClose(cursor);
|
||||
mdb_cursor_close(cursor);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwDuplicateOrUnknown(rc, toString(key));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \brief Changes key-value record to the storage (public transaction variant)
|
||||
*
|
||||
* This method schedules a modification of a key-value record, but doesn't immidiately changes it.
|
||||
* You can obtain LMDBAL::WriteTransaction calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* If duplicates mode is enabled this function will find the first entry of the key
|
||||
* (which is pretty couterintuitive, see LMDBAL::Storage::getRecord() description)
|
||||
* and change it's value to the given one.
|
||||
* If the given value matches some of the other values for given key the method will throw LMDBAL::Exist,
|
||||
* if no key was found it will still throw LMDBAL::NotFound.
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value new value of the record
|
||||
* \param[in] txn transaction
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Exist thrown in duplicates mode when the given value matches some of existing values for the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, const WriteTransaction& txn) {
|
||||
ensureOpened(changeRecordMethodName);
|
||||
changeRecord(key, value, extractTransactionId(txn, changeRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database
|
||||
|
||||
@ -441,7 +374,10 @@ void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value) const {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (private transaction variant)
|
||||
* \brief Gets the record from the database (transaction variant)
|
||||
*
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to get LMDBAL::NotFound is thrown
|
||||
*
|
||||
@ -456,19 +392,22 @@ void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value) const {
|
||||
* \returns the value from the storage
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
V LMDBAL::Storage<K, V>::getRecord(const K& key, TransactionID txn) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
|
||||
V value;
|
||||
Storage<K, V>::getRecord(key, value, txn);
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (public transaction variant)
|
||||
* \brief Gets the record from the database (transaction, reference variant)
|
||||
*
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to get LMDBAL::NotFound is thrown
|
||||
@ -480,80 +419,27 @@ V LMDBAL::Storage<K, V>::getRecord(const K& key, TransactionID txn) const {
|
||||
* - if your values are anything else - they are compared byte by byte as if they are strings, it makes it especially complicated to predict the exact value for float or double templated storages. For strings if makes a bit more sence: if the choise is from "50" and "100" - "100" is returned, because the first byte of the "100" is lower than the first byte of the "50"
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
* \returns the value from the storage
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
V LMDBAL::Storage<K, V>::getRecord(const K& key, const Transaction& txn) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
return getRecord(key, extractTransactionId(txn, getRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (private transaction, reference variant)
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to get LMDBAL::NotFound is thrown
|
||||
*
|
||||
* If the storage supports duplicates the exact value returned from it depends on comparison function of lmdb.
|
||||
* It's not very straight forward, so, you shouldn't really use this method if you use duplicates and you rely on exact result.
|
||||
* Anyway:
|
||||
* - if your values are signed or unsigned integer of any size the LOWEST value is returned compared as <b>UNSIGNED</b>. For example for storage with int32_t as value, from the same key, from the set of values {-33, -1, 5573, 77753} 5573 is returned as it is the lowest by <b>UNSIGNED</b> comparison.
|
||||
* - if your values are anything else - they are compared byte by byte as if they are strings, it makes it especially complicated to predict the exact value for float or double templated storages. For strings if makes a bit more sence: if the choise is from "50" and "100" - "100" is returned, because the first byte of the "100" is lower than the first byte of the "50"
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[out] value the value from the storage
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
|
||||
MDB_val lmdbKey = keySerializer.setData(key);
|
||||
MDB_val lmdbData;
|
||||
|
||||
int rc = _mdbGet(txn, lmdbKey, lmdbData);
|
||||
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwNotFoundOrUnknown(rc, toString(key));
|
||||
|
||||
valueSerializer.deserialize(lmdbData, value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (public transaction, reference variant)
|
||||
*
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to get LMDBAL::NotFound is thrown
|
||||
*
|
||||
* If the storage supports duplicates the exact value returned from it depends on comparison function of lmdb.
|
||||
* It's not very straight forward, so, you shouldn't really use this method if you use duplicates and you rely on exact result.
|
||||
* Anyway:
|
||||
* - if your values are signed or unsigned integer of any size the LOWEST value is returned compared as <b>UNSIGNED</b>. For example for storage with int32_t as value, from the same key, from the set of values {-33, -1, 5573, 77753} 5573 is returned as it is the lowest by <b>UNSIGNED</b> comparison.
|
||||
* - if your values are anything else - they are compared byte by byte as if they are strings, it makes it especially complicated to predict the exact value for float or double templated storages. For strings if makes a bit more sence: if the choise is from "50" and "100" - "100" is returned, because the first byte of the "100" is lower than the first byte of the "50"
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[out] value the value from the storage
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, const Transaction& txn) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
getRecord(key, value, extractTransactionId(txn, getRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Chechs if storage has value
|
||||
*
|
||||
@ -581,20 +467,26 @@ bool LMDBAL::Storage<K, V>::checkRecord(const K& key) const {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Chechs if storage has value (private transaction variant)
|
||||
* \brief Chechs if storage has value (transaction variant)
|
||||
*
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
* \returns true if there was a record with given key, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const {
|
||||
ensureOpened(checkRecordMethodName);
|
||||
|
||||
MDB_val lmdbKey = keySerializer.setData(key);
|
||||
MDB_val lmdbData;
|
||||
|
||||
int rc = _mdbGet(txn, lmdbKey, lmdbData);
|
||||
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
if (rc == MDB_SUCCESS)
|
||||
return true;
|
||||
|
||||
@ -604,26 +496,6 @@ bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Chechs if storage has value (public transaction variant)
|
||||
*
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[in] txn transaction, can be read only transaction
|
||||
* \returns true if there was a record with given key, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::checkRecord(const K& key, const Transaction& txn) const {
|
||||
ensureOpened(checkRecordMethodName);
|
||||
return checkRecord(key, extractTransactionId(txn, checkRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reads whole storage into a map
|
||||
*
|
||||
@ -673,25 +545,7 @@ void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result) const {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reads whole storage into a map (private transaction variant)
|
||||
*
|
||||
* Basically just reads all database in an std::map, usefull when you store small storages
|
||||
* In case storage supports duplicates <b>only what lmdb considered to be lowest value</b>
|
||||
* (see LMDBAL::Storage::getRecord() description) is returned in the resulting map
|
||||
*
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
std::map<K, V> LMDBAL::Storage<K, V>::readAll(TransactionID txn) const {
|
||||
std::map<K, V> result;
|
||||
Storage<K, V>::readAll(result, txn);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reads whole storage into a map (public transaction variant)
|
||||
* \brief Reads whole storage into a map (transaction variant)
|
||||
*
|
||||
* Basically just reads all database in an std::map, usefull when you store small storages
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
@ -700,40 +554,48 @@ std::map<K, V> LMDBAL::Storage<K, V>::readAll(TransactionID txn) const {
|
||||
* In case storage supports duplicates <b>only what lmdb considered to be lowest value</b>
|
||||
* (see LMDBAL::Storage::getRecord() description) is returned in the resulting map
|
||||
*
|
||||
* \param[in] txn transaction, can be read only transaction
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
std::map<K, V> LMDBAL::Storage<K, V>::readAll(const Transaction& txn) const {
|
||||
std::map<K, V> LMDBAL::Storage<K, V>::readAll(TransactionID txn) const {
|
||||
ensureOpened(readAllMethodName);
|
||||
return readAll(extractTransactionId(txn, readAllMethodName));
|
||||
|
||||
std::map<K, V> result;
|
||||
Storage<K, V>::readAll(result, txn);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reads whole storage into a map (private transaction, reference variant)
|
||||
* \brief Reads whole storage into a map (transaction, reference variant)
|
||||
*
|
||||
* Basically just reads all database in an std::map, usefull when you store small storages
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* In case storage supports duplicates <b>only what lmdb considered to be lowest value</b>
|
||||
* (see LMDBAL::Storage::getRecord() description) is returned in the resulting map
|
||||
*
|
||||
* \param[out] result a map that is going to contain all data
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) const {
|
||||
ensureOpened(readAllMethodName);
|
||||
|
||||
MDB_cursor* cursor;
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
|
||||
int rc = _mdbCursorOpen(txn, &cursor);
|
||||
int rc = mdb_cursor_open(txn, dbi, &cursor);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
|
||||
rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_FIRST);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
|
||||
while (rc == MDB_SUCCESS) {
|
||||
K key;
|
||||
keySerializer.deserialize(lmdbKey, key);
|
||||
@ -741,36 +603,13 @@ void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) c
|
||||
if (probe.second) //I do this to avoid overwrites in case duplicates are enabled
|
||||
valueSerializer.deserialize(lmdbData, probe.first->second);
|
||||
|
||||
rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_NEXT);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT);
|
||||
}
|
||||
_mdbCursorClose(cursor);
|
||||
mdb_cursor_close(cursor);
|
||||
if (rc != MDB_NOTFOUND)
|
||||
throwUnknown(rc);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reads whole storage into a map (public transaction, reference variant)
|
||||
*
|
||||
* Basically just reads all database in an std::map, usefull when you store small storages
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* In case storage supports duplicates <b>only what lmdb considered to be lowest value</b>
|
||||
* (see LMDBAL::Storage::getRecord() description) is returned in the resulting map
|
||||
*
|
||||
* \param[out] result a map that is going to contain all data
|
||||
* \param[in] txn transaction, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, const Transaction& txn) const {
|
||||
ensureOpened(readAllMethodName);
|
||||
readAll(result, extractTransactionId(txn, readAllMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Replaces the content of the whole storage with the given
|
||||
*
|
||||
@ -797,17 +636,22 @@ void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Replaces the content of the whole storage with the given (private transaction variant)
|
||||
* \brief Replaces the content of the whole storage with the given (transaction variant)
|
||||
*
|
||||
* Basically this method drops the database and adds all the records from the given map
|
||||
* This method schedules a data replacement, but doesn't immidiately execute it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* \param[in] data new data of the storage
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data, TransactionID txn) {
|
||||
ensureOpened(replaceAllMethodName);
|
||||
|
||||
int rc = drop(txn);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
@ -817,32 +661,12 @@ void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data, TransactionID
|
||||
lmdbKey = keySerializer.setData(pair.first);
|
||||
lmdbData = valueSerializer.setData(pair.second);
|
||||
|
||||
rc = _mdbPut(txn, lmdbKey, lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here?
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here?
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Replaces the content of the whole storage with the given (public transaction variant)
|
||||
*
|
||||
* Basically this method drops the database and adds all the records from the given map
|
||||
* This method schedules a data replacement, but doesn't immidiately execute it.
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* \param[in] data new data of the storage
|
||||
* \param[in] txn transaction
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data, const WriteTransaction& txn) {
|
||||
ensureOpened(replaceAllMethodName);
|
||||
replaceAll(data, extractTransactionId(txn, replaceAllMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds records in bulk
|
||||
*
|
||||
@ -872,27 +696,31 @@ uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, bool over
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds records in bulk (private transaction variant)
|
||||
* \brief Adds records in bulk (transaction variant)
|
||||
*
|
||||
* This method schedules a data addition, but doesn't immidiately execute it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* \param[in] data the data to be added
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
* \param[in] overwrite if false method throws LMDBAL::Exist on repeated key, if true - overwrites it
|
||||
* \returns new actual amount of records in the storage
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Exist thrown if overwrite==false and at least one of the keys of data already exists in the storage
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite) {
|
||||
ensureOpened(addRecordsMethodName);
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
int rc;
|
||||
for (const std::pair<const K, V>& pair : data) {
|
||||
lmdbKey = keySerializer.setData(pair.first);
|
||||
lmdbData = valueSerializer.setData(pair.second);
|
||||
|
||||
rc = _mdbPut(txn, lmdbKey, lmdbData, overwrite ? 0 : MDB_NOOVERWRITE);
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE);
|
||||
if (rc == MDB_KEYEXIST)
|
||||
throwDuplicate(toString(pair.first));
|
||||
|
||||
@ -901,35 +729,13 @@ uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, Transacti
|
||||
}
|
||||
|
||||
MDB_stat stat;
|
||||
rc = _mdbStat(txn, stat);
|
||||
rc = mdb_stat(txn, dbi, &stat);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
|
||||
return stat.ms_entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds records in bulk (public transaction variant)
|
||||
*
|
||||
* This method schedules a data addition, but doesn't immidiately execute it.
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* \param[in] data the data to be added
|
||||
* \param[in] txn transaction
|
||||
* \param[in] overwrite if false method throws LMDBAL::Exist on repeated key, if true - overwrites it
|
||||
* \returns new actual amount of records in the storage
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Exist thrown if overwrite==false and at least one of the keys of data already exists in the storage
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, const WriteTransaction& txn, bool overwrite) {
|
||||
ensureOpened(addRecordsMethodName);
|
||||
return addRecords(data, extractTransactionId(txn, addRecordsMethodName), overwrite);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Removes one of the records
|
||||
*
|
||||
@ -957,46 +763,29 @@ void LMDBAL::Storage<K, V>::removeRecord(const K& key) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Removes one of the records (private transaction variant)
|
||||
* \brief Removes one of the records (transaction variant)
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to remove LMDBAL::NotFound is thrown
|
||||
* This method schedules a record removal, but doesn't immidiately execute it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* \param[in] key key of the record you wish to be removed
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::NotFound thrown if the record with given key wasn't found
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) {
|
||||
ensureOpened(removeRecordMethodName);
|
||||
|
||||
MDB_val lmdbKey = keySerializer.setData(key);
|
||||
int rc = _mdbDel(txn, lmdbKey);
|
||||
int rc = mdb_del(txn, dbi, &lmdbKey, NULL);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwNotFoundOrUnknown(rc, toString(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Removes one of the records (transaction variant)
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to remove LMDBAL::NotFound is thrown
|
||||
* This method schedules a record removal, but doesn't immidiately execute it.
|
||||
* You can obtain LMDBAL::Transaction calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* \param[in] key key of the record you wish to be removed
|
||||
* \param[in] txn transaction ID
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::NotFound thrown if the record with given key wasn't found
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::removeRecord(const K& key, const WriteTransaction& txn) {
|
||||
ensureOpened(removeRecordMethodName);
|
||||
removeRecord(key, extractTransactionId(txn, removeRecordMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief A private virtual method I need to open each storage in the database
|
||||
*
|
||||
@ -1013,8 +802,8 @@ int LMDBAL::Storage<K, V>::open(MDB_txn* transaction) {
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::close() {
|
||||
for (const std::pair<const uint32_t, Cursor<K, V>*>& pair : cursors)
|
||||
pair.second->terminated();
|
||||
for (Cursor<K, V>* cursor : cursors)
|
||||
cursor->terminated();
|
||||
|
||||
iStorage::close();
|
||||
}
|
||||
@ -1022,37 +811,14 @@ void LMDBAL::Storage<K, V>::close() {
|
||||
/**
|
||||
* \brief Creates cursor
|
||||
*
|
||||
* This is a legitimate way to aquire cursor to a storage.
|
||||
* Aquired cursor is RAII safe, automatic destructor will free everything it occupied.
|
||||
*
|
||||
* \returns LMDBAL::Cursor for this storage and returs you a pointer to a created cursor
|
||||
*/
|
||||
template<class K, class V>
|
||||
LMDBAL::Cursor<K, V> LMDBAL::Storage<K, V>::createCursor() {
|
||||
return Cursor<K, V>(this);
|
||||
}
|
||||
LMDBAL::Cursor<K, V>* LMDBAL::Storage<K, V>::createCursor() {
|
||||
Cursor<K, V>* cursor = new Cursor<K, V>(this);
|
||||
cursors.insert(cursor);
|
||||
|
||||
/**
|
||||
* \brief Frees cursor
|
||||
*
|
||||
* This is a legitimate way to free cursor.
|
||||
* You don't actually need to do it manually,
|
||||
* you can just reassign cursor, let it be destroyed by leaving the scope
|
||||
* or call LMDBAL::Cursor<K, V>::drop, but you may if you wish.
|
||||
*
|
||||
* \param[in] cursor cursor you wish to destroy
|
||||
*
|
||||
* \exception LMDBAL::Unknown thrown if you try to destroy a cursor this storage didn't create
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::destroyCursor(LMDBAL::Cursor<K, V>& cursor) {
|
||||
typename std::map<uint32_t, Cursor<K, V>*>::iterator itr = cursors.find(cursor.id);
|
||||
if (itr == cursors.end())
|
||||
throwUnknown("An attempt to destroy a cursor the storage doesn't own");
|
||||
|
||||
cursor.close();
|
||||
cursors.erase(itr);
|
||||
cursor.freed();
|
||||
return cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1071,7 +837,7 @@ uint32_t LMDBAL::Storage<K, V>::flags() const {
|
||||
uint32_t result;
|
||||
TransactionID txn = beginReadOnlyTransaction();
|
||||
|
||||
int res = _mdbFlags(txn, result);
|
||||
int res = mdb_dbi_flags(txn, dbi, &result);
|
||||
abortTransaction(txn);
|
||||
if (res != MDB_SUCCESS)
|
||||
throwUnknown(res);
|
||||
@ -1079,6 +845,25 @@ uint32_t LMDBAL::Storage<K, V>::flags() const {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Destroys cursor
|
||||
*
|
||||
* This a normal way to discard a cursor you don't need anymore
|
||||
*
|
||||
* \param[in] cursor a pointer to a cursor you want to destroy
|
||||
*
|
||||
* \exception LMDBAL::Unknown thrown if you try to destroy something that this storage didn't create
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::destroyCursor(Cursor<K, V>* cursor) {
|
||||
typename std::set<Cursor<K, V>*>::const_iterator itr = cursors.find(cursor);
|
||||
if (itr == cursors.end())
|
||||
throwUnknown("An attempt to destroy a cursor the storage doesn't own");
|
||||
|
||||
cursors.erase(itr);
|
||||
delete cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief A private virtual method that cursor calls when he reads a record, does nothing here but populates the LMDBAL::Cache
|
||||
*
|
||||
@ -1140,7 +925,7 @@ inline int LMDBAL::iStorage::makeStorage(MDB_txn* transaction, bool duplicates)
|
||||
flags |= MDB_INTEGERDUP;
|
||||
}
|
||||
|
||||
return _mdbOpen(transaction, flags);
|
||||
return mdb_dbi_open(transaction, name.c_str(), flags, &dbi);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,154 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
#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);
|
||||
};
|
||||
|
||||
}
|
@ -12,8 +12,9 @@ add_executable(runUnitTests
|
||||
duplicates.cpp
|
||||
)
|
||||
|
||||
target_compile_options(runUnitTests PRIVATE -fPIC -Wall -Wextra -O0)
|
||||
target_compile_options(runUnitTests PRIVATE -fPIC)
|
||||
|
||||
target_include_directories(runUnitTests PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
|
||||
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
|
||||
|
||||
@ -22,6 +23,7 @@ target_link_libraries(
|
||||
GTest::gtest_main
|
||||
${PROJECT_NAME}
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
lmdb
|
||||
)
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(runUnitTests)
|
||||
|
@ -113,9 +113,10 @@ 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(140, "whatever");
|
||||
c1->addRecord(-37, "aaaaa tss tsss tsss tsss aaaaaaa");
|
||||
EXPECT_EQ(c1->getRecord(40), "whatever");
|
||||
EXPECT_EQ(c1->getRecord(140), "whatever");
|
||||
EXPECT_EQ(c1->getRecord(-116), "whatever");
|
||||
}
|
||||
|
||||
TEST_F(BaseTest, AddingKeysToVariableCache) {
|
||||
@ -188,8 +189,8 @@ TEST_F(BaseTest, Persistence) {
|
||||
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(-116), true);
|
||||
EXPECT_EQ(c1->getRecord(-116), "whatever");
|
||||
EXPECT_EQ(c1->checkRecord(-4), true);
|
||||
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
|
||||
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
|
||||
|
@ -23,30 +23,28 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
int getCacheCursorsSize() const {
|
||||
return cache->cursors.size();
|
||||
}
|
||||
|
||||
static void TearDownTestSuite() {
|
||||
cursor.drop();
|
||||
transaction.terminate();
|
||||
db->close();
|
||||
db->removeDirectory();
|
||||
delete db;
|
||||
db = nullptr;
|
||||
cursor = nullptr;
|
||||
emptyCursor = nullptr;
|
||||
}
|
||||
|
||||
static LMDBAL::Base* db;
|
||||
static LMDBAL::Cursor<uint64_t, std::string> cursor;
|
||||
static LMDBAL::Transaction transaction;
|
||||
static LMDBAL::Cursor<uint64_t, std::string>* cursor;
|
||||
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor;
|
||||
static LMDBAL::TransactionID 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;
|
||||
LMDBAL::Cursor<uint64_t, std::string>* CacheCursorTest::cursor = nullptr;
|
||||
LMDBAL::Cursor<uint64_t, std::string>* CacheCursorTest::emptyCursor = nullptr;
|
||||
LMDBAL::TransactionID CacheCursorTest::transaction = nullptr;
|
||||
|
||||
static const std::map<uint64_t, std::string> data({
|
||||
{245665783, "bothering nerds"},
|
||||
@ -67,24 +65,22 @@ TEST_F(CacheCursorTest, PopulatingTheTable) {
|
||||
}
|
||||
|
||||
TEST_F(CacheCursorTest, Creation) {
|
||||
EXPECT_EQ(getCacheCursorsSize(), 0);
|
||||
cursor = cache->createCursor();
|
||||
EXPECT_EQ(getCacheCursorsSize(), 1);
|
||||
emptyCursor = emptyCache->createCursor();
|
||||
|
||||
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);
|
||||
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();
|
||||
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::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);
|
||||
@ -93,21 +89,20 @@ TEST_F(CacheCursorTest, FirstPrivate) {
|
||||
}
|
||||
|
||||
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();
|
||||
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_THROW(cursor->next(), LMDBAL::NotFound);
|
||||
EXPECT_EQ(cache->count(), data.size());
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
std::pair<uint64_t, std::string> element = cursor->first();
|
||||
reference = data.begin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -116,10 +111,9 @@ TEST_F(CacheCursorTest, NextPrivate) {
|
||||
}
|
||||
|
||||
TEST_F(CacheCursorTest, LastPrivate) {
|
||||
EXPECT_EQ(getCacheCursorsSize(), 1);
|
||||
EXPECT_EQ(cache->count(), data.size());
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
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);
|
||||
@ -128,21 +122,20 @@ TEST_F(CacheCursorTest, LastPrivate) {
|
||||
}
|
||||
|
||||
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();
|
||||
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_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||
EXPECT_EQ(cache->count(), data.size());
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
std::pair<uint64_t, std::string> element = cursor->last();
|
||||
reference = data.rbegin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -151,30 +144,29 @@ TEST_F(CacheCursorTest, PrevPrivate) {
|
||||
}
|
||||
|
||||
TEST_F(CacheCursorTest, CurrentPrivate) {
|
||||
EXPECT_EQ(getCacheCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
cursor->next();
|
||||
cursor->next();
|
||||
cursor->prev();
|
||||
element = cursor->current();
|
||||
++reference;
|
||||
++reference;
|
||||
--reference;
|
||||
@ -184,62 +176,26 @@ TEST_F(CacheCursorTest, CurrentPrivate) {
|
||||
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);
|
||||
cursor->close();
|
||||
|
||||
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);
|
||||
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);
|
||||
EXPECT_THROW(emptyCache->destroyCursor(cursor), LMDBAL::Unknown);
|
||||
cache->destroyCursor(cursor);
|
||||
|
||||
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();
|
||||
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);
|
||||
@ -248,21 +204,20 @@ TEST_F(CacheCursorTest, FirstPublic) {
|
||||
}
|
||||
|
||||
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();
|
||||
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_THROW(cursor->next(), LMDBAL::NotFound);
|
||||
EXPECT_EQ(cache->count(), data.size());
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
std::pair<uint64_t, std::string> element = cursor->first();
|
||||
reference = data.begin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -270,8 +225,7 @@ TEST_F(CacheCursorTest, NextPublic) {
|
||||
}
|
||||
|
||||
TEST_F(CacheCursorTest, LastPublic) {
|
||||
EXPECT_EQ(getCacheCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
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);
|
||||
@ -280,20 +234,19 @@ TEST_F(CacheCursorTest, LastPublic) {
|
||||
}
|
||||
|
||||
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();
|
||||
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_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
std::pair<uint64_t, std::string> element = cursor->last();
|
||||
reference = data.rbegin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -302,30 +255,29 @@ TEST_F(CacheCursorTest, PrevPublic) {
|
||||
}
|
||||
|
||||
TEST_F(CacheCursorTest, CurrentPublic) {
|
||||
EXPECT_EQ(getCacheCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
cursor->next();
|
||||
cursor->next();
|
||||
cursor->prev();
|
||||
element = cursor->current();
|
||||
++reference;
|
||||
++reference;
|
||||
--reference;
|
||||
@ -335,106 +287,40 @@ TEST_F(CacheCursorTest, CurrentPublic) {
|
||||
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();
|
||||
db->abortTransaction(transaction);
|
||||
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();
|
||||
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
|
||||
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());
|
||||
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();
|
||||
element = cursor->current();
|
||||
EXPECT_EQ(element.first, breference->first);
|
||||
EXPECT_EQ(element.second, breference->second);
|
||||
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
|
||||
cursor.close();
|
||||
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
|
||||
cursor->close();
|
||||
|
||||
cursor.open();
|
||||
element = cursor.next();
|
||||
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();
|
||||
element = cursor->current();
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
EXPECT_EQ(element.second, reference->second);
|
||||
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
|
||||
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ TEST_F(CacheTransactionsTest, Adding) {
|
||||
EXPECT_EQ(c1->count(), 0);
|
||||
EXPECT_EQ(c2->count(), 0);
|
||||
|
||||
LMDBAL::WriteTransaction txn = db->beginTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||
c1->addRecord(5, 13, txn);
|
||||
c1->addRecord(-53, 782, txn);
|
||||
c1->addRecord(5892, -37829, txn);
|
||||
@ -76,7 +76,7 @@ TEST_F(CacheTransactionsTest, Adding) {
|
||||
EXPECT_EQ(c1->count(), 0);
|
||||
EXPECT_EQ(c2->count(), 0);
|
||||
|
||||
txn.commit();
|
||||
db->commitTransaction(txn);
|
||||
|
||||
EXPECT_EQ(c1->count(), 3);
|
||||
EXPECT_EQ(c1->getRecord(5), 13);
|
||||
@ -95,7 +95,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
|
||||
LMDBAL::SizeType s1 = c1->count();
|
||||
LMDBAL::SizeType s2 = c2->count();
|
||||
|
||||
LMDBAL::WriteTransaction txn = db->beginTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||
c1->addRecord(18, 40, txn);
|
||||
c1->addRecord(85, -4, txn);
|
||||
c1->addRecord(-5, -3, txn);
|
||||
@ -107,7 +107,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
|
||||
EXPECT_EQ(c1->count(), s1);
|
||||
EXPECT_EQ(c2->count(), s2);
|
||||
|
||||
txn.abort();
|
||||
db->abortTransaction(txn);
|
||||
|
||||
EXPECT_EQ(c1->count(), s1);
|
||||
EXPECT_EQ(c2->count(), s2);
|
||||
@ -116,7 +116,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
|
||||
TEST_F(CacheTransactionsTest, Reading) {
|
||||
EXPECT_EQ(db->ready(), true);
|
||||
|
||||
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginReadOnlyTransaction();
|
||||
|
||||
EXPECT_EQ(c1->count(txn), 3);
|
||||
EXPECT_EQ(c1->getRecord(5, txn), 13);
|
||||
@ -128,14 +128,14 @@ TEST_F(CacheTransactionsTest, Reading) {
|
||||
EXPECT_FLOAT_EQ(c2->getRecord("decallence", txn), 8532.48);
|
||||
EXPECT_FLOAT_EQ(c2->getRecord("prevent recovery", txn), -64.64);
|
||||
|
||||
txn.terminate();
|
||||
db->abortTransaction(txn);
|
||||
}
|
||||
|
||||
TEST_F(CacheTransactionsTest, ConcurentReading) {
|
||||
EXPECT_EQ(db->ready(), true);
|
||||
|
||||
LMDBAL::SizeType size = c1->count();
|
||||
LMDBAL::WriteTransaction txn = db->beginTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||
EXPECT_EQ(c1->getRecord(5, txn), 13);
|
||||
EXPECT_EQ(c1->getRecord(5), 13);
|
||||
|
||||
@ -160,7 +160,7 @@ TEST_F(CacheTransactionsTest, ConcurentReading) {
|
||||
EXPECT_EQ(c1->count(txn), 1);
|
||||
EXPECT_EQ(c1->count(), size);
|
||||
|
||||
txn.commit();
|
||||
db->commitTransaction(txn);
|
||||
|
||||
EXPECT_FALSE(c1->checkRecord(5));
|
||||
EXPECT_EQ(c1->count(), 1);
|
||||
@ -182,9 +182,8 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
|
||||
|
||||
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
|
||||
LMDBAL::TransactionID 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);
|
||||
@ -199,13 +198,13 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
|
||||
EXPECT_EQ(c1->getRecord(5), 812);
|
||||
|
||||
std::cout << "commiting second transaction" << std::endl;
|
||||
txn2.commit();
|
||||
db->commitTransaction(txn2);
|
||||
|
||||
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();
|
||||
LMDBAL::TransactionID txn1 = db->beginTransaction();
|
||||
|
||||
std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
|
||||
usleep(5);
|
||||
@ -220,7 +219,7 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
|
||||
EXPECT_FALSE(c1->checkRecord(5));
|
||||
|
||||
std::cout << "commiting first transaction" << std::endl;
|
||||
txn1.commit();
|
||||
db->commitTransaction(txn1);
|
||||
|
||||
std::cout << "waiting for the other thread to finish" << std::endl;
|
||||
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
|
||||
@ -230,52 +229,3 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -130,10 +130,7 @@ TEST_F(DuplicatesTest, Adding) {
|
||||
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);
|
||||
|
||||
EXPECT_EQ(tu3->getRecord(7.2), -113);
|
||||
float tu3dd = tu3->getRecord(5119);
|
||||
EXPECT_TRUE(tu3ds == tu3dd);
|
||||
EXPECT_EQ(tu3ds, tu3dd);
|
||||
@ -147,11 +144,8 @@ TEST_F(DuplicatesTest, Adding) {
|
||||
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);
|
||||
EXPECT_EQ(tu4->getRecord(172), 0.00000001);
|
||||
EXPECT_EQ(tu4->getRecord(327), 463.28348); //since they are not int's they are compared sort of lexicographically
|
||||
|
||||
tu5->addRecord(-84.7, 45656753);
|
||||
EXPECT_THROW(tu5->addRecord(-84.7, 45656753), LMDBAL::Exist);
|
||||
@ -180,19 +174,13 @@ TEST_F(DuplicatesTest, Forcing) {
|
||||
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_EQ(tu1->getRecord(-56), 274); //like yeah, it's really counterintuitive, since it's compared byte by byte
|
||||
EXPECT_TRUE(tu1->forceRecord(-56, 14));
|
||||
EXPECT_EQ(tu1->count(), tu1Size += 1);
|
||||
EXPECT_EQ(res56.count(tu1->getRecord(-56)), 1);
|
||||
|
||||
EXPECT_EQ(tu1->getRecord(-56), 14);
|
||||
EXPECT_FALSE(tu1->forceRecord(-56, 274));
|
||||
EXPECT_EQ(tu1->count(), tu1Size);
|
||||
|
||||
@ -213,38 +201,26 @@ TEST_F(DuplicatesTest, Forcing) {
|
||||
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->getRecord(17.3), 105.1f); //here too, really one should not use this function with duplicates,
|
||||
EXPECT_TRUE(tu3->forceRecord(17.3, 5.1)); //unless he wishes for kinda randomish result
|
||||
EXPECT_EQ(tu3->count(), tu3Size += 1);
|
||||
EXPECT_EQ(res17.count(tu3->getRecord(17.3)), 1);
|
||||
|
||||
EXPECT_EQ(tu3->getRecord(17.3), 5.1f);
|
||||
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_EQ(tu4->getRecord(84), 2879.654);
|
||||
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_EQ(tu4->getRecord(84), 2679.5);
|
||||
EXPECT_FALSE(tu4->forceRecord(84, -359.109));
|
||||
EXPECT_EQ(tu4->count(), tu4Size);
|
||||
|
||||
@ -310,7 +286,6 @@ TEST_F(DuplicatesTest, Changing) {
|
||||
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);
|
||||
@ -319,13 +294,11 @@ TEST_F(DuplicatesTest, Changing) {
|
||||
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);
|
||||
EXPECT_EQ(tu3->getRecord(26.7), 23.18f);
|
||||
tu3->changeRecord(26.7, 21.7);
|
||||
EXPECT_EQ(tu3->count(), tu3Size);
|
||||
EXPECT_EQ(tu3->getRecord(26.7), 21.7f);
|
||||
@ -343,40 +316,36 @@ TEST_F(DuplicatesTest, Changing) {
|
||||
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);
|
||||
EXPECT_EQ(tu4->getRecord(852), 13.54);
|
||||
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_EQ(tu4->getRecord(852), 213.85);
|
||||
EXPECT_THROW(tu4->changeRecord(852, 46324.1135), LMDBAL::Exist);
|
||||
}
|
||||
|
||||
TEST_F(DuplicatesTest, GettingAllRecords) {
|
||||
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
|
||||
LMDBAL::TransactionID 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();
|
||||
LMDBAL::Cursor<int16_t, uint16_t>* c1 = tu1->createCursor();
|
||||
tu1->readAll(m1, txn);
|
||||
c1.open(txn);
|
||||
c1->open(txn);
|
||||
|
||||
cycle = false;
|
||||
iterations = 0;
|
||||
do {
|
||||
try {
|
||||
std::pair<int16_t, uint16_t> pair = c1.next();
|
||||
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) {
|
||||
@ -390,25 +359,25 @@ TEST_F(DuplicatesTest, GettingAllRecords) {
|
||||
cycle = false;
|
||||
}
|
||||
} while (cycle);
|
||||
tu1->destroyCursor(c1);
|
||||
|
||||
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();
|
||||
LMDBAL::Cursor<std::string, int8_t>* c2 = tu2->createCursor();
|
||||
tu2->readAll(m2, txn);
|
||||
c2.open(txn);
|
||||
c2->open(txn);
|
||||
|
||||
cycle = false;
|
||||
iterations = 0;
|
||||
do {
|
||||
try {
|
||||
std::pair<std::string, int8_t> pair = c2.next();
|
||||
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) {
|
||||
@ -422,25 +391,25 @@ TEST_F(DuplicatesTest, GettingAllRecords) {
|
||||
cycle = false;
|
||||
}
|
||||
} while (cycle);
|
||||
tu2->destroyCursor(c2);
|
||||
|
||||
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();
|
||||
LMDBAL::Cursor<float, float>* c3 = tu3->createCursor();
|
||||
tu3->readAll(m3, txn);
|
||||
c3.open(txn);
|
||||
c3->open(txn);
|
||||
|
||||
cycle = false;
|
||||
iterations = 0;
|
||||
do {
|
||||
try {
|
||||
std::pair<float, float> pair = c3.next();
|
||||
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) {
|
||||
@ -454,25 +423,25 @@ TEST_F(DuplicatesTest, GettingAllRecords) {
|
||||
cycle = false;
|
||||
}
|
||||
} while (cycle);
|
||||
tu3->destroyCursor(c3);
|
||||
|
||||
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();
|
||||
LMDBAL::Cursor<uint16_t, double>* c4 = tu4->createCursor();
|
||||
tu4->readAll(m4, txn);
|
||||
c4.open(txn);
|
||||
c4->open(txn);
|
||||
|
||||
cycle = false;
|
||||
iterations = 0;
|
||||
do {
|
||||
try {
|
||||
std::pair<uint16_t, double> pair = c4.next();
|
||||
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) {
|
||||
@ -486,12 +455,13 @@ TEST_F(DuplicatesTest, GettingAllRecords) {
|
||||
cycle = false;
|
||||
}
|
||||
} while (cycle);
|
||||
c4.drop();
|
||||
tu4->destroyCursor(c4);
|
||||
|
||||
EXPECT_EQ(iterations, tu4->count(txn));
|
||||
EXPECT_EQ(k4.size(), m4.size());
|
||||
EXPECT_NE(iterations, 0);
|
||||
EXPECT_NE(k4.size(), 0);
|
||||
|
||||
txn.terminate();
|
||||
|
||||
db->abortTransaction(txn);
|
||||
}
|
||||
|
@ -22,30 +22,28 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
int getTableCursorsSize() const {
|
||||
return table->cursors.size();
|
||||
}
|
||||
|
||||
static void TearDownTestSuite() {
|
||||
cursor.drop();
|
||||
transaction.terminate();
|
||||
db->close();
|
||||
db->removeDirectory();
|
||||
delete db;
|
||||
db = nullptr;
|
||||
cursor = nullptr;
|
||||
emptyCursor = nullptr;
|
||||
}
|
||||
|
||||
static LMDBAL::Base* db;
|
||||
static LMDBAL::Cursor<uint64_t, std::string> cursor;
|
||||
static LMDBAL::Transaction transaction;
|
||||
static LMDBAL::Cursor<uint64_t, std::string>* cursor;
|
||||
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor;
|
||||
static LMDBAL::TransactionID 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();
|
||||
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::cursor = nullptr;
|
||||
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::emptyCursor = nullptr;
|
||||
LMDBAL::TransactionID StorageCursorTest::transaction = nullptr;
|
||||
|
||||
static const std::map<uint64_t, std::string> data({
|
||||
{245665783, "bothering nerds"},
|
||||
@ -66,22 +64,20 @@ TEST_F(StorageCursorTest, PopulatingTheTable) {
|
||||
}
|
||||
|
||||
TEST_F(StorageCursorTest, Creation) {
|
||||
EXPECT_EQ(getTableCursorsSize(), 0);
|
||||
cursor = table->createCursor();
|
||||
EXPECT_EQ(getTableCursorsSize(), 1);
|
||||
emptyCursor = emptyTable->createCursor();
|
||||
|
||||
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);
|
||||
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();
|
||||
cursor->open();
|
||||
}
|
||||
|
||||
TEST_F(StorageCursorTest, FirstPrivate) {
|
||||
EXPECT_EQ(getTableCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
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);
|
||||
@ -89,19 +85,18 @@ TEST_F(StorageCursorTest, FirstPrivate) {
|
||||
}
|
||||
|
||||
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();
|
||||
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);
|
||||
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
std::pair<uint64_t, std::string> element = cursor->first();
|
||||
reference = data.begin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -109,8 +104,7 @@ TEST_F(StorageCursorTest, NextPrivate) {
|
||||
}
|
||||
|
||||
TEST_F(StorageCursorTest, LastPrivate) {
|
||||
EXPECT_EQ(getTableCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
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);
|
||||
@ -118,19 +112,18 @@ TEST_F(StorageCursorTest, LastPrivate) {
|
||||
}
|
||||
|
||||
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();
|
||||
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);
|
||||
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
std::pair<uint64_t, std::string> element = cursor->last();
|
||||
reference = data.rbegin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -138,29 +131,28 @@ TEST_F(StorageCursorTest, PrevPrivate) {
|
||||
}
|
||||
|
||||
TEST_F(StorageCursorTest, CurrentPrivate) {
|
||||
EXPECT_EQ(getTableCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
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();
|
||||
element = cursor->current();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
EXPECT_EQ(element.second, reference->second);
|
||||
|
||||
cursor.next();
|
||||
element = cursor.current();
|
||||
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();
|
||||
cursor->next();
|
||||
cursor->next();
|
||||
cursor->prev();
|
||||
element = cursor->current();
|
||||
++reference;
|
||||
++reference;
|
||||
--reference;
|
||||
@ -169,62 +161,26 @@ TEST_F(StorageCursorTest, CurrentPrivate) {
|
||||
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);
|
||||
cursor->close();
|
||||
|
||||
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);
|
||||
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);
|
||||
EXPECT_THROW(emptyTable->destroyCursor(cursor), LMDBAL::Unknown);
|
||||
table->destroyCursor(cursor);
|
||||
|
||||
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();
|
||||
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);
|
||||
@ -232,19 +188,18 @@ TEST_F(StorageCursorTest, FirstPublic) {
|
||||
}
|
||||
|
||||
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();
|
||||
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);
|
||||
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
std::pair<uint64_t, std::string> element = cursor->first();
|
||||
reference = data.begin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -252,8 +207,7 @@ TEST_F(StorageCursorTest, NextPublic) {
|
||||
}
|
||||
|
||||
TEST_F(StorageCursorTest, LastPublic) {
|
||||
EXPECT_EQ(getTableCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
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);
|
||||
@ -261,19 +215,18 @@ TEST_F(StorageCursorTest, LastPublic) {
|
||||
}
|
||||
|
||||
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();
|
||||
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);
|
||||
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||
|
||||
std::pair<uint64_t, std::string> element = cursor.last();
|
||||
std::pair<uint64_t, std::string> element = cursor->last();
|
||||
reference = data.rbegin();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
@ -281,29 +234,28 @@ TEST_F(StorageCursorTest, PrevPublic) {
|
||||
}
|
||||
|
||||
TEST_F(StorageCursorTest, CurrentPublic) {
|
||||
EXPECT_EQ(getTableCursorsSize(), 1);
|
||||
std::pair<uint64_t, std::string> element = cursor.first();
|
||||
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();
|
||||
element = cursor->current();
|
||||
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
EXPECT_EQ(element.second, reference->second);
|
||||
|
||||
cursor.next();
|
||||
element = cursor.current();
|
||||
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();
|
||||
cursor->next();
|
||||
cursor->next();
|
||||
cursor->prev();
|
||||
element = cursor->current();
|
||||
++reference;
|
||||
++reference;
|
||||
--reference;
|
||||
@ -312,106 +264,39 @@ TEST_F(StorageCursorTest, CurrentPublic) {
|
||||
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();
|
||||
db->abortTransaction(transaction);
|
||||
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();
|
||||
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
|
||||
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());
|
||||
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();
|
||||
element = cursor->current();
|
||||
EXPECT_EQ(element.first, breference->first);
|
||||
EXPECT_EQ(element.second, breference->second);
|
||||
EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
|
||||
cursor.close();
|
||||
EXPECT_THROW(cursor->next(), LMDBAL::NotFound);
|
||||
cursor->close();
|
||||
|
||||
cursor.open();
|
||||
element = cursor.next();
|
||||
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();
|
||||
element = cursor->current();
|
||||
EXPECT_EQ(element.first, reference->first);
|
||||
EXPECT_EQ(element.second, reference->second);
|
||||
EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
|
||||
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound);
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ protected:
|
||||
}
|
||||
if (WIFEXITED(status)) {
|
||||
const int exit_status = WEXITSTATUS(status);
|
||||
if (exit_status != 0)
|
||||
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;
|
||||
@ -64,7 +64,7 @@ TEST_F(StorageTransactionsTest, Adding) {
|
||||
EXPECT_EQ(t1->count(), 0);
|
||||
EXPECT_EQ(t2->count(), 0);
|
||||
|
||||
LMDBAL::WriteTransaction txn = db->beginTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||
t1->addRecord(5, 13, txn);
|
||||
t1->addRecord(-53, 782, txn);
|
||||
t1->addRecord(5892, -37829, txn);
|
||||
@ -76,7 +76,7 @@ TEST_F(StorageTransactionsTest, Adding) {
|
||||
EXPECT_EQ(t1->count(), 0);
|
||||
EXPECT_EQ(t2->count(), 0);
|
||||
|
||||
txn.commit();
|
||||
db->commitTransaction(txn);
|
||||
|
||||
EXPECT_EQ(t1->count(), 3);
|
||||
EXPECT_EQ(t1->getRecord(5), 13);
|
||||
@ -95,7 +95,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
|
||||
LMDBAL::SizeType s1 = t1->count();
|
||||
LMDBAL::SizeType s2 = t2->count();
|
||||
|
||||
LMDBAL::WriteTransaction txn = db->beginTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||
t1->addRecord(18, 40, txn);
|
||||
t1->addRecord(85, -4, txn);
|
||||
t1->addRecord(-5, -3, txn);
|
||||
@ -107,7 +107,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
|
||||
EXPECT_EQ(t1->count(), s1);
|
||||
EXPECT_EQ(t2->count(), s2);
|
||||
|
||||
txn.abort();
|
||||
db->abortTransaction(txn);
|
||||
|
||||
EXPECT_EQ(t1->count(), s1);
|
||||
EXPECT_EQ(t2->count(), s2);
|
||||
@ -116,7 +116,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
|
||||
TEST_F(StorageTransactionsTest, Reading) {
|
||||
EXPECT_EQ(db->ready(), true);
|
||||
|
||||
LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginReadOnlyTransaction();
|
||||
|
||||
EXPECT_EQ(t1->count(txn), 3);
|
||||
EXPECT_EQ(t1->getRecord(5, txn), 13);
|
||||
@ -128,14 +128,14 @@ TEST_F(StorageTransactionsTest, Reading) {
|
||||
EXPECT_FLOAT_EQ(t2->getRecord("decallence", txn), 8532.48);
|
||||
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery", txn), -64.64);
|
||||
|
||||
txn.terminate();
|
||||
db->abortTransaction(txn);
|
||||
}
|
||||
|
||||
TEST_F(StorageTransactionsTest, ConcurentReading) {
|
||||
EXPECT_EQ(db->ready(), true);
|
||||
|
||||
LMDBAL::SizeType size = t1->count();
|
||||
LMDBAL::WriteTransaction txn = db->beginTransaction();
|
||||
LMDBAL::TransactionID txn = db->beginTransaction();
|
||||
EXPECT_EQ(t1->getRecord(5, txn), 13);
|
||||
EXPECT_EQ(t1->getRecord(5), 13);
|
||||
|
||||
@ -160,7 +160,7 @@ TEST_F(StorageTransactionsTest, ConcurentReading) {
|
||||
EXPECT_EQ(t1->count(txn), 1);
|
||||
EXPECT_EQ(t1->count(), size);
|
||||
|
||||
txn.commit();
|
||||
db->commitTransaction(txn);
|
||||
|
||||
EXPECT_FALSE(t1->checkRecord(5));
|
||||
EXPECT_EQ(t1->count(), 1);
|
||||
@ -181,9 +181,8 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
|
||||
|
||||
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
|
||||
LMDBAL::TransactionID 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);
|
||||
@ -198,13 +197,13 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
|
||||
EXPECT_EQ(t1->getRecord(5), 812);
|
||||
|
||||
std::cout << "commiting second transaction" << std::endl;
|
||||
txn2.commit();
|
||||
db->commitTransaction(txn2);
|
||||
|
||||
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();
|
||||
LMDBAL::TransactionID txn1 = db->beginTransaction();
|
||||
|
||||
std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
|
||||
usleep(5);
|
||||
@ -219,61 +218,12 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
|
||||
EXPECT_FALSE(t1->checkRecord(5));
|
||||
|
||||
std::cout << "commiting first transaction" << std::endl;
|
||||
txn1.commit();
|
||||
db->commitTransaction(txn1);
|
||||
|
||||
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;
|
||||
std::cout << "checking 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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user