Skip to content

Commit 5879a57

Browse files
committed
Prettify byte counts. Only build sqlshell if configured
1 parent a84a922 commit 5879a57

File tree

5 files changed

+77
-17
lines changed

5 files changed

+77
-17
lines changed

CMakeLists.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
66

77
option(UseLTO "Use link-time optimization" OFF)
88
option(Static "Statically link" OFF)
9+
option(BuildSqlShell "Build the sqlite shell" OFF)
910

1011
if(UseLTO)
1112
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
@@ -45,6 +46,8 @@ set(SOURCE_FILES
4546
src/ConsoleLog.cpp
4647
src/StringView.cpp
4748
src/StringView.h
49+
src/PrettyBytes.h
50+
src/PrettyBytes.cpp
4851
ext/sqlite/sqlite3.c)
4952

5053
set(TEST_FILES
@@ -56,6 +59,7 @@ set(TEST_FILES
5659
tests/RegExpIndexerTest.cpp
5760
tests/TempDir.h
5861
tests/TempDir.cpp
62+
tests/PrettyBytesTest.cpp
5963
tests/IndexTest.cpp
6064
tests/LogTest.cpp)
6165

@@ -67,11 +71,13 @@ target_link_libraries(zindex libzindex ${ZLIB_LIBRARIES} dl)
6771
add_executable(zq src/zq.cpp)
6872
target_link_libraries(zq libzindex ${ZLIB_LIBRARIES} dl)
6973

70-
add_executable(unit-tests ${TEST_FILES})
74+
add_executable(unit-tests ${TEST_FILES} )
7175
target_link_libraries(unit-tests libzindex ${ZLIB_LIBRARIES} dl)
7276

73-
add_executable(sql-shell ext/sqlite/shell.c ext/sqlite/sqlite3.c)
74-
target_link_libraries(sql-shell dl)
77+
if(BuildSqlShell)
78+
add_executable(sql-shell ext/sqlite/shell.c ext/sqlite/sqlite3.c)
79+
target_link_libraries(sql-shell dl)
80+
endif(BuildSqlShell)
7581

7682
enable_testing()
7783
add_test(NAME unit-tests

src/Index.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "IndexSink.h"
1919
#include "Log.h"
2020
#include "StringView.h"
21+
#include "PrettyBytes.h"
2122
#include <sys/types.h>
2223
#include <sys/stat.h>
2324
#include <unistd.h>
@@ -59,7 +60,7 @@ size_t makeWindow(uint8_t *out, size_t outSize, const uint8_t *in,
5960
return destLen;
6061
}
6162

62-
void uncompress(const std::vector <uint8_t> &compressed, uint8_t *to,
63+
void uncompress(const std::vector<uint8_t> &compressed, uint8_t *to,
6364
size_t len) {
6465
uLongf destLen = len;
6566
R(::uncompress(to, &len, &compressed[0], compressed.size()));
@@ -325,7 +326,7 @@ Index::Index() { }
325326

326327
Index::~Index() { }
327328

328-
Index::Index(std::unique_ptr < Impl > && imp) : impl_(std::move(imp)) { }
329+
Index::Index(std::unique_ptr<Impl> &&imp) : impl_(std::move(imp)) { }
329330

330331
struct Index::Builder::Impl : LineSink {
331332
Log &log;
@@ -336,7 +337,7 @@ struct Index::Builder::Impl : LineSink {
336337
Sqlite::Statement addIndexSql;
337338
Sqlite::Statement addMetaSql;
338339
uint64_t indexEvery = DefaultIndexEvery;
339-
std::unordered_map <std::string, std::unique_ptr<IndexHandler>> indexers;
340+
std::unordered_map<std::string, std::unique_ptr<IndexHandler>> indexers;
340341

341342
Impl(Log &log, File &&from, const std::string &fromPath,
342343
const std::string &indexFilename)
@@ -397,8 +398,8 @@ INSERT INTO Indexes VALUES(:name, :creationString, :isNumeric)
397398
}
398399

399400
void build() {
400-
log.info("Building index, generating a checkpoint every ", indexEvery,
401-
" bytes");
401+
log.info("Building index, generating a checkpoint every ",
402+
PrettyBytes(indexEvery));
402403
struct stat compressedStat;
403404
if (fstat(fileno(from.get()), &compressedStat) != 0)
404405
throw ZlibError(Z_DATA_ERROR);
@@ -457,8 +458,9 @@ INSERT INTO LineOffsets VALUES(:line, :offset, :length))");
457458
bool endOfBlock = zs.stream.data_type & 0x80;
458459
bool lastBlockInStream = zs.stream.data_type & 0x40;
459460
if (endOfBlock && !lastBlockInStream && needsIndex) {
460-
log.debug("Creating checkpoint at ", totalOut,
461-
" (compressed offset ", totalIn, ")");
461+
log.debug("Creating checkpoint at ", PrettyBytes(totalOut),
462+
" (compressed offset ", PrettyBytes(totalIn),
463+
")");
462464
if (totalOut != 0) {
463465
// Flush previous information.
464466
addIndex
@@ -482,8 +484,8 @@ INSERT INTO LineOffsets VALUES(:line, :offset, :length))");
482484
char pc[16];
483485
snprintf(pc, sizeof(pc) - 1, "%.2f",
484486
(totalIn * 100.0) / compressedStat.st_size);
485-
log.info("Progress: ", totalIn, " of ",
486-
compressedStat.st_size,
487+
log.info("Progress: ", PrettyBytes(totalIn), " of ",
488+
PrettyBytes(compressedStat.st_size),
487489
" (", pc, "%)");
488490
nextProgress = now + LogProgressEverySecs;
489491
}
@@ -597,9 +599,9 @@ Index Index::load(Log &log, File &&fromCompressed,
597599
Sqlite db(log);
598600
db.open(indexFilename.c_str(), true);
599601

600-
std::unique_ptr <Impl> impl(new Impl(log,
601-
std::move(fromCompressed),
602-
std::move(db)));
602+
std::unique_ptr<Impl> impl(new Impl(log,
603+
std::move(fromCompressed),
604+
std::move(db)));
603605
impl->init(forceLoad);
604606
return Index(std::move(impl));
605607
}
@@ -608,7 +610,7 @@ void Index::getLine(uint64_t line, LineSink &sink) {
608610
impl_->getLine(line, sink);
609611
}
610612

611-
void Index::getLines(const std::vector <uint64_t> &lines, LineSink &sink) {
613+
void Index::getLines(const std::vector<uint64_t> &lines, LineSink &sink) {
612614
for (auto line : lines) impl_->getLine(line, sink);
613615
}
614616

@@ -618,7 +620,7 @@ void Index::queryIndex(const std::string &index, const std::string &query,
618620
}
619621

620622
void Index::queryIndexMulti(const std::string &index,
621-
const std::vector <std::string> &queries,
623+
const std::vector<std::string> &queries,
622624
LineSink &sink) {
623625
// TODO be a little smarter about this.
624626
for (auto query : queries) impl_->queryIndex(index, query, sink);

src/PrettyBytes.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "PrettyBytes.h"
2+
3+
#include <iostream>
4+
#include <iomanip>
5+
6+
using namespace std;
7+
8+
ostream &operator<<(ostream &o, PrettyBytes pb) {
9+
if (pb.bytes_ == 1) return o << "1 byte";
10+
if (pb.bytes_ < 1024) return o << pb.bytes_ << " bytes";
11+
o << setprecision(2) << fixed;
12+
if (pb.bytes_ < 1024 * 1024)
13+
return o << (pb.bytes_ / (1024.0)) << " KiB";
14+
if (pb.bytes_ < 1024 * 1024 * 1024)
15+
return o << (pb.bytes_ / (1024 * 1024.0)) << " MiB";
16+
return o << (pb.bytes_ / (1024 * 1024 * 1024.0)) << " GiB";
17+
}

src/PrettyBytes.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <iosfwd>
4+
#include <cstdint>
5+
6+
class PrettyBytes {
7+
uint64_t bytes_;
8+
public:
9+
explicit PrettyBytes(uint64_t bytes) : bytes_(bytes) { }
10+
11+
friend std::ostream &operator<<(std::ostream &, PrettyBytes pb);
12+
};

tests/PrettyBytesTest.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "PrettyBytes.h"
2+
3+
#include "catch.hpp"
4+
#include <sstream>
5+
6+
#define STR(XX) ([&]() ->std::string { \
7+
std::stringstream STR__STREAM; \
8+
STR__STREAM << XX; \
9+
return STR__STREAM.str(); \
10+
})()
11+
12+
TEST_CASE("pretty bytes", "[PrettyBytes]") {
13+
CHECK(STR(PrettyBytes(0)) == "0 bytes");
14+
CHECK(STR(PrettyBytes(1)) == "1 byte");
15+
CHECK(STR(PrettyBytes(2)) == "2 bytes");
16+
CHECK(STR(PrettyBytes(1023)) == "1023 bytes");
17+
CHECK(STR(PrettyBytes(1024)) == "1.00 KiB");
18+
CHECK(STR(PrettyBytes(1200)) == "1.17 KiB");
19+
CHECK(STR(PrettyBytes(1200000)) == "1.14 MiB");
20+
CHECK(STR(PrettyBytes(1200000000)) == "1.12 GiB");
21+
CHECK(STR(PrettyBytes(std::numeric_limits<uint64_t>::max())) ==
22+
"17179869184.00 GiB");
23+
}

0 commit comments

Comments
 (0)