Compare commits

...

49 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
48 changed files with 2890 additions and 695 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

View File

@ -1,15 +1,48 @@
# Changelog # Changelog
## LMDBAL 0.4.0 (August 13, 2023) # 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 ### Bug fixes
- possible cache unsync - 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 ### Improvements
- read only cursors
- some more documentation - some more documentation
- more tests - more tests
## LMDBAL 0.4.0 (August 13, 2023)
### New Features
- read only cursors
### Bug fixes
- possible cache unsync
- doxygen-awesome build bix - doxygen-awesome build bix
### Improvements
- some more documentation
- more tests
## LMDBAL 0.3.1 (April 14, 2023) ## LMDBAL 0.3.1 (April 14, 2023)
### Bug fixes ### Bug fixes
- build with qt5 now is possible again - build with qt5 now is possible again
@ -17,6 +50,7 @@
### Improvements ### Improvements
- exception documentation - exception documentation
## LMDBAL 0.3.0 (April 12, 2023) ## LMDBAL 0.3.0 (April 12, 2023)
### New features ### New features
- transaction functions - transaction functions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(LMDBAL project(LMDBAL
VERSION 0.4.0 VERSION 0.5.4
DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer" DESCRIPTION "LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer"
LANGUAGES CXX LANGUAGES CXX
) )
@ -12,7 +12,9 @@ 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) option(BUILD_DOXYGEN_AWESOME "Builds documentation alternative style" OFF)
include(GNUInstallDirs) include(GNUInstallDirs)
@ -21,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)
@ -54,14 +56,21 @@ 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)
@ -74,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
) )
@ -100,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

@ -7,10 +7,12 @@
### 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
@ -95,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,6 +1,12 @@
set(DOXYGEN_GENERATE_HTML YES) if (BUILD_DOC_HTML)
set(DOXYGEN_GENERATE_MAN YES) set(DOXYGEN_GENERATE_HTML YES)
set(DOXYGEN_GENERATE_XML YES) endif()
if (BUILD_DOC_MAN)
set(DOXYGEN_GENERATE_MAN YES)
endif()
if (BUILD_DOC_XML)
set(DOXYGEN_GENERATE_XML YES)
endif()
if (BUILD_DOXYGEN_AWESOME) if (BUILD_DOXYGEN_AWESOME)
include(ExternalProject) include(ExternalProject)
@ -36,12 +42,24 @@ doxygen_add_docs(
ALL ALL
COMMENT "Generate man and html pages" COMMENT "Generate man and html pages"
) )
install(DIRECTORY if (BUILD_DOC_MAN)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/man ${CMAKE_CURRENT_BINARY_DIR}/man
TYPE DOC
)
endif()
if (BUILD_DOC_HTML)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/html ${CMAKE_CURRENT_BINARY_DIR}/html
TYPE DOC
)
endif()
if (BUILD_DOC_XML)
install(DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/xml ${CMAKE_CURRENT_BINARY_DIR}/xml
TYPE DOC TYPE DOC
) )
endif()
if (BUILD_DOXYGEN_AWESOME) if (BUILD_DOXYGEN_AWESOME)
add_dependencies(documentation doxygen-awesome-css) add_dependencies(documentation doxygen-awesome-css)

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,17 +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 if you'll 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. * To discover how to store read and modify data take a look at LMDBAL::Storage and LMDBAL::Cache classes.
*/ */

View File

