forked from bigeasy/strata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (131 loc) · 4.16 KB
/
script.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var cadence = require('cadence')
var path = require('path')
var fs = require('fs')
var Queue = require('./queue')
function Script (logger) {
this._logger = logger
this._journal = []
this._operations = []
}
Script.prototype.rotate = function (page) {
this._operations.push({ name: '_rotate', page: page })
}
Script.prototype.unlink = function (page) {
var rotations = []
for (var i = 0; i <= page.rotation; i++) {
rotations.push(path.join(this._logger._directory, 'pages', page.address + '.' + i))
}
this._journal.push({
name: '_purge', rotations: rotations
})
}
Script.prototype.rewriteLeaf = function (page) {
this._operations.push({
name: '_rewriteLeaf', page: page
})
}
Script.prototype.writeBranch = function (page) {
this._operations.push({
name: '_writeBranch', page: page
})
}
Script.prototype._rotate = function (operation, callback) {
var page = operation.page, queue = operation.queue, entry
page.rotation++
var from = this._logger.filename(page, true)
var to = this._logger.filename(page)
this._journal.push({
name: '_replace', from: from, to: to
})
this._logger.rotate(page, from, callback)
}
Script.prototype._writeBranch = cadence(function (async, operation) {
var page = operation.page
var file = this._logger.filename(page, true)
this._journal.push({
name: '_replace', from: file, to: this._logger.filename(page)
})
this._logger.writeBranch(page, file, async())
})
Script.prototype._rewriteLeaf = cadence(function (async, operation) {
var page = operation.page
this.unlink(page)
page.rotation = 0
var file = this._logger.filename(page, true)
this._journal.push({
name: '_replace', from: file, to: this._logger.filename(page)
})
this._logger.rewriteLeaf(page, file, async())
})
Script.prototype.commit = cadence(function (async) {
async(function () {
async.forEach(function (operation) {
this[operation.name](operation, async())
})(this._operations)
}, function () {
this._journal.push({ name: '_complete' })
var pending = path.join(this._logger._directory, 'journal.pending')
var comitted = path.join(this._logger._directory, 'journal')
async(function () {
var script = this._journal.map(function (operation) {
return JSON.stringify(operation)
}).join('\n') + '\n'
fs.writeFile(pending, script, 'utf8', async())
}, function () {
fs.rename(pending, comitted, async())
}, function () {
this.play(async())
})
})
})
Script.prototype.play = cadence(function (async, page) {
async.forEach(function (operation) {
this[operation.name](operation, async())
})(this._journal)
})
Script.prototype._replace = cadence(function (async, operation) {
async(function () {
var block = async([function () {
fs.stat(operation.from, async())
}, function (error) {
if (error.code !== 'ENOENT') {
throw error
}
return [ block.break ]
}], [function () {
fs.unlink(operation.to, async())
}, function (error) {
if (error.code !== 'ENOENT') {
throw error
}
}], function () {
fs.rename(operation.from, operation.to, async())
}, function () {
return [ block.break ]
})()
}, function () {
fs.stat(operation.to, async())
})
})
Script.prototype._complete = cadence(function (async, operation) {
var journal = path.join(this._logger._directory, 'journal')
async([function () {
fs.unlink(journal, async())
}, function (error) {
if (error.code !== 'ENOENT') {
throw error
}
}])
})
Script.prototype._purge = cadence(function (async, operation) {
async.forEach(function (file) {
async([function () {
fs.unlink(file, async())
}, function (error) {
if (error.code !== 'ENOENT') {
throw error
}
}])
})(operation.rotations)
})
module.exports = Script