Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append to CSV #1612

Open
jalius opened this issue Feb 12, 2025 · 2 comments
Open

Append to CSV #1612

jalius opened this issue Feb 12, 2025 · 2 comments

Comments

@jalius
Copy link
Contributor

jalius commented Feb 12, 2025

Is there a good way to append to a CSV buffer?
I see this issue tangentially mentioned in #1019 but it's not clear if this is implemented yet.

For now, I could write my CSV to a string buffer and append from the buffer to the file system manually. Not sure if that is any faster than just writing the entire file repeatedly in my use case.

@stephenberry
Copy link
Owner

Here is somewhat of a hack that @anders-wind came up with. I haven't tested this exact code, but it creates a wrapping buffer that can be passed to Glaze to trick Glaze into appending data starting at the offset. Should Glaze have a structure like this as a permanent solution? Perhaps, and I'd love to hear your thoughts if you find this approach useful.

template <typename InnerBufferT>
struct AppendStringBuffer
{
    InnerBufferT& buffer;
    size_t offset {0};

   using reference = char&;

   constexpr auto* data()
   {
      return buffer.data() + offset;
   }

   constexpr const auto* data() const
   {
      return buffer.data() + offset;
   }

    constexpr auto operator[](size_t index) -> char&
    {
        return buffer[this->offset + index];
    }

    constexpr auto begin()
    {
        return buffer.begin() + static_cast<int64_t>(this->offset);
    }
    constexpr auto end()
    {
        return buffer.end();
    }

    constexpr auto resize(size_t val) -> void
    {
        buffer.resize(this->offset + val);
    }

    constexpr auto size() const -> size_t
    {
        return buffer.size() - this->offset;
    }

    [[nodiscard]]
    constexpr auto empty() const -> bool
    {
        return this->size() == 0;
    }
};

@anders-wind
Copy link

Yes we are using the above in production and then

    auto buffer = std::string{"My already existing data."};
    auto offset_buffer = AppendStringBuffer<std::string> {.buffer = buffer, .offset = buffer.size()};
    auto ec =
        glz::write<glz::opts {.format = glz::JSON}>(value, offset_buffer); // now the value will be serialized and appended to buffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants