Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions python/fromdocs/connection/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

from grpc import RpcError

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should split the samples, not only for python but in the other languages as well, regarding deprecated login/logout and managed sessions

but we may do that change in another PR

from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)


def main():
client = ImmudbClient(URL)
# database parameter is optional
client.login(LOGIN, PASSWORD, database=DB)
client.logout()

# Bad login
try:
client.login("verybadlogin", "verybadpassword")
except RpcError as exception:
print(exception.debug_error_string())
print(exception.details())


# Managed session support
with client.openManagedSession(LOGIN, PASSWORD, database=DB) as session:
transaction = session.newTx()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for login/logout there were no actions, here it's executing sql stmts, we may keep just the session handling?

transaction.sqlExec("CREATE TABLE IF NOT EXISTS connectiontest1 (id INTEGER AUTO_INCREMENT, name VARCHAR[255], PRIMARY KEY(id))")
commited = transaction.commit()
print(commited) # If table was created exists - shows transaction informations

# Not managed session. You need to handle keep alive request yourself
session = client.openSession(LOGIN, PASSWORD, database=DB)
transaction = session.newTx()
transaction.sqlExec("CREATE TABLE IF NOT EXISTS connectiontest2 (id INTEGER AUTO_INCREMENT, name VARCHAR[255], PRIMARY KEY(id))")
commited = transaction.commit()
print(commited) # If table was created - shows transaction informations
client.closeSession()



if __name__ == "__main__":
main()
22 changes: 22 additions & 0 deletions python/fromdocs/kvoperations/dataexpiration/expire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from immudb import ImmudbClient
from datetime import datetime, timedelta
import time

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.expireableSet(b"TEST", b"test", datetime.now() + timedelta(seconds=3))
print(client.get(b"TEST")) # b"test"
time.sleep(4)
try:
print(client.get(b"TEST"))
except:
pass # Key not found, because it expires, raises Exception

if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions python/fromdocs/kvoperations/deleting/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from immudb import ImmudbClient
from immudb.datatypes import DeleteKeysRequest

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.set(b"immu", b"immudb-not-rulezz")
print(client.get(b"immu")) # b"immudb-not-rulezz"

deleteRequest = DeleteKeysRequest(keys = [b"immu"])
client.delete(deleteRequest)
print(client.get(b"immu")) # None

if __name__ == "__main__":
main()
41 changes: 41 additions & 0 deletions python/fromdocs/kvoperations/queriesandhistory/getreference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from immudb import ImmudbClient

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

references samples may be under it's own folder? or in secondary indexes?


URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.verifiedSet(b'x', b'1')
client.verifiedSet(b'y', b'1')
retrieved = client.verifiedGet(b'x')
print(retrieved.refkey) # Entry reference key (None)

client.verifiedSetReference(b'x', b'reference1')
client.setReference(b'x', b'reference2')
client.setReference(b'y', b'reference2')
client.verifiedSet(b'y', b'2')

retrieved = client.verifiedGet(b'reference1')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (b'reference1')
print(retrieved.verified) # Entry verification status (True)

retrieved = client.verifiedGet(b'reference2')
print(retrieved.key) # Entry key (b'y')
print(retrieved.refkey) # Entry reference key (b'reference2')
print(retrieved.verified) # Entry verification status (True)
print(retrieved.value) # Entry value (b'3')

retrieved = client.verifiedGet(b'x')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (None)
print(retrieved.verified) # Entry verification status (True)

retrieved = client.get(b'reference2')
print(retrieved.key) # Entry key (b'y')

if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions python/fromdocs/kvoperations/queriesandhistory/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)

client.set(b'test', b'1')
client.set(b'test', b'2')
client.set(b'test', b'3')

history = client.history(b'test', 0, 100, True) # List[immudb.datatypes.historyResponseItem]
responseItemFirst = history[0]
print(responseItemFirst.key) # Entry key (b'test')
print(responseItemFirst.value) # Entry value (b'3')
print(responseItemFirst.tx) # Transaction id

responseItemThird = history[2]
print(responseItemThird.key) # Entry key (b'test')
print(responseItemThird.value) # Entry value (b'1')
print(responseItemThird.tx) # Transaction id

if __name__ == "__main__":
main()
37 changes: 37 additions & 0 deletions python/fromdocs/kvoperations/queriesandhistory/scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
toSet = {
b"aaa": b'1',
b'bbb': b'2',
b'ccc': b'3',
b'acc': b'1',
b'aac': b'2',
b'aac:test1': b'3',
b'aac:test2': b'1',
b'aac:xxx:test': b'2'
}
client.setAll(toSet)

result = client.scan(b'', b'', True, 100) # All entries
print(result)
result = client.scan(b'', b'aac', True, 100) # All entries with prefix 'aac' including 'aac'
print(result)

# Seek key example (allows retrieve entries in proper chunks):
result = client.scan(b'', b'', False, 3)
while result:
for item, value in result.items():
print("SEEK", item, value)
lastKey = list(result.keys())[-1]
result = client.scan(lastKey, b'', False, 3)

