API Reference
get_snapshot_of

get_snapshot_of()

Retrieves the Snapshot instance from which a given object was originally fetched.

This is useful when you have an object instance and need to perform further queries (e.g., checking tags or relationships) within the context of the exact point-in-time state from which that object version was loaded.


Parameters

NameTypeDescription
objobjectRequired. An object instance previously fetched from a snapshot.

Returns

Returns the Snapshot object corresponding to the state from which obj was loaded. You can then use this snapshot object to perform further read-only operations like fetch() or find().


Example

Let's say you take two snapshots over time. An object obj exists in both, but its tags change between snapshots. If you fetch both versions, get_snapshot_of() lets you retrieve the correct snapshot for each version to inspect its tags accurately.

# Create an object and commit it with "tag1"
obj = MemoTestClass(123)
db0.tags(obj).add("tag1")
db0.commit()
state_1 = db0.get_state_num(finalized=True)
 
# In a new transaction, add "tag2" and commit
db0.tags(obj).add("tag2")
db0.commit()
state_2 = db0.get_state_num(finalized=True)
 
# Create snapshots for both states and retain them
snap_1 = db0.snapshot(state_1)
snap_2 = db0.snapshot(state_2)
 
# Fetch the different versions of the object
ver_1 = snap_1.fetch(db0.uuid(obj))
ver_2 = snap_2.fetch(db0.uuid(obj))
 
# Retrieve the specific snapshot for each object version
origin_snap_1 = db0.get_snapshot_of(ver_1)
origin_snap_2 = db0.get_snapshot_of(ver_2)
 
# Check tags in the context of the first snapshot
# The first version only has "tag1"
assert len(origin_snap_1.find(obj, "tag1")) == 1
assert len(origin_snap_1.find(obj, "tag2")) == 0
 
# Check tags in the context of the second snapshot
# The second version has both "tag1" and "tag2"
assert len(origin_snap_2.find(obj, "tag1")) == 1
assert len(origin_snap_2.find(obj, "tag2")) == 1