Expose public interface in a public directory (#51)
Move headers into public directory.
Add VCDiffEncoder constructor with provided CodeTableWriter.
diff --git a/src/blockhash_test.cc b/src/blockhash_test.cc
index 52cb3ca..489a641 100644
--- a/src/blockhash_test.cc
+++ b/src/blockhash_test.cc
@@ -17,7 +17,7 @@
#include <limits.h> // INT_MIN
#include <string.h> // memcpy, memcmp, strlen
#include <iostream>
-#include "encodetable.h"
+#include "google/encodetable.h"
#include "rolling_hash.h"
#include "testing.h"
#include "unique_ptr.h" // auto_ptr, unique_ptr
diff --git a/src/encodetable.cc b/src/encodetable.cc
index 4d39142..ce017de 100644
--- a/src/encodetable.cc
+++ b/src/encodetable.cc
@@ -17,7 +17,7 @@
#include <string>
#include "addrcache.h"
#include "codetable.h"
-#include "encodetable.h"
+#include "google/encodetable.h"
#include "instruction_map.h"
#include "logging.h"
#include "google/output_string.h"
diff --git a/src/encodetable_test.cc b/src/encodetable_test.cc
index 7679688..be99d8b 100644
--- a/src/encodetable_test.cc
+++ b/src/encodetable_test.cc
@@ -15,7 +15,6 @@
// Unit tests for the class VCDiffCodeTableWriter, found in encodetable.h.
#include <config.h>
-#include "encodetable.h"
#include <string.h> // strlen
#include <algorithm>
#include <string>
@@ -23,6 +22,7 @@
#include "checksum.h"
#include "codetable.h"
#include "google/output_string.h"
+#include "google/encodetable.h"
#include "testing.h"
#include "vcdiff_defs.h"
diff --git a/src/codetablewriter_interface.h b/src/google/codetablewriter_interface.h
similarity index 100%
rename from src/codetablewriter_interface.h
rename to src/google/codetablewriter_interface.h
diff --git a/src/encodetable.h b/src/google/encodetable.h
similarity index 100%
rename from src/encodetable.h
rename to src/google/encodetable.h
diff --git a/src/jsonwriter.h b/src/google/jsonwriter.h
similarity index 100%
rename from src/jsonwriter.h
rename to src/google/jsonwriter.h
diff --git a/src/google/vcencoder.h b/src/google/vcencoder.h
index 732de59..37d6fd6 100644
--- a/src/google/vcencoder.h
+++ b/src/google/vcencoder.h
@@ -23,6 +23,7 @@
class VCDiffEngine;
class VCDiffStreamingEncoderImpl;
+class CodeTableWriterInterface;
// A HashedDictionary must be constructed from the dictionary data
// in order to use VCDiffStreamingEncoder. If the same dictionary will
@@ -100,9 +101,17 @@
// look_for_target_matches) because the cost of checking for matches
// across the source-target boundary would not justify its benefits.
//
+ // Second version of constructor uses provided CodeTableInterfaceWriter
+ // pointer instead of constructing one based on format_extenstions and will
+ // take ownership of it. It's useful when you want to gather some statistics
+ // for ADD/COPY/RUN instructions and archived compression ratio.
VCDiffStreamingEncoder(const HashedDictionary* dictionary,
VCDiffFormatExtensionFlags format_extensions,
bool look_for_target_matches);
+ VCDiffStreamingEncoder(const HashedDictionary* dictionary,
+ VCDiffFormatExtensionFlags format_extensions,
+ bool look_for_target_matches,
+ CodeTableWriterInterface* writer);
~VCDiffStreamingEncoder();
// The client should use these routines as follows:
diff --git a/src/jsonwriter.cc b/src/jsonwriter.cc
index c147b62..d4f2f33 100644
--- a/src/jsonwriter.cc
+++ b/src/jsonwriter.cc
@@ -17,8 +17,8 @@
#include <string.h>
#include <sstream>
#include <string>
-#include "jsonwriter.h"
#include "logging.h"
+#include "google/jsonwriter.h"
#include "google/output_string.h"
namespace open_vcdiff {
diff --git a/src/jsonwriter_test.cc b/src/jsonwriter_test.cc
index b8676db..1075af0 100644
--- a/src/jsonwriter_test.cc
+++ b/src/jsonwriter_test.cc
@@ -15,9 +15,9 @@
// Unit tests for the class JSONCodeTableWriter, found in jsonwriter.h.
#include <config.h>
-#include "jsonwriter.h"
#include "testing.h"
#include "vcdiff_defs.h"
+#include "google/jsonwriter.h"
#include "google/output_string.h"
namespace open_vcdiff {
diff --git a/src/vcdiff_main.cc b/src/vcdiff_main.cc
index d58a959..41d1c17 100644
--- a/src/vcdiff_main.cc
+++ b/src/vcdiff_main.cc
@@ -29,6 +29,8 @@
#include "gflags/gflags.h"
#include "google/vcdecoder.h"
#include "google/vcencoder.h"
+#include "google/jsonwriter.h"
+#include "google/encodetable.h"
#include "unique_ptr.h" // auto_ptr, unique_ptr
#ifndef HAS_GLOBAL_STRING
@@ -387,6 +389,7 @@
return false;
}
VCDiffFormatExtensionFlags format_flags = open_vcdiff::VCD_STANDARD_FORMAT;
+ UNIQUE_PTR<CodeTableWriterInterface> writer;
if (FLAGS_interleaved) {
format_flags |= open_vcdiff::VCD_FORMAT_INTERLEAVED;
}
@@ -395,10 +398,15 @@
}
if (FLAGS_json) {
format_flags |= open_vcdiff::VCD_FORMAT_JSON;
+ writer.reset(new JSONCodeTableWriter);
+ } else {
+ writer.reset(new VCDiffCodeTableWriter(FLAGS_interleaved));
}
+
open_vcdiff::VCDiffStreamingEncoder encoder(hashed_dictionary_.get(),
format_flags,
- FLAGS_target_matches);
+ FLAGS_target_matches,
+ writer.release());
string output;
size_t input_size = 0;
size_t output_size = 0;
diff --git a/src/vcdiffengine.cc b/src/vcdiffengine.cc
index 85a5b5f..0060867 100644
--- a/src/vcdiffengine.cc
+++ b/src/vcdiffengine.cc
@@ -17,7 +17,7 @@
#include <stdint.h> // uint32_t
#include <string.h> // memcpy
#include "blockhash.h"
-#include "codetablewriter_interface.h"
+#include "google/codetablewriter_interface.h"
#include "logging.h"
#include "rolling_hash.h"
diff --git a/src/vcdiffengine_test.cc b/src/vcdiffengine_test.cc
index b915a55..cb60d95 100644
--- a/src/vcdiffengine_test.cc
+++ b/src/vcdiffengine_test.cc
@@ -19,7 +19,7 @@
#include <string>
#include "addrcache.h"
#include "blockhash.h"
-#include "encodetable.h"
+#include "google/encodetable.h"
#include "google/output_string.h"
#include "rolling_hash.h"
#include "testing.h"
diff --git a/src/vcencoder.cc b/src/vcencoder.cc
index c1c5a92..7fe5a6a 100644
--- a/src/vcencoder.cc
+++ b/src/vcencoder.cc
@@ -28,16 +28,34 @@
#include <config.h>
#include "checksum.h"
-#include "encodetable.h"
+#include "google/encodetable.h"
#include "google/output_string.h"
#include "google/vcencoder.h"
-#include "jsonwriter.h"
+#include "google/jsonwriter.h"
#include "logging.h"
#include "unique_ptr.h" // auto_ptr, unique_ptr
#include "vcdiffengine.h"
namespace open_vcdiff {
+namespace {
+
+// Helper function to create default CodeTableWriter
+CodeTableWriterInterface* create_writer(
+ VCDiffFormatExtensionFlags format_extensions) {
+ if (format_extensions & VCD_FORMAT_JSON) {
+ return new JSONCodeTableWriter();
+ } else {
+ // This implementation of the encoder uses the default
+ // code table. A VCDiffCodeTableWriter could also be constructed
+ // using a custom code table.
+ return new VCDiffCodeTableWriter(
+ (format_extensions & VCD_FORMAT_INTERLEAVED) != 0);
+ }
+}
+
+} // namespace
+
HashedDictionary::HashedDictionary(const char* dictionary_contents,
size_t dictionary_size)
: engine_(new VCDiffEngine(dictionary_contents, dictionary_size)) { }
@@ -52,7 +70,8 @@
public:
VCDiffStreamingEncoderImpl(const HashedDictionary* dictionary,
VCDiffFormatExtensionFlags format_extensions,
- bool look_for_target_matches);
+ bool look_for_target_matches,
+ CodeTableWriterInterface* writer);
// These functions are identical to their counterparts
// in VCDiffStreamingEncoder.
@@ -89,21 +108,13 @@
inline VCDiffStreamingEncoderImpl::VCDiffStreamingEncoderImpl(
const HashedDictionary* dictionary,
VCDiffFormatExtensionFlags format_extensions,
- bool look_for_target_matches)
+ bool look_for_target_matches,
+ CodeTableWriterInterface* writer)
: engine_(dictionary->engine()),
+ coder_(writer),
format_extensions_(format_extensions),
look_for_target_matches_(look_for_target_matches),
- encode_chunk_allowed_(false) {
- if (format_extensions & VCD_FORMAT_JSON) {
- coder_.reset(new JSONCodeTableWriter());
- } else {
- // This implementation of the encoder uses the default
- // code table. A VCDiffCodeTableWriter could also be constructed
- // using a custom code table.
- coder_.reset(new VCDiffCodeTableWriter(
- (format_extensions & VCD_FORMAT_INTERLEAVED) != 0));
- }
-}
+ encode_chunk_allowed_(false) { }
inline bool VCDiffStreamingEncoderImpl::StartEncoding(
OutputStringInterface* out) {
@@ -156,9 +167,21 @@
const HashedDictionary* dictionary,
VCDiffFormatExtensionFlags format_extensions,
bool look_for_target_matches)
+ : impl_(new VCDiffStreamingEncoderImpl(
+ dictionary,
+ format_extensions,
+ look_for_target_matches,
+ create_writer(format_extensions))) { }
+
+VCDiffStreamingEncoder::VCDiffStreamingEncoder(
+ const HashedDictionary* dictionary,
+ VCDiffFormatExtensionFlags format_extensions,
+ bool look_for_target_matches,
+ CodeTableWriterInterface* writer)
: impl_(new VCDiffStreamingEncoderImpl(dictionary,
format_extensions,
- look_for_target_matches)) { }
+ look_for_target_matches,
+ writer)) { }
VCDiffStreamingEncoder::~VCDiffStreamingEncoder() { delete impl_; }
diff --git a/src/vcencoder_test.cc b/src/vcencoder_test.cc
index d428cc8..99f449e 100644
--- a/src/vcencoder_test.cc
+++ b/src/vcencoder_test.cc
@@ -24,6 +24,7 @@
#include "testing.h"
#include "varint_bigendian.h"
#include "google/vcdecoder.h"
+#include "google/jsonwriter.h"
#include "vcdiff_defs.h"
#ifdef HAVE_EXT_ROPE
@@ -136,6 +137,7 @@
VCDiffEncoder simple_encoder_;
VCDiffDecoder simple_decoder_;
VCDiffStreamingEncoder json_encoder_;
+ VCDiffStreamingEncoder external_encoder_;
VCDiffEncoder nonascii_simple_encoder_;
string result_target_;
@@ -171,6 +173,10 @@
json_encoder_(&hashed_dictionary_,
VCD_FORMAT_JSON,
/* look_for_target_matches = */ true),
+ external_encoder_(&hashed_dictionary_,
+ 0,
+ /* look_for_target_matches = */ true,
+ new JSONCodeTableWriter()),
nonascii_simple_encoder_(kNonAscii, sizeof(kNonAscii)) {
EXPECT_TRUE(hashed_dictionary_.Init());
}
@@ -288,6 +294,13 @@
EXPECT_EQ(kJSONDiff, delta_as_const());
}
+TEST_F(VCDiffEncoderTest, EncodeSimpleExternalJSON) {
+ EXPECT_TRUE(external_encoder_.StartEncoding(delta()));
+ EXPECT_TRUE(external_encoder_.EncodeChunk(kTarget, strlen(kTarget), delta()));
+ EXPECT_TRUE(external_encoder_.FinishEncoding(delta()));
+ EXPECT_EQ(kJSONDiff, delta_as_const());
+}
+
TEST_F(VCDiffEncoderTest, EncodeDecodeSeparate) {
string delta_start, delta_encode, delta_finish;
EXPECT_TRUE(encoder_.StartEncoding(&delta_start));