Skip to content

Commit

Permalink
fix dodgy merge, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Jul 27, 2020
1 parent 9071da3 commit 37b098b
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 179 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fergies-inverted-index",
"version": "5.2.0",
"version": "5.2.1",
"description": "An inverted index that allows javascript objects to be easily serialised and retrieved using promises and map-reduce",
"main": "dist/fergies-inverted-index.cjs.js",
"module": "dist/fergies-inverted-index.esm.js",
Expand Down
56 changes: 27 additions & 29 deletions src/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default function init (db, ops) {
const parseToken = token => new Promise((resolve, reject) => {
// case: <value>
// case: <FIELD>:<VALUE>

// case: undefined

const setCase = str => ops.caseSensitive ? str : str.toLowerCase()
Expand Down Expand Up @@ -52,12 +51,14 @@ export default function init (db, ops) {
LTE: setCase(token.VALUE)
}
}

if (typeof token.VALUE === 'undefined') {
token.VALUE = {
GTE: '!',
LTE: '○'
}
}

token.VALUE = Object.assign(token.VALUE, {
GTE: setCase(token.VALUE.GTE || '!'),
LTE: setCase(token.VALUE.LTE || '○')
Expand All @@ -71,8 +72,10 @@ export default function init (db, ops) {
})
))
}

// Allow FIELD to be an array or a string
token.FIELD = [token.FIELD].flat()

return resolve(token)
})

