summaryrefslogtreecommitdiff
path: root/cpp/src/IceStorm/Migrate.cpp
blob: f4f3132f6a8e914e27e8623eaa923a5c7bea6016 (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
// **********************************************************************
//
// Copyright (c) 2003-2007 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 <IceStorm/PersistentTopicMap.h>
#include <IceStorm/SubscriberMap.h>
#include <IceStorm/IceStormInternal.h>
#include <IceStorm/LLUMap.h>
#include <Freeze/Freeze.h>

using namespace std;
using namespace Ice;
using namespace IceStorm;

class Client : public Application
{
public:

    void usage();
    virtual int run(int, char*[]);
};

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);
}

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] << ": database environment names must be different" << endl;
        return EXIT_FAILURE;
    }

    Freeze::ConnectionPtr oldCon = Freeze::createConnection(communicator(), oldEnvName);
    Freeze::ConnectionPtr newCon = Freeze::createConnection(communicator(), newEnvName);

    // We should not create the old database.
    PersistentTopicMap topicMap(oldCon, "topics", false);
    // Creating the new database is fine.
    SubscriberMap subscriberMap(newCon, "subscribers");
    LLUMap lluMap(newCon, "llu");

    Freeze::TransactionHolder oldTxn(oldCon);
    Freeze::TransactionHolder newTxn(newCon);
    for(PersistentTopicMap::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));
        }
    }

    // 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));

    oldTxn.rollback();
    newTxn.commit();

    return 0;
}