diff --git a/tarantool/pom.xml b/tarantool/pom.xml index 6754818e..b8818f58 100644 --- a/tarantool/pom.xml +++ b/tarantool/pom.xml @@ -47,4 +47,29 @@ LICENSE file. provided + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.15 + + true + ../checkstyle.xml + true + true + + + + validate + validate + + checkstyle + + + + + + diff --git a/tarantool/src/main/java/com/yahoo/ycsb/db/TarantoolClient.java b/tarantool/src/main/java/com/yahoo/ycsb/db/TarantoolClient.java index b90dff11..e86120e1 100644 --- a/tarantool/src/main/java/com/yahoo/ycsb/db/TarantoolClient.java +++ b/tarantool/src/main/java/com/yahoo/ycsb/db/TarantoolClient.java @@ -1,12 +1,12 @@ /** - * Copyright (c) 2014, Yahoo!, Inc. All rights reserved. - * + * Copyright (c) 2014 - 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 * 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 @@ -16,162 +16,137 @@ */ package com.yahoo.ycsb.db; -import com.yahoo.ycsb.ByteIterator; -import com.yahoo.ycsb.DB; -import com.yahoo.ycsb.DBException; -import com.yahoo.ycsb.Status; -import com.yahoo.ycsb.StringByteIterator; - +import com.yahoo.ycsb.*; import org.tarantool.TarantoolConnection16; import org.tarantool.TarantoolConnection16Impl; import org.tarantool.TarantoolException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.Vector; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; +/** + * YCSB binding for Tarantool. + */ public class TarantoolClient extends DB { + private static final Logger LOGGER = Logger.getLogger(TarantoolClient.class.getName()); - public static final String HOST_PROPERTY = "tarantool.host"; - public static final String PORT_PROPERTY = "tarantool.port"; - public static final String SPACE_PROPERTY = "tarantool.space"; + private static final String HOST_PROPERTY = "tarantool.host"; + private static final String PORT_PROPERTY = "tarantool.port"; + private static final String SPACE_PROPERTY = "tarantool.space"; + private static final String DEFAULT_HOST = "localhost"; + private static final String DEFAULT_PORT = "3301"; + private static final String DEFAULT_SPACE = "1024"; - public static final String DEFAULT_HOST = "localhost"; - public static final int DEFAULT_PORT = 3301; - public static final int DEFAULT_SPACE = 1024; + private TarantoolConnection16 connection; + private int spaceNo; - private static final Logger logger = Logger.getLogger(TarantoolClient.class.getName()); - private TarantoolConnection16 connection; - private int spaceNo; + public void init() throws DBException { + Properties props = getProperties(); - public void init() throws DBException { - Properties props = getProperties(); + int port = Integer.parseInt(props.getProperty(PORT_PROPERTY, DEFAULT_PORT)); + String host = props.getProperty(HOST_PROPERTY, DEFAULT_HOST); + spaceNo = Integer.parseInt(props.getProperty(SPACE_PROPERTY, DEFAULT_SPACE)); - int port = DEFAULT_PORT; - String portString = props.getProperty(PORT_PROPERTY); - if (portString != null) { - port = Integer.parseInt(portString); - } + try { + this.connection = new TarantoolConnection16Impl(host, port); + } catch (Exception exc) { + throw new DBException("Can't initialize Tarantool connection", exc); + } + } - String host = props.getProperty(HOST_PROPERTY); - if (host == null) { - host = DEFAULT_HOST; - } + public void cleanup() throws DBException { + this.connection.close(); + } - spaceNo = DEFAULT_SPACE; - String spaceString = props.getProperty(SPACE_PROPERTY); - if (spaceString != null) { - spaceNo = Integer.parseInt(spaceString); - } + @Override + public Status insert(String table, String key, HashMap values) { + return replace(key, values, "Can't insert element"); + } - try { - this.connection = new TarantoolConnection16Impl(host, port); - } catch (Exception exc) { - logger.log(Level.SEVERE,"Can't initialize Tarantool connection", exc); - return; - } - } + private HashMap tupleConvertFilter(List input, Set fields) { + HashMap result = new HashMap<>(); + if (input == null) { + return result; + } + for (int i = 1; i < input.toArray().length; i += 2) { + if (fields == null || fields.contains(input.get(i))) { + result.put(input.get(i), new StringByteIterator(input.get(i + 1))); + } + } + return result; + } - public void cleanup() throws DBException{ - this.connection.close(); - } + @Override + public Status read(String table, String key, Set fields, HashMap result) { + try { + List response = this.connection.select(this.spaceNo, 0, Arrays.asList(key), 0, 1, 0); + result = tupleConvertFilter(response, fields); + return Status.OK; + } catch (TarantoolException exc) { + LOGGER.log(Level.SEVERE, "Can't select element", exc); + return Status.ERROR; + } catch (NullPointerException exc) { + return Status.ERROR; + } + } - @Override - public Status insert(String table, String key, HashMap values) { - return replace(key, values, "Can't insert element"); - } + @Override + public Status scan(String table, String startkey, + int recordcount, Set fields, + Vector> result) { + List> response; + try { + response = this.connection.select(this.spaceNo, 0, Arrays.asList(startkey), 0, recordcount, 6); + } catch (TarantoolException exc) { + LOGGER.log(Level.SEVERE, "Can't select range elements", exc); + return Status.ERROR; + } catch (NullPointerException exc) { + return Status.ERROR; + } + for (List i : response) { + HashMap temp = tupleConvertFilter(i, fields); + if (!temp.isEmpty()) { + result.add((HashMap) temp.clone()); + } + } + return Status.OK; + } - private HashMap tuple_convert_filter (List input, - Set fields) { - HashMap result = new HashMap(); - if (input == null) - return result; - for (int i = 1; i < input.toArray().length; i += 2) - if (fields == null || fields.contains(input.get(i))) - result.put(input.get(i), new StringByteIterator(input.get(i+1))); - return result; - } + @Override + public Status delete(String table, String key) { + try { + this.connection.delete(this.spaceNo, Collections.singletonList(key)); + } catch (TarantoolException exc) { + LOGGER.log(Level.SEVERE, "Can't delete element", exc); + return Status.ERROR; + } catch (NullPointerException e) { + return Status.ERROR; + } + return Status.OK; + } - @Override - public Status read(String table, String key, Set fields, - HashMap result) { - try { - List response; - response = this.connection.select(this.spaceNo, 0, Arrays.asList(key), 0, 1, 0); - result = tuple_convert_filter(response, fields); - return Status.OK; - } catch (TarantoolException exc) { - logger.log(Level.SEVERE,"Can't select element", exc); - return Status.ERROR; - } catch (NullPointerException exc) { - return Status.ERROR; - } - } + @Override + public Status update(String table, String key, HashMap values) { + return replace(key, values, "Can't replace element"); + } - @Override - public Status scan(String table, String startkey, - int recordcount, Set fields, - Vector> result) { - List> response; - try { - response = this.connection.select(this.spaceNo, 0, Arrays.asList(startkey), 0, recordcount, 6); - } catch (TarantoolException exc) { - logger.log(Level.SEVERE,"Can't select range elements", exc); - return Status.ERROR; - } catch (NullPointerException exc) { - return Status.ERROR; - } - for(List i: response) { - HashMap temp = tuple_convert_filter(i, fields); - if (!temp.isEmpty()) - result.add((HashMap) temp.clone()); - } - return Status.OK; - } + private Status replace(String key, HashMap values, String exceptionDescription) { + int j = 0; + String[] tuple = new String[1 + 2 * values.size()]; + tuple[0] = key; + for (Map.Entry i : values.entrySet()) { + tuple[j + 1] = i.getKey(); + tuple[j + 2] = i.getValue().toString(); + j += 2; + } + try { + this.connection.replace(this.spaceNo, tuple); + } catch (TarantoolException exc) { + LOGGER.log(Level.SEVERE, exceptionDescription, exc); + return Status.ERROR; + } + return Status.OK; - @Override - public Status delete(String table, String key) { - try { - this.connection.delete(this.spaceNo, Arrays.asList(key)); - } catch (TarantoolException exc) { - logger.log(Level.SEVERE,"Can't delete element", exc); - return Status.ERROR; - } catch (NullPointerException e) { - return Status.ERROR; - } - return Status.OK; - } - @Override - public Status update(String table, String key, - HashMap values) { - return replace(key, values, "Can't replace element"); - - } - - private Status replace(String key, - HashMap values, - String exceptionDescription) { - int j = 0; - String[] tuple = new String[1 + 2 * values.size()]; - tuple[0] = key; - for (Map.Entry i: values.entrySet()) { - tuple[j + 1] = i.getKey(); - tuple[j + 2] = i.getValue().toString(); - j += 2; - } - try { - this.connection.replace(this.spaceNo, tuple); - } catch (TarantoolException exc) { - logger.log(Level.SEVERE,exceptionDescription, exc); - return Status.ERROR; - } - return Status.OK; - - } + } } diff --git a/tarantool/src/main/java/com/yahoo/ycsb/db/package-info.java b/tarantool/src/main/java/com/yahoo/ycsb/db/package-info.java new file mode 100644 index 00000000..1f34b7c9 --- /dev/null +++ b/tarantool/src/main/java/com/yahoo/ycsb/db/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2014 - 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 + * 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. + */ + +/** + * YCSB binding for Tarantool. + */ +package com.yahoo.ycsb.db; +