Compare commits

..

71 Commits

Author SHA1 Message Date
75aafd2750
A fix for CI hopefully
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 1m6s
2024-10-28 14:14:39 +02:00
4fd3799c19
Playing with CI
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 1m8s
2024-10-28 14:11:30 +02:00
def2b8bc4d
Some test fixes
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 1m5s
2024-10-06 20:11:44 +03:00
fc55ec6737
Pipeline fix on the new document build options
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 1m4s
2024-10-06 12:18:07 +03:00
3282524c8b Merge pull request 'Choice in documentation formats to build' (#2) from bmckwm/lmdbal:bmckwm-patch-1 into master
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 57s
Reviewed-on: #2
Reviewed-by: Blue <blue@macaw.me>
2024-10-05 15:16:35 +00:00
c60bb10923 Merge pull request 'Indicate available under GPL-3.0-or-later' (#5) from bmckwm/lmdbal:update-readme into master
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 1m1s
Reviewed-on: #5
Reviewed-by: Blue <blue@macaw.me>
2024-10-05 15:15:28 +00:00
9baade7b8c Merge pull request 'Use text from fsf.org' (#7) from bmckwm/lmdbal:license-text into master
Some checks failed
Main LMDBAL workfow / Archlinux (push) Has been cancelled
Reviewed-on: #7
Reviewed-by: Blue <blue@macaw.me>
2024-10-05 15:14:51 +00:00
deded0c4e1 Use text from fsf.org
https://www.gnu.org/licenses/gpl-3.0.md
2024-10-05 12:15:03 +00:00
a09030cd4e Specify which files are under which licenses 2024-10-05 11:26:03 +00:00
d2a68ef5ee Indicate available under GPL-3.0-or-later
Signed-off-by: bmckwm <benson_muite@disroot.org>
2024-10-05 11:19:39 +00:00
8ad684144f Only install documentation if built 2024-10-01 11:20:37 +00:00
a6ea9bedc5 Option to build documentation formats separately
Signed-off-by: bmckwm <benson_muite@disroot.org>
2024-10-01 11:13:48 +00:00
dcf2d289dc
workflow destination transition, some examples on title docs page
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 48s
2024-03-03 20:09:06 -03:00
f638228d24
removed lmdbal subdir from lib on installation
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 40s
2024-02-04 19:49:06 -03:00
6dddd06f93
some cmake magick to make it work with installed paths
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 42s
2024-02-04 16:10:43 -03:00
d62eddc47e
small build adjustments
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 41s
2024-02-01 13:05:49 -03:00
79240aa535
a little fix for building as subdirectory
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 45s
2024-01-31 10:13:27 -03:00
6b475f615e
lmdb calls are now compiled into .so
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 41s
2023-11-14 20:15:16 -03:00
77ba8f9e7b
a bug in readAll of cache
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 45s
2023-11-10 19:27:04 -03:00
3307860ca6
tests, set method, tests for set method
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 46s
2023-11-01 19:45:42 -03:00
96d7d9ef64
First attempt to make RAII cursors, no tests yet
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 40s
2023-10-25 19:18:23 -03:00
a0eebc978d
a sleep to confurency test
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 38s
2023-10-21 16:45:18 -03:00
0079f6e96e
a bug fix, reorganization and 0.5.1
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 38s
2023-10-21 16:41:41 -03:00
6b348023bb
tests for transaction RAII behaviour, transaction documentation, minor doc fixes
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 37s
2023-10-18 12:44:53 -03:00
de741eda21
RAII transactions
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 40s
2023-10-17 18:06:11 -03:00
a9aa6b549f
missing include
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 34s
2023-10-15 11:19:53 -03:00
fbcf94d1c2
Doc fixes, testing for QVariant
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 15s
2023-10-15 11:10:27 -03:00
275406df61
Release CI Workflow
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 37s
2023-10-14 15:09:50 -03:00
de210b44f5
readAll method now works correctly for duplicates mode, testing, some doc fixes
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 40s
2023-08-20 13:38:29 -03:00
dbbc46e7c9
an attempt to actually deploy docs, try 6
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 33s
2023-08-20 12:23:58 -03:00
a0288727e1
an attempt to actually deploy docs, try 5
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 37s
2023-08-20 12:21:08 -03:00
a19a141611
an attempt to actually deploy docs, try 4
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 32s
2023-08-20 12:10:32 -03:00
af07ca2795
an attempt to actually deploy docs, try 3
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 33s
2023-08-20 12:04:50 -03:00
a9be6be54f
an attempt to actually deploy docs, try 2
Some checks failed
Main LMDBAL workfow / Archlinux (push) Failing after 44s
2023-08-20 11:58:30 -03:00
bcfc9c0dc2
an attempt to actually deploy docs 2023-08-20 11:56:39 -03:00
e3759f1dbe
okay, getting to the bottom of how it works, now should test
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 33s
2023-08-19 22:41:27 -03:00
f3242e2665
okay, getting to the bottom of how it works, now should test 2023-08-19 22:41:18 -03:00
9611431295
a type in the workflow
Some checks failed
Main LMDBAL workfow / Compile (push) Successful in 31s
Main LMDBAL workfow / UnitTests (push) Failing after 2s
2023-08-19 22:38:23 -03:00
437b65df2f
an attempt to run tests 2023-08-19 22:37:21 -03:00
c36230b252
forth attempt to perform build
All checks were successful
Main LMDBAL workfow / Compile (push) Successful in 32s
2023-08-19 22:15:34 -03:00
0d39a613c6
third attempt to perform build
Some checks failed
Main LMDBAL workfow / Compile (push) Failing after 3s
2023-08-19 22:10:09 -03:00
beab78a863
second attempt to perform build
Some checks failed
Main LMDBAL workfow / Compile (push) Failing after 3s
2023-08-19 21:16:13 -03:00
f3a82acdbd
first attempt to compile in CI
Some checks reported warnings
Main LMDBAL workfow / Compile (push) Has been cancelled
2023-08-19 19:31:19 -03:00
a32c35910b
act_runner demo
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 8s
2023-08-19 18:57:25 -03:00
2d40692560
duplicates handling for forceRecord and changeRecord methods, tests and docs 2023-08-19 16:25:52 -03:00
f00f017b16
got down to the MDB_DUP... flags, some more tests, some docs 2023-08-18 12:04:37 -03:00
180c40370c
more duplicates test, misinterpreted something about duplicates, had to fallback 2023-08-18 10:31:30 -03:00
06e1aca45a
some more ideas about duplicates 2023-08-17 11:45:11 -03:00
f0727aa73d
started to work on duplicates support 2023-08-15 15:48:19 -03:00
7b26d57ab6
some more docs, doc build fix 2023-08-13 14:53:07 -03:00
161a850088
Some more tests over cache cursor operations 2023-08-13 13:30:17 -03:00
d57d27f952
some more tests, one subtle but important bugfix with cache 2023-08-12 18:28:49 -03:00
8ff5672655
mostly documentation mostly for Cursor, some corner cases testing 2023-08-10 20:07:12 -03:00
8cb1e97e30
finally methods that actually do something, some testing of them 2023-08-09 14:41:15 -03:00
5fba60f7f0
some thoughts about cursors, nothing special yet 2023-08-07 18:27:44 -03:00
69bf1fcc3d
replaced some pointers to mutables, some first thoughts about cursors 2023-08-05 17:13:43 -03:00
4975721a5c
0.3.1 fix qt5 build, exception docs 2023-04-14 11:44:46 -03:00
2b4763b575
0.3.0 2023-04-12 12:36:33 -03:00
66df0da5f6
documented iStorage, hosted docs and added readme link 2023-04-11 12:11:27 -03:00
af0e48a684
Base class documentation, doxugen tweaking, new exception for one case 2023-04-10 18:01:19 -03:00
ec0d2d57f0
finished documentation for Storage object, added doxygen awesome theme support 2023-04-09 14:19:23 -03:00
6d21ecc155
one more test for adding multiple entries, fix of leaking cursor, Storage::change method rework, beginning of documentation 2023-04-08 02:27:53 +03:00
064277fa6e
two more methods for getting all records from cache, one more test for reading all 2023-04-07 02:51:45 +03:00
181a645efc
some more transaction methods, method for valueReference in cache, some errors fix 2023-04-06 02:01:24 +03:00
f99d5559cd
some transaction methods for cache, some more tests for transactions 2023-04-05 02:27:31 +03:00
150a0b0da9
better transaction tests 2023-04-04 19:14:25 +03:00
8be63d7551
some more transaction thought on cache 2023-04-03 21:48:13 +03:00
f0779ae2aa
some ideas about transaction 2023-04-02 16:00:21 +03:00
a4bb7e6269
a method to get the size of the storage with opened transaction, tests for readAll, and some test for transactions 2023-04-01 17:45:20 +03:00
17fb37075c
unit tests for serialization 2023-03-30 20:00:56 +03:00
e2dbea21d1
external transaction methods for storage 2023-03-28 23:45:35 +03:00
51 changed files with 7254 additions and 563 deletions

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

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

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

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

View File

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

62
CHANGELOG.md Normal file
View File

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

View File

@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(LMDBAL VERSION 0.2.0 LANGUAGES CXX) project(LMDBAL
VERSION 0.5.4
DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer"
LANGUAGES CXX
)
string(TOLOWER ${PROJECT_NAME} PROJECT_LOW) string(TOLOWER ${PROJECT_NAME} PROJECT_LOW)
cmake_policy(SET CMP0076 NEW) cmake_policy(SET CMP0076 NEW)
@ -8,7 +12,10 @@ cmake_policy(SET CMP0079 NEW)
option(BUILD_STATIC "Builds library as static library" OFF) option(BUILD_STATIC "Builds library as static library" OFF)
option(BUILD_TESTS "Builds tests" OFF) option(BUILD_TESTS "Builds tests" OFF)
option(BUILD_DOC "Builds documentation" OFF) option(BUILD_DOC_MAN "Builds man page documentation" OFF)
option(BUILD_DOC_HTML "Builds html documentation" OFF)
option(BUILD_DOC_XML "Builds xml documentation" OFF)
option(BUILD_DOXYGEN_AWESOME "Builds documentation alternative style" OFF)
include(GNUInstallDirs) include(GNUInstallDirs)
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
@ -16,7 +23,7 @@ include(CMakePackageConfigHelpers)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
if (NOT DEFINED QT_VERSION_MAJOR) if (NOT DEFINED QT_VERSION_MAJOR)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
@ -36,16 +43,34 @@ else ()
add_library(${PROJECT_NAME} SHARED) add_library(${PROJECT_NAME} SHARED)
endif() endif()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
list(APPEND COMPILE_OPTIONS -O3)
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND COMPILE_OPTIONS -g)
list(APPEND COMPILE_OPTIONS -Wall)
list(APPEND COMPILE_OPTIONS -Wextra)
endif()
message("Compilation options: " ${COMPILE_OPTIONS})
target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS})
set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${version}) set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${version})
set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION 1) set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION 1)
set_property(TARGET ${PROJECT_NAME} PROPERTY set_property(TARGET ${PROJECT_NAME} PROPERTY EXPORT_NAME ${PROJECT_NAME})
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 set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY
COMPATIBLE_INTERFACE_STRING ${PROJECT_NAME}_MAJOR_VERSION 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) add_subdirectory(src)
if (BUILD_DOC) if (BUILD_DOC_MAN OR BUILD_DOC_HTML OR BUILD_DOC_XML)
find_package(Doxygen) find_package(Doxygen)
if (DOXYGEN_FOUND) if (DOXYGEN_FOUND)
add_subdirectory(doc) add_subdirectory(doc)
@ -58,14 +83,18 @@ if (BUILD_TESTS)
add_subdirectory(test) add_subdirectory(test)
endif () endif ()
target_include_directories(${PROJECT_NAME} PUBLIC target_include_directories(
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>" ${PROJECT_NAME}
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}>" PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/serializer>
) )
target_include_directories(${PROJECT_NAME} PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS}) target_include_directories(${PROJECT_NAME} PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS}) target_include_directories(${PROJECT_NAME} PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE target_link_libraries(
${PROJECT_NAME}
Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Core
lmdb lmdb
) )
@ -84,8 +113,7 @@ write_basic_package_version_file(
install(TARGETS ${PROJECT_NAME} install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_LOW}Targets EXPORT ${PROJECT_LOW}Targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_LOW}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW} INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}
) )

