Skip to content
Snippets Groups Projects
Commit 593c2625 authored by gkamat's avatar gkamat
Browse files

Merge pull request #366 from allanbank/pr-365

[mongodb] Update the pom.xml to make checkstyle fail the build. Fixes #365
parents deba06be 472beb38
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yahoo.ycsb</groupId>
<artifactId>binding-parent</artifactId>
<version>0.4.0-SNAPSHOT</version>
<relativePath>../binding-parent</relativePath>
</parent>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yahoo.ycsb</groupId>
<artifactId>binding-parent</artifactId>
<version>0.4.0-SNAPSHOT</version>
<relativePath>../binding-parent</relativePath>
</parent>
<artifactId>mongodb-binding</artifactId>
<name>MongoDB Binding</name>
<packaging>jar</packaging>
<artifactId>mongodb-binding</artifactId>
<name>MongoDB Binding</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongodb.version}</version>
</dependency>
<dependency>
<groupId>com.allanbank</groupId>
<artifactId>mongodb-async-driver</artifactId>
<version>${mongodb.async.version}</version>
</dependency>
<dependency>
<groupId>com.yahoo.ycsb</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongodb.version}</version>
</dependency>
<dependency>
<groupId>com.allanbank</groupId>
<artifactId>mongodb-async-driver</artifactId>
<version>${mongodb.async.version}</version>
</dependency>
<dependency>
<groupId>com.yahoo.ycsb</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>allanbank</id>
<name>Allanbank Releases</name>
<url>http://www.allanbank.com/repo/</url>
<layout>default</layout>
</repository>
</repositories>
</project>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<configuration>
<consoleOutput>true</consoleOutput>
<configLocation>../checkstyle.xml</configLocation>
<failOnViolation>true</failOnViolation>
<failsOnError>true</failsOnError>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>allanbank</id>
<name>Allanbank Releases</name>
<url>http://www.allanbank.com/repo/</url>
<layout>default</layout>
</repository>
</repositories>
</project>
This diff is collapsed.
/*
* #%L
* OptionsSupport.java - mongodb-binding - Yahoo!, Inc.
* %%
* Copyright (C) 2015 Yahoo!, Inc.
* %%
* Licensed 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
*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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.
* #L%
* 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. See accompanying
* LICENSE file.
*/
package com.yahoo.ycsb.db;
import java.util.Properties;
/**
* OptionsSupport provides methods for handling legacy options.
*
*
* @author rjm
*/
public final class OptionsSupport {
/** Value for an unavailable property. */
private static final String UNAVAILABLE = "n/a";
/**
* Updates the URL with the appropriate attributes if legacy properties are
* set and the URL does not have the property already set.
*
* @param url
* The URL to update.
* @param props
* The legacy properties.
* @return The updated URL.
*/
public static String updateUrl(String url, Properties props) {
String result = url;
/** Value for an unavailable property. */
private static final String UNAVAILABLE = "n/a";
// max connections.
final String maxConnections = props.getProperty(
"mongodb.maxconnections", UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(maxConnections)) {
result = addUrlOption(result, "maxPoolSize", maxConnections);
}
/**
* Updates the URL with the appropriate attributes if legacy properties are
* set and the URL does not have the property already set.
*
* @param url
* The URL to update.
* @param props
* The legacy properties.
* @return The updated URL.
*/
public static String updateUrl(String url, Properties props) {
String result = url;
// Blocked thread multiplier.
final String threadsAllowedToBlockForConnectionMultiplier = props
.getProperty(
"mongodb.threadsAllowedToBlockForConnectionMultiplier",
UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(threadsAllowedToBlockForConnectionMultiplier)) {
result = addUrlOption(result, "waitQueueMultiple",
threadsAllowedToBlockForConnectionMultiplier);
}
// max connections.
final String maxConnections =
props.getProperty("mongodb.maxconnections", UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(maxConnections)) {
result = addUrlOption(result, "maxPoolSize", maxConnections);
}
// write concern
String writeConcernType = props.getProperty("mongodb.writeConcern",
// Blocked thread multiplier.
final String threadsAllowedToBlockForConnectionMultiplier =
props
.getProperty(
"mongodb.threadsAllowedToBlockForConnectionMultiplier",
UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(writeConcernType)) {
if ("errors_ignored".equals(writeConcernType)) {
result = addUrlOption(result, "w", "0");
}
else if ("unacknowledged".equals(writeConcernType)) {
result = addUrlOption(result, "w", "0");
}
else if ("acknowledged".equals(writeConcernType)) {
result = addUrlOption(result, "w", "1");
}
else if ("journaled".equals(writeConcernType)) {
result = addUrlOption(result, "journal", "true"); // this is the documented option name
result = addUrlOption(result, "j", "true"); // but keep this until MongoDB Java driver supports "journal" option
}
else if ("replica_acknowledged".equals(writeConcernType)) {
result = addUrlOption(result, "w", "2");
}
else if ("majority".equals(writeConcernType)) {
result = addUrlOption(result, "w", "majority");
}
else {
System.err.println("WARNING: Invalid writeConcern: '"
+ writeConcernType + "' will be ignored. "
+ "Must be one of [ unacknowledged | acknowledged | "
+ "journaled | replica_acknowledged | majority ]");
}
}
if (!UNAVAILABLE.equals(threadsAllowedToBlockForConnectionMultiplier)) {
result =
addUrlOption(result, "waitQueueMultiple",
threadsAllowedToBlockForConnectionMultiplier);
}
// read preference
String readPreferenceType = props.getProperty("mongodb.readPreference",
UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(readPreferenceType)) {
if ("primary".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "primary");
}
else if ("primary_preferred".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference",
"primaryPreferred");
}
else if ("secondary".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "secondary");
}
else if ("secondary_preferred".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference",
"secondaryPreferred");
}
else if ("nearest".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "nearest");
}
else {
System.err.println("WARNING: Invalid readPreference: '"
+ readPreferenceType + "' will be ignored. "
+ "Must be one of [ primary | primary_preferred | "
+ "secondary | secondary_preferred | nearest ]");
}
}
// write concern
String writeConcernType =
props.getProperty("mongodb.writeConcern", UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(writeConcernType)) {
if ("errors_ignored".equals(writeConcernType)) {
result = addUrlOption(result, "w", "0");
} else if ("unacknowledged".equals(writeConcernType)) {
result = addUrlOption(result, "w", "0");
} else if ("acknowledged".equals(writeConcernType)) {
result = addUrlOption(result, "w", "1");
} else if ("journaled".equals(writeConcernType)) {
result = addUrlOption(result, "journal", "true"); // this is the
// documented option
// name
result = addUrlOption(result, "j", "true"); // but keep this until
// MongoDB Java driver
// supports "journal" option
} else if ("replica_acknowledged".equals(writeConcernType)) {
result = addUrlOption(result, "w", "2");
} else if ("majority".equals(writeConcernType)) {
result = addUrlOption(result, "w", "majority");
} else {
System.err.println("WARNING: Invalid writeConcern: '"
+ writeConcernType + "' will be ignored. "
+ "Must be one of [ unacknowledged | acknowledged | "
+ "journaled | replica_acknowledged | majority ]");
}
}
return result;
// read preference
String readPreferenceType =
props.getProperty("mongodb.readPreference", UNAVAILABLE).toLowerCase();
if (!UNAVAILABLE.equals(readPreferenceType)) {
if ("primary".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "primary");
} else if ("primary_preferred".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "primaryPreferred");
} else if ("secondary".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "secondary");
} else if ("secondary_preferred".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "secondaryPreferred");
} else if ("nearest".equals(readPreferenceType)) {
result = addUrlOption(result, "readPreference", "nearest");
} else {
System.err.println("WARNING: Invalid readPreference: '"
+ readPreferenceType + "' will be ignored. "
+ "Must be one of [ primary | primary_preferred | "
+ "secondary | secondary_preferred | nearest ]");
}
}
/**
* Adds an option to the url if it does not already contain the option.
*
* @param url
* The URL to append the options to.
* @param name
* The name of the option.
* @param value
* The value for the option.
* @return The updated URL.
*/
private static String addUrlOption(String url, String name, String value) {
String fullName = name + "=";
if (!url.contains(fullName)) {
if (url.contains("?")) {
return url + "&" + fullName + value;
}
return url + "?" + fullName + value;
}
return url;
return result;
}
/**
* Adds an option to the url if it does not already contain the option.
*
* @param url
* The URL to append the options to.
* @param name
* The name of the option.
* @param value
* The value for the option.
* @return The updated URL.
*/
private static String addUrlOption(String url, String name, String value) {
String fullName = name + "=";
if (!url.contains(fullName)) {
if (url.contains("?")) {
return url + "&" + fullName + value;
}
return url + "?" + fullName + value;
}
return url;
}
/**
* Hidden Constructor.
*/
private OptionsSupport() {
// Nothing.
}
}
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
/**
* The YCSB binding for <a href="https://www.mongodb.org/">MongoDB</a>.
* For additional details on using and configuring the binding see the
* accompanying <a
* href="https://github.com/brianfrankcooper/YCSB/blob/master/mongodb/README.md"
* >README.md</a>.
* <p>
* A YCSB binding is provided for both the the
* <a href="http://www.allanbank.com/mongodb-async-driver/">Asynchronous
* Java Driver</a> and the MongoDB Inc.
* <a href="http://docs.mongodb.org/ecosystem/drivers/java/">driver</a>.
* </p>
*/
package com.yahoo.ycsb.db;
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
package com.yahoo.ycsb.db;
......@@ -49,219 +49,215 @@ import com.yahoo.ycsb.DB;
@SuppressWarnings("boxing")
public abstract class AbstractDBTestCases {
/** The default port for MongoDB. */
private static final int MONGODB_DEFAULT_PORT = 27017;
/** The default port for MongoDB. */
private static final int MONGODB_DEFAULT_PORT = 27017;
/**
* Verifies the mongod process (or some process) is running on port 27017,
* if not the tests are skipped.
*/
@BeforeClass
public static void setUpBeforeClass() {
// Test if we can connect.
Socket socket = null;
/**
* Verifies the mongod process (or some process) is running on port 27017, if
* not the tests are skipped.
*/
@BeforeClass
public static void setUpBeforeClass() {
// Test if we can connect.
Socket socket = null;
try {
// Connect
socket = new Socket(InetAddress.getLocalHost(), MONGODB_DEFAULT_PORT);
assertThat("Socket is not bound.", socket.getLocalPort(), not(-1));
} catch (IOException connectFailed) {
assumeNoException("MongoDB is not running. Skipping tests.",
connectFailed);
} finally {
if (socket != null) {
try {
// Connect
socket = new Socket(InetAddress.getLocalHost(),
MONGODB_DEFAULT_PORT);
assertThat("Socket is not bound.", socket.getLocalPort(), not(-1));
}
catch (IOException connectFailed) {
assumeNoException("MongoDB is not running. Skipping tests.",
connectFailed);
}
finally {
if (socket != null) {
try {
socket.close();
}
catch (IOException ignore) {
// Ignore.
}
}
socket = null;
socket.close();
} catch (IOException ignore) {
// Ignore.
}
}
socket = null;
}
}
/**
* Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#delete}
* .
*/
@Test
public void testInsertReadDelete() {
final DB client = getDB();
/**
* Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#delete} .
*/
@Test
public void testInsertReadDelete() {
final DB client = getDB();
final String table = "test";
final String id = "delete";
final String table = getClass().getSimpleName();
final String id = "delete";
HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] { 1, 2, 3, 4 }));
int result = client.insert(table, id, inserted);
assertThat("Insert did not return success (0).", result, is(0));
HashMap<String, ByteIterator> inserted =
new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] { 1, 2, 3, 4 }));
int result = client.insert(table, id, inserted);
assertThat("Insert did not return success (0).", result, is(0));
HashMap<String, ByteIterator> read = new HashMap<String, ByteIterator>();
Set<String> keys = Collections.singleton("a");
result = client.read(table, id, keys, read);
assertThat("Read did not return success (0).", result, is(0));
for (String key : keys) {
ByteIterator iter = read.get(key);
HashMap<String, ByteIterator> read = new HashMap<String, ByteIterator>();
Set<String> keys = Collections.singleton("a");
result = client.read(table, id, keys, read);
assertThat("Read did not return success (0).", result, is(0));
for (String key : keys) {
ByteIterator iter = read.get(key);
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 1)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 2)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 3)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 4)));
assertFalse(iter.hasNext());
}
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 1)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 2)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 3)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 4)));
assertFalse(iter.hasNext());
}
result = client.delete(table, id);
assertThat("Delete did not return success (0).", result, is(0));
result = client.delete(table, id);
assertThat("Delete did not return success (0).", result, is(0));
read.clear();
result = client.read(table, id, null, read);
assertThat("Read, after delete, did not return not found (1).", result,
is(1));
assertThat("Found the deleted fields.", read.size(), is(0));
read.clear();
result = client.read(table, id, null, read);
assertThat("Read, after delete, did not return not found (1).", result,
is(1));
assertThat("Found the deleted fields.", read.size(), is(0));
result = client.delete(table, id);
assertThat("Delete did not return not found (1).", result, is(1));
}
result = client.delete(table, id);
assertThat("Delete did not return not found (1).", result, is(1));
}
/**
* Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#update}
* .
*/
@Test
public void testInsertReadUpdate() {
DB client = getDB();
/**
* Test method for {@link DB#insert}, {@link DB#read}, and {@link DB#update} .
*/
@Test
public void testInsertReadUpdate() {
DB client = getDB();
final String table = "test";
final String id = "update";
final String table = getClass().getSimpleName();
final String id = "update";
HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] { 1, 2, 3, 4 }));
int result = client.insert(table, id, inserted);
assertThat("Insert did not return success (0).", result, is(0));
HashMap<String, ByteIterator> inserted =
new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] { 1, 2, 3, 4 }));
int result = client.insert(table, id, inserted);
assertThat("Insert did not return success (0).", result, is(0));
HashMap<String, ByteIterator> read = new HashMap<String, ByteIterator>();
Set<String> keys = Collections.singleton("a");
result = client.read(table, id, keys, read);
assertThat("Read did not return success (0).", result, is(0));
for (String key : keys) {
ByteIterator iter = read.get(key);
HashMap<String, ByteIterator> read = new HashMap<String, ByteIterator>();
Set<String> keys = Collections.singleton("a");
result = client.read(table, id, keys, read);
assertThat("Read did not return success (0).", result, is(0));
for (String key : keys) {
ByteIterator iter = read.get(key);
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 1)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 2)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 3)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 4)));
assertFalse(iter.hasNext());
}
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 1)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 2)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 3)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 4)));
assertFalse(iter.hasNext());
}
HashMap<String, ByteIterator> updated = new HashMap<String, ByteIterator>();
updated.put("a", new ByteArrayByteIterator(new byte[] { 5, 6, 7, 8 }));
result = client.update(table, id, updated);
assertThat("Update did not return success (0).", result, is(0));
HashMap<String, ByteIterator> updated = new HashMap<String, ByteIterator>();
updated.put("a", new ByteArrayByteIterator(new byte[] { 5, 6, 7, 8 }));
result = client.update(table, id, updated);
assertThat("Update did not return success (0).", result, is(0));
read.clear();
result = client.read(table, id, null, read);
assertThat("Read, after update, did not return success (0).", result,
is(0));
for (String key : keys) {
ByteIterator iter = read.get(key);
read.clear();
result = client.read(table, id, null, read);
assertThat("Read, after update, did not return success (0).", result, is(0));
for (String key : keys) {
ByteIterator iter = read.get(key);
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 5)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 6)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 7)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 8)));
assertFalse(iter.hasNext());
}
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 5)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 6)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 7)));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) 8)));
assertFalse(iter.hasNext());
}
}
/**
* Test method for {@link DB#scan}.
*/
@Test
public void testScan() {
final DB client = getDB();
/**
* Test method for {@link DB#scan}.
*/
@Test
public void testScan() {
final DB client = getDB();
final String table = "test";
final String table = getClass().getSimpleName();
// Insert a bunch of documents.
for (int i = 0; i < 100; ++i) {
HashMap<String, ByteIterator> inserted = new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] {
(byte) (i & 0xFF), (byte) (i >> 8 & 0xFF),
(byte) (i >> 16 & 0xFF), (byte) (i >> 24 & 0xFF) }));
int result = client.insert(table, padded(i), inserted);
assertThat("Insert did not return success (0).", result, is(0));
}
// Insert a bunch of documents.
for (int i = 0; i < 100; ++i) {
HashMap<String, ByteIterator> inserted =
new HashMap<String, ByteIterator>();
inserted.put("a", new ByteArrayByteIterator(new byte[] {
(byte) (i & 0xFF), (byte) (i >> 8 & 0xFF), (byte) (i >> 16 & 0xFF),
(byte) (i >> 24 & 0xFF) }));
int result = client.insert(table, padded(i), inserted);
assertThat("Insert did not return success (0).", result, is(0));
}
Set<String> keys = Collections.singleton("a");
Vector<HashMap<String, ByteIterator>> results = new Vector<HashMap<String, ByteIterator>>();
int result = client.scan(table, "00050", 5, null, results);
assertThat("Read did not return success (0).", result, is(0));
assertThat(results.size(), is(5));
for (int i = 0; i < 5; ++i) {
HashMap<String, ByteIterator> read = results.get(i);
for (String key : keys) {
ByteIterator iter = read.get(key);
Set<String> keys = Collections.singleton("a");
Vector<HashMap<String, ByteIterator>> results =
new Vector<HashMap<String, ByteIterator>>();
int result = client.scan(table, "00050", 5, null, results);
assertThat("Read did not return success (0).", result, is(0));
assertThat(results.size(), is(5));
for (int i = 0; i < 5; ++i) {
HashMap<String, ByteIterator> read = results.get(i);
for (String key : keys) {
ByteIterator iter = read.get(key);
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) & 0xFF))));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) >> 8 & 0xFF))));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) >> 16 & 0xFF))));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) >> 24 & 0xFF))));
assertFalse(iter.hasNext());
}
}
assertThat("Did not read the inserted field: " + key, iter,
notNullValue());
assertTrue(iter.hasNext());
assertThat(iter.nextByte(), is(Byte.valueOf((byte) ((i + 50) & 0xFF))));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) >> 8 & 0xFF))));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) >> 16 & 0xFF))));
assertTrue(iter.hasNext());
assertThat(iter.nextByte(),
is(Byte.valueOf((byte) ((i + 50) >> 24 & 0xFF))));
assertFalse(iter.hasNext());
}
}
}
/**
* Gets the test DB.
*
* @return The test DB.
*/
protected abstract DB getDB();
/**
* Gets the test DB.
*
* @return The test DB.
*/
protected abstract DB getDB();
/**
* Creates a zero padded integer.
*
* @param i
* The integer to padd.
* @return The padded integer.
*/
private String padded(int i) {
String result = String.valueOf(i);
while (result.length() < 5) {
result = "0" + result;
}
return result;
/**
* Creates a zero padded integer.
*
* @param i
* The integer to padd.
* @return The padded integer.
*/
private String padded(int i) {
String result = String.valueOf(i);
while (result.length() < 5) {
result = "0" + result;
}
return result;
}
}
\ No newline at end of file
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
package com.yahoo.ycsb.db;
......@@ -30,48 +30,45 @@ import com.yahoo.ycsb.DB;
*/
public class AsyncMongoDbClientTest extends AbstractDBTestCases {
/** The client to use. */
private AsyncMongoDbClient myClient = null;
/** The client to use. */
private AsyncMongoDbClient myClient = null;
/**
* Start a test client.
*/
@Before
public void setUp() {
myClient = new AsyncMongoDbClient();
myClient.setProperties(new Properties());
try {
myClient.init();
}
catch (Exception error) {
assumeNoException(error);
}
/**
* Start a test client.
*/
@Before
public void setUp() {
myClient = new AsyncMongoDbClient();
myClient.setProperties(new Properties());
try {
myClient.init();
} catch (Exception error) {
assumeNoException(error);
}
}
/**
* Stops the test client.
*/
@After
public void tearDown() {
try {
myClient.cleanup();
}
catch (Exception error) {
// Ignore.
}
finally {
myClient = null;
}
/**
* Stops the test client.
*/
@After
public void tearDown() {
try {
myClient.cleanup();
} catch (Exception error) {
// Ignore.
} finally {
myClient = null;
}
}
/**
* {@inheritDoc}
* <p>
* Overriden to return the {@link AsyncMongoDbClient}.
* </p>
*/
@Override
protected DB getDB() {
return myClient;
}
/**
* {@inheritDoc}
* <p>
* Overriden to return the {@link AsyncMongoDbClient}.
* </p>
*/
@Override
protected DB getDB() {
return myClient;
}
}
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
package com.yahoo.ycsb.db;
......@@ -30,48 +30,45 @@ import com.yahoo.ycsb.DB;
*/
public class MongoDbClientTest extends AbstractDBTestCases {
/** The client to use. */
private MongoDbClient myClient = null;
/** The client to use. */
private MongoDbClient myClient = null;
/**
* Start a test client.
*/
@Before
public void setUp() {
myClient = new MongoDbClient();
myClient.setProperties(new Properties());
try {
myClient.init();
}
catch (Exception error) {
assumeNoException(error);
}
/**
* Start a test client.
*/
@Before
public void setUp() {
myClient = new MongoDbClient();
myClient.setProperties(new Properties());
try {
myClient.init();
} catch (Exception error) {
assumeNoException(error);
}
}
/**
* Stops the test client.
*/
@After
public void tearDown() {
try {
myClient.cleanup();
}
catch (Exception error) {
// Ignore.
}
finally {
myClient = null;
}
/**
* Stops the test client.
*/
@After
public void tearDown() {
try {
myClient.cleanup();
} catch (Exception error) {
// Ignore.
} finally {
myClient = null;
}
}
/**
* {@inheritDoc}
* <p>
* Overriden to return the {@link MongoDbClient}.
* </p>
*/
@Override
protected DB getDB() {
return myClient;
}
/**
* {@inheritDoc}
* <p>
* Overriden to return the {@link MongoDbClient}.
* </p>
*/
@Override
protected DB getDB() {
return myClient;
}
}
/*
* #%L
* OptionsSupport.java - mongodb-binding - Yahoo!, Inc.
* %%
* Copyright (C) 2015 Yahoo!, Inc.
* %%
* Licensed 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
*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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.
* #L%
* 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. See accompanying
* LICENSE file.
*/
package com.yahoo.ycsb.db;
import static com.yahoo.ycsb.db.OptionsSupport.updateUrl;
......@@ -30,164 +26,159 @@ import org.junit.Test;
/**
* OptionsSupportTest provides tests for the OptionsSupport class.
*
*
* @author rjm
*/
public class OptionsSupportTest {
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.maxconnections}.
*/
@Test
public void testUpdateUrlMaxConnections() {
assertThat(
updateUrl("mongodb://locahost:27017/",
props("mongodb.maxconnections", "1234")),
is("mongodb://locahost:27017/?maxPoolSize=1234"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.maxconnections", "1234")),
is("mongodb://locahost:27017/?foo=bar&maxPoolSize=1234"));
assertThat(
updateUrl("mongodb://locahost:27017/?maxPoolSize=1",
props("mongodb.maxconnections", "1234")),
is("mongodb://locahost:27017/?maxPoolSize=1"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
*/
@Test
public void testUpdateUrlWaitQueueMultiple() {
assertThat(
updateUrl(
"mongodb://locahost:27017/",
props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
"1234")),
is("mongodb://locahost:27017/?waitQueueMultiple=1234"));
assertThat(
updateUrl(
"mongodb://locahost:27017/?foo=bar",
props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
"1234")),
is("mongodb://locahost:27017/?foo=bar&waitQueueMultiple=1234"));
assertThat(
updateUrl(
"mongodb://locahost:27017/?waitQueueMultiple=1",
props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
"1234")),
is("mongodb://locahost:27017/?waitQueueMultiple=1"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
*/
@Test
public void testUpdateUrlWriteConcern() {
assertThat(
updateUrl("mongodb://locahost:27017/",
props("mongodb.writeConcern", "errors_ignored")),
is("mongodb://locahost:27017/?w=0"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "unacknowledged")),
is("mongodb://locahost:27017/?foo=bar&w=0"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "acknowledged")),
is("mongodb://locahost:27017/?foo=bar&w=1"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "journaled")),
is("mongodb://locahost:27017/?foo=bar&journal=true&j=true"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "replica_acknowledged")),
is("mongodb://locahost:27017/?foo=bar&w=2"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "majority")),
is("mongodb://locahost:27017/?foo=bar&w=majority"));
// w already exists.
assertThat(
updateUrl("mongodb://locahost:27017/?w=1",
props("mongodb.writeConcern", "acknowledged")),
is("mongodb://locahost:27017/?w=1"));
// Unknown options
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
*/
@Test
public void testUpdateUrlReadPreference() {
assertThat(
updateUrl("mongodb://locahost:27017/",
props("mongodb.readPreference", "primary")),
is("mongodb://locahost:27017/?readPreference=primary"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "primary_preferred")),
is("mongodb://locahost:27017/?foo=bar&readPreference=primaryPreferred"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "secondary")),
is("mongodb://locahost:27017/?foo=bar&readPreference=secondary"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "secondary_preferred")),
is("mongodb://locahost:27017/?foo=bar&readPreference=secondaryPreferred"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "nearest")),
is("mongodb://locahost:27017/?foo=bar&readPreference=nearest"));
// readPreference already exists.
assertThat(
updateUrl("mongodb://locahost:27017/?readPreference=primary",
props("mongodb.readPreference", "secondary")),
is("mongodb://locahost:27017/?readPreference=primary"));
// Unknown options
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Factory method for a {@link Properties} object.
*
* @param key
* The key for the property to set.
* @param value
* The value for the property to set.
* @return The {@link Properties} with the property added.
*/
private Properties props(String key, String value) {
Properties props = new Properties();
props.setProperty(key, value);
return props;
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.maxconnections}.
*/
@Test
public void testUpdateUrlMaxConnections() {
assertThat(
updateUrl("mongodb://locahost:27017/",
props("mongodb.maxconnections", "1234")),
is("mongodb://locahost:27017/?maxPoolSize=1234"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.maxconnections", "1234")),
is("mongodb://locahost:27017/?foo=bar&maxPoolSize=1234"));
assertThat(
updateUrl("mongodb://locahost:27017/?maxPoolSize=1",
props("mongodb.maxconnections", "1234")),
is("mongodb://locahost:27017/?maxPoolSize=1"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
*/
@Test
public void testUpdateUrlWaitQueueMultiple() {
assertThat(
updateUrl(
"mongodb://locahost:27017/",
props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
"1234")),
is("mongodb://locahost:27017/?waitQueueMultiple=1234"));
assertThat(
updateUrl(
"mongodb://locahost:27017/?foo=bar",
props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
"1234")),
is("mongodb://locahost:27017/?foo=bar&waitQueueMultiple=1234"));
assertThat(
updateUrl(
"mongodb://locahost:27017/?waitQueueMultiple=1",
props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
"1234")), is("mongodb://locahost:27017/?waitQueueMultiple=1"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
*/
@Test
public void testUpdateUrlWriteConcern() {
assertThat(
updateUrl("mongodb://locahost:27017/",
props("mongodb.writeConcern", "errors_ignored")),
is("mongodb://locahost:27017/?w=0"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "unacknowledged")),
is("mongodb://locahost:27017/?foo=bar&w=0"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "acknowledged")),
is("mongodb://locahost:27017/?foo=bar&w=1"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "journaled")),
is("mongodb://locahost:27017/?foo=bar&journal=true&j=true"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "replica_acknowledged")),
is("mongodb://locahost:27017/?foo=bar&w=2"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.writeConcern", "majority")),
is("mongodb://locahost:27017/?foo=bar&w=majority"));
// w already exists.
assertThat(
updateUrl("mongodb://locahost:27017/?w=1",
props("mongodb.writeConcern", "acknowledged")),
is("mongodb://locahost:27017/?w=1"));
// Unknown options
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
* {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
*/
@Test
public void testUpdateUrlReadPreference() {
assertThat(
updateUrl("mongodb://locahost:27017/",
props("mongodb.readPreference", "primary")),
is("mongodb://locahost:27017/?readPreference=primary"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "primary_preferred")),
is("mongodb://locahost:27017/?foo=bar&readPreference=primaryPreferred"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "secondary")),
is("mongodb://locahost:27017/?foo=bar&readPreference=secondary"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "secondary_preferred")),
is("mongodb://locahost:27017/?foo=bar&readPreference=secondaryPreferred"));
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar",
props("mongodb.readPreference", "nearest")),
is("mongodb://locahost:27017/?foo=bar&readPreference=nearest"));
// readPreference already exists.
assertThat(
updateUrl("mongodb://locahost:27017/?readPreference=primary",
props("mongodb.readPreference", "secondary")),
is("mongodb://locahost:27017/?readPreference=primary"));
// Unknown options
assertThat(
updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
is("mongodb://locahost:27017/?foo=bar"));
}
/**
* Factory method for a {@link Properties} object.
*
* @param key
* The key for the property to set.
* @param value
* The value for the property to set.
* @return The {@link Properties} with the property added.
*/
private Properties props(String key, String value) {
Properties props = new Properties();
props.setProperty(key, value);
return props;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment