Skip to content

Commit

Permalink
[couchbase] final checkstyle edits and enforcing
Browse files Browse the repository at this point in the history
Also removed some unnecessary comments

Closes brianfrankcooper#480
  • Loading branch information
kruthar committed Feb 6, 2016
1 parent 605b5ce commit 72c4950
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 129 deletions.
28 changes: 0 additions & 28 deletions couchbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ LICENSE file.
<name>Couchbase Binding</name>
<packaging>jar</packaging>

<properties>
<checkstyle.failOnViolation>false</checkstyle.failOnViolation>
</properties>

<dependencies>
<dependency>
<groupId>com.couchbase.client</groupId>
Expand All @@ -56,28 +52,4 @@ LICENSE file.
<artifactId>slf4j-api</artifactId>
</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>
</project>
141 changes: 41 additions & 100 deletions couchbase/src/main/java/com/yahoo/ycsb/db/CouchbaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@
package com.yahoo.ycsb.db;

import com.couchbase.client.protocol.views.*;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -47,33 +33,25 @@
import net.spy.memcached.ReplicateTo;
import net.spy.memcached.internal.OperationFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

/**
* A class that wraps the CouchbaseClient to allow it to be interfaced with
* YCSB. This class extends {@link DB} and implements the database interface
* used by YCSB client.
*
* <p>
* The following options must be passed when using this database client.
*
* <ul>
* <li><b>couchbase.url=http://127.0.0.1:8091/pools</b> The connection URL from
* one server.</li>
* <li><b>couchbase.bucket=default</b> The bucket name to use./li>
* <li><b>couchbase.password=</b> The password of the bucket.</li>
* <li><b>couchbase.checkFutures=true</b> If the futures should be inspected
* (makes ops sync).</li>
* <li><b>couchbase.persistTo=0</b> Observe Persistence ("PersistTo" constraint)
* </li>
* <li><b>couchbase.replicateTo=0</b> Observe Replication ("ReplicateTo"
* constraint)</li>
* <li><b>couchbase.json=true</b> Use json or java serialization as target
* format.</li>
* </ul>
*
* @author Michael Nitschinger
* A class that wraps the CouchbaseClient to allow it to be interfaced with YCSB.
* This class extends {@link DB} and implements the database interface used by YCSB client.
*/
public class CouchbaseClient extends DB {

public static final String URL_PROPERTY = "couchbase.url";
public static final String BUCKET_PROPERTY = "couchbase.bucket";
public static final String PASSWORD_PROPERTY = "couchbase.password";
Expand Down Expand Up @@ -123,13 +101,11 @@ public void init() throws DBException {
Double scanproportion = Double.valueOf(props.getProperty(SCAN_PROPERTY, SCAN_PROPERTY_DEFAULT));

Properties systemProperties = System.getProperties();
systemProperties.put("net.spy.log.LoggerImpl",
"net.spy.memcached.compat.log.SLF4JLogger");
systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.SLF4JLogger");
System.setProperties(systemProperties);

try {
client = new com.couchbase.client.CouchbaseClient(
Arrays.asList(new URI(url)), bucket, password);
client = new com.couchbase.client.CouchbaseClient(Arrays.asList(new URI(url)), bucket, password);
} catch (Exception e) {
throw new DBException("Could not create CouchbaseClient object.", e);
}
Expand All @@ -147,14 +123,11 @@ public void init() throws DBException {
/**
* Parse the replicate property into the correct enum.
*
* @param property
* the stringified property value.
* @throws DBException
* if parsing the property did fail.
* @param property the stringified property value.
* @throws DBException if parsing the property did fail.
* @return the correct enum.
*/
private ReplicateTo parseReplicateTo(final String property)
throws DBException {
private ReplicateTo parseReplicateTo(final String property) throws DBException {
int value = Integer.parseInt(property);

switch (value) {
Expand All @@ -174,10 +147,8 @@ private ReplicateTo parseReplicateTo(final String property)
/**
* Parse the persist property into the correct enum.
*
* @param property
* the stringified property value.
* @throws DBException
* if parsing the property did fail.
* @param property the stringified property value.
* @throws DBException if parsing the property did fail.
* @return the correct enum.
*/
private PersistTo parsePersistTo(final String property) throws DBException {
Expand Down Expand Up @@ -208,8 +179,8 @@ public void cleanup() {
}

@Override
public Status read(final String table, final String key,
final Set<String> fields, final HashMap<String, ByteIterator> result) {
public Status read(final String table, final String key, final Set<String> fields,
final HashMap<String, ByteIterator> result) {
String formattedKey = formatKey(table, key);

try {
Expand All @@ -229,30 +200,14 @@ public Status read(final String table, final String key,
}
}

/**
* Scan is currently not implemented.
*
* @param table
* The name of the table
* @param startkey
* The record key of the first record to read.
* @param recordcount
* The number of records to read
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A Vector of HashMaps, where each HashMap is a set field/value
* pairs for one record
* @return Status.ERROR, because not implemented yet.
*/
@Override
public Status scan(final String table, final String startkey, final int recordcount, final Set<String> fields,
final Vector<HashMap<String, ByteIterator>> result) {
try {
Query query = new Query().setRangeStart(startkey)
.setLimit(recordcount)
.setIncludeDocs(true)
.setStale(stale);
.setLimit(recordcount)
.setIncludeDocs(true)
.setStale(stale);
ViewResponse response = client.query(view, query);

for (ViewRow row : response) {
Expand All @@ -270,13 +225,11 @@ public Status scan(final String table, final String startkey, final int recordco
}

@Override
public Status update(final String table, final String key,
final HashMap<String, ByteIterator> values) {
public Status update(final String table, final String key, final HashMap<String, ByteIterator> values) {
String formattedKey = formatKey(table, key);

try {
final OperationFuture<Boolean> future =
client.replace(formattedKey, encode(values), persistTo, replicateTo);
final OperationFuture<Boolean> future = client.replace(formattedKey, encode(values), persistTo, replicateTo);
return checkFutureStatus(future);
} catch (Exception e) {
if (log.isErrorEnabled()) {
Expand All @@ -287,13 +240,11 @@ public Status update(final String table, final String key,
}

@Override
public Status insert(final String table, final String key,
final HashMap<String, ByteIterator> values) {
public Status insert(final String table, final String key, final HashMap<String, ByteIterator> values) {
String formattedKey = formatKey(table, key);

try {
final OperationFuture<Boolean> future =
client.add(formattedKey, encode(values), persistTo, replicateTo);
final OperationFuture<Boolean> future = client.add(formattedKey, encode(values), persistTo, replicateTo);
return checkFutureStatus(future);
} catch (Exception e) {
if (log.isErrorEnabled()) {
Expand All @@ -308,8 +259,7 @@ public Status delete(final String table, final String key) {
String formattedKey = formatKey(table, key);

try {
final OperationFuture<Boolean> future =
client.delete(formattedKey, persistTo, replicateTo);
final OperationFuture<Boolean> future = client.delete(formattedKey, persistTo, replicateTo);
return checkFutureStatus(future);
} catch (Exception e) {
if (log.isErrorEnabled()) {
Expand All @@ -322,10 +272,8 @@ public Status delete(final String table, final String key) {
/**
* Prefix the key with the given prefix, to establish a unique namespace.
*
* @param prefix
* the prefix to use.
* @param key
* the actual key.
* @param prefix the prefix to use.
* @param key the actual key.
* @return the formatted and prefixed key.
*/
private String formatKey(final String prefix, final String key) {
Expand All @@ -335,8 +283,7 @@ private String formatKey(final String prefix, final String key) {
/**
* Wrapper method that either inspects the future or not.
*
* @param future
* the future to potentially verify.
* @param future the future to potentially verify.
* @return the status of the future result.
*/
private Status checkFutureStatus(final OperationFuture<?> future) {
Expand All @@ -350,15 +297,11 @@ private Status checkFutureStatus(final OperationFuture<?> future) {
/**
* Decode the object from server into the storable result.
*
* @param source
* the loaded object.
* @param fields
* the fields to check.
* @param dest
* the result passed back to the ycsb core.
* @param source the loaded object.
* @param fields the fields to check.
* @param dest the result passed back to the ycsb core.
*/
private void decode(final Object source, final Set<String> fields,
final HashMap<String, ByteIterator> dest) {
private void decode(final Object source, final Set<String> fields, final HashMap<String, ByteIterator> dest) {
if (useJson) {
try {
JsonNode json = JSON_MAPPER.readTree((String) source);
Expand Down Expand Up @@ -388,8 +331,7 @@ private void decode(final Object source, final Set<String> fields,
/**
* Encode the object for couchbase storage.
*
* @param source
* the source value.
* @param source the source value.
* @return the storable object.
*/
private Object encode(final HashMap<String, ByteIterator> source) {
Expand All @@ -412,5 +354,4 @@ private Object encode(final HashMap<String, ByteIterator> source) {
}
return writer.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
* Copyright (c) 2015 - 2016 YCSB contributors. 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
Expand Down

0 comments on commit 72c4950

Please sign in to comment.