Skip to content

Commit

Permalink
Move IterKey::TrimAppend() definition to dbformat.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
rockeet committed Jan 28, 2023
1 parent 9155169 commit e8d4698
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
26 changes: 26 additions & 0 deletions db/dbformat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ LookupKey::LookupKey(const Slice& _user_key, SequenceNumber s,
end_ = dst;
}

void IterKey::TrimAppend(const size_t shared_len, const char* non_shared_data,
const size_t non_shared_len) {
assert(shared_len <= key_size_);
size_t total_size = shared_len + non_shared_len;

if (IsKeyPinned() /* key is not in buf_ */) {
// Copy the key from external memory to buf_ (copy shared_len bytes)
EnlargeBufferIfNeeded(total_size);
memcpy(buf(), key_, shared_len);
} else if (total_size > buf_size_) {
// Need to allocate space, delete previous space
char* p = new char[total_size];
memcpy(p, key_, shared_len);

if (buf_size_ != sizeof(space_)) {
delete[] buf_;
}

buf_ = p;
buf_size_ = total_size;
}
memcpy(buf() + shared_len, non_shared_data, non_shared_len);
key_ = buf();
key_size_ = total_size;
}

void IterKey::EnlargeBuffer(size_t key_size) {
// If size is smaller than buffer size, continue using current buffer,
// or the static allocated one, as default
Expand Down
25 changes: 1 addition & 24 deletions db/dbformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,30 +487,7 @@ class IterKey {
// shared_len: bytes in [0, shard_len-1] would be remained
// non_shared_data: data to be append, its length must be >= non_shared_len
void TrimAppend(const size_t shared_len, const char* non_shared_data,
const size_t non_shared_len) {
assert(shared_len <= key_size_);
size_t total_size = shared_len + non_shared_len;

if (IsKeyPinned() /* key is not in buf_ */) {
// Copy the key from external memory to buf_ (copy shared_len bytes)
EnlargeBufferIfNeeded(total_size);
memcpy(buf(), key_, shared_len);
} else if (total_size > buf_size_) {
// Need to allocate space, delete previous space
char* p = new char[total_size];
memcpy(p, key_, shared_len);

if (buf_size_ != sizeof(space_)) {
delete[] buf_;
}

buf_ = p;
buf_size_ = total_size;
}
memcpy(buf() + shared_len, non_shared_data, non_shared_len);
key_ = buf();
key_size_ = total_size;
}
const size_t non_shared_len);

Slice SetKey(const Slice& key, bool copy = true) {
// is_user_key_ expected to be set already via SetIsUserKey
Expand Down

0 comments on commit e8d4698

Please sign in to comment.