Skip to content

Commit

Permalink
Partial go driver implementation (#663)
Browse files Browse the repository at this point in the history
## Usage and product changes
Implement Basic core driver functionality of creating and closing a
database.

## Implementation
Implement User and Database interfaces, and Driver. 
Left a few TODO comments, for user functions, session functions and some
iterator work where we need to use swig commands.

---------

Co-authored-by: Georgii Novoselov <[email protected]>
  • Loading branch information
nickan2c and farost authored Jun 28, 2024
1 parent de01566 commit b45116c
Show file tree
Hide file tree
Showing 30 changed files with 1,246 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ See the table below for links to README files, documentation, and source code.
| Java | [README](https://github.com/vaticle/typedb-driver/tree/development/java/README.md) | [Documentation](https://typedb.com/docs/drivers/java/overview) | [`java/`](https://github.com/vaticle/typedb-driver/tree/development/java) |
| C | [README](https://github.com/vaticle/typedb-driver/tree/development/c/README.md) | See C++ | [`c/`](https://github.com/vaticle/typedb-driver/tree/development/c) |
| C++ | [README](https://github.com/vaticle/typedb-driver/tree/development/cpp/README.md) | [Documentation](https://typedb.com/docs/drivers/cpp/overview) | [`cpp/`](https://github.com/vaticle/typedb-driver/tree/development/cpp) |
| C# | [README](https://github.com/vaticle/typedb-driver/tree/development/csharp/README.md) | (Coming soon!) | [`csharp/`](https://github.com/vaticle/typedb-driver/tree/development/csharp) |
| C# | [README](https://github.com/vaticle/typedb-driver/tree/development/csharp/README.md) | [Documentation](https://typedb.com/docs/drivers/csharp/overview) | [`csharp/`](https://github.com/vaticle/typedb-driver/tree/development/csharp) |

### Package hosting

Expand Down
18 changes: 17 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ workspace(name = "vaticle_typedb_driver")
##############################
# Load @vaticle_dependencies #
##############################

load("//dependencies/vaticle:repositories.bzl", "vaticle_dependencies")
vaticle_dependencies()

Expand Down Expand Up @@ -79,6 +78,22 @@ paket2bazel_dependencies()
load("//csharp/nuget:paket.csharp_deps.bzl", csharp_deps = "csharp_deps")
csharp_deps()

# Load //builder/go
load("@vaticle_dependencies//builder/go:deps.bzl", go_deps = "deps")
go_deps()
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies")
go_rules_dependencies()

load("//go:go_versions.bzl", "register_all_toolchains")
register_all_toolchains()

# gazelle:repo bazel_gazelle <- Used to tell gazelle that it is loaded in a macro.
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//go:deps.bzl", "go_repositories")
# gazelle:repository_macro go/deps.bzl%go_repositories
go_repositories()
gazelle_dependencies()

# Load //builder/proto_grpc
load("@vaticle_dependencies//builder/proto_grpc:deps.bzl", grpc_deps = "deps")
grpc_deps()
Expand Down Expand Up @@ -246,6 +261,7 @@ vaticle_typedb_protocol_npm_repositories()

# Setup rules_ts
load("@aspect_rules_ts//ts:repositories.bzl", "rules_ts_dependencies")

rules_ts_dependencies(
ts_version_from = "//nodejs:package.json",
)
Expand Down
20 changes: 20 additions & 0 deletions c/swig/typedb_driver_go.swg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/* TODO: Implement */
2 changes: 1 addition & 1 deletion dependencies/vaticle/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def vaticle_dependencies():
git_repository(
name = "vaticle_dependencies",
remote = "https://github.com/vaticle/dependencies",
commit = "294ef724c3853c9851e1e0c6bc04e0470c724e10", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_dependencies
commit = "0b52d5d6dc5906d6a5dd6444ad2849466363ec6b", # sync-marker: do not remove this comment, this is used for sync-dependencies by @vaticle_dependencies
)

def vaticle_typeql():
Expand Down
82 changes: 82 additions & 0 deletions go/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

load("@vaticle_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("//go:rules.bzl", "swig_native_go_library")

# TODO: Building this target doesn't put the dynamic library (libtypedb_driver_go_native.dylib for MacOS) into
# bazel-bin/go, while it's needed for the clinkopts below.
swig_native_go_library(
name = "typedb_driver_go_native",
lib = "//c:typedb_driver_clib_headers",
package_name= "typedb_driver",
interface = "//c:typedb_driver.i",
includes = ["//c:swig/typedb_driver_go.swg"],
enable_cxx = True,
visibility = ["//visibility:public"],
)

# TODO: Cannot be built in CI because of the absolute path used in clinkopts.
# For now, requires manually running `bazel build //go:libtypedb_driver_go_native` before this building this target.
#go_library(
# name = "driver-go",
# srcs = glob(["*.go"]),
# data = ["//:LICENSE"],
# importpath = "typedb_driver/go",
# clinkopts = [
# "-L$TYPEDB_DRIVER_PATH/typedb-driver/bazel-bin/go", # TODO: Use relative path instead of the full path (couldn't make working for now)
# "-ltypedb_driver_go_native",
# "-framework", "CoreFoundation"
# ],
# deps = [
# "//go:typedb_driver_go_native",
# "//go/api/user:user",
# "//go/api/database:database",
# "//go/connection:connection",
# ],
# cgo = True,
# visibility = ["//visibility:public"],
#)

# TODO: The targets below are proofs of concept and are placed here for a simpler linker debug process.
# It runs based on `driver_proof_of_concept.go` (NOTICE: this file is built inside the library, which is incorrect,
# but it was enough for a simple proof of concept. Need to verify if the dynamic library can be found by external packages.
#go_binary(
# name = "driver-temp-exe",
# embed = [":driver-go"],
# visibility = ["//visibility:public"],
#)

# TODO: This test doesn't work because it can't find the driver's native dynamic library.
#go_test(
# name = "db-connection-test",
# srcs = ["//go/test/integration:db_connection_test.go"],
# embed = ["//go:driver-go"],
# visibility = ["//visibility:public"],
#)

checkstyle_test(
name = "checkstyle",
size = "small",
include = glob(["*"]),
exclude = glob([
"README.md",
"docs/**/*.adoc",
]),
license_type = "apache-header",
)
28 changes: 28 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# TypeDB Go Driver (WIP)

## Current state

`TypeDB Go Driver` is not implemented yet. This repository contains proofs of concept, including driver's library build
phase and basic database manipulation functionality. A usage example could be found
in [driver_proof_of_concept.go](driver_proof_of_concept.go) (database creation and deletion).

### Issues

- [BUILD](BUILD) expects manual actions to build on a specific machine and is not fully automated yet (more details
below and inside the file)
- Only basic connection and database manipulation features are implemented and not tested
- There is no resource management, exceptions handling and some of the needed Rust structs wrappers on the SWIG side

### Run

- Run `bazel build //go:libtypedb_driver_go_native`
- Uncomment `driver-go` (and `driver-temp-exe`) targets in [BUILD](BUILD)
- Specify your local path to the `typedb_driver_go_native` dynamic library instead
of `$TYPEDB_DRIVER_PATH/typedb-driver/bazel-bin/go`
- Run `bazel build //go:driver-go` (or `bazel run //go:driver-temp-exe`). Make sure that you have a `TypeDB` server
running to connect to it

```bash
bazel build //go:libtypedb_driver_go_native
bazel build //go:driver-go
```
41 changes: 41 additions & 0 deletions go/api/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

package(default_visibility = ["//visibility:public"])

load("@vaticle_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

#go_library(
# name = "api",
# srcs = glob(["*.go", "*/*.go"]),
# deps = [
# "//go/api/database:database",
# "//go/api/user:user",
# "//go:typedb_driver_go_native",
# ],
# importpath = "typedb_driver/go/api",
# visibility = ["//visibility:public"],
# cgo = True
#)

checkstyle_test(
name = "checkstyle",
include = glob(["*", "*/*"]),
license_type = "apache-header",
size = "small",
)
35 changes: 35 additions & 0 deletions go/api/TypeDBDriver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package api

import (
"typedb_driver/go/api/database"
"typedb_driver/go/api/user"
"typedb_driver/go_wrapper"
)

type Driver interface {
IsOpen() bool
Databases() database.DatabaseManager
Session() typedb_driver.Session // TODO: Implement sessions and return it instead of the native one
Close()
User() user.User
Users() user.UserManager
}
37 changes: 37 additions & 0 deletions go/api/database/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

load("@vaticle_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

# TODO: Consider merging everything into api/BUILD
#go_library(
# name = "database",
# srcs = [
# "DatabaseManager.go",
# "Database.go"
# ],
# importpath = "typedb_driver/go/api/database",
# visibility = ["//visibility:public"],
#)

checkstyle_test(
name = "checkstyle",
include = glob(["*"]),
license_type = "apache-header",
size = "small",
)
38 changes: 38 additions & 0 deletions go/api/database/Database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package database

type Database interface {
Name() string
Schema() string
TypeSchema() string
RuleSchema() string
Delete()
Replicas() []Replica
PrimaryReplica() Replica
PreferredReplica() Replica
}

type Replica interface {
Server() string
IsPrimary() bool
IsPreferred() bool
Term() int64
}
27 changes: 27 additions & 0 deletions go/api/database/DatabaseManager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package database

type DatabaseManager interface {
Get(name string) Database
Contains(name string) bool
Create(name string)
All() []Database
}
Loading

0 comments on commit b45116c

Please sign in to comment.