summaryrefslogtreecommitdiff
path: root/cpp/src/Freeze/SharedDb.h
blob: c51dcb45983bccc95ee4fdfb86f1e3da71130bed (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef FREEZE_SHARED_DB_H
#define FREEZE_SHARED_DB_H

#include <IceUtil/Config.h>
#include <db_cxx.h>
#include <Freeze/ConnectionI.h>
#include <Freeze/Map.h>
#include <IceUtil/Handle.h>
#include <map>

namespace Freeze
{

class SharedDbEnv;

class SharedDb;
typedef IceUtil::Handle<SharedDb> SharedDbPtr;

class MapIndexI
{
public:
    
    MapIndexI(const ConnectionIPtr&, SharedDb&,
	      DbTxn*, bool, const MapIndexBasePtr&);

    ~MapIndexI();
   
    IteratorHelper* untypedFind(const Key&, bool, const MapHelperI&) const;
    int untypedCount(const Key&, const ConnectionIPtr&) const;
    
    int
    secondaryKeyCreate(Db*, const Dbt*, const Dbt*, Dbt*);

    const std::string name() const
    {
	return _index->name();
    }
    
    Db* db() const
    {
	return _db.get();
    }

private:

    const MapIndexBasePtr _index;
    std::auto_ptr<Db> _db;
    std::string _dbName;
};


class SharedDb : public ::Db
{
public:
    
    using Db::get;

    static SharedDbPtr get(const ConnectionIPtr&, const std::string&,
			   const std::string&, const std::string&,
			   const std::vector<MapIndexBasePtr>&, bool);

    static SharedDbPtr openCatalog(SharedDbEnv&);

    ~SharedDb();

    void __incRef();
    void __decRef();

    const std::string& 
    dbName() const;

    const std::string&
    key() const;

    const std::string&
    value() const;

#ifdef __HP_aCC
    
    virtual int 
    get(DbTxn *txnid, Dbt *key, Dbt *data, u_int32_t flags)
    {
	::Db::get(txnid, key, data, flags);
    }
#endif

    typedef std::map<std::string, MapIndexI*> IndexMap;

private:

    struct MapKey
    {
	std::string envName;
	Ice::CommunicatorPtr communicator;
	std::string dbName;

	inline bool
	operator<(const MapKey& rhs) const;
    };

    typedef std::map<MapKey, Freeze::SharedDb*> SharedDbMap;
   
    SharedDb(const MapKey&, const std::string&, const std::string&,
	     const ConnectionIPtr&, const std::vector<MapIndexBasePtr>&, bool);
    
    SharedDb(const MapKey&, DbEnv*);
    
    void connectIndices(const std::vector<MapIndexBasePtr>&) const;
    void cleanup(bool);

    MapKey _mapKey;
    std::string _key;
    std::string _value;
    int _refCount;
    Ice::Int _trace;

    IndexMap _indices;

    static SharedDbMap* sharedDbMap;
};

inline const std::string& 
SharedDb::dbName() const
{
    return _mapKey.dbName;
}

inline const std::string& 
SharedDb::key() const
{
    return _key;
}

inline const std::string& 
SharedDb::value() const
{
    return _value;
}


inline bool 
SharedDb::MapKey::operator<(const MapKey& rhs) const
{
    return (communicator < rhs.communicator) ||
	((communicator == rhs.communicator) && (dbName < rhs.dbName)) ||
	((communicator == rhs.communicator) && (dbName == rhs.dbName) && (envName < rhs.envName));
}
    
}
#endif