Skip to content

Commit

Permalink
Rewrite to use a custom class that that just writes a bare string to …
Browse files Browse the repository at this point in the history
…the header
  • Loading branch information
TomHodson committed Aug 1, 2024
1 parent 15bc01f commit 7978720
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
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
1 change: 0 additions & 1 deletion src/odc/codec/String.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace codec {

namespace {
core::CodecBuilder<CodecChars> charsBuilder;
core::CodecBuilder<CodecLongConstantString> LongConstantStringBuilder;
core::CodecBuilder<CodecInt8String> int8StringBuilder;
core::CodecBuilder<CodecInt16String> int16StringBuilder;
}
Expand Down
9 changes: 0 additions & 9 deletions src/odc/codec/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,6 @@ class IntStringCodecBase : public CodecChars<ByteOrder> {

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

// This class uses the same machinery as CodecInt8String/CodecInt16String etc but it only ever stores one string in the header
// CodecZeroBitInteger just doesn't encode any data and always returns index 0 when decoding.
template<typename ByteOrder>
struct CodecLongConstantString : public IntStringCodecBase<ByteOrder, CodecZeroBitInteger<ByteOrder, int64_t, CodecLongConstantString<ByteOrder>>> {
constexpr static const char* codec_name() { return "long_constant_string"; }
CodecLongConstantString(api::ColumnType type) : IntStringCodecBase<ByteOrder, CodecZeroBitInteger<ByteOrder, int64_t, CodecLongConstantString<ByteOrder>>>(type, codec_name()) {}
~CodecLongConstantString() override {}
};

template<typename ByteOrder>
struct CodecInt8String : public IntStringCodecBase<ByteOrder, CodecInt8<ByteOrder, int64_t>> {
constexpr static const char* codec_name() { return "int8_string"; }
Expand Down

0 comments on commit 7978720

Please sign in to comment.