Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ofriedma committed Jul 18, 2023
1 parent b6d6717 commit b8f232c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
15 changes: 4 additions & 11 deletions db/db_impl/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3717,10 +3717,9 @@ SnapshotImpl* DBImpl::GetSnapshotImpl(bool is_write_conflict_boundary,
if (!is_snapshot_supported_) {
return nullptr;
}
int64_t unix_time = 0;
SnapshotImpl* s = new SnapshotImpl;
#ifdef SPEEDB_SNAP_OPTIMIZATION
if (RefSnapshot(unix_time, is_write_conflict_boundary, s)) {
if (RefSnapshot(is_write_conflict_boundary, s)) {
return s;
}
#endif
Expand All @@ -3730,14 +3729,7 @@ SnapshotImpl* DBImpl::GetSnapshotImpl(bool is_write_conflict_boundary,
} else {
mutex_.AssertHeld();
}
// returns null if the underlying memtable does not support snapshot.
if (!is_snapshot_supported_) {
if (lock) {
mutex_.Unlock();
}
delete s;
return nullptr;
}
int64_t unix_time = 0;
immutable_db_options_.clock->GetCurrentTime(&unix_time)
.PermitUncheckedError(); // Ignore error
auto snapshot_seq = GetLastPublishedSequence();
Expand Down Expand Up @@ -3890,12 +3882,13 @@ bool DBImpl::UnRefSnapshot(const SnapshotImpl* snapshot,
return false;
}

bool DBImpl::RefSnapshot(int64_t unix_time, bool is_write_conflict_boundary,
bool DBImpl::RefSnapshot(bool is_write_conflict_boundary,
SnapshotImpl* snapshot) {
std::shared_ptr<SnapshotImpl> shared_snap = snapshots_.last_snapshot_;
if (shared_snap &&
shared_snap->GetSequenceNumber() == GetLastPublishedSequence() &&
shared_snap->is_write_conflict_boundary_ == is_write_conflict_boundary) {
int64_t unix_time;
immutable_db_options_.clock->GetCurrentTime(&unix_time)
.PermitUncheckedError(); // Ignore error
snapshot->cached_snapshot = shared_snap;
Expand Down
2 changes: 1 addition & 1 deletion db/db_impl/db_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class DBImpl : public DB {
// Returns true if the snapshot has not been deleted from SnapshotList
bool UnRefSnapshot(const SnapshotImpl* snapshot, bool& is_cached_snapshot);
// true if the snapshot provided has been referenced, otherwise false
bool RefSnapshot(int64_t unix_time, bool is_write_conflict_boundary,
bool RefSnapshot(bool is_write_conflict_boundary,
SnapshotImpl* snapshot);
virtual void ReleaseSnapshot(const Snapshot* snapshot) override;
// Create a timestamped snapshot. This snapshot can be shared by multiple
Expand Down
26 changes: 9 additions & 17 deletions db/snapshot_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ class SnapshotList;
// Each SnapshotImpl corresponds to a particular sequence number.
class SnapshotImpl : public Snapshot {
public:
int64_t unix_time_;
uint64_t timestamp_;
// Will this snapshot be used by a Transaction to do write-conflict checking?
bool is_write_conflict_boundary_;

#ifdef SPEEDB_SNAP_OPTIMIZATION
std::atomic_uint64_t refcount = {1};
std::shared_ptr<SnapshotImpl> cached_snapshot = nullptr;

struct Deleter {
inline void operator()(SnapshotImpl* snap) const;
};
int64_t unix_time_;
uint64_t timestamp_;
// Will this snapshot be used by a Transaction to do write-conflict checking?
bool is_write_conflict_boundary_;
#endif
SequenceNumber number_; // const after creation
// It indicates the smallest uncommitted data at the time the snapshot was
Expand All @@ -56,14 +58,7 @@ class SnapshotImpl : public Snapshot {
SnapshotImpl* next_;

SnapshotList* list_; // just for sanity checks
#ifndef SPEEDB_SNAP_OPTIMIZATION
int64_t unix_time_;

uint64_t timestamp_;

// Will this snapshot be used by a Transaction to do write-conflict checking?
bool is_write_conflict_boundary_;
#endif
};

class SnapshotList {
Expand Down Expand Up @@ -122,7 +117,7 @@ class SnapshotList {

SnapshotImpl* New(SnapshotImpl* s, SequenceNumber seq, uint64_t unix_time,
bool is_write_conflict_boundary,
uint64_t ts = std::numeric_limits<uint64_t>::max()) {
uint64_t ts = std::numeric_limits<uint64_t>::max(), const DB* db_impl = nullptr) {
#ifdef SPEEDB_SNAP_OPTIMIZATION
std::unique_lock<std::mutex> l(lock);
logical_count_.fetch_add(1);
Expand Down Expand Up @@ -236,20 +231,17 @@ class SnapshotList {
}
}

// How many snapshots in the SnapshotList
uint64_t count() const { return count_; }
#ifdef SPEEDB_SNAP_OPTIMIZATION
// changing count_ always under snapshot_list mutex
uint64_t count_;
// How many snapshots in the system included those that created refcount
uint64_t logical_count() const { return logical_count_; }
std::atomic_uint64_t logical_count_;
#endif

private:
// Dummy head of doubly-linked list of snapshots
SnapshotImpl list_;
#ifndef SPEEDB_SNAP_OPTIMIZATION
uint64_t count_;
#endif

};

// All operations on TimestampedSnapshotList must be protected by db mutex.
Expand Down

0 comments on commit b8f232c

Please sign in to comment.