代码之家  ›  专栏  ›  技术社区  ›  JasonSmith

为本地开发提供一个大型CouchDB数据库示例,避免长视图构建

  •  2
  • JasonSmith  · 技术社区  · 14 年前

    CouchDB便于在本地开发(CouchApps),然后推进远程生产。不幸的是,对于生产规模的数据集,处理视图可能很麻烦。

    有什么好的方法可以获取CouchDB数据库的样本以用于本地开发?

    1 回复  |  直到 14 年前
        1
  •  2
  •   JasonSmith    14 年前

    答案是过滤复制。我喜欢分两部分来做:

    1. 复制生产数据库, example_db example_db_full
    2. 从执行筛选的复制 示例\u db \u full 示例\u db

    要选择的文档可以是特定于应用程序的。在这个时候,我对一个简单的随机通过/失败的百分比感到满意。随机性是一致的(即,相同的文档总是通过或失败)

    我的技术是规范文档中的内容校验和 _rev 范围为[0.0,1.0]的字段。然后我简单地指定一些分数(例如。 0.01 ),如果规范化的校验和值为<=my fraction,则文档通过。

    function(doc, req) {
      if(/^_design\//.test(doc._id))
        return true;
    
      if(!req.query.p)
        throw {error: "Must supply a 'p' parameter with the fraction"
                      + " of documents to pass [0.0-1.0]"};
    
      var p = parseFloat(req.query.p);
      if(!(p >= 0.0 && p <= 1.0)) // Also catches NaN
        throw {error: "Must supply a 'p' parameter with the fraction of documents"
                      + " to pass [0.0-1.0]"};
    
      // Consider the first 8 characters of the doc checksum (for now, taken
      // from _rev) as a real number on the range [0.0, 1.0), i.e.
      // ["00000000", "ffffffff").
      var ONE = 4294967295; // parseInt("ffffffff", 16);
      var doc_val = parseInt(doc._rev.match(/^\d+-([0-9a-f]{8})/)[1], 16);
    
      return doc_val <= (ONE * p);
    }