duanke3985 2018-01-23 15:44
浏览 104
已采纳

从php中的couchdb清除已删除的文件

I am trying to write an automated script to run through a couchdb, find the deleted documents (About 100,000 doc deletetions a month) and purge them and their revisions.

I haven't found documentation explaining how to get all deleted documents, all their revisions, and construct them into the POST request shown here.

http://docs.couchdb.org/en/stable/api/database/misc.html

How do I construct the views, get the data, and create the POST to do this?

Thanks

  • 写回答

1条回答 默认 最新

  • donglin317704291 2018-01-26 19:30
    关注

    So I ended up making a nodejs script for it that runs periodically. Some things are hard coded since this was for testing, and not production (yet). Anyone referencing this should watch for hard coded references. I used pouchdb to handle the replication, as it was able to run all 400,000 tombstones in a few minutes, whereas I never actually got couchdb to finish replicating after a full day of running. This is my first node script ever, so I apologize if it's awful.

    var PouchDB = require('pouchdb');
    var fs = require('fs');
    var child = require('child_process');
    
    var originalDB = "dbName";
    var tempDB = "dbName_clean";
    var serviceName = "Apache CouchDB";
    var couchDBDataPath = "C:\\bin\\couchdb\\couchdb.2.1.1\\data\\";
    
    var db = new PouchDB('http://URL/' + originalDB);
    var db2 = new PouchDB('http://URL/' + tempDB);
    
    console.log("Compacting");
    return db.compact().then(function (result) {
        console.log("Compacting done, replicating");
        var batch_size = 100; 
    
        return db2.destroy().then(function () {
            db2 = new PouchDB('http://URL/' + tempDB);
            return db.replicate.to(db2, {
                batch_size: batch_size,
                filter: function (doc, req) {
                    return !doc._deleted;
                }
            }).on('change', function (info) {
                console.log("batch done");
            }).on('complete', function () {
                console.log('complete');
            }).on('paused', function (err) {
                // replication paused (e.g. replication up to date, user went offline)
                console.log("paused", err);
            }).on('active', function () {
                // replicate resumed (e.g. new changes replicating, user went back online)
                console.log("Active");
            }).on('denied', function (err) {
                // a document failed to replicate (e.g. due to permissions)
                console.log("Denied", err);
            }).on('error', function (err) {
                // handle error
                console.log("error", err);
                // reject(err);
            }).then(function () {
                //replicate done, rename everything
                var date = new Date();
                console.log("Stopping service");
                child.execSync('net stop "' + serviceName + '"');
                console.log("Service Stopped");
                var newName;
                var counter = 0;
                do {
                    newName = "_" + date.getFullYear() + date.getMonth() + date.getDay() + "_" + counter;
                    counter++;
                } while (fs.existsSync(couchDBDataPath + originalDB + newName + ".couch") || fs.existsSync(couchDBDataPath + "." + originalDB + "_design" + newName));
    
    
                console.log("Renaming original couch to backup labeled", originalDB + newName);
                fs.renameSync(couchDBDataPath + originalDB + ".couch", couchDBDataPath + originalDB + newName + ".couch");
                fs.renameSync(couchDBDataPath + "." + originalDB + "_design", couchDBDataPath + "." + originalDB + newName + "_design");
    
                console.log("Renaming clean couch back to original", tempDB);
                fs.renameSync(couchDBDataPath + tempDB + ".couch", couchDBDataPath + originalDB + ".couch");
                fs.mkdirSync(couchDBDataPath + "." + originalDB + "_design"); 
    
                console.log("Starting service");
                child.execSync('net start "' + serviceName + '"');
                console.log("Service started");
            });
        });
    }).catch(function (err) {
        console.log(err);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?