Skip to content

Commit 559f03f

Browse files
Merge pull request #536 from fergiemcdowall/v2-issue-535
fix issue #535
2 parents 7d14235 + d522d17 commit 559f03f

File tree

8 files changed

+107
-34
lines changed

8 files changed

+107
-34
lines changed
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/search-index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
search-index-2.2.0.js
1+
search-index-2.3.0.js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "search-index",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "A network resilient, persistent full-text search library for the browser and Node.js",
55
"engines": {
66
"node": ">=12"

src/main.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ const Cache = require('./cache.js')
44
const reader = require('./read.js')
55
const writer = require('./write.js')
66

7-
const makeASearchIndex = ops => {
7+
const makeASearchIndex = ops => new Promise((resolve) => {
88
// ".flush" clears the cache ".cache" creates/promotes a cache entry
99
const c = new Cache(ops.cacheLength)
1010

1111
const w = writer(ops.fii, ops) // TODO: should be just ops?
1212
const r = reader(ops.fii)
1313

14-
return {
14+
return w._INCREMENT_DOC_COUNT(0).then(() => resolve({
1515
// internal functions inherited from fergies-inverted-index
1616
_AND: ops.fii.AND,
1717
_BUCKET: ops.fii.BUCKET,
@@ -38,7 +38,7 @@ const makeASearchIndex = ops => {
3838
EXPORT: ops.fii.EXPORT,
3939
FACETS: r.FACETS,
4040
FIELDS: ops.fii.FIELDS,
41-
FLUSH: () => ops.fii.STORE.clear(),
41+
FLUSH: () => c.flush().then(w.FLUSH),
4242
IMPORT: idx => c.flush().then(() => ops.fii.IMPORT(idx)),
4343
INDEX: ops.fii,
4444
LAST_UPDATED: ops.fii.LAST_UPDATED,
@@ -47,8 +47,8 @@ const makeASearchIndex = ops => {
4747
PUT: (docs, pops) => c.flush().then(() => w.PUT(docs, pops)),
4848
PUT_RAW: docs => c.flush().then(() => w.PUT_RAW(docs)),
4949
QUERY: (q, qops) => c.cache({ QUERY: [q, qops] }, r.QUERY(q, qops))
50-
}
51-
}
50+
}))
51+
})
5252

5353
const initIndex = (ops = {}) => new Promise((resolve, reject) => {
5454
ops = Object.assign({

src/write.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ module.exports = (fii, ops) => {
6969
? ({ body: doc })
7070
: doc
7171

72-
let counter = 0;
72+
let counter = 0
7373
const generateId = (doc, i) => (typeof doc._id === 'undefined')
7474
? Object.assign(doc, {
7575
// counter is needed because if this function is called in quick
7676
// succession, Date.now() is not guaranteed to be unique. This could
7777
// still conflict if the DB is closed, clock is reset to the past, then
7878
// DB reopened. That's a bit of a corner case though.
79-
_id: `${Date.now()}-${i}-${counter++}`,
80-
})
81-
: doc;
79+
_id: `${Date.now()}-${i}-${counter++}`
80+
})
81+
: doc
8282

8383
const indexingPipeline = docs => new Promise(
8484
resolve => resolve(
@@ -126,13 +126,26 @@ module.exports = (fii, ops) => {
126126
]).then(() => result)
127127
})
128128

129+
const _FLUSH = () => ops.fii.STORE.clear()
130+
.then(() => {
131+
const timestamp = Date.now()
132+
return ops.fii.STORE.batch([
133+
{ type: 'put', key: '○○CREATED', value: timestamp },
134+
{ type: 'put', key: '○○LAST_UPDATED', value: timestamp },
135+
{ type: 'put', key: '○DOCUMENT_COUNT○', value: 0 }
136+
])
137+
})
138+
.then(() => true)
139+
129140
return {
130141
// TODO: DELETE should be able to handle errors (_id not found etc.)
131142
DELETE: docIds => _DELETE(docIds), // for external use
143+
FLUSH: _FLUSH,
132144
IMPORT: fii.IMPORT,
133145
PUT: _PUT,
134146
PUT_RAW: _PUT_RAW,
135-
_DELETE: _DELETE, // for internal use
147+
_DELETE: _DELETE,
148+
_INCREMENT_DOC_COUNT: incrementDocCount,
136149
_PUT: _PUT,
137150
_PUT_RAW: _PUT_RAW
138151
}

test/src/FLUSH-test.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ test('can add some data', t => {
3636

3737
test('verify index structure', t => {
3838
const expectedIndexStructure = [
39-
{ key: 'body.metadata:coolness#1.00', value: [ 'a' ] },
40-
{ key: 'body.metadata:documentness#1.00', value: [ 'a' ] },
41-
{ key: 'body.text:cool#1.00', value: [ 'a' ] },
42-
{ key: 'body.text:document#0.33', value: [ 'a' ] },
43-
{ key: 'body.text:is#0.33', value: [ 'a' ] },
44-
{ key: 'body.text:really#0.33', value: [ 'a' ] },
45-
{ key: 'body.text:this#0.33', value: [ 'a' ] },
46-
{ key: 'importantnumber:5000#1.00', value: [ 'a' ] },
47-
{ key: 'title:a#1.00', value: [ 'a' ] },
48-
{ key: 'title:cool#1.00', value: [ 'a' ] },
49-
{ key: 'title:document#1.00', value: [ 'a' ] },
50-
{ key: 'title:quite#1.00', value: [ 'a' ] },
39+
{ key: 'body.metadata:coolness#1.00', value: ['a'] },
40+
{ key: 'body.metadata:documentness#1.00', value: ['a'] },
41+
{ key: 'body.text:cool#1.00', value: ['a'] },
42+
{ key: 'body.text:document#0.33', value: ['a'] },
43+
{ key: 'body.text:is#0.33', value: ['a'] },
44+
{ key: 'body.text:really#0.33', value: ['a'] },
45+
{ key: 'body.text:this#0.33', value: ['a'] },
46+
{ key: 'importantnumber:5000#1.00', value: ['a'] },
47+
{ key: 'title:a#1.00', value: ['a'] },
48+
{ key: 'title:cool#1.00', value: ['a'] },
49+
{ key: 'title:document#1.00', value: ['a'] },
50+
{ key: 'title:quite#1.00', value: ['a'] },
5151
{ key: '○DOCUMENT_COUNT○', value: 1 },
5252
{
5353
key: '○DOC_RAW○a○',
@@ -65,14 +65,14 @@ test('verify index structure', t => {
6565
key: '○DOC○a○',
6666
value: {
6767
_id: 'a',
68-
title: [ 'a#1.00', 'cool#1.00', 'document#1.00', 'quite#1.00' ],
68+
title: ['a#1.00', 'cool#1.00', 'document#1.00', 'quite#1.00'],
6969
body: {
7070
text: [
7171
'cool#1.00', 'document#0.33', 'is#0.33', 'really#0.33', 'this#0.33'
7272
],
73-
metadata: [ 'coolness#1.00', 'documentness#1.00' ]
73+
metadata: ['coolness#1.00', 'documentness#1.00']
7474
},
75-
importantNumber: [ '5000#1.00' ]
75+
importantNumber: ['5000#1.00']
7676
}
7777
},
7878
{ key: '○FIELD○body.metadata○', value: 'body.metadata' },
@@ -83,14 +83,19 @@ test('verify index structure', t => {
8383
t.plan(expectedIndexStructure.length)
8484
global[indexName].INDEX.STORE.createReadStream({ lt: '○○' })
8585
.on('data', d => t.deepEquals(
86-
d, expectedIndexStructure.shift())
87-
)
86+
d, expectedIndexStructure.shift()
87+
))
8888
})
8989

9090
test('FLUSH index and verify', t => {
91-
t.plan(1)
91+
t.plan(2)
92+
const expectedIndexStructure = [
93+
{ key: '○DOCUMENT_COUNT○', value: 0 }
94+
]
9295
global[indexName].FLUSH().then(
9396
() => global[indexName].INDEX.STORE.createReadStream({ lt: '○○' })
94-
.on('data', d => t.fail('there shouldnt be anything here'))
95-
).then(() => t.pass('index appears empty'))
97+
.on('data', d => t.deepEquals(
98+
d, expectedIndexStructure.shift()
99+
))
100+
).then(() => t.pass('index appears empty'))
96101
})

test/src/issue-535-test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const si = require('../../')
2+
const test = require('tape')
3+
4+
const sandbox = 'test/sandbox/'
5+
6+
const docs = [
7+
{ _id: 'qwertyu', idx: 'q' },
8+
{ _id: 'asdfgh', idx: 'a' }
9+
]
10+
11+
test('set up as per issue #535', async function (t) {
12+
t.plan(7)
13+
14+
const { PUT, QUERY, FLUSH } = await si({
15+
name: sandbox + '535'
16+
})
17+
t.ok(PUT)
18+
19+
t.deepEquals(await PUT(docs), [
20+
{ _id: 'qwertyu', status: 'CREATED', operation: 'PUT' },
21+
{ _id: 'asdfgh', status: 'CREATED', operation: 'PUT' }
22+
])
23+
24+
t.deepEquals(await QUERY({
25+
SEARCH: ['q']
26+
}), {
27+
RESULT: [
28+
{ _id: 'qwertyu', _match: ['idx:q#1.00'], _score: 1.1 }
29+
],
30+
RESULT_LENGTH: 1
31+
})
32+
33+
t.ok(await FLUSH())
34+
35+
t.deepEquals(await QUERY({
36+
SEARCH: ['q']
37+
}), {
38+
RESULT: [],
39+
RESULT_LENGTH: 0
40+
})
41+
42+
t.deepEquals(await PUT(docs), [
43+
{ _id: 'qwertyu', status: 'CREATED', operation: 'PUT' },
44+
{ _id: 'asdfgh', status: 'CREATED', operation: 'PUT' }
45+
])
46+
47+
t.deepEquals(await QUERY({
48+
SEARCH: ['q']
49+
}), {
50+
RESULT: [
51+
{ _id: 'qwertyu', _match: ['idx:q#1.00'], _score: 1.1 }
52+
],
53+
RESULT_LENGTH: 1
54+
})
55+
})

0 commit comments

Comments
 (0)