This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.js
102 lines (74 loc) · 2.42 KB
/
populate.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var fs = require('fs');
var redis = require('redis');
// Code copied at the courtesy of http://blog.jaeckel.com/2010/03/i-tried-to-find-example-on-using-node.html
// Gets the job done. I'm not a big fan of the un-node-like API though.
var FileLineReader = function(filename, bufferSize) {
if(!bufferSize) {
bufferSize = 8192;
}
//private:
var currentPositionInFile = 0;
var buffer = "";
var fd = fs.openSync(filename, "r");
// return -1
// when EOF reached
// fills buffer with next 8192 or less bytes
var fillBuffer = function(position) {
var res = fs.readSync(fd, bufferSize, position, "ascii");
buffer += res[0];
if (res[1] == 0) {
return -1;
}
return position + res[1];
};
currentPositionInFile = fillBuffer(0);
//public:
this.hasNextLine = function() {
while (buffer.indexOf("\n") == -1) {
currentPositionInFile = fillBuffer(currentPositionInFile);
if (currentPositionInFile == -1) {
return false;
}
}
if (buffer.indexOf("\n") > -1) {
return true;
}
return false;
};
//public:
this.nextLine = function() {
var lineEnd = buffer.indexOf("\n");
var result = buffer.substring(0, lineEnd);
buffer = buffer.substring(result.length + 1, buffer.length);
return result;
};
return this;
};
var populate = function() {
var client = redis.createClient();
client.auth('');
client.on('ready', function() {
var reader = FileLineReader('data.txt', 1024);
var tiles = [];
while (reader.hasNextLine()) {
var line = reader.nextLine();
if (line.indexOf('/map') < 0)
continue;
var tile = line.split(' ');
var count = parseInt(tile[1]);
client.set(tile[0], count);
}
var reader2 = FileLineReader('maxd.txt', 1024);
while (reader2.hasNextLine()) {
var line = reader2.nextLine();
if (line.indexOf('zoomlevel') < 0)
continue;
var chunks = line.split(' ');
var count = chunks[1];
client.set(chunks[0], count, redis.print);
}
});
client.on('error', function(err) { console.error(err); });
}
if (!module.parent)
populate();