Skip to content

Commit

Permalink
io:stream
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Jun 19, 2022
1 parent ae009b7 commit 4fda7a7
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 9 deletions.
2 changes: 1 addition & 1 deletion db/sstable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace db {
namespace {

TEST(SSTableTest, basic) {
TEST(SSTableTest, main) {
io::MemoryFile file;

// Build a SSTable.
Expand Down
20 changes: 20 additions & 0 deletions io/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,23 @@ cc_library(
hdrs = ["memory-file.h"],
deps = [":file"],
)

cc_library(
name = "stream",
srcs = ["stream.cc"],
hdrs = ["stream.h"],
deps = [
":file",
":file-utils",
],
)

cc_test(
name = "stream_test",
srcs = ["stream_test.cc"],
deps = [
":stream",
":memory-file",
"@com_google_googletest//:gtest_main",
],
)
2 changes: 1 addition & 1 deletion io/line-reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace io {
namespace {

TEST(LineReaderTest, basic) {
TEST(LineReaderTest, main) {
MemoryFile file({'h', 'e', 'l', 'l', 'o', '\n',
'w', 'o', 'r', 'l', 'd', '\n'});
LineReader reader(file, {});
Expand Down
2 changes: 1 addition & 1 deletion io/posix/file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace io {
namespace posix {
namespace {

TEST(FileTest, basic) {
TEST(FileTest, main) {
std::string filename = absl::StrCat(getenv("TEST_TMPDIR"), "/test");

// Create a file and use pwrite to overwrite a portion.
Expand Down
36 changes: 36 additions & 0 deletions io/stream.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "io/stream.h"

#include "io/file-utils.h"

namespace io {

OStream::OStream(File &file)
: std::ostream(this),
file_(file) {
setp(buffer_.begin(), buffer_.end());
}

int OStream::sync() {
size_t size = pptr() - pbase();
if (!size) {
return 0;
}
if (io::write(file_, {pbase(), size})) {
return -1;
}
setp(buffer_.begin(), buffer_.end());
return 0;
}

int OStream::overflow(int c) {
if (sync() < 0) {
return EOF;
}
if (c != EOF) {
*pptr() = c;
pbump(1);
}
return 0;
}

} // namespace io
27 changes: 27 additions & 0 deletions io/stream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _IO_STREAM_H
#define _IO_STREAM_H

#include <array>
#include <ostream>
#include <streambuf>

#include "io/file.h"

namespace io {

class OStream : private std::streambuf, public std::ostream {
public:
explicit OStream(File &file);
~OStream() override { sync(); }

private:
int sync() override;
int overflow(int c) override;

File &file_;
std::array<char, 8192> buffer_;
};

} // namespace io

#endif // _IO_STREAM_H
21 changes: 21 additions & 0 deletions io/stream_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "io/stream.h"

#include "io/memory-file.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace io {
namespace {

using ::testing::ElementsAre;

TEST(OStreamTest, main) {
MemoryFile file;
OStream os(file);
os << "foobar" << std::flush;
EXPECT_THAT(file.content(), ElementsAre('f', 'o', 'o', 'b', 'a', 'r'));
}

} // namespace
} // namespace io
2 changes: 2 additions & 0 deletions util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ cc_binary(
srcs = ["hash-filter_stats.cc"],
deps = [
":hash-filter",
"//io:stream",
"//io/posix:file",
"@com_google_absl//absl/random",
],
)
Expand Down
14 changes: 8 additions & 6 deletions util/hash-filter_stats.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <cstdint>
#include <iostream>
#include <vector>

#include "absl/random/random.h"
#include "io/posix/file.h"
#include "io/stream.h"
#include "util/hash-filter.h"

int main() {
io::OStream os(io::posix::stdout);
absl::InsecureBitGen gen;
util::HashFilter32 filter(262144);
std::vector<uint64_t> fingerprints;
Expand All @@ -14,7 +16,7 @@ int main() {
filter.insert(fingerprint);
fingerprints.push_back(fingerprint);
}
std::cout << "inserted " << filter.size() << std::endl;
os << "inserted " << filter.size() << std::endl;

int true_positives = 0;
int false_negatives = 0;
Expand All @@ -25,8 +27,8 @@ int main() {
++false_negatives;
}
}
std::cout << "true positives " << true_positives << std::endl;
std::cout << "false negatives " << false_negatives << std::endl;
os << "true positives " << true_positives << std::endl;
os << "false negatives " << false_negatives << std::endl;

int false_positives = 0;
int true_negatives = 0;
Expand All @@ -38,6 +40,6 @@ int main() {
++true_negatives;
}
}
std::cout << "false positives " << false_positives << std::endl;
std::cout << "true negatives " << true_negatives << std::endl;
os << "false positives " << false_positives << std::endl;
os << "true negatives " << true_negatives << std::endl;
}

0 comments on commit 4fda7a7

Please sign in to comment.