Skip to content

Commit 74f6c3f

Browse files
committed
get oeis files from API server
1 parent fa3bf91 commit 74f6c3f

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ To install or update LODA, please follow the [installation instructions](https:/
66

77
* Extend timeout for folder locking on Windows
88

9+
### Features
10+
11+
* Retrieve OEIS files from cache on API server
12+
913
## v22.1.9
1014

1115
### Bugfixes

src/api_client.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ void ApiClient::postCPUHour() {
6363
}
6464
}
6565

66+
bool ApiClient::getOeisFile(const std::string& filename,
67+
const std::string& local_path) {
68+
const std::string url = BASE_URL + "oeis/" + filename + ".gz";
69+
const bool success = WebClient::get(url, local_path + ".gz", false, false);
70+
if (success) {
71+
gunzip(local_path + ".gz");
72+
}
73+
return success;
74+
}
75+
6676
bool ApiClient::getProgram(int64_t index, const std::string& path) {
6777
std::remove(path.c_str());
6878
return WebClient::get(BASE_URL + "programs/" + std::to_string(index), path,
@@ -133,7 +143,6 @@ void ApiClient::updateSession() {
133143
}
134144

135145
int64_t ApiClient::fetchInt(const std::string& endpoint) {
136-
// TODO: store in tmp folder
137146
const std::string tmp = "tmp_int.txt";
138147
WebClient::get(BASE_URL + endpoint, tmp, true, true);
139148
std::ifstream in(tmp);

src/file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ void gunzip(const std::string &path) {
102102
}
103103
// https://stackoverflow.com/questions/9964865/c-system-not-working-when-there-are-spaces-in-two-different-parameters
104104
execCmd("cmd /S /C \"\"" + program_files +
105-
"\\Git\\usr\\bin\\gzip.exe\" -d \"" + path + "\"\"");
105+
"\\Git\\usr\\bin\\gzip.exe\" -f -d \"" + path + "\"\"");
106106
}
107107
#else
108-
execCmd("gzip -d \"" + path + "\"");
108+
execCmd("gzip -f -d \"" + path + "\"");
109109
#endif
110110
}
111111

src/include/api_client.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class ApiClient {
1414

1515
void postCPUHour();
1616

17+
bool getOeisFile(const std::string& filename, const std::string& local_path);
18+
1719
Program getNextProgram();
1820

1921
private:

src/oeis_manager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,13 @@ void OeisManager::update() {
327327
std::string cmd, path;
328328
for (auto &file : files) {
329329
path = Setup::getOeisHome() + file;
330-
WebClient::get("https://oeis.org/" + file + ".gz", path + ".gz");
331-
std::ifstream f(Setup::getOeisHome() + file);
332-
if (f.good()) {
333-
f.close();
334-
std::remove(path.c_str());
330+
ApiClient api_client;
331+
if (!api_client.getOeisFile(file, path)) {
332+
Log::get().warn("Cannot fetch " + file +
333+
".gz from API server, falling back to OEIS");
334+
WebClient::get("https://oeis.org/" + file + ".gz", path + ".gz");
335+
gunzip(path + ".gz");
335336
}
336-
gunzip(path + ".gz");
337337
}
338338
// update programs repository using git pull
339339
auto mode = Setup::getMiningMode();

src/oeis_sequence.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <iomanip>
55
#include <sstream>
66

7+
#include "api_client.hpp"
78
#include "big_number.hpp"
89
#include "file.hpp"
910
#include "parser.hpp"
@@ -210,7 +211,12 @@ Sequence OeisSequence::getTerms(int64_t max_num_terms) const {
210211
big_file.peek() == std::ifstream::traits_type::eof()) {
211212
ensureDir(path);
212213
std::remove(path.c_str());
213-
WebClient::get(url_str() + "/" + id_str("b") + ".txt", path);
214+
ApiClient api_client;
215+
if (!api_client.getOeisFile(id_str("b") + ".txt", path)) {
216+
Log::get().warn(
217+
"Cannot fetch b-file from API server, falling back to OEIS");
218+
WebClient::get(url_str() + "/" + id_str("b") + ".txt", path);
219+
}
214220
big = loadBFile(id, terms);
215221
}
216222
}

0 commit comments

Comments
 (0)