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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
// **********************************************************************
//
// Copyright (c) 2004
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
#include <FreezeScript/Transformer.h>
#include <FreezeScript/TransformDescriptors.h>
#include <FreezeScript/Exception.h>
#include <FreezeScript/TransformAnalyzer.h>
#include <FreezeScript/Util.h>
#include <db_cxx.h>
using namespace std;
namespace FreezeScript
{
class DescriptorHandler : public IceXML::Handler
{
public:
DescriptorHandler(const DataFactoryPtr&, const Slice::UnitPtr&, const Slice::UnitPtr&, const ErrorReporterPtr&);
virtual void startElement(const std::string&, const IceXML::Attributes&, int, int);
virtual void endElement(const std::string&, int, int);
virtual void characters(const std::string&, int, int);
virtual void error(const std::string&, int, int);
TransformDBDescriptorPtr descriptor() const;
private:
DataFactoryPtr _factory;
Slice::UnitPtr _old;
Slice::UnitPtr _new;
ErrorReporterPtr _errorReporter;
DescriptorPtr _current;
TransformDBDescriptorPtr _descriptor;
};
} // End of namespace FreezeScript
//
// DescriptorHandler
//
FreezeScript::DescriptorHandler::DescriptorHandler(const DataFactoryPtr& factory, const Slice::UnitPtr& oldUnit,
const Slice::UnitPtr& newUnit,
const ErrorReporterPtr& errorReporter) :
_factory(factory), _old(oldUnit), _new(newUnit), _errorReporter(errorReporter)
{
}
void
FreezeScript::DescriptorHandler::startElement(const string& name, const IceXML::Attributes& attributes, int line,
int column)
{
DescriptorPtr d;
if(name == "transformdb")
{
if(_current)
{
_errorReporter->descriptorError("<transformdb> must be the top-level element", line);
}
_descriptor = new TransformDBDescriptor(line, _factory, _errorReporter, attributes, _old, _new);
d = _descriptor;
}
else if(name == "database")
{
if(!_current)
{
_errorReporter->descriptorError("<database> must be a child of <transformdb>", line);
}
d = new DatabaseDescriptor(_current, line, _factory, _errorReporter, attributes, _old, _new);
}
else if(name == "record")
{
if(!_current)
{
_errorReporter->descriptorError("<record> must be a child of <database>", line);
}
d = new RecordDescriptor(_current, line, _factory, _errorReporter, attributes, _old, _new);
}
else if(name == "transform")
{
if(!_current)
{
_errorReporter->descriptorError("<transform> must be a child of <transformdb>", line);
}
d = new TransformDescriptor(_current, line, _factory, _errorReporter, attributes, _old, _new);
}
else if(name == "init")
{
if(!_current)
{
_errorReporter->descriptorError("<init> must be a child of <transformdb>", line);
}
d = new InitDescriptor(_current, line, _factory, _errorReporter, attributes, _old, _new);
}
else if(name == "set")
{
if(!_current)
{
_errorReporter->descriptorError("<set> cannot be a top-level element", line);
}
d = new SetDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "define")
{
if(!_current)
{
_errorReporter->descriptorError("<define> cannot be a top-level element", line);
}
d = new DefineDescriptor(_current, line, _factory, _errorReporter, attributes, _old, _new);
}
else if(name == "add")
{
if(!_current)
{
_errorReporter->descriptorError("<add> cannot be a top-level element", line);
}
d = new AddDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "remove")
{
if(!_current)
{
_errorReporter->descriptorError("<remove> cannot be a top-level element", line);
}
d = new RemoveDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "delete")
{
if(!_current)
{
_errorReporter->descriptorError("<delete> cannot be a top-level element", line);
}
d = new DeleteDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "fail")
{
if(!_current)
{
_errorReporter->descriptorError("<fail> cannot be a top-level element", line);
}
d = new FailDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "echo")
{
if(!_current)
{
_errorReporter->descriptorError("<echo> cannot be a top-level element", line);
}
d = new EchoDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "if")
{
if(!_current)
{
_errorReporter->descriptorError("<if> cannot be a top-level element", line);
}
d = new IfDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else if(name == "iterate")
{
if(!_current)
{
_errorReporter->descriptorError("<iterate> cannot be a top-level element", line);
}
d = new IterateDescriptor(_current, line, _factory, _errorReporter, attributes);
}
else
{
_errorReporter->descriptorError("unknown descriptor `" + name + "'", line);
}
if(_current)
{
_current->addChild(d);
}
_current = d;
}
void
FreezeScript::DescriptorHandler::endElement(const std::string& name, int, int)
{
assert(_current);
_current = _current->parent();
}
void
FreezeScript::DescriptorHandler::characters(const std::string&, int, int)
{
}
void
FreezeScript::DescriptorHandler::error(const std::string& msg, int line, int col)
{
_errorReporter->descriptorError(msg, line);
}
FreezeScript::TransformDBDescriptorPtr
FreezeScript::DescriptorHandler::descriptor() const
{
return _descriptor;
}
//
// Transformer
//
FreezeScript::Transformer::Transformer(const Ice::CommunicatorPtr& communicator, const Slice::UnitPtr& oldUnit,
const Slice::UnitPtr& newUnit, bool ignoreTypeChanges, bool purgeObjects) :
_communicator(communicator), _old(oldUnit), _new(newUnit), _ignoreTypeChanges(ignoreTypeChanges),
_purgeObjects(purgeObjects)
{
createCoreSliceTypes(_old);
createCoreSliceTypes(_new);
createEvictorSliceTypes(_old);
createEvictorSliceTypes(_new);
}
void
FreezeScript::Transformer::analyze(const string& oldKey, const string& newKey, const string& oldValue,
const string& newValue, ostream& descriptors, Ice::StringSeq& missingTypes,
Ice::StringSeq& errors)
{
//
// Look up the Slice definitions for the key and value types.
//
Slice::TypePtr oldKeyType = findType(_old, oldKey, errors);
Slice::TypePtr newKeyType = findType(_new, newKey, errors);
Slice::TypePtr oldValueType = findType(_old, oldValue, errors);
Slice::TypePtr newValueType = findType(_new, newValue, errors);
if(!oldKeyType || !newKeyType || !oldValueType || !newValueType)
{
return;
}
TransformAnalyzer analyzer(_old, _new, _ignoreTypeChanges);
analyzer.analyze(oldKeyType, newKeyType, oldValueType, newValueType, descriptors, missingTypes, errors);
}
void
FreezeScript::Transformer::analyze(ostream& descriptors, Ice::StringSeq& missingTypes, Ice::StringSeq& errors)
{
const string keyType = "::Freeze::EvictorStorageKey";
const string valueType = "::Freeze::ObjectRecord";
analyze(keyType, keyType, valueType, valueType, descriptors, missingTypes, errors);
}
void
FreezeScript::Transformer::transform(istream& is, Db* db, DbTxn* txn, Db* dbNew, DbTxn* txnNew, ostream& errors,
bool suppress)
{
ErrorReporterPtr errorReporter = new ErrorReporter(errors, suppress);
try
{
DataFactoryPtr factory = new DataFactory(_communicator, _new, errorReporter);
DescriptorHandler dh(factory, _old, _new, errorReporter);
IceXML::Parser::parse(is, dh);
TransformDBDescriptorPtr descriptor = dh.descriptor();
descriptor->validate();
descriptor->transform(_communicator, db, txn, dbNew, txnNew, _purgeObjects);
}
catch(const IceXML::ParserException& ex)
{
errorReporter->error(ex.reason());
}
}
Slice::TypePtr
FreezeScript::Transformer::findType(const Slice::UnitPtr& u, const string& type, Ice::StringSeq& errors)
{
Slice::TypeList l;
l = u->lookupType(type, false);
if(l.empty())
{
errors.push_back("error: unknown type `" + type + "'");
return 0;
}
return l.front();
}
|