mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
35 lines
624 B
JavaScript
35 lines
624 B
JavaScript
'use strict';
|
|
|
|
const OpTypes = require('./Operation').Types;
|
|
|
|
class KVIndex {
|
|
constructor() {
|
|
this._index = {};
|
|
}
|
|
|
|
get(key) {
|
|
return this._index[key];
|
|
}
|
|
|
|
updateIndex(oplog) {
|
|
let handled = [];
|
|
const _createLWWSet = (item) => {
|
|
if(handled.indexOf(item.key) === -1) {
|
|
handled.push(item.key);
|
|
if(OpTypes.isInsert(item.op))
|
|
return item;
|
|
}
|
|
return null;
|
|
};
|
|
|
|
this._index = {};
|
|
oplog.ops
|
|
.reverse()
|
|
.map(_createLWWSet)
|
|
.filter((f) => f !== null)
|
|
.forEach((f) => this._index[f.key] = f.value);
|
|
}
|
|
}
|
|
|
|
module.exports = KVIndex;
|