CouchDB如何计算序列ID(seq)

时间:2018-12-28 18:40:56

标签: couchdb

我正在寻找伪代码或源存储库(https://github.com/apache/couchdb)中的实际代码,这些代码定义CouchDB如何计算Y的值。

查看文档中的技术概述:http://docs.couchdb.org/en/2.2.0/intro/overview.html它说:

  

对数据库实例的每次更新都会生成一个新的序号。

但是它没有涉及如何。我认为它可能取决于实现,请查看它指出的复制协议文档:

  

序列ID:   变更Feed提供的ID。它必须是增量的,但不能总是整数。

// get prime factors of a using pre-generated sieve
std::vector<int> getPrimeFactors(int a, const std::vector<int> & primes) {
    std::vector<int> f;
    for (auto p : primes) {
        if (p > a) break;
        if (a % p == 0) {
            f.push_back(p);
            do {
                a /= p;
            } while (a % p == 0);
        }
    }
    if (a > 1) f.push_back(a);

    return f;
}

// find coprime pairs A_i and B_j
// A_i and B_i <= 1e9
void solution(const std::vector<int> & A, const std::vector<int> & B) {
    // generate prime sieve
    std::vector<int> primes;
    primes.push_back(2);

    for (int i = 3; i*i <= 1e9; ++i) {
        bool isPrime = true;
        for (auto p : primes) {
            if (i % p == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            primes.push_back(i);
        }
    }

    int N = A.size();

    struct Entry {
        int n = 0;
        int64_t p = 0;
    };

    // cntp[X] - number of times the product X can be expressed
    // with prime factors of A_i
    std::map<int64_t, int64_t> cntp;

    for (int i = 0; i < N; i++) {
        auto f = getPrimeFactors(A[i], primes);

        // count possible products using non-repeating prime factors of A_i
        std::vector<Entry> x;
        x.push_back({ 0, 1 });

        for (auto p : f) {
            int k = x.size();
            for (int i = 0; i < k; ++i) {
                int nn = x[i].n + 1;
                int64_t pp = x[i].p*p;

                ++cntp[pp];
                x.push_back({ nn, pp });
            }
        }
    }

    // use Inclusion–exclusion principle to count non-coprime pairs
    // and subtract them from the total number of prairs N*N

    int64_t cnt = N; cnt *= N;

    for (int i = 0; i < N; i++) {
        auto f = getPrimeFactors(B[i], primes);

        std::vector<Entry> x;
        x.push_back({ 0, 1 });

        for (auto p : f) {
            int k = x.size();
            for (int i = 0; i < k; ++i) {
                int nn = x[i].n + 1;
                int64_t pp = x[i].p*p;

                x.push_back({ nn, pp });

                if (nn % 2 == 1) {
                    cnt -= cntp[pp];
                } else {
                    cnt += cntp[pp];
                }
            }
        }
    }

    printf("cnt = %d\n", (int) cnt);
}

这个问题有些相关,因为它询问如何计算文档的另一个部分,即rev:

How does CouchDB calculate the Revision number

1 个答案:

答案 0 :(得分:1)

您可以找到here有关更改序列内部的一些信息

序列号反映了文档更新序列的群集状态,此信息被编码为序列号。

序列号的数字部分是每个群集节点中文档更新序列的总和。

序列号的第二部分是每个群集节点中具有更新序列信息的base64编码的字符串。