Skip to content

Commit

Permalink
Rename grnxx::Bytes::ptr() to grnxx::Bytes::data().
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 28, 2013
1 parent dbe1eaa commit 9caf2b5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
36 changes: 18 additions & 18 deletions lib/grnxx/bytes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class Bytes {
// Trivial default constructor.
Bytes() = default;
// Create a reference to an empty (zero-size) sequence.
Bytes(nullptr_t) : ptr_(nullptr), size_(0) {}
Bytes(nullptr_t) : data_(nullptr), size_(0) {}
// Create a reference to a zero-terminated string.
Bytes(const char *str)
: ptr_(reinterpret_cast<const uint8_t *>(str)),
: data_(reinterpret_cast<const uint8_t *>(str)),
size_(std::strlen(str)) {}
// Create a reference to a sequence of bytes.
Bytes(const void *ptr, uint64_t size)
: ptr_(static_cast<const uint8_t *>(ptr)),
Bytes(const void *data, uint64_t size)
: data_(static_cast<const uint8_t *>(data)),
size_(size) {}

// Return true iff the sequence is not empty.
Expand All @@ -49,34 +49,34 @@ class Bytes {

// Skip the first "n" bytes and extract the subsequent "m" bytes.
Bytes extract(uint64_t n, uint64_t m) const {
return Bytes(ptr_ + n, m);
return Bytes(data_ + n, m);
}
// Remove the first "n" bytes and the last "m" bytes.
Bytes trim(uint64_t n, uint64_t m) const {
return Bytes(ptr_ + n, size_ - n - m);
return Bytes(data_ + n, size_ - n - m);
}

// Extract the first "n" bytes.
Bytes prefix(uint64_t n) const {
return Bytes(ptr_, n);
return Bytes(data_, n);
}
// Extract the last "n" bytes.
Bytes suffix(uint64_t n) const {
return Bytes(ptr_ + size_ - n, n);
return Bytes(data_ + size_ - n, n);
}

// Remove the first "n" bytes.
Bytes except_prefix(uint64_t n) const {
return Bytes(ptr_ + n, size_ - n);
return Bytes(data_ + n, size_ - n);
}
// Remove the last "n" bytes.
Bytes except_suffix(uint64_t n) const {
return Bytes(ptr_, size_ - n);
return Bytes(data_, size_ - n);
}

// Return true iff "*this" == "rhs".
bool operator==(const Bytes &rhs) const {
return (size_ == rhs.size_) && (std::memcmp(ptr_, rhs.ptr_, size_) == 0);
return (size_ == rhs.size_) && (std::memcmp(data_, rhs.data_, size_) == 0);
}
// Return true iff "*this" != "rhs".
bool operator!=(const Bytes &rhs) const {
Expand All @@ -85,7 +85,7 @@ class Bytes {
// Return true iff "*this" < "rhs".
bool operator<(const Bytes &rhs) const {
const uint64_t min_size = (size_ < rhs.size_) ? size_ : rhs.size_;
int result = std::memcmp(ptr_, rhs.ptr_, min_size);
int result = std::memcmp(data_, rhs.data_, min_size);
return (result < 0) || ((result == 0) && (size_ < rhs.size_));
}
// Return true iff "*this" > "rhs".
Expand All @@ -106,7 +106,7 @@ class Bytes {
// otherwise (if "*this" > "bytes").
int compare(const Bytes &bytes) const {
const uint64_t min_size = (size_ < bytes.size_) ? size_ : bytes.size_;
int result = std::memcmp(ptr_, bytes.ptr_, min_size);
int result = std::memcmp(data_, bytes.data_, min_size);
if (result != 0) {
return result;
}
Expand All @@ -124,23 +124,23 @@ class Bytes {

// Return the "i"-th byte.
uint8_t operator[](uint64_t i) const {
return ptr_[i];
return data_[i];
}
// Return the starting address.
const void *address() const {
return ptr_;
return data_;
}
// Return a pointer to the sequence.
const uint8_t *ptr() const {
return ptr_;
const uint8_t *data() const {
return data_;
}
// Return the number of bytes.
uint64_t size() const {
return size_;
}

private:
const uint8_t *ptr_;
const uint8_t *data_;
uint64_t size_;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/grnxx/map/bytes_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ bool BytesArray::create_array(Storage *storage, uint32_t storage_node_id,
header_ = static_cast<BytesArrayHeader *>(storage_node.body());
*header_ = BytesArrayHeader();
header_->default_value_size = default_value.size();
std::memcpy(header_ + 1, default_value.ptr(), default_value.size());
std::memcpy(header_ + 1, default_value.data(), default_value.size());
default_value_ = Value(header_ + 1, default_value.size());
ids_.reset(IDArray::create(storage, storage_node_id_,
BYTES_STORE_INVALID_BYTES_ID));
Expand Down
2 changes: 1 addition & 1 deletion lib/grnxx/map/bytes_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ bool BytesStoreImpl::add(ValueArg bytes, uint64_t *bytes_id) {
if (!page) {
return false;
}
std::memcpy(page + offset_in_page, bytes.ptr(), size);
std::memcpy(page + offset_in_page, bytes.data(), size);
*bytes_id = get_bytes_id(offset, size);
page_header->size_in_use += size;
if (offset == header_->next_offset) {
Expand Down
4 changes: 2 additions & 2 deletions lib/grnxx/storage/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,11 +1165,11 @@ char *StorageImpl::generate_path(uint16_t file_id) {
return nullptr;
}
if (has_extension) {
std::memcpy(path, prefix.ptr(), prefix.size() - 4);
std::memcpy(path, prefix.data(), prefix.size() - 4);
std::sprintf(path + prefix.size() - 4, "_%03d", file_id);
std::strcpy(path + prefix.size(), ".grn");
} else {
std::memcpy(path, prefix.ptr(), prefix.size());
std::memcpy(path, prefix.data(), prefix.size());
std::sprintf(path + prefix.size(), "_%03d", file_id);
}
return path;
Expand Down
2 changes: 1 addition & 1 deletion lib/grnxx/string_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ StringBuilder &operator<<(StringBuilder &builder, const void *value) {

StringBuilder &operator<<(StringBuilder &builder, const Bytes &bytes) {
// TODO: StringBuilder should support const uint_8 *.
return builder.append(reinterpret_cast<const char *>(bytes.ptr()),
return builder.append(reinterpret_cast<const char *>(bytes.data()),
bytes.size());
}

Expand Down
16 changes: 8 additions & 8 deletions test/test_bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ void test_extract() {
grnxx::Bytes part = bytes.extract(5, 0);

assert(!part);
assert(part.ptr() == (bytes.ptr() + 5));
assert(part.data() == (bytes.data() + 5));
assert(part.size() == 0);

part = bytes.extract(3, 5);

assert(part);
assert(part.ptr() == (bytes.ptr() + 3));
assert(part.data() == (bytes.data() + 3));
assert(part.size() == 5);
}

Expand All @@ -67,13 +67,13 @@ void test_trim() {
grnxx::Bytes part = bytes.extract(5, 0);

assert(!part);
assert(part.ptr() == (bytes.ptr() + 5));
assert(part.data() == (bytes.data() + 5));
assert(part.size() == 0);

part = bytes.extract(3, 5);

assert(part);
assert(part.ptr() == (bytes.ptr() + 3));
assert(part.data() == (bytes.data() + 3));
assert(part.size() == 5);
}

Expand All @@ -82,13 +82,13 @@ void test_prefix() {
grnxx::Bytes prefix = bytes.prefix(0);

assert(!prefix);
assert(prefix.ptr() == bytes.ptr());
assert(prefix.data() == bytes.data());
assert(prefix.size() == 0);

prefix = bytes.prefix(5);

assert(prefix);
assert(prefix.ptr() == bytes.ptr());
assert(prefix.data() == bytes.data());
assert(prefix.size() == 5);
}

Expand All @@ -97,13 +97,13 @@ void test_suffix() {
grnxx::Bytes suffix = bytes.suffix(0);

assert(!suffix);
assert(suffix.ptr() == (bytes.ptr() + 10));
assert(suffix.data() == (bytes.data() + 10));
assert(suffix.size() == 0);

suffix = bytes.suffix(5);

assert(suffix);
assert(suffix.ptr() == (bytes.ptr() + 5));
assert(suffix.data() == (bytes.data() + 5));
assert(suffix.size() == 5);
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void generate_random_keys(std::uint64_t num_keys,
keyset.clear();
while (keyset.size() < num_keys) {
const grnxx::Bytes key = generate_random_key<grnxx::Bytes>();
keyset.insert(std::string(reinterpret_cast<const char *>(key.ptr()),
keyset.insert(std::string(reinterpret_cast<const char *>(key.data()),
key.size()));
}
keys->clear();
Expand Down

0 comments on commit 9caf2b5

Please sign in to comment.