mirror of
https://github.com/hoytech/strfry.git
synced 2025-06-18 09:17:12 +00:00
wip
This commit is contained in:
56
js/xor.js
56
js/xor.js
@ -112,6 +112,62 @@ class XorView {
|
||||
lastTimestampOut[0] = temp;
|
||||
return encodeVarInt(timestamp + 1);
|
||||
};
|
||||
|
||||
let appendBoundKey = (key, output) => {
|
||||
output.push(...encodeTimestampOut(key.timestamp));
|
||||
output.push(...encodeVarInt(key.id.length));
|
||||
output.push(...key.id);
|
||||
};
|
||||
|
||||
let appendMinimalBoundKey = (curr, prev) => {
|
||||
output.push(...encodeTimestampOut(curr.timestamp));
|
||||
|
||||
if (curr.timestamp !== prev.timestamp) {
|
||||
output.push(...encodeVarInt(0));
|
||||
} else {
|
||||
let sharedPrefixBytes = 0;
|
||||
|
||||
for (let i = 0; i < this.idSize; i++) {
|
||||
if (curr.id[i] !== prev.id[i]) break;
|
||||
sharedPrefixBytes++;
|
||||
}
|
||||
|
||||
output.push(...encodeVarInt(sharedPrefixBytes + 1));
|
||||
output.push(...curr.id.slice(0, sharedPrefixBytes + 1));
|
||||
}
|
||||
};
|
||||
|
||||
// Split our range
|
||||
let numElems = upper - lower;
|
||||
let buckets = 16;
|
||||
|
||||
if (numElems < buckets * 2) {
|
||||
appendBoundKey(lowerKey);
|
||||
appendBoundKey(upperKey);
|
||||
|
||||
output.push(...encodeVarInt(numElems + 8));
|
||||
for (let it = lower; it < upper; ++it) output.push(...this.elems[it].id);
|
||||
} else {
|
||||
let elemsPerBucket = Math.floor(numElems / buckets);
|
||||
let bucketsWithExtra = numElems % buckets;
|
||||
let curr = lower;
|
||||
|
||||
for (let i = 0; i < buckets; i++) {
|
||||
if (i == 0) appendBoundKey(lowerKey);
|
||||
else appendMinimalBoundKey(this.elems[curr], this.elems[curr - 1]);
|
||||
|
||||
let ourXorSet = new Array(this.idSize).fill(0);
|
||||
for (let bucketEnd = curr + elemsPerBucket + (i < bucketsWithExtra ? 1 : 0); curr != bucketEnd; curr++) {
|
||||
for (let j = 0; j < this.idSize; j++) ourXorSet[j] ^= this.elems[curr][j];
|
||||
}
|
||||
|
||||
if (i === buckets - 1) appendBoundKey(upperKey);
|
||||
else appendMinimalBoundKey(this.elems[curr], this.elems[curr - 1]);
|
||||
|
||||
output.push(...encodeVarInt(0)); // mode = 0
|
||||
output.push(...ourXorSet.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
16
src/xor.h
16
src/xor.h
@ -157,13 +157,13 @@ struct XorView {
|
||||
return encodeVarInt(timestamp + 1);
|
||||
};
|
||||
|
||||
auto appendBoundKey = [&](uint64_t t, std::string k, std::string &output) {
|
||||
auto appendBoundKey = [&](uint64_t t, std::string k) {
|
||||
output += encodeTimestampOut(t);
|
||||
output += encodeVarInt(k.size());
|
||||
output += k;
|
||||
};
|
||||
|
||||
auto appendMinimalBoundKey = [&](const XorElem &curr, const XorElem &prev, std::string &output) {
|
||||
auto appendMinimalBoundKey = [&](const XorElem &curr, const XorElem &prev) {
|
||||
output += encodeTimestampOut(curr.timestamp);
|
||||
|
||||
if (curr.timestamp != prev.timestamp) {
|
||||
@ -188,8 +188,8 @@ struct XorView {
|
||||
const uint64_t buckets = 16;
|
||||
|
||||
if (numElems < buckets * 2) {
|
||||
appendBoundKey(lowerTimestamp, lowerKey, output);
|
||||
appendBoundKey(upperTimestamp, upperKey, output);
|
||||
appendBoundKey(lowerTimestamp, lowerKey);
|
||||
appendBoundKey(upperTimestamp, upperKey);
|
||||
|
||||
output += encodeVarInt(numElems + 8);
|
||||
for (auto it = lower; it < upper; ++it) output += it->getId(idSize);
|
||||
@ -199,16 +199,16 @@ struct XorView {
|
||||
auto curr = lower;
|
||||
|
||||
for (uint64_t i = 0; i < buckets; i++) {
|
||||
if (i == 0) appendBoundKey(lowerTimestamp, lowerKey, output);
|
||||
else appendMinimalBoundKey(*curr, *std::prev(curr), output);
|
||||
if (i == 0) appendBoundKey(lowerTimestamp, lowerKey);
|
||||
else appendMinimalBoundKey(*curr, *std::prev(curr));
|
||||
|
||||
XorElem ourXorSet;
|
||||
for (auto bucketEnd = curr + elemsPerBucket + (i < bucketsWithExtra ? 1 : 0); curr != bucketEnd; curr++) {
|
||||
ourXorSet ^= *curr;
|
||||
}
|
||||
|
||||
if (i == buckets - 1) appendBoundKey(upperTimestamp, upperKey, output);
|
||||
else appendMinimalBoundKey(*curr, *std::prev(curr), output);
|
||||
if (i == buckets - 1) appendBoundKey(upperTimestamp, upperKey);
|
||||
else appendMinimalBoundKey(*curr, *std::prev(curr));
|
||||
|
||||
output += encodeVarInt(0); // mode = 0
|
||||
output += ourXorSet.getId(idSize);
|
||||
|
@ -55,7 +55,7 @@ int main() {
|
||||
x1.finalise();
|
||||
x2.finalise();
|
||||
|
||||
std::string q = x1.initialQuery();
|
||||
std::string q = x1.initial();
|
||||
|
||||
uint64_t round = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user