-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.cpp
142 lines (127 loc) · 3.66 KB
/
storage.cpp
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
#include <iostream>
#include <mutex>
#include <cassert>
using namespace std;
#include "storage.h"
//#include "goods.h"
//Storage::Storage(void)
//{
// this->map = new unordered_map;
//}
Storage::~Storage()
{
//this->manufacturer_index.erase(this->manufacturer_index.begin(), manufacturer_index.end());
//this->vendor_index.erase(vendor_index.begin(), vendor_index.end());
for (auto iter : this->manufacturer_index)
iter.second.~shared_ptr();
for (auto iter : this->vendor_index)
iter.second.~shared_ptr();
this->manufacturer_index.clear();
this->vendor_index.clear();
cout << "Storage removed" << endl;
}
bool Storage::insert_item(Item &item)
{
//this->my_mutex.lock();
unique_lock lock(this->my_mutex);
/* Should we? */
//item.value->my_mutex.lock();
string vc = item.get_vendor_code();
auto res = this->vendor_index.emplace(vc, item.value);
//auto res = this->vendor_index.insert(vc, item.value);
if (res.second)
this->manufacturer_index.emplace(item.get_manufacturer(), item.value);
//this->my_mutex.unlock();
return res.second;
}
bool Storage::insert_item(string vendor_code, string manufacturer, string name, unsigned int price)
{
Item item = Item(vendor_code, manufacturer, name, price);
//Item item = Item(vendor_code.c_str(), manufacturer.c_str(), name.c_str(), price);
//this->my_mutex.lock();
unique_lock lock(my_mutex);
//auto res = this->map.emplace(vendor_code, Item(vendor_code, manufacturer, name, price));
auto res = this->vendor_index.emplace(vendor_code, item.value);
if (res.second)
this->manufacturer_index.emplace(item.get_manufacturer(), item.value);
//this->my_mutex.unlock();
return res.second;
//return false;
}
bool Storage::remove_item(Item &item)
{
//item.value->my_mutex.lock();
//unique_lock lock(item.value->my_mutex);
//this->my_mutex.lock();
unique_lock lock(my_mutex);
string vc = item.get_vendor_code();
//auto res = this->vendor_index.erase(vc);
auto res = this->vendor_index.find(item.get_vendor_code());
if (res != this->vendor_index.end())
{
auto _pair = this->manufacturer_index.equal_range(item.get_manufacturer());
for (; _pair.first != _pair.second; ++_pair.first)
{
if (_pair.first->second == item.value)
{
_pair.first->second.~shared_ptr();
this->manufacturer_index.erase(_pair.first);
break;
}
}
this->vendor_index.erase(res->first);
res->second.~shared_ptr();
return true;
}
return false;
//auto res_v = this->vendor_index.erase(item.get_vendor_code());
//if (res_v > 0)
//{
// if (res_v != res_m)
// {
// string error_msg = string("Removed from vendor_index: ") +
// to_string(res_v) + ", " +
// "removed from manufacturer_index: " +
// to_string(res_m) + ".";
// throw error_msg;
// }
//}
//return res_v > 0;
}
bool Storage::remove_item(string &vendor_code)
{
//unique_lock lock(my_mutex);
//auto res = this->vendor_index.erase(vendor_code);
//return res > 0;
Item *item = get_item(vendor_code);
if (item)
return remove_item(*item);
else
return false;
}
Item *Storage::get_item(string &vendor_code)
{
//this->my_mutex.lock_shared();
shared_lock lock(my_mutex);
auto res = this->vendor_index.find(vendor_code);
//this->my_mutex.unlock();
if (res != this->vendor_index.end())
{
Item *item = new Item(res->second);
return item;
}
else
return NULL;
}
/* Since C++11 it is safe to return vector, constructed inside function */
vector<Item> Storage::get_items_by_manufacturer(string manufacturer)
{
shared_lock lock(my_mutex);
vector<Item> _vector;
auto _pair = this->manufacturer_index.equal_range(manufacturer);
for (; _pair.first != _pair.second; ++_pair.first)
{
_vector.emplace_back(Item(_pair.first->second));
}
return _vector;
}