-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
324 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include <cstdio> | ||
#include <iostream> | ||
#include <unistd.h> | ||
#include <time.h> | ||
#include <sys/time.h> | ||
#include <sstream> | ||
|
||
#include "succinct_file.h" | ||
|
||
/** | ||
* Prints usage. | ||
*/ | ||
void print_usage(char *exec) { | ||
fprintf(stderr, "Usage: %s [-m mode] [file]\n", exec); | ||
} | ||
|
||
void print_valid_cmds() { | ||
std::cerr | ||
<< "Command must be one of: search [query], count [query], extract [offset] [length]\n"; | ||
} | ||
|
||
typedef unsigned long long int timestamp_t; | ||
|
||
static timestamp_t get_timestamp() { | ||
struct timeval now; | ||
gettimeofday(&now, NULL); | ||
|
||
return (now.tv_usec + (time_t) now.tv_sec * 1000000); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
if (argc < 2 || argc > 5) { | ||
print_usage(argv[0]); | ||
return -1; | ||
} | ||
|
||
int c; | ||
uint32_t mode = 0; | ||
while ((c = getopt(argc, argv, "m:")) != -1) { | ||
switch (c) { | ||
case 'm': | ||
mode = atoi(optarg); | ||
break; | ||
default: | ||
fprintf(stderr, "Invalid option %c\n", c); | ||
exit(0); | ||
} | ||
} | ||
|
||
if (optind == argc) { | ||
print_usage(argv[0]); | ||
return -1; | ||
} | ||
|
||
std::string filename = std::string(argv[optind]); | ||
|
||
SuccinctFile *s_file = NULL; | ||
if (mode == 0) { | ||
// If mode is set to 0, compress the input file. | ||
// Use default parameters. | ||
std::cout << "Constructing Succinct data structures...\n"; | ||
s_file = new SuccinctFile(filename); | ||
|
||
std::cout << "Serializing Succinct data structures...\n"; | ||
s_file->Serialize(); | ||
} else { | ||
// If mode is set to 1, read the serialized data structures from disk. | ||
// The serialized data structures must exist at <filename>.succinct. | ||
std::cout << "De-serializing Succinct data structures...\n"; | ||
s_file = new SuccinctFile(filename, SuccinctMode::LOAD_IN_MEMORY); | ||
} | ||
|
||
std::cout << "Done. Starting Succinct Shell...\n"; | ||
|
||
print_valid_cmds(); | ||
while (true) { | ||
char cmd_line[500]; | ||
std::cout << "succinct> "; | ||
std::cin.getline(cmd_line, sizeof(cmd_line)); | ||
std::istringstream iss(cmd_line); | ||
std::string cmd, arg; | ||
int64_t offset, length; | ||
if (!(iss >> cmd)) { | ||
std::cerr << "Could not parse command: " << cmd_line << "\n"; | ||
continue; | ||
} | ||
|
||
if (cmd == "search") { | ||
if (!(iss >> arg)) { | ||
std::cerr << "Could not parse argument: " << cmd_line << "\n"; | ||
continue; | ||
} | ||
std::vector<int64_t> results; | ||
timestamp_t start = get_timestamp(); | ||
s_file->Search(results, arg); | ||
timestamp_t tot_time = get_timestamp() - start; | ||
std::cout << "Found " << results.size() << " results in " << tot_time | ||
<< "us:\n"; | ||
for (auto res : results) { | ||
std::cout << res << ", "; | ||
} | ||
std::cout << std::endl; | ||
} else if (cmd == "count") { | ||
if (!(iss >> arg)) { | ||
std::cerr << "Could not parse argument: " << cmd_line << "\n"; | ||
continue; | ||
} | ||
timestamp_t start = get_timestamp(); | ||
int64_t count = s_file->Count(arg); | ||
timestamp_t tot_time = get_timestamp() - start; | ||
std::cout << "Count = " << count << "; Time taken: " << tot_time | ||
<< "us\n"; | ||
} else if (cmd == "extract") { | ||
if (!(iss >> offset >> length)) { | ||
std::cerr << "Could not parse argument: " << cmd_line << "\n"; | ||
continue; | ||
} | ||
timestamp_t start = get_timestamp(); | ||
std::string result; | ||
s_file->Extract(result, offset, length); | ||
timestamp_t tot_time = get_timestamp() - start; | ||
std::cout << "Extracted string = " << result << "; Time taken: " | ||
<< tot_time << "us\n"; | ||
} else { | ||
std::cerr << "Unsupported command: " << cmd << std::endl; | ||
print_valid_cmds(); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.