Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python examples #143

Merged
merged 4 commits into from
Apr 19, 2022
Merged
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
33 changes: 30 additions & 3 deletions src/master/develop/connection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Connection and authentication

The immudb server runs on port 3323 as the default. The code examples below illustrate how to connect your client to the server and authenticate using default options and the default username and password.
The immudb server runs on port 3322 as the default. The code examples below illustrate how to connect your client to the server and authenticate using default options and the default username and password.
Razikus marked this conversation as resolved.
Show resolved Hide resolved
You can modify defaults on the immudb server in `immudb.toml` in the config folder.
:::: tabs

Expand Down Expand Up @@ -51,8 +51,35 @@ immuClient.login("immudb", "immudb");
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)

```python
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)
# database parameter is optional
client.login(LOGIN, PASSWORD, database=DB)
Copy link
Contributor

Choose a reason for hiding this comment

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

In immudb 1.2 we've introduced new session mechanism - @mabmayer is it used for this login functionality?

If not then we should promote the new session-based approach (that is necessary for opening SQL transactions)

Choose a reason for hiding this comment

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

@byo @tomekkolo No session mechanism right now supported.

client.logout()

# Bad login
try:
client.login("verybadlogin", "verybadpassword")
except RpcError as exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

An idea for future improvement: We should detect what error it is - like here that credentials are incorrect. There should be data sent along the error code from the backend in the details - it could be used to convert to appropriate specific exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely it should cast to some normal Exception

Unfortunately from my investigation on it - this exception have mainly 3 "normal" fields now

debug_error_string {"created":"@1649922703.326925387","description":"Error received from peer ipv6:[::1]:3322","file":"src/core/lib/surface/call.cc","file_line":903,"grpc_message":"invalid user name or password","grpc_status":2}

details invalid user name or password

code StatusCode.UNKNOWN - it matches internal codes from GRPC, and not the code provided by server (08004)

I looked depper into it and i see that the proper status code is in some unparsed trailing_metadata field
(_Metadatum(key='grpc-status-details-bin', value=b'\x08\x02\x12\x1dinvalid user name or password\x1aE\n+type.googleapis.com/immudb.schema.ErrorInfo\x12\x16\n\x0508004\x12\rkey not found\x1a-\n+type.googleapis.com/immudb.schema.RetryInfo'),)

.....schema.ErrorInfo\x12\x16\n\x05------->08004<-------\x12\rkey not found\x1a-\n+type.google......

I will write an issue also

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link

@mabmayer mabmayer Apr 18, 2022

Choose a reason for hiding this comment

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

@tomekkolo Last year, I suggested immudb should report error codes. Error codes are used by many solutions in different areas, are language agnostic and are extensible. I remember Duncan worked on that and created a proposal. Can we continue where he stopped?

print(exception.debug_error_string())
print(exception.details())


if __name__ == "__main__":
main()

```
:::

::: tab Node.js
Expand Down
166 changes: 158 additions & 8 deletions src/master/develop/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,39 @@ Note that, similar with many other methods, `history` method is overloaded to al
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)

Python immudb sdk currently doesn't support `SinceTx` parameter
Razikus marked this conversation as resolved.
Show resolved Hide resolved

```python
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()
```
:::

::: tab Node.js
Expand Down Expand Up @@ -218,8 +249,45 @@ List<KV> scanResult = immuClient.scan("scan", 1, 5, false);
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
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()
```
:::

::: tab Node.js
Expand Down Expand Up @@ -410,8 +478,49 @@ try {
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
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()
```
:::

::: tab Node.js
Expand Down Expand Up @@ -538,8 +647,49 @@ Do you want to make a feature request or help out? Open an issue on [Java sdk gi
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
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()
```
:::

::: tab Node.js
Expand Down
31 changes: 29 additions & 2 deletions src/master/develop/indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,35 @@ List<KV> zScan2 = immuClient.zScan("set2", 5, false);
:::

::: tab Python
This feature is not yet supported or not documented.
Do you want to make a feature request or help out? Open an issue on [Python sdk github project](https://github.com/codenotary/immudb-py/issues/new)
```python
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"user1", b"[email protected]")
client.set(b"user2", b"[email protected]")
client.set(b"user3", b"[email protected]")
client.set(b"user4", b"[email protected]")

client.zAdd(b"age", 100, b"user1")
client.zAdd(b"age", 101, b"user2")
client.zAdd(b"age", 99, b"user3")
client.zAdd(b"age", 100, b"user4")

scanResult = client.zScan(b"age", b"", 0, 0, True, 50, False, 100, 101)
print(scanResult) # Shows records with 'age' 100 <= score < 101
# with descending order and limit = 50


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

::: tab Node.js
Expand Down
Loading