github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/chunked_buffer.h (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 #pragma once 12 13 #include <rocksdb/db.h> 14 #include "libroach.h" 15 16 namespace cockroach { 17 18 class chunkedBuffer { 19 public: 20 chunkedBuffer() { Clear(); } 21 ~chunkedBuffer() { Clear(); } 22 23 // Write a key/value pair to this chunkedBuffer. 24 void Put(const rocksdb::Slice& key, const rocksdb::Slice& value); 25 26 // Clear this chunkedBuffer. 27 void Clear(); 28 29 void GetChunks(DBSlice** bufs, int32_t* len) { 30 // Cap the last buffer's size to the amount that's been written to it. 31 DBSlice& last = bufs_.back(); 32 last.len = buf_ptr_ - last.data; 33 *bufs = &bufs_.front(); 34 *len = bufs_.size(); 35 } 36 37 // Get the number of key/value pairs written to this chunkedBuffer. 38 int Count() const { return count_; } 39 // Get the number of bytes written to this chunkedBuffer. 40 int NumBytes() const { return bytes_; } 41 42 private: 43 void put(const char* data, int len, int next_size_hint); 44 45 private: 46 std::vector<DBSlice> bufs_; 47 int64_t count_; 48 int64_t bytes_; 49 char* buf_ptr_; 50 }; 51 52 } // namespace cockroach