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

jdbc URL string support #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/main/java/com/vesoft/nebula/jdbc/impl/NebulaDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void closePool(){
@Override
public Connection connect(String url, Properties connectionConfig) throws SQLException {
if(this.acceptsURL(url)){
url = parseAddress(url);
parseUrlProperties(url, connectionConfig);
this.connectionConfig.put("url", url);
String graphSpace = this.connectionConfig.getProperty("graphSpace");
Expand All @@ -140,6 +141,50 @@ public Connection connect(String url, Properties connectionConfig) throws SQLExc
}
}

/**
* Parse an extended JDBC url supporting the syntax:
*
* jdbc:nebula://host:port,host:port,.../graphSpace
*
* @param url extended url
* @return the original url 'jdbc:nebula://graphSpace'
* @throws SQLException if the servers are not specified as 'host:port' or 'port' is not an integer
*/
protected String parseAddress(String url) throws SQLException {
// jdbc:nebula://ip:port,ip:port,.../graphspace

int b = url.indexOf("//");
int e = url.indexOf("/", b+2);
if (e == -1)
return url;

String addresses = url.substring(b+2, e);
url = url.substring(0, b+2) + url.substring(e+1);

String[] addressesList = addresses.split(",");
List<HostAddress> customizedAddressList = new ArrayList<>();

for (String address : addressesList) {
String[] addressInfo = address.split(":");
if(addressInfo.length != 2){
throw new SQLException(String.format("url [%s] is invalid, please make sure your url match thr format: \"ip:port\".", url));

Choose a reason for hiding this comment

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

"thr" seems to be a typo?

Copy link
Member Author

@wey-gu wey-gu Sep 9, 2022

Choose a reason for hiding this comment

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

Ha, thanks! Should be /thr/the/😊

}
String ip = addressInfo[0];
int port = Integer.parseInt(addressInfo[1]);
customizedAddressList.add(new HostAddress(ip, port));
}

// close the current pool that uses '127.0.0.1' (WHY????)
this.nebulaPool.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it should be refactored.
If url is the param of connect, then when get driver, there's should't init the pool, just init the pool in connect function.
Or give the url to driver, and init the connection pool in getDriver function.

The default client address 127.0.0.1 is unreasonable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

totally agreed, would be better to remove this one.

Copy link

Choose a reason for hiding this comment

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

@Nicole00 I'm sorry but i saw line 180 the customizedAddressList will replace the poolProperties's addressList , so in my opinion the default IP 127.0.0.1 will by replaced by the customizedAddressList ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it will be replaced by customizedAddressList only when user call new NebulaDriver(address), but it's not the standard way to use jdbc. Almost usage is Class.forName() and it will call the empty construct function, so the address will be 127.0.0.1.

We can remove the customizedAddressList and put the user address in url, which will be more common.

// set in the pool properties the new 'addressList'
this.poolProperties.put("addressList", customizedAddressList);
// create & initialize a new pool
this.nebulaPool = new NebulaPool();
this.initNebulaPool();

return url;
}

/**
* get the Properties that used in NebulaDriver's constructor to specify addressList and parameters of {@link NebulaPool}.
*/
Expand Down