@ -1,17 +1,17 @@
# Maintainer: Yury Gubich <blue@macaw.me> # Maintainer: Yury Gubich <blue@macaw.me>
pkgname=lmdbal pkgname=lmdbal
pkgver=0.3.1 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::https://git.macaw.me/blue/lmdbal/archive/$pkgver.tar.gz") source=("$pkgname-$pkgver.tar.gz::https://git.macaw.me/blue/$pkgname/archive/$pkgver.tar.gz")
sha256sums=('df1a9687d81d609d160754285f2613d7e07fc6deb781d0fb0084e4857ea82e95') sha256sums=('SKIP')
build() { build() {
cd "$srcdir/$pkgname" cd "$srcdir/$pkgname"
cmake . -D CMAKE_INSTALL_PREFIX=/usr -D CMAKE_BUILD_TYPE=Release -D QT_VERSION_MAJOR=5 cmake . -D CMAKE_INSTALL_PREFIX=/usr -D CMAKE_BUILD_TYPE=Release -D QT_VERSION_MAJOR=5

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
@ -13,27 +14,12 @@ set(HEADERS
cursor.hpp 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,6 +19,7 @@
#include "base.h" #include "base.h"
#include "exceptions.h" #include "exceptions.h"
#include "storage.h" #include "storage.h"
#include "transaction.h"
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
@ -120,8 +121,7 @@ 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())
@ -148,10 +148,8 @@ 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)
@ -169,6 +167,17 @@ QString LMDBAL::Base::createDirectory() {
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 * \brief Returns database state
@ -190,38 +199,63 @@ void LMDBAL::Base::drop() {
if (!opened) if (!opened)
throw Closed("drop", name); throw Closed("drop", name);
TransactionID txn = beginTransaction(); TransactionID txn = beginPrivateTransaction(emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages) { for (const std::pair<const std::string, iStorage*>& pair : storages) {
int rc = pair.second->drop(txn); int rc = pair.second->drop(txn);
if (rc != MDB_SUCCESS) { if (rc != MDB_SUCCESS) {
abortTransaction(txn); abortPrivateTransaction(txn, emptyName);
throw Unknown(name, mdb_strerror(rc), pair.first); throw Unknown(name, mdb_strerror(rc), pair.first);
} }
} }
commitTransaction(txn);
commitPrivateTransaction(txn, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages)
pair.second->handleDrop();
} }
/** /**
* \brief Begins read-only transaction * \brief Begins read-only transaction
* *
* \returns read-only transaction ID * 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::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened * \exception LMDBAL::Unknown - thrown if something unexpected happened
*/ */
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const { LMDBAL::Transaction LMDBAL::Base::beginReadOnlyTransaction() const {
return beginReadOnlyTransaction(emptyName);} TransactionID id = beginReadOnlyTransaction(emptyName);
return Transaction(id, this);
}
/** /**
* \brief Begins writable transaction * \brief Begins writable transaction
* *
* \returns writable transaction ID * 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::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened * \exception LMDBAL::Unknown - thrown if something unexpected happened
*/ */
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const { LMDBAL::WriteTransaction LMDBAL::Base::beginTransaction() {
return beginTransaction(emptyName);} TransactionID id = beginTransaction(emptyName);
return WriteTransaction(id, this);
}
/** /**
* \brief Aborts transaction * \brief Aborts transaction
@ -368,10 +402,9 @@ void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string
LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::string& storageName) const { LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::string& storageName) const {
MDB_txn* txn; MDB_txn* txn;
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn); int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
if (rc != MDB_SUCCESS) { if (rc != MDB_SUCCESS)
mdb_txn_abort(txn);
throw Unknown(name, mdb_strerror(rc), storageName); throw Unknown(name, mdb_strerror(rc), storageName);
}
return txn; return txn;
} }

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; /**<I'm going to use transaction pointers as transaction IDs*/ 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,29 +66,30 @@ 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);
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*> Storages; /**<Storage and Cache pointers are saved in the std::map*/ typedef std::map<std::string, LMDBAL::iStorage*> Storages; /**<\brief Storage and Cache pointers are saved in the std::map*/
typedef std::set<TransactionID> Transactions; /**<Piblic transaction IDs are saved in the std::set*/ 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); void commitTransaction(TransactionID id, const std::string& storageName);
@ -114,7 +120,9 @@ private:
* Defines that the database is going to have the following storage. * Defines that the database is going to have the following storage.
* The LMDBAL::Base must be closed * The LMDBAL::Base must be closed
* *
* \param[in] _name - storage name * \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 * \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 K - key type of the storage
@ -124,14 +132,14 @@ private:
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add storage with repeating name * \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& _name) { LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
if (opened) { if (opened)
throw Opened(name, "add storage " + _name); throw Opened(name, "add storage " + storageName);
}
Storage<K, V>* storage = new Storage<K, V>(_name, this); Storage<K, V>* storage = new Storage<K, V>(this, storageName, duplicates);
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(_name, (iStorage*)storage)); std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)storage));
if (!pair.second) if (!pair.second)
throw StorageDuplicate(name, _name); throw StorageDuplicate(name, storageName);
return storage; return storage;
} }
@ -142,7 +150,7 @@ LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& _name) {
* Defines that the database is going to have the following cache. * Defines that the database is going to have the following cache.
* The LMDBAL::Base must be closed * The LMDBAL::Base must be closed
* *
* \param[in] _name - cache name * \param[in] storageName - cache name
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it * \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 K - key type of the cache
@ -152,14 +160,14 @@ LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& _name) {
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add cache with repeating name * \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& _name) { LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
if (opened) { if (opened)
throw Opened(name, "add cache " + _name); throw Opened(name, "add cache " + storageName);
}
Cache<K, V>* cache = new Cache<K, V>(_name, this); Cache<K, V>* cache = new Cache<K, V>(this, storageName, false);
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(_name, (iStorage*)cache)); std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)cache));
if (!pair.second) if (!pair.second)
throw StorageDuplicate(name, _name); throw StorageDuplicate(name, storageName);
return cache; return cache;
} }
@ -173,7 +181,7 @@ LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& _name) {
* this method with template parameters <std::string, std::string> * 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 * on the same name of the previously added storage, or calling it on cache - the behaviour is undefined
* *
* \param[in] _name - storage name * \param[in] storageName - storage name
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it * \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 K - key type of the storage
@ -182,8 +190,8 @@ LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& _name) {
* \exception std::out_of_range thrown if storage with the given name was not found * \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& _name) { LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& storageName) {
return static_cast<Storage<K, V>*>(storages.at(_name)); return static_cast<Storage<K, V>*>(storages.at(storageName));
} }
/** /**
@ -195,7 +203,7 @@ LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& _name) {
* this method with template parameters <std::string, std::string> * 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 * on the same name of the previously added cache, or calling it on storage - the behaviour is undefined
* *
* \param[in] _name - cache name * \param[in] storageName - cache name
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it * \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 K - key type of the cache
@ -204,8 +212,8 @@ LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& _name) {
* \exception std::out_of_range thrown if cache with the given name was not found * \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& _name) { LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& storageName) {
return static_cast<Cache<K, V>*>(storages.at(_name)); return static_cast<Cache<K, V>*>(storages.at(storageName));
} }
#endif //LMDBAL_BASE_H #endif //LMDBAL_BASE_H

View File

@ -51,15 +51,30 @@ class Cache : public Storage<K, V> {
typedef std::map<TransactionID, Queue> TransactionCache; 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 transactionStarted(TransactionID txn, bool readOnly) const override;
virtual void transactionCommited(TransactionID txn) override; virtual void transactionCommited(TransactionID txn) override;
virtual void transactionAborted(TransactionID txn) const 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) const override;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) 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;
@ -72,37 +87,34 @@ private:
void handleForceRecord(const K& key, const V& value, bool added); void handleForceRecord(const K& key, const V& value, bool added);
void handleReplaceAll(std::map<K, V>* data); void handleReplaceAll(std::map<K, V>* data);
void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize); void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize);
void handleDrop();
void appendToCache(const K& key, const V& value) const; void appendToCache(const K& key, const V& value) const;
public: public:
using Storage<K, V>::drop; using Storage<K, V>::drop;
virtual int drop(TransactionID transaction) override; 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 void addRecord(const K& key, const V& value, TransactionID txn) override;
virtual bool forceRecord(const K& key, const V& value) override; virtual bool forceRecord(const K& key, const V& value) override;
virtual bool forceRecord(const K& key, const V& value, TransactionID txn) override;
virtual void changeRecord(const K& key, const V& value) override; virtual void changeRecord(const K& key, const V& value) override;
virtual void changeRecord(const K& key, const V& value, TransactionID txn) override;
virtual void removeRecord(const K& key) override; virtual void removeRecord(const K& key) override;
virtual void removeRecord(const K& key, TransactionID txn) override;
virtual bool checkRecord(const K& key) const override; virtual bool checkRecord(const K& key) const override;
virtual bool checkRecord(const K& key, TransactionID txn) const override;
virtual void getRecord(const K& key, V& out) const override; virtual void getRecord(const K& key, V& out) const override;
virtual void getRecord(const K& key, V& out, TransactionID txn) const override;
virtual V getRecord(const K& key) const override; virtual V getRecord(const K& key) const override;
virtual V getRecord(const K& key, TransactionID txn) const override;
virtual SizeType count() const override; virtual SizeType count() const override;
virtual SizeType count(TransactionID txn) const override;
virtual std::map<K, V> readAll() const override; virtual std::map<K, V> readAll() const override;
virtual std::map<K, V> readAll(TransactionID txn) const override;
virtual void readAll(std::map<K, V>& out) const override; virtual void readAll(std::map<K, V>& out) const override;
virtual void readAll(std::map<K, V>& out, TransactionID txn) const override;
virtual void replaceAll(const std::map<K, V>& data) override; virtual void replaceAll(const std::map<K, V>& data) override;
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn) override;
virtual SizeType addRecords(const std::map<K, V>& data, bool overwrite = false) override; virtual SizeType addRecords(const std::map<K, V>& data, bool overwrite = false) override;
virtual SizeType addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false) override;
protected: protected:
/** /**

View File

@ -39,12 +39,13 @@
/** /**
* \brief Creates a cache * \brief Creates a cache
* *
* \param[in] _name - name of the new cache * \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] parent - parent database pointed (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& _name, Base* parent): LMDBAL::Cache<K, V>::Cache(Base* parent, const std::string& name, bool duplicates):
Storage<K, V>(_name, parent), Storage<K, V>(parent, name, duplicates),
mode(Mode::nothing), 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>()),
@ -74,8 +75,6 @@ void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value) {
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value, TransactionID txn) { void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
iStorage::ensureOpened(iStorage::addRecordMethodName);
if (cache->count(key) > 0) if (cache->count(key) > 0)
iStorage::throwDuplicate(iStorage::toString(key)); iStorage::throwDuplicate(iStorage::toString(key));
@ -108,8 +107,6 @@ bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value) {
template<class K, class V> template<class K, class V>
bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) { bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
iStorage::ensureOpened(iStorage::forceRecordMethodName);
bool added = Storage<K, V>::forceRecord(key, value, txn); bool added = Storage<K, V>::forceRecord(key, value, txn);
typename TransactionCache::iterator tc = transactionCache->find(txn); typename TransactionCache::iterator tc = transactionCache->find(txn);
@ -170,8 +167,6 @@ void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) { void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
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())
@ -247,8 +242,6 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
template<class K, class V> template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const { V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
V value; V value;
Cache<K, V>::getRecord(key, value, txn); Cache<K, V>::getRecord(key, value, txn);
return value; return value;
@ -256,8 +249,6 @@ V LMDBAL::Cache<K, V>::getRecord(const K& key, TransactionID txn) const {
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) const { void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
//if there are any changes made within this transaction //if there are any changes made within this transaction
//I will be able to see them among pending changes //I will be able to see them among pending changes
//so, I'm going to go through them in reverse order //so, I'm going to go through them in reverse order
@ -386,8 +377,6 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
template<class K, class V> template<class K, class V>
bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const { bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
iStorage::ensureOpened(iStorage::checkRecordMethodName);
//if there are any changes made within this transaction //if there are any changes made within this transaction
//I will be able to see them among pending changes //I will be able to see them among pending changes
//so, I'm going to go through them in reverse order //so, I'm going to go through them in reverse order
@ -496,8 +485,6 @@ void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out) const {
template<class K, class V> template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const { std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const {
iStorage::ensureOpened(iStorage::readAllMethodName);
std::map<K, V> out; std::map<K, V> out;
readAll(out, txn); readAll(out, txn);
@ -506,8 +493,6 @@ std::map<K, V> LMDBAL::Cache<K, V>::readAll(TransactionID txn) const {
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const { void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const {
iStorage::ensureOpened(iStorage::readAllMethodName);
typename TransactionCache::iterator tc = transactionCache->find(txn); typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) { if (tc != transactionCache->end()) {
Queue& queue = tc->second; Queue& queue = tc->second;
@ -564,7 +549,7 @@ void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const
} else { } else {
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
Storage<K, V>::readAll(out); //that are missing in the cache Storage<K, V>::readAll(out, txn); //that are missing in the cache
*cache = out; *cache = out;
abscent->clear(); abscent->clear();
sizeDifference = 0; sizeDifference = 0;
@ -675,8 +660,6 @@ void LMDBAL::Cache<K, V>::removeRecord(const K& key) {
template<class K, class V> template<class K, class V>
void LMDBAL::Cache<K, V>::removeRecord(const K& key, TransactionID txn) { void LMDBAL::Cache<K, V>::removeRecord(const K& key, TransactionID txn) {
iStorage::ensureOpened(iStorage::removeRecordMethodName);
bool noKey = false; bool noKey = false;
if (mode != Mode::full) if (mode != Mode::full)
noKey = cache->count(key) == 0; noKey = cache->count(key) == 0;
@ -728,7 +711,6 @@ uint32_t LMDBAL::Cache<K, V>::count() const {
template<class K, class V> template<class K, class V>
uint32_t LMDBAL::Cache<K, V>::count(TransactionID txn) const { uint32_t LMDBAL::Cache<K, V>::count(TransactionID txn) const {
int32_t diff = 0; int32_t diff = 0;
bool currentTransaction = false; bool currentTransaction = false;
typename TransactionCache::const_iterator tc = transactionCache->find(txn); typename TransactionCache::const_iterator tc = transactionCache->find(txn);
@ -799,10 +781,15 @@ void LMDBAL::Cache<K, V>::handleMode() const {
} }
template<class K, class V> template<class K, class V>
int LMDBAL::Cache<K, V>::drop(TransactionID transaction) { int LMDBAL::Cache<K, V>::drop(const WriteTransaction& transaction) {
int res = Storage<K, V>::drop(transaction); iStorage::ensureOpened(iStorage::dropMethodName);
TransactionID txn = iStorage::extractTransactionId(transaction, iStorage::dropMethodName);
int res = Storage<K, V>::drop(txn);
typename TransactionCache::iterator tc = transactionCache->find(transaction); if (res != MDB_SUCCESS)
return res;
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) if (tc != transactionCache->end())
tc->second.emplace_back(Operation::drop, nullptr); tc->second.emplace_back(Operation::drop, nullptr);
@ -854,27 +841,23 @@ void LMDBAL::Cache<K, V>::handleTransactionEntry(const Entry& entry) {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second); std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
handleAddRecord(pair->first, pair->second); handleAddRecord(pair->first, pair->second);
delete pair; delete pair;
} } break;
break;
case Operation::remove: { case Operation::remove: {
K* key = static_cast<K*>(entry.second); K* key = static_cast<K*>(entry.second);
handleRemoveRecord(*key); handleRemoveRecord(*key);
delete key; delete key;
} } break;
break;
case Operation::change: { case Operation::change: {
std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second); std::pair<K, V>* pair = static_cast<std::pair<K, V>*>(entry.second);
handleChangeRecord(pair->first, pair->second); handleChangeRecord(pair->first, pair->second);
delete pair; delete pair;
} } break;
case Operation::force: { case Operation::force: {
std::tuple<bool, K, V>* tuple = static_cast<std::tuple<bool, K, V>*>(entry.second); std::tuple<bool, K, V>* tuple = static_cast<std::tuple<bool, K, V>*>(entry.second);
const std::tuple<bool, K, V>& t = *tuple; const std::tuple<bool, K, V>& t = *tuple;
handleForceRecord(std::get<1>(t), std::get<2>(t), std::get<0>(t)); handleForceRecord(std::get<1>(t), std::get<2>(t), std::get<0>(t));
delete tuple; delete tuple;
} } break;
break;
case Operation::drop: case Operation::drop:
handleDrop(); handleDrop();
break; break;
@ -886,8 +869,7 @@ void LMDBAL::Cache<K, V>::handleTransactionEntry(const Entry& entry) {
const std::tuple<bool, SizeType, std::map<K, V>>& t = * tuple; const std::tuple<bool, SizeType, std::map<K, V>>& t = * tuple;
handleAddRecords(std::get<2>(t), std::get<0>(t), std::get<1>(t)); handleAddRecords(std::get<2>(t), std::get<0>(t), std::get<1>(t));
delete tuple; delete tuple;
} } break;
break;
} }
} }

View File

@ -24,6 +24,7 @@
#include "lmdb.h" #include "lmdb.h"
#include "base.h" #include "base.h"
#include "storage.h" #include "storage.h"
#include "transaction.h"
namespace LMDBAL { namespace LMDBAL {
@ -37,37 +38,50 @@ private:
openedPrivate /**< - opened with private transaction, only current storage will be notified when cursor is closed*/ openedPrivate /**< - opened with private transaction, only current storage will be notified when cursor is closed*/
}; };
public:
Cursor();
Cursor(Storage<K, V>* parent); Cursor(Storage<K, V>* parent);
Cursor(const Cursor& other) = delete;
Cursor(Cursor&& other);
~Cursor(); ~Cursor();
public: Cursor& operator = (const Cursor& other) = delete;
void open() const; Cursor& operator = (Cursor&& other);
void open(TransactionID txn) const;
void renew() const; void open();
void renew(TransactionID txn) const; void open(const Transaction& transaction);
void close() const; void renew();
void renew(const Transaction& transaction);
void close();
bool opened() const; bool opened() const;
bool empty() const;
std::pair<K, V> first() const; void drop();
std::pair<K, V> last() const;
std::pair<K, V> next() const; std::pair<K, V> first();
std::pair<K, V> prev() const; std::pair<K, V> last();
std::pair<K, V> next();
std::pair<K, V> prev();
std::pair<K, V> current() const; std::pair<K, V> current() const;
bool set(const K& target);
void first(K& key, V& value) const; void first(K& key, V& value);
void last(K& key, V& value) const; void last(K& key, V& value);
void next(K& key, V& value) const; void next(K& key, V& value);
void prev(K& key, V& value) const; void prev(K& key, V& value);
void current(K& key, V& value) const; void current(K& key, V& value) const;
private: private:
void terminated() const; 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; void operateCursorRead(K& key, V& value, MDB_cursor_op operation, const std::string& methodName, const std::string& operationName) const;
private: private:
Storage<K, V>* storage; Storage<K, V>* storage;
mutable MDB_cursor* cursor; MDB_cursor* cursor;
mutable State state; 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 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 closeCursorMethodName = "Cursor::close"; /**<\brief member function name, just for exceptions*/
@ -78,6 +92,7 @@ private:
inline static const std::string nextMethodName = "next"; /**<\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 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 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 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 lastOperationName = "Cursor::last"; /**<\brief member function name, just for exceptions*/

