-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
82 lines (69 loc) · 2.31 KB
/
init.js
File metadata and controls
82 lines (69 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const contactPoints = process.env.CASSANDRA ? process.env.CASSANDRA.split(',') : ['192.168.99.100'];
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({contactPoints: contactPoints});
const fs = require('fs');
const portscanner = require('portscanner');
/**
* CSV file of the Wordlist
* @type {string}
*/
const csvFile = "./server/model/words.csv";
/* STATEMENTS */
let queries = [
"CREATE KEYSPACE IF NOT EXISTS gameoflife WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 } AND DURABLE_WRITES = false",
"CREATE TABLE IF NOT EXISTS gameoflife.coordinates (x int, y int, color varchar, time timestamp, PRIMARY KEY (x, y))",
"CREATE TABLE IF NOT EXISTS gameoflife.words(wordno int, word varchar, isactive boolean, PRIMARY KEY(wordno));",
"CREATE TABLE IF NOT EXISTS gameoflife.activeWord(sessionno int , time timestamp, word varchar, PRIMARY KEY(sessionno, time))WITH CLUSTERING ORDER BY (time DESC);"
];
function processArray(array, fn) {
var index = 0;
function next() {
if (index < array.length) {
fn(array[index++]).then(next);
} else {
process.exit();
}
}
next();
}
/**
* Initialization of the wordlist
*/
function initWords(callback) {
fs.readFile(csvFile, "utf8", (err, data) => {
let words = data.split(';');
exports.wordCount = words.length;
shuffle(words);
for (var i = 0; i < words.length - 1; i++) {
let query = "INSERT INTO gameoflife.words (wordno,word,isactive) values (" + i + ",'" + words[i] + "' ,false)";
queries.push(query)
}
let query = "UPDATE gameoflife.words SET isactive = true WHERE wordno = 0";
queries.push(query);
if (callback) callback()
});
}
/**
* Shuffels an Array
* @param a array
*/
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
function executeQueries() {
initWords(processArray(queries, query => client.execute(query).then(result => console.log(result)).catch(err => console.log(err))));
}
function checkCassandra() {
portscanner.checkPortStatus(9042, contactPoints[0], function(err, status) {
if(status === 'open') {
executeQueries();
} else {
console.log("Retrying!");
setTimeout(checkCassandra, 2000);
}
});
}
checkCassandra();