Apparently Python 2.7 doesn’t come with setuptools installed, and thus, easy_install doesn’t work. I really miss my OSX, but this new job requires Windows because we have SDKs that we work with that are Windows only. So be sure to download and run ez_setup.py after you install Python 2.7, and then be sure to add C:\Python27\Scripts to your users path so that you can just type easy_install
like you all ways could on OSX or Linux.
Looks like Maven 2 is pretty much inline with the generic Ant based build system I have been using for the past 8 years. So I am going to take the plunge and start using Maven 2 so I don’t have to keep maintaining my system. Less time maintaining a build system means more time writing code that actually does something unique.
Here is a little code snippet on how to get a list of all the Views from all the Design Documents from all the Databases in a CouchDB instance with couchdbkit.
from couchdbkit import *
server = Server()
dbs = server.all_dbs()
for dbname in dbs:
db = server.get_or_create_db(dbname)
result = db.all_docs(startkey=‘_design’, endkey=‘_design0′)
for doc in result.all():
designdoc = db.get(doc['id'])
if ‘views’ in designdoc:
for view in designdoc['views']:
print ‘%s/%s/_view/%s’ % (dbname, designdoc['_id'], view)
Update: this has been applied in CouchDB as of applied in r939443.
Here is the bit you add to jquery.couch.js in the couchdb/www/script directory. I added it right between the query function and the view function. Here is the entire modified jquery.couch.js if you just want to download it and use it without having to edit it yourself.
{
var list = list.split(‘/’);
var options = options || {};
var type = ‘GET’;
var data = null;
if (options['keys'])
{
type = ‘POST’;
var keys = options['keys'];
delete options['keys'];
data = toJSON({‘keys’: keys });
}
ajax({
type: type,
data: data,
url: this.uri + ‘_design/’ + list[0] + ‘/_list/’ + list[1] + ‘/’ + view + encodeOptions(options)
}, options, ‘An error occured accessing the list’);
},
UPDATE: this has also been added in couchapp/couchapp on GitHub.
then inside vendor/couchapp/_attachments/jquery.couch.app.js make the Design object look like this
{
this.doc_id = "_design/" + name;
this.view = function(view, opts)
{
db.view(name + ‘/’ + view, opts);
};
this.list = function(list, view, opts)
{
db.list(name + ‘/’ + list, view, opts);
}
}
and then make the appExports entry in jquery.couch.app.js look like this
db : db,
design : design,
view : design.view,
list : design.list,
docForm : docForm,
req : mockReq
}, $.couch.app.app);
In a previous post I documented my first pass at implementing ad-hoc queries in CouchDB. I have now abandoned the Reduce step of that process, it doesn’t reduce fast enough in some cases of extremely large data. And I moved all the merging of duplicate document ids to a List function, and I made it much more generic. The List function should work with any Map function that returns keys in the format ["field","value"].
Here is what an Ad-Hoc Map function looks like.
{
emit(['guid', doc.guid], null);
emit(['src', doc.sourceServer], null);
emit(['dest', doc.destServer], null);
}
Here is the generic List function that dedupes the lists based on field selection and returns back JSON compatible with what a normal View function returns.
{
var maxResults = parseInt(req.query.maxResults);
function merge(a, b)
{
function sortById(a, b)
{
if (a.id > b.id) { return 1; }
if (b.id > a.id) { return -1; }
return 0;
}
if (a.length > 0 && b.length == 0) { return a; }
if (b.length > 0 && a.length == 0) { return b; }
a.sort(sortById);
b.sort(sortById);
var result = [];
while( a.length > 0 && b.length > 0 )
{
if (a[0].id < b[0].id) { a.shift(); }
else if (a[0].id > b[0].id) { b.shift(); }
else { result.push(a.shift()); b.shift(); }
}
return result;
}
var totalRows = 0;
var fields = {};
var field;
var row;
while (row = getRow())
{
totalRows += 1;
field = row.key[0];
if (!fields[field])
{
fields[field] = [];
}
fields[field].push({ id:row.id, doc:row.doc});
}
var r = [];
for (var i in fields)
{
r = merge(r,fields[i]);
}
if (maxResults > 0) { r = r.slice(0,maxResults); }
result = {total_rows:totalRows, offset:0, rows:r};
send(toJSON(result));
}
A query using curl might look something like this
of course the “keys” could include multiple “src” and or “guid” and or “dest” fields in this example as well.