better naming

FragmentIndex -> FragmentIdIndex (index of fragment ids,
   not an index number in a fragment collection)
EventKey -> EventIndex (implies being ordered)
FragmentKey -> FragmentId
  (implies not being ordered, hence FragmentIdIndex)
This commit is contained in:
Bruno Windels 2019-05-01 15:36:32 +02:00
parent d90411a6dd
commit 99c8816bf9
3 changed files with 84 additions and 84 deletions

View File

@ -1,10 +1,10 @@
- DONE: write FragmentIndex - DONE: write FragmentIndex
- adapt SortKey ... naming! : - DONE: adapt SortKey ... naming! :
- FragmentIndex (index as in db index) - FragmentIdIndex (index as in db index)
- compare(fragmentKeyA, fragmentKeyB) - compare(idA, idB)
- SortKey - SortKey
- FragmentKey - FragmentId
- EventKey (we don't use id here because we already have event_id in the event) - EventIndex
- write fragmentStore - write fragmentStore
- adapt timelineStore - adapt timelineStore
- adapt persister - adapt persister

View File

@ -106,7 +106,7 @@ class Island {
/* /*
index for fast lookup of how two fragments can be sorted index for fast lookup of how two fragments can be sorted
*/ */
export default class FragmentIndex { export default class FragmentIdIndex {
constructor(fragments) { constructor(fragments) {
this.rebuild(fragments); this.rebuild(fragments);
} }
@ -163,7 +163,7 @@ export default class FragmentIndex {
export function tests() { export function tests() {
return { return {
test_1_island_3_fragments(assert) { test_1_island_3_fragments(assert) {
const index = new FragmentIndex([ const index = new FragmentIdIndex([
{id: 3, previousId: 2}, {id: 3, previousId: 2},
{id: 1, nextId: 2}, {id: 1, nextId: 2},
{id: 2, nextId: 3, previousId: 1}, {id: 2, nextId: 3, previousId: 1},
@ -180,7 +180,7 @@ export function tests() {
assert.equal(index.compare(1, 1), 0); assert.equal(index.compare(1, 1), 0);
}, },
test_2_island_dont_compare(assert) { test_2_island_dont_compare(assert) {
const index = new FragmentIndex([ const index = new FragmentIdIndex([
{id: 1}, {id: 1},
{id: 2}, {id: 2},
]); ]);
@ -188,7 +188,7 @@ export function tests() {
assert.throws(() => index.compare(2, 1)); assert.throws(() => index.compare(2, 1));
}, },
test_2_island_compare_internally(assert) { test_2_island_compare_internally(assert) {
const index = new FragmentIndex([ const index = new FragmentIdIndex([
{id: 1, nextId: 2}, {id: 1, nextId: 2},
{id: 2, previousId: 1}, {id: 2, previousId: 1},
{id: 11, nextId: 12}, {id: 11, nextId: 12},
@ -203,12 +203,12 @@ export function tests() {
assert.throws(() => index.compare(12, 2)); assert.throws(() => index.compare(12, 2));
}, },
test_unknown_id(assert) { test_unknown_id(assert) {
const index = new FragmentIndex([{id: 1}]); const index = new FragmentIdIndex([{id: 1}]);
assert.throws(() => index.compare(1, 2)); assert.throws(() => index.compare(1, 2));
assert.throws(() => index.compare(2, 1)); assert.throws(() => index.compare(2, 1));
}, },
test_rebuild_flushes_old_state(assert) { test_rebuild_flushes_old_state(assert) {
const index = new FragmentIndex([ const index = new FragmentIdIndex([
{id: 1, nextId: 2}, {id: 1, nextId: 2},
{id: 2, previousId: 1}, {id: 2, previousId: 1},
]); ]);

View File

@ -11,32 +11,32 @@ const MID = MID_UINT32;
const MAX = MAX_UINT32; const MAX = MAX_UINT32;
export default class SortKey { export default class SortKey {
constructor(fragmentIndex, buffer) { constructor(fragmentIdComparer, buffer) {
if (buffer) { if (buffer) {
this._keys = new DataView(buffer); this._keys = new DataView(buffer);
} else { } else {
this._keys = new DataView(new ArrayBuffer(8)); this._keys = new DataView(new ArrayBuffer(8));
// start default key right at the middle fragment key, min event key // start default key right at the middle fragment key, min event key
// so we have the same amount of key address space either way // so we have the same amount of key address space either way
this.fragmentKey = MID; this.fragmentId = MID;
this.eventKey = MIN; this.eventIndex = MIN;
} }
this._fragmentIndex = fragmentIndex; this._fragmentIdComparer = fragmentIdComparer;
} }
get fragmentKey() { get fragmentId() {
return this._keys.getUint32(0, false); return this._keys.getUint32(0, false);
} }
set fragmentKey(value) { set fragmentId(value) {
return this._keys.setUint32(0, value, false); return this._keys.setUint32(0, value, false);
} }
get eventKey() { get eventIndex() {
return this._keys.getUint32(4, false); return this._keys.getUint32(4, false);
} }
set eventKey(value) { set eventIndex(value) {
return this._keys.setUint32(4, value, false); return this._keys.setUint32(4, value, false);
} }
@ -45,136 +45,136 @@ export default class SortKey {
} }
nextFragmentKey() { nextFragmentKey() {
const k = new SortKey(this._fragmentIndex); const k = new SortKey(this._fragmentIdComparer);
k.fragmentKey = this.fragmentKey + 1; k.fragmentId = this.fragmentId + 1;
k.eventKey = MIN; k.eventIndex = MIN;
return k; return k;
} }
nextKey() { nextKey() {
const k = new SortKey(this._fragmentIndex); const k = new SortKey(this._fragmentIdComparer);
k.fragmentKey = this.fragmentKey; k.fragmentId = this.fragmentId;
k.eventKey = this.eventKey + 1; k.eventIndex = this.eventIndex + 1;
return k; return k;
} }
previousKey() { previousKey() {
const k = new SortKey(this._fragmentIndex); const k = new SortKey(this._fragmentIdComparer);
k.fragmentKey = this.fragmentKey; k.fragmentId = this.fragmentId;
k.eventKey = this.eventKey - 1; k.eventIndex = this.eventIndex - 1;
return k; return k;
} }
clone() { clone() {
const k = new SortKey(); const k = new SortKey();
k.fragmentKey = this.fragmentKey; k.fragmentId = this.fragmentId;
k.eventKey = this.eventKey; k.eventIndex = this.eventIndex;
return k; return k;
} }
static get maxKey() { static get maxKey() {
const maxKey = new SortKey(null); const maxKey = new SortKey(null);
maxKey.fragmentKey = MAX; maxKey.fragmentId = MAX;
maxKey.eventKey = MAX; maxKey.eventIndex = MAX;
return maxKey; return maxKey;
} }
static get minKey() { static get minKey() {
const minKey = new SortKey(null); const minKey = new SortKey(null);
minKey.fragmentKey = MIN; minKey.fragmentId = MIN;
minKey.eventKey = MIN; minKey.eventIndex = MIN;
return minKey; return minKey;
} }
compare(otherKey) { compare(otherKey) {
const fragmentDiff = this.fragmentKey - otherKey.fragmentKey; const fragmentDiff = this.fragmentId - otherKey.fragmentId;
if (fragmentDiff === 0) { if (fragmentDiff === 0) {
return this.eventKey - otherKey.eventKey; return this.eventIndex - otherKey.eventIndex;
} else { } else {
// minKey and maxKey might not have fragmentIndex, so short-circuit this first ... // minKey and maxKey might not have fragmentIdComparer, so short-circuit this first ...
if (this.fragmentKey === MIN || otherKey.fragmentKey === MAX) { if (this.fragmentId === MIN || otherKey.fragmentId === MAX) {
return -1; return -1;
} }
if (this.fragmentKey === MAX || otherKey.fragmentKey === MIN) { if (this.fragmentId === MAX || otherKey.fragmentId === MIN) {
return 1; return 1;
} }
// ... then delegate to fragmentIndex. // ... then delegate to fragmentIdComparer.
// This might throw if the relation of two fragments is unknown. // This might throw if the relation of two fragments is unknown.
return this._fragmentIndex.compare(this.fragmentKey, otherKey.fragmentKey); return this._fragmentIdComparer.compare(this.fragmentId, otherKey.fragmentId);
} }
} }
toString() { toString() {
return `[${this.fragmentKey}/${this.eventKey}]`; return `[${this.fragmentId}/${this.eventIndex}]`;
} }
} }
//#ifdef TESTS //#ifdef TESTS
export function tests() { export function tests() {
const fragmentIndex = {compare: (a, b) => a - b}; const fragmentIdComparer = {compare: (a, b) => a - b};
return { return {
test_default_key(assert) { test_default_key(assert) {
const k = new SortKey(fragmentIndex); const k = new SortKey(fragmentIdComparer);
assert.equal(k.fragmentKey, MID); assert.equal(k.fragmentId, MID);
assert.equal(k.eventKey, MIN); assert.equal(k.eventIndex, MIN);
}, },
test_inc(assert) { test_inc(assert) {
const a = new SortKey(fragmentIndex); const a = new SortKey(fragmentIdComparer);
const b = a.nextKey(); const b = a.nextKey();
assert.equal(a.fragmentKey, b.fragmentKey); assert.equal(a.fragmentId, b.fragmentId);
assert.equal(a.eventKey + 1, b.eventKey); assert.equal(a.eventIndex + 1, b.eventIndex);
const c = b.previousKey(); const c = b.previousKey();
assert.equal(b.fragmentKey, c.fragmentKey); assert.equal(b.fragmentId, c.fragmentId);
assert.equal(c.eventKey + 1, b.eventKey); assert.equal(c.eventIndex + 1, b.eventIndex);
assert.equal(a.eventKey, c.eventKey); assert.equal(a.eventIndex, c.eventIndex);
}, },
test_min_key(assert) { test_min_key(assert) {
const minKey = SortKey.minKey; const minKey = SortKey.minKey;
const k = new SortKey(fragmentIndex); const k = new SortKey(fragmentIdComparer);
assert(minKey.fragmentKey <= k.fragmentKey); assert(minKey.fragmentId <= k.fragmentId);
assert(minKey.eventKey <= k.eventKey); assert(minKey.eventIndex <= k.eventIndex);
assert(k.compare(minKey) > 0); assert(k.compare(minKey) > 0);
assert(minKey.compare(k) < 0); assert(minKey.compare(k) < 0);
}, },
test_max_key(assert) { test_max_key(assert) {
const maxKey = SortKey.maxKey; const maxKey = SortKey.maxKey;
const k = new SortKey(fragmentIndex); const k = new SortKey(fragmentIdComparer);
assert(maxKey.fragmentKey >= k.fragmentKey); assert(maxKey.fragmentId >= k.fragmentId);
assert(maxKey.eventKey >= k.eventKey); assert(maxKey.eventIndex >= k.eventIndex);
assert(k.compare(maxKey) < 0); assert(k.compare(maxKey) < 0);
assert(maxKey.compare(k) > 0); assert(maxKey.compare(k) > 0);
}, },
test_immutable(assert) { test_immutable(assert) {
const a = new SortKey(fragmentIndex); const a = new SortKey(fragmentIdComparer);
const fragmentKey = a.fragmentKey; const fragmentId = a.fragmentId;
const eventKey = a.eventKey; const eventIndex = a.eventIndex;
a.nextFragmentKey(); a.nextFragmentKey();
assert.equal(a.fragmentKey, fragmentKey); assert.equal(a.fragmentId, fragmentId);
assert.equal(a.eventKey, eventKey); assert.equal(a.eventIndex, eventIndex);
}, },
test_cmp_fragmentkey_first(assert) { test_cmp_fragmentid_first(assert) {
const a = new SortKey(fragmentIndex); const a = new SortKey(fragmentIdComparer);
const b = new SortKey(fragmentIndex); const b = new SortKey(fragmentIdComparer);
a.fragmentKey = 2; a.fragmentId = 2;
a.eventKey = 1; a.eventIndex = 1;
b.fragmentKey = 1; b.fragmentId = 1;
b.eventKey = 100000; b.eventIndex = 100000;
assert(a.compare(b) > 0); assert(a.compare(b) > 0);
}, },
test_cmp_eventkey_second(assert) { test_cmp_eventindex_second(assert) {
const a = new SortKey(fragmentIndex); const a = new SortKey(fragmentIdComparer);
const b = new SortKey(fragmentIndex); const b = new SortKey(fragmentIdComparer);
a.fragmentKey = 1; a.fragmentId = 1;
a.eventKey = 100000; a.eventIndex = 100000;
b.fragmentKey = 1; b.fragmentId = 1;
b.eventKey = 2; b.eventIndex = 2;
assert(a.compare(b) > 0); assert(a.compare(b) > 0);
}, },
@ -182,13 +182,13 @@ export function tests() {
assert(SortKey.minKey.compare(SortKey.maxKey) < 0); assert(SortKey.minKey.compare(SortKey.maxKey) < 0);
}, },
test_cmp_fragmentkey_first_large(assert) { test_cmp_fragmentid_first_large(assert) {
const a = new SortKey(fragmentIndex); const a = new SortKey(fragmentIdComparer);
const b = new SortKey(fragmentIndex); const b = new SortKey(fragmentIdComparer);
a.fragmentKey = MAX; a.fragmentId = MAX;
a.eventKey = MIN; a.eventIndex = MIN;
b.fragmentKey = MIN; b.fragmentId = MIN;
b.eventKey = MAX; b.eventIndex = MAX;
assert(b < a); assert(b < a);
assert(a > b); assert(a > b);
} }