return ret;
}
-int CrushWrapper::insert_device(int item, int weight, string name,
+int CrushWrapper::insert_item(int item, int weight, string name,
map<string,string>& loc) // typename -> bucketname
{
- cout << "insert_device item " << item << " weight " << weight
+ cout << "insert_item item " << item << " weight " << weight
<< " name " << name << " loc " << loc << std::endl;
if (name_exists(name.c_str())) {
- cerr << "error: device name '" << name << "' already exists" << std::endl;
- return -EEXIST;
- }
- if (item_exists(item)) {
- cerr << "error: device id '" << item << "' already exists" << std::endl;
+ cerr << "error: device name '" << name << "' already exists as id "
+ << get_item_id(name.c_str()) << std::endl;
return -EEXIST;
}
int id = get_item_id(loc[p->second].c_str());
if (!id) {
// create the bucket
- cout << "insert_device creating bucket " << loc[p->second] << std::endl;
+ cout << "insert_item creating bucket " << loc[p->second] << std::endl;
int empty = 0;
id = add_bucket(0, CRUSH_BUCKET_STRAW, CRUSH_HASH_DEFAULT, p->first, 1, &item, &empty);
set_item_name(id, loc[p->second].c_str());
// add to an existing bucket
if (!bucket_exists(id)) {
- cout << "insert_device don't have bucket " << id << std::endl;
+ cout << "insert_item don't have bucket " << id << std::endl;
return -EINVAL;
}
-
- cout << "insert_device adding " << item << " weight " << weight
- << " to bucket " << id << std::endl;
+
crush_bucket *b = get_bucket(id);
assert(b);
+
+ // make sure the item doesn't already exist in this bucket
+ for (unsigned j=0; j<b->size; j++)
+ if (b->items[j] == item) {
+ cerr << "insert_item " << item << " already exists in bucket " << b->id << std::endl;
+ return -EEXIST;
+ }
+
+ cout << "insert_item adding " << item << " weight " << weight
+ << " to bucket " << id << std::endl;
crush_bucket_add_item(b, item, 0);
adjust_item_weight(item, weight);
return 0;
void find_roots(set<int>& roots) const;
- int insert_device(int id, int weight, string name, map<string,string>& loc);
+ int insert_item(int id, int weight, string name, map<string,string>& loc);
int remove_item(int id);
int adjust_item_weight(int id, int weight);
int adjust_item_weightf(int id, float weight) {
if (add_item >= 0) {
cout << me << " adding item " << add_item << " weight " << add_weight
<< " at " << add_loc << std::endl;
- int r = crush.insert_device(add_item, (int)(add_weight * (float)0x10000), add_name, add_loc);
+ int r = crush.insert_item(add_item, (int)(add_weight * (float)0x10000), add_name, add_loc);
if (r == 0)
modified = true;
else {