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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services