Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.edgedb.examples;

import com.edgedb.driver.EdgeDBClient;
import com.edgedb.driver.EdgeDBClientConfig;
import com.edgedb.driver.*;
import com.edgedb.driver.exceptions.EdgeDBException;
import com.edgedb.driver.namingstrategies.NamingStrategy;
import org.slf4j.Logger;
Expand All @@ -24,6 +23,8 @@ public static void main(String[] args) throws IOException, EdgeDBException {
runJavaExamples(client);

logger.info("Examples complete");

System.exit(0);
}

private static void runJavaExamples(EdgeDBClient client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public final class Transactions implements Example {
private static final Logger logger = LoggerFactory.getLogger(Transactions.class);

@Override
public CompletionStage<Void> run(EdgeDBClient client) {
// verify we can run transactions
if(!client.supportsTransactions()) {
logger.info("Skipping transactions, client type {} doesn't support it", client.getClientType());
return CompletableFuture.completedFuture(null);
}

return client.transaction(tx -> {
logger.info("In transaction");
return tx.queryRequiredSingle(String.class, "select 'Result from Transaction'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.edgedb.examples

import com.edgedb.driver.EdgeDBClient
import com.edgedb.driver.EdgeDBClientConfig
import com.edgedb.driver.EdgeDBConnection
import com.edgedb.driver.namingstrategies.NamingStrategy
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import kotlin.system.exitProcess

object Main {
private val logger = LoggerFactory.getLogger(Main::class.java)
Expand Down Expand Up @@ -37,5 +39,7 @@ object Main {
}
}
}

exitProcess(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class Transactions : Example {
}

override suspend fun runAsync(client: EdgeDBClient) {
// verify we can run transactions
if (!client.supportsTransactions()) {
logger.info("Skipping transactions, client type {} doesn't support it", client.clientType)
return
}

val transactionResult = client.transaction { tx ->
tx.queryRequiredSingle(String::class.java, "SELECT 'Hello from transaction!'")
}.await()
Expand Down
2 changes: 1 addition & 1 deletion examples/scala-examples/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ThisBuild / scalaVersion := "3.1.3"
//resolvers += Resolver.file("my-test-repo", file("test"))

libraryDependencies ++= Seq(
"com.edgedb" % "driver" % "0.0.1" from "file:///" + System.getProperty("user.dir") + "/lib/com.edgedb.driver-0.0.1-SNAPSHOT.jar",
"com.edgedb" % "driver" % "0.1.1" from "file:///" + System.getProperty("user.dir") + "/lib/com.edgedb.driver-0.1.1-SNAPSHOT.jar",
"ch.qos.logback" % "logback-classic" % "1.4.7",
"ch.qos.logback" % "logback-core" % "1.4.7",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.15.1",
Expand Down
2 changes: 2 additions & 0 deletions examples/scala-examples/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def main(): Unit = {
Await.ready(runExample(logger, client, example), Duration.Inf)

logger.info("Examples complete!")

System.exit(0)
}

private def runExample(logger: Logger, client: EdgeDBClient, example: Example)(implicit context: ExecutionContext): Future[Unit] = {
Expand Down
6 changes: 6 additions & 0 deletions examples/scala-examples/src/main/scala/Transactions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import scala.concurrent.{ExecutionContext, Future}
class Transactions extends Example {
private val logger = LoggerFactory.getLogger(classOf[Transactions])
override def run(client: EdgeDBClient)(implicit context: ExecutionContext): Future[Unit] = {
// verify we can run transactions
if (!client.supportsTransactions()) {
logger.info("Skipping transactions, client type {} doesn't support it", client.getClientType)
return Future.unit
}

client.transaction((tx: Transaction) => {
logger.info("In transaction")
tx.queryRequiredSingle(classOf[String], "select 'Result from Transaction'")
Expand Down
15 changes: 13 additions & 2 deletions src/driver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
testImplementation "org.assertj:assertj-core:$assertj_version"
testImplementation 'org.burningwave:core:12.62.6'
testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_databind_version'
testImplementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
testImplementation "ch.qos.logback:logback-classic:$logback_version"
testImplementation "ch.qos.logback:logback-core:$logback_version"
}
Expand All @@ -33,13 +33,24 @@ jar {
}
}

tasks.register('copyJarToBin') {
def deleteOldJar = tasks.register('deleteOldJarInBin') {
var path = Paths.get(project.rootDir.toString(), 'examples', 'scala-examples', 'lib')
var paths = path.toFile().listFiles((FileFilter) { File f -> f.name.startsWith('com.edgedb.driver') })
delete files(paths)
}

def copyJar = tasks.register('copyJarToBin') {
copy {
from jar
into Paths.get(project.rootDir.toString(), 'examples', 'scala-examples', 'lib')
}
}

copyJar.configure {
dependsOn(deleteOldJar, compileJava)
}


publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
80 changes: 50 additions & 30 deletions src/driver/src/main/java/com/edgedb/driver/EdgeDBClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.edgedb.driver;

import com.edgedb.driver.abstractions.ClientQueryDelegate;
import com.edgedb.driver.clients.BaseEdgeDBClient;
import com.edgedb.driver.clients.EdgeDBTCPClient;
import com.edgedb.driver.clients.StatefulClient;
import com.edgedb.driver.clients.TransactableClient;
import com.edgedb.driver.clients.*;
import com.edgedb.driver.datatypes.Json;
import com.edgedb.driver.exceptions.ConfigurationException;
import com.edgedb.driver.exceptions.EdgeDBException;
Expand Down Expand Up @@ -80,14 +77,6 @@ public EdgeDBClient(EdgeDBConnection connection, @NotNull EdgeDBClientConfig con
this.clientAvailability = config.getClientAvailability();
}

private @NotNull ClientFactory createClientFactory() throws ConfigurationException {
if(config.getClientType() == ClientType.TCP) {
return EdgeDBTCPClient::new;
}

throw new ConfigurationException(String.format("No such implementation for client type %s found", this.config.getClientType()));
}

/**
* Constructs a new {@linkplain EdgeDBClient}.
* @param connection The connection parameters used to connect this client to EdgeDB.
Expand Down Expand Up @@ -126,6 +115,25 @@ private EdgeDBClient(@NotNull EdgeDBClient other, Session session) {
this.clientAvailability = other.clientAvailability;
}

private @NotNull ClientFactory createClientFactory() throws ConfigurationException {
if(config.getClientType() == ClientType.TCP) {
return EdgeDBTCPClient::new;
} else if (config.getClientType() == ClientType.HTTP) {
return EdgeDBHttpClient::new;
}

throw new ConfigurationException(String.format("No such implementation for client type %s found", this.config.getClientType()));
}

/**
* Gets the underlying client type for this client pool.
* @return The underlying client type, usually based on transport.
* @see ClientType
*/
public ClientType getClientType() {
return config.getClientType();
}

/**
* Gets whether this client supports transactions.
* @return {@code true} if the client supports transactions; otherwise {@code false}.
Expand Down Expand Up @@ -232,14 +240,23 @@ public <T> CompletionStage<T> transaction(@NotNull Function<Transaction, Complet
return new EdgeDBClient(this, this.session.withModule(module));
}

// added because Map.entry cannot contain nulls
private static final class ExecutePair<U> {
public final BaseEdgeDBClient client;
public final U result;
private final BaseEdgeDBClient client;
private final @Nullable U result;

private ExecutePair(BaseEdgeDBClient client, U result) {
private ExecutePair(BaseEdgeDBClient client, @Nullable U result) {
this.client = client;
this.result = result;
}

public @Nullable U getResult() {
return result;
}

public BaseEdgeDBClient getClient() {
return client;
}
}

private <T, U> CompletionStage<U> executePooledQuery(
Expand All @@ -254,21 +271,18 @@ private <T, U> CompletionStage<U> executePooledQuery(
query,
args,
capabilities
).handle((r, x) -> new ExecutePair<>(client, r))
).thenApply(r -> new ExecutePair<>(client, r))
)
.handle((pair, exc) -> {
try {
pair.client.close();
} catch (Exception e) {
throw new CompletionException(e);
.whenComplete((entry, exc) -> {
if(entry != null) {
try {
entry.getClient().close();
} catch (Exception e) {
throw new CompletionException(e);
}
}

if(exc != null) {
throw new CompletionException(exc);
}

return pair.result;
});
})
.thenApply(ExecutePair::getResult);
}

@Override
Expand Down Expand Up @@ -374,7 +388,12 @@ private CompletionStage<BaseEdgeDBClient> createClient() {
return this.poolHolder.acquireContract()
.thenApply(contract -> {
logger.trace("Contract acquired, remaining handles: {}", this.poolHolder.remaining());
var client = clientFactory.create(this.connection, this.config, contract);
BaseEdgeDBClient client;
try {
client = clientFactory.create(this.connection, this.config, contract);
} catch (EdgeDBException e) {
throw new CompletionException(e);
}
contract.register(client, this::acceptClient);
client.onReady(this::onClientReady);
logger.debug("client instance created: {}", client);
Expand All @@ -385,6 +404,7 @@ private CompletionStage<BaseEdgeDBClient> createClient() {

@FunctionalInterface
private interface ClientFactory {
BaseEdgeDBClient create(EdgeDBConnection connection, EdgeDBClientConfig config, AutoCloseable poolHandle);
BaseEdgeDBClient create(EdgeDBConnection connection, EdgeDBClientConfig config, AutoCloseable poolHandle)
throws EdgeDBException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected void setPassword(String value) {
* @return The hostname part of the connection.
*/
public @NotNull String getHostname() {
return hostname == null ? "127.0.0.1" : hostname;
return hostname == null ? "localhost" : hostname;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ public void skip(int count) {
this.buffer.skipBytes(count);
}

public void skip(long count) {
if(count >> 32 == 0) {
// can convert to int
skip((int)count);
return;
}

var temp = count;
do {
this.buffer.skipBytes((int) temp);
temp -= Integer.MAX_VALUE;
}
while (temp >= Integer.MAX_VALUE);
}

public boolean isEmpty() {
return this.buffer.readableBytes() == 0;
}
Expand Down
Loading