Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 119a6d7

Browse files
authored
Implement native calls from Python Driver to Rust Driver (#304)
## What is the goal of this PR? We replace the direct implementation of the client side of the TypeDB protocol in Python with native calls to the Rust client over FFI. ## What are the changes implemented in this PR? - merge `TypeDBClient` and `ClusterClient` into single `Client`, following the protocol merge; - remove `Remote` variants of `Concept`s; Concept API calls now require a `Transaction` to be provided; - make `Value` "generic" over its value type, which allows us to collapse all the `Attribute.<Type>`s into a single `Attribute` class; - associate each top-level class (`Client`, `Session`, `Concept`, etc.) with its native representation on the Rust side; - replace Python method implementations with calls using the native representation over FFI.
1 parent cd69e3a commit 119a6d7

File tree

139 files changed

+3341
-6368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+3341
-6368
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.1.1
1+
6.2.0

BUILD

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,27 @@ load("@vaticle_dependencies//distribution:deployment.bzl", "deployment")
3434
load(":deployment.bzl", github_deployment = "deployment")
3535

3636

37+
genrule(
38+
name = "native-client-wrapper",
39+
outs = ["typedb/native_client_wrapper.py"],
40+
srcs = ["@vaticle_typedb_driver_java//rust:native_client_python_wrapper"],
41+
cmd = "cp $< $@",
42+
visibility = ["//visibility:public"]
43+
)
44+
45+
genrule(
46+
name = "native-client-binary",
47+
outs = ["typedb/native_client_python.so"],
48+
srcs = ["@vaticle_typedb_driver_java//rust:native_client_python"],
49+
cmd = "cp $< $@",
50+
visibility = ["//visibility:public"]
51+
)
52+
3753
py_library(
3854
name = "client_python",
39-
srcs = glob(["typedb/**/*.py"]),
40-
deps = [
41-
vaticle_typedb_client_python_requirement("typedb-protocol"),
42-
vaticle_typedb_client_python_requirement("protobuf"),
43-
vaticle_typedb_client_python_requirement("grpcio"),
44-
],
55+
srcs = glob(["typedb/**/*.py"]) + [":native-client-wrapper"],
56+
data = [":native-client-binary"],
57+
deps = ["@vaticle_typedb_driver_java//rust:native_client_python_wrapper"],
4558
visibility = ["//visibility:public"]
4659
)
4760

@@ -80,7 +93,7 @@ assemble_pip(
8093
"Environment :: Console",
8194
"Topic :: Database :: Front-Ends"
8295
],
83-
url = "https://github.com/vaticle/typedb-client-python/",
96+
url = "https://github.com/vaticle/typedb-driver-python/",
8497
author = "Vaticle",
8598
author_email = "[email protected]",
8699
license = "Apache-2.0",

WORKSPACE

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ workspace(name = "vaticle_typedb_client_python")
2828
load("//dependencies/vaticle:repositories.bzl", "vaticle_dependencies")
2929
vaticle_dependencies()
3030

31+
# Load //builder/python
32+
load("@vaticle_dependencies//builder/python:deps.bzl", python_deps = "deps")
33+
python_deps()
34+
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
35+
py_repositories()
36+
python_register_toolchains(name = "python39", python_version = "3.9")
37+
3138
# Load //builder/bazel for RBE
3239
load("@vaticle_dependencies//builder/bazel:deps.bzl", "bazel_toolchain")
3340
bazel_toolchain()
@@ -44,26 +51,43 @@ kotlin_repositories()
4451
load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
4552
kt_register_toolchains()
4653

47-
# Load //builder/python
48-
load("@vaticle_dependencies//builder/python:deps.bzl", python_deps = "deps")
49-
python_deps()
54+
# Load //builder/rust
55+
load("@vaticle_dependencies//builder/rust:deps.bzl", rust_deps = "deps")
56+
rust_deps()
57+
58+
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
59+
rules_rust_dependencies()
60+
rust_register_toolchains(edition = "2021", include_rustc_srcs = True)
61+
62+
load("@vaticle_dependencies//library/crates:crates.bzl", "fetch_crates")
63+
fetch_crates()
64+
load("@crates//:defs.bzl", "crate_repositories")
65+
crate_repositories()
5066

5167
# Load //builder/grpc
5268
load("@vaticle_dependencies//builder/grpc:deps.bzl", grpc_deps = "deps")
5369
grpc_deps()
54-
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl",
55-
com_github_grpc_grpc_deps = "grpc_deps")
70+
71+
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", com_github_grpc_grpc_deps = "grpc_deps")
5672
com_github_grpc_grpc_deps()
73+
load("@stackb_rules_proto//java:deps.bzl", "java_grpc_compile")
74+
java_grpc_compile()
5775

