-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from markwylde/collections-metadata
Implement collections metadata
- Loading branch information
Showing
8 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const debarrel = require('debarrel'); | ||
|
||
async function upsertRecord (driver, collectionId, changes) { | ||
const collection = (await driver.get('system.collections', { collectionId }))[0]; | ||
if (collection) { | ||
await driver.patch('system.collections', { | ||
documentCount: collection.documentCount + changes.documentCountAdd | ||
}, { | ||
collectionId | ||
}); | ||
} else { | ||
await driver.post('system.collections', { | ||
collectionId, | ||
documentCount: changes.documentCountAdd | ||
}); | ||
} | ||
} | ||
|
||
function createCollectionMetadataUpdater (state) { | ||
const cache = {}; | ||
let processing = false; | ||
|
||
async function processCache (driver, cache) { | ||
if (state.closed) { | ||
return; | ||
} | ||
|
||
if (processing) { | ||
setTimeout(() => processCache(driver, cache), 25); | ||
return; | ||
} | ||
|
||
processing = true; | ||
|
||
const promises = Object.keys(cache).map(async collectionId => { | ||
const promise = upsertRecord(driver, collectionId, cache[collectionId]).catch((error) => { | ||
if (!state.closed) { | ||
throw error; | ||
} | ||
}); | ||
delete cache[collectionId]; | ||
return promise; | ||
}); | ||
|
||
await Promise.all(promises); | ||
|
||
processing = false; | ||
} | ||
|
||
const watch = debarrel( | ||
() => processCache(state.driver, cache), | ||
{ | ||
minimumFlushTime: 25, | ||
maximumFlushTime: 100 | ||
} | ||
); | ||
|
||
return watch((collectionId, change) => { | ||
const collectionMetadata = cache[collectionId] = cache[collectionId] || { | ||
documentCountAdd: 0 | ||
}; | ||
|
||
collectionMetadata.documentCountAdd = collectionMetadata.documentCountAdd + change.documentCountAdd; | ||
}); | ||
} | ||
|
||
module.exports = createCollectionMetadataUpdater; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const fs = require('fs'); | ||
|
||
const test = require('basictap'); | ||
const httpRequest = require('./helpers/httpRequest'); | ||
const createTestCluster = require('./helpers/createTestCluster'); | ||
|
||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
const tls = { | ||
key: fs.readFileSync('./certs/localhost.privkey.pem'), | ||
cert: fs.readFileSync('./certs/localhost.cert.pem'), | ||
ca: [fs.readFileSync('./certs/ca.cert.pem')], | ||
requestCert: true | ||
}; | ||
|
||
test('collectionMetadata - create a record', async t => { | ||
t.plan(5); | ||
|
||
const cluster = await createTestCluster(3, tls); | ||
|
||
const postRequest = await httpRequest(`${cluster.nodes[1].url}/tests`, { | ||
method: 'POST', | ||
data: { | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
} | ||
}); | ||
|
||
await sleep(100); | ||
|
||
const metadataRequest = await httpRequest(`${cluster.nodes[1].url}/system.collections`, { | ||
method: 'GET' | ||
}); | ||
|
||
t.equal(metadataRequest.data[0].documentCount, 1); | ||
|
||
const deleteRequest = await httpRequest(`${cluster.nodes[1].url}/tests/${postRequest.data.id}`, { | ||
method: 'DELETE' | ||
}); | ||
|
||
const getRequest = await httpRequest(`${cluster.nodes[2].url}/tests/${postRequest.data.id}`); | ||
|
||
cluster.closeAll(); | ||
|
||
t.deepEqual(getRequest.data, {}); | ||
|
||
t.equal(postRequest.status, 201); | ||
t.equal(deleteRequest.status, 200); | ||
t.equal(getRequest.status, 404); | ||
}); | ||
|
||
test('collectionMetadata - delete a record before debarrel', async t => { | ||
t.plan(5); | ||
|
||
const cluster = await createTestCluster(3, tls); | ||
|
||
const postRequest = await httpRequest(`${cluster.nodes[1].url}/tests`, { | ||
method: 'POST', | ||
data: { | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
} | ||
}); | ||
|
||
const deleteRequest = await httpRequest(`${cluster.nodes[1].url}/tests/${postRequest.data.id}`, { | ||
method: 'DELETE' | ||
}); | ||
|
||
await sleep(100); | ||
|
||
const metadataRequest = await httpRequest(`${cluster.nodes[1].url}/system.collections`, { | ||
method: 'GET' | ||
}); | ||
|
||
t.equal(metadataRequest.data[0].documentCount, 0); | ||
|
||
const getRequest = await httpRequest(`${cluster.nodes[2].url}/tests/${postRequest.data.id}`); | ||
|
||
cluster.closeAll(); | ||
|
||
t.deepEqual(getRequest.data, {}); | ||
|
||
t.equal(postRequest.status, 201); | ||
t.equal(deleteRequest.status, 200); | ||
t.equal(getRequest.status, 404); | ||
}); | ||
|
||
test('collectionMetadata - delete a record after tick', async t => { | ||
t.plan(4); | ||
|
||
const cluster = await createTestCluster(3, tls); | ||
|
||
const postRequest = await httpRequest(`${cluster.nodes[1].url}/tests`, { | ||
method: 'POST', | ||
data: { | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
} | ||
}); | ||
|
||
await sleep(300); | ||
|
||
const deleteRequest = await httpRequest(`${cluster.nodes[1].url}/tests/${postRequest.data.id}`, { | ||
method: 'DELETE' | ||
}); | ||
|
||
const getRequest = await httpRequest(`${cluster.nodes[2].url}/tests/${postRequest.data.id}`); | ||
|
||
cluster.closeAll(); | ||
|
||
t.deepEqual(getRequest.data, {}); | ||
|
||
t.equal(postRequest.status, 201); | ||
t.equal(deleteRequest.status, 200); | ||
t.equal(getRequest.status, 404); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters