Find an Object State
On this page: A how-to guide to find an object state at a given time in Python.

Steps

Step 1. Connect to a database

Connect to an existing database with multiple commit activities.

Code: Connect to an existing database

1
import os
2
import time
3
import datetime
4
from terminusdb_client import WOQLClient
5
from terminusdb_client import WOQLQuery as WOQL
6
7
server_url = "http://127.0.0.1:6363"
8
db = "seshat"
9
user = "admin"
10
account = "admin"
11
key = "root"
12
client = WOQLClient(server_url)
13
14
client.connect(user=user, account=account, key=key, db=db)
Copied!

Step 2. Get the initial commit

Get the timestamp of the last commit before the date specified in date_string, using the timetuple() method.

Code: Get the timestamp of a commit

1
date_string = "14/10/2020"
2
timestamp = time.mktime(datetime.datetime.strptime(date_string, "%d/%m/%Y").timetuple())
Copied!

Step 3. Get the subsequent commit

Get the next commit after the timestamp retrieved in step 2 and store commit data in commit_query.

Code: Get the details of the subsequent commit

1
commit_query = WOQL().using("admin/seshat/local/_commits",
2
WOQL().limit(1,
3
WOQL.woql_and(
4
WOQL().triple("v:Branch", "ref:branch_name", "v2"),
5
WOQL().triple("v:Branch", "ref:ref_commit", " v:Head"),
6
WOQL().path("v:Head", "ref:commit_parent+", "v:Tail", "v:Path"),
7
WOQL().triple("v:Tail", "ref:commit_id", "v:TailID"),
8
WOQL().triple("v:Tail", "ref:commit_timestamp", "v:TimeStamp"),
9
WOQL().greater(timestamp, "v:TimeStamp"),
10
)))
Copied!

Step 4. Get the commit identifier

Get the commit identifier into variable commit_id after running the commit_query defined in step 3.

Code: Get the commit id

1
results = client.query(commit_query)
2
commit_id = results['bindings'][0]['TailID']['@value']
Copied!

Step 5. Get the commit object state

Get the commit object associated with the commit_id obtained in step 4. Note the full path of the commit_id.

Code: Get the commit object state

1
path = f"admin/seshat/local/commit/{commit_id}"
2
3
object_query = WOQL().using(path,
4
WOQL().read_object("terminusdb:///data/afghazn", "v:Document"))
5
6
results = client.query(object_query)
7
8
print(results)
Copied!