View File

@ -1,4 +1,4 @@
### GNU GENERAL PUBLIC LICENSE # GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 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 Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed. license document, but changing it is not allowed.
### Preamble ## Preamble
The GNU General Public License is a free, copyleft license for The GNU General Public License is a free, copyleft license for
software and other kinds of works. 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 The precise terms and conditions for copying, distribution and
modification follow. modification follow.
### TERMS AND CONDITIONS ## TERMS AND CONDITIONS
#### 0. Definitions. ### 0. Definitions.
"This License" refers to version 3 of the GNU General Public License. "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 the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion. 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 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 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 The Corresponding Source for a work in source code form is that same
work. work.
#### 2. Basic Permissions. ### 2. Basic Permissions.
All rights granted under this License are granted for the term of All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated 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 conditions stated below. Sublicensing is not allowed; section 10 makes
it unnecessary. 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 No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article 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 the work's users, your or third parties' legal rights to forbid
circumvention of technological measures. 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 You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and 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, 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. 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 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 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 in an aggregate does not cause this License to apply to the other
parts of the aggregate. 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 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 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 source code form), and must require no special password or key for
unpacking, reading or copying. unpacking, reading or copying.
#### 7. Additional Terms. ### 7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this "Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions. 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 form of a separately written license, or stated as exceptions; the
above requirements apply either way. above requirements apply either way.
#### 8. Termination. ### 8. Termination.
You may not propagate or modify a covered work except as expressly You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or 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 reinstated, you do not qualify to receive new licenses for the same
material under section 10. 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 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 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 not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so. 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 Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and 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 any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it. 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 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 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 any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law. 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 If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not 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 satisfy both those terms and this License would be to refrain entirely
from conveying the Program. 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 Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed 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 section 13, concerning interaction through a network will apply to the
combination as such. 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 The Free Software Foundation may publish revised and/or new versions
of the GNU General Public License from time to time. Such 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 author or copyright holder as a result of your choosing to follow a
later version. later version.
#### 15. Disclaimer of Warranty. ### 15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 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 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION. CORRECTION.
#### 16. Limitation of Liability. ### 16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 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 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 TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 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 If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms, 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 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 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 possible use to the public, the best way to achieve this is to make it
@ -673,3 +673,4 @@ 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 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, GNU Lesser General Public License instead of this License. But first,
please read <https://www.gnu.org/licenses/why-not-lgpl.html>. please read <https://www.gnu.org/licenses/why-not-lgpl.html>.

View File

