Skip to content

Commit a60d5a7

Browse files
committed
Mapped server error codes
Also plumbed in the init and fini methods. JAVA-2806 Code review updates and added bonus - server error code translation
1 parent 59fae8f commit a60d5a7

14 files changed

+363
-143
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ configure(subprojects.findAll { it.name != 'util' && it.name != 'mongo-java-driv
164164

165165
systemProperties(
166166
'org.mongodb.test.uri': System.getProperty('org.mongodb.test.uri'),
167+
'org.mongodb.test.embedded.path': System.getProperty('org.mongodb.test.embedded.path'),
167168
'org.mongodb.useSocket': System.getProperty('org.mongodb.useSocket', 'false'),
168169
'org.mongodb.disableAsync': System.getProperty('org.mongodb.disableAsync', 'false'),
169170
'org.mongodb.async.type': System.getProperty('org.mongodb.async.type', 'nio2'),

driver-embedded/src/main/com/mongodb/embedded/client/EmbeddedConnection.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.mongodb.embedded.client;
1818

19-
import com.mongodb.MongoException;
2019
import com.sun.jna.Pointer;
2120
import com.sun.jna.ptr.IntByReference;
2221
import com.sun.jna.ptr.PointerByReference;
@@ -26,44 +25,24 @@
2625
import java.util.List;
2726

2827
final class EmbeddedConnection implements Closeable {
29-
30-
private final MongoDBCAPI mongoDBCAPI;
3128
private volatile Pointer clientPointer;
3229

33-
EmbeddedConnection(final MongoDBCAPI mongoDBCAPI, final Pointer databasePointer) {
34-
this.mongoDBCAPI = mongoDBCAPI;
35-
try {
36-
this.clientPointer = mongoDBCAPI.libmongodbcapi_db_client_new(databasePointer);
37-
} catch (Throwable t) {
38-
throw new MongoException("Error from embedded server when calling db_client_new: " + t.getMessage(), t);
39-
}
30+
EmbeddedConnection(final Pointer databasePointer) {
31+
this.clientPointer = MongoDBCAPIHelper.db_client_new(databasePointer);
4032
}
4133

4234
public ByteBuffer sendAndReceive(final List<ByteBuffer> messageBufferList) {
4335
byte[] message = createCompleteMessage(messageBufferList);
4436

4537
PointerByReference outputBufferReference = new PointerByReference();
4638
IntByReference outputSize = new IntByReference();
47-
48-
try {
49-
int errorCode = mongoDBCAPI.libmongodbcapi_db_client_wire_protocol_rpc(clientPointer, message, message.length,
50-
outputBufferReference, outputSize);
51-
if (errorCode != 0) {
52-
throw new MongoException(errorCode, "Error from embedded server: " + errorCode);
53-
}
54-
return outputBufferReference.getValue().getByteBuffer(0, outputSize.getValue());
55-
} catch (Throwable t) {
56-
throw new MongoException("Error from embedded server when calling db_client_wire_protocol_rpc: " + t.getMessage(), t);
57-
}
39+
MongoDBCAPIHelper.db_client_wire_protocol_rpc(clientPointer, message, message.length, outputBufferReference, outputSize);
40+
return outputBufferReference.getValue().getByteBuffer(0, outputSize.getValue());
5841
}
5942

6043
@Override
6144
public void close() {
62-
try {
63-
mongoDBCAPI.libmongodbcapi_db_client_destroy(clientPointer);
64-
} catch (Throwable t) {
65-
throw new MongoException("Error from embedded server when calling db_client_destroy: " + t.getMessage(), t);
66-
}
45+
MongoDBCAPIHelper.db_client_destroy(clientPointer);
6746
clientPointer = null;
6847
}
6948

driver-embedded/src/main/com/mongodb/embedded/client/EmbeddedDatabase.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
package com.mongodb.embedded.client;
1818

19-
import com.mongodb.MongoClientException;
2019
import com.mongodb.MongoException;
2120
import com.mongodb.diagnostics.logging.Logger;
2221
import com.mongodb.diagnostics.logging.Loggers;
23-
import com.sun.jna.Native;
24-
import com.sun.jna.NativeLibrary;
2522
import com.sun.jna.Pointer;
2623

2724
import java.io.Closeable;
@@ -32,8 +29,6 @@
3229

3330
final class EmbeddedDatabase implements Closeable {
3431
private static final Logger LOGGER = Loggers.getLogger("embedded.client");
35-
private static final String NATIVE_LIBRARY_NAME = "mongo_embedded_capi";
36-
private final MongoDBCAPI mongoDBCAPI;
3732
private volatile Pointer databasePointer;
3833

3934
EmbeddedDatabase(final MongoClientSettings mongoClientSettings) {
@@ -46,55 +41,22 @@ final class EmbeddedDatabase implements Closeable {
4641
throw new MongoException(format("Could not validate / create the dbpath: %s", mongoClientSettings.getDbPath()));
4742
}
4843

49-
if (mongoClientSettings.getLibraryPath() != null) {
50-
NativeLibrary.addSearchPath(NATIVE_LIBRARY_NAME, mongoClientSettings.getLibraryPath());
51-
}
52-
try {
53-
this.mongoDBCAPI = Native.loadLibrary(NATIVE_LIBRARY_NAME, MongoDBCAPI.class);
54-
} catch (UnsatisfiedLinkError e) {
55-
throw new MongoClientException(format("Failed to load the mongodb library: '%s'."
56-
+ "%n %s %n"
57-
+ "%n Please set the library location by either:"
58-
+ "%n - Adding it to the classpath."
59-
+ "%n - Setting 'jna.library.path' system property"
60-
+ "%n - Configuring it in the 'MongoClientSettings.builder().libraryPath' method."
61-
+ "%n", NATIVE_LIBRARY_NAME, e.getMessage()), e);
62-
}
63-
6444
String[] cmdArgs = createCmdArgs(mongoClientSettings);
65-
try {
66-
this.databasePointer = mongoDBCAPI.libmongodbcapi_db_new(cmdArgs.length, cmdArgs, createEnvArgs());
67-
if (databasePointer == null) {
68-
throw new MongoException("Could not create a new embedded database");
69-
}
70-
} catch (Throwable t) {
71-
throw new MongoException("Error from embedded server when calling db_new: " + t.getMessage(), t);
72-
}
45+
this.databasePointer = MongoDBCAPIHelper.db_new(cmdArgs.length, cmdArgs, createEnvArgs());
7346
}
7447

7548
public EmbeddedConnection createConnection() {
76-
return new EmbeddedConnection(mongoDBCAPI, databasePointer);
49+
return new EmbeddedConnection(databasePointer);
7750
}
7851

7952
public void pump() {
80-
try {
81-
int errorCode = mongoDBCAPI.libmongodbcapi_db_pump(databasePointer);
82-
if (errorCode != 0) {
83-
throw new MongoException(errorCode, "Error from embedded server: + " + errorCode);
84-
}
85-
} catch (Throwable t) {
86-
throw new MongoException("Error from embedded server when calling db_pump: " + t.getMessage(), t);
87-
}
53+
MongoDBCAPIHelper.db_pump(databasePointer);
8854
}
8955

9056
@Override
9157
public void close() {
9258
if (databasePointer != null) {
93-
try {
94-
mongoDBCAPI.libmongodbcapi_db_destroy(databasePointer);
95-
} catch (Throwable t) {
96-
throw new MongoException("Error from embedded server when calling db_destroy: " + t.getMessage(), t);
97-
}
59+
MongoDBCAPIHelper.db_destroy(databasePointer);
9860
databasePointer = null;
9961
}
10062
}

driver-embedded/src/main/com/mongodb/embedded/client/MongoClientSettings.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public final class MongoClientSettings {
4444

4545
private final com.mongodb.MongoClientSettings wrappedMongoClientSettings;
4646
private final String dbPath;
47-
private final String libraryPath;
4847

4948
/**
5049
* Gets the default codec registry. It includes the following providers:
@@ -94,7 +93,6 @@ public static Builder builder(final MongoClientSettings settings) {
9493
public static final class Builder {
9594
private com.mongodb.MongoClientSettings.Builder wrappedBuilder;
9695
private String dbPath;
97-
private String libraryPath;
9896

9997
private Builder() {
10098
wrappedBuilder = com.mongodb.MongoClientSettings.builder();
@@ -104,7 +102,6 @@ private Builder(final MongoClientSettings settings) {
104102
notNull("settings", settings);
105103
wrappedBuilder = com.mongodb.MongoClientSettings.builder(settings.wrappedMongoClientSettings);
106104
dbPath = settings.dbPath;
107-
libraryPath = settings.libraryPath;
108105
}
109106

110107
/**
@@ -187,17 +184,6 @@ public Builder dbPath(final String dbPath) {
187184
return this;
188185
}
189186

190-
/**
191-
* Sets the library path for mongod.
192-
*
193-
* @param libraryPath the library path for mongod
194-
* @return this
195-
*/
196-
public Builder libraryPath(final String libraryPath) {
197-
this.libraryPath = libraryPath;
198-
return this;
199-
}
200-
201187
/**
202188
* Build an instance of {@code MongoClientSettings}.
203189
*
@@ -249,15 +235,6 @@ public String getDbPath() {
249235
return dbPath;
250236
}
251237

252-
/**
253-
* Gets the library path for the embedded mongod
254-
*
255-
* @return the library path if set or null
256-
*/
257-
public String getLibraryPath() {
258-
return libraryPath;
259-
}
260-
261238
/**
262239
* Returns the wrapped MongoClientSettings instance
263240
*
@@ -270,7 +247,6 @@ public com.mongodb.MongoClientSettings getWrappedMongoClientSettings() {
270247
private MongoClientSettings(final Builder builder) {
271248
isTrue("dbPath is set", builder.dbPath != null && !builder.dbPath.isEmpty());
272249
dbPath = builder.dbPath;
273-
libraryPath = builder.libraryPath;
274250
wrappedMongoClientSettings = builder.wrappedBuilder.build();
275251
}
276252
}

driver-embedded/src/main/com/mongodb/embedded/client/MongoClients.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public final class MongoClients {
3535
* @param mongoEmbeddedSettings the settings for the embedded driver.
3636
*/
3737
public static void init(final MongoEmbeddedSettings mongoEmbeddedSettings) {
38+
// TODO - support yaml
39+
MongoDBCAPIHelper.init(mongoEmbeddedSettings, "");
3840
}
3941

4042
/**
@@ -44,6 +46,7 @@ public static void init(final MongoEmbeddedSettings mongoEmbeddedSettings) {
4446
* @return the client
4547
*/
4648
public static MongoClient create(final MongoClientSettings mongoClientSettings) {
49+
MongoDBCAPIHelper.checkHasBeenInitialized();
4750
Cluster cluster = new EmbeddedCluster(mongoClientSettings);
4851
return new MongoClientImpl(cluster, mongoClientSettings.getWrappedMongoClientSettings(), null);
4952
}
@@ -52,6 +55,7 @@ public static MongoClient create(final MongoClientSettings mongoClientSettings)
5255
* Closes down the mongod library
5356
*/
5457
public static void close() {
58+
MongoDBCAPIHelper.fini();
5559
}
5660

5761
private MongoClients() {

driver-embedded/src/main/com/mongodb/embedded/client/MongoDBCAPI.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ interface MongoDBCAPI extends Library {
2626

2727
// CHECKSTYLE.OFF: MethodName
2828

29+
/**
30+
* Initializes the mongodbcapi library, required before any other call. Cannot be called again
31+
* without libmongodbcapi_fini() being called first.
32+
*
33+
* @param config the YAML formatted MongoDB configuration string. See documentation for valid options.
34+
*
35+
* @note This function is not thread safe.
36+
*
37+
* @return the error, or 0 if success
38+
*/
39+
int libmongodbcapi_init(String config);
40+
41+
/**
42+
* Tears down the state of the library, all databases must be closed before calling this.
43+
*
44+
* @return the error, or 0 if success
45+
*/
46+
int libmongodbcapi_fini();
47+
2948
/**
3049
* Create a new db instance.
3150
*

0 commit comments

Comments
 (0)