De-duplcation I - Before
by Jeff Cave
HTML
<html>
<script src="//cdn.jsdelivr.net/pouchdb/latest/pouchdb.min.js"></script>
<script type="module" src="./index.js"></script>
<body>
<table>
<thead>
<tr>
<td>User</td>
<td>Files</td>
<td>Size</td>
<td>Min</td>
<td>Avg</td>
<td>Max</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<pre id="display">Log ...<hr /></pre>
</body>
</html>
CSS
pre {
border: 1px solid black;
background-color: lightgray;
padding: 1em;
}
td,
tr {
padding: 1em;
padding-top: 0.1em;
}
JavaScript
let db = new PouchDB('filestore');
async function CreateIndex() {
await db.put({
_id: '_design/allfiles',
views: {
'allfiles': {
map: function(doc) {
// eslint-disable-next-line no-undef
emit([doc.user, doc.path], doc.size);
}.toString(),
reduce: '_stats'
}
}
});
}
async function save(user, filename, blob) {
//let hash = crypto.createHash('sha256').update(blob).digest('base64');
db.put({
_id: [user, filename].join('@'),
user: user,
path: filename,
size: blob.length,
_attachments: {
'0': {
content_type: 'text/plain',
data: window.btoa(blob)
}
}
});
}
async function dload(user, filename) {
let recs = await db.query('allfiles', {
reduce: false,
include_docs: true,
attachments: true,
key: [user, filename]
});
let blob = recs.rows[0].doc._attachments['0'].data;
blob = window.atob(blob);
return blob;
}
function log(msg) {
let display = document.querySelector('#display');
if (typeof msg !== "string") {
msg = JSON.stringify(msg);
}
display.innerHTML += msg + '\n';
};
function report(data) {
let html = data.rows.map((d) => {
return '' +
'<tr><td>' + [
d.key,
d.value.count,
d.value.sum,
d.value.min,
Math.floor(d.value.sum / d.value.count),
d.value.max
].join('</td><td>') +
'</td></tr>';;
}).join('');
document.querySelector('tbody').innerHTML = html;
}
async function main() {
await db.destroy();
db = new PouchDB('filestore');
CreateIndex();
await save('alice', 'gilbert.txt', 'I am the very model of a modern major general');
await save('bob', 'sullivan.txt', 'I am the very model of a modern major general');
await save('carol', 'gilbert.txt', 'I have knowledge of things animal, vegetible and mineral');
await save('alice', 'sullivan.txt', 'I have knowledge of things animal, vegetible and mineral');
let data =...