@ -3,13 +3,16 @@
[![AUR license](https://img.shields.io/aur/license/lmdbal?style=flat-square)](https://git.macaw.me/blue/lmdbal/raw/branch/master/LICENSE.md) [![AUR license](https://img.shields.io/aur/license/lmdbal?style=flat-square)](https://git.macaw.me/blue/lmdbal/raw/branch/master/LICENSE.md)
[![AUR version](https://img.shields.io/aur/version/lmdbal?style=flat-square)](https://aur.archlinux.org/packages/lmdbal/) [![AUR version](https://img.shields.io/aur/version/lmdbal?style=flat-square)](https://aur.archlinux.org/packages/lmdbal/)
[![Liberapay patrons](https://img.shields.io/liberapay/patrons/macaw.me?logo=liberapay&style=flat-square)](https://liberapay.com/macaw.me) [![Liberapay patrons](https://img.shields.io/liberapay/patrons/macaw.me?logo=liberapay&style=flat-square)](https://liberapay.com/macaw.me)
[![Documentation](https://img.shields.io/badge/Documentation-HTML-green)](https://macaw.me/lmdbal/doc/html)
### Prerequisites ### Prerequisites
- Qt 5 or higher - a compiler (c++ would do)
- Qt 5 or higher (qt5-base would do)
- lmdb - lmdb
- CMake 3.16 or higher - CMake 3.16 or higher
- Doxygen (optional, for documentation) - Doxygen (optional, for documentation)
- gtest (optional, for tests)
### Using with CMake ### Using with CMake
@ -79,6 +82,7 @@ Here is the list of keys you can pass to configuration phase of `cmake ..`:
- `BUILD_STATIC` - `True` builds project as a static library, `False` builds as dynamic (default is `False`); - `BUILD_STATIC` - `True` builds project as a static library, `False` builds as dynamic (default is `False`);
- `BUILD_TESTS` - `True` build unit tests, `False` does not (default is `False`); - `BUILD_TESTS` - `True` build unit tests, `False` does not (default is `False`);
- `BUILD_DOC` - `True` build doxygen documentation, `False` does not (default is `False`); - `BUILD_DOC` - `True` build doxygen documentation, `False` does not (default is `False`);
- `BUILD_DOXYGEN_AWESOME` - `True` build doxygen awesome theme if `BUILD_DOC` is also `True` (default is `False`);
- `QT_VERSION_MAJOR` - `5` links against Qt5, `6` links agains Qt6, there is no default, so, if you didn't specify it the project will chose automatically; - `QT_VERSION_MAJOR` - `5` links against Qt5, `6` links agains Qt6, there is no default, so, if you didn't specify it the project will chose automatically;
#### Running tests #### Running tests
@ -93,4 +97,4 @@ if you're in the same directory with it
## License ## License
This project is licensed under the GPLv3 License - see the [LICENSE.md](LICENSE.md) file for details 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

View File

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

3
doc/custom.css Normal file
View File

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

78
doc/header.html Normal file
View File

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

View File

@ -1,12 +1,21 @@
/*! \mainpage Getting Started /*! \mainpage Getting Started
* *
* Everything begins with a data nase represented by the class LMDBAL::Base. * Everything begins with a data base represented by the class LMDBAL::Base.
* It repesents a collection of key-value storages that are going to be stored in a sigle data base directory. * 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. * 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 * 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>) * <a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#writableLocation">writableLocation</a>(<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#StandardLocation-enum">CacheLocation</a>)
* so, the file system destination of your data would depend on the * so, the file system destination of your data depends on the
* <a class="el" href="https://doc.qt.io/qt-6/qcoreapplication.html">QCoreApplication</a> configuration of your app. * <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. * After you have created a LMDBAL::Base you probably want to obtain storage handlers.
@ -15,16 +24,55 @@
* <a class="el" href="https://en.cppreference.com/w/cpp/container/map">std::map</a> * <a class="el" href="https://en.cppreference.com/w/cpp/container/map">std::map</a>
* to speed up the access. * to speed up the access.
* *
* You can obtain handlers by calling LMDBAL::Base::addStorage(const std::string&) or LMDBAL::Base::addCache(const std::string& name). * @code{.cpp}
*
* #include "storage.h"
* #include "cache.h"
*
* //...
*
* LMDBAL::Storage<uint32_t, uint32_t> storage = base.addStorage<uint32_t, uint32_t>("storage");
* LMDBAL::Cache<int8_t, std::string> cache = base.addCache<int8_t, std::string>("cache");
*
* @endcode
*
* You can obtain handlers by calling LMDBAL::Base::addStorage() or LMDBAL::Base::addCache().
* Note that the handlers still belong to the LMDBAL::Base and it's his responsibility to destroy them. * 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 are not obliged to save those handlers,
* you can obtain them at any time later using methods LMDBAL::Base::getStorage(const std::string&) or LMDBAL::Base::getCache(const std::string&) * you can obtain them at any time later using methods LMDBAL::Base::getStorage() or LMDBAL::Base::getCache()
* calling them with the same template types and names. * 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(). * 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. * 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. * 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. * 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 you will try to destoroy onpened LMDBAL::Base. * Also it will automatically close itself if you'll try to destoroy onpened LMDBAL::Base.
* *
* @code{.cpp}
*
* //...
*
* storage->addRecord(54, 75);
* cache->addRecord(9, "my value");
*
* uint32_t value1 = storage->getRecord(54); //75
* std::string value2 = cache->getRecord(9); //"myValue"
*
* uint32_t count1 = storage->count(); //1
* uint32_t count2 = cache->count(); //1
*
* storage->removeRecord(54);
* cache->removeRecord(9);
*
* @endcode
*
* To discover how to store read and modify data take a look at LMDBAL::Storage and LMDBAL::Cache classes.
*/ */

View File

@ -1,16 +1,16 @@
# Maintainer: Yury Gubich <blue@macaw.me> # Maintainer: Yury Gubich <blue@macaw.me>
pkgname=lmdbal pkgname=lmdbal
pkgver=0.2.0 pkgver=1.0.0
pkgrel=1 pkgrel=1
pkgdesc="LMDB Abstraction Layer, qt5 version" pkgdesc="LMDB Abstraction Layer"
arch=('i686' 'x86_64') arch=('i686' 'x86_64')
url="https://git.macaw.me/blue/lmdbal" url="https://git.macaw.me/blue/lmdbal"
license=('GPL3') license=('GPL3')
depends=( 'lmdb' 'qt5-base') depends=( 'lmdb' )
makedepends=('cmake>=3.16') makedepends=('cmake>=3.16' 'gcc')
optdepends=() optdepends=()
source=("$pkgname-$pkgver.tar.gz") source=("$pkgname-$pkgver.tar.gz::https://git.macaw.me/blue/$pkgname/archive/$pkgver.tar.gz")
sha256sums=('SKIP') sha256sums=('SKIP')
build() { build() {
cd "$srcdir/$pkgname" cd "$srcdir/$pkgname"

View File

@ -2,6 +2,7 @@ set(SOURCES
exceptions.cpp exceptions.cpp
storage.cpp storage.cpp
base.cpp base.cpp
transaction.cpp
) )
set(HEADERS set(HEADERS
@ -9,29 +10,16 @@ set(HEADERS
exceptions.h exceptions.h
storage.h storage.h
storage.hpp storage.hpp
cursor.h
cursor.hpp
cache.h cache.h
cache.hpp 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 operators.hpp
transaction.h
) )
target_sources(${PROJECT_NAME} PRIVATE target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
${SOURCES}
${HEADERS} add_subdirectory(serializer)
)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW}) install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_LOW})

View File

@ -19,34 +19,61 @@
#include "base.h" #include "base.h"
#include "exceptions.h" #include "exceptions.h"
#include "storage.h" #include "storage.h"
#include "transaction.h"
LMDBAL::Base::Base(const QString& p_name, uint16_t mapSize): #define UNUSED(x) (void)(x)
name(p_name.toStdString()),
/**
* \class LMDBAL::Base
* \brief Database abstraction
*
* This is a basic class that represents the database as a collection of storages.
* Storages is something key-value database has instead of tables in classic SQL databases.
*/
/**
* \brief Creates the database
*
* \param[in] _name - name of the database, it is going to affect folder name that is created to store data
* \param[in] _mapSize - LMDB map size (MiB), multiplied by 1024^2 and passed to <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gaa2506ec8dab3d969b0e609cd82e619e5">mdb_env_set_mapsize</a> during the call of LMDBAL::Base::open()
*/
LMDBAL::Base::Base(const QString& _name, uint16_t _mapSize):
name(_name.toStdString()),
opened(false), opened(false),
size(mapSize), size(_mapSize),
environment(), environment(),
tables(), storages(),
transactions(new Transactions()) transactions(new Transactions())
{} {}
/**
* \brief Destroys the database
*/
LMDBAL::Base::~Base() { LMDBAL::Base::~Base() {
close(); close();
delete transactions; delete transactions;
for (const std::pair<const std::string, iStorage*>& pair : tables) for (const std::pair<const std::string, iStorage*>& pair : storages)
delete pair.second; delete pair.second;
} }
/**
* \brief Closes the database
*
* Closes all lmdb handles, aborts all public transactions.
* This function will do nothing on closed database
*
* \exception LMDBAL::Unknown - thrown if something went wrong aborting transactions
*/
void LMDBAL::Base::close() { void LMDBAL::Base::close() {
if (opened) { if (opened) {
for (LMDBAL::TransactionID id : *transactions) for (const LMDBAL::TransactionID id : *transactions)
mdb_txn_abort(id); abortTransaction(id, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages)
pair.second->close();
for (const std::pair<const std::string, iStorage*>& pair : tables) {
iStorage* table = pair.second;
mdb_dbi_close(environment, table->dbi);
}
mdb_env_close(environment); mdb_env_close(environment);
transactions->clear(); transactions->clear();
opened = false; opened = false;
@ -54,37 +81,47 @@ void LMDBAL::Base::close() {
} }
/** /**
* Almost every LMDBAL::Base require it to be opened, this function opens it. It laso creates the directory for the database if it was an initial launch * \brief Opens the database
*
* Almost every LMDBAL::Base require it to be opened, this function does it.
* It laso creates the directory for the database if it was an initial launch.
* This function will do nothing on opened database
*
* \exception LMDBAL::Unknown - thrown if something went wrong opening storages and caches
*/ */
void LMDBAL::Base::open() { void LMDBAL::Base::open() {
if (!opened) { if (!opened) {
mdb_env_create(&environment); mdb_env_create(&environment);
QString path = createDirectory(); QString path = createDirectory();
mdb_env_set_maxdbs(environment, tables.size()); mdb_env_set_maxdbs(environment, storages.size());
mdb_env_set_mapsize(environment, size * 1024UL * 1024UL); mdb_env_set_mapsize(environment, size * 1024UL * 1024UL);
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664); mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
MDB_txn *txn; TransactionID txn = beginPrivateTransaction(emptyName);
mdb_txn_begin(environment, NULL, 0, &txn); for (const std::pair<const std::string, iStorage*>& pair : storages) {
iStorage* storage = pair.second;
for (const std::pair<const std::string, iStorage*>& pair : tables) { int rc = storage->open(txn);
iStorage* table = pair.second;
int rc = table->createTable(txn);
if (rc) if (rc)
throw Unknown(name, mdb_strerror(rc)); throw Unknown(name, mdb_strerror(rc));
} }
mdb_txn_commit(txn); commitPrivateTransaction(txn, emptyName);
opened = true; opened = true;
} }
} }
/**
* \brief Removes database directory
*
* \returns true if removal was successfull of if no directory was created where it's expected to be, false otherwise
*
* \exception LMDBAL::Opened - thrown if this function was called on opened database
*/
bool LMDBAL::Base::removeDirectory() { bool LMDBAL::Base::removeDirectory() {
if (opened) if (opened)
throw Opened(name, "remove database directory"); throw Opened(name, "remove database directory");
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); QString path = getPath();
path += "/" + getName();
QDir cache(path); QDir cache(path);
if (cache.exists()) if (cache.exists())
@ -93,14 +130,26 @@ bool LMDBAL::Base::removeDirectory() {
return true; return true;
} }
/**
* \brief Creates database directory
*
* Creates or opens existing directory with the given name in the location acquired with
* <a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#writableLocation">writableLocation</a>(<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html">QStandardPaths</a>::<a class="el" href="https://doc.qt.io/qt-6/qstandardpaths.html#StandardLocation-enum">CacheLocation</a>)
* so, the file system destination of your data would depend on the
* <a class="el" href="https://doc.qt.io/qt-6/qcoreapplication.html">QCoreApplication</a> configuration of your app.
* This function does nothing if the directory was already created
*
* \returns the path of the created directory
*
* \exception LMDBAL::Opened - thrown if called on opened database
* \exception LMDBAL::Directory - if the database couldn't create the folder
*/
QString LMDBAL::Base::createDirectory() { QString LMDBAL::Base::createDirectory() {
if (opened) if (opened)
throw Opened(name, "create database directory"); throw Opened(name, "create database directory");
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); QString path = getPath();
path += "/" + getName();
QDir cache(path); QDir cache(path);
if (!cache.exists()) { if (!cache.exists()) {
bool res = cache.mkpath(path); bool res = cache.mkpath(path);
if (!res) if (!res)
@ -110,71 +159,192 @@ QString LMDBAL::Base::createDirectory() {
return path; return path;
} }
/**
* \brief Returns database name
*
* \returns database name
*/
QString LMDBAL::Base::getName() const { QString LMDBAL::Base::getName() const {
return QString::fromStdString(name);} return QString::fromStdString(name);}
/**
* \brief Returns database name
*
* \returns database path
*/
QString LMDBAL::Base::getPath() const {
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
return path;
}
/**
* \brief Returns database state
*
* \returns true if the database is opened and ready for work, false otherwise
*/
bool LMDBAL::Base::ready() const { bool LMDBAL::Base::ready() const {
return opened;} return opened;}
/**
* \brief Drops the database
*
* Clears all caches and storages of the database
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happend
*/
void LMDBAL::Base::drop() { void LMDBAL::Base::drop() {
if (!opened) if (!opened)
throw Closed("drop", name); throw Closed("drop", name);
MDB_txn *txn; TransactionID txn = beginPrivateTransaction(emptyName);
int rc = mdb_txn_begin(environment, NULL, 0, &txn); for (const std::pair<const std::string, iStorage*>& pair : storages) {
if (rc) int rc = pair.second->drop(txn);
throw Unknown(name, mdb_strerror(rc)); if (rc != MDB_SUCCESS) {
abortPrivateTransaction(txn, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : tables) {
rc = pair.second->drop(txn);
if (rc)
throw Unknown(name, mdb_strerror(rc), pair.first); throw Unknown(name, mdb_strerror(rc), pair.first);
} }
mdb_txn_commit(txn);
} }
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const { commitPrivateTransaction(txn, emptyName);
return beginReadOnlyTransaction(emptyName);} for (const std::pair<const std::string, iStorage*>& pair : storages)
pair.second->handleDrop();
}
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const { /**
return beginTransaction(emptyName);} * \brief Begins read-only transaction
*
* This is the legitimate way to retrieve LMDBAL::Transaction.
* LMDBAL::Transaction is considered runnig right after creation by this method.
* You can terminate transaction manually calling LMDBAL::Transaction::terminate
* but it's not required, because transaction will be terminated automatically
* (if it was not terminated manually) upon the call of the destructor.
*
* You can not use termitated transaction any more.
*
* \returns read-only transaction
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::Transaction LMDBAL::Base::beginReadOnlyTransaction() const {
TransactionID id = beginReadOnlyTransaction(emptyName);
return Transaction(id, this);
}
/**
* \brief Begins writable transaction
*
* This is the legitimate way to retrieve LMDBAL::WriteTransaction.
* LMDBAL::WriteTransaction is considered runnig right after creation by this method.
* You can commit all the changes made by this transaction calling LMDBAL::WriteTransaction::commit.
* You can cancel any changes made bu this transaction calling LMDBAL::WriteTransaction::abort
* (or LMDBAL::Transaction::terminate which LMDBAL::WriteTransaction inherits),
* but it's not required, because transaction will be aborted automatically
* (if it was not terminated (committed <b>OR</b> aborted) manually) upon the call of the destructor.
*
* You can not use termitated transaction any more.
*
* \returns writable transaction
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::WriteTransaction LMDBAL::Base::beginTransaction() {
TransactionID id = beginTransaction(emptyName);
return WriteTransaction(id, this);
}
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* This is an optimal way to terminate read-only transactions
*
* \param[in] id - transaction ID you want to abort
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const { void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const {
return abortTransaction(id, emptyName);} return abortTransaction(id, emptyName);}
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) const { /**
* \brief Commits transaction
*
* Terminates transaction applying changes.
*
* \param[in] id - transaction ID you want to commit
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) {
return commitTransaction(id, emptyName);} return commitTransaction(id, emptyName);}
/**
* \brief Begins read-only transaction
*
* This function is intended to be called from subordinate storage or cache
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns read-only transaction ID
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const { LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const {
if (!opened) if (!opened)
throw Closed("beginReadOnlyTransaction", name, storageName); throw Closed("beginReadOnlyTransaction", name, storageName);
MDB_txn* txn; TransactionID txn = beginPrivateReadOnlyTransaction(storageName);
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
if (rc) {
mdb_txn_abort(txn);
throw Unknown(name, mdb_strerror(rc), storageName);
}
transactions->emplace(txn); transactions->emplace(txn);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionStarted(txn, true);
return txn; return txn;
} }
/**
* \brief Begins writable transaction
*
* This function is intended to be called from subordinate storage or cache
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns writable transaction ID
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageName) const { LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageName) const {
if (!opened) if (!opened)
throw Closed("beginTransaction", name, storageName); throw Closed("beginTransaction", name, storageName);
MDB_txn* txn; TransactionID txn = beginPrivateTransaction(storageName);
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
if (rc) {
mdb_txn_abort(txn);
throw Unknown(name, mdb_strerror(rc), storageName);
}
transactions->emplace(txn); transactions->emplace(txn);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionStarted(txn, false);
return txn; return txn;
} }
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* This is an optimal way to terminate read-only transactions.
* This function is intended to be called from subordinate storage or cache
*
* \param[in] id - transaction ID you want to abort
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string& storageName) const { void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
if (!opened) if (!opened)
throw Closed("abortTransaction", name, storageName); throw Closed("abortTransaction", name, storageName);
@ -183,11 +353,26 @@ void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string&
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
throw Unknown(name, "unable to abort transaction: transaction was not found", storageName); throw Unknown(name, "unable to abort transaction: transaction was not found", storageName);
mdb_txn_abort(id); abortPrivateTransaction(id, storageName);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionAborted(id);
transactions->erase(itr); transactions->erase(itr);
} }
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string& storageName) const { /**
* \brief Commits transaction
*
* Terminates transaction applying changes.
* This function is intended to be called from subordinate storage or cache
*
* \param[in] id - transaction ID you want to commit
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string& storageName) {
if (!opened) if (!opened)
throw Closed("abortTransaction", name, storageName); throw Closed("abortTransaction", name, storageName);
@ -195,8 +380,86 @@ void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
throw Unknown(name, "unable to commit transaction: transaction was not found", storageName); throw Unknown(name, "unable to commit transaction: transaction was not found", storageName);
int rc = mdb_txn_commit(id); commitPrivateTransaction(id, storageName);
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
pair.second->transactionCommited(id);
transactions->erase(itr); transactions->erase(itr);
}
/**
* \brief Begins read-only transaction
*
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns read-only transaction ID
*
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::string& storageName) const {
MDB_txn* txn;
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
if (rc != MDB_SUCCESS)
throw Unknown(name, mdb_strerror(rc), storageName);
return txn;
}
/**
* \brief Begins writable transaction
*
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \returns writable transaction ID
*
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginPrivateTransaction(const std::string& storageName) const {
MDB_txn* txn;
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
if (rc != MDB_SUCCESS) {
mdb_txn_abort(txn);
throw Unknown(name, mdb_strerror(rc), storageName);
}
return txn;
}
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* This is an optimal way to terminate read-only transactions.
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] id - transaction ID you want to abort
* \param[in] storageName - name of the storage/cache that you begin transaction from, unused here
*/
void LMDBAL::Base::abortPrivateTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
UNUSED(storageName);
mdb_txn_abort(id);
}
/**
* \brief Commits transaction
*
* Terminates transaction applying changes.
* This function is intended to be called from subordinate storage or cache
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] id - transaction ID you want to commit
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
* \exception LMDBAL::Unknown - thrown if transaction with given ID was not found or if something unexpected happened
*/
void LMDBAL::Base::commitPrivateTransaction(LMDBAL::TransactionID id, const std::string& storageName) {
int rc = mdb_txn_commit(id);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throw Unknown(name, mdb_strerror(rc), storageName); throw Unknown(name, mdb_strerror(rc), storageName);
} }

View File

@ -36,6 +36,8 @@
namespace LMDBAL { namespace LMDBAL {
class iStorage; class iStorage;
class Transaction;
class WriteTransaction;
template<class T> template<class T>
class Serializer; class Serializer;
@ -46,10 +48,13 @@ class Storage;
template <class K, class V> template <class K, class V>
class Cache; class Cache;
typedef MDB_txn* TransactionID; typedef MDB_txn* TransactionID; /**<\brief I'm going to use transaction pointers as transaction IDs*/
typedef uint32_t SizeType; /**<\brief All LMDBAL sizes are uint32_t*/
class Base { class Base {
friend class iStorage; friend class iStorage;
friend class Transaction;
friend class WriteTransaction;
public: public:
Base(const QString& name, uint16_t mapSize = 10); Base(const QString& name, uint16_t mapSize = 10);
@ -61,88 +66,154 @@ public:
bool removeDirectory(); bool removeDirectory();
QString createDirectory(); QString createDirectory();
QString getName() const; QString getName() const;
QString getPath() const;
void drop(); void drop();
TransactionID beginReadOnlyTransaction() const; Transaction beginReadOnlyTransaction() const;
TransactionID beginTransaction() const; WriteTransaction beginTransaction();
void commitTransaction(TransactionID id) const;
void abortTransaction(TransactionID id) const;
template <class K, class V> template <class K, class V>
LMDBAL::Storage<K, V>* addStorage(const std::string& name); LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
template <class K, class V> template <class K, class V>
LMDBAL::Cache<K, V>* addCache(const std::string& name); LMDBAL::Cache<K, V>* addCache(const std::string& storageName);
template <class K, class V> template <class K, class V>
LMDBAL::Storage<K, V>* getStorage(const std::string& name); LMDBAL::Storage<K, V>* getStorage(const std::string& storageName);
template <class K, class V> template <class K, class V>
LMDBAL::Cache<K, V>* getCache(const std::string& name); LMDBAL::Cache<K, V>* getCache(const std::string& storageName);
private: private:
typedef std::map<std::string, LMDBAL::iStorage*> Tables; typedef std::map<std::string, LMDBAL::iStorage*> Storages; /**<\brief Storage and Cache pointers are saved in the std::map*/
typedef std::set<TransactionID> Transactions; 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 beginReadOnlyTransaction(const std::string& storageName) const;
TransactionID beginTransaction(const std::string& storageName) const; TransactionID beginTransaction(const std::string& storageName) const;
void commitTransaction(TransactionID id, const std::string& storageName) const; void commitTransaction(TransactionID id, const std::string& storageName);
void abortTransaction(TransactionID id, const std::string& storageName) const; void abortTransaction(TransactionID id, const std::string& storageName) const;
private: TransactionID beginPrivateReadOnlyTransaction(const std::string& storageName) const;
std::string name; TransactionID beginPrivateTransaction(const std::string& storageName) const;
bool opened; void commitPrivateTransaction(TransactionID id, const std::string& storageName);
uint16_t size; void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
MDB_env* environment;
Tables tables;
Transactions* transactions;
inline static const std::string emptyName = ""; private:
std::string name; /**<\brief Name of this database*/
bool opened; /**<\brief State of this database*/
uint16_t size; /**<\brief lmdb map size in MiB*/
MDB_env* environment; /**<\brief lmdb environment handle*/
Storages storages; /**<\brief Registered storages and caches*/
Transactions* transactions; /**<\brief Active public transactions*/
inline static const std::string emptyName = ""; /**<\brief Empty string for general fallback purposes*/
}; };
} }
#include "operators.hpp" #include "operators.hpp"
/** /**
* Adds LMDBAL::Storage with the given name to the LMDBAL::Base, also returns it. The LMDBAL::Base must be closed * \brief Adds LMDBAL::Storage to the database
*
* Defines that the database is going to have the following storage.
* The LMDBAL::Base must be closed
*
* \param[in] storageName - storage name
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
*
* \tparam K - key type of the storage
* \tparam V - value type of the storage
*
* \exception LMDBAL::Opened thrown if this method is called on the opened database
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add storage with repeating name
*/ */
template <class K, class V> template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& p_name) { LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
if (opened) { if (opened)
throw Opened(name, "add table " + p_name); throw Opened(name, "add storage " + storageName);
}
Storage<K, V>* table = new Storage<K, V>(p_name, this); Storage<K, V>* storage = new Storage<K, V>(this, storageName, duplicates);
tables.insert(std::make_pair(p_name, (iStorage*)table)); std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)storage));
return table; if (!pair.second)
throw StorageDuplicate(name, storageName);
return storage;
} }
/** /**
* Adds LMDBAL::Cache with given the name to the LMDBAL::Base, also returns it. The LMDBAL::Base must be closed * \brief Adds LMDBAL::Cache to the database
*
* Defines that the database is going to have the following cache.
* The LMDBAL::Base must be closed
*
* \param[in] storageName - cache name
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it
*
* \tparam K - key type of the cache
* \tparam V - value type of the cahce
*
* \exception LMDBAL::Opened thrown if this method is called on the opened database
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add cache with repeating name
*/ */
template<class K, class V> template<class K, class V>
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& p_name) { LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
if (opened) { if (opened)
throw Opened(name, "add cache " + p_name); throw Opened(name, "add cache " + storageName);
}
Cache<K, V>* cache = new Cache<K, V>(p_name, this); Cache<K, V>* cache = new Cache<K, V>(this, storageName, false);
tables.insert(std::make_pair(p_name, (iStorage*)cache)); std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)cache));
if (!pair.second)
throw StorageDuplicate(name, storageName);
return cache; return cache;
} }
/** /**
* Returns LMDBAL::Storage with the given name * \brief Returns LMDBAL::Storage handle
*
* Requested storage must have been added before opening database
* Note that template parameters is user responsibility zone!
* If user, for instance, had added storage <int, int> but calling
* this method with template parameters <std::string, std::string>
* on the same name of the previously added storage, or calling it on cache - the behaviour is undefined
*
* \param[in] storageName - storage name
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
*
* \tparam K - key type of the storage
* \tparam V - value type of the storage
*
* \exception std::out_of_range thrown if storage with the given name was not found
*/ */
template <class K, class V> template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& p_name) { LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& storageName) {
return static_cast<Storage<K, V>*>(tables.at(p_name)); return static_cast<Storage<K, V>*>(storages.at(storageName));
} }
/** /**
* Returns LMDBAL::Cache with the given name * \brief Returns LMDBAL::Cache handle
*
* Requested cache must have been added before opening database
* Note that template parameters is user responsibility zone!
* If user, for instance, had added cache <int, int> but calling
* this method with template parameters <std::string, std::string>
* on the same name of the previously added cache, or calling it on storage - the behaviour is undefined
*
* \param[in] storageName - cache name
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it
*
* \tparam K - key type of the cache
* \tparam V - value type of the cahce
*
* \exception std::out_of_range thrown if cache with the given name was not found
*/ */
template <class K, class V> template <class K, class V>
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& p_name) { LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& storageName) {
return static_cast<Cache<K, V>*>(tables.at(p_name)); return static_cast<Cache<K, V>*>(storages.at(storageName));
} }
#endif //LMDBAL_BASE_H #endif //LMDBAL_BASE_H