5876
# Load //tool/common
5977
load("@vaticle_dependencies//tool/common:deps.bzl", "vaticle_dependencies_ci_pip",
60-
vaticle_dependencies_tool_maven_artifacts = "maven_artifacts")
78+
vaticle_dependencies_tool_maven_artifacts = "maven_artifacts")
6179
vaticle_dependencies_ci_pip()
80+
load("@vaticle_dependencies_ci_pip//:requirements.bzl", "install_deps")
81+
install_deps()
6282

6383
# Load //tool/checkstyle
6484
load("@vaticle_dependencies//tool/checkstyle:deps.bzl", checkstyle_deps = "deps")
6585
checkstyle_deps()
6686

87+
# Load //tool/swig
88+
load("@vaticle_dependencies//tool/swig:deps.bzl", swig_deps = "deps")
89+
swig_deps()
90+
6791
######################################
6892
# Load @vaticle_bazel_distribution #
6993
######################################
@@ -80,33 +104,40 @@ rules_pkg_dependencies()
80104
# Load //pip
81105
load("@vaticle_bazel_distribution//pip:deps.bzl", pip_deps = "deps")
82106
pip_deps()
107+
load("@vaticle_bazel_distribution_pip//:requirements.bzl", "install_deps")
108+
install_deps()
83109

84110
# Load //github
85111
load("@vaticle_bazel_distribution//github:deps.bzl", github_deps = "deps")
86112
github_deps()
87113

88-
################################
114+
##############################
89115
# Load @vaticle dependencies #
90-
################################
116+
##############################
91117

92-
load("//dependencies/vaticle:repositories.bzl", "vaticle_typedb_common", "vaticle_typedb_behaviour")
93-
vaticle_typedb_common()
118+
load("//dependencies/vaticle:repositories.bzl", "vaticle_typedb_behaviour", "vaticle_typedb_common", "vaticle_typedb_driver_java", "vaticle_typedb_protocol", "vaticle_typeql")
94119
vaticle_typedb_behaviour()
120+
vaticle_typedb_common()
121+
vaticle_typedb_driver_java()
122+
vaticle_typedb_protocol()
123+
vaticle_typeql()
95124

96125
# Load artifacts
97126
load("//dependencies/vaticle:artifacts.bzl", "vaticle_typedb_artifacts", "vaticle_typedb_cluster_artifacts")
98127
vaticle_typedb_artifacts()
99128
vaticle_typedb_cluster_artifacts()
100129

101-
#################################
130+
######################################
102131
# Load @vaticle_typedb_client_python #
103-
#################################
132+
######################################
104133

