Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkafei committed Jul 25, 2023
1 parent 054d0db commit d21f4c6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ The `remove` operation has the following general format:
}
}
```
where all objects satisfy the `constraints` will be removed from the database. The format of constraints is the same as that in [query](#query) operation. Note that all `remove` operations will be cached and will not take effect immediately. To make `remove` operations effective, you need to call the [build](#build) operation.
where all objects satisfy the `constraints` will be removed from the database. The `count` field in the returned json indicates the number of objects that have been removed. The format of constraints is the same as that in [query](#query) operation. Note that all `remove` operations will be cached and will not take effect immediately. To make `remove` operations effective, you need to call the [build](#build) operation.

### Build
The `build` operation has the following format:
Expand Down
2 changes: 1 addition & 1 deletion src/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ std::vector<std::pair<int64_t, int64_t>> bool_index::query(const std::string &ra
vec_ptr = &data[1];
}
else {
throw std::runtime_error("Invalid query: " + range);
throw std::runtime_error(std::format("Invalid query: \"{}\"", range));
}
ret.reserve(vec_ptr->size());
for (auto id : *vec_ptr) {
Expand Down
39 changes: 30 additions & 9 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,30 @@ json jsonify(const auto& object) {
}
return j;
}
auto get_constraints(const json &data) {
std::vector<std::pair<std::string, std::vector<std::string>>> ret;
const auto &items = data.items();
for (auto iter = items.begin(); iter != items.end(); ++iter) {
const auto &item = *iter;
if (item.value().is_array()) {
ret.emplace_back(item.key(), item.value().template get<std::vector<std::string>>());
}
else if (item.value().is_string()) {
ret.emplace_back(item.key(), std::vector<std::string>(1, item.value().template get<std::string>()));
}
else {
throw std::runtime_error(std::format("The constraint type of \"{}\" must be string or array of strings", item.key()));
}
}
return ret;
}
auto filter(const json &data) {
std::optional<std::pair<int64_t, int64_t>> correlation_range;
std::vector<std::pair<int64_t, int64_t>> answer;
std::vector<std::pair<std::string, std::vector<std::string>>> constraints;
bool first = true;
const auto &items = data.items();
if (items.begin() == items.end()) { // No constraints
return std::make_pair(constraints, query());
return query();
}
for (auto iter = items.begin(); iter != items.end(); ++iter) {
const auto &item = *iter;
Expand Down Expand Up @@ -115,7 +131,6 @@ auto filter(const json &data) {
}
answer = std::move(tmp);
}
constraints.emplace_back(item.key(), std::move(ranges));
}
if (correlation_range) {
auto [L, R] = *correlation_range;
Expand All @@ -127,11 +142,14 @@ auto filter(const json &data) {
std::sort(answer.begin(), answer.end(), [](auto x, auto y) {
return x.second > y.second; // Sort in descending order of $correlation.
});
return std::make_pair(constraints, answer);
return answer;
}
std::string response(json command) {
std::string ret;
std::string ret = "{}";
auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();
if (!command.is_object()) {
throw std::runtime_error("You should pass a json object to CoffeeDB");
}
std::string operation = command.at("operation");
command.erase(command.find("operation"));
backup(timestamp, command);
Expand Down Expand Up @@ -163,7 +181,8 @@ std::string response(json command) {
std::vector<std::pair<std::string, std::vector<std::string>>> constraints;
std::vector<std::pair<int64_t, int64_t>> result;
if (command.contains("constraints")) {
std::tie(constraints, result) = filter(command.at("constraints"));
result = filter(command.at("constraints"));
constraints = get_constraints(command.at("constraints"));
command.erase(command.find("constraints"));
}
else {
Expand Down Expand Up @@ -231,18 +250,20 @@ std::string response(json command) {
throw std::runtime_error("For security, the remove operation must have a \"constraints\" field");
}
auto constraints = command.at("constraints");
auto [_, result] = filter(constraints);
auto result = filter(constraints);
remove(result);
command.erase(command.find("constraints"));
json j;
j["count"] = result.size();
ret = j.dump();
}
else if (operation == "build") {
build();
}
else if (operation == "count") {
std::vector<std::pair<std::string, std::vector<std::string>>> constraints;
std::vector<std::pair<int64_t, int64_t>> result;
if (command.contains("constraints")) {
std::tie(constraints, result) = filter(command.at("constraints"));
auto result = filter(command.at("constraints"));
command.erase(command.find("constraints"));
}
else {
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ void test() {
send(R"({
"operation": "query",
"constraints": {
"bool": "true"
"bool": true
}
})"_json);
send(R"({
"operation": "count"
})"_json);
send(R"( "123123" )"_json);
}
int main(int argc, char *argv[]) {
// curl http://127.0.0.1:14920/coffeedb -X POST -d '{"operation":"clear"}'
Expand Down
3 changes: 1 addition & 2 deletions test/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def send(data):
now = time()
result = requests.post(url, data)
total += time() - now
if result.text:
return json.loads(result.text)
return json.loads(result.text)
send({
"operation": "clear"
})
Expand Down
3 changes: 1 addition & 2 deletions test/test-highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def send(data):
global total
data = json.dumps(data, indent=4)
result = requests.post(url, data)
if result.text:
return json.loads(result.text)
return json.loads(result.text)
send({
"operation": "clear"
})
Expand Down
3 changes: 1 addition & 2 deletions test/test-string.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def send(data):
now = time()
result = requests.post(url, data)
total += time() - now
if result.text:
return json.loads(result.text)
return json.loads(result.text)
def count(str, sub):
ret = 0
for i in range(0, len(str) - len(sub) + 1):
Expand Down

0 comments on commit d21f4c6

Please sign in to comment.