View File

@ -20,6 +20,7 @@
#define LMDBAL_CURSOR_HPP #define LMDBAL_CURSOR_HPP
#include "cursor.h" #include "cursor.h"
#include <iostream>
/** /**
* \class LMDBAL::Cursor * \class LMDBAL::Cursor
@ -40,6 +41,8 @@
* You are not supposed to instantiate or destory instances of this class yourself! * You are not supposed to instantiate or destory instances of this class yourself!
*/ */
static uint32_t idCounter = 0;
/** /**
* \brief Creates a cursor * \brief Creates a cursor
* *
@ -49,9 +52,70 @@ template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent): LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
storage(parent), storage(parent),
cursor(nullptr), cursor(nullptr),
state(closed) 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 * \brief Destroys a cursor
* *
@ -60,13 +124,67 @@ LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
template<class K, class V> template<class K, class V>
LMDBAL::Cursor<K, V>::~Cursor () { LMDBAL::Cursor<K, V>::~Cursor () {
close(); 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 * \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> template<class K, class V>
void LMDBAL::Cursor<K, V>::terminated () const { 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 close(); //for now it's the same, but if I ever going to make writable cursor - here is where it's gonna be different
} }
@ -79,16 +197,20 @@ void LMDBAL::Cursor<K, V>::terminated () const {
* This function should be called when the LMDBAL::Storage is already opened and before any query with 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). * It will do nothing to a cursor that was already opened (no matter what way).
* *
* \throws LMDBAL::Closed thrown if you try to open the cursor on a closed database * \exception LMDBAL::Closed thrown if you try to open the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb, or to begin a transaction * \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> template<class K, class V>
void LMDBAL::Cursor<K, V>::open () const { void LMDBAL::Cursor<K, V>::open () {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(openCursorMethodName); storage->ensureOpened(openCursorMethodName);
switch (state) { switch (state) {
case closed: { case closed: {
TransactionID txn = storage->beginReadOnlyTransaction(); TransactionID txn = storage->beginReadOnlyTransaction();
int result = mdb_cursor_open(txn, storage->dbi, &cursor); int result = storage->_mdbCursorOpen(txn, &cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn); storage->throwUnknown(result, txn);
@ -109,15 +231,23 @@ void LMDBAL::Cursor<K, V>::open () const {
* This function should be called when the LMDBAL::Storage is already opened and before any query with 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). * It will do nothing to a cursor that was already opened (no matter what way).
* *
* \throws LMDBAL::Closed thrown if you try to open the cursor on a closed database * \param[in] transaction - a transaction, can be read only
* \throws LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb *
* \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> template<class K, class V>
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const { void LMDBAL::Cursor<K, V>::open (const Transaction& transaction) {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(openCursorMethodName); storage->ensureOpened(openCursorMethodName);
TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName);
switch (state) { switch (state) {
case closed: { case closed: {
int result = mdb_cursor_open(txn, storage->dbi, &cursor); int result = storage->_mdbCursorOpen(txn, &cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result); storage->throwUnknown(result);
@ -141,22 +271,26 @@ void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
* *
* This function does nothing if the cursor is closed * This function does nothing if the cursor is closed
* *
* \throws LMDBAL::Closed thrown if you try to renew the cursor on a closed database * \exception LMDBAL::Closed thrown if you try to renew the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem beginning new transaction or if there was a problem renewing the cursor by lmdb * \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> template<class K, class V>
void LMDBAL::Cursor<K, V>::renew () const { void LMDBAL::Cursor<K, V>::renew () {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(renewCursorMethodName); storage->ensureOpened(renewCursorMethodName);
switch (state) { switch (state) {
case openedPrivate: { case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor); TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->abortTransaction(txn); storage->abortTransaction(txn);
storage->transactionAborted(txn); storage->transactionAborted(txn);
[[fallthrough]]; [[fallthrough]];
} }
case openedPublic: { case openedPublic: {
TransactionID txn = storage->beginReadOnlyTransaction(); TransactionID txn = storage->beginReadOnlyTransaction();
int result = mdb_cursor_renew(txn, cursor); int result = storage->_mdbCursorRenew(txn, cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn); storage->throwUnknown(result, txn);
@ -181,23 +315,29 @@ void LMDBAL::Cursor<K, V>::renew () const {
* *
* This function does nothing if the cursor is closed * This function does nothing if the cursor is closed
* *
* \param[in] txn a transaction you wish this cursor to be bound to * \param[in] transaction - a transaction you wish this cursor to be bound to
* *
* \throws LMDBAL::Closed thrown if you try to renew the cursor on a closed database * \exception LMDBAL::Closed thrown if you try to renew the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem renewing the cursor by lmdb * \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> template<class K, class V>
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const { void LMDBAL::Cursor<K, V>::renew (const Transaction& transaction) {
if (empty())
throw CursorEmpty(openCursorMethodName);
storage->ensureOpened(renewCursorMethodName); storage->ensureOpened(renewCursorMethodName);
TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName);
switch (state) { switch (state) {
case openedPrivate: { case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor); TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->abortTransaction(txn); storage->abortTransaction(txn);
storage->transactionAborted(txn); storage->transactionAborted(txn);
[[fallthrough]]; [[fallthrough]];
} }
case openedPublic: { case openedPublic: {
int result = mdb_cursor_renew(txn, cursor); int result = storage->_mdbCursorRenew(txn, cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result); storage->throwUnknown(result);
@ -219,16 +359,16 @@ void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
* This function does nothing on a closed cursor. * This function does nothing on a closed cursor.
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::close () const { void LMDBAL::Cursor<K, V>::close () {
switch (state) { switch (state) {
case openedPublic: { case openedPublic: {
mdb_cursor_close(cursor); storage->_mdbCursorClose(cursor);
state = closed; state = closed;
} break; } break;
case openedPrivate: { case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor); TransactionID txn = storage->_mdbCursorTxn(cursor);
mdb_cursor_close(cursor); storage->_mdbCursorClose(cursor);
storage->abortTransaction(txn); storage->abortTransaction(txn);
storage->transactionAborted(txn); storage->transactionAborted(txn);
@ -255,12 +395,12 @@ bool LMDBAL::Cursor<K, V>::opened () const {
* \param[out] key a reference to an object the key of queried element is going to be assigned * \param[out] 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[out] value a reference to an object the value of queried element is going to be assigned
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::first (K& key, V& value) const { void LMDBAL::Cursor<K, V>::first (K& key, V& value) {
operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName); operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
} }
@ -272,12 +412,12 @@ void LMDBAL::Cursor<K, V>::first (K& key, V& value) const {
* \param[out] key a reference to an object the key of queried element is going to be assigned * \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[out] value a reference to an object the value of queried element is going to be assigned
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::last (K& key, V& value) const { void LMDBAL::Cursor<K, V>::last (K& key, V& value) {
operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName); operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
} }
@ -295,12 +435,12 @@ void LMDBAL::Cursor<K, V>::last (K& key, V& value) const {
* \param[out] key a reference to an object the key of queried element is going to be assigned * \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[out] value a reference to an object the value of queried element is going to be assigned
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage * \exception LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::next (K& key, V& value) const { void LMDBAL::Cursor<K, V>::next (K& key, V& value) {
operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName); operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
} }
@ -318,12 +458,12 @@ void LMDBAL::Cursor<K, V>::next (K& key, V& value) const {
* \param[out] key a reference to an object the key of queried element is going to be assigned * \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[out] value a reference to an object the value of queried element is going to be assigned
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage * \exception LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::prev (K& key, V& value) const { void LMDBAL::Cursor<K, V>::prev (K& key, V& value) {
operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName); operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
} }
@ -337,9 +477,9 @@ void LMDBAL::Cursor<K, V>::prev (K& key, V& value) const {
* \param[out] key a reference to an object the key of queried element is going to be assigned * \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[out] value a reference to an object the value of queried element is going to be assigned
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about * \exception LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::current (K& key, V& value) const { void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
@ -353,12 +493,12 @@ void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
* *
* \returns std::pair where first is element key and second is element value * \returns std::pair where first is element key and second is element value
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::first () const { std::pair<K, V> LMDBAL::Cursor<K, V>::first () {
std::pair<K, V> result; std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName); operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
return result; return result;
@ -371,12 +511,12 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::first () const {
* *
* \returns std::pair where first is element key and second is element value * \returns std::pair where first is element key and second is element value
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::last () const { std::pair<K, V> LMDBAL::Cursor<K, V>::last () {
std::pair<K, V> result; std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName); operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
return result; return result;
@ -395,12 +535,12 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::last () const {
* *
* \returns std::pair where first is element key and second is element value * \returns std::pair where first is element key and second is element value
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage * \exception LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::next () const { std::pair<K, V> LMDBAL::Cursor<K, V>::next () {
std::pair<K, V> result; std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName); operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
return result; return result;
@ -419,12 +559,12 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::next () const {
* *
* \returns std::pair where first is element key and second is element value * \returns std::pair where first is element key and second is element value
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage * \exception LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::prev () const { std::pair<K, V> LMDBAL::Cursor<K, V>::prev () {
std::pair<K, V> result; std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName); operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
return result; return result;
@ -439,9 +579,9 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::prev () const {
* *
* \returns std::pair where first is element key and second is element value * \returns std::pair where first is element key and second is element value
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about * \exception LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
* \throws LMDBAL::Unknown thrown if there was no positioning operation before of if there was some unexpected problem with lmdb * \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> template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::current () const { std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
@ -450,6 +590,34 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
return result; 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 * \brief a private mothod that actually doing all the reading
* *
@ -461,9 +629,9 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
* \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] 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 * \param[in] operationName a name of the opeartion, just for the exception message if something goes not as expected
* *
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound mostly thrown if the query wasn't found * \exception LMDBAL::NotFound mostly thrown if the query wasn't found
* \throws LMDBAL::Unknown mostly thrown if there was some unexpected problem with lmdb * \exception LMDBAL::Unknown mostly thrown if there was some unexpected problem with lmdb
*/ */
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::operateCursorRead( void LMDBAL::Cursor<K, V>::operateCursorRead(
@ -477,7 +645,7 @@ void LMDBAL::Cursor<K, V>::operateCursorRead(
storage->throwCursorNotReady(methodName); storage->throwCursorNotReady(methodName);
MDB_val mdbKey, mdbValue; MDB_val mdbKey, mdbValue;
int result = mdb_cursor_get(cursor, &mdbKey, &mdbValue, operation); int result = storage->_mdbCursorGet(cursor, mdbKey, mdbValue, operation);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwNotFoundOrUnknown(result, operationName); storage->throwNotFoundOrUnknown(result, operationName);
@ -487,7 +655,7 @@ void LMDBAL::Cursor<K, V>::operateCursorRead(
if (state == openedPrivate) if (state == openedPrivate)
storage->discoveredRecord(key, value); storage->discoveredRecord(key, value);
else else
storage->discoveredRecord(key, value, mdb_cursor_txn(cursor)); storage->discoveredRecord(key, value, storage->_mdbCursorTxn(cursor));
} }
#endif //LMDBAL_CURSOR_HPP #endif //LMDBAL_CURSOR_HPP

View File

@ -72,6 +72,14 @@ std::string LMDBAL::CursorNotReady::getMessage() const {
return msg; 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),
@ -112,7 +120,6 @@ std::string LMDBAL::StorageDuplicate::getMessage() const {
+ " but the database already has a storage with given name"; + " 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,
@ -130,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

@ -97,6 +97,23 @@ private:
std::string tableName; 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 * \brief Thrown if something in the database was called on opened state and it is not supported
*/ */
@ -177,6 +194,27 @@ 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 * \brief Thrown if something unexpected happened
*/ */

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

@ -23,11 +23,12 @@
/** /**
* \class LMDBAL::Serializer * \class LMDBAL::Serializer
* \brief A class handling serialization/deserialization
* *
* A class that is constructed in every LMDBAL::Storage * A class that is constructed in every LMDBAL::Storage
* to serialize or deserialize keys and values. * 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>MDB_val * 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 K type of the keys of the storage
* \tparam V type of the values of the storage * \tparam V type of the values of the storage

View File

@ -23,6 +23,8 @@
/** /**
* \class LMDBAL::iStorage * \class LMDBAL::iStorage
* *
* \brief Storage interface
*
* This is a interface-like class, it's designed to be an inner database interface to * 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 * be used as a polymorphic entity, and provide protected interaction with the database
* from the heirs code * from the heirs code
@ -30,11 +32,16 @@
/** /**
* \brief Constructs a storage interface * \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(const std::string& p_name, Base* parent): LMDBAL::iStorage::iStorage(Base* parent, const std::string& name, bool duplicates):
dbi(), dbi(),
db(parent), db(parent),
name(p_name) name(name),
duplicates(duplicates)
{} {}
/** /**
@ -49,6 +56,23 @@ void LMDBAL::iStorage::close() {
mdb_dbi_close(db->environment, dbi); 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 * \brief Drops content of a storage interface
@ -61,7 +85,7 @@ void LMDBAL::iStorage::close() {
void LMDBAL::iStorage::drop() { void LMDBAL::iStorage::drop() {
ensureOpened(dropMethodName); ensureOpened(dropMethodName);
TransactionID txn = db->beginTransaction(); TransactionID txn = beginTransaction();
int rc = iStorage::drop(txn); int rc = iStorage::drop(txn);
if (rc != MDB_SUCCESS) { if (rc != MDB_SUCCESS) {
abortTransaction(txn); abortTransaction(txn);
@ -69,6 +93,7 @@ void LMDBAL::iStorage::drop() {
} }
db->commitTransaction(txn); db->commitTransaction(txn);
handleDrop();
} }
/** /**
@ -83,6 +108,21 @@ int LMDBAL::iStorage::drop(TransactionID transaction) {
return mdb_drop(transaction, dbi, 0); return mdb_drop(transaction, dbi, 0);
} }
/**
* \brief Drops content of a storage interface (public transaction variant)
*
* Just performs content drop
*
* \param[in] txn - transaction ID, must be writable transaction!
* \returns MDB_SUCCESS if everything went fine, MDB_<error> code otherwise
*
* \exception LMDBAL::TransactionTerminated thrown if the transaction was not active
*/
int LMDBAL::iStorage::drop(const WriteTransaction& txn) {
ensureOpened(dropMethodName);
return drop(extractTransactionId(txn, dropMethodName));
}
/** /**
* \brief Helper function, thows exception if the database is not opened * \brief Helper function, thows exception if the database is not opened
* *
@ -120,7 +160,7 @@ LMDBAL::SizeType LMDBAL::iStorage::count() const {
} }
/** /**
* \brief Storage size (transaction variant) * \brief Storage size (private transaction variant)
* *
* \param[in] txn - transaction ID, can be read-only transaction * \param[in] txn - transaction ID, can be read-only transaction
* \returns amount of records in the storage * \returns amount of records in the storage
@ -136,6 +176,21 @@ LMDBAL::SizeType LMDBAL::iStorage::count(TransactionID txn) const {
return stat.ms_entries; 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) * \brief Throws LMDBAL::Exist or LMDBAL::Unknown (transaction vairiant)
* *
@ -384,5 +439,71 @@ void LMDBAL::iStorage::transactionCommited(LMDBAL::TransactionID txn) {
void LMDBAL::iStorage::transactionAborted(LMDBAL::TransactionID txn) const { void LMDBAL::iStorage::transactionAborted(LMDBAL::TransactionID txn) const {
UNUSED(txn);} 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,20 +19,26 @@
#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 "cursor.h"
#include "transaction.h"
class BaseTest;
class DuplicatesTest;
class CacheCursorTest;
class StorageCursorTest;
namespace LMDBAL { namespace LMDBAL {
typedef uint32_t SizeType;
class iStorage { class iStorage {
friend class Base; friend class Base;
public: public:
protected: protected:
iStorage(const std::string& name, Base* parent); iStorage(Base* parent, const std::string& name, bool duplicates = false);
virtual ~iStorage(); virtual ~iStorage();
/** /**
@ -43,6 +49,7 @@ protected:
*/ */
virtual int open(MDB_txn * transaction) = 0; virtual int open(MDB_txn * transaction) = 0;
virtual void close(); virtual void close();
virtual void handleDrop();
bool isDBOpened() const; bool isDBOpened() const;
const std::string& dbName() const; const std::string& dbName() const;
@ -59,6 +66,7 @@ protected:
void throwNotFound(const std::string& key) const; void throwNotFound(const std::string& key) const;
void throwCursorNotReady(const std::string& method) const; void throwCursorNotReady(const std::string& method) const;
TransactionID extractTransactionId(const Transaction& txn, const std::string& action = "") const;
TransactionID beginReadOnlyTransaction() const; TransactionID beginReadOnlyTransaction() const;
TransactionID beginTransaction() const; TransactionID beginTransaction() const;
void commitTransaction(TransactionID id); void commitTransaction(TransactionID id);
@ -66,20 +74,44 @@ protected:
virtual void transactionStarted(TransactionID txn, bool readOnly) const; virtual void transactionStarted(TransactionID txn, bool readOnly) const;
virtual void transactionCommited(TransactionID txn); virtual void transactionCommited(TransactionID txn);
virtual void transactionAborted(TransactionID txn) const; 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 int drop(TransactionID transaction); virtual int drop(const WriteTransaction& txn);
virtual SizeType count() const; virtual SizeType count() const;
virtual SizeType count(TransactionID txn) const; virtual SizeType count(const Transaction& txn) const;
protected: protected:
MDB_dbi dbi; /**<\brief lmdb storage handle*/ MDB_dbi dbi; /**<\brief lmdb storage handle*/
Base* db; /**<\brief parent database pointer (borrowed)*/ Base* db; /**<\brief parent database pointer (borrowed)*/
const std::string name; /**<\brief this storage 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"; /**<\brief member function name, just for exceptions*/ inline static const std::string dropMethodName = "drop"; /**<\brief member function name, just for exceptions*/
inline static const std::string countMethodName = "count"; /**<\brief member function name, just for exceptions*/ inline static const std::string countMethodName = "count"; /**<\brief member function name, just for exceptions*/
inline static const std::string flagsMethodName = "flags"; /**<\brief member function name, just for exceptions*/
inline static const std::string addRecordMethodName = "addRecord"; /**<\brief member function name, just for exceptions*/ inline static const std::string addRecordMethodName = "addRecord"; /**<\brief member function name, just for exceptions*/
inline static const std::string forceRecordMethodName = "forceRecord"; /**<\brief member function name, just for exceptions*/ inline static const std::string forceRecordMethodName = "forceRecord"; /**<\brief member function name, just for exceptions*/
@ -92,8 +124,8 @@ protected:
inline static const std::string addRecordsMethodName = "addRecords"; /**<\brief member function name, just for exceptions*/ 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 makeStorage(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);
@ -101,47 +133,64 @@ 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>; 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) const;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) 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, TransactionID txn); 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, TransactionID txn); 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, TransactionID txn); 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, TransactionID txn); 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, TransactionID txn) const; 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;
virtual void getRecord(const K& key, V& value, TransactionID txn) 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, TransactionID txn) 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(TransactionID txn) 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;
virtual void readAll(std::map<K, V>& result, TransactionID txn) 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, TransactionID txn); 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, TransactionID txn, bool overwrite = false); virtual uint32_t addRecords(const std::map<K, V>& data, const WriteTransaction& txn, bool overwrite = false);
Cursor<K, V>* createCursor(); Cursor<K, V> createCursor();
void destroyCursor(Cursor<K, V>* cursor); void destroyCursor(Cursor<K, V>& cursor);
protected: protected:
mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/ mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/
mutable Serializer<V> valueSerializer; /**<\brief internal object that would serialize and deserialize values*/ mutable Serializer<V> valueSerializer; /**<\brief internal object that would serialize and deserialize values*/
std::set<Cursor<K, V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/ std::map<uint32_t, Cursor<K, V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
int open(MDB_txn* transaction) override; int open(MDB_txn* transaction) override;
void close() 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

