Skip to content

Commit

Permalink
Add new LongConstantString Codec behind feature flag
Browse files Browse the repository at this point in the history
The new codec is enabled for reading but odc will not write data with the new codec unless "ODC_ENABLE_WRITING_LONG_STRING_CODEC" is set in the environment. This is to give some time for installations in the wild to update before files containing the new code are produced by default.
  • Loading branch information
TomHodson committed Aug 6, 2024
1 parent baf3b3b commit 8f52ebb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/odc/codec/CodecOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ int CodecOptimizer::setOptimalCodecs(core::MetaData& columns)
n = col.coder().numStrings();
if (n == 1 && col.coder().dataSizeDoubles() == 1)
codec = "constant_string";
else if (n == 1 && std::getenv("ODC_ENABLE_WRITING_LONG_STRING_CODEC") != NULL)
codec = "long_constant_string";
else if(n < 256)
codec = "int8_string";
else if(n < 65536)
Expand Down
1 change: 1 addition & 0 deletions src/odc/codec/Constant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace codec {
namespace {
core::IntegerCodecBuilder<CodecConstant> constantBuilder;
core::CodecBuilder<CodecConstantString> constantStringBuilder;
core::CodecBuilder<CodecLongConstantString> LongConstantStringBuilder;
}

//----------------------------------------------------------------------------------------------------------------------
Expand Down
53 changes: 53 additions & 0 deletions src/odc/codec/Constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define odc_core_codec_Constant_H

#include "odc/core/Codec.h"
#include "odc/codec/String.h"

namespace odc {
namespace codec {
Expand Down Expand Up @@ -74,6 +75,25 @@ class CodecConstantString : public CodecConstant<ByteOrder, double> {
void save(core::DataStream<ByteOrder>& ds) override;
};

template<typename ByteOrder>
class CodecLongConstantString : public CodecChars<ByteOrder> {

public: // methods
constexpr static const char* codec_name() { return "long_constant_string"; }
CodecLongConstantString(api::ColumnType type) : CodecChars<ByteOrder>(type, codec_name()) {};
~CodecLongConstantString() override {}

private: // methods
unsigned char* encode(unsigned char* p, const double& d) override;
void decode(double* out) override;
void skip() override;

size_t numStrings() const override { return 1; }

void load(core::DataStream<ByteOrder>& ds) override;
void save(core::DataStream<ByteOrder>& ds) override;
};

//----------------------------------------------------------------------------------------------------------------------

// Implementation of Constant
Expand Down Expand Up @@ -151,6 +171,39 @@ void CodecConstantString<ByteOrder>::print(std::ostream& s) const {

//----------------------------------------------------------------------------------------------------------------------

// Implementation of LongConstantString

template <typename ByteOrder>
unsigned char* CodecLongConstantString<ByteOrder>::encode(unsigned char* p, const double&) {
return p;
}

template <typename ByteOrder>
void CodecLongConstantString<ByteOrder>::decode(double* out) {
::memset(out, 0, this->decodedSizeDoubles_*sizeof(double));
::memcpy(reinterpret_cast<char*>(out), &this->strings_[0][0], std::min(this->strings_[0].length(), this->decodedSizeDoubles_*sizeof(double)));
}

template <typename ByteOrder>
void CodecLongConstantString<ByteOrder>::skip() {}

template <typename ByteOrder>
void CodecLongConstantString<ByteOrder>::load(core::DataStream<ByteOrder>& ds) {
core::DataStreamCodec<ByteOrder>::load(ds);
std::string s;
ds.read(s);
this->decodedSizeDoubles_ = ((s.length()-1) / sizeof(double)) +1;
this->strings_.push_back(s);
}

template <typename ByteOrder>
void CodecLongConstantString<ByteOrder>::save(core::DataStream<ByteOrder>& ds) {
core::DataStreamCodec<ByteOrder>::save(ds);
ds.write(this->strings_[0]);
}

//----------------------------------------------------------------------------------------------------------------------

} // namespace codec
} // namespace odc

Expand Down

0 comments on commit 8f52ebb

Please sign in to comment.