105-
load("@rules_python//python:pip.bzl", "pip_install")
106-
pip_install(
134+
load("@rules_python//python:pip.bzl", "pip_parse")
135+
pip_parse(
107136
name = "vaticle_typedb_client_python_pip",
108-
requirements = "//:requirements_dev.txt",
137+
requirements_lock = "//:requirements_dev.txt",
109138
)
139+
load("@vaticle_typedb_client_python_pip//:requirements.bzl", "install_deps")
140+
install_deps()
110141

111142
############################
112143
# Load @maven dependencies #
@@ -115,9 +146,9 @@ pip_install(
115146
load("@vaticle_dependencies//library/maven:rules.bzl", "maven")
116147
maven(vaticle_dependencies_tool_maven_artifacts)
117148

118-
##################################################
149+
#######################################################
119150
# Create @vaticle_typedb_client_python_workspace_refs #
120-
##################################################
151+
#######################################################
121152
load("@vaticle_bazel_distribution//common:rules.bzl", "workspace_refs")
122153
workspace_refs(
123154
name = "vaticle_typedb_client_python_workspace_refs"

dependencies/vaticle/artifacts.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def vaticle_typedb_artifacts():
2929
artifact_name = "typedb-server-{platform}-{version}.{ext}",
3030
tag_source = deployment["artifact.release"],
3131
commit_source = deployment["artifact.snapshot"],
32-
tag = "2.19.0",
32+
commit = "7de728a6cc51273299a041608fd589bb1f1ff0ff",
3333
)
3434

3535
def vaticle_typedb_cluster_artifacts():
@@ -39,5 +39,5 @@ def vaticle_typedb_cluster_artifacts():
3939
artifact_name = "typedb-cluster-all-{platform}-{version}.{ext}",
4040
tag_source = deployment_private["artifact.release"],
4141
commit_source = deployment_private["artifact.snapshot"],
42-
tag = "2.19.0",
42+
commit = "9f26c8967c2e1a7fcbd72b819b48c1f7f67c78e8",
4343
)

dependencies/vaticle/repositories.bzl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def vaticle_dependencies():
2525
git_repository(
2626
name = "vaticle_dependencies",
2727
remote = "https://github.com/vaticle/dependencies",
28-
commit = "51fdda8c935d5f9fc34bfdf41b0a6ac1ca77e2ee", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_dependencies
28+
commit = "cbdaf0134a33eaa71bfe0edb9e4878250ec9cdc0", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_dependencies
2929
)
3030

3131
def vaticle_typedb_common():
@@ -35,10 +35,37 @@ def vaticle_typedb_common():
3535
tag = "2.18.0" # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_common
3636
)
3737

38+
def vaticle_typeql():
39+
git_repository(
40+
name = "vaticle_typeql",
41+
remote = "https://github.com/dmikhalin/typeql",
42+
commit = "c3ab12f7e96079bb5dc6d7e962e5270fec4d976e", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typeql
43+
)
44+
45+
def vaticle_typedb_protocol():
46+
git_repository(
47+
name = "vaticle_typedb_protocol",
48+
remote = "https://github.com/vaticle/typedb-protocol",
49+
commit = "444dc87520160fd4385f47617b98df6d7e741184", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_protocol
50+
)
51+
3852
def vaticle_typedb_behaviour():
3953
git_repository(
4054
name = "vaticle_typedb_behaviour",
4155
remote = "https://github.com/vaticle/typedb-behaviour",
42-
commit = "90b4addcd71a398e9e52e322021c49310e66a47a" # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_behaviour
56+
commit = "26f4a19a797203c2eeb0682b6f7d25e238b97345", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_typedb_behaviour
4357
)
4458

59+
def vaticle_factory_tracing():
60+
git_repository(
61+
name = "vaticle_factory_tracing",
62+
remote = "https://github.com/vaticle/factory-tracing",
63+
tag = "2.12.0" # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_factory_tracing
64+
)
65+
66+
def vaticle_typedb_driver_java():
67+
git_repository(
68+
name = "vaticle_typedb_driver_java",
69+
remote = "https://github.com/vaticle/typedb-driver-java",
70+
commit = "c162f8a58e9c70e03517d5b854acb8b055e30bc5" # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_factory_tracing
71+
)

requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,4 @@
3535

3636
## Dependencies
3737

38-
typedb-protocol==2.18.0
39-
grpcio>=1.43.0,<2
40-
protobuf>=3.15.6,<4
4138
parse==1.18.0

tests/behaviour/background/cluster/environment.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
def before_all(context: Context):
3232
environment_base.before_all(context)
3333
context.credential_root_ca_path = os.environ["ROOT_CA"]
34-
context.setup_context_client_fn = lambda user="admin", password="password": setup_context_client(context, user, password)
34+
context.setup_context_client_fn = lambda user="admin", password="password": \
35+
setup_context_client(context, user, password)
3536

3637

3738
def before_scenario(context: Context, scenario):
@@ -43,23 +44,24 @@ def before_scenario(context: Context, scenario):
4344

4445

4546
def setup_context_client(context, username, password):
46-
credential = TypeDBCredential(username, password, context.credential_root_ca_path)
47-
context.client = TypeDB.cluster_client(addresses=["127.0.0.1:" + context.config.userdata["port"]], credential=credential)
48-
context.session_options = TypeDBOptions.cluster().set_infer(True)
49-
context.transaction_options = TypeDBOptions.cluster().set_infer(True)
47+
credential = TypeDBCredential(username, password, tls_root_ca_path=context.credential_root_ca_path)
48+
context.client = TypeDB.cluster_client(addresses=["localhost:" + context.config.userdata["port"]],
49+
credential=credential)
50+
context.session_options = TypeDBOptions(infer=True)
51+
context.transaction_options = TypeDBOptions(infer=True)
5052