if __name__ == "__main__":
main()
41 changes: 41 additions & 0 deletions python/fromdocs/kvoperations/queriesandhistory/setreference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
client.verifiedSet(b'x', b'1')
client.verifiedSet(b'y', b'1')
retrieved = client.verifiedGet(b'x')
print(retrieved.refkey) # Entry reference key (None)

client.verifiedSetReference(b'x', b'reference1')
client.setReference(b'x', b'reference2')
client.setReference(b'y', b'reference2')
client.verifiedSet(b'y', b'2')

retrieved = client.verifiedGet(b'reference1')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (b'reference1')
print(retrieved.verified) # Entry verification status (True)

retrieved = client.verifiedGet(b'reference2')
print(retrieved.key) # Entry key (b'y')
print(retrieved.refkey) # Entry reference key (b'reference2')
print(retrieved.verified) # Entry verification status (True)
print(retrieved.value) # Entry value (b'3')

retrieved = client.verifiedGet(b'x')
print(retrieved.key) # Entry key (b'x')
print(retrieved.refkey) # Entry reference key (None)
print(retrieved.verified) # Entry verification status (True)

retrieved = client.get(b'reference2')
print(retrieved.key) # Entry key (b'y')

if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions python/fromdocs/kvoperations/readwrites/getatrevision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from grpc import RpcError
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)
first = client.set(b'x', b'y') # Not important, just to demonstrate
# that it will not affect atRevision
# for other keys

key = b'immudb130130130'

client.set(key, b'111')
client.set(key, b'222')
client.set(key, b'333')

print(client.get(key, -2)) # b"111" - value on relative -2 point history
print(client.get(key, -1)) # b"222" - value on relative -1 point history
print(client.get(key, 0)) # b"333" - value on relative 0 (current) point history

print(client.get(key, 1)) # b"111" - value at first revision of key
print(client.get(key, 2)) # b"222" - value on second revision of key
print(client.get(key, 3)) # b"333" - value on third revision of key

try:
print(client.get(key, -20000))
except RpcError as error:
print(error.details()) # invalid key revision number

if __name__ == "__main__":
main()
40 changes: 40 additions & 0 deletions python/fromdocs/kvoperations/readwrites/getatx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from immudb import ImmudbClient

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def main():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sample doesn't correspond to getAtTx but to getTxById

also, I'm curious about the get api, how it's differentiating from getAtRevision... in java sdk even with param overloading it was better to have a dedicated method get, getAtRevision, getAtTx ...

client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)

keyFirst = b'333'
keySecond = b'555'

first = client.set(keyFirst, b'111')
firstTransaction = first.id

second = client.set(keySecond, b'222')
secondTransaction = second.id

toSet = {
b'1': b'test1',
b'2': b'test2',
b'3': b'test3'
}

third = client.setAll(toSet)
thirdTransaction = third.id

keysAtFirst = client.txById(firstTransaction)
keysAtSecond = client.txById(secondTransaction)
keysAtThird = client.txById(thirdTransaction)

print(keysAtFirst) # [b'333']
print(keysAtSecond) # [b'555']
print(keysAtThird) # [b'1', b'2', b'3']


if __name__ == "__main__":
main()
70 changes: 70 additions & 0 deletions python/fromdocs/kvoperations/readwrites/getset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from immudb import ImmudbClient
import json

URL = "localhost:3322" # immudb running on your machine
LOGIN = "immudb" # Default username
PASSWORD = "immudb" # Default password
DB = b"defaultdb" # Default database name (must be in bytes)

def encode(what: str):
return what.encode("utf-8")

def decode(what: bytes):
return what.decode("utf-8")

def main():
client = ImmudbClient(URL)
client.login(LOGIN, PASSWORD, database = DB)

# You have to operate on bytes
setResult = client.set(b'x', b'y')
print(setResult) # immudb.datatypes.SetResponse
print(setResult.id) # id of transaction
print(setResult.verified) # in this case verified = False
# see Tamperproof reading and writing

# Also you get response in bytes
retrieved = client.get(b'x')
print(retrieved) # immudb.datatypes.GetResponse
print(retrieved.key) # Value is b'x'
print(retrieved.value) # Value is b'y'
print(retrieved.tx) # Transaction number

print(type(retrieved.key)) # <class 'bytes'>
print(type(retrieved.value)) # <class 'bytes'>

# Operating with strings
encodedHello = encode("Hello")
encodedImmutable = encode("Immutable")
client.set(encodedHello, encodedImmutable)
retrieved = client.get(encodedHello)

print(decode(retrieved.value) == "Immutable") # Value is True

notExisting = client.get(b'asdasd')
print(notExisting) # Value is None


# JSON example
toSet = {"hello": "immutable"}
encodedToSet = encode(json.dumps(toSet))
client.set(encodedHello, encodedToSet)

retrieved = json.loads(decode(client.get(encodedHello).value))
print(retrieved) # Value is {"hello": "immutable"}

# setAll example - sets all keys to value from dictionary
toSet = {
b'1': b'test1',
b'2': b'test2',
b'3': b'test3'
}

client.setAll(toSet)
retrieved = client.getAll(list(toSet.keys()))
print(retrieved)
# Value is {b'1': b'test1', b'2': b'test2', b'3': b'test3'}


if __name__ == "__main__":
main()
Loading