/
KeyValuePairs.h
140 lines (113 loc) · 4.54 KB
/
KeyValuePairs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/************************************************************************
* Copyright(c) 2010, One Unified. All rights reserved. *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
#pragma once
// 2010/09/12
// eventually, this will supercede what is in KeyValuePair.h.
// for the time being, both are used
// database for incrementing keys
// databases for various record types
// may need to worry about multi-threading at some point in time
#include <string>
#include "EnvManager.h"
//
// CKeyValuePairsBase
//
class CKeyValuePairsBase {
public:
CKeyValuePairsBase( const std::string& sDbFileName, const std::string& sDbName );
virtual ~CKeyValuePairsBase( void );
void Set( Dbt *pKey, Dbt *pValue, DbTxn* pTxn = NULL );
void Get( Dbt *pKey, Dbt *pValue, DbTxn* pTxn = NULL );
void Truncate( DbTxn* pTxn = NULL );
// void OpenCursor( void );
// void CloseCursor( void );
// void ResetCursor( void );
protected:
Db *m_pdb; // database
//Dbc *mpdbc; // cursor
private:
};
//
// CKeyValuePairs
//
// K=type of key, V=type of value; used for size calcuations, and for type checking inbound/outbound
class CKeyValuePairs: public CKeyValuePairsBase
{
public:
CKeyValuePairs( const std::string& sDbFileName, const std::string& sDbName )
: CKeyValuePairsBase( sDbFileName, sDbName ) {};
~CKeyValuePairs(void) {};
// be very wary of these. You don't want to pass in Dbt for key and value. Need a compile time type checker here.
// for Dbts, use CKeyValuePairsBase directly
// Sets
template<typename K, typename V>
void Set( const K& key, const V& value ) {
Dbt k( static_cast<void*>( const_cast<K*>( &key ) ), sizeof( K ) );
Dbt v( &value, sizeof( V ) );
CKeyValuePairsBase::Set( &k, &v );
}
template<typename V>
void Set( const std::string& key, V& value ) {
Dbt k( static_cast<void*>( const_cast<char*>( key.c_str() ) ), key.length() );
Dbt v( static_cast<void*>( &value ), sizeof( V ) );
CKeyValuePairsBase::Set( &k, &v );
}
template<typename K>
void Set( const K& key, const std::string& value ) {
Dbt k( static_cast<void*>( const_cast<K*>( &key ) ), sizeof( K ) );
Dbt v( static_cast<void*>( const_cast<char*>( value.c_str() ) ), value.length() );
CKeyValuePairsBase::Set( &k, &v );
}
void Set( const std::string& key, const std::string& value ) {
Dbt k( static_cast<void*>( const_cast<char*>( key.c_str() ) ), key.length() );
Dbt v( static_cast<void*>( const_cast<char*>( value.c_str() ) ), value.length() );
CKeyValuePairsBase::Set( &k, &v );
}
// gets
template<typename K, typename V>
void Get( const K& key, V* value ) {
Dbt k( &key, sizeof( K ) );
Dbt v;
v.set_flags( DB_DBT_USERMEM );
v.set_ulen( sizeof( V ) );
v.set_size( sizeof( V ) );
v.set_data( value );
CKeyValuePairsBase::Get( &k, &v );
}
template<typename V>
void Get( const std::string& key, V* value ) {
Dbt k( static_cast<void*>( const_cast<char*>( key.c_str() ) ), key.length() );
Dbt v;
v.set_flags( DB_DBT_USERMEM );
v.set_ulen( sizeof( V ) );
v.set_size( sizeof( V ) );
v.set_data( value );
CKeyValuePairsBase::Get( &k, &v );
}
template<typename K>
void Get( const K& key, std::string* value ) {
Dbt k( &key, sizeof( K ) );
Dbt v; // db engine allocates the memory
CKeyValuePairsBase::Get( &k, &v );
value->assign( v.get_data(), v.get_size() );
}
void Get( const std::string& key, std::string* value ) {
Dbt k( static_cast<void*>( const_cast<char*>( key.c_str() ) ), key.length() );
Dbt v; // db engine allocates the memory
CKeyValuePairsBase::Get( &k, &v );
value->assign( static_cast<const char*>( v.get_data() ), v.get_size() );
}
protected:
private:
CKeyValuePairs( void );
};