-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.cpp
62 lines (53 loc) · 1.48 KB
/
operation.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
#include "operation.hpp"
using namespace templatedb;
Operation::Operation(std::string op_string, int _key, std::vector<int> & _args)
{
if (op_string == "I")
type = PUT;
else if (op_string == "Q")
type = GET;
else if (op_string == "S")
type = SCAN;
else if (op_string == "D")
type = DELETE;
else
type = NO_OP;
key = _key;
args = _args;
}
Operation::Operation(op_code _type, int _key, std::vector<int> & _args)
{
type = _type;
key = _key;
args = _args;
}
std::vector<Operation> Operation::ops_from_file(std::string file_name)
{
std::ifstream fid(file_name);
std::vector<Operation> ops;
std::vector<int> args;
if (fid.is_open())
{
std::string line, item, op_string, key;
std::getline(fid, line); // First line is number of ops
int num_ops = stoi(line);
ops.reserve(num_ops);
while (std::getline(fid, line))
{
args = std::vector<int>();
std::stringstream linestream(line);
std::getline(linestream, op_string, ' '); // First line is an op_code
std::getline(linestream, key, ' '); // First argument is a key
while(std::getline(linestream, item, ' '))
{
args.push_back(stoi(item));
}
ops.push_back(Operation(op_string, stoi(key), args));
}
}
else
{
printf("Unable to read %s\n", file_name.c_str());
}
return ops;
}