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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
// **********************************************************************
//
// Copyright (c) 2003-2008 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.
//
// **********************************************************************
#include <IceUtil/DisableWarnings.h>
#include <Freeze/Freeze.h>
#include <IceStorm/SubscriberMap.h>
#include <IceStorm/IceStormInternal.h>
#include <IceStorm/LLUMap.h>
#include <IceStorm/V32FormatDB.h>
#include <IceStorm/V31FormatDB.h>
using namespace std;
using namespace Ice;
using namespace IceStorm;
class Client : public Application
{
public:
void usage();
virtual int run(int, char*[]);
private:
void v32migrate(const Freeze::ConnectionPtr&, SubscriberMap&);
void v31migrate(const Freeze::ConnectionPtr&, SubscriberMap&);
};
int
main(int argc, char* argv[])
{
Client app;
int rc = app.main(argc, argv);
return rc;
}
void
Client::usage()
{
cerr << "Usage: " << appName() << " old-env new-env\n";
}
string
identityToTopicName(const Ice::Identity& id)
{
//
// Work out the topic name. If the category is empty then we're in
// backwards compatibility mode and the name is just
// identity.name. Otherwise identity.name is topic.<topicname>.
//
if(id.category.empty())
{
return id.name;
}
assert(id.name.length() > 6 && id.name.compare(0, 6, "topic.") == 0);
return id.name.substr(6);
}
void
Client::v32migrate(const Freeze::ConnectionPtr& oldCon, SubscriberMap& subscriberMap)
{
// We should not create the old database.
V32Format topicMap(oldCon, "topics", false);
Freeze::TransactionHolder oldTxn(oldCon);
for(V32Format::const_iterator p = topicMap.begin(); p != topicMap.end(); ++p)
{
// First the placeholder record for the topic.
SubscriberRecordKey key;
key.topic = p->first;
SubscriberRecord rec;
rec.link = false;
rec.cost = 0;
subscriberMap.put(SubscriberMap::value_type(key, rec));
string topicName = identityToTopicName(key.topic);
// Next each link.
for(LinkRecordSeq::const_iterator q = p->second.begin(); q != p->second.end(); ++q)
{
Ice::Identity id = q->theTopic->ice_getIdentity();
key.id = id;
rec.id = id;
rec.obj = q->obj;
rec.theTopic = q->theTopic;
rec.topicName = topicName;
rec.link = true;
rec.cost = q->cost;
subscriberMap.put(SubscriberMap::value_type(key, rec));
}
}
oldTxn.rollback();
}
void
Client::v31migrate(const Freeze::ConnectionPtr& oldCon, SubscriberMap& subscriberMap)
{
// We should not create the old database.
V31Format topicMap(oldCon, "topics", false);
Freeze::TransactionHolder oldTxn(oldCon);
for(V31Format::const_iterator p = topicMap.begin(); p != topicMap.end(); ++p)
{
// First the placeholder record for the topic.
SubscriberRecordKey key;
key.topic.name = p->first;
SubscriberRecord rec;
rec.link = false;
rec.cost = 0;
subscriberMap.put(SubscriberMap::value_type(key, rec));
string topicName = identityToTopicName(key.topic);
// Next each link.
for(LinkRecordDict::const_iterator q = p->second.begin(); q != p->second.end(); ++q)
{
Ice::Identity id = q->second.theTopic->ice_getIdentity();
key.id = id;
rec.id = id;
rec.obj = q->second.obj;
rec.theTopic = q->second.theTopic;
rec.topicName = topicName;
rec.link = true;
rec.cost = q->second.cost;
subscriberMap.put(SubscriberMap::value_type(key, rec));
}
}
oldTxn.rollback();
}
int
Client::run(int argc, char* argv[])
{
if(argc != 3)
{
usage();
return EXIT_FAILURE;
}
string oldEnvName = argv[1];
string newEnvName = argv[2];
if(oldEnvName == newEnvName)
{
cerr << argv[0] << ": The database environment names must be different" << endl;
return EXIT_FAILURE;
}
bool migrate31 = false;
Freeze::ConnectionPtr oldCon = Freeze::createConnection(communicator(), oldEnvName);
Freeze::Catalog catalog(oldCon, Freeze::catalogName());
if(catalog.size() != 1 || catalog.begin()->first != "topics")
{
cerr << argv[0] << ": The old database environment does not contain an IceStorm database." << endl;
return EXIT_FAILURE;
}
Freeze::CatalogData data = catalog.begin()->second;
if(!data.evictor && data.key == "string" && data.value == "::IceStorm::LinkRecordDict")
{
migrate31 = true;
}
else if(!data.evictor && data.key == "::Ice::Identity" && data.value == "::IceStorm::LinkRecordSeq")
{
migrate31 = false;
}
else
{
cerr << argv[0] << ": The old environment contains an unrecognized IceStorm database version." << endl;
return EXIT_FAILURE;
}
// Creating the new database is fine.
Freeze::ConnectionPtr newCon = Freeze::createConnection(communicator(), newEnvName);
SubscriberMap subscriberMap(newCon, "subscribers");
LLUMap lluMap(newCon, "llu");
Freeze::TransactionHolder newTxn(newCon);
if(migrate31)
{
v31migrate(oldCon, subscriberMap);
}
else
{
v32migrate(oldCon, subscriberMap);
}
// We need to write a record in the LLU map so that if this
// database is used for a migration this database will be picked
// as the latest. We use generation 1 since the default is 0.
IceStormElection::LogUpdate llu;
llu.generation = 1;
llu.iteration = 0;
lluMap.put(LLUMap::value_type("_manager", llu));
newTxn.commit();
return 0;
}
|