read json lines field values

This commit is contained in:
Jan Prochazka
2022-03-31 15:35:38 +02:00
parent 5aac142e4c
commit c0b365602b
5 changed files with 52 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
const { filterName } = require('dbgate-tools');
const fs = require('fs');
const lineReader = require('line-reader');
const _ = require('lodash');
@@ -148,6 +149,19 @@ module.exports = {
return {};
},
loadFieldValues_meta: true,
async loadFieldValues({ jslid, field, search }) {
const datastore = await this.ensureDatastore(jslid);
const res = new Set();
await datastore.enumRows(row => {
if (!filterName(search, row[field])) return true;
res.add(row[field]);
return res.size < 100;
});
// @ts-ignore
return [...res].map(value => ({ value }));
},
async notifyChangedStats(stats) {
// console.log('SENDING STATS', JSON.stringify(stats));
const datastore = this.datastores[stats.jslid];

View File

@@ -159,6 +159,18 @@ class JsonLinesDatastore {
}
}
async enumRows(eachRow) {
await lock.acquire('reader', async () => {
await this._ensureReader(0, null);
for (;;) {
const line = await this._readLine(true);
if (line == null) break;
const shouldContinue = eachRow(line);
if (!shouldContinue) break;
}
});
}
async getRows(offset, limit, filter) {
const res = [];
await lock.acquire('reader', async () => {