Skip to content

Commit 0b9acb0

Browse files
committed
Code styling.
1 parent 20ac8cf commit 0b9acb0

File tree

10 files changed

+41
-43
lines changed

10 files changed

+41
-43
lines changed

listing-2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const inputFilePath = "./data/weather-stations.csv";
1616
function openDatabase () {
1717
return MongoClient.connect(hostName)
1818
.then(client => {
19-
var db = client.db(databaseName);
20-
var collection = db.collection(collectionName);
19+
const db = client.db(databaseName);
20+
const collection = db.collection(collectionName);
2121
return {
2222
collection: collection,
2323
close: () => {

listing-3.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const collectionName = 'daily_readings';
1212
function openDatabase () {
1313
return MongoClient.connect(hostName)
1414
.then(client => {
15-
var db = client.db(databaseName);
16-
var collection = db.collection(collectionName);
15+
const db = client.db(databaseName);
16+
const collection = db.collection(collectionName);
1717
return {
1818
collection: collection,
1919
close: () => {
@@ -23,7 +23,7 @@ function openDatabase () {
2323
});
2424
};
2525

26-
var numRecords = 0;
26+
let numRecords = 0;
2727

2828
//
2929
// Read the entire database, document by document using a database cursor.
@@ -48,7 +48,7 @@ function readDatabase (cursor) {
4848

4949
openDatabase()
5050
.then(db => {
51-
var databaseCursor = db.collection.find();
51+
const databaseCursor = db.collection.find();
5252
return readDatabase(databaseCursor) // NOTE: You could use a query here.
5353
.then(() => db.close()); // Close database when done.
5454
})

listing-4.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const collectionName = 'daily_readings';
1212
function openDatabase () {
1313
return MongoClient.connect(hostName)
1414
.then(client => {
15-
var db = client.db(databaseName);
16-
var collection = db.collection(collectionName);
15+
const db = client.db(databaseName);
16+
const collection = db.collection(collectionName);
1717
return {
1818
collection: collection,
1919
close: () => {
@@ -23,15 +23,15 @@ function openDatabase () {
2323
});
2424
};
2525

26-
var numRecords = 0;
27-
var numWindows = 0;
26+
let numRecords = 0;
27+
let numWindows = 0;
2828

2929
//
3030
// Read a single data window from the database.
3131
//
3232
function readWindow (collection, windowIndex, windowSize) {
33-
var skipAmount = windowIndex * windowSize;
34-
var limitAmount = windowSize;
33+
const skipAmount = windowIndex * windowSize;
34+
const limitAmount = windowSize;
3535
return collection.find()
3636
.skip(skipAmount)
3737
.limit(windowSize)
@@ -65,7 +65,7 @@ function readDatabase (collection, startWindowIndex, windowSize) {
6565

6666
openDatabase()
6767
.then(db => {
68-
var windowSize = 100;
68+
const windowSize = 100;
6969
return readDatabase(db.collection, 0, windowSize)
7070
.then(() => {
7171
return db.close(); // Close database when done.

listing-5.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const collectionName = 'daily_readings';
1212
function openDatabase () {
1313
return MongoClient.connect(hostName)
1414
.then(client => {
15-
var db = client.db(databaseName);
16-
var collection = db.collection(collectionName);
15+
const db = client.db(databaseName);
16+
const collection = db.collection(collectionName);
1717
return {
1818
collection: collection,
1919
close: () => {
@@ -25,7 +25,7 @@ function openDatabase () {
2525

2626
openDatabase()
2727
.then(db => {
28-
var query = { // Define our database query
28+
const query = { // Define our database query
2929
Year: {
3030
$gte: 2016, // Year >= 2016
3131
},

listing-6.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const collectionName = 'daily_readings';
1212
function openDatabase () {
1313
return MongoClient.connect(hostName)
1414
.then(client => {
15-
var db = client.db(databaseName);
16-
var collection = db.collection(collectionName);
15+
const db = client.db(databaseName);
16+
const collection = db.collection(collectionName);
1717
return {
1818
collection: collection,
1919
close: () => {
@@ -25,8 +25,8 @@ function openDatabase () {
2525

2626
openDatabase()
2727
.then(db => {
28-
var query = {}; // Retreive all records.
29-
var projection = { // This defines the fields to retreive from each record.
28+
const query = {}; // Retreive all records.
29+
const projection = { // This defines the fields to retreive from each record.
3030
fields: {
3131
_id: 0,
3232
Year: 1,

listing-7.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const collectionName = 'daily_readings';
1212
function openDatabase () {
1313
return MongoClient.connect(hostName)
1414
.then(client => {
15-
var db = client.db(databaseName);
16-
var collection = db.collection(collectionName);
15+
const db = client.db(databaseName);
16+
const collection = db.collection(collectionName);
1717
return {
1818
collection: collection,
1919
close: () => {

listing-8.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ if (argv.skip === undefined || argv.limit === undefined) {
1717
function openDatabase () {
1818
return MongoClient.connect(hostName)
1919
.then(client => {
20-
var db = client.db(databaseName);
21-
var collection = db.collection(collectionName);
20+
const db = client.db(databaseName);
21+
const collection = db.collection(collectionName);
2222
return {
2323
collection: collection,
2424
close: () => {

listing-9.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const collectionName = 'daily_readings';
1515
function openDatabase () {
1616
return MongoClient.connect(hostName)
1717
.then(client => {
18-
var db = client.db(databaseName);
19-
var collection = db.collection(collectionName);
18+
const db = client.db(databaseName);
19+
const collection = db.collection(collectionName);
2020
return {
2121
collection: collection,
2222
close: () => {
@@ -31,7 +31,7 @@ function openDatabase () {
3131
//
3232
function runSlave (skip, limit, slaveIndex) {
3333
return new Promise((resolve, reject) => {
34-
var args = [ 'listing-8.js', '--skip', skip, '--limit', limit ];
34+
const args = [ 'listing-8.js', '--skip', skip, '--limit', limit ];
3535

3636
const childProcess = spawn('node', args);
3737
childProcess.stdout.on('data', data => {
@@ -61,7 +61,7 @@ function runSlave (skip, limit, slaveIndex) {
6161
// Run the slave process for a particular batch of records.
6262
//
6363
function processBatch (batchIndex, batchSize) {
64-
var startIndex = batchIndex * batchSize;
64+
const startIndex = batchIndex * batchSize;
6565
return () => { // Encapsulate in an anon fn so that execution is deferred until later.
6666
return runSlave(startIndex, batchSize, batchIndex);
6767
};
@@ -74,11 +74,11 @@ function processBatch (batchIndex, batchSize) {
7474
//
7575
function processDatabase (numRecords) {
7676

77-
var batchSize = 100; // The number of records to process in each batchs.
78-
var maxProcesses = 2; // The number of process to run in parallel.
79-
var numBatches = numRecords / batchSize; // Total number of batches that we need to process.
80-
var slaveProcesses = [];
81-
for (var batchIndex = 0; batchIndex < numBatches; ++batchIndex) {
77+
const batchSize = 100; // The number of records to process in each batchs.
78+
const maxProcesses = 2; // The number of process to run in parallel.
79+
const numBatches = numRecords / batchSize; // Total number of batches that we need to process.
80+
const slaveProcesses = [];
81+
for (let batchIndex = 0; batchIndex < numBatches; ++batchIndex) {
8282
slaveProcesses.push(processBatch(batchIndex, batchSize));
8383
}
8484

toolkit/open-csv-input-stream.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ function openCsvInputStream (inputFilePath) {
2929
},
3030

3131
error: (err) => { // An error has occurred.
32-
console.error("error in stream!"); //fio:
33-
console.error(err); //fio:
3432
csvInputStream.emit('error', err); // Pass on errors.
3533
}
3634
});

vm-with-sample-db/db-init.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
var papa = require('papaparse');
4-
var fs = require('fs');
3+
const papa = require('papaparse');
4+
const fs = require('fs');
55

66
//
77
// Read a text file form the file system.
88
//
9-
var read = function (fileName) {
9+
function read (fileName) {
1010
return new Promise((resolve, reject) => {
1111
fs.readFile(fileName, 'utf8',
1212
function (err, textFileData) {
@@ -24,28 +24,28 @@ var read = function (fileName) {
2424
//
2525
// Helper function to import a CSV file.
2626
//
27-
var importCsvFile = function (filePath) {
27+
const importCsvFile (filePath) {
2828
return read(filePath)
2929
.then(textFileData => {
30-
var result = papa.parse(textFileData, {
30+
const result = papa.parse(textFileData, {
3131
header: true,
3232
dynamicTyping: true,
3333
});
3434
return result.data;
3535
});
3636
};
3737

38-
var exportToMongoDB = function (db, collectionName, data) {
38+
const exportToMongoDB (db, collectionName, data) {
3939
return data.reduce((prevPromise, row) => {
4040
return prevPromise.then(() => {
4141
return db[collectionName].insert(row);
4242
});
4343
}, Promise.resolve());
4444
};
4545

46-
var mongo = require('promised-mongo');
46+
const mongo = require('promised-mongo');
4747

48-
var db = mongo('localhost:27017/weather_stations', ['daily_readings']);
48+
const db = mongo('localhost:27017/weather_stations', ['daily_readings']);
4949

5050
importCsvFile('/code/data/weather-stations.csv')
5151
.then(data => exportToMongoDB(db, 'daily_readings', data))

0 commit comments

Comments
 (0)