Expand All @@ -82,26 +85,26 @@ export default function init (db, ops) {

// OR
const UNION = (...keys) => Promise.all(
keys.map(key => GET(key))
keys.map(GET)
).then(sets => {
// flatten
sets = [].concat.apply([], sets)
var setObject = sets.reduce((acc, cur) => {
acc[cur._id] = [...(acc[cur._id] || []), cur._match]
return acc
}, {})
const setObject = sets.flat(Infinity).reduce(
(acc, cur) => {
acc[cur._id] = [...(acc[cur._id] || []), cur._match]
return acc
},
{}
)
return Object.keys(setObject).map(id => ({
_id: id,
_match: setObject[id]
}))
})

// AND
const INTERSECTION = (...keys) => UNION(...keys).then(
result => result.filter(
const INTERSECTION = (...keys) => UNION(...keys)
.then(result => result.filter(
item => (item._match.length === keys.length)
)
)
))

// NOT (set a minus set b)
const SET_SUBTRACTION = (a, b) => Promise.all([
Expand All @@ -111,18 +114,18 @@ export default function init (db, ops) {
aItem => b.map(bItem => bItem._id).indexOf(aItem._id) === -1)
)

const RANGE = ops => new Promise(resolve => {
const RANGE = token => new Promise(resolve => {
const rs = {} // resultset
return Promise.all(
ops.FIELD.map(
fieldName => new Promise(resolve =>
db.createReadStream({
gte: fieldName + ':' + ops.VALUE.GTE,
lte: fieldName + ':' + ops.VALUE.LTE + '○'
token.FIELD.map(
fieldName => new Promise(resolve => {
return db.createReadStream({
gte: fieldName + ':' + token.VALUE.GTE + ops.tokenAppend,
lte: fieldName + ':' + token.VALUE.LTE + ops.tokenAppend + '○'
}).on('data', token => token.value.forEach(docId => {
rs[docId] = [...(rs[docId] || []), token.key]
})).on('end', resolve)
)
})
)
).then(() => resolve(
// convert map into array
Expand Down Expand Up @@ -208,17 +211,12 @@ export default function init (db, ops) {
}).on('data', resolve)
})

const DIST = ops => new Promise(
resolve => (ops || {}).FIELD
// bump string or Array to Array
? resolve([ops.FIELD].flat(Infinity))
: AVAILABLE_FIELDS().then(resolve)
).then(fields => Promise.all(
fields.map(field => getRange({
gte: field + ':' + ((ops && ops.VALUE && ops.VALUE.GTE) || ''),
lte: field + ':' + ((ops && ops.VALUE && ops.VALUE.LTE) || '') + '○'
const DIST = token => parseToken(token).then(token => Promise.all(
token.FIELD.map(field => getRange({
gte: field + ':' + token.VALUE.GTE,
lte: field + ':' + token.VALUE.LTE + '○'
}).then(items => items.map(item => ({
FIELD: [item.split(/:(.+)/)[0]],
FIELD: item.split(/:(.+)/)[0],
VALUE: item.split(/:(.+)/)[1]
}))))
)).then(result => result.flat())
Expand Down
64 changes: 29 additions & 35 deletions test/cjs/aggregation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function init (db, ops) {
const parseToken = token => new Promise((resolve, reject) => {
// case: <value>
// case: <FIELD>:<VALUE>

// case: undefined

const setCase = str => ops.caseSensitive ? str : str.toLowerCase();
Expand Down Expand Up @@ -60,12 +59,14 @@ function init (db, ops) {
LTE: setCase(token.VALUE)
};
}

if (typeof token.VALUE === 'undefined') {
token.VALUE = {
GTE: '!',
LTE: '○'
};
}

token.VALUE = Object.assign(token.VALUE, {
GTE: setCase(token.VALUE.GTE || '!'),
LTE: setCase(token.VALUE.LTE || '○')
Expand All @@ -92,7 +93,7 @@ function init (db, ops) {

// OR
const UNION = (...keys) => Promise.all(
keys.map(key => GET(key))
keys.map(GET)
).then(sets => {
const setObject = sets.flat(Infinity).reduce(
(acc, cur) => {
Expand All @@ -108,11 +109,10 @@ function init (db, ops) {
});

// AND
const INTERSECTION = (...keys) => UNION(...keys).then(
result => result.filter(
const INTERSECTION = (...keys) => UNION(...keys)
.then(result => result.filter(
item => (item._match.length === keys.length)
)
);
));

// NOT (set a minus set b)
const SET_SUBTRACTION = (a, b) => Promise.all([
Expand All @@ -122,19 +122,18 @@ function init (db, ops) {
aItem => b.map(bItem => bItem._id).indexOf(aItem._id) === -1)
);


const RANGE = ops => new Promise(resolve => {
const rs = {}; // resultset
const RANGE = token => new Promise(resolve => {
const rs = {}; // resultset
return Promise.all(
ops.FIELD.map(
fieldName => new Promise(resolve =>
db.createReadStream({
gte: fieldName + ':' + ops.VALUE.GTE,
lte: fieldName + ':' + ops.VALUE.LTE + '○'
token.FIELD.map(
fieldName => new Promise(resolve => {
return db.createReadStream({
gte: fieldName + ':' + token.VALUE.GTE + ops.tokenAppend,
lte: fieldName + ':' + token.VALUE.LTE + ops.tokenAppend + '○'
}).on('data', token => token.value.forEach(docId => {
rs[docId] = [...(rs[docId] || []), token.key];
})).on('end', resolve)
)
})
)
).then(() => resolve(
// convert map into array
Expand All @@ -144,7 +143,7 @@ function init (db, ops) {
}))
))
});

const AVAILABLE_FIELDS = () => new Promise(resolve => {
const fieldNames = [];
db.createReadStream({
Expand Down Expand Up @@ -188,7 +187,7 @@ function init (db, ops) {
})
})
);

const OBJECT = _ids => Promise.all(
_ids.map(
id => db.get('○DOC○' + id._id + '○').catch(reason => null)
Expand Down Expand Up @@ -220,21 +219,16 @@ function init (db, ops) {
}).on('data', resolve);
});

const DIST = ops => new Promise(
resolve => (ops || {}).FIELD
// bump string or Array to Array
? resolve([ops.FIELD].flat(Infinity))
: AVAILABLE_FIELDS().then(resolve)
).then(fields => Promise.all(
fields.map(field => getRange({
gte: field + ':' + ((ops && ops.VALUE && ops.VALUE.GTE) || ''),
lte: field + ':' + ((ops && ops.VALUE && ops.VALUE.LTE) || '') + '○'
const DIST = token => parseToken(token).then(token => Promise.all(
token.FIELD.map(field => getRange({
gte: field + ':' + token.VALUE.GTE,
lte: field + ':' + token.VALUE.LTE + '○'
}).then(items => items.map(item => ({
FIELD: [ item.split(/:(.+)/)[0] ],
FIELD: item.split(/:(.+)/)[0],
VALUE: item.split(/:(.+)/)[1]
}))))
)).then(result => result.flat());

return {
FIELDS: AVAILABLE_FIELDS,
BUCKET: BUCKET,
Expand Down Expand Up @@ -612,11 +606,11 @@ test('can GET a single bucket with gte LTE', t => {
test('can get DISTINCT values', t => {
t.plan(1);
global[indexName].DISTINCT({
FIELD:'make'
FIELD: 'make'
}).then(result => t.deepEquals(result, [
{ FIELD: [ 'make' ], VALUE: 'BMW' },
{ FIELD: [ 'make' ], VALUE: 'Tesla' },
{ FIELD: [ 'make' ], VALUE: 'Volvo' }
{ FIELD: 'make', VALUE: 'BMW' },
{ FIELD: 'make', VALUE: 'Tesla' },
{ FIELD: 'make', VALUE: 'Volvo' }
]));
});

Expand All @@ -628,8 +622,8 @@ test('can get DISTINCT values with gte', t => {
GTE: 'C'
}
}).then(result => t.deepEquals(result, [
{ FIELD: [ 'make' ], VALUE: 'Tesla' },
{ FIELD: [ 'make' ], VALUE: 'Volvo' }
{ FIELD: 'make', VALUE: 'Tesla' },
{ FIELD: 'make', VALUE: 'Volvo' }
]));
});

Expand All @@ -642,7 +636,7 @@ test('can get DISTINCT VALUEs with GTE and LTE', t => {
LTE: 'U'
}
}).then(result => t.deepEquals(result, [
{ FIELD: [ 'make' ], VALUE: 'Tesla' }
{ FIELD: 'make', VALUE: 'Tesla' }
]));
});

Expand Down
Loading

0 comments on commit 37b098b

Please sign in to comment.