summaryrefslogtreecommitdiff
path: root/db/cloner.cpp
blob: 4f3da012a304c3a56d814a49effbbc848cce3ad7 (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
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
// cloner.cpp - copy a database (export/import basically)

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"
#include "pdfile.h"
#include "dbclient.h"
#include "../util/builder.h"
#include "jsobj.h"
#include "query.h"
#include "commands.h"
#include "db.h"

extern int port;
bool userCreateNS(const char *ns, JSObj& j, string& err);

class Cloner: boost::noncopyable { 
	DBClientConnection conn;
	void copy(const char *from_ns, const char *to_ns, bool isindex = false);
public:
	Cloner() { }
	bool go(const char *masterHost, string& errmsg, const string& fromdb);
};

/* for index info object:
     { "name" : "name_1" , "ns" : "foo.index3" , "key" :  { "name" : 1.0 } }
   we need to fix up the value in the "ns" parameter.
*/
JSObj fixindex(JSObj o) { 
    JSObjBuilder b;
    JSElemIter i(o);
    while( i.more() ) { 
        Element e = i.next();
        if( e.eoo() )
            break;
        if( string("ns") == e.fieldName() ) {
            uassert("bad ns field for index during dbcopy", e.type() == String);
            const char *p = strchr(e.valuestr(), '.');
            uassert("bad ns field for index during dbcopy [2]", p);
            string newname = client->name + p;
            b.append("ns", newname);
        }
        else
            b.append(e);
    }
    JSObj res= b.doneAndDecouple();

/*    if( mod ) {
    cout << "before: " << o.toString() << endl;
    o.dump();
    cout << "after:  " << res.toString() << endl;
    res.dump();
    }*/

    return res;
}

void Cloner::copy(const char *from_collection, const char *to_collection, bool isindex) {
    auto_ptr<DBClientCursor> c;
    {
        dbtemprelease r;
        c = auto_ptr<DBClientCursor>( conn.query(from_collection, emptyObj) );
    }
	assert( c.get() );
    while( 1 ) {
        {
            dbtemprelease r;
            if( !c->more() )
                break;
        }
        JSObj tmp = c->next();

        /* assure object is valid.  note this will slow us down a good bit. 
           (a validate() instead of toString would be slightly faster)
        */
        try { 
            tmp.toString();
        }
        catch(...) { 
            cout << "skipping bad object from " << from_collection << '\n';
            continue;
        }

        JSObj js = tmp;
        if( isindex )
            js = fixindex(tmp);
		theDataFileMgr.insert(to_collection, (void*) js.objdata(), js.objsize());
	}
}

bool Cloner::go(const char *masterHost, string& errmsg, const string& fromdb) { 
    string todb = client->name;
    stringstream a,b;
    a << "localhost:" << port;
    b << "127.0.0.1:" << port;
	if( a.str() == masterHost || b.str() == masterHost ) { 
        if( fromdb == todb ) {
            // guard against an "infinite" loop
            /* if you are replicating, the local.sources config may be wrong if you get this */
            errmsg = "can't clone from self (localhost).";
            return false;
        }
	}
    /* todo: we can put thesee releases inside dbclient or a dbclient specialization.
       or just wait until we get rid of global lcok anyway. 
       */
	string ns = fromdb + ".system.namespaces";
	auto_ptr<DBClientCursor> c;
    {
        dbtemprelease r;
        if( !conn.connect(masterHost, errmsg) )
            return false;
        c = auto_ptr<DBClientCursor>( conn.query(ns.c_str(), emptyObj) );
    }
	if( c.get() == 0 ) {
		errmsg = "query failed " + ns;
		return false;
	}

    while( 1 ) {
        {
            dbtemprelease r;
            if( !c->more() )
                break;
        }
		JSObj collection = c->next();
		Element e = collection.findElement("name");
		assert( !e.eoo() );
		assert( e.type() == String );
		const char *from_name = e.valuestr();
        if( strstr(from_name, ".system.") || strchr(from_name, '$') ) {
			continue;
        }
		JSObj options = collection.getObjectField("options");

        /* change name "<fromdb>.collection" -> <todb>.collection */
        const char *p = strchr(from_name, '.');
        assert(p);
        string to_name = todb + p;

		//if( !options.isEmpty() )
        {
			string err;
			userCreateNS(to_name.c_str(), options, err);
		}
		copy(from_name, to_name.c_str());
	}

	// now build the indexes
	string system_indexes_from = fromdb + ".system.indexes";
	string system_indexes_to = todb + ".system.indexes";
	copy(system_indexes_from.c_str(), system_indexes_to.c_str(), true);

	return true;
}

bool cloneFrom(const char *masterHost, string& errmsg, const string& fromdb)
{
	Cloner c;
	return c.go(masterHost, errmsg, fromdb);
}

/* Usage:
   mydb.$cmd.findOne( { clone: "fromhost" } ); 
*/
class CmdClone : public Command { 
public:
    CmdClone() : Command("clone") { }

    virtual bool run(const char *ns, JSObj& cmdObj, string& errmsg, JSObjBuilder& result) {
        string from = cmdObj.getStringField("clone");
        if( from.empty() ) 
            return false;
        return cloneFrom(from.c_str(), errmsg, client->name);
    }
} cmdclone;

/* Usage:
   admindb.$cmd.findOne( { copydb: 1, fromhost: <hostname>, fromdb: <db>, todb: <db> } );
*/
class CmdCopyDb : public Command { 
public:
    CmdCopyDb() : Command("copydb") { }
    virtual bool adminOnly() { return true; }

    virtual bool run(const char *ns, JSObj& cmdObj, string& errmsg, JSObjBuilder& result) {
        string fromhost = cmdObj.getStringField("fromhost");
        if( fromhost.empty() ) { 
            /* copy from self */
            stringstream ss;
            ss << "localhost:" << port;
            fromhost = ss.str();
        }
        string fromdb = cmdObj.getStringField("fromdb");
        string todb = cmdObj.getStringField("todb");
        if( fromhost.empty() || todb.empty() || fromdb.empty() ) {
            errmsg = "parms missing - {copydb: 1, fromhost: <hostname>, fromdb: <db>, todb: <db>}";
            return false;
        }
        setClient(todb.c_str());
        bool res = cloneFrom(fromhost.c_str(), errmsg, fromdb);
        client = 0;
        return res;
    }
} cmdcopydb;