Skip to content

Commit 36ef365

Browse files
committed
object delete (+cli)
1 parent 3e550b3 commit 36ef365

File tree

4 files changed

+140
-111
lines changed

4 files changed

+140
-111
lines changed

src/client/client_cli.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,56 @@ ClientCLI.prototype.download = function(key) {
171171

172172
/**
173173
*
174-
* DELETE
174+
* DEL
175175
*
176176
* delete object by key
177177
*
178178
*/
179-
ClientCLI.prototype.delete = function(key) {
179+
ClientCLI.prototype.del = function(key) {
180180
var self = this;
181181

182182
return Q.fcall(function() {
183-
// ...
183+
return self.client.object.delete_object({
184+
bucket: self.params.bucket,
185+
key: key
186+
});
184187
})
185188
.then(function() {
186-
// ...
189+
console.log('COMPLETED: del');
190+
}, function(err) {
191+
console.log('ERROR: del', err);
192+
});
193+
};
194+
195+
196+
/**
197+
*
198+
* SYS
199+
*
200+
* show system info
201+
*
202+
*/
203+
ClientCLI.prototype.sys = function() {
204+
var self = this;
205+
206+
return Q.fcall(function() {
207+
return self.client.system.read_system();
208+
})
209+
.then(function(res) {
210+
console.log('\n\nSystem info:', res.name);
211+
console.log('------------');
212+
console.log('\nroles:\n', res.roles);
213+
console.log('\ntiers:\n', res.tiers);
214+
console.log('\nstorage:\n', res.storage);
215+
console.log('\nnodes:\n', res.nodes);
216+
console.log('\nbuckets:\n', res.buckets);
217+
console.log('\nobjects:\n', res.objects);
218+
console.log('\n\n');
187219
})
188220
.then(function() {
189-
console.log('COMPLETED: delete');
221+
console.log('COMPLETED: sys');
190222
}, function(err) {
191-
console.log('ERROR: delete', err);
223+
console.log('ERROR: sys', err);
192224
});
193225
};
194226

@@ -226,7 +258,7 @@ ClientCLI.prototype.list = function(key) {
226258

227259
/**
228260
*
229-
* LIST
261+
* LIST_NODES
230262
*
231263
* list objects in bucket
232264
*

src/server/object_mapper.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
allocate_object_part: allocate_object_part,
1313
read_object_mappings: read_object_mappings,
1414
read_node_mappings: read_node_mappings,
15+
delete_object_mappings: delete_object_mappings,
1516
bad_block_in_part: bad_block_in_part,
1617
};
1718

@@ -202,6 +203,41 @@ function read_parts_mappings(parts, set_obj) {
202203

203204

204205

206+
/**
207+
*
208+
* delete_object_mappings
209+
*
210+
*/
211+
function delete_object_mappings(obj) {
212+
// find parts intersecting the [start,end) range
213+
return Q.when(db.ObjectPart
214+
.find({
215+
obj: obj.id,
216+
})
217+
.populate('chunks.chunk')
218+
.exec())
219+
.then(function(parts) {
220+
var chunks = _.pluck(_.flatten(_.map(parts, 'chunks')), 'chunk');
221+
var chunk_ids = _.pluck(chunks, 'id');
222+
var in_chunk_ids = {
223+
$in: chunk_ids
224+
};
225+
var deleted_update = {
226+
deleted: new Date()
227+
};
228+
return Q.all([
229+
db.DataChunk.update({
230+
_id: in_chunk_ids
231+
}, deleted_update).exec(),
232+
db.DataBlock.update({
233+
chunk: in_chunk_ids
234+
}, deleted_update).exec()
235+
]);
236+
});
237+
}
238+
239+
240+
205241
/**
206242
*
207243
* bad_block_in_part

src/server/object_server.js

Lines changed: 62 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ module.exports = new api.object_api.Server({
4343
*
4444
*/
4545
function create_multipart_upload(req) {
46-
return get_bucket_from_cache(req)
47-
.then(function(bucket) {
46+
return load_bucket(req)
47+
.then(function() {
4848
var info = {
4949
system: req.system.id,
50-
bucket: bucket.id,
50+
bucket: req.bucket.id,
5151
key: req.rest_params.key,
5252
size: req.rest_params.size,
5353
content_type: req.rest_params.content_type,
@@ -65,20 +65,17 @@ function create_multipart_upload(req) {
6565
*
6666
*/
6767
function complete_multipart_upload(req) {
68-
return get_bucket_from_cache(req)
69-
.then(function(bucket) {
70-
var info = {
71-
system: req.system.id,
72-
bucket: bucket.id,
73-
key: req.rest_params.key,
74-
};
75-
var updates = {
76-
$unset: {
77-
upload_mode: 1
78-
}
79-
};
80-
return db.ObjectMD.findOneAndUpdate(info, updates).exec();
81-
}).thenResolve();
68+
return find_object_md(req)
69+
.then(function(obj) {
70+
return db.ObjectMD
71+
.findByIdAndUpdate(obj.id, {
72+
$unset: {
73+
upload_mode: 1
74+
}
75+
})
76+
.exec();
77+
})
78+
.thenResolve();
8279
}
8380

8481

@@ -89,18 +86,7 @@ function complete_multipart_upload(req) {
8986
*
9087
*/
9188
function abort_multipart_upload(req) {
92-
return get_bucket_from_cache(req)
93-
.then(function(bucket) {
94-
var info = {
95-
system: req.system.id,
96-
bucket: bucket.id,
97-
key: req.rest_params.key,
98-
};
99-
var updates = {
100-
upload_mode: true
101-
};
102-
return db.ObjectMD.findOneAndUpdate(info, updates).exec();
103-
}).thenResolve();
89+
return delete_object(req);
10490
}
10591

10692

@@ -111,25 +97,14 @@ function abort_multipart_upload(req) {
11197
*
11298
*/
11399
function allocate_object_part(req) {
114-
var bucket;
115-
return get_bucket_from_cache(req)
116-
.then(function(bucket_arg) {
117-
bucket = bucket_arg;
118-
var info = {
119-
system: req.system.id,
120-
bucket: bucket.id,
121-
key: req.rest_params.key,
122-
};
123-
return db.ObjectMD.findOne(info).exec();
124-
})
125-
.then(db.check_not_deleted(req, 'object'))
100+
return find_object_md(req)
126101
.then(function(obj) {
127102
if (!obj.upload_mode) {
128103
// TODO handle the upload_mode state
129104
// throw new Error('object not in upload mode');
130105
}
131106
return object_mapper.allocate_object_part(
132-
bucket,
107+
req.bucket,
133108
obj,
134109
req.rest_params.start,
135110
req.rest_params.end,
@@ -140,21 +115,8 @@ function allocate_object_part(req) {
140115

141116

142117
function report_bad_block(req) {
143-
var bucket;
144-
var obj;
145-
return get_bucket_from_cache(req)
146-
.then(function(bucket_arg) {
147-
bucket = bucket_arg;
148-
var info = {
149-
system: req.system.id,
150-
bucket: bucket.id,
151-
key: req.rest_params.key,
152-
};
153-
return db.ObjectMD.findOne(info).exec();
154-
})
155-
.then(db.check_not_deleted(req, 'object'))
156-
.then(function(obj_arg) {
157-
obj = obj_arg;
118+
return find_object_md(req)
119+
.then(function(obj) {
158120
return object_mapper.bad_block_in_part(
159121
obj,
160122
req.rest_params.start,
@@ -181,16 +143,7 @@ function report_bad_block(req) {
181143
function read_object_mappings(req) {
182144
var obj;
183145

184-
return get_bucket_from_cache(req)
185-
.then(function(bucket) {
186-
var info = {
187-
system: req.system.id,
188-
bucket: bucket.id,
189-
key: req.rest_params.key,
190-
};
191-
return db.ObjectMD.findOne(info).exec();
192-
})
193-
.then(db.check_not_deleted(req, 'object'))
146+
return find_object_md(req)
194147
.then(function(obj_arg) {
195148
obj = obj_arg;
196149
return object_mapper.read_object_mappings(
@@ -214,16 +167,7 @@ function read_object_mappings(req) {
214167
*
215168
*/
216169
function read_object_md(req) {
217-
return get_bucket_from_cache(req)
218-
.then(function(bucket) {
219-
var info = {
220-
system: req.system.id,
221-
bucket: bucket.id,
222-
key: req.rest_params.key,
223-
};
224-
return db.ObjectMD.findOne(info).exec();
225-
})
226-
.then(db.check_not_deleted(req, 'object'))
170+
return find_object_md(req)
227171
.then(function(obj) {
228172
return get_object_info(obj);
229173
});
@@ -237,15 +181,10 @@ function read_object_md(req) {
237181
*
238182
*/
239183
function update_object_md(req) {
240-
return get_bucket_from_cache(req)
241-
.then(function(bucket) {
242-
var info = {
243-
system: req.system.id,
244-
bucket: bucket.id,
245-
key: req.rest_params.key,
246-
};
184+
return find_object_md(req)
185+
.then(function(obj) {
247186
var updates = _.pick(req.rest_params, 'content_type');
248-
return db.ObjectMD.findOneAndUpdate(info, updates).exec();
187+
return db.ObjectMD.findByIdAndUpdate(obj.id, updates).exec();
249188
})
250189
.then(db.check_not_deleted(req, 'object'))
251190
.thenResolve();
@@ -259,16 +198,21 @@ function update_object_md(req) {
259198
*
260199
*/
261200
function delete_object(req) {
262-
return get_bucket_from_cache(req)
263-
.then(function(bucket) {
264-
var info = {
265-
system: req.system.id,
266-
bucket: bucket.id,
267-
key: req.rest_params.key,
268-
};
269-
return db.ObjectMD.findOneAndRemove(info).exec();
201+
return load_bucket(req)
202+
.then(function() {
203+
var query = _.omit(object_md_query(req), 'deleted');
204+
return db.ObjectMD.findOne(query).exec();
205+
})
206+
.then(db.check_not_found(req, 'object'))
207+
.then(function(obj) {
208+
return db.ObjectMD.findByIdAndUpdate(obj.id, {
209+
deleted: new Date()
210+
}).exec();
270211
})
271212
.then(db.check_not_found(req, 'object'))
213+
.then(function(obj) {
214+
return object_mapper.delete_object_mappings(obj);
215+
})
272216
.thenResolve();
273217
}
274218

@@ -280,12 +224,9 @@ function delete_object(req) {
280224
*
281225
*/
282226
function list_objects(req) {
283-
return get_bucket_from_cache(req)
284-
.then(function(bucket) {
285-
var info = {
286-
system: req.system.id,
287-
bucket: bucket.id,
288-
};
227+
return load_bucket(req)
228+
.then(function() {
229+
var info = _.omit(object_md_query(req), 'key');
289230
if (req.rest_params.key) {
290231
info.key = new RegExp(req.rest_params.key);
291232
}
@@ -321,10 +262,30 @@ function get_object_info(md) {
321262
return info;
322263
}
323264

324-
function get_bucket_from_cache(req) {
265+
function load_bucket(req) {
325266
return db.BucketCache.get({
326267
system: req.system.id,
327268
name: req.rest_params.bucket,
328269
})
329-
.then(db.check_not_deleted(req, 'bucket'));
270+
.then(db.check_not_deleted(req, 'bucket'))
271+
.then(function(bucket) {
272+
req.bucket = bucket;
273+
});
274+
}
275+
276+
function object_md_query(req) {
277+
return {
278+
system: req.system.id,
279+
bucket: req.bucket.id,
280+
key: req.rest_params.key,
281+
deleted: null
282+
};
283+
}
284+
285+
function find_object_md(req) {
286+
return load_bucket(req)
287+
.then(function() {
288+
return db.ObjectMD.findOne(object_md_query(req)).exec();
289+
})
290+
.then(db.check_not_deleted(req, 'object'));
330291
}

0 commit comments

Comments
 (0)