Permalink
Browse files

Release v0.3.0b2

  • Loading branch information...
1 parent b2340a4 commit 1c6235dcc500c6a1c35562a87f4fbb96685c4ed5 @pgte committed Jan 22, 2011
View
@@ -9,7 +9,8 @@ var path = require('path'),
//crypto = require('crypto');
var default_options = {
- buffer_length: 16 * 1024
+ buffer_length: 16 * 1024,
+ read_only: false
};
// 0xDC80 os an invalid UTF8 char to appear as a record header (so we can search for a header on error case)
@@ -49,7 +50,8 @@ File.prototype.openFile = function(callback) {
var self = this;
this._acquireLock(function(err) {
if (err) { callback(err); return; }
- fs.open(self.file_path, 'a+', 0600, function(err, fd) {
+ var mode = self.options.read_only ? 'r' : 'a+';
+ fs.open(self.file_path, mode, 0600, function(err, fd) {
if (err) {
callback(err);
} else {
@@ -403,6 +405,9 @@ File.prototype._lockFilePath = function(callback) {
};
File.prototype._acquireLock = function(callback) {
+
+ if (this.options.read_only) { callback(null); return; }
+
var self = this,
lock_file_path = this._lockFilePath();
@@ -434,6 +439,9 @@ File.prototype._acquireLock = function(callback) {
};
File.prototype._releaseLock = function(callback) {
+
+ if (this.options.read_only) { callback(null); return; }
+
var self = this,
lock_file_path = this._lockFilePath();
@@ -1,6 +1,7 @@
var EventEmitter = require('events').EventEmitter,
assert = require('assert'),
- util = require('util');
+ util = require('util'),
+ File = require('../../files/file');
var LogStream = function(logger) {
var self = this;
@@ -24,18 +25,24 @@ module.exports.open = function(logger) {
LogStream.prototype._grabNextFile = function(callback) {
var self = this;
this.current_file_index ++;
- this.current_file = this.logger.livelog_files[this.current_file_index];
- this.current_file_pos = 0;
- if (!this.current_file) {
- this.logger.once('data', function() {
- self.current_file = self.logger.livelog_files[self.current_file_index]
- assert.ok(!!self.current_file, 'Couldn\'t get a file from the logger');
+ var file_path = this.logger.livelog_file_paths[this.current_file_index];
+ var openFilePath = function(file_path) {
+ File.open(file_path, {read_only: true}, function(err, file) {
+ if (err) { callback(err); return; }
+ self.current_file = file;
+ self.current_file_pos = 0;
callback(self.current_file)
+ })
+ };
+ if (!file_path) {
+ self.logger.once('data', function() {
+ file_path = this.logger.livelog_file_paths[this.current_file_index];
+ assert.ok(!!file_path, 'Couldn\'t get a file from the logger');
+ openFilePath(file_path);
});
} else {
- callback(this.current_file)
+ openFilePath(file_path);
}
-
}
LogStream.prototype._read = function() {
@@ -56,7 +63,10 @@ LogStream.prototype._read = function() {
// file reached end
// now we have to know if it has rolled into another file or should we keep trying to read this one
// check if a new file exists
- if (self.logger.livelog_files.length > (self.current_file_index + 1)) {
+ if (self.logger.livelog_file_paths.length > (self.current_file_index + 1)) {
+ self.current_file.end(function(err) {
+ if (err) { emit('error', err); return; }
+ });
self._grabNextFile(function() {
process.nextTick(function() {
self._read();
@@ -15,7 +15,7 @@ var Logger = function(database, options, callback) {
this.options.max_file_size = this.options.max_file_size_kb * 1024;
this.database = database;
this.current_log_pos = 0;
- this.livelog_files = [];
+ this.livelog_file_paths = [];
this._clearOldBacklog(function(err) {
if (err) { callback(err); return; }
self._createReplicationDir(function(err) {
@@ -92,7 +92,7 @@ Logger.prototype._loadBacklogAndStartLiveLog = function(callback) {
if (err) { callback(err); return; }
self.backlog_file = backlog_file;
- self.livelog_files.push(backlog_file);
+ self.livelog_file_paths.push(backLogPath);
self._openLiveLog(function(err) {
if (err) { callback(err); return; }
@@ -156,7 +156,7 @@ Logger.prototype._openLiveLog = function(callback) {
File.open(liveLogPath, {}, function(err, livelog_file) {
if (err) { callback(err); return; }
self.livelog_file = livelog_file;
- self.livelog_files.push(self.livelog_file);
+ self.livelog_file_paths.push(liveLogPath);
callback(null);
});
};
View
@@ -1,6 +1,6 @@
{ "name" : "alfred"
, "description" : "In-process key-value store"
-, "version" : "0.3.0b1"
+, "version" : "0.3.0b2"
, "homepage" : "http://pgte.github.com/alfred"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com> (http://metaduck.com)"
, "contributors" :
@@ -1,5 +1,6 @@
var assert = require('assert')
, fs = require('fs')
+ , path = require('path')
, net = require('net')
, child_process = require('child_process')
, util = require('util')
@@ -21,16 +22,18 @@ var USER_COUNT = 7;
module.exports.setup = function(next) {
(function removeFilesUnder(dir) {
- fs.readdirSync(dir).forEach(function(path) {
- var path = dir + '/' + path;
- var stat = fs.statSync(path);
- if (stat.isFile()) {
- fs.unlinkSync(path);
- } else {
- removeFilesUnder(path);
- fs.rmdirSync(path);
- }
- });
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
})(DB_PATH);
next();
};

0 comments on commit 1c6235d

Please sign in to comment.