5153

5254
def after_scenario(context: Context, scenario):
5355
environment_base.after_scenario(context, scenario)
5456

5557
# TODO: reset the database through the TypeDB runner once it exists
5658
context.setup_context_client_fn()
57-
for database in context.client.databases().all():
59+
for database in context.client.databases.all():
5860
database.delete()
5961

60-
for user in context.client.users().all():
62+
for user in context.client.users.all():
6163
if user.username() != "admin":
62-
context.client.users().delete(user.username())
64+
context.client.users.delete(user.username())
6365
context.client.close()
6466

6567

tests/behaviour/background/core/environment.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ def setup_context_client(context, username=None, password=None):
4444
if username is not None or password is not None:
4545
raise Exception("Core client does not support authentication")
4646
context.client = TypeDB.core_client(address="localhost:%d" % int(context.config.userdata["port"]))
47-
context.session_options = TypeDBOptions.core().set_infer(True)
48-
context.transaction_options = TypeDBOptions.core().set_infer(True)
47+
context.session_options = TypeDBOptions(infer=True)
48+
context.transaction_options = TypeDBOptions(infer=True)
4949

5050

5151
def after_scenario(context: Context, scenario):
5252
environment_base.after_scenario(context, scenario)
5353

5454
# TODO: reset the database through the TypeDB runner once it exists
5555
context.setup_context_client_fn()
56-
for database in context.client.databases().all():
56+
for database in context.client.databases.all():
5757
database.delete()
5858
context.client.close()
5959

60+
6061
def after_all(context: Context):
6162
environment_base.after_all(context)

tests/behaviour/background/environment_base.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# specific language governing permissions and limitations
1919
# under the License.
2020
#
21+
2122
from behave.model_core import Status
2223
from typedb.client import *
2324

@@ -44,8 +45,8 @@ def before_scenario(context: Context):
4445
context.get_thing_type = lambda root_label, type_label: _get_thing_type_impl(context, root_label, type_label)
4546
context.clear_answers = lambda: _clear_answers_impl(context)
4647
context.option_setters = {
47-
"session-idle-timeout-millis": lambda option, value: option.set_session_idle_timeout_millis(int(value)),
48-
"transaction-timeout-millis": lambda option, value: option.set_transaction_timeout_millis(int(value)),
48+
"session-idle-timeout-millis": lambda options, value: setattr(options, "session_idle_timeout_millis", value),
49+
"transaction-timeout-millis": lambda options, value: setattr(options, "transaction_timeout_millis", value),
4950
}
5051

5152

@@ -55,13 +56,11 @@ def _put_impl(context: Context, variable: str, thing: Thing):
5556

5657
def _get_thing_type_impl(context: Context, root_label: RootLabel, type_label: str):
5758
if root_label == RootLabel.ENTITY:
58-
return context.tx().concepts().get_entity_type(type_label)
59+
return context.tx().concepts.get_entity_type(type_label)
5960
elif root_label == RootLabel.ATTRIBUTE:
60-
return context.tx().concepts().get_attribute_type(type_label)
61+
return context.tx().concepts.get_attribute_type(type_label)
6162
elif root_label == RootLabel.RELATION:
62-
return context.tx().concepts().get_relation_type(type_label)
63-
elif root_label == RootLabel.THING:
64-
return context.tx().concepts().get_root_thing_type()
63+
return context.tx().concepts.get_relation_type(type_label)
6564
else:
6665
raise ValueError("Unrecognised value")
6766

tests/behaviour/concept/serialization/json/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedb_behaviour_py_test(
4848
"//tests/behaviour/util:util",
4949
"//tests/behaviour/config:parameters",
5050
"//tests/behaviour/background",
51+
"//:native-client-binary",
5152
],
5253
native_typedb_artifact = "//tests:native-typedb-artifact",
5354
native_typedb_cluster_artifact = "//tests:native-typedb-cluster-artifact",

0 commit comments

Comments
 (0)