Skip to content

Commit

Permalink
fixed cacheLength thing, isLeaf option is no longer user-definable
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Aug 18, 2023
1 parent 1faef27 commit 9fa8e7d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 91 deletions.
3 changes: 1 addition & 2 deletions src/DocumentProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const DocumentProcessor = ops => {

const processValueUnknownType = (unknown, key) =>
// eslint-disable-next-line
new Promise(async resolve => {
new Promise(async resolve => {
if (unknown === null) return resolve([null, '1.00'])
if (isEmptyObject(unknown)) return resolve([[], '1.00'])
if (Number.isInteger(unknown)) return resolve([unknown, unknown])
Expand All @@ -42,7 +42,6 @@ export const DocumentProcessor = ops => {
if (isString(doc)) doc = { body: doc }

// Docs with no _id are auto-assigned an ID
// if (!doc.hasOwnProperty('_id')) doc._id = ops.idGenerator.next().value
if (!Object.prototype.hasOwnProperty.call(doc, '_id')) {
doc._id = ops.idGenerator.next().value
}
Expand Down
26 changes: 14 additions & 12 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@ export class Main {
yield Date.now() + '-' + i++
}
})(),
isLeaf: node =>
Array.isArray(node) &&
node.length === 2 &&
node.every(
item =>
typeof item === 'string' ||
typeof item === 'number' ||
item === null
),
skipFields: [],
ngrams: {},
replace: {},
storeRawDocs: true,
stopwords: [],
storeVectors: true, // TODO: make a test for this being false
tokenAppend: '#',
tokenSplitRegex: /[\p{L}\d]+/gu,
tokenizer: tokenization.tokenizationPipeline,
...ops
}
this.INDEX = new InvertedIndex(ops)
this.INDEX = new InvertedIndex({
...ops,
// isLeaf must be like so and is not a user defined option
isLeaf: node =>
Array.isArray(node) &&
node.length === 2 &&
node.every(
item =>
typeof item === 'string' ||
typeof item === 'number' ||
item === null
)
})
// Now that constructor is not async- not sure where this should be called...
this._CACHE = new LRUCache({ max: 1000 })
this._CACHE = new LRUCache({ max: ops.cacheLength })
this.r = new Reader(ops, this._CACHE, this.INDEX)
this.w = new Writer(ops, this._CACHE, this.INDEX)
validateVersion(this.INDEX)
Expand Down
1 change: 1 addition & 0 deletions src/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class Writer {
}

DELETE_RAW (...docIds) {
// TODO: should we be using ops.docExistsSpace instead of DOC_RAW?
return Promise.all(docIds.map(id => this.#ii.STORE.del(['DOC_RAW', id])))
}

Expand Down
202 changes: 125 additions & 77 deletions test/src/CACHE-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,85 @@ const sandbox = 'test/sandbox/'
const indexName = sandbox + 'CACHE'
const global = {}

test('create a search index', async t => {
const data = [
{
_id: 0,
make: 'Tesla',
manufacturer: 'Volvo',
brand: 'Volvo',
colour: 'yellow'
},
{
_id: 1,
make: 'BMW',
manufacturer: 'Volvo',
brand: 'Volvo',
colour: 'red'
},
{
_id: 2,
make: 'Tesla',
manufacturer: 'Tesla',
brand: 'Volvo',
colour: 'blue'
},
{
_id: 3,
make: 'Tesla',
manufacturer: 'Volvo',
brand: 'BMW',
colour: 'red'
},
{
_id: 4,
make: 'Volvo',
manufacturer: 'Volvo',
brand: 'Volvo',
colour: 'red'
},
{
_id: 5,
make: 'Volvo',
manufacturer: 'Tesla',
brand: 'Volvo',
colour: 'blue'
},
{
_id: 6,
make: 'Tesla',
manufacturer: 'Tesla',
brand: 'BMW',
colour: 'yellow'
},
{
_id: 7,
make: 'BMW',
manufacturer: 'Tesla',
brand: 'Tesla',
colour: 'yellow'
},
{
_id: 8,
make: 'Volvo',
manufacturer: 'BMW',
brand: 'Tesla',
colour: 'blue'
},
{
_id: 9,
make: 'BMW',
manufacturer: 'Tesla',
brand: 'Volvo',
colour: 'red'
}
]

test('create a search index', t => {
t.plan(1)
try {
global[indexName] = await new SearchIndex({
name: indexName,
cacheLength: 5 // TODO: this needs to be updated
global[indexName] = new SearchIndex({
name: indexName
// cacheLength: 5 // TODO: this needs to be updated
})
t.ok(global[indexName])
} catch (e) {
Expand All @@ -20,79 +93,6 @@ test('create a search index', async t => {
})

test('can add data', t => {
const data = [
{
_id: 0,
make: 'Tesla',
manufacturer: 'Volvo',
brand: 'Volvo',
colour: 'yellow'
},
{
_id: 1,
make: 'BMW',
manufacturer: 'Volvo',
brand: 'Volvo',
colour: 'red'
},
{
_id: 2,
make: 'Tesla',
manufacturer: 'Tesla',
brand: 'Volvo',
colour: 'blue'
},
{
_id: 3,
make: 'Tesla',
manufacturer: 'Volvo',
brand: 'BMW',
colour: 'red'
},
{
_id: 4,
make: 'Volvo',
manufacturer: 'Volvo',
brand: 'Volvo',
colour: 'red'
},
{
_id: 5,
make: 'Volvo',
manufacturer: 'Tesla',
brand: 'Volvo',
colour: 'blue'
},
{
_id: 6,
make: 'Tesla',
manufacturer: 'Tesla',
brand: 'BMW',
colour: 'yellow'
},
{
_id: 7,
make: 'BMW',
manufacturer: 'Tesla',
brand: 'Tesla',
colour: 'yellow'
},
{
_id: 8,
make: 'Volvo',
manufacturer: 'BMW',
brand: 'Tesla',
colour: 'blue'
},
{
_id: 9,
make: 'BMW',
manufacturer: 'Tesla',
brand: 'Volvo',
colour: 'red'
}
]

t.plan(1)
global[indexName].PUT(data).then(t.pass)
})
Expand Down Expand Up @@ -319,3 +319,51 @@ test('cache is filling up again', t => {
'{"QUERY":["boooom",null]}'
)
})

test('test cacheLength', t => {
t.plan(9)
const cacheLengthIndex = new SearchIndex({
name: sandbox + 'cacheLength-test',
cacheLength: 5
})
t.ok(cacheLengthIndex)
cacheLengthIndex
.PUT(data)
.then(t.ok)
.then(() => cacheLengthIndex.QUERY('one'))
.then(res => cacheLengthIndex.QUERY('two'))
.then(res => cacheLengthIndex.QUERY('two'))
.then(res => cacheLengthIndex.QUERY('two'))
.then(res => cacheLengthIndex.QUERY('three'))
.then(res => cacheLengthIndex.QUERY('three'))
.then(res => cacheLengthIndex.QUERY('three'))
.then(res => cacheLengthIndex.DICTIONARY())
.then(res => cacheLengthIndex.DOCUMENTS())
.then(res => cacheLengthIndex.QUERY('three'))
.then(res => cacheLengthIndex.QUERY('three'))
.then(res => cacheLengthIndex.SEARCH('tesla'))
.then(res => cacheLengthIndex.SEARCH('tesla'))
.then(res => cacheLengthIndex.SEARCH(['tesla']))
.then(res => cacheLengthIndex.QUERY('three'))
.then(res => cacheLengthIndex.QUERY('four'))
.then(res => t.pass('done'))
.then(() => {
const keys = [
'{"QUERY":["four",null]}',
'{"QUERY":["three",null]}',
'{"SEARCH":[["tesla"],null]}',
'{"SEARCH":["tesla",null]}',
'{"DOCUMENTS":[]}'
]
// cache only has 5 entries since cacheLength:5
let cacheSize = 0
const cKeys = cacheLengthIndex._CACHE.keys()
keys.forEach(item => {
cacheSize++
const cKeysNext = cKeys.next().value
t.deepEquals(cKeysNext, item)
})
t.equal(cacheSize, 5)
})
.catch(t.error)
})

0 comments on commit 9fa8e7d

Please sign in to comment.