View File

@ -21,6 +21,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <tuple>
#include "storage.h" #include "storage.h"
@ -29,38 +30,105 @@ namespace LMDBAL {
template <class K, class V> template <class K, class V>
class Cache : public Storage<K, V> { class Cache : public Storage<K, V> {
friend class Base; friend class Base;
enum class Mode { //it's a cache state when we: enum class Mode { /**<it's a cache state when we:*/
nothing, // - know nothing about records in database on disk nothing, /**< - know nothing about records in database on disk*/
size, // - know just an amount of records size, /**< - know just an amount of records*/
full // - shure that our cache is equal to the database on disk full /**< - shure that our cache is equal to the database on disk*/
}; };
enum class Operation { /**<Operation type, used in pcrocessing cache modification after writable transaction is commited*/
add,
remove,
change,
force,
drop,
replace,
addMany
};
typedef std::pair<Operation, void*> Entry;
typedef std::list<Entry> Queue;
typedef std::map<TransactionID, Queue> TransactionCache;
protected: protected:
Cache(const std::string& name, Base* parent); Cache(Base* parent, const std::string& name, bool duplicates = false);
~Cache() override; ~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: private:
void handleMode() const; void handleMode() const;
void handleTransactionEntry(const Entry& entry);
void destroyTransactionEntry(const Entry& entry) const;
void handleAddRecord(const K& key, const V& value);
void handleRemoveRecord(const K& key);
void handleChangeRecord(const K& key, const V& value);
void handleForceRecord(const K& key, const V& value, bool added);
void handleReplaceAll(std::map<K, V>* data);
void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize);
void appendToCache(const K& key, const V& value) const;
public: public:
using Storage<K, V>::drop;
using Storage<K, V>::addRecord;
using Storage<K, V>::forceRecord;
using Storage<K, V>::changeRecord;
using Storage<K, V>::removeRecord;
using Storage<K, V>::checkRecord;
using Storage<K, V>::getRecord;
using Storage<K, V>::readAll;
using Storage<K, V>::replaceAll;
using Storage<K, V>::addRecords;
using Storage<K, V>::count;
virtual int drop(const WriteTransaction& transaction) override;
virtual void addRecord(const K& key, const V& value) override; virtual void addRecord(const K& key, const V& value) override;
virtual bool forceRecord(const K& key, const V& value) override; virtual bool forceRecord(const K& key, const V& value) override;
virtual void changeRecord(const K& key, const V& value) override; virtual void changeRecord(const K& key, const V& value) override;
virtual void removeRecord(const K& key) override; virtual void removeRecord(const K& key) override;
virtual bool checkRecord(const K& key) const override; virtual bool checkRecord(const K& key) const override;
virtual void getRecord(const K& key, V& out) const override;
virtual V getRecord(const K& key) const override; virtual V getRecord(const K& key) const override;
virtual uint32_t count() const override; virtual SizeType count() const override;
using Storage<K, V>::drop;
virtual int drop(MDB_txn * transaction) override;
virtual std::map<K, V> readAll() const override; virtual std::map<K, V> readAll() const override;
virtual void readAll(std::map<K, V>& out) const override;
virtual void replaceAll(const std::map<K, V>& data) override; virtual void replaceAll(const std::map<K, V>& data) override;
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false) override; virtual SizeType addRecords(const std::map<K, V>& data, bool overwrite = false) override;
protected: protected:
Mode* mode; /**
std::map<K, V>* cache; * \brief Cache mode
std::set<K>* abscent; *
uint32_t* sizeDifference; * Sometimes we have a complete information about the content of the database,
* and we don't even need to call lmdb to fetch or check for data.
* This member actually shows what is the current state, more about them you can read in Enum descriptions
*/
mutable Mode mode;
std::map<K, V>* cache; /**<\brief Cached data*/
std::set<K>* abscent; /**<\brief Set of keys that are definitely not in the cache*/
mutable SizeType sizeDifference; /**<\brief Difference of size between cached data and amount of records in the lmdb storage*/
TransactionCache* transactionCache; /**<\brief All changes made under under uncommited transactions*/
}; };
} }

View File