@ -9,11 +9,11 @@ add_executable(runUnitTests
cachetransaction.cpp cachetransaction.cpp
storagecursor.cpp storagecursor.cpp
cachecursor.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})
@ -22,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,10 +113,22 @@ 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, AddingKeysToVariableCache) {
EXPECT_EQ(db->ready(), true);
c2->addRecord("regrets", "blah balah");
c2->addRecord("fossil fingers", 842);
c2->addRecord("preloaded cut", 539.75);
c2->addRecord("dihotomy", false);
EXPECT_EQ(c2->getRecord("regrets"), "blah balah");
EXPECT_EQ(c2->getRecord("fossil fingers"), 842);
EXPECT_EQ(c2->getRecord("preloaded cut"), 539.75);
EXPECT_EQ(c2->getRecord("dihotomy"), false);
} }
TEST_F(BaseTest, AddingRepeatingKey) { TEST_F(BaseTest, AddingRepeatingKey) {
@ -94,6 +142,9 @@ TEST_F(BaseTest, AddingRepeatingKey) {
EXPECT_THROW(c1->addRecord(-4, "world"), LMDBAL::Exist); EXPECT_THROW(c1->addRecord(-4, "world"), LMDBAL::Exist);
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) {
@ -102,6 +153,7 @@ TEST_F(BaseTest, GettingNotExistingKeys) {
EXPECT_THROW(t2->getRecord("almonds"), LMDBAL::NotFound); EXPECT_THROW(t2->getRecord("almonds"), LMDBAL::NotFound);
EXPECT_THROW(t1->getRecord(64), LMDBAL::NotFound); EXPECT_THROW(t1->getRecord(64), LMDBAL::NotFound);
EXPECT_THROW(c1->getRecord(21), LMDBAL::NotFound); EXPECT_THROW(c1->getRecord(21), LMDBAL::NotFound);
EXPECT_THROW(c2->getRecord("praising"), LMDBAL::NotFound);
} }
TEST_F(BaseTest, Persistence) { TEST_F(BaseTest, Persistence) {
@ -109,6 +161,7 @@ TEST_F(BaseTest, Persistence) {
uint32_t t1Size = t1->count(); uint32_t t1Size = t1->count();
uint32_t t2Size = t2->count(); uint32_t t2Size = t2->count();
uint32_t c1Size = c1->count(); uint32_t c1Size = c1->count();
uint32_t c2Size = c2->count();
db->close(); db->close();
delete db; delete db;
@ -117,6 +170,7 @@ 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->count(), t1Size);
@ -134,8 +188,8 @@ TEST_F(BaseTest, Persistence) {
EXPECT_EQ(t2->count(), t2Size); EXPECT_EQ(t2->count(), t2Size);
EXPECT_EQ(c1->count(), c1Size); EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c1->checkRecord(-116), true); EXPECT_EQ(c1->checkRecord(40), true);
EXPECT_EQ(c1->getRecord(-116), "whatever"); EXPECT_EQ(c1->getRecord(40), "whatever");
EXPECT_EQ(c1->checkRecord(-4), true); 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");
@ -145,9 +199,16 @@ TEST_F(BaseTest, Persistence) {
EXPECT_EQ(c1->getRecord(2), "blah balah"); EXPECT_EQ(c1->getRecord(2), "blah balah");
EXPECT_EQ(c1->count(), c1Size); EXPECT_EQ(c1->count(), c1Size);
EXPECT_EQ(c2->count(), c2Size);
EXPECT_EQ(c2->getRecord("regrets"), "blah balah");
EXPECT_EQ(c2->getRecord("fossil fingers"), 842);
EXPECT_EQ(c2->getRecord("preloaded cut"), 539.75);
EXPECT_EQ(c2->getRecord("dihotomy"), false);
EXPECT_THROW(t2->getRecord("cats"), LMDBAL::NotFound); EXPECT_THROW(t2->getRecord("cats"), LMDBAL::NotFound);
EXPECT_THROW(t1->getRecord(7893), LMDBAL::NotFound); EXPECT_THROW(t1->getRecord(7893), LMDBAL::NotFound);
EXPECT_THROW(c1->getRecord(89), LMDBAL::NotFound); EXPECT_THROW(c1->getRecord(89), LMDBAL::NotFound);
EXPECT_THROW(c2->getRecord("marathons"), LMDBAL::NotFound);
} }
TEST_F(BaseTest, CountAndDrop) { TEST_F(BaseTest, CountAndDrop) {
@ -155,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) {
@ -177,37 +241,45 @@ 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(t1->changeRecord(37, 49), LMDBAL::NotFound);
EXPECT_THROW(t2->changeRecord("precision", "cryoplastics"), LMDBAL::NotFound); EXPECT_THROW(t2->changeRecord("precision", "cryoplastics"), LMDBAL::NotFound);
EXPECT_THROW(c1->changeRecord(-62, "sharks"), 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) {
@ -219,21 +291,30 @@ TEST_F(BaseTest, Force) {
EXPECT_EQ(t2->forceRecord("lawfirm", "paracetamol"), false); //changing EXPECT_EQ(t2->forceRecord("lawfirm", "paracetamol"), false); //changing
EXPECT_EQ(c1->forceRecord(89, "canine"), false); //changing EXPECT_EQ(c1->forceRecord(89, "canine"), false); //changing
EXPECT_EQ(c1->forceRecord(98, "duration"), true); //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) { TEST_F(BaseTest, ReadAll) {
@ -242,21 +323,29 @@ TEST_F(BaseTest, ReadAll) {
std::map<uint32_t, uint32_t> m1 = t1->readAll(); std::map<uint32_t, uint32_t> m1 = t1->readAll();
std::map<QString, QString> m2 = t2->readAll(); std::map<QString, QString> m2 = t2->readAll();
std::map<int8_t, std::string> m3 = c1->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(2), 49);
EXPECT_EQ(m1.at(58), 35); EXPECT_EQ(m1.at(58), 35);
EXPECT_EQ(m1.at(68), 36); EXPECT_EQ(m1.at(68), 36);
EXPECT_EQ(m2.at("sdfhga"), "void"); EXPECT_EQ(m2.at("sdfhga"), "void");
EXPECT_EQ(m2.at("prophecy"), "dumpling"); EXPECT_EQ(m2.at("prophecy"), "dumpling");
EXPECT_EQ(m2.at("lawfirm"), "paracetamol"); EXPECT_EQ(m2.at("lawfirm"), "paracetamol");
EXPECT_EQ(m3.at(15), "recording"); EXPECT_EQ(m3.at(15), "recording");
EXPECT_EQ(m3.at(12), "thermal"); EXPECT_EQ(m3.at(12), "thermal");
EXPECT_EQ(m3.at(89), "canine"); EXPECT_EQ(m3.at(89), "canine");
EXPECT_EQ(m3.at(98), "duration"); 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(m1.size(), 3);
EXPECT_EQ(m2.size(), 3); EXPECT_EQ(m2.size(), 3);
EXPECT_EQ(m3.size(), 4); EXPECT_EQ(m3.size(), 4);
EXPECT_EQ(m4.size(), 3);
} }
TEST_F(BaseTest, ReplaceAll) { TEST_F(BaseTest, ReplaceAll) {
@ -274,10 +363,14 @@ TEST_F(BaseTest, ReplaceAll) {
{"ronin", "cheese"} {"ronin", "cheese"}
}); });
c1->replaceAll({}); c1->replaceAll({});
c2->replaceAll({
{"kind", 73}
});
EXPECT_EQ(t1->count(), 4); EXPECT_EQ(t1->count(), 4);
EXPECT_EQ(t2->count(), 3); EXPECT_EQ(t2->count(), 3);
EXPECT_EQ(c1->count(), 0); EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 1);
EXPECT_FALSE(t1->checkRecord(2)); EXPECT_FALSE(t1->checkRecord(2));
EXPECT_FALSE(t1->checkRecord(58)); EXPECT_FALSE(t1->checkRecord(58));
@ -292,6 +385,10 @@ TEST_F(BaseTest, ReplaceAll) {
EXPECT_FALSE(c1->checkRecord(89)); EXPECT_FALSE(c1->checkRecord(89));
EXPECT_FALSE(c1->checkRecord(98)); 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(7), 48);
EXPECT_EQ(t1->getRecord(194), 582); EXPECT_EQ(t1->getRecord(194), 582);
EXPECT_EQ(t1->getRecord(857), 39); EXPECT_EQ(t1->getRecord(857), 39);
@ -301,6 +398,8 @@ TEST_F(BaseTest, ReplaceAll) {
EXPECT_EQ(t2->getRecord("cluster"), "throttle"); EXPECT_EQ(t2->getRecord("cluster"), "throttle");
EXPECT_EQ(t2->getRecord("ronin"), "cheese"); EXPECT_EQ(t2->getRecord("ronin"), "cheese");
EXPECT_EQ(c2->getRecord("kind"), 73);
c1->replaceAll({ c1->replaceAll({
{68, "quality"}, {68, "quality"},
{31, "ridgid body"}, {31, "ridgid body"},
@ -317,8 +416,6 @@ TEST_F(BaseTest, ReplaceAll) {
EXPECT_EQ(c1->getRecord(-117), "lance of Michael"); EXPECT_EQ(c1->getRecord(-117), "lance of Michael");
} }
TEST_F(BaseTest, AddRecords) { TEST_F(BaseTest, AddRecords) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
@ -447,4 +544,33 @@ TEST_F(BaseTest, AddRecords) {
EXPECT_EQ(c1->getRecord(-32), "stitched"); EXPECT_EQ(c1->getRecord(-32), "stitched");
EXPECT_EQ(c1->getRecord(123), "horibly unforseen"); 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);
} }

View File

@ -23,28 +23,30 @@ protected:
} }
} }
int getCacheCursorsSize() const {
return cache->cursors.size();
}
static void TearDownTestSuite() { static void TearDownTestSuite() {
cursor.drop();
transaction.terminate();
db->close(); db->close();
db->removeDirectory(); db->removeDirectory();
delete db; delete db;
db = nullptr; db = nullptr;
cursor = nullptr;
emptyCursor = nullptr;
} }
static LMDBAL::Base* db; static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string>* cursor; static LMDBAL::Cursor<uint64_t, std::string> cursor;
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor; static LMDBAL::Transaction transaction;
static LMDBAL::TransactionID transaction;
LMDBAL::Cache<uint64_t, std::string>* cache; LMDBAL::Cache<uint64_t, std::string>* cache;
LMDBAL::Cache<uint64_t, std::string>* emptyCache; LMDBAL::Cache<uint64_t, std::string>* emptyCache;
}; };
LMDBAL::Base* CacheCursorTest::db = nullptr; LMDBAL::Base* CacheCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string>* CacheCursorTest::cursor = nullptr; LMDBAL::Cursor<uint64_t, std::string> CacheCursorTest::cursor;
LMDBAL::Cursor<uint64_t, std::string>* CacheCursorTest::emptyCursor = nullptr; LMDBAL::Transaction CacheCursorTest::transaction;
LMDBAL::TransactionID CacheCursorTest::transaction = nullptr;
static const std::map<uint64_t, std::string> data({ static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"}, {245665783, "bothering nerds"},
@ -65,22 +67,24 @@ TEST_F(CacheCursorTest, PopulatingTheTable) {
} }
TEST_F(CacheCursorTest, Creation) { TEST_F(CacheCursorTest, Creation) {
EXPECT_EQ(getCacheCursorsSize(), 0);
cursor = cache->createCursor(); cursor = cache->createCursor();
emptyCursor = emptyCache->createCursor(); EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_THROW(cursor->first(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->last(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
cursor->open(); cursor.open();
} }
TEST_F(CacheCursorTest, FirstPrivate) { TEST_F(CacheCursorTest, FirstPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -89,20 +93,21 @@ TEST_F(CacheCursorTest, FirstPrivate) {
} }
TEST_F(CacheCursorTest, NextPrivate) { TEST_F(CacheCursorTest, NextPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++; reference++;
for (; reference != data.end(); ++reference) { for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->next(); std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
} }
EXPECT_THROW(cursor->next(), LMDBAL::NotFound); EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin(); reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -111,9 +116,10 @@ TEST_F(CacheCursorTest, NextPrivate) {
} }
TEST_F(CacheCursorTest, LastPrivate) { TEST_F(CacheCursorTest, LastPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->last(); std::pair<uint64_t, std::string> element = cursor.last();
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -122,20 +128,21 @@ TEST_F(CacheCursorTest, LastPrivate) {
} }
TEST_F(CacheCursorTest, PrevPrivate) { TEST_F(CacheCursorTest, PrevPrivate) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++; reference++;
for (; reference != data.rend(); ++reference) { for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->prev(); std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
} }
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound); EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->last(); std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin(); reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -144,29 +151,30 @@ TEST_F(CacheCursorTest, PrevPrivate) {
} }
TEST_F(CacheCursorTest, CurrentPrivate) { TEST_F(CacheCursorTest, CurrentPrivate) {
std::pair<uint64_t, std::string> element = cursor->first(); EXPECT_EQ(getCacheCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
cursor->next(); cursor.next();
cursor->prev(); cursor.prev();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
++reference; ++reference;
--reference; --reference;
@ -176,26 +184,62 @@ TEST_F(CacheCursorTest, CurrentPrivate) {
EXPECT_EQ(cache->count(), data.size()); 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) { TEST_F(CacheCursorTest, Destruction) {
cursor->close(); EXPECT_EQ(getCacheCursorsSize(), 1);
cursor.close();
EXPECT_EQ(getCacheCursorsSize(), 1);
EXPECT_THROW(cursor->first(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->last(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
EXPECT_THROW(emptyCache->destroyCursor(cursor), LMDBAL::Unknown); cursor = LMDBAL::Cursor<uint64_t, std::string>();
cache->destroyCursor(cursor); EXPECT_EQ(getCacheCursorsSize(), 0);
cursor = cache->createCursor(); cursor = cache->createCursor();
EXPECT_EQ(getCacheCursorsSize(), 1);
} }
TEST_F(CacheCursorTest, FirstPublic) { TEST_F(CacheCursorTest, FirstPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
transaction = db->beginTransaction(); transaction = db->beginTransaction();
cursor->open(transaction); cursor.open(transaction);
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -204,20 +248,21 @@ TEST_F(CacheCursorTest, FirstPublic) {
} }
TEST_F(CacheCursorTest, NextPublic) { TEST_F(CacheCursorTest, NextPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++; reference++;
for (; reference != data.end(); ++reference) { for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->next(); std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
} }
EXPECT_THROW(cursor->next(), LMDBAL::NotFound); EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin(); reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -225,7 +270,8 @@ TEST_F(CacheCursorTest, NextPublic) {
} }
TEST_F(CacheCursorTest, LastPublic) { TEST_F(CacheCursorTest, LastPublic) {
std::pair<uint64_t, std::string> element = cursor->last(); 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(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -234,19 +280,20 @@ TEST_F(CacheCursorTest, LastPublic) {
} }
TEST_F(CacheCursorTest, PrevPublic) { TEST_F(CacheCursorTest, PrevPublic) {
EXPECT_EQ(getCacheCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++; reference++;
for (; reference != data.rend(); ++reference) { for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->prev(); std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
} }
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound); EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor->last(); std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin(); reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -255,29 +302,30 @@ TEST_F(CacheCursorTest, PrevPublic) {
} }
TEST_F(CacheCursorTest, CurrentPublic) { TEST_F(CacheCursorTest, CurrentPublic) {
std::pair<uint64_t, std::string> element = cursor->first(); EXPECT_EQ(getCacheCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(cache->count(), data.size()); EXPECT_EQ(cache->count(), data.size());
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
cursor->next(); cursor.next();
cursor->prev(); cursor.prev();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
++reference; ++reference;
--reference; --reference;
@ -287,40 +335,106 @@ TEST_F(CacheCursorTest, CurrentPublic) {
EXPECT_EQ(cache->count(), data.size()); 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) { TEST_F(CacheCursorTest, CornerCases) {
db->abortTransaction(transaction); transaction.terminate();
EXPECT_THROW(cursor->current(), LMDBAL::Unknown); EXPECT_THROW(cursor.current(), LMDBAL::Unknown);
cursor->close(); cursor.close();
emptyCursor->open(); LMDBAL::Cursor<uint64_t, std::string> emptyCursor = emptyCache->createCursor();
EXPECT_THROW(emptyCursor->first(), LMDBAL::NotFound); emptyCursor.open();
EXPECT_THROW(emptyCursor->last(), LMDBAL::NotFound); EXPECT_THROW(emptyCursor.first(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->next(), LMDBAL::NotFound); EXPECT_THROW(emptyCursor.last(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->prev(), LMDBAL::NotFound); EXPECT_THROW(emptyCursor.next(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->current(), LMDBAL::Unknown); EXPECT_THROW(emptyCursor.prev(), LMDBAL::NotFound);
emptyCursor->close(); EXPECT_THROW(emptyCursor.current(), LMDBAL::Unknown);
emptyCursor.close();
cursor->open(); cursor.open();
EXPECT_THROW(cursor->current(), LMDBAL::Unknown); //yeah, nice thing to write in the doc 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::map<uint64_t, std::string>::const_reverse_iterator breference = data.rbegin();
std::pair<uint64_t, std::string> element(cursor->prev()); std::pair<uint64_t, std::string> element(cursor.prev());
EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again! EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again!
EXPECT_EQ(element.second, breference->second); EXPECT_EQ(element.second, breference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(element.first, breference->first); EXPECT_EQ(element.first, breference->first);
EXPECT_EQ(element.second, breference->second); EXPECT_EQ(element.second, breference->second);
EXPECT_THROW(cursor->next(), LMDBAL::NotFound); EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
cursor->close(); cursor.close();
cursor->open(); cursor.open();
element = cursor->next(); element = cursor.next();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound); EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
} }

View File

@ -64,7 +64,7 @@ TEST_F(CacheTransactionsTest, Adding) {
EXPECT_EQ(c1->count(), 0); EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 0); EXPECT_EQ(c2->count(), 0);
LMDBAL::TransactionID txn = db->beginTransaction(); LMDBAL::WriteTransaction txn = db->beginTransaction();
c1->addRecord(5, 13, txn); c1->addRecord(5, 13, txn);
c1->addRecord(-53, 782, txn); c1->addRecord(-53, 782, txn);
c1->addRecord(5892, -37829, txn); c1->addRecord(5892, -37829, txn);
@ -76,7 +76,7 @@ TEST_F(CacheTransactionsTest, Adding) {
EXPECT_EQ(c1->count(), 0); EXPECT_EQ(c1->count(), 0);
EXPECT_EQ(c2->count(), 0); EXPECT_EQ(c2->count(), 0);
db->commitTransaction(txn); txn.commit();
EXPECT_EQ(c1->count(), 3); EXPECT_EQ(c1->count(), 3);
EXPECT_EQ(c1->getRecord(5), 13); EXPECT_EQ(c1->getRecord(5), 13);
@ -95,7 +95,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
LMDBAL::SizeType s1 = c1->count(); LMDBAL::SizeType s1 = c1->count();
LMDBAL::SizeType s2 = c2->count(); LMDBAL::SizeType s2 = c2->count();
LMDBAL::TransactionID txn = db->beginTransaction(); LMDBAL::WriteTransaction txn = db->beginTransaction();
c1->addRecord(18, 40, txn); c1->addRecord(18, 40, txn);
c1->addRecord(85, -4, txn); c1->addRecord(85, -4, txn);
c1->addRecord(-5, -3, txn); c1->addRecord(-5, -3, txn);
@ -107,7 +107,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
EXPECT_EQ(c1->count(), s1); EXPECT_EQ(c1->count(), s1);
EXPECT_EQ(c2->count(), s2); EXPECT_EQ(c2->count(), s2);
db->abortTransaction(txn); txn.abort();
EXPECT_EQ(c1->count(), s1); EXPECT_EQ(c1->count(), s1);
EXPECT_EQ(c2->count(), s2); EXPECT_EQ(c2->count(), s2);
@ -116,7 +116,7 @@ TEST_F(CacheTransactionsTest, Aborting) {
TEST_F(CacheTransactionsTest, Reading) { TEST_F(CacheTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
LMDBAL::TransactionID txn = db->beginReadOnlyTransaction(); LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
EXPECT_EQ(c1->count(txn), 3); EXPECT_EQ(c1->count(txn), 3);
EXPECT_EQ(c1->getRecord(5, txn), 13); EXPECT_EQ(c1->getRecord(5, txn), 13);
@ -128,14 +128,14 @@ TEST_F(CacheTransactionsTest, Reading) {
EXPECT_FLOAT_EQ(c2->getRecord("decallence", txn), 8532.48); EXPECT_FLOAT_EQ(c2->getRecord("decallence", txn), 8532.48);
EXPECT_FLOAT_EQ(c2->getRecord("prevent recovery", txn), -64.64); EXPECT_FLOAT_EQ(c2->getRecord("prevent recovery", txn), -64.64);
db->abortTransaction(txn); txn.terminate();
} }
TEST_F(CacheTransactionsTest, ConcurentReading) { TEST_F(CacheTransactionsTest, ConcurentReading) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType size = c1->count(); LMDBAL::SizeType size = c1->count();
LMDBAL::TransactionID txn = db->beginTransaction(); LMDBAL::WriteTransaction txn = db->beginTransaction();
EXPECT_EQ(c1->getRecord(5, txn), 13); EXPECT_EQ(c1->getRecord(5, txn), 13);
EXPECT_EQ(c1->getRecord(5), 13); EXPECT_EQ(c1->getRecord(5), 13);
@ -160,7 +160,7 @@ TEST_F(CacheTransactionsTest, ConcurentReading) {
EXPECT_EQ(c1->count(txn), 1); EXPECT_EQ(c1->count(txn), 1);
EXPECT_EQ(c1->count(), size); EXPECT_EQ(c1->count(), size);
db->commitTransaction(txn); txn.commit();
EXPECT_FALSE(c1->checkRecord(5)); EXPECT_FALSE(c1->checkRecord(5));
EXPECT_EQ(c1->count(), 1); EXPECT_EQ(c1->count(), 1);
@ -182,8 +182,9 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
int pid = fork(); int pid = fork();
if (pid == 0) { // I am the child if (pid == 0) { // I am the child
usleep(1);
std::cout << "beggining second transaction" << std::endl; std::cout << "beggining second transaction" << std::endl;
LMDBAL::TransactionID txn2 = db->beginTransaction(); //<--- this is where the execution should pause LMDBAL::WriteTransaction txn2 = db->beginTransaction(); //<--- this is where the execution should pause
//and wait for the first transaction to get finished //and wait for the first transaction to get finished
std::cout << "checking result of the first transaction value" << std::endl; std::cout << "checking result of the first transaction value" << std::endl;
EXPECT_EQ(c1->getRecord(5, txn2), 812); EXPECT_EQ(c1->getRecord(5, txn2), 812);
@ -198,13 +199,13 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
EXPECT_EQ(c1->getRecord(5), 812); EXPECT_EQ(c1->getRecord(5), 812);
std::cout << "commiting second transaction" << std::endl; std::cout << "commiting second transaction" << std::endl;
db->commitTransaction(txn2); txn2.commit();
std::cout << "quitting child thread" << std::endl; std::cout << "quitting child thread" << std::endl;
exit(testing::Test::HasFailure()); exit(testing::Test::HasFailure());
} else { // I am the parent } else { // I am the parent
std::cout << "beggining first transaction" << std::endl; std::cout << "beggining first transaction" << std::endl;
LMDBAL::TransactionID txn1 = db->beginTransaction(); LMDBAL::WriteTransaction txn1 = db->beginTransaction();
std::cout << "putting parent thread to sleep for 5 ms" << std::endl; std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
usleep(5); usleep(5);
@ -219,7 +220,7 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
EXPECT_FALSE(c1->checkRecord(5)); EXPECT_FALSE(c1->checkRecord(5));
std::cout << "commiting first transaction" << std::endl; std::cout << "commiting first transaction" << std::endl;
db->commitTransaction(txn1); txn1.commit();
std::cout << "waiting for the other thread to finish" << std::endl; std::cout << "waiting for the other thread to finish" << std::endl;
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
@ -229,3 +230,52 @@ TEST_F(CacheTransactionsTest, ConcurentModification) {
EXPECT_EQ(c1->getRecord(5), -46); 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();
}

View File

@ -4,12 +4,13 @@
#include <operators.hpp> #include <operators.hpp>
TEST(Serialization, Double) { TEST(Serialization, Double) {
double source = 5344.6542; double source1 = 5344.6542;
double source2 = 0.4329248;
LMDBAL::Serializer<double> serializer; LMDBAL::Serializer<double> serializer;
LMDBAL::Serializer<double> serializer2(source); LMDBAL::Serializer<double> serializer2(source1);
LMDBAL::Serializer<double> deserializer; LMDBAL::Serializer<double> deserializer;
serializer.setData(source); serializer.setData(source1);
MDB_val data = serializer.getData(); MDB_val data = serializer.getData();
MDB_val data2 = serializer2.getData(); MDB_val data2 = serializer2.getData();
@ -19,11 +20,16 @@ TEST(Serialization, Double) {
double destination; double destination;
serializer.deserialize(data, destination); serializer.deserialize(data, destination);
EXPECT_DOUBLE_EQ(source, destination); EXPECT_DOUBLE_EQ(source1, destination);
double dest2 = serializer.deserialize(data); double dest2 = serializer.deserialize(data);
EXPECT_DOUBLE_EQ(source, dest2); EXPECT_DOUBLE_EQ(source1, dest2);
data = serializer.setData(source2);
serializer.deserialize(data, destination);
EXPECT_DOUBLE_EQ(source2, destination);
} }
TEST(Serialization, Float) { TEST(Serialization, Float) {

View File

@ -22,28 +22,30 @@ protected:
} }
} }
int getTableCursorsSize() const {
return table->cursors.size();
}
static void TearDownTestSuite() { static void TearDownTestSuite() {
cursor.drop();
transaction.terminate();
db->close(); db->close();
db->removeDirectory(); db->removeDirectory();
delete db; delete db;
db = nullptr; db = nullptr;
cursor = nullptr;
emptyCursor = nullptr;
} }
static LMDBAL::Base* db; static LMDBAL::Base* db;
static LMDBAL::Cursor<uint64_t, std::string>* cursor; static LMDBAL::Cursor<uint64_t, std::string> cursor;
static LMDBAL::Cursor<uint64_t, std::string>* emptyCursor; static LMDBAL::Transaction transaction;
static LMDBAL::TransactionID transaction;
LMDBAL::Storage<uint64_t, std::string>* table; LMDBAL::Storage<uint64_t, std::string>* table;
LMDBAL::Storage<uint64_t, std::string>* emptyTable; LMDBAL::Storage<uint64_t, std::string>* emptyTable;
}; };
LMDBAL::Base* StorageCursorTest::db = nullptr; LMDBAL::Base* StorageCursorTest::db = nullptr;
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::cursor = nullptr; LMDBAL::Cursor<uint64_t, std::string> StorageCursorTest::cursor;
LMDBAL::Cursor<uint64_t, std::string>* StorageCursorTest::emptyCursor = nullptr; LMDBAL::Transaction StorageCursorTest::transaction = LMDBAL::Transaction();
LMDBAL::TransactionID StorageCursorTest::transaction = nullptr;
static const std::map<uint64_t, std::string> data({ static const std::map<uint64_t, std::string> data({
{245665783, "bothering nerds"}, {245665783, "bothering nerds"},
@ -64,20 +66,22 @@ TEST_F(StorageCursorTest, PopulatingTheTable) {
} }
TEST_F(StorageCursorTest, Creation) { TEST_F(StorageCursorTest, Creation) {
EXPECT_EQ(getTableCursorsSize(), 0);
cursor = table->createCursor(); cursor = table->createCursor();
emptyCursor = emptyTable->createCursor(); EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_THROW(cursor->first(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->last(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
cursor->open(); cursor.open();
} }
TEST_F(StorageCursorTest, FirstPrivate) { TEST_F(StorageCursorTest, FirstPrivate) {
std::pair<uint64_t, std::string> element = cursor->first(); EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -85,18 +89,19 @@ TEST_F(StorageCursorTest, FirstPrivate) {
} }
TEST_F(StorageCursorTest, NextPrivate) { TEST_F(StorageCursorTest, NextPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++; reference++;
for (; reference != data.end(); ++reference) { for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->next(); std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
} }
EXPECT_THROW(cursor->next(), LMDBAL::NotFound); EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin(); reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -104,7 +109,8 @@ TEST_F(StorageCursorTest, NextPrivate) {
} }
TEST_F(StorageCursorTest, LastPrivate) { TEST_F(StorageCursorTest, LastPrivate) {
std::pair<uint64_t, std::string> element = cursor->last(); 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(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -112,18 +118,19 @@ TEST_F(StorageCursorTest, LastPrivate) {
} }
TEST_F(StorageCursorTest, PrevPrivate) { TEST_F(StorageCursorTest, PrevPrivate) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++; reference++;
for (; reference != data.rend(); ++reference) { for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->prev(); std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
} }
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound); EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor->last(); std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin(); reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -131,28 +138,29 @@ TEST_F(StorageCursorTest, PrevPrivate) {
} }
TEST_F(StorageCursorTest, CurrentPrivate) { TEST_F(StorageCursorTest, CurrentPrivate) {
std::pair<uint64_t, std::string> element = cursor->first(); EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
cursor->next(); cursor.next();
cursor->prev(); cursor.prev();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
++reference; ++reference;
--reference; --reference;
@ -161,26 +169,62 @@ TEST_F(StorageCursorTest, CurrentPrivate) {
EXPECT_EQ(element.second, reference->second); 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) { TEST_F(StorageCursorTest, Destruction) {
cursor->close(); EXPECT_EQ(getTableCursorsSize(), 1);
cursor.close();
EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_THROW(cursor->first(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.first(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->last(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.last(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->next(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.next(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->prev(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.prev(), LMDBAL::CursorNotReady);
EXPECT_THROW(cursor->current(), LMDBAL::CursorNotReady); EXPECT_THROW(cursor.current(), LMDBAL::CursorNotReady);
EXPECT_THROW(emptyTable->destroyCursor(cursor), LMDBAL::Unknown); cursor = LMDBAL::Cursor<uint64_t, std::string>();
table->destroyCursor(cursor); EXPECT_EQ(getTableCursorsSize(), 0);
cursor = table->createCursor(); cursor = table->createCursor();
EXPECT_EQ(getTableCursorsSize(), 1);
} }
TEST_F(StorageCursorTest, FirstPublic) { TEST_F(StorageCursorTest, FirstPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
transaction = db->beginReadOnlyTransaction(); transaction = db->beginReadOnlyTransaction();
cursor->open(transaction); cursor.open(transaction);
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -188,18 +232,19 @@ TEST_F(StorageCursorTest, FirstPublic) {
} }
TEST_F(StorageCursorTest, NextPublic) { TEST_F(StorageCursorTest, NextPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
reference++; reference++;
for (; reference != data.end(); ++reference) { for (; reference != data.end(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->next(); std::pair<uint64_t, std::string> element = cursor.next();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
} }
EXPECT_THROW(cursor->next(), LMDBAL::NotFound); EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor->first(); std::pair<uint64_t, std::string> element = cursor.first();
reference = data.begin(); reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -207,7 +252,8 @@ TEST_F(StorageCursorTest, NextPublic) {
} }
TEST_F(StorageCursorTest, LastPublic) { TEST_F(StorageCursorTest, LastPublic) {
std::pair<uint64_t, std::string> element = cursor->last(); 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(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -215,18 +261,19 @@ TEST_F(StorageCursorTest, LastPublic) {
} }
TEST_F(StorageCursorTest, PrevPublic) { TEST_F(StorageCursorTest, PrevPublic) {
EXPECT_EQ(getTableCursorsSize(), 1);
std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin(); std::map<uint64_t, std::string>::const_reverse_iterator reference = data.rbegin();
reference++; reference++;
for (; reference != data.rend(); ++reference) { for (; reference != data.rend(); ++reference) {
std::pair<uint64_t, std::string> element = cursor->prev(); std::pair<uint64_t, std::string> element = cursor.prev();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
} }
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound); EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
std::pair<uint64_t, std::string> element = cursor->last(); std::pair<uint64_t, std::string> element = cursor.last();
reference = data.rbegin(); reference = data.rbegin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
@ -234,28 +281,29 @@ TEST_F(StorageCursorTest, PrevPublic) {
} }
TEST_F(StorageCursorTest, CurrentPublic) { TEST_F(StorageCursorTest, CurrentPublic) {
std::pair<uint64_t, std::string> element = cursor->first(); EXPECT_EQ(getTableCursorsSize(), 1);
std::pair<uint64_t, std::string> element = cursor.first();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
cursor->next(); cursor.next();
cursor->next(); cursor.next();
cursor->prev(); cursor.prev();
element = cursor->current(); element = cursor.current();
++reference; ++reference;
++reference; ++reference;
--reference; --reference;
@ -264,39 +312,106 @@ TEST_F(StorageCursorTest, CurrentPublic) {
EXPECT_EQ(element.second, reference->second); 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) { TEST_F(StorageCursorTest, CornerCases) {
db->abortTransaction(transaction); EXPECT_EQ(getTableCursorsSize(), 1);
EXPECT_THROW(cursor->current(), LMDBAL::Unknown); transaction.terminate();
cursor->close(); EXPECT_THROW(cursor.current(), LMDBAL::Unknown);
cursor.close();
emptyCursor->open(); LMDBAL::Cursor<uint64_t, std::string> emptyCursor = emptyTable->createCursor();
EXPECT_THROW(emptyCursor->first(), LMDBAL::NotFound); emptyCursor.open();
EXPECT_THROW(emptyCursor->last(), LMDBAL::NotFound); EXPECT_THROW(emptyCursor.first(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->next(), LMDBAL::NotFound); EXPECT_THROW(emptyCursor.last(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->prev(), LMDBAL::NotFound); EXPECT_THROW(emptyCursor.next(), LMDBAL::NotFound);
EXPECT_THROW(emptyCursor->current(), LMDBAL::Unknown); EXPECT_THROW(emptyCursor.prev(), LMDBAL::NotFound);
emptyCursor->close(); EXPECT_THROW(emptyCursor.current(), LMDBAL::Unknown);
emptyCursor.close();
cursor->open(); cursor.open();
EXPECT_THROW(cursor->current(), LMDBAL::Unknown); //yeah, nice thing to write in the doc 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::map<uint64_t, std::string>::const_reverse_iterator breference = data.rbegin();
std::pair<uint64_t, std::string> element(cursor->prev()); std::pair<uint64_t, std::string> element(cursor.prev());
EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again! EXPECT_EQ(element.first, breference->first); //nice thing to write in the doc, again!
EXPECT_EQ(element.second, breference->second); EXPECT_EQ(element.second, breference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(element.first, breference->first); EXPECT_EQ(element.first, breference->first);
EXPECT_EQ(element.second, breference->second); EXPECT_EQ(element.second, breference->second);
EXPECT_THROW(cursor->next(), LMDBAL::NotFound); EXPECT_THROW(cursor.next(), LMDBAL::NotFound);
cursor->close(); cursor.close();
cursor->open(); cursor.open();
element = cursor->next(); element = cursor.next();
std::map<uint64_t, std::string>::const_iterator reference = data.begin(); std::map<uint64_t, std::string>::const_iterator reference = data.begin();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
element = cursor->current(); element = cursor.current();
EXPECT_EQ(element.first, reference->first); EXPECT_EQ(element.first, reference->first);
EXPECT_EQ(element.second, reference->second); EXPECT_EQ(element.second, reference->second);
EXPECT_THROW(cursor->prev(), LMDBAL::NotFound); EXPECT_THROW(cursor.prev(), LMDBAL::NotFound);
} }

View File

@ -22,9 +22,9 @@ protected:
} }
if (WIFEXITED(status)) { if (WIFEXITED(status)) {
const int exit_status = WEXITSTATUS(status); const int exit_status = WEXITSTATUS(status);
if (exit_status != 0) { if (exit_status != 0)
std::cerr << "[----------] Non-zero exit status " << exit_status << " from test!" << std::endl; std::cerr << "[----------] Non-zero exit status " << exit_status << " from test!" << std::endl;
}
return exit_status; return exit_status;
} else { } else {
std::cerr << "[----------] Non-normal exit from child!" << std::endl; std::cerr << "[----------] Non-normal exit from child!" << std::endl;
@ -64,7 +64,7 @@ TEST_F(StorageTransactionsTest, Adding) {
EXPECT_EQ(t1->count(), 0); EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0); EXPECT_EQ(t2->count(), 0);
LMDBAL::TransactionID txn = db->beginTransaction(); LMDBAL::WriteTransaction txn = db->beginTransaction();
t1->addRecord(5, 13, txn); t1->addRecord(5, 13, txn);
t1->addRecord(-53, 782, txn); t1->addRecord(-53, 782, txn);
t1->addRecord(5892, -37829, txn); t1->addRecord(5892, -37829, txn);
@ -76,7 +76,7 @@ TEST_F(StorageTransactionsTest, Adding) {
EXPECT_EQ(t1->count(), 0); EXPECT_EQ(t1->count(), 0);
EXPECT_EQ(t2->count(), 0); EXPECT_EQ(t2->count(), 0);
db->commitTransaction(txn); txn.commit();
EXPECT_EQ(t1->count(), 3); EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t1->getRecord(5), 13); EXPECT_EQ(t1->getRecord(5), 13);
@ -95,7 +95,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
LMDBAL::SizeType s1 = t1->count(); LMDBAL::SizeType s1 = t1->count();
LMDBAL::SizeType s2 = t2->count(); LMDBAL::SizeType s2 = t2->count();
LMDBAL::TransactionID txn = db->beginTransaction(); LMDBAL::WriteTransaction txn = db->beginTransaction();
t1->addRecord(18, 40, txn); t1->addRecord(18, 40, txn);
t1->addRecord(85, -4, txn); t1->addRecord(85, -4, txn);
t1->addRecord(-5, -3, txn); t1->addRecord(-5, -3, txn);
@ -107,7 +107,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
EXPECT_EQ(t1->count(), s1); EXPECT_EQ(t1->count(), s1);
EXPECT_EQ(t2->count(), s2); EXPECT_EQ(t2->count(), s2);
db->abortTransaction(txn); txn.abort();
EXPECT_EQ(t1->count(), s1); EXPECT_EQ(t1->count(), s1);
EXPECT_EQ(t2->count(), s2); EXPECT_EQ(t2->count(), s2);
@ -116,7 +116,7 @@ TEST_F(StorageTransactionsTest, Aborting) {
TEST_F(StorageTransactionsTest, Reading) { TEST_F(StorageTransactionsTest, Reading) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
LMDBAL::TransactionID txn = db->beginReadOnlyTransaction(); LMDBAL::Transaction txn = db->beginReadOnlyTransaction();
EXPECT_EQ(t1->count(txn), 3); EXPECT_EQ(t1->count(txn), 3);
EXPECT_EQ(t1->getRecord(5, txn), 13); EXPECT_EQ(t1->getRecord(5, txn), 13);
@ -128,14 +128,14 @@ TEST_F(StorageTransactionsTest, Reading) {
EXPECT_FLOAT_EQ(t2->getRecord("decallence", txn), 8532.48); EXPECT_FLOAT_EQ(t2->getRecord("decallence", txn), 8532.48);
EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery", txn), -64.64); EXPECT_FLOAT_EQ(t2->getRecord("prevent recovery", txn), -64.64);
db->abortTransaction(txn); txn.terminate();
} }
TEST_F(StorageTransactionsTest, ConcurentReading) { TEST_F(StorageTransactionsTest, ConcurentReading) {
EXPECT_EQ(db->ready(), true); EXPECT_EQ(db->ready(), true);
LMDBAL::SizeType size = t1->count(); LMDBAL::SizeType size = t1->count();
LMDBAL::TransactionID txn = db->beginTransaction(); LMDBAL::WriteTransaction txn = db->beginTransaction();
EXPECT_EQ(t1->getRecord(5, txn), 13); EXPECT_EQ(t1->getRecord(5, txn), 13);
EXPECT_EQ(t1->getRecord(5), 13); EXPECT_EQ(t1->getRecord(5), 13);
@ -160,7 +160,7 @@ TEST_F(StorageTransactionsTest, ConcurentReading) {
EXPECT_EQ(t1->count(txn), 1); EXPECT_EQ(t1->count(txn), 1);
EXPECT_EQ(t1->count(), size); EXPECT_EQ(t1->count(), size);
db->commitTransaction(txn); txn.commit();
EXPECT_FALSE(t1->checkRecord(5)); EXPECT_FALSE(t1->checkRecord(5));
EXPECT_EQ(t1->count(), 1); EXPECT_EQ(t1->count(), 1);
@ -181,8 +181,9 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
int pid = fork(); int pid = fork();
if (pid == 0) { // I am the child if (pid == 0) { // I am the child
usleep(1);
std::cout << "beggining second transaction" << std::endl; std::cout << "beggining second transaction" << std::endl;
LMDBAL::TransactionID txn2 = db->beginTransaction(); //<--- this is where the execution should pause LMDBAL::WriteTransaction txn2 = db->beginTransaction(); //<--- this is where the execution should pause
//and wait for the first transaction to get finished //and wait for the first transaction to get finished
std::cout << "checking result of the first transaction value" << std::endl; std::cout << "checking result of the first transaction value" << std::endl;
EXPECT_EQ(t1->getRecord(5, txn2), 812); EXPECT_EQ(t1->getRecord(5, txn2), 812);
@ -197,13 +198,13 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
EXPECT_EQ(t1->getRecord(5), 812); EXPECT_EQ(t1->getRecord(5), 812);
std::cout << "commiting second transaction" << std::endl; std::cout << "commiting second transaction" << std::endl;
db->commitTransaction(txn2); txn2.commit();
std::cout << "quitting child thread" << std::endl; std::cout << "quitting child thread" << std::endl;
exit(testing::Test::HasFailure()); exit(testing::Test::HasFailure());
} else { // I am the parent } else { // I am the parent
std::cout << "beggining first transaction" << std::endl; std::cout << "beggining first transaction" << std::endl;
LMDBAL::TransactionID txn1 = db->beginTransaction(); LMDBAL::WriteTransaction txn1 = db->beginTransaction();
std::cout << "putting parent thread to sleep for 5 ms" << std::endl; std::cout << "putting parent thread to sleep for 5 ms" << std::endl;
usleep(5); usleep(5);
@ -218,12 +219,61 @@ TEST_F(StorageTransactionsTest, ConcurentModification) {
EXPECT_FALSE(t1->checkRecord(5)); EXPECT_FALSE(t1->checkRecord(5));
std::cout << "commiting first transaction" << std::endl; std::cout << "commiting first transaction" << std::endl;
db->commitTransaction(txn1); txn1.commit();
std::cout << "waiting for the other thread to finish" << std::endl; std::cout << "waiting for the other thread to finish" << std::endl;
ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems ASSERT_EQ(0, waitForChildFork(pid)); //child process should have no problems
} }
std::cout << "checking final result" << std::endl; std::cout << "checking the final result" << std::endl;
EXPECT_EQ(t1->getRecord(5), -46); 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);
}