-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
164 lines (134 loc) · 5.23 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var util = require('util');
var utils = require('./utils');
var url = require('url');
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN;
var RiakIterator = require('./iterator');
var riakpbc = require('riakpbc');
var async = require('async');
function toKey(key) {
return typeof key === 'string' ? key : JSON.stringify(key);
}
function RiakDOWN(location) {
if (!(this instanceof RiakDOWN)) return new RiakDOWN(location);
AbstractLevelDOWN.call(this, location);
var parsed = url.parse(location);
this._client = riakpbc.createClient({ host: parsed.hostname, port: parsed.port, parse_values: false });
this._bucket = parsed.path.split('/')[1];
}
util.inherits(RiakDOWN, AbstractLevelDOWN);
RiakDOWN.destroy = function (location, callback) {
var parsed = url.parse(location);
var client = riakpbc.createClient({ host: parsed.hostname, port: parsed.port, parse_values: false });
var bucket = parsed.path.split('/')[1];
client.getKeys({ bucket: bucket }, function (err, res) {
async.each(res.keys ? res.keys : [], function (key, cb) {
client.del({ bucket: bucket, key: key }, cb);
}, callback);
});
};
RiakDOWN.prototype._open = function (options, callback) {
this._client.connect(callback);
};
RiakDOWN.prototype._close = function (callback) {
this._client.disconnect();
process.nextTick(function () {
callback();
});
};
RiakDOWN.prototype._put = function (key, value, options, callback) {
var bucket = options.bucket || this._bucket;
if (options.type === 'counter') {
this._client.updateCounter({
bucket: bucket,
key: toKey(key),
amount: value
}, callback);
} else {
var indexes = [].concat(options.indexes || []).map(function (index) {
return {
key: /_(bin|int)$/.test(index.key) ? index.key : index.key + '_bin',
value: index.value
};
});
indexes.forEach(function (index) {
indexes.push({
key: '_reverse_' + index.key,
value: /_int$/.test(index.key) ? -1 * index.value : utils.reverseString(index.value)
});
});
indexes.push({
key: '_reverse_key_bin',
value: utils.reverseString(toKey(key))
});
this._client.put({
bucket: bucket,
key: toKey(key),
content: {
value: value,
content_type: options.content_type || 'application/octet-stream',
indexes: indexes
},
return_head: true,
vclock: options.vclock
}, callback);
}
};
RiakDOWN.prototype._get = function (key, options, callback) {
var self = this;
var bucket = options.bucket || this._bucket;
var newoptions;
if (options.type === 'counter') {
this._client.getCounter({
bucket: bucket,
key: toKey(key)
}, function (err, reply) {
if (err || !reply || !reply.hasOwnProperty('value')) {
return callback(new Error('NotFound'));
}
callback(null, options.asBuffer === false ? reply.value.toString(10) : new Buffer(reply.value.toString(10)));
});
} else {
this._client.get({ bucket: bucket, key: toKey(key) }, function (err, reply) {
if (err || (!reply || !reply.content || !reply.content.length)) {
return callback(new Error('NotFound'));
}
if (reply.content.length === 1) {
return callback(null, options.asBuffer === false ? reply.content[0].value.toString() : reply.content[0].value, { vclock: reply.vclock });
}
newoptions = JSON.parse(JSON.stringify(options)); // ugly clone so we don't modify the original options
newoptions.bucket = bucket;
RiakDOWN._siblingResolver(key, reply.content, newoptions, function (err, resolved) {
if (err) {
return callback(err);
}
if (typeof resolved !== 'string' && !Buffer.isBuffer(resolved)) {
resolved = JSON.stringify(resolved);
}
self._put(key, resolved, util._extend(newoptions, { bucket: bucket, vclock: reply.vclock }), function (err) {
self._get(key, options, callback);
});
});
});
}
};
RiakDOWN.prototype._del = function (key, options, callback) {
var bucket = options.bucket || this._bucket;
this._client.del({ bucket: bucket, key: toKey(key), vclock: options.vclock }, callback);
};
RiakDOWN.prototype._batch = function (array, options, callback) {
var self = this;
async.eachSeries(array, function (item, cb) {
if (item.type === 'put') {
self._put(toKey(item.key), item.value, options, cb);
} else if (item.type === 'del') {
self._del(toKey(item.key), options, cb);
}
}, callback);
};
RiakDOWN.prototype._iterator = function (options) {
return new RiakIterator(this, options);
};
RiakDOWN._siblingResolver = function (key, siblings, options, callback) {
callback(null, siblings[0].value);
};
module.exports = RiakDOWN;