@ -22,22 +22,42 @@
#include "cache.h" #include "cache.h"
#include "exceptions.h" #include "exceptions.h"
/**
* \class LMDBAL::Cache
* \brief Storage with additional caching in std::map
*
* \tparam K type of the keys of the cache
* \tparam V type of the values of the cache
*
* You can receive an instance of this class calling LMDBAL::Base::addCache(const std::string&)
* if the database is yet closed and you're defining the storages you're going to need.
* Or you can call LMDBAL::Base::getCache(const std::string&) if you didn't save a pointer to the cache at first
*
* You are not supposed to instantiate or destory instances of this class yourself!
*/
/**
* \brief Creates a cache
*
* \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] name - the name of the storage
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*/
template<class K, class V> template<class K, class V>
LMDBAL::Cache<K, V>::Cache(const std::string& p_name, Base* parent): LMDBAL::Cache<K, V>::Cache(Base* parent, const std::string& name, bool duplicates):
Storage<K, V>(p_name, parent), Storage<K, V>(parent, name, duplicates),
mode(new Mode), mode(Mode::nothing),
cache(new std::map<K, V>()), cache(new std::map<K, V>()),
abscent(new std::set<K>()), abscent(new std::set<K>()),
sizeDifference(new uint32_t) sizeDifference(0),
{ transactionCache(new TransactionCache) {}
*mode = Mode::nothing;
*sizeDifference = 0;
}
/**
* \brief Destroys a cache
*/
template<class K, class V> template<class K, class V>
LMDBAL::Cache<K, V>::~Cache() { LMDBAL::Cache<K, V>::~Cache() {
delete sizeDifference; delete transactionCache;
delete mode;
delete cache; delete cache;
delete abscent; delete abscent;
} }
@ -50,9 +70,28 @@ void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value) {
iStorage::throwDuplicate(iStorage::toString(key)); iStorage::throwDuplicate(iStorage::toString(key));
Storage<K, V>::addRecord(key, value); Storage<K, V>::addRecord(key, value);
handleAddRecord(key, value);
}
template<class K, class V>
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
if (cache->count(key) > 0)
iStorage::throwDuplicate(iStorage::toString(key));
Storage<K, V>::addRecord(key, value, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::pair<K, V>* pair = new std::pair<K, V>(key, value);
tc->second.emplace_back(Operation::add, pair);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleAddRecord(const K& key, const V& value) {
cache->insert(std::make_pair(key, value)); cache->insert(std::make_pair(key, value));
if (*mode != Mode::full) if (mode != Mode::full)
abscent->erase(key); abscent->erase(key);
} }
@ -61,27 +100,46 @@ bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::forceRecordMethodName); iStorage::ensureOpened(iStorage::forceRecordMethodName);
bool added = Storage<K, V>::forceRecord(key, value); bool added = Storage<K, V>::forceRecord(key, value);
if (*mode == Mode::full) { handleForceRecord(key, value, added);
(*cache)[key] = value;
} else {
if (added)
abscent->erase(key);
std::pair<typename std::map<K, V>::iterator, bool> result = cache->insert(std::make_pair(key, value)); return added;
if (!result.second) }
result.first->second = value;
else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference template<class K, class V>
handleMode(); bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
bool added = Storage<K, V>::forceRecord(key, value, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::tuple<bool, K, V>* t = new std::tuple<bool, K, V>(added, key, value);
tc->second.emplace_back(Operation::force, t);
} }
return added; return added;
} }
template<class K, class V>
void LMDBAL::Cache<K, V>::handleForceRecord(const K& key, const V& value, bool added) {
if (mode == Mode::full) {
(*cache)[key] = value;
} else {
if (added)
abscent->erase(key);
std::pair<typename std::map<K, V>::iterator, bool> result =
cache->insert(std::make_pair(key, value));
if (!result.second)
result.first->second = value;
else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference
handleMode();
}
}
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) { void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::changeRecordMethodName); iStorage::ensureOpened(iStorage::changeRecordMethodName);
if (*mode == Mode::full) { if (mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key); typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end()) if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key)); iStorage::throwNotFound(iStorage::toString(key));
@ -94,7 +152,8 @@ void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
try { try {
Storage<K, V>::changeRecord(key, value); Storage<K, V>::changeRecord(key, value);
typename std::pair<typename std::map<K, V>::iterator, bool> res = cache->insert(std::make_pair(key, value)); typename std::pair<typename std::map<K, V>::iterator, bool> res =
cache->insert(std::make_pair(key, value));
if (!res.second) if (!res.second)
res.first->second = value; res.first->second = value;
else else
@ -107,29 +166,191 @@ void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
} }
template<class K, class V> template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key) const { void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
iStorage::ensureOpened(iStorage::getRecordMethodName); if (mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key));
typename std::map<K, V>::const_iterator itr = cache->find(key); Storage<K, V>::changeRecord(key, value, txn);
if (itr != cache->end()) } else {
return itr->second; if (abscent->count(key) > 0)
if (*mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key)); iStorage::throwNotFound(iStorage::toString(key));
try { try {
V value = Storage<K, V>::getRecord(key); Storage<K, V>::changeRecord(key, value, txn);
cache->insert(std::make_pair(key, value));
handleMode();
return value;
} catch (const NotFound& error) { } catch (const NotFound& error) {
if (*mode != Mode::full) abscent->insert(key);
throw error;
}
}
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::pair<K, V>* pair = new std::pair<K, V>(key, value);
tc->second.emplace_back(Operation::add, pair);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleChangeRecord(const K& key, const V& value) {
if (mode == Mode::full) {
cache->at(key) = value;
} else {
typename std::pair<typename std::map<K, V>::iterator, bool> res =
cache->insert(std::make_pair(key, value));
if (!res.second)
res.first->second = value;
else
handleMode();
}
}
template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
V value;
Cache<K, V>::getRecord(key, value);
return value;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end()) {
out = itr->second;
return;
}
if (mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::getRecord(key, out);
appendToCache(key, out);
return;
} catch (const NotFound& error) {
if (mode != Mode::full)
abscent->insert(key); abscent->insert(key);
throw error; throw error;
} }
} }
template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const {
V value;
Cache<K, V>::getRecord(key, value, txn);
return value;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) const {
//if there are any changes made within this transaction
//I will be able to see them among pending changes
//so, I'm going to go through them in reverse order
//and check every key. If it has anything to do this requested key
//there is a way to tell...
bool currentTransaction = false;
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
currentTransaction = true;
const Queue& queue = tc->second;
for (typename Queue::const_reverse_iterator i = queue.rbegin(), end = queue.rend(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key) {
out = static_cast<std::pair<K, V>*>(entry.second)->second;
return;
}
break;
case Operation::remove:
iStorage::throwNotFound(iStorage::toString(key));
break;
case Operation::change:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key) {
out = static_cast<std::pair<K, V>*>(entry.second)->second;
return;
}
break;
case Operation::force:
if (std::get<1>(*static_cast<std::tuple<bool, K, V>*>(entry.second)) == key) {
out = std::get<2>(*static_cast<std::tuple<bool, K, V>*>(entry.second));
return;
}
break;
case Operation::drop:
iStorage::throwNotFound(iStorage::toString(key));
break;
case Operation::replace: {
std::map<K, V>* newMap = static_cast<std::map<K, V>*>(entry.second);
typename std::map<K, V>::const_iterator vitr = newMap->find(key);
if (vitr != newMap->end()) {
out = vitr->second;
return;
} else {
iStorage::throwNotFound(iStorage::toString(key));
}
}
break;
case Operation::addMany: {
const std::tuple<bool, SizeType, std::map<K, V>>& tuple =
*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
const std::map<K, V>& newElements = std::get<2>(tuple);
typename std::map<K, V>::const_iterator vitr = newElements.find(key);
if (vitr != newElements.end()) {
out = vitr->second;
return;
}
}
break;
}
}
}
//... but if nothing was found or if the transaction is not the one
//which caused the changes i just need to check it among local cache
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end()) {
out = itr->second;
return;
}
if (mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::getRecord(key, out, txn);
if (!currentTransaction)
appendToCache(key, out);
return;
} catch (const NotFound& error) {
if (!currentTransaction && mode != Mode::full)
abscent->insert(key);
throw error;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::discoveredRecord(const K& key, const V& value) const {
appendToCache(key, value);
}
template<class K, class V>
void LMDBAL::Cache<K, V>::discoveredRecord(const K& key, const V& value, TransactionID txn) const {
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc == transactionCache->end()) //there is a way to look though all the records in transaction log and cache the new pair
discoveredRecord(key, value); //if there is nothing in transaction log about it, but it seems like too much for a small gain
}
template<class K, class V> template<class K, class V>
bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const { bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
iStorage::ensureOpened(iStorage::checkRecordMethodName); iStorage::ensureOpened(iStorage::checkRecordMethodName);
@ -138,55 +359,266 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
if (itr != cache->end()) if (itr != cache->end())
return true; return true;
if (*mode == Mode::full || abscent->count(key) != 0) if (mode == Mode::full || abscent->count(key) != 0)
return false; return false;
try { try {
V value = Storage<K, V>::getRecord(key); V value = Storage<K, V>::getRecord(key);
cache->insert(std::make_pair(key, value)); appendToCache(key, value);
handleMode();
return true; return true;
} catch (const NotFound& error) { } catch (const NotFound& error) {
if (*mode != Mode::full) if (mode != Mode::full)
abscent->insert(key); abscent->insert(key);
return false; return false;
} }
} }
template<class K, class V>
bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
//if there are any changes made within this transaction
//I will be able to see them among pending changes
//so, I'm going to go through them in reverse order
//and check every key. If it has anything to do this requested key
//there is a way to tell...
bool currentTransaction = false;
typename TransactionCache::const_iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
currentTransaction = true;
const Queue& queue = tc->second;
for (typename Queue::const_reverse_iterator i = queue.rbegin(), end = queue.rend(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key)
return true;
break;
case Operation::remove:
if (*static_cast<K*>(entry.second) == key)
return false;
break;
case Operation::change:
if (static_cast<std::pair<K, V>*>(entry.second)->first == key)
return true;
break;
case Operation::force:
if (std::get<1>(*static_cast<std::tuple<bool, K, V>*>(entry.second)) == key)
return true;
break;
case Operation::drop:
return false;
break;
case Operation::replace:
if (static_cast<std::map<K, V>*>(entry.second)->count(key) > 0)
return true;
else
return false;
break;
case Operation::addMany:
if (std::get<2>(
*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second)
).count(key) > 0)
return true;
break;
}
}
}
//... but if nothing was found or if the transaction is not the one
//which caused the changes i just need to check it among local cache
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return true;
if (mode == Mode::full || abscent->count(key) != 0)
return false;
try {
V value = Storage<K, V>::getRecord(key, txn);
if (!currentTransaction)
appendToCache(key, value);
return true;
} catch (const NotFound& error) {
if (!currentTransaction && mode != Mode::full)
abscent->insert(key);
return false;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::appendToCache(const K& key, const V& value) const {
typename std::pair<typename std::map<K, V>::const_iterator, bool> pair = cache->insert(std::make_pair(key, value));
if (pair.second)
handleMode();
}
template<class K, class V> template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll() const { std::map<K, V> LMDBAL::Cache<K, V>::readAll() const {
iStorage::ensureOpened(iStorage::readAllMethodName); iStorage::ensureOpened(iStorage::readAllMethodName);
if (*mode != Mode::full) { //there is a room for optimization if (mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values mode = Mode::full; //I can read and deserialize only those values
*cache = Storage<K, V>::readAll(); //that are missing in the cache *cache = Storage<K, V>::readAll(); //that are missing in the cache
abscent->clear(); abscent->clear();
*sizeDifference = 0; sizeDifference = 0;
} }
return *cache; return *cache;
} }
template<class K, class V>
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out) const {
iStorage::ensureOpened(iStorage::readAllMethodName);
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
Storage<K, V>::readAll(out); //that are missing in the cache
*cache = out;
abscent->clear();
sizeDifference = 0;
}
}
template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const {
std::map<K, V> out;
readAll(out, txn);
return out;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const {
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
Queue& queue = tc->second;
if (mode != Mode::full) {
out = *cache;
for (typename Queue::const_iterator i = queue.begin(), end = queue.end(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
out.insert(*static_cast<std::pair<K, V>*>(entry.second));
break;
case Operation::remove:
out.erase(*static_cast<K*>(entry.second));
break;
case Operation::change: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
out.at(pair->first) = pair->second;
}
break;
case Operation::force:{
const std::tuple<bool, K, V>& tuple =
*static_cast<std::tuple<bool, K, V>*>(entry.second);
out[std::get<1>(tuple)] = std::get<2>(tuple);
}
break;
case Operation::drop:
out.clear();
break;
case Operation::replace:
out = *static_cast<std::map<K, V>*>(entry.second);
break;
case Operation::addMany: {
const std::tuple<bool, SizeType, std::map<K, V>>& t =
*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
const std::map<K, V>& added = std::get<2>(t);
bool overwrite = std::get<0>(t);
for (const std::pair<const K, V>& pair : added) {
if (overwrite)
out[pair.first] = pair.second;
else
out.insert(pair);
}
}
break;
}
}
} else {
Storage<K, V>::readAll(out, txn);
//queue.clear(); //since I'm getting a complete state of the database
queue.emplace_back(Operation::replace, new std::map<K, V>(out)); //I can as well erase all previous cache entries
}
} else {
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
Storage<K, V>::readAll(out, txn); //that are missing in the cache
*cache = out;
abscent->clear();
sizeDifference = 0;
}
}
}
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data) { void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
Storage<K, V>::replaceAll(data); Storage<K, V>::replaceAll(data);
*cache = data; *cache = data;
if (*mode != Mode::full) { if (mode != Mode::full) {
*mode = Mode::full; mode = Mode::full;
abscent->clear(); abscent->clear();
*sizeDifference = 0; sizeDifference = 0;
} }
} }
template<class K, class V> template<class K, class V>
uint32_t LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) { void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data, TransactionID txn) {
uint32_t newSize = Storage<K, V>::addRecords(data, overwrite); Storage<K, V>::replaceAll(data, txn);
Mode& m = *mode; typename TransactionCache::iterator tc = transactionCache->find(txn);
if (m == Mode::nothing) if (tc != transactionCache->end()) {
m = Mode::size; //queue.clear();
std::map<K, V>* map = new std::map<K, V>(data); //since I'm getting a complete state of the database
tc->second.emplace_back(Operation::replace, map); //I can as well erase all previous cache entries
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleReplaceAll(std::map<K, V>* data) {
delete cache;
cache = data;
if (mode != Mode::full) {
mode = Mode::full;
abscent->clear();
sizeDifference = 0;
}
}
template<class K, class V>
LMDBAL::SizeType LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
SizeType newSize = Storage<K, V>::addRecords(data, overwrite);
handleAddRecords(data, overwrite, newSize);
return newSize;
}
template<class K, class V>
LMDBAL::SizeType LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite) {
SizeType newSize = Storage<K, V>::addRecords(data, txn, overwrite);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
std::tuple<bool, SizeType, std::map<K, V>>* tuple =
new std::tuple<bool, SizeType, std::map<K, V>>(overwrite, newSize, data);
tc->second.emplace_back(Operation::addMany, tuple);
}
return newSize;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize) {
if (mode == Mode::nothing)
mode = Mode::size;
std::map<K, V>& c = *cache; std::map<K, V>& c = *cache;
std::set<K>& a = *abscent; std::set<K>& a = *abscent;
@ -195,55 +627,81 @@ uint32_t LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwr
if (!res.second) { if (!res.second) {
if (overwrite) if (overwrite)
res.first->second = pair.second; res.first->second = pair.second;
} else if (m != Mode::full) { } else if (mode != Mode::full) {
a.erase(pair.first); a.erase(pair.first);
} }
} }
if (m != Mode::full) { if (mode != Mode::full) {
*sizeDifference = newSize - c.size(); sizeDifference = newSize - c.size();
if (*sizeDifference == 0) { if (sizeDifference == 0) {
*mode = Mode::full; mode = Mode::full;
abscent->clear(); abscent->clear();
} }
} }
return newSize;
} }
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::removeRecord(const K& key) { void LMDBAL::Cache<K, V>::removeRecord(const K& key) {
iStorage::ensureOpened(iStorage::removeRecordMethodName); iStorage::ensureOpened(iStorage::removeRecordMethodName);
typename std::pair<typename std::set<K>::const_iterator, bool> pair = abscent->insert(key); bool noKey = false;
if (!pair.second) if (mode != Mode::full)
noKey = cache->count(key) == 0;
else
noKey = abscent->count(key) > 0;
if (noKey)
iStorage::throwNotFound(iStorage::toString(key)); iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::removeRecord(key); Storage<K, V>::removeRecord(key);
handleRemoveRecord(key);
}
template<class K, class V>
void LMDBAL::Cache<K, V>::removeRecord(const K& key, TransactionID txn) {
bool noKey = false;
if (mode != Mode::full)
noKey = cache->count(key) == 0;
else
noKey = abscent->count(key) > 0;
if (noKey)
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::removeRecord(key, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end())
tc->second.emplace_back(Operation::remove, new K(key));
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleRemoveRecord(const K& key) {
if (cache->erase(key) == 0) //if it was not cached and we are now in size mode then the sizeDifference would decrease if (cache->erase(key) == 0) //if it was not cached and we are now in size mode then the sizeDifference would decrease
handleMode(); handleMode();
if (*mode != Mode::full) if (mode != Mode::full)
abscent->insert(key); abscent->insert(key);
} }
template<class K, class V> template<class K, class V>
uint32_t LMDBAL::Cache<K, V>::count() const { uint32_t LMDBAL::Cache<K, V>::count() const {
switch (*mode) { switch (mode) {
case Mode::nothing: case Mode::nothing:
{ {
uint32_t sz = Storage<K, V>::count(); uint32_t sz = Storage<K, V>::count();
*sizeDifference = sz - cache->size(); sizeDifference = sz - cache->size();
if (sz == 0) { if (sz == 0) {
*mode = Mode::full; mode = Mode::full;
abscent->clear(); abscent->clear();
} else { } else {
*mode = Mode::size; mode = Mode::size;
} }
return sz; return sz;
} }
case Mode::size: case Mode::size:
return cache->size() + *sizeDifference; return cache->size() + sizeDifference;
case Mode::full: case Mode::full:
return cache->size(); return cache->size();
default: default:
@ -251,25 +709,194 @@ 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);
if (tc != transactionCache->end()) {
currentTransaction = true;
const Queue& queue = tc->second;
for (typename Queue::const_reverse_iterator i = queue.rbegin(), end = queue.rend(); i != end; ++i) {
const Entry& entry = *i;
switch (entry.first) {
case Operation::add:
++diff;
break;
case Operation::remove:
--diff;
break;
case Operation::change:
break;
case Operation::force:
if (std::get<0>(*static_cast<std::tuple<bool, K, V>*>(entry.second)))
++diff;
break;
case Operation::drop:
return false;
break;
case Operation::replace:
return static_cast<std::map<K, V>*>(entry.second)->size() + diff;
break;
case Operation::addMany:
return std::get<1>(*static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second)) + diff;
break;
}
}
}
switch (mode) {
case Mode::nothing: {
uint32_t sz = Storage<K, V>::count(txn);
if (!currentTransaction) {
sizeDifference = sz - cache->size();
if (sz == 0) {
mode = Mode::full;
abscent->clear();
} else {
mode = Mode::size;
}
}
return sz;
}
case Mode::size:
return cache->size() + sizeDifference + diff;
case Mode::full:
return cache->size() + diff;
default:
return 0; //unreachable, no such state, just to suppress the waring
}
}
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::handleMode() const { void LMDBAL::Cache<K, V>::handleMode() const {
if (*mode == Mode::size) { if (mode == Mode::size) {
--(*sizeDifference); --sizeDifference;
if (*sizeDifference == 0) { if (sizeDifference == 0) {
*mode = Mode::full; mode = Mode::full;
abscent->clear(); abscent->clear();
} }
} }
} }
template<class K, class V> template<class K, class V>
int LMDBAL::Cache<K, V>::drop(MDB_txn * transaction) { int LMDBAL::Cache<K, V>::drop(const WriteTransaction& transaction) {
int res = Storage<K, V>::drop(transaction); iStorage::ensureOpened(iStorage::dropMethodName);
cache->clear(); TransactionID txn = iStorage::extractTransactionId(transaction, iStorage::dropMethodName);
abscent->clear(); int res = Storage<K, V>::drop(txn);
*mode = Mode::full;
*sizeDifference = 0; if (res != MDB_SUCCESS)
return res;
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end())
tc->second.emplace_back(Operation::drop, nullptr);
return res; return res;
} }
template<class K, class V>
void LMDBAL::Cache<K, V>::handleDrop() {
cache->clear();
abscent->clear();
mode = Mode::full;
sizeDifference = 0;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::transactionStarted(TransactionID txn, bool readOnly) const {
if (!readOnly)
transactionCache->emplace(txn, Queue());
}
template<class K, class V>
void LMDBAL::Cache<K, V>::transactionCommited(TransactionID txn) {
typename TransactionCache::iterator itr = transactionCache->find(txn);
if (itr != transactionCache->end()) {
Queue& queue = itr->second;
for (const Entry& entry : queue)
handleTransactionEntry(entry);
transactionCache->erase(itr);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::transactionAborted(TransactionID txn) const {
typename TransactionCache::iterator itr = transactionCache->find(txn);
if (itr != transactionCache->end()) {
Queue& queue = itr->second;
for (const Entry& entry : queue)
destroyTransactionEntry(entry);
transactionCache->erase(itr);
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::handleTransactionEntry(const Entry& entry) {
switch (entry.first) {
case Operation::add: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
handleAddRecord(pair->first, pair->second);
delete pair;
} break;
case Operation::remove: {
K* key = static_cast<K*>(entry.second);
handleRemoveRecord(*key);
delete key;
} break;
case Operation::change: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
handleChangeRecord(pair->first, pair->second);
delete pair;
} break;
case Operation::force: {
std::tuple<bool, K, V>* tuple = static_cast<std::tuple<bool, K, V>*>(entry.second);
const std::tuple<bool, K, V>& t = *tuple;
handleForceRecord(std::get<1>(t), std::get<2>(t), std::get<0>(t));
delete tuple;
} break;
case Operation::drop:
handleDrop();
break;
case Operation::replace:
handleReplaceAll(static_cast<std::map<K, V>*>(entry.second)); //I take ownership, no need to delete
break;
case Operation::addMany: {
std::tuple<bool, SizeType, std::map<K, V>>* tuple = static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
const std::tuple<bool, SizeType, std::map<K, V>>& t = * tuple;
handleAddRecords(std::get<2>(t), std::get<0>(t), std::get<1>(t));
delete tuple;
} break;
}
}
template<class K, class V>
void LMDBAL::Cache<K, V>::destroyTransactionEntry(const Entry& entry) const {
switch (entry.first) {
case Operation::add:
delete static_cast<std::pair<K, V>*>(entry.second);
break;
case Operation::remove:
delete static_cast<K*>(entry.second);
break;
case Operation::change:
delete static_cast<std::pair<K, V>*>(entry.second);
break;
case Operation::force:
delete static_cast<std::tuple<bool, K, V>*>(entry.second);
break;
case Operation::drop:
break;
case Operation::replace:
delete static_cast<std::map<K, V>*>(entry.second);
break;
case Operation::addMany:
delete static_cast<std::tuple<bool, SizeType, std::map<K, V>>*>(entry.second);
break;
}
}
#endif //LMDBAL_CACHE_HPP #endif //LMDBAL_CACHE_HPP

109
src/cursor.h Normal file
View File

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

661
src/cursor.hpp Normal file
View File

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

View File

@ -55,6 +55,31 @@ std::string LMDBAL::Closed::getMessage() const {
return msg; return msg;
} }
LMDBAL::CursorNotReady::CursorNotReady(
const std::string& p_operation,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
operation(p_operation),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::CursorNotReady::getMessage() const {
std::string msg = "An attempt to perform operation " + operation
+ " on closed cursor that belongs to the table " + tableName
+ " within database " + dbName;
return msg;
}
LMDBAL::CursorEmpty::CursorEmpty(const std::string & operation):
Exception(),
operation(operation) {}
std::string LMDBAL::CursorEmpty::getMessage() const {
return "An attempt to perform an operation \"" + operation + "\" on an empty cursor";
}
LMDBAL::Opened::Opened(const std::string& p_dbName, const std::string& p_action): LMDBAL::Opened::Opened(const std::string& p_dbName, const std::string& p_action):
Exception(), Exception(),
dbName(p_dbName), dbName(p_dbName),
@ -82,6 +107,19 @@ std::string LMDBAL::NotFound::getMessage() const {
+ " in database " + dbName + " in database " + dbName
+ " in table " + tableName;} + " in table " + tableName;}
LMDBAL::StorageDuplicate::StorageDuplicate(
const std::string& p_dbName,
const std::string& p_tableName
):
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::StorageDuplicate::getMessage() const {
return "An attempt to add a storage (or cache) " + tableName
+ " to database " + dbName
+ " but the database already has a storage with given name";
}
LMDBAL::Exist::Exist( LMDBAL::Exist::Exist(
const std::string& p_key, const std::string& p_key,
const std::string& p_dbName, const std::string& p_dbName,
@ -99,6 +137,29 @@ std::string LMDBAL::Exist::getMessage() const {
+ " but it already has an element with given id"; + " 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( LMDBAL::Unknown::Unknown(
const std::string& p_dbName, const std::string& p_dbName,
const std::string& message, const std::string& message,

View File

@ -25,18 +25,29 @@
namespace LMDBAL { namespace LMDBAL {
/**
* \brief Parent abstract class for all LMDBAL exceptions
*/
class Exception : public std::exception { class Exception : public std::exception {
public: public:
Exception(); Exception();
virtual ~Exception(); virtual ~Exception();
virtual std::string getMessage() const = 0; virtual std::string getMessage() const = 0; /**<\brief returns exception message*/
const char* what() const noexcept( true ); const char* what() const noexcept( true ) override; /**<\brief system exception method that is actually called to show the message*/
}; };
/**
* \brief Thrown if LMDBAL had issues creating or opening database directory
*/
class Directory: public Exception { class Directory: public Exception {
public: public:
/**
* \brief Creates exception
*
* \param path - path of the directory that was supposed to be used to store the database
*/
Directory(const std::string& path); Directory(const std::string& path);
std::string getMessage() const; std::string getMessage() const;
@ -44,9 +55,19 @@ private:
std::string path; std::string path;
}; };
/**
* \brief Thrown if something in the database was called on closed state and it is not supported
*/
class Closed : public Exception { class Closed : public Exception {
public: public:
Closed(const std::string& p_operation, const std::string& dbName, const std::optional<std::string>& tableName = std::nullopt); /**
* \brief Creates exception
*
* \param operation - text name of the method that was called on closed database
* \param dbName - name of the database
* \param tableName - name of the storage which called that method, abscent if it's untracable or if it's thrown by the database
*/
Closed(const std::string& operation, const std::string& dbName, const std::optional<std::string>& tableName = std::nullopt);
std::string getMessage() const; std::string getMessage() const;
private: private:
@ -55,8 +76,55 @@ private:
std::optional<std::string> tableName; std::optional<std::string> tableName;
}; };
/**
* \brief Thrown if the cursor was operated in closed state
*/
class CursorNotReady : public Exception {
public:
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on closed cursor
* \param dbName - name of the database
* \param tableName - name of the storage owning the cursor
*/
CursorNotReady(const std::string& operation, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string operation;
std::string dbName;
std::string tableName;
};
/**
* \brief Thrown if an empty cursor was somehow operated
*/
class CursorEmpty : public Exception {
public:
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on an empty cursor
*/
CursorEmpty(const std::string& operation);
std::string getMessage() const;
private:
std::string operation;
};
/**
* \brief Thrown if something in the database was called on opened state and it is not supported
*/
class Opened : Exception { class Opened : Exception {
public: public:
/**
* \brief Creates exception
*
* \param action - text name of the method that was called on opened database
* \param dbName - name of the database
*/
Opened(const std::string& dbName, const std::string& action); Opened(const std::string& dbName, const std::string& action);
std::string getMessage() const; std::string getMessage() const;
@ -65,8 +133,18 @@ private:
std::string action; std::string action;
}; };
/**
* \brief Thrown if something in the database was not found
*/
class NotFound : public Exception { class NotFound : public Exception {
public: public:
/**
* \brief Creates exception
*
* \param key - record key that was not found
* \param dbName - name of the database
* \param tableName - name of the storage that was looked for a record
*/
NotFound(const std::string& key, const std::string& dbName, const std::string& tableName); NotFound(const std::string& key, const std::string& dbName, const std::string& tableName);
std::string getMessage() const; std::string getMessage() const;
@ -76,8 +154,37 @@ private:
std::string tableName; std::string tableName;
}; };
/**
* \brief Thrown if there was attempt to define storages with conflicting names
*/
class StorageDuplicate : public Exception {
public:
/**
* \brief Creates exception
*
* \param dbName - name of the database
* \param tableName - that name that was conflicting
*/
StorageDuplicate(const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string dbName;
std::string tableName;
};
/**
* \brief Thrown if there was a key conflict in one of the storages
*/
class Exist : public Exception { class Exist : public Exception {
public: public:
/**
* \brief Creates exception
*
* \param key - record key that caused the conflict
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with
*/
Exist(const std::string& key, const std::string& dbName, const std::string& tableName); Exist(const std::string& key, const std::string& dbName, const std::string& tableName);
std::string getMessage() const; std::string getMessage() const;
@ -87,8 +194,39 @@ private:
std::string tableName; std::string tableName;
}; };
/**
* Thrown if there was an attempt to perform action using terminated transaction
*/
class TransactionTerminated : public Exception {
public:
/**
* \brief Creates exception
*
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with
* \param action - optional action, just to enrich the exception message
*/
TransactionTerminated(const std::string& dbName, const std::string& tableName, const std::string& action = "");
std::string getMessage() const;
private:
std::string dbName;
std::string tableName;
std::string action;
};
/**
* \brief Thrown if something unexpected happened
*/
class Unknown : public Exception { class Unknown : public Exception {
public: public:
/**
* \brief Creates exception
*
* \param message - text description of the error, most of the times contains the result of <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#ga569e66c1e3edc1a6016b86719ee3d098">mdb_strerror</a>
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with, abscent if the operation was with the database itself
*/
Unknown(const std::string& dbName, const std::string& message, const std::optional<std::string>& tableName = std::nullopt); Unknown(const std::string& dbName, const std::string& message, const std::optional<std::string>& tableName = std::nullopt);
std::string getMessage() const; std::string getMessage() const;

View File

@ -75,6 +75,7 @@ QDataStream& operator >> (QDataStream &in, std::multimap<K, V>& container) {
return in; return in;
} }
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
template <class K, class V> template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) { QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) {
out << pair.first; out << pair.first;
@ -90,6 +91,7 @@ QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) {
return in; return in;
} }
#endif
template <class K> template <class K>
QDataStream& operator << (QDataStream &out, const std::set<K>& container) { QDataStream& operator << (QDataStream &out, const std::set<K>& container) {

View File

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

View File

@ -37,6 +37,7 @@ public:
~Serializer(); ~Serializer();
T deserialize(const MDB_val& value); T deserialize(const MDB_val& value);
void deserialize(const MDB_val& value, T& result);
MDB_val setData(const T& value); MDB_val setData(const T& value);
MDB_val getData(); MDB_val getData();
void clear(); void clear();

View File

@ -21,6 +21,22 @@
#include "serializer.h" #include "serializer.h"
/**
* \class LMDBAL::Serializer
* \brief A class handling serialization/deserialization
*
* A class that is constructed in every LMDBAL::Storage
* to serialize or deserialize keys and values.
*
* It serializes to and deserializes from <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#structMDB__val">MDB_val</a>
*
* \tparam K type of the keys of the storage
* \tparam V type of the values of the storage
*/
/**
* \brief Creates an empty Serializer
*/
template<class T> template<class T>
LMDBAL::Serializer<T>::Serializer() : LMDBAL::Serializer<T>::Serializer() :
bytes(), bytes(),
@ -30,6 +46,13 @@ LMDBAL::Serializer<T>::Serializer() :
buffer.open(QIODevice::ReadWrite); buffer.open(QIODevice::ReadWrite);
} }
/**
* \brief Creates a Serializer with some data in it
*
* The data automatically gets serialized
*
* \param[in] value - a value that is assigned to the serializer
*/
template<class T> template<class T>
LMDBAL::Serializer<T>::Serializer(const T& value) : LMDBAL::Serializer<T>::Serializer(const T& value) :
bytes(), bytes(),
@ -37,14 +60,26 @@ LMDBAL::Serializer<T>::Serializer(const T& value) :
stream(&buffer) stream(&buffer)
{ {
buffer.open(QIODevice::ReadWrite); buffer.open(QIODevice::ReadWrite);
_setValue(value); _setData(value);
} }
/**
* \brief Destoys the serializer
*/
template<class T> template<class T>
LMDBAL::Serializer<T>::~Serializer() { LMDBAL::Serializer<T>::~Serializer() {
buffer.close(); buffer.close();
} }
/**
* \brief Sets the data to the seriazer
*
* This is a normal way to seriaze value
*
* \param[in] value - a value you want to serialize
*
* \returns serialized value
*/
template<class T> template<class T>
MDB_val LMDBAL::Serializer<T>::setData(const T& value) { MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
clear(); clear();
@ -52,21 +87,53 @@ MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
return getData(); return getData();
} }
/**
* \brief Deserializes value
*
* This is a normal way to deseriaze value
*
* \param[in] value - a value you want to deserialize
*
* \returns deserialized value
*/
template<class T> template<class T>
T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) { T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
clear();
bytes.setRawData((char*)value.mv_data, value.mv_size);
T result; T result;
stream >> result; deserialize(value, result);
return result; return result;
} }
/**
* \brief Deserializes value
*
* This is a normal way to deseriaze value
*
* \param[in] value - a value you want to deserialize
* \param[out] result - deserialized value
*/
template<class T>
void LMDBAL::Serializer<T>::deserialize(const MDB_val& value, T& result) {
clear();
bytes.setRawData((char*)value.mv_data, value.mv_size);
stream >> result;
}
/**
* \brief Private function that handles serialization
*
* \param[in] value - a value you want to serialize
*/
template<class T> template<class T>
void LMDBAL::Serializer<T>::_setData(const T& value) { void LMDBAL::Serializer<T>::_setData(const T& value) {
stream << value; stream << value;
} }
/**
* \brief Clears the state of serializer
*
* Normally you don't need to call this function
*/
template<class T> template<class T>
void LMDBAL::Serializer<T>::clear() { void LMDBAL::Serializer<T>::clear() {
if (buffer.pos() > 0) { if (buffer.pos() > 0) {
@ -74,6 +141,15 @@ void LMDBAL::Serializer<T>::clear() {
} }
} }
/**
* \brief Returns the data if it already was serialized
*
* Normally you don't need to call this function
*
* This may be usefull if you called LMDBAL::Serilizer::setData() but lost the result
*
* \returns Serialized data
*/
template<class T> template<class T>
MDB_val LMDBAL::Serializer<T>::getData() { MDB_val LMDBAL::Serializer<T>::getData() {
MDB_val val; MDB_val val;

View File

@ -29,9 +29,12 @@ public:
~Serializer() {}; ~Serializer() {};
double deserialize(const MDB_val& data) { double deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 8); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, double& result) {
std::memcpy(&result, data.mv_data, 8);
}
MDB_val setData(const double& data) { MDB_val setData(const double& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -29,9 +29,12 @@ public:
~Serializer() {}; ~Serializer() {};
float deserialize(const MDB_val& data) { float deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 4); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, float& result) {
std::memcpy(&result, data.mv_data, 4);
}
MDB_val setData(const float& data) { MDB_val setData(const float& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
int16_t deserialize(const MDB_val& data) { int16_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 2); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, int16_t& result) {
std::memcpy(&result, data.mv_data, 2);
}
MDB_val setData(const int16_t& data) { MDB_val setData(const int16_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
int32_t deserialize(const MDB_val& data) { int32_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 4); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, int32_t& result) {
std::memcpy(&result, data.mv_data, 4);
}
MDB_val setData(const int32_t& data) { MDB_val setData(const int32_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
int64_t deserialize(const MDB_val& data) { int64_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 8); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, int64_t& result) {
std::memcpy(&result, data.mv_data, 8);
}
MDB_val setData(const int64_t& data) { MDB_val setData(const int64_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
int8_t deserialize(const MDB_val& data) { int8_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 1); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, int8_t& result) {
std::memcpy(&result, data.mv_data, 1);
}
MDB_val setData(const int8_t& data) { MDB_val setData(const int8_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
QByteArray deserialize(const MDB_val& data) { QByteArray deserialize(const MDB_val& data) {
value.setRawData((char*)data.mv_data, data.mv_size); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, QByteArray& result) {
result.setRawData((char*)data.mv_data, data.mv_size);
}
MDB_val setData(const QByteArray& data) { MDB_val setData(const QByteArray& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -35,6 +35,10 @@ public:
value = QByteArray((char*)data.mv_data, data.mv_size); value = QByteArray((char*)data.mv_data, data.mv_size);
return QString::fromUtf8(value); return QString::fromUtf8(value);
}; };
void deserialize(const MDB_val& data, QString& result) {
value = QByteArray((char*)data.mv_data, data.mv_size);
result = QString::fromUtf8(value);
}
MDB_val setData(const QString& data) { MDB_val setData(const QString& data) {
value = data.toUtf8(); value = data.toUtf8();
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
std::string deserialize(const MDB_val& data) { std::string deserialize(const MDB_val& data) {
value = std::string((char*)data.mv_data, data.mv_size); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, std::string& result) {
result.assign((char*)data.mv_data, data.mv_size);
}
MDB_val setData(const std::string& data) { MDB_val setData(const std::string& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
uint16_t deserialize(const MDB_val& data) { uint16_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 2); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, uint16_t& result) {
std::memcpy(&result, data.mv_data, 2);
}
MDB_val setData(const uint16_t& data) { MDB_val setData(const uint16_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
uint32_t deserialize(const MDB_val& data) { uint32_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 4); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, uint32_t& result) {
std::memcpy(&result, data.mv_data, 4);
}
MDB_val setData(const uint32_t& data) { MDB_val setData(const uint32_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
uint64_t deserialize(const MDB_val& data) { uint64_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 8); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, uint64_t& result) {
std::memcpy(&result, data.mv_data, 8);
}
MDB_val setData(const uint64_t& data) { MDB_val setData(const uint64_t& data) {
value = data; value = data;
return getData(); return getData();

View File

@ -31,9 +31,12 @@ public:
~Serializer() {}; ~Serializer() {};
uint8_t deserialize(const MDB_val& data) { uint8_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 1); deserialize(data, value);
return value; return value;
}; };
void deserialize(const MDB_val& data, uint8_t& result) {
std::memcpy(&result, data.mv_data, 1);
}
MDB_val setData(const uint8_t& data) { MDB_val setData(const uint8_t& data) {
value = data; value = data;
return getData(); return getData();

View File

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

View File

@ -19,61 +19,113 @@
#ifndef LMDBAL_STORAGE_H #ifndef LMDBAL_STORAGE_H
#define LMDBAL_STORAGE_H #define LMDBAL_STORAGE_H
#include <type_traits>
#include <cstring>
#include "base.h" #include "base.h"
#include "serializer.h" #include "serializer.h"
#include "cursor.h"
#include "transaction.h"
class BaseTest;
class DuplicatesTest;
class CacheCursorTest;
class StorageCursorTest;
namespace LMDBAL { namespace LMDBAL {
class iStorage { class iStorage {
friend class Base; friend class Base;
public:
protected: protected:
iStorage(const std::string& name, Base* parent); iStorage(Base* parent, const std::string& name, bool duplicates = false);
virtual ~iStorage(); virtual ~iStorage();
virtual int createTable(MDB_txn * transaction) = 0; /**
virtual int drop(MDB_txn * transaction); * \brief A private virtual function I need to open each storage in the database
*
* \param[in] transaction - lmdb transaction to call <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a">mdb_dbi_open</a>
* \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code
*/
virtual int open(MDB_txn * transaction) = 0;
virtual void close();
virtual void handleDrop();
bool isDBOpened() const; bool isDBOpened() const;
const std::string& dbName() const; const std::string& dbName() const;
void ensureOpened(const std::string& methodName) const; void ensureOpened(const std::string& methodName) const;
void throwDuplicateOrUnknown(int rc, const std::string& key) const;
void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const; void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const;
void throwNotFoundOrUnknown(int rc, const std::string& key) const;
void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const; void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const;
void throwUnknown(int rc, TransactionID txn) const; void throwUnknown(int rc, TransactionID txn) const;
void throwUnknown(int rc) const; void throwUnknown(int rc) const;
void throwUnknown(const std::string& message) const;
void throwDuplicate(const std::string& key) const; void throwDuplicate(const std::string& key) const;
void throwNotFound(const std::string& key) const; void throwNotFound(const std::string& key) const;
void throwCursorNotReady(const std::string& method) const;
TransactionID extractTransactionId(const Transaction& txn, const std::string& action = "") const;
TransactionID beginReadOnlyTransaction() const;
TransactionID beginTransaction() const;
void commitTransaction(TransactionID id);
void abortTransaction(TransactionID id) const;
virtual void transactionStarted(TransactionID txn, bool readOnly) const;
virtual void transactionCommited(TransactionID txn);
virtual void transactionAborted(TransactionID txn) const;
virtual int drop(TransactionID transaction);
virtual SizeType count(TransactionID txn) const;
int _mdbOpen(MDB_txn* txn, unsigned int flags = 0);
int _mdbPut(MDB_txn* txn, MDB_val& key, MDB_val& data, unsigned int flags = 0);
int _mdbGet(MDB_txn* txn, MDB_val& key, MDB_val& data) const;
int _mdbDel(MDB_txn* txn, MDB_val& key);
int _mdbDel(MDB_txn* txn, MDB_val& key, MDB_val& data);
int _mdbStat(MDB_txn* txn, MDB_stat& stat) const;
int _mdbFlags(MDB_txn* txn, uint32_t& flags) const;
int _mdbCursorOpen(MDB_txn* txn, MDB_cursor** cursor) const;
void _mdbCursorClose(MDB_cursor* cursor) const;
int _mdbCursorGet(MDB_cursor* cursor, MDB_val& key, MDB_val& data, MDB_cursor_op operation) const;
int _mdbCursorSet(MDB_cursor* cursor, MDB_val& key) const;
int _mdbCursorDel(MDB_cursor* cursor, unsigned int flags = 0);
int _mdbCursorPut(MDB_cursor* cursor, MDB_val& key, MDB_val& data, unsigned int flags = 0);
int _mdbCursorRenew(MDB_txn* txn, MDB_cursor* cursor) const;
MDB_txn* _mdbCursorTxn(MDB_cursor* cursor) const;
public: public:
virtual void drop(); virtual void drop();
virtual uint32_t count() const; virtual int drop(const WriteTransaction& txn);
virtual SizeType count() const;
TransactionID beginReadOnlyTransaction() const; virtual SizeType count(const Transaction& txn) const;
TransactionID beginTransaction() const;
void commitTransaction(TransactionID id) const;
void abortTransaction(TransactionID id) const;
protected: protected:
MDB_dbi dbi; MDB_dbi dbi; /**<\brief lmdb storage handle*/
Base* db; Base* db; /**<\brief parent database pointer (borrowed)*/
const std::string name; const std::string name; /**<\brief this storage name*/
const bool duplicates; /**<\brief true if storage supports duplicates*/
inline static const std::string dropMethodName = "drop"; inline static const std::string dropMethodName = "drop"; /**<\brief member function name, just for exceptions*/
inline static const std::string countMethodName = "count"; inline static const std::string countMethodName = "count"; /**<\brief member function name, just for exceptions*/
inline static const std::string flagsMethodName = "flags"; /**<\brief member function name, just for exceptions*/
inline static const std::string addRecordMethodName = "addRecord"; inline static const std::string addRecordMethodName = "addRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string forceRecordMethodName = "forceRecord"; inline static const std::string forceRecordMethodName = "forceRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string changeRecordMethodName = "changeRecord"; inline static const std::string changeRecordMethodName = "changeRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string removeRecordMethodName = "removeRecord"; inline static const std::string removeRecordMethodName = "removeRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string checkRecordMethodName = "checkRecord"; inline static const std::string checkRecordMethodName = "checkRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string getRecordMethodName = "getRecord"; inline static const std::string getRecordMethodName = "getRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string readAllMethodName = "readAllRecord"; inline static const std::string readAllMethodName = "readAllRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string replaceAllMethodName = "replaceAll"; inline static const std::string replaceAllMethodName = "replaceAll"; /**<\brief member function name, just for exceptions*/
inline static const std::string addRecordsMethodName = "addRecords"; inline static const std::string addRecordsMethodName = "addRecords"; /**<\brief member function name, just for exceptions*/
protected: protected:
template <class T> template <class K, class V>
int makeTable(MDB_txn* transaction); int makeStorage(MDB_txn* transaction, bool duplicates = false);
template <class T> template <class T>
static std::string toString(const T& value); static std::string toString(const T& value);
@ -81,28 +133,67 @@ protected:
template <class K, class V> template <class K, class V>
class Storage : public iStorage { class Storage : public iStorage {
friend class ::BaseTest;
friend class ::DuplicatesTest;
friend class ::CacheCursorTest;
friend class ::StorageCursorTest;
friend class Base; friend class Base;
friend class Cursor<K, V>;
protected: protected:
Storage(const std::string& name, Base* parent); Storage(Base* parent, const std::string& name, bool duplicates = false);
~Storage() override; ~Storage() override;
virtual void discoveredRecord(const K& key, const V& value) const;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const;
uint32_t flags() const;
virtual void addRecord(const K& key, const V& value, TransactionID txn);
virtual bool forceRecord(const K& key, const V& value, TransactionID txn);
virtual void changeRecord(const K& key, const V& value, TransactionID txn);
virtual void removeRecord(const K& key, TransactionID txn);
virtual bool checkRecord(const K& key, TransactionID txn) const;
virtual void getRecord(const K& key, V& value, TransactionID txn) const;
virtual V getRecord(const K& key, TransactionID txn) const;
virtual std::map<K, V> readAll(TransactionID txn) const;
virtual void readAll(std::map<K, V>& result, TransactionID txn) const;
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn);
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
public: public:
using iStorage::drop; using iStorage::drop;
virtual void addRecord(const K& key, const V& value); virtual void addRecord(const K& key, const V& value);
virtual void addRecord(const K& key, const V& value, const WriteTransaction& txn);
virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change
virtual bool forceRecord(const K& key, const V& value, const WriteTransaction& txn);
virtual void changeRecord(const K& key, const V& value); virtual void changeRecord(const K& key, const V& value);
virtual void changeRecord(const K& key, const V& value, const WriteTransaction& txn);
virtual void removeRecord(const K& key); virtual void removeRecord(const K& key);
virtual void removeRecord(const K& key, const WriteTransaction& txn);
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
virtual bool checkRecord(const K& key, const Transaction& txn) const;
virtual void getRecord(const K& key, V& value) const;
virtual void getRecord(const K& key, V& value, const Transaction& txn) const;
virtual V getRecord(const K& key) const; virtual V getRecord(const K& key) const;
virtual V getRecord(const K& key, const Transaction& txn) const;
virtual std::map<K, V> readAll() const; virtual std::map<K, V> readAll() const;
virtual std::map<K, V> readAll(const Transaction& txn) const;
virtual void readAll(std::map<K, V>& result) const;
virtual void readAll(std::map<K, V>& result, const Transaction& txn) const;
virtual void replaceAll(const std::map<K, V>& data); virtual void replaceAll(const std::map<K, V>& data);
virtual void replaceAll(const std::map<K, V>& data, const WriteTransaction& txn);
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false); virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
virtual uint32_t addRecords(const std::map<K, V>& data, const WriteTransaction& txn, bool overwrite = false);
Cursor<K, V> createCursor();
void destroyCursor(Cursor<K, V>& cursor);
protected: protected:
Serializer<K>* keySerializer; mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/
Serializer<V>* valueSerializer; mutable Serializer<V> valueSerializer; /**<\brief internal object that would serialize and deserialize values*/
std::map<uint32_t, Cursor<K, V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
int createTable(MDB_txn* transaction) override; int open(MDB_txn* transaction) override;
void close() override;
}; };
} }

File diff suppressed because it is too large Load Diff

154
src/transaction.cpp Normal file
View File

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

46
src/transaction.h Normal file
View File

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

View File

@ -4,11 +4,16 @@ include_directories(${GTEST_INCLUDE_DIR})
add_executable(runUnitTests add_executable(runUnitTests
basic.cpp basic.cpp
serialization.cpp
storagetransaction.cpp
cachetransaction.cpp
storagecursor.cpp
cachecursor.cpp
duplicates.cpp
) )
target_compile_options(runUnitTests PRIVATE -fPIC) target_compile_options(runUnitTests PRIVATE -fPIC -Wall -Wextra -O0)
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}_INCLUDE_DIRS})
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS}) target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
@ -17,7 +22,6 @@ target_link_libraries(
GTest::gtest_main GTest::gtest_main
${PROJECT_NAME} ${PROJECT_NAME}
Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Core
lmdb
) )
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(runUnitTests) gtest_discover_tests(runUnitTests)

View File

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

440
test/cachecursor.cpp Normal file
View File

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

281
test/cachetransaction.cpp Normal file
View File

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

497
test/duplicates.cpp Normal file
View File

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

339
test/serialization.cpp Normal file
View File

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

417
test/storagecursor.cpp Normal file
View File

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

279
test/storagetransaction.cpp Normal file
View File

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