Skip to content

Instantly share code, notes, and snippets.

@yuanfeiz
Created May 7, 2017 14:30
Show Gist options
  • Save yuanfeiz/a7e8d840fd01e4023244076a3a023e0a to your computer and use it in GitHub Desktop.
Save yuanfeiz/a7e8d840fd01e4023244076a3a023e0a to your computer and use it in GitHub Desktop.
A script to load the ENS preimage names to redis
'use strict';
const config = require('./config');
const LineByLineReader = require('line-by-line');
const lr = new LineByLineReader(config.dictionary.path);
const Redis = require('redis');
let redis = Redis.createClient(config.redis.url);
let multi = redis.multi();
const sha3 = require('solidity-sha3').default;
const BUFFER_CAP = config.dictinary.bufferSize || 1000;
redis.on('connect', () => {
console.log('Redis is connected');
});
lr.on('error', function (err) {
console.error(err);
});
let bufferPtr = 0;
let batch = 0;
function flushBuffer(cb) {
multi.exec((err, replies) => {
let desc = `Batch[${batch}, ${bufferPtr - BUFFER_CAP}-${bufferPtr}]`;
if (err) {
console.error(desc, 'Err', err);
} else {
console.info(desc, 'Ok');
}
batch++;
if (cb) {
cb();
}
})
}
lr.on('line', function (domain) {
console.log(' ', domain)
if (++bufferPtr % BUFFER_CAP !== 0) {
let hash = sha3(domain);
multi.set(`hash:${hash}`, domain);
return;
}
lr.pause();
flushBuffer(() => {
lr.resume();
});
});
lr.on('end', function () {
flushBuffer(() => {
console.info('Complete! Total lines', bufferPtr);
process.exit(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment