Implement GetUniqueId for Mac

Summary:
Persistent read cache relies on the accuracy of the GetUniqueIdFromFile
to generate a unique key for a given block of data. Currently we don't have an
implementation for Mac.

This patch adds an implementation.

Test Plan: Run unit tests

Reviewers: sdong

Subscribers: andrewkr, dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D58413
main
krad 9 years ago
parent 1f2dca0eaa
commit bb98ca3c80
  1. 25
      util/io_posix.cc
  2. 2
      util/io_posix.h

@ -132,7 +132,30 @@ static size_t GetUniqueIdFromFile(int fd, char* id, size_t max_size) {
assert(rid >= id); assert(rid >= id);
return static_cast<size_t>(rid - id); return static_cast<size_t>(rid - id);
} }
} // namespace
#endif
#if defined(OS_MACOSX)
namespace {
static size_t GetUniqueIdFromFile(int fd, char* id, size_t max_size) {
if (max_size < kMaxVarint64Length * 3) {
return 0;
}
struct stat buf;
int result = fstat(fd, &buf);
if (result == -1) {
return 0;
}
char* rid = id;
rid = EncodeVarint64(rid, buf.st_dev);
rid = EncodeVarint64(rid, buf.st_ino);
rid = EncodeVarint64(rid, buf.st_gen);
assert(rid >= id);
return static_cast<size_t>(rid - id);
} }
} // namespace
#endif #endif
/* /*
@ -181,7 +204,7 @@ Status PosixRandomAccessFile::Read(uint64_t offset, size_t n, Slice* result,
return s; return s;
} }
#ifdef OS_LINUX #if defined(OS_LINUX) || defined(OS_MACOSX)
size_t PosixRandomAccessFile::GetUniqueId(char* id, size_t max_size) const { size_t PosixRandomAccessFile::GetUniqueId(char* id, size_t max_size) const {
return GetUniqueIdFromFile(fd_, id, max_size); return GetUniqueIdFromFile(fd_, id, max_size);
} }

@ -56,7 +56,7 @@ class PosixRandomAccessFile : public RandomAccessFile {
virtual Status Read(uint64_t offset, size_t n, Slice* result, virtual Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const override; char* scratch) const override;
#ifdef OS_LINUX #if defined(OS_LINUX) || defined(OS_MACOSX)
virtual size_t GetUniqueId(char* id, size_t max_size) const override; virtual size_t GetUniqueId(char* id, size_t max_size) const override;
#endif #endif
virtual void Hint(AccessPattern pattern) override; virtual void Hint(AccessPattern pattern) override;

Loading…
Cancel
Save