Mercurial > hg > release > thermostat-2.0
changeset 1584:f2a417f1cc51
Move statement engine project to exported but experimental.
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-November/011659.html
reviewed-by: omajid, vanaltj, jerboaa
line wrap: on
line diff
--- a/storage/core/pom.xml Mon Dec 01 08:51:43 2014 -0500 +++ b/storage/core/pom.xml Wed Dec 03 16:01:36 2014 +0100 @@ -66,6 +66,7 @@ com.redhat.thermostat.storage.connect, com.redhat.thermostat.storage.core, com.redhat.thermostat.storage.core.experimental, + com.redhat.thermostat.storage.core.experimental.statement, com.redhat.thermostat.storage.core.auth, com.redhat.thermostat.storage.config, com.redhat.thermostat.storage.model,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapter.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,183 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.Cursor; +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.PreparedStatement; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.core.StatementExecutionException; +import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.storage.model.Pojo; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + */ +public class BeanAdapter<T extends Pojo> { + public static final Id INSERT = new Id("BeanAdapter_Insert"); + + private static final Logger logger = LoggingUtils.getLogger(BeanAdapter.class); + + protected Category<T> category; + + private Map<Id, Statement> describedQueries; + private Map<Id, Query<T>> queries; + private List<FieldDescriptor> fieldDescriptors; + + public BeanAdapter() { + this.queries = new HashMap<>(); + this.describedQueries = new HashMap<>(); + } + + protected void setCategory(Category<T> category) { + this.category = category; + } + + public Category<T> getCategory() { + return category; + } + + public Set<String> describeStatements() { + Set<String> descriptions = new HashSet<>(); + for (Statement statement : describedQueries.values()) { + descriptions.add(statement.get()); + } + return descriptions; + } + + public void insert(T bean, Storage storage) + throws StatementExecutionException + { + Statement statement = describedQueries.get(INSERT); + + StatementDescriptor<T> desc = + new StatementDescriptor<>(category, statement.get()); + try { + PreparedStatement<T> prepared = storage.prepareStatement(desc); + int i = 0; + for (FieldDescriptor descriptor : fieldDescriptors) { + Value value = StatementUtils.getValue(bean, descriptor); + StatementUtils.setData(prepared, value, i++); + } + + prepared.execute(); + + } catch (DescriptorParsingException e) { + logger.log(Level.SEVERE, "Preparing stmt '" + desc + "' failed!", e); + } + } + + public Query<T> getQuery(Id id) { + return queries.get(id); + } + + public void query(QueryValues values, ResultHandler<T> handler, + Storage storage) throws StatementExecutionException + { + Objects.requireNonNull(handler, "ResultHandler cannot be null"); + Objects.requireNonNull(values, "QueryValues cannot be null"); + Objects.requireNonNull(storage, "Storage cannot be null"); + + Query query = values.getQuery(); + if (!queries.containsKey(query.getId())) { + throw new IllegalArgumentException("This adapter does not know" + + "about the given query: " + + query.getId()); + } + + Statement statement = describedQueries.get(query.getId()); + List<Criterion> criteria = query.describe(); + + StatementDescriptor<T> desc = + new StatementDescriptor<>(category, statement.get()); + PreparedStatement<T> prepared = null; + try { + prepared = storage.prepareStatement(desc); + int i = 0; + for (Criterion criterion : criteria) { + // sort doesn't take values + if (criterion instanceof SortCriterion) { + continue; + } + + Value value = values.getValue(criterion); + StatementUtils.setData(prepared, value, i++); + } + + } catch (DescriptorParsingException e) { + logger.log(Level.SEVERE, "Preparing stmt '" + desc + + "' failed!", e); + + // this can't really happen, but if it does is serious + throw new AssertionError("Autogenerated statement failed to parse", + e); + } + + Cursor<T> cursor = prepared.executeQuery(); + while (cursor.hasNext()) { + T result = cursor.next(); + try { + handler.onResult(result); + + } catch (Throwable t) { + logger.log(Level.SEVERE, "Exception executing results", t); + } + } + } + + protected void addStatement(Id id, Statement statement) { + describedQueries.put(id, statement); + } + + protected void addQuery(Id id, Query query) { + queries.put(id, query); + } + + protected void setFieldDescriptors(List<FieldDescriptor> fieldDescriptors) { + this.fieldDescriptors = fieldDescriptors; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapterBuilder.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,124 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.model.Pojo; +import java.util.ArrayList; +import java.util.List; + +/** + * + */ +public class BeanAdapterBuilder<T extends Pojo> { + + private Class<T> target; + private BeanAdapter<T> adapter; + + private List<Query<T>> queries; + + public BeanAdapterBuilder(Class<T> target, List<Query<T>> queries) { + this.target = target; + this.queries = queries; + } + + public BeanAdapterBuilder(Class<T> target, Query<T> query) { + this(target, new ArrayList<Query<T>>()); + queries.add(query); + } + + public BeanAdapter<T> build() { + try { + Category<T> category = new CategoryBuilder<>(target).build(); + List<FieldDescriptor> fieldDescriptors = + StatementUtils.createDescriptors(target); + + adapter = new BeanAdapter<>(); + adapter.setCategory(category); + adapter.setFieldDescriptors(fieldDescriptors); + + createInsert(fieldDescriptors); + createQueries(); + + return adapter; + + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + protected void createQueries() { + for (Query<T> query : queries) { + createQuery(query); + } + } + + private void createQuery(Query<T> query) { + List<Criterion> queryDescriptors = query.describe(); + + QueryEngine engine = new QueryEngine(); + engine.prologue(adapter.getCategory()); + for (Criterion criterion : queryDescriptors) { + if (criterion instanceof WhereCriterion) { + WhereCriterion where = (WhereCriterion) criterion; + engine.add(where.getFieldDescriptor(), where.getCriteria()); + + } else if (criterion instanceof SortCriterion) { + SortCriterion sort = (SortCriterion) criterion; + engine.sort(sort.getFieldDescriptor(), sort.getCriteria()); + + } else if (criterion instanceof LimitCriterion) { + engine.limit(); + } + } + + adapter.addStatement(query.getId(), engine.build()); + adapter.addQuery(query.getId(), query); + } + + protected void createInsert(List<FieldDescriptor> descriptors) { + + InsertEngine engine = new InsertEngine(); + engine.prologue(adapter.getCategory()); + + for (FieldDescriptor descriptor : descriptors) { + engine.add(descriptor); + } + + adapter.addStatement(BeanAdapter.INSERT, engine.build()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Category.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +public @interface Category { + String value(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/CategoryBuilder.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,138 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.storage.model.Pojo; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + */ +public class CategoryBuilder<T extends Pojo> { + + private static final String LOCK = new String("CategoryBuilder_lock"); + private static final Map<String, Category> categories = new HashMap<>(); + + private Class<T> bean; + + public CategoryBuilder(Class<T> bean) { + this.bean = bean; + } + + public Category<T> build() { + + synchronized (LOCK) { + + String document = StatementUtils.getDocument(bean); + + // This helps performance as well, but it's needed because Category + // doesn't allow us to create the same twice, even with all the + // same data; categories are in fact some kind of singleton + if (categories.containsKey(document)) { + return categories.get(document); + } + + List<FieldDescriptor> descriptors = + StatementUtils.createDescriptors(bean); + + // we first build the keys and the indexed keys + List<Key> indexed = new ArrayList<>(); + List<Key> keys = new ArrayList<>(); + + // key are easy, the arguments are in the form: + // new Key<Type>(String name), we don't need reflection + + for (FieldDescriptor descriptor : descriptors) { + + Key key = new Key(descriptor.getName()); + + keys.add(key); + if (descriptor.isIndexed()) { + indexed.add(key); + } + } + + List<Class> argumentClasses = new ArrayList<>(); + argumentClasses.add(String.class); + argumentClasses.add(Class.class); + argumentClasses.add(List.class); + + List argumentObjects = new ArrayList(); + argumentObjects.add(document); + argumentObjects.add(bean); + argumentObjects.add(keys); + + // indexed keys are optional + if (!indexed.isEmpty()) { + argumentClasses.add(List.class); + argumentObjects.add(indexed); + } + + Class[] classes = argumentClasses.toArray(new Class[argumentClasses.size()]); + Object[] objects = argumentObjects.toArray(); + + Category<T> category = create(classes, objects); + categories.put(document, category); + + return category; + } + } + + private Category<T> create(Class[] classes, Object[] objects) { + + // we are using this constructor: + // Category(String name, Class<T> dataClass, + // List<Key<?>> keys, List<Key<?>> indexedKeys) + + try { + Constructor<Category> constructor = + Category.class.getConstructor(classes); + constructor.setAccessible(true); + + return constructor.newInstance(objects); + + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Criterion.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +public interface Criterion { + Id getId(); + Class<?> getType(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/FieldDescriptor.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,95 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.util.Objects; + +/** + * + */ +public class FieldDescriptor { + private Class<?> type; + private String name; + private boolean indexed; + + public void setType(Class<?> type) { + this.type = type; + } + + public Class<?> getType() { + return type; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setIndexed(boolean indexed) { + this.indexed = indexed; + } + + public boolean isIndexed() { + return indexed; + } + + @Override + public String toString() { + return "[FieldDescriptor: " + + "type = " + type + + ", name = '" + name + '\'' + + ", indexed = " + indexed + + ']'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + FieldDescriptor that = (FieldDescriptor) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return name != null ? name.hashCode() : 0; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Id.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.util.UUID; + +/** + * + */ +public class Id { + private String id; + + public Id() { + id = UUID.randomUUID().toString(); + } + + public Id(String id) { + this.id = id; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Id sessionID = (Id) o; + + if (id != null ? !id.equals(sessionID.id) : sessionID.id != null) + return false; + + return true; + } + + public String get() { + return id; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Indexed.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +public @interface Indexed { +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/InsertEngine.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,59 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +class InsertEngine extends StatementEngine { + + public InsertEngine() { + super(TypeMapper.Statement.Insert); + delimiter = TypeMapper.Symbol.InsertSeparator.id(); + tokens.add(TypeMapper.Clause.Add.id()); + } + + public InsertEngine add(FieldDescriptor descriptor) { + super.add(descriptor, TypeMapper.Criteria.Equal); + return this; + } + + @Override + protected void addPrologueClause() { + tokens.add(TypeMapper.Clause.Set.id()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/LimitCriterion.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +public class LimitCriterion implements Criterion { + + private Id id; + + public LimitCriterion(Id id) { + this.id = id; + } + + @Override + public Id getId() { + return id; + } + + @Override + public Class<?> getType() { + return int.class; + } + + @Override + public String toString() { + return "Limit"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Query.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,102 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.model.Pojo; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + */ +public abstract class Query<T extends Pojo> { + + protected final class Criteria { + private List<Criterion> criteria; + private Map<Id, Criterion> map; + + Criteria() { + criteria = new ArrayList<>(); + map = new HashMap<>(); + } + + public void add(Criterion criterion) { + Id id = criterion.getId(); + if (map.containsKey(id)) { + throw new IllegalArgumentException("Already contains criteria" + + " with this id." + + " New: " + criterion + + " Old: " + map.get(id)); + } + map.put(id, criterion); + criteria.add(criterion); + } + } + + private List<Criterion> describedQuery; + private Criteria criteria; + + public Query() { + criteria = new Criteria(); + } + + public abstract Id getId(); + + public final List<Criterion> describe() { + if (describedQuery == null) { + describe(criteria); + describedQuery = Collections.unmodifiableList(criteria.criteria); + } + return describedQuery; + } + + protected abstract void describe(Criteria criteria); + + public final QueryValues createValues() { + if (describedQuery == null) { + throw new IllegalStateException("Query must be described first"); + } + + QueryValues setter = new QueryValues(this); + setter.addCriteria(describedQuery); + + return setter; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/QueryEngine.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,130 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import static com.redhat.thermostat.storage.core.experimental.statement.TypeMapper.Symbol; +import static com.redhat.thermostat.storage.core.experimental.statement.TypeMapper.Clause; +import static com.redhat.thermostat.storage.core.experimental.statement.TypeMapper.Symbol.Quote; +import static com.redhat.thermostat.storage.core.experimental.statement.TypeMapper.Symbol.Sort; +import static com.redhat.thermostat.storage.core.experimental.statement.TypeMapper.Symbol.Space; + +/** + * + */ +class QueryEngine extends StatementEngine { + + private String limit; + private String sort; + + public QueryEngine() { + super(TypeMapper.Statement.Query); + addDelimiter = false; + delimiter = TypeMapper.Symbol.InsertSeparator.id(); + delimiter = Symbol.QuerySeparator.id(); + tokens.add(Clause.Query.id()); + } + + public QueryEngine add(FieldDescriptor descriptor, TypeMapper.Criteria criteria) { + + if (addDelimiter) { + tokens.add(delimiter); + } + + addDelimiter = true; + + StringBuilder field = new StringBuilder(); + + field.append(Quote.id()); + field.append(descriptor.getName()); + field.append(Quote.id()); + + tokens.add(field.toString()); + + tokens.add(criteria.id()); + tokens.add(TypeMapper.get(descriptor.getType())); + + return this; + } + + public QueryEngine sort(FieldDescriptor descriptor, TypeMapper.Sort criteria) { + + StringBuilder field = new StringBuilder(); + + field.append(Sort.id()); + field.append(Space.id()); + + field.append(Quote.id()); + field.append(descriptor.getName()); + field.append(Quote.id()); + + field.append(Space.id()); + field.append(criteria.id()); + + sort = field.toString(); + + return this; + } + + public QueryEngine limit() { + + StringBuilder field = new StringBuilder(); + + field.append(Symbol.Limit.id()); + field.append(Space.id()); + field.append(TypeMapper.get(int.class)); + + limit = field.toString(); + + return this; + } + + @Override + protected void addPrologueClause() { + tokens.add(Clause.Where.id()); + } + + @Override + protected void buildImpl(StringBuilder builder) { + if (sort != null) { + builder.append(Space.id()).append(sort); + } + + if (limit != null) { + builder.append(Space.id()).append(limit); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/QueryValues.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,157 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + */ +public class QueryValues { + + private static final Class EMPTY_VALUE = QueryValues.class; + + private Query query; + private Map<Id, Value> values; + private Map<Id, Class> types; + + protected QueryValues(Query query) { + this.query = query; + values = new HashMap<>(); + types = new HashMap<>(); + } + + public Query getQuery() { + return query; + } + + public void set(Id criteriaId, String value) { + setImpl(criteriaId, value); + } + + public void set(Id criteriaId, long value) { + setImpl(criteriaId, value); + } + + public void set(Id criteriaId, int value) { + setImpl(criteriaId, value); + } + + public void set(Id criteriaId, boolean value) { + setImpl(criteriaId, value); + } + + private void setImpl(Id criteriaId, Object value) { + if (!values.containsKey(criteriaId)) { + throw new IllegalArgumentException("Query does not contain this criteria"); + } + check(criteriaId, value); + } + + private void check(Id criteriaId, Object value) { + + Class type = value.getClass(); + Class targetType = types.get(criteriaId); + + boolean match = false; + if (targetType.isAssignableFrom(int.class) || + targetType.isAssignableFrom(Integer.class)) + { + if (type.isAssignableFrom(int.class) || + type.isAssignableFrom(Integer.class)) + { + match = true; + } + } else if (targetType.isAssignableFrom(long.class) || + targetType.isAssignableFrom(Long.class)) + { + if (type.isAssignableFrom(long.class) || + type.isAssignableFrom(Long.class)) + { + match = true; + } + } else if (targetType.isAssignableFrom(boolean.class) || + targetType.isAssignableFrom(Boolean.class)) + { + if (type.isAssignableFrom(boolean.class) || + type.isAssignableFrom(Boolean.class)) + { + match = true; + } + } else if (targetType.isAssignableFrom(String.class)) { + if (type.isAssignableFrom(String.class)) + { + match = true; + } + } + + if (!match) { + throw new IllegalArgumentException("value type does not match " + + "target criteria with id: " + + criteriaId.get() + + " Wanted: " + + targetType + ", received: " + + type); + } + + values.put(criteriaId, new Value(value)); + } + + public void set(Criterion criterion, Object value) { + setImpl(criterion.getId(), value); + } + + Value getValue(Criterion criterion) { + return values.get(criterion.getId()); + } + + void addCriteria(List<Criterion> _criteria) { + for (Criterion criterion : _criteria) { + + // sort doesn't take values + if (criterion instanceof SortCriterion) { + continue; + } + + Id id = criterion.getId(); + values.put(id, new Value(EMPTY_VALUE)); + types.put(id, criterion.getType()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/ResultHandler.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +public interface ResultHandler<T> { + void onResult(T result); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/SortCriterion.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +public class SortCriterion implements Criterion { + private FieldDescriptor descriptor; + private TypeMapper.Sort criteria; + + private Id id; + + public SortCriterion(FieldDescriptor descriptor, + TypeMapper.Sort criteria) + { + this(new Id(descriptor.getName() + ":" + criteria.name()), descriptor, criteria); + } + + private SortCriterion(Id id, + FieldDescriptor descriptor, + TypeMapper.Sort criteria) + { + this.descriptor = descriptor; + this.criteria = criteria; + } + + @Override + public Class<?> getType() { + return descriptor.getType(); + } + + public FieldDescriptor getFieldDescriptor() { + return descriptor; + } + + public TypeMapper.Sort getCriteria() { + return criteria; + } + + @Override + public Id getId() { + return id; + } + + @Override + public String toString() { + return "[Sort: " + criteria.name() + " -: " + descriptor + "]"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Statement.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,82 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +class Statement { + private String statement; + + public Statement(String statement) { + this.statement = statement; + } + + public String get() { + return statement; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + + if (o == null) { + return false; + } + + if (o instanceof String && statement != null) { + return o.equals(statement); + } + + if (getClass() != o.getClass()) return false; + + Statement statement1 = (Statement) o; + + if (statement != null ? !statement.equals(statement1.statement) : + statement1.statement != null) + { + return false; + } + + return true; + } + + @Override + public int hashCode() { + return statement != null ? statement.hashCode() : 0; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/StatementEngine.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,106 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.model.Pojo; +import java.util.LinkedList; +import java.util.List; + +/** + * + */ +abstract class StatementEngine { + + protected List<String> tokens; + + private TypeMapper.Statement type; + protected String delimiter; + + protected boolean addDelimiter; + + protected StatementEngine(TypeMapper.Statement type) { + tokens = new LinkedList<>(); + this.type = type; + addDelimiter = false; + } + + protected StatementEngine add(FieldDescriptor descriptor, TypeMapper.Criteria criteria) { + + if (addDelimiter) { + tokens.add(delimiter); + } + + addDelimiter = true; + + StringBuilder field = new StringBuilder(); + + field.append(TypeMapper.Symbol.Quote.id()); + field.append(descriptor.getName()); + field.append(TypeMapper.Symbol.Quote.id()); + + tokens.add(field.toString()); + + tokens.add(criteria.id()); + tokens.add(TypeMapper.get(descriptor.getType())); + + return this; + } + + protected abstract void addPrologueClause(); + + public StatementEngine prologue(com.redhat.thermostat.storage.core.Category<? extends Pojo> category) { + tokens.add(category.getName()); + addPrologueClause(); + return this; + } + + protected void buildImpl(StringBuilder builder) {} + + + public Statement build() { + StringBuilder builder = new StringBuilder(); + for (String token : tokens) { + builder.append(token).append(TypeMapper.Symbol.Space.id()); + } + int position = builder.length() - 1; + builder.deleteCharAt(position); + + buildImpl(builder); + + return new Statement(builder.toString()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/StatementUtils.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,179 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.core.PreparedStatement; +import com.redhat.thermostat.storage.model.Pojo; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + */ +public class StatementUtils { + + static boolean isPersistent(PropertyDescriptor desc) { + return hasAnnotation(desc, Persist.class); + } + + static boolean isIndexed(PropertyDescriptor desc) { + return hasAnnotation(desc, Indexed.class); + } + + static <T extends Pojo> String getDocument(Class<T> pojoClass) { + if (!pojoClass.isAnnotationPresent(Category.class)) { + return null; + } + + return pojoClass.getAnnotation(Category.class).value(); + } + + static boolean hasAnnotation(PropertyDescriptor desc, + Class<? extends Annotation> annotation) + { + Method readMethod = desc.getReadMethod(); + boolean hasRead = (readMethod != null && + readMethod.isAnnotationPresent(annotation)); + + Method writeMethod = desc.getWriteMethod(); + boolean hasWrite = (writeMethod != null && + writeMethod.isAnnotationPresent(annotation)); + + return hasRead && hasWrite; + } + + public static Map<String, FieldDescriptor> createDescriptorMap(List<FieldDescriptor> descriptors) { + Map<String, FieldDescriptor> map = new HashMap<>(); + for (FieldDescriptor desc : descriptors) { + map.put(desc.getName(), desc); + } + return map; + } + + public static <T extends Pojo> List<FieldDescriptor> createDescriptors(Class<T> pojoClass) { + try { + BeanInfo info = Introspector.getBeanInfo(pojoClass); + PropertyDescriptor[] props = info.getPropertyDescriptors(); + + List<FieldDescriptor> descriptors = new ArrayList<>(); + for (PropertyDescriptor desc : props) { + + if (StatementUtils.isPersistent(desc)) { + FieldDescriptor descriptor = new FieldDescriptor(); + descriptor.setType(desc.getPropertyType()); + descriptor.setName(desc.getName()); + descriptor.setIndexed(StatementUtils.isIndexed(desc)); + + descriptors.add(descriptor); + } + } + + // Afaik it's not specified that methods are returned or listed in + // any particular order by Introspector.getBeanInfo, so if we don't + // sort, we may end up with two differently ordered lists in two + // different calls to this method. + // This is a problem because the prepated statement API doesn't + // check if two statements are the same if fields are sorted in + // random order, it only considers two statements the same if + // their string representation completely matches; + // it also make the code depending on FieldDescriptor easier to + // write if it can count on the ordering being always consistent + Collections.sort(descriptors, new Comparator<FieldDescriptor>() { + @Override + public int compare(FieldDescriptor o1, FieldDescriptor o2) { + return o1.getName().compareTo(o2.getName()); + } + }); + + return Collections.unmodifiableList(descriptors); + + } catch (IntrospectionException e) { + throw new RuntimeException(e); + } + } + + static <T extends Pojo> Value getValue(T bean, FieldDescriptor descriptor) + { + try { + PropertyDescriptor property = + new PropertyDescriptor(descriptor.getName(), + bean.getClass()); + Method method = property.getReadMethod(); + method.setAccessible(true); + + return new Value(method.invoke(bean, new Object[0])); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + static <T extends Pojo> void setData(PreparedStatement<T> prepared, + Value value, int index) + { + Class type = value.get().getClass(); + if (type.isAssignableFrom(int.class) || + type.isAssignableFrom(Integer.class)) + { + prepared.setInt(index, ((Integer) value.get()).intValue()); + + } else if (type.isAssignableFrom(long.class) || + type.isAssignableFrom(Long.class)) + { + prepared.setLong(index, ((Long) value.get()).longValue()); + + } else if (type.isAssignableFrom(boolean.class) || + type.isAssignableFrom(Boolean.class)) + { + prepared.setBoolean(index, ((Boolean) value.get()).booleanValue()); + + } else if (type.isAssignableFrom(String.class)) { + prepared.setString(index, (String) value.get()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/TypeMapper.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,142 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.util.HashMap; +import java.util.Map; + +/** + * + */ +public class TypeMapper { + + private static final Map<Class, String> map = new HashMap<>(); + static { + map.put(String.class, "?s"); + map.put(int.class, "?i"); + map.put(long.class, "?l"); + map.put(boolean.class, "?b"); + } + + static String get(Class type) { + return map.get(type); + } + + static enum Symbol { + + QuerySeparator("AND"), + InsertSeparator(","), + + Quote("'"), + Space(" "), + + Sort("SORT"), + Limit("LIMIT"), + + ; + + private final String definition; + Symbol(String definition) { + this.definition = definition; + } + + public String id() { + return definition; + } + } + + public static enum Criteria { + Less("<"), + LessEqual("<="), + + Equal("="), + + Greater(">"), + GreaterEqual(">="), + + ; + + private final String definition; + Criteria(String definition) { + this.definition = definition; + } + + public String id() { + return definition; + } + } + + public static enum Sort { + Ascending("ASC"), + Descending("DSC"), + ; + + private final String definition; + Sort(String definition) { + this.definition = definition; + } + + public String id() { + return definition; + } + } + + static enum Statement { + Query, + Insert, + } + + static enum Clause { + + Add("ADD"), + Set("SET"), + + Query("QUERY"), + Where("WHERE"), + + ; + + private final String definition; + Clause(String definition) { + this.definition = definition; + } + + public String id() { + return definition; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/Value.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +public class Value { + private Object value; + + public Value(Object value) { + this.value = value; + } + + public Object get() { + return value; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/WhereCriterion.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +/** + * + */ +public class WhereCriterion implements Criterion { + + private Id id; + + private FieldDescriptor descriptor; + private TypeMapper.Criteria criteria; + + public WhereCriterion(Id id, + FieldDescriptor descriptor, + TypeMapper.Criteria criteria) + { + this.descriptor = descriptor; + this.criteria = criteria; + this.id = id; + } + + @Override + public Class<?> getType() { + return descriptor.getType(); + } + + @Override + public Id getId() { + return id; + } + + public FieldDescriptor getFieldDescriptor() { + return descriptor; + } + + public TypeMapper.Criteria getCriteria() { + return criteria; + } + + @Override + public String toString() { + return "[" + criteria.name() + " -: " + descriptor + "]"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapterBuilderTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,183 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.internal.statement.StatementDescriptorTester; +import com.redhat.thermostat.storage.model.TimeStampedPojo; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class BeanAdapterBuilderTest { + + @Category("testCategory") + private static class SampleBean implements TimeStampedPojo { + + private long timeStamp; + private int vmId; + private String name; + + @Indexed + @Persist + public int getVmId() { + return vmId; + } + + @Indexed + @Persist + public void setVmId(int vmId) { + this.vmId = vmId; + } + + @Persist + public String getName() { + return name; + } + + @Persist + public void setName(String name) { + this.name = name; + } + + @Indexed + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Indexed + @Persist + @Override + public long getTimeStamp() { + return timeStamp; + } + } + + private List<Query<SampleBean>> queries; + + @Before + public void setUp() { + queries = new ArrayList<>(); + + List<FieldDescriptor> descriptors = StatementUtils.createDescriptors(SampleBean.class); + final Map<String, FieldDescriptor> map = StatementUtils.createDescriptorMap(descriptors); + Query query = new Query<SampleBean>() { + @Override + protected void describe(Criteria criterias) { + criterias.add(new WhereCriterion(new Id("name"), map.get("name"), TypeMapper.Criteria.Equal)); + criterias.add(new WhereCriterion(new Id("vmId>="), map.get("vmId"), TypeMapper.Criteria.GreaterEqual)); + criterias.add(new WhereCriterion(new Id("vmId<="), map.get("vmId"), TypeMapper.Criteria.LessEqual)); + criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Ascending)); + criterias.add(new LimitCriterion(new Id("limit"))); + } + + @Override + public Id getId() { + return new Id("SortByTimeStamp"); + } + }; + queries.add(query); + + query = new Query<SampleBean>() { + @Override + protected void describe(Criteria criterias) { + criterias.add(new WhereCriterion(new Id("name"), map.get("name"), TypeMapper.Criteria.Equal)); + criterias.add(new WhereCriterion(new Id("timeStamp>="), map.get("timeStamp"), TypeMapper.Criteria.GreaterEqual)); + criterias.add(new WhereCriterion(new Id("timeStamp<="), map.get("timeStamp"), TypeMapper.Criteria.LessEqual)); + criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Ascending)); + criterias.add(new LimitCriterion(new Id("limit"))); + } + + @Override + public Id getId() { + return new Id("RangedQuery"); + } + }; + queries.add(query); + } + + @Test + public void testBeanAdapterBuilder() throws Exception { + + BeanAdapterBuilder<SampleBean> builder = + new BeanAdapterBuilder<>(SampleBean.class, queries); + + BeanAdapter<SampleBean> adapter = builder.build(); + assertNotNull(adapter); + + com.redhat.thermostat.storage.core.Category<SampleBean> category = + adapter.getCategory(); + + assertNotNull(category); + assertEquals("testCategory", category.getName()); + + String expected = "ADD testCategory SET 'name' = ?s , 'timeStamp' = ?l , 'vmId' = ?i"; + Set<String> statements = adapter.describeStatements(); + + assertEquals(3, statements.size()); + assertTrue(statements.contains(expected)); + + expected = "QUERY testCategory WHERE 'name' = ?s AND 'vmId' >= ?i AND 'vmId' <= ?i SORT 'timeStamp' ASC LIMIT ?i"; + assertTrue(statements.contains(expected)); + + expected = "QUERY testCategory WHERE 'name' = ?s AND 'timeStamp' >= ?l AND 'timeStamp' <= ?l SORT 'timeStamp' ASC LIMIT ?i"; + assertTrue(statements.contains(expected)); + + for (String s : statements) { + testStatement(category, s); + } + } + + private void testStatement(com.redhat.thermostat.storage.core.Category<SampleBean> category, + String statement) throws DescriptorParsingException + { + StatementDescriptorTester<SampleBean> tester = new StatementDescriptorTester<>(); + StatementDescriptor<SampleBean> desc = new StatementDescriptor<>(category, statement); + tester.testParseBasic(desc); + tester.testParseSemantic(desc); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapterTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,287 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.Cursor; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.core.PreparedStatement; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.storage.model.TimeStampedPojo; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +public class BeanAdapterTest { + + @Category("testCategory") + private static class SampleBean implements TimeStampedPojo { + + private long timeStamp; + private int vmId; + private String name; + + @Indexed + @Persist + public int getVmId() { + return vmId; + } + + @Indexed + @Persist + public void setVmId(int vmId) { + this.vmId = vmId; + } + + @Persist + public String getName() { + return name; + } + + @Persist + public void setName(String name) { + this.name = name; + } + + @Indexed + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Indexed + @Persist + @Override + public long getTimeStamp() { + return timeStamp; + } + } + + private Storage storage; + private PreparedStatement<SampleBean> insert; + private PreparedStatement<SampleBean> query; + + private List<Query<SampleBean>> queries; + + private static final Id SORT = new Id("SortByTimeStamp"); + private static final Id RANGE = new Id("RangedQuery"); + + @Before + public void setUp() throws Exception { + storage = mock(Storage.class); + insert = mock(PreparedStatement.class); + query = mock(PreparedStatement.class); + + queries = new ArrayList<>(); + + List<FieldDescriptor> descriptors = StatementUtils.createDescriptors(SampleBean.class); + final Map<String, FieldDescriptor> map = StatementUtils.createDescriptorMap(descriptors); + Query query = new Query<SampleBean>() { + @Override + protected void describe(Criteria criterias) { + criterias.add(new WhereCriterion(new Id("0"), map.get("name"), TypeMapper.Criteria.Equal)); + criterias.add(new WhereCriterion(new Id("1"), map.get("vmId"), TypeMapper.Criteria.GreaterEqual)); + criterias.add(new WhereCriterion(new Id("2"), map.get("vmId"), TypeMapper.Criteria.LessEqual)); + criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Ascending)); + criterias.add(new LimitCriterion(new Id("3"))); + } + + @Override + public Id getId() { + return SORT; + } + }; + queries.add(query); + + query = new Query<SampleBean>() { + @Override + protected void describe(Criteria criterias) { + criterias.add(new WhereCriterion(new Id("0"), map.get("name"), TypeMapper.Criteria.Equal)); + criterias.add(new WhereCriterion(new Id("1"), map.get("timeStamp"), TypeMapper.Criteria.GreaterEqual)); + criterias.add(new WhereCriterion(new Id("2"), map.get("timeStamp"), TypeMapper.Criteria.LessEqual)); + criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Descending)); + criterias.add(new LimitCriterion(new Id("3"))); + } + + @Override + public Id getId() { + return RANGE; + } + }; + queries.add(query); + } + + @Test + public void testHasQueries() throws Exception { + BeanAdapterBuilder<SampleBean> builder = + new BeanAdapterBuilder<>(SampleBean.class, queries); + + BeanAdapter<SampleBean> adapter = builder.build(); + + Query query = adapter.getQuery(SORT); + assertSame(queries.get(0), query); + + query = adapter.getQuery(RANGE); + assertSame(queries.get(1), query); + } + + @Test + public void testInsert() throws Exception { + when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(insert); + + BeanAdapterBuilder<SampleBean> builder = + new BeanAdapterBuilder<>(SampleBean.class, queries); + + BeanAdapter<SampleBean> adapter = builder.build(); + + SampleBean bean = new SampleBean(); + bean.setName("fluff"); + bean.setTimeStamp(1000l); + bean.setVmId(0xcafe); + + adapter.insert(bean, storage); + + verify(storage).prepareStatement(any(StatementDescriptor.class)); + + // the order is given by the bean methods, they are sorted in + // alphabetical order, this is not really important though, + // since the order is based on the same FieldDescriptors used by + // both the builder and the adapter, so it's transparent to the user + + verify(insert).setString(0, "fluff"); + verify(insert).setLong(1, 1000l); + verify(insert).setInt(2, 0xcafe); + + verify(insert).execute(); + verifyNoMoreInteractions(insert); + } + + @Test + public void testSortQuery() throws Exception { + + when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(query); + Cursor cursor = mock(Cursor.class); + SampleBean sample = mock(SampleBean.class); + when(query.executeQuery()).thenReturn(cursor); + when(cursor.hasNext()).thenReturn(true).thenReturn(false); + when(cursor.next()).thenReturn(sample); + + BeanAdapterBuilder<SampleBean> builder = + new BeanAdapterBuilder<>(SampleBean.class, queries); + + BeanAdapter<SampleBean> adapter = builder.build(); + Query sort = adapter.getQuery(SORT); + assertNotNull(sort); + assertSame(sort, queries.get(0)); + + QueryValues values = sort.createValues(); + values.set(new Id("0"), "fluff"); + values.set(new Id("1"), 5); + values.set(new Id("2"), 5); + values.set(new Id("3"), 2); + + final boolean [] called = new boolean[1]; + ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() { + @Override + public void onResult(SampleBean result) { + called[0] = true; + } + }; + + adapter.query(values, handler, storage); + + verify(query).setString(0, "fluff"); + verify(query).setInt(1, 5); + verify(query).setInt(2, 5); + verify(query).setInt(3, 2); + + assertTrue(called[0]); + } + + @Test + public void testRangeQuery() throws Exception { + + when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(query); + Cursor cursor = mock(Cursor.class); + SampleBean sample = mock(SampleBean.class); + when(query.executeQuery()).thenReturn(cursor); + when(cursor.hasNext()).thenReturn(true).thenReturn(false); + when(cursor.next()).thenReturn(sample); + + BeanAdapterBuilder<SampleBean> builder = + new BeanAdapterBuilder<>(SampleBean.class, queries); + + BeanAdapter<SampleBean> adapter = builder.build(); + Query range = adapter.getQuery(RANGE); + assertNotNull(range); + assertSame(range, queries.get(1)); + + QueryValues values = range.createValues(); + values.set(new Id("0"), "fluff"); + values.set(new Id("1"), 10l); + values.set(new Id("2"), 20l); + values.set(new Id("3"), 2); + + final boolean [] called = new boolean[1]; + ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() { + @Override + public void onResult(SampleBean result) { + called[0] = true; + } + }; + + adapter.query(values, handler, storage); + + verify(query).setString(0, "fluff"); + verify(query).setLong(1, 10); + verify(query).setLong(2, 20); + verify(query).setInt(3, 2); + + assertTrue(called[0]); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/CategoryBuilderTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,125 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.model.TimeStampedPojo; +import java.util.Collection; +import java.util.List; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class CategoryBuilderTest { + + private static final String CATEGORY_NAME = "builder-category-test"; + + @Category(CATEGORY_NAME) + private static class SampleBean implements TimeStampedPojo { + + private long timeStamp; + private String vmId; + private String name; + + @Indexed + @Persist + public String getVmId() { + return vmId; + } + + @Indexed + @Persist + public void setVmId(String vmId) { + this.vmId = vmId; + } + + @Persist + public String getName() { + return name; + } + + @Persist + public void setName(String name) { + this.name = name; + } + + @Indexed + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Indexed + @Persist + @Override + public long getTimeStamp() { + return timeStamp; + } + } + + @Test + public void testBuild() throws Exception { + CategoryBuilder builder = new CategoryBuilder(SampleBean.class); + com.redhat.thermostat.storage.core.Category<SampleBean> category = + builder.build(); + + assertNotNull(category); + assertEquals(CATEGORY_NAME, category.getName()); + assertEquals(SampleBean.class, category.getDataClass()); + + List<Key<?>> indexed = category.getIndexedKeys(); + assertEquals(2, indexed.size()); + + Key key0 = new Key("vmId"); + Key key1 = new Key("timeStamp"); + + assertTrue(indexed.contains(key0)); + assertTrue(indexed.contains(key1)); + + Collection<Key<?>> keys = category.getKeys(); + assertEquals(3, keys.size()); + + Key key2 = new Key("name"); + + assertTrue(keys.contains(key0)); + assertTrue(keys.contains(key1)); + assertTrue(keys.contains(key2)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/InsertEngineTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,90 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.experimental.statement.FieldDescriptor; +import com.redhat.thermostat.storage.core.experimental.statement.InsertEngine; +import com.redhat.thermostat.storage.core.experimental.statement.Statement; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class InsertEngineTest { + private com.redhat.thermostat.storage.core.Category category; + private FieldDescriptor descriptor0; + private FieldDescriptor descriptor1; + private FieldDescriptor descriptor2; + + @Before + public void setup() { + category = mock(com.redhat.thermostat.storage.core.Category.class); + when(category.getName()).thenReturn("testCategory"); + + descriptor0 = mock(FieldDescriptor.class); + when(descriptor0.getName()).thenReturn("descriptor0"); + Class type = String.class; + when(descriptor0.getType()).thenReturn(type); + + descriptor1 = mock(FieldDescriptor.class); + when(descriptor1.getName()).thenReturn("descriptor1"); + type = long.class; + when(descriptor1.getType()).thenReturn(type); + + descriptor2 = mock(FieldDescriptor.class); + when(descriptor2.getName()).thenReturn("descriptor2"); + type = int.class; + when(descriptor2.getType()).thenReturn(type); + } + + @Test + public void testBuildInsert() throws Exception { + InsertEngine engine = new InsertEngine(); + + engine.prologue(category); + engine.add(descriptor0); + engine.add(descriptor1); + engine.add(descriptor2); + + Statement statement = engine.build(); + + String expected = "ADD testCategory SET 'descriptor0' = ?s , 'descriptor1' = ?l , 'descriptor2' = ?i"; + assertEquals(expected, statement.get()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/QueryEngineTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class QueryEngineTest { + private com.redhat.thermostat.storage.core.Category category; + private FieldDescriptor descriptor0; + private FieldDescriptor descriptor1; + private FieldDescriptor descriptor2; + + @Before + public void setup() { + category = mock(com.redhat.thermostat.storage.core.Category.class); + when(category.getName()).thenReturn("testCategory"); + + descriptor0 = mock(FieldDescriptor.class); + when(descriptor0.getName()).thenReturn("descriptor0"); + Class type = String.class; + when(descriptor0.getType()).thenReturn(type); + + descriptor1 = mock(FieldDescriptor.class); + when(descriptor1.getName()).thenReturn("descriptor1"); + type = long.class; + when(descriptor1.getType()).thenReturn(type); + + descriptor2 = mock(FieldDescriptor.class); + when(descriptor2.getName()).thenReturn("descriptor2"); + type = int.class; + when(descriptor2.getType()).thenReturn(type); + } + + @Test + public void testBuildQuery() throws Exception { + QueryEngine engine = new QueryEngine(); + + engine.prologue(category); + engine.add(descriptor0, TypeMapper.Criteria.Equal); + engine.add(descriptor1, TypeMapper.Criteria.GreaterEqual); + engine.add(descriptor2, TypeMapper.Criteria.LessEqual); + + engine.limit(); + engine.sort(descriptor1, TypeMapper.Sort.Ascending); + + Statement statement = engine.build(); + + String expected = "QUERY testCategory WHERE 'descriptor0' = ?s AND 'descriptor1' >= ?l AND 'descriptor2' <= ?i SORT 'descriptor1' ASC LIMIT ?i"; + + assertEquals(expected, statement.get()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/QueryValuesTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,97 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class QueryValuesTest { + + private Query query; + private List<Criterion> criterias; + + private Criterion criterion0; + private Criterion criterion1; + private Criterion criterion2; + + @Before + public void setup() { + query = mock(Query.class); + criterias = new ArrayList<>(); + + criterion0 = mock(Criterion.class); + when(criterion0.getType()).thenReturn((Class) int.class); + when(criterion0.getId()).thenReturn(new Id("0")); + + criterion1 = mock(Criterion.class); + when(criterion1.getType()).thenReturn((Class) String.class); + when(criterion1.getId()).thenReturn(new Id("1")); + + criterion2 = mock(Criterion.class); + when(criterion2.getType()).thenReturn((Class) long.class); + when(criterion2.getId()).thenReturn(new Id("2")); + + criterias.add(criterion0); + criterias.add(criterion1); + criterias.add(criterion2); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetWithWrongArguments() throws Exception { + + QueryValues values = new QueryValues(query); + values.addCriteria(criterias); + + values.set(criterion0, "wrong type"); + } + + @Test + public void testSet() throws Exception { + + QueryValues values = new QueryValues(query); + values.addCriteria(criterias); + + values.set(criterion0, 10); + values.set(criterion1, "test"); + values.set(criterion2, 42l); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/StatementTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,70 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class StatementTest { + + @Test + public void testEquals() throws Exception { + Statement statement = new Statement("test"); + Statement statement2 = new Statement("test"); + + assertTrue(statement.equals("test")); + assertTrue(statement.equals(statement2)); + + statement2 = new Statement("fluff"); + assertFalse(statement.equals("fluff")); + assertFalse(statement.equals(statement2)); + } + + @Test + public void testHashCode() throws Exception { + Statement statement = new Statement("test"); + Statement statement2 = new Statement("test"); + + assertEquals(statement.hashCode(), statement2.hashCode()); + + statement2 = new Statement("fluff"); + assertFalse(statement.hashCode() == statement2.hashCode()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/StatementUtilsTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,146 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core.experimental.statement; + +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.model.TimeStampedPojo; +import java.util.List; +import java.util.Map; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class StatementUtilsTest { + + @Category("testCategory") + private static class SampleBean implements TimeStampedPojo { + + private long timeStamp; + private int vmId; + private String name; + + @Indexed + @Persist + public int getVmId() { + return vmId; + } + + @Indexed + @Persist + public void setVmId(int vmId) { + this.vmId = vmId; + } + + @Persist + public String getName() { + return name; + } + + @Persist + public void setName(String name) { + this.name = name; + } + + @Indexed + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Indexed + @Persist + @Override + public long getTimeStamp() { + return timeStamp; + } + } + + @Category("testCategory2") + private static class InvalidSampleBean implements TimeStampedPojo { + + private long timeStamp; + private int vmId; + private String name; + + public int getVmId() { + return vmId; + } + + @Indexed + @Persist + public void setVmId(int vmId) { + this.vmId = vmId; + } + + @Persist + public String getName() { + return name; + } + + @Persist + public void setName(String name) { + this.name = name; + } + + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Indexed + @Persist + @Override + public long getTimeStamp() { + return timeStamp; + } + } + + @Test + public void testAnnotationSupport() { + List<FieldDescriptor> descriptors = + StatementUtils.createDescriptors(SampleBean.class); + Map<String, FieldDescriptor> map = + StatementUtils.createDescriptorMap(descriptors); + + assertTrue(map.containsKey("timeStamp")); + + descriptors = StatementUtils.createDescriptors(InvalidSampleBean.class); + map = StatementUtils.createDescriptorMap(descriptors); + + assertFalse(map.containsKey("timeStamp")); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/statement/DescriptorParserImplFactory.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.internal.statement; + +import com.redhat.thermostat.storage.core.Add; +import com.redhat.thermostat.storage.core.AggregateQuery; +import com.redhat.thermostat.storage.core.AggregateQuery.AggregateFunction; +import com.redhat.thermostat.storage.core.BackingStorage; +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.Query; +import com.redhat.thermostat.storage.core.Remove; +import com.redhat.thermostat.storage.core.Replace; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.core.Update; +import com.redhat.thermostat.storage.model.Pojo; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/* + * NOTE class copied from storage-test-utils for internal testing of Statement + * Adapters + */ +class DescriptorParserImplFactory { + + <T extends Pojo> StatementDescriptorParser<T> getParser(ParserType type, StatementDescriptor<T> desc) { + switch (type) { + case BASIC: + return new BasicDescriptorParser<>(getBasicBackingStorage(), desc); + case SEMANTIC: + return new SemanticsEnabledDescriptorParser<>(getSemanticBackingStorage(), desc); + default: + throw new IllegalStateException("Not implemented"); + } + } + + private BackingStorage getBasicBackingStorage() { + return mock(BackingStorage.class); + } + + @SuppressWarnings("unchecked") + private BackingStorage getSemanticBackingStorage() { + BackingStorage storage = mock(BackingStorage.class); + when(storage.createAdd(any(Category.class))).thenReturn(mock(Add.class)); + when(storage.createUpdate(any(Category.class))).thenReturn(mock(Update.class)); + when(storage.createRemove(any(Category.class))).thenReturn(mock(Remove.class)); + when(storage.createReplace(any(Category.class))).thenReturn(mock(Replace.class)); + when(storage.createQuery(any(Category.class))).thenReturn(mock(Query.class)); + when(storage.createAggregateQuery(any(AggregateFunction.class), any(Category.class))).thenReturn(mock(AggregateQuery.class)); + return storage; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/statement/ParserType.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.internal.statement; + +/* + * NOTE class copied from storage-test-utils for internal testing + */ +public enum ParserType { + BASIC, + SEMANTIC +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/statement/StatementDescriptorTester.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,59 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.internal.statement; + +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.model.Pojo; + +/* + * NOTE class copied from storage-test-utils for internal testing of Statement + * Adapters + */ +public final class StatementDescriptorTester<T extends Pojo> { + + public void testParseBasic(StatementDescriptor<T> desc) + throws DescriptorParsingException { + TestParser<T> testParser = new TestParser<>(desc, ParserType.BASIC); + testParser.parse(); + } + + public void testParseSemantic(StatementDescriptor<T> desc) throws DescriptorParsingException { + TestParser<T> testParser = new TestParser<>(desc, ParserType.SEMANTIC); + testParser.parse(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/statement/TestParser.java Wed Dec 03 16:01:36 2014 +0100 @@ -0,0 +1,66 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.internal.statement; + +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.ParsedStatement; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.model.Pojo; + +/* + * NOTE class copied from storage-test-utils for internal testing + */ +public final class TestParser<T extends Pojo> implements StatementDescriptorParser<T> { + + private final StatementDescriptor<T> desc; + private final DescriptorParserImplFactory factory; + private final ParserType type; + + public TestParser(StatementDescriptor<T> desc, ParserType type) { + this.desc = desc; + this.factory = new DescriptorParserImplFactory(); + this.type = type; + } + + @Override + public ParsedStatement<T> parse() throws DescriptorParsingException { + StatementDescriptorParser<T> actualParser = factory.getParser(type, desc); + actualParser.parse(); + return null; // Always return null. Testing utility only. + } + +}
--- a/thread/collector/pom.xml Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/pom.xml Wed Dec 03 16:01:36 2014 +0100 @@ -118,7 +118,6 @@ com.redhat.thermostat.thread.collector, com.redhat.thermostat.thread.dao, com.redhat.thermostat.thread.model, - com.redhat.thermostat.thread.dao.impl.statement.support, </Export-Package> <Private-Package> com.redhat.thermostat.thread.common.osgi,
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategories.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategories.java Wed Dec 03 16:01:36 2014 +0100 @@ -40,7 +40,7 @@ import com.redhat.thermostat.storage.core.Category; import com.redhat.thermostat.storage.core.Storage; import com.redhat.thermostat.storage.model.Pojo; -import com.redhat.thermostat.thread.dao.impl.statement.support.CategoryBuilder; +import com.redhat.thermostat.storage.core.experimental.statement.CategoryBuilder; import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; import java.util.ArrayList;
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java Wed Dec 03 16:01:36 2014 +0100 @@ -51,12 +51,12 @@ import com.redhat.thermostat.thread.dao.ThreadDao; import com.redhat.thermostat.thread.dao.impl.statement.SessionQuery; import com.redhat.thermostat.thread.dao.impl.statement.SummaryQuery; -import com.redhat.thermostat.thread.dao.impl.statement.support.BeanAdapter; -import com.redhat.thermostat.thread.dao.impl.statement.support.BeanAdapterBuilder; +import com.redhat.thermostat.storage.core.experimental.statement.BeanAdapter; +import com.redhat.thermostat.storage.core.experimental.statement.BeanAdapterBuilder; -import com.redhat.thermostat.thread.dao.impl.statement.support.Query; -import com.redhat.thermostat.thread.dao.impl.statement.support.QueryValues; -import com.redhat.thermostat.thread.dao.impl.statement.support.ResultHandler; +import com.redhat.thermostat.storage.core.experimental.statement.Query; +import com.redhat.thermostat.storage.core.experimental.statement.QueryValues; +import com.redhat.thermostat.storage.core.experimental.statement.ResultHandler; import com.redhat.thermostat.thread.model.SessionID; import com.redhat.thermostat.thread.model.ThreadContentionSample; import com.redhat.thermostat.thread.model.ThreadHarvestingStatus;
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/SessionQuery.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/SessionQuery.java Wed Dec 03 16:01:36 2014 +0100 @@ -36,14 +36,14 @@ package com.redhat.thermostat.thread.dao.impl.statement; -import com.redhat.thermostat.thread.dao.impl.statement.support.FieldDescriptor; -import com.redhat.thermostat.thread.dao.impl.statement.support.Id; -import com.redhat.thermostat.thread.dao.impl.statement.support.LimitCriterion; -import com.redhat.thermostat.thread.dao.impl.statement.support.Query; -import com.redhat.thermostat.thread.dao.impl.statement.support.SortCriterion; -import com.redhat.thermostat.thread.dao.impl.statement.support.StatementUtils; -import com.redhat.thermostat.thread.dao.impl.statement.support.TypeMapper; -import com.redhat.thermostat.thread.dao.impl.statement.support.WhereCriterion; +import com.redhat.thermostat.storage.core.experimental.statement.FieldDescriptor; +import com.redhat.thermostat.storage.core.experimental.statement.Id; +import com.redhat.thermostat.storage.core.experimental.statement.LimitCriterion; +import com.redhat.thermostat.storage.core.experimental.statement.Query; +import com.redhat.thermostat.storage.core.experimental.statement.SortCriterion; +import com.redhat.thermostat.storage.core.experimental.statement.StatementUtils; +import com.redhat.thermostat.storage.core.experimental.statement.TypeMapper; +import com.redhat.thermostat.storage.core.experimental.statement.WhereCriterion; import com.redhat.thermostat.thread.model.ThreadSession; import java.util.List; import java.util.Map;
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/SummaryQuery.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/SummaryQuery.java Wed Dec 03 16:01:36 2014 +0100 @@ -36,14 +36,14 @@ package com.redhat.thermostat.thread.dao.impl.statement; -import com.redhat.thermostat.thread.dao.impl.statement.support.FieldDescriptor; -import com.redhat.thermostat.thread.dao.impl.statement.support.Id; -import com.redhat.thermostat.thread.dao.impl.statement.support.LimitCriterion; -import com.redhat.thermostat.thread.dao.impl.statement.support.Query; -import com.redhat.thermostat.thread.dao.impl.statement.support.SortCriterion; -import com.redhat.thermostat.thread.dao.impl.statement.support.StatementUtils; -import com.redhat.thermostat.thread.dao.impl.statement.support.TypeMapper; -import com.redhat.thermostat.thread.dao.impl.statement.support.WhereCriterion; +import com.redhat.thermostat.storage.core.experimental.statement.FieldDescriptor; +import com.redhat.thermostat.storage.core.experimental.statement.Id; +import com.redhat.thermostat.storage.core.experimental.statement.LimitCriterion; +import com.redhat.thermostat.storage.core.experimental.statement.Query; +import com.redhat.thermostat.storage.core.experimental.statement.SortCriterion; +import com.redhat.thermostat.storage.core.experimental.statement.StatementUtils; +import com.redhat.thermostat.storage.core.experimental.statement.TypeMapper; +import com.redhat.thermostat.storage.core.experimental.statement.WhereCriterion; import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; import java.util.List;
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/BeanAdapter.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,183 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.Cursor; -import com.redhat.thermostat.storage.core.DescriptorParsingException; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.core.StatementDescriptor; -import com.redhat.thermostat.storage.core.StatementExecutionException; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.storage.model.Pojo; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - */ -public class BeanAdapter<T extends Pojo> { - public static final Id INSERT = new Id("BeanAdapter_Insert"); - - private static final Logger logger = LoggingUtils.getLogger(BeanAdapter.class); - - protected Category<T> category; - - private Map<Id, Statement> describedQueries; - private Map<Id, Query<T>> queries; - private List<FieldDescriptor> fieldDescriptors; - - public BeanAdapter() { - this.queries = new HashMap<>(); - this.describedQueries = new HashMap<>(); - } - - protected void setCategory(Category<T> category) { - this.category = category; - } - - public Category<T> getCategory() { - return category; - } - - public Set<String> describeStatements() { - Set<String> descriptions = new HashSet<>(); - for (Statement statement : describedQueries.values()) { - descriptions.add(statement.get()); - } - return descriptions; - } - - public void insert(T bean, Storage storage) - throws StatementExecutionException - { - Statement statement = describedQueries.get(INSERT); - - StatementDescriptor<T> desc = - new StatementDescriptor<>(category, statement.get()); - try { - PreparedStatement<T> prepared = storage.prepareStatement(desc); - int i = 0; - for (FieldDescriptor descriptor : fieldDescriptors) { - Value value = StatementUtils.getValue(bean, descriptor); - StatementUtils.setData(prepared, value, i++); - } - - prepared.execute(); - - } catch (DescriptorParsingException e) { - logger.log(Level.SEVERE, "Preparing stmt '" + desc + "' failed!", e); - } - } - - public Query<T> getQuery(Id id) { - return queries.get(id); - } - - public void query(QueryValues values, ResultHandler<T> handler, - Storage storage) throws StatementExecutionException - { - Objects.requireNonNull(handler, "ResultHandler cannot be null"); - Objects.requireNonNull(values, "QueryValues cannot be null"); - Objects.requireNonNull(storage, "Storage cannot be null"); - - Query query = values.getQuery(); - if (!queries.containsKey(query.getId())) { - throw new IllegalArgumentException("This adapter does not know" + - "about the given query: " + - query.getId()); - } - - Statement statement = describedQueries.get(query.getId()); - List<Criterion> criteria = query.describe(); - - StatementDescriptor<T> desc = - new StatementDescriptor<>(category, statement.get()); - PreparedStatement<T> prepared = null; - try { - prepared = storage.prepareStatement(desc); - int i = 0; - for (Criterion criterion : criteria) { - // sort doesn't take values - if (criterion instanceof SortCriterion) { - continue; - } - - Value value = values.getValue(criterion); - StatementUtils.setData(prepared, value, i++); - } - - } catch (DescriptorParsingException e) { - logger.log(Level.SEVERE, "Preparing stmt '" + desc + - "' failed!", e); - - // this can't really happen, but if it does is serious - throw new AssertionError("Autogenerated statement failed to parse", - e); - } - - Cursor<T> cursor = prepared.executeQuery(); - while (cursor.hasNext()) { - T result = cursor.next(); - try { - handler.onResult(result); - - } catch (Throwable t) { - logger.log(Level.SEVERE, "Exception executing results", t); - } - } - } - - protected void addStatement(Id id, Statement statement) { - describedQueries.put(id, statement); - } - - protected void addQuery(Id id, Query query) { - queries.put(id, query); - } - - protected void setFieldDescriptors(List<FieldDescriptor> fieldDescriptors) { - this.fieldDescriptors = fieldDescriptors; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/BeanAdapterBuilder.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,124 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.model.Pojo; -import java.util.ArrayList; -import java.util.List; - -/** - * - */ -public class BeanAdapterBuilder<T extends Pojo> { - - private Class<T> target; - private BeanAdapter<T> adapter; - - private List<Query<T>> queries; - - public BeanAdapterBuilder(Class<T> target, List<Query<T>> queries) { - this.target = target; - this.queries = queries; - } - - public BeanAdapterBuilder(Class<T> target, Query<T> query) { - this(target, new ArrayList<Query<T>>()); - queries.add(query); - } - - public BeanAdapter<T> build() { - try { - Category<T> category = new CategoryBuilder<>(target).build(); - List<FieldDescriptor> fieldDescriptors = - StatementUtils.createDescriptors(target); - - adapter = new BeanAdapter<>(); - adapter.setCategory(category); - adapter.setFieldDescriptors(fieldDescriptors); - - createInsert(fieldDescriptors); - createQueries(); - - return adapter; - - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - protected void createQueries() { - for (Query<T> query : queries) { - createQuery(query); - } - } - - private void createQuery(Query<T> query) { - List<Criterion> queryDescriptors = query.describe(); - - QueryEngine engine = new QueryEngine(); - engine.prologue(adapter.getCategory()); - for (Criterion criterion : queryDescriptors) { - if (criterion instanceof WhereCriterion) { - WhereCriterion where = (WhereCriterion) criterion; - engine.add(where.getFieldDescriptor(), where.getCriteria()); - - } else if (criterion instanceof SortCriterion) { - SortCriterion sort = (SortCriterion) criterion; - engine.sort(sort.getFieldDescriptor(), sort.getCriteria()); - - } else if (criterion instanceof LimitCriterion) { - engine.limit(); - } - } - - adapter.addStatement(query.getId(), engine.build()); - adapter.addQuery(query.getId(), query); - } - - protected void createInsert(List<FieldDescriptor> descriptors) { - - InsertEngine engine = new InsertEngine(); - engine.prologue(adapter.getCategory()); - - for (FieldDescriptor descriptor : descriptors) { - engine.add(descriptor); - } - - adapter.addStatement(BeanAdapter.INSERT, engine.build()); - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Category.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -public @interface Category { - String value(); -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/CategoryBuilder.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,138 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.model.Pojo; -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * - */ -public class CategoryBuilder<T extends Pojo> { - - private static final String LOCK = new String("CategoryBuilder_lock"); - private static final Map<String, Category> categories = new HashMap<>(); - - private Class<T> bean; - - public CategoryBuilder(Class<T> bean) { - this.bean = bean; - } - - public Category<T> build() { - - synchronized (LOCK) { - - String document = StatementUtils.getDocument(bean); - - // This helps performance as well, but it's needed because Category - // doesn't allow us to create the same twice, even with all the - // same data; categories are in fact some kind of singleton - if (categories.containsKey(document)) { - return categories.get(document); - } - - List<FieldDescriptor> descriptors = - StatementUtils.createDescriptors(bean); - - // we first build the keys and the indexed keys - List<Key> indexed = new ArrayList<>(); - List<Key> keys = new ArrayList<>(); - - // key are easy, the arguments are in the form: - // new Key<Type>(String name), we don't need reflection - - for (FieldDescriptor descriptor : descriptors) { - - Key key = new Key(descriptor.getName()); - - keys.add(key); - if (descriptor.isIndexed()) { - indexed.add(key); - } - } - - List<Class> argumentClasses = new ArrayList<>(); - argumentClasses.add(String.class); - argumentClasses.add(Class.class); - argumentClasses.add(List.class); - - List argumentObjects = new ArrayList(); - argumentObjects.add(document); - argumentObjects.add(bean); - argumentObjects.add(keys); - - // indexed keys are optional - if (!indexed.isEmpty()) { - argumentClasses.add(List.class); - argumentObjects.add(indexed); - } - - Class[] classes = argumentClasses.toArray(new Class[argumentClasses.size()]); - Object[] objects = argumentObjects.toArray(); - - Category<T> category = create(classes, objects); - categories.put(document, category); - - return category; - } - } - - private Category<T> create(Class[] classes, Object[] objects) { - - // we are using this constructor: - // Category(String name, Class<T> dataClass, - // List<Key<?>> keys, List<Key<?>> indexedKeys) - - try { - Constructor<Category> constructor = - Category.class.getConstructor(classes); - constructor.setAccessible(true); - - return constructor.newInstance(objects); - - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Criterion.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -public interface Criterion { - Id getId(); - Class<?> getType(); -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/FieldDescriptor.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.util.Objects; - -/** - * - */ -public class FieldDescriptor { - private Class<?> type; - private String name; - private boolean indexed; - - public void setType(Class<?> type) { - this.type = type; - } - - public Class<?> getType() { - return type; - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setIndexed(boolean indexed) { - this.indexed = indexed; - } - - public boolean isIndexed() { - return indexed; - } - - @Override - public String toString() { - return "[FieldDescriptor: " + - "type = " + type + - ", name = '" + name + '\'' + - ", indexed = " + indexed + - ']'; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - FieldDescriptor that = (FieldDescriptor) o; - return Objects.equals(name, that.name); - } - - @Override - public int hashCode() { - return name != null ? name.hashCode() : 0; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Id.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.util.UUID; - -/** - * - */ -public class Id { - private String id; - - public Id() { - id = UUID.randomUUID().toString(); - } - - public Id(String id) { - this.id = id; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Id sessionID = (Id) o; - - if (id != null ? !id.equals(sessionID.id) : sessionID.id != null) - return false; - - return true; - } - - public String get() { - return id; - } - - @Override - public int hashCode() { - return id != null ? id.hashCode() : 0; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Indexed.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -public @interface Indexed { -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/InsertEngine.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -class InsertEngine extends StatementEngine { - - public InsertEngine() { - super(TypeMapper.Statement.Insert); - delimiter = TypeMapper.Symbol.InsertSeparator.id(); - tokens.add(TypeMapper.Clause.Add.id()); - } - - public InsertEngine add(FieldDescriptor descriptor) { - super.add(descriptor, TypeMapper.Criteria.Equal); - return this; - } - - @Override - protected void addPrologueClause() { - tokens.add(TypeMapper.Clause.Set.id()); - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/LimitCriterion.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -public class LimitCriterion implements Criterion { - - private Id id; - - public LimitCriterion(Id id) { - this.id = id; - } - - @Override - public Id getId() { - return id; - } - - @Override - public Class<?> getType() { - return int.class; - } - - @Override - public String toString() { - return "Limit"; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Query.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.model.Pojo; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * - */ -public abstract class Query<T extends Pojo> { - - protected final class Criteria { - private List<Criterion> criteria; - private Map<Id, Criterion> map; - - Criteria() { - criteria = new ArrayList<>(); - map = new HashMap<>(); - } - - public void add(Criterion criterion) { - Id id = criterion.getId(); - if (map.containsKey(id)) { - throw new IllegalArgumentException("Already contains criteria" + - " with this id." + - " New: " + criterion + - " Old: " + map.get(id)); - } - map.put(id, criterion); - criteria.add(criterion); - } - } - - private List<Criterion> describedQuery; - private Criteria criteria; - - public Query() { - criteria = new Criteria(); - } - - public abstract Id getId(); - - public final List<Criterion> describe() { - if (describedQuery == null) { - describe(criteria); - describedQuery = Collections.unmodifiableList(criteria.criteria); - } - return describedQuery; - } - - protected abstract void describe(Criteria criteria); - - public final QueryValues createValues() { - if (describedQuery == null) { - throw new IllegalStateException("Query must be described first"); - } - - QueryValues setter = new QueryValues(this); - setter.addCriteria(describedQuery); - - return setter; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/QueryEngine.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,127 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import static com.redhat.thermostat.thread.dao.impl.statement.support.TypeMapper.Symbol.Quote; -import static com.redhat.thermostat.thread.dao.impl.statement.support.TypeMapper.Symbol.Space; - -/** - * - */ -class QueryEngine extends StatementEngine { - - private String limit; - private String sort; - - public QueryEngine() { - super(TypeMapper.Statement.Query); - addDelimiter = false; - delimiter = TypeMapper.Symbol.InsertSeparator.id(); - delimiter = TypeMapper.Symbol.QuerySeparator.id(); - tokens.add(TypeMapper.Clause.Query.id()); - } - - public QueryEngine add(FieldDescriptor descriptor, TypeMapper.Criteria criteria) { - - if (addDelimiter) { - tokens.add(delimiter); - } - - addDelimiter = true; - - StringBuilder field = new StringBuilder(); - - field.append(Quote.id()); - field.append(descriptor.getName()); - field.append(Quote.id()); - - tokens.add(field.toString()); - - tokens.add(criteria.id()); - tokens.add(TypeMapper.get(descriptor.getType())); - - return this; - } - - public QueryEngine sort(FieldDescriptor descriptor, TypeMapper.Sort criteria) { - - StringBuilder field = new StringBuilder(); - - field.append(TypeMapper.Symbol.Sort.id()); - field.append(Space.id()); - - field.append(Quote.id()); - field.append(descriptor.getName()); - field.append(Quote.id()); - - field.append(Space.id()); - field.append(criteria.id()); - - sort = field.toString(); - - return this; - } - - public QueryEngine limit() { - - StringBuilder field = new StringBuilder(); - - field.append(TypeMapper.Symbol.Limit.id()); - field.append(Space.id()); - field.append(TypeMapper.get(int.class)); - - limit = field.toString(); - - return this; - } - - @Override - protected void addPrologueClause() { - tokens.add(TypeMapper.Clause.Where.id()); - } - - @Override - protected void buildImpl(StringBuilder builder) { - if (sort != null) { - builder.append(Space.id()).append(sort); - } - - if (limit != null) { - builder.append(Space.id()).append(limit); - } - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/QueryValues.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,157 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * - */ -public class QueryValues { - - private static final Class EMPTY_VALUE = QueryValues.class; - - private Query query; - private Map<Id, Value> values; - private Map<Id, Class> types; - - protected QueryValues(Query query) { - this.query = query; - values = new HashMap<>(); - types = new HashMap<>(); - } - - public Query getQuery() { - return query; - } - - public void set(Id criteriaId, String value) { - setImpl(criteriaId, value); - } - - public void set(Id criteriaId, long value) { - setImpl(criteriaId, value); - } - - public void set(Id criteriaId, int value) { - setImpl(criteriaId, value); - } - - public void set(Id criteriaId, boolean value) { - setImpl(criteriaId, value); - } - - private void setImpl(Id criteriaId, Object value) { - if (!values.containsKey(criteriaId)) { - throw new IllegalArgumentException("Query does not contain this criteria"); - } - check(criteriaId, value); - } - - private void check(Id criteriaId, Object value) { - - Class type = value.getClass(); - Class targetType = types.get(criteriaId); - - boolean match = false; - if (targetType.isAssignableFrom(int.class) || - targetType.isAssignableFrom(Integer.class)) - { - if (type.isAssignableFrom(int.class) || - type.isAssignableFrom(Integer.class)) - { - match = true; - } - } else if (targetType.isAssignableFrom(long.class) || - targetType.isAssignableFrom(Long.class)) - { - if (type.isAssignableFrom(long.class) || - type.isAssignableFrom(Long.class)) - { - match = true; - } - } else if (targetType.isAssignableFrom(boolean.class) || - targetType.isAssignableFrom(Boolean.class)) - { - if (type.isAssignableFrom(boolean.class) || - type.isAssignableFrom(Boolean.class)) - { - match = true; - } - } else if (targetType.isAssignableFrom(String.class)) { - if (type.isAssignableFrom(String.class)) - { - match = true; - } - } - - if (!match) { - throw new IllegalArgumentException("value type does not match " + - "target criteria with id: " + - criteriaId.get() + - " Wanted: " + - targetType + ", received: " + - type); - } - - values.put(criteriaId, new Value(value)); - } - - public void set(Criterion criterion, Object value) { - setImpl(criterion.getId(), value); - } - - Value getValue(Criterion criterion) { - return values.get(criterion.getId()); - } - - void addCriteria(List<Criterion> _criteria) { - for (Criterion criterion : _criteria) { - - // sort doesn't take values - if (criterion instanceof SortCriterion) { - continue; - } - - Id id = criterion.getId(); - values.put(id, new Value(EMPTY_VALUE)); - types.put(id, criterion.getType()); - } - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/ResultHandler.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -public interface ResultHandler<T> { - void onResult(T result); -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/SortCriterion.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -public class SortCriterion implements Criterion { - private FieldDescriptor descriptor; - private TypeMapper.Sort criteria; - - private Id id; - - public SortCriterion(FieldDescriptor descriptor, - TypeMapper.Sort criteria) - { - this(new Id(descriptor.getName() + ":" + criteria.name()), descriptor, criteria); - } - - private SortCriterion(Id id, - FieldDescriptor descriptor, - TypeMapper.Sort criteria) - { - this.descriptor = descriptor; - this.criteria = criteria; - } - - @Override - public Class<?> getType() { - return descriptor.getType(); - } - - public FieldDescriptor getFieldDescriptor() { - return descriptor; - } - - public TypeMapper.Sort getCriteria() { - return criteria; - } - - @Override - public Id getId() { - return id; - } - - @Override - public String toString() { - return "[Sort: " + criteria.name() + " -: " + descriptor + "]"; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Statement.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -class Statement { - private String statement; - - public Statement(String statement) { - this.statement = statement; - } - - public String get() { - return statement; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - - if (o == null) { - return false; - } - - if (o instanceof String && statement != null) { - return o.equals(statement); - } - - if (getClass() != o.getClass()) return false; - - Statement statement1 = (Statement) o; - - if (statement != null ? !statement.equals(statement1.statement) : - statement1.statement != null) - { - return false; - } - - return true; - } - - @Override - public int hashCode() { - return statement != null ? statement.hashCode() : 0; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/StatementEngine.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,109 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.model.Pojo; -import java.util.LinkedList; -import java.util.List; - -import static com.redhat.thermostat.thread.dao.impl.statement.support.TypeMapper.Symbol.Quote; -import static com.redhat.thermostat.thread.dao.impl.statement.support.TypeMapper.Symbol.Space; - -/** - * - */ -abstract class StatementEngine { - - protected List<String> tokens; - - private TypeMapper.Statement type; - protected String delimiter; - - protected boolean addDelimiter; - - protected StatementEngine(TypeMapper.Statement type) { - tokens = new LinkedList<>(); - this.type = type; - addDelimiter = false; - } - - protected StatementEngine add(FieldDescriptor descriptor, TypeMapper.Criteria criteria) { - - if (addDelimiter) { - tokens.add(delimiter); - } - - addDelimiter = true; - - StringBuilder field = new StringBuilder(); - - field.append(Quote.id()); - field.append(descriptor.getName()); - field.append(Quote.id()); - - tokens.add(field.toString()); - - tokens.add(criteria.id()); - tokens.add(TypeMapper.get(descriptor.getType())); - - return this; - } - - protected abstract void addPrologueClause(); - - public StatementEngine prologue(com.redhat.thermostat.storage.core.Category<? extends Pojo> category) { - tokens.add(category.getName()); - addPrologueClause(); - return this; - } - - protected void buildImpl(StringBuilder builder) {} - - - public Statement build() { - StringBuilder builder = new StringBuilder(); - for (String token : tokens) { - builder.append(token).append(Space.id()); - } - int position = builder.length() - 1; - builder.deleteCharAt(position); - - buildImpl(builder); - - return new Statement(builder.toString()); - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/StatementUtils.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,179 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.model.Pojo; -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * - */ -public class StatementUtils { - - static boolean isPersistent(PropertyDescriptor desc) { - return hasAnnotation(desc, Persist.class); - } - - static boolean isIndexed(PropertyDescriptor desc) { - return hasAnnotation(desc, Indexed.class); - } - - static <T extends Pojo> String getDocument(Class<T> pojoClass) { - if (!pojoClass.isAnnotationPresent(Category.class)) { - return null; - } - - return pojoClass.getAnnotation(Category.class).value(); - } - - static boolean hasAnnotation(PropertyDescriptor desc, - Class<? extends Annotation> annotation) - { - Method readMethod = desc.getReadMethod(); - boolean hasRead = (readMethod != null && - readMethod.isAnnotationPresent(annotation)); - - Method writeMethod = desc.getWriteMethod(); - boolean hasWrite = (writeMethod != null && - writeMethod.isAnnotationPresent(annotation)); - - return hasRead && hasWrite; - } - - public static Map<String, FieldDescriptor> createDescriptorMap(List<FieldDescriptor> descriptors) { - Map<String, FieldDescriptor> map = new HashMap<>(); - for (FieldDescriptor desc : descriptors) { - map.put(desc.getName(), desc); - } - return map; - } - - public static <T extends Pojo> List<FieldDescriptor> createDescriptors(Class<T> pojoClass) { - try { - BeanInfo info = Introspector.getBeanInfo(pojoClass); - PropertyDescriptor[] props = info.getPropertyDescriptors(); - - List<FieldDescriptor> descriptors = new ArrayList<>(); - for (PropertyDescriptor desc : props) { - - if (StatementUtils.isPersistent(desc)) { - FieldDescriptor descriptor = new FieldDescriptor(); - descriptor.setType(desc.getPropertyType()); - descriptor.setName(desc.getName()); - descriptor.setIndexed(StatementUtils.isIndexed(desc)); - - descriptors.add(descriptor); - } - } - - // Afaik it's not specified that methods are returned or listed in - // any particular order by Introspector.getBeanInfo, so if we don't - // sort, we may end up with two differently ordered lists in two - // different calls to this method. - // This is a problem because the prepated statement API doesn't - // check if two statements are the same if fields are sorted in - // random order, it only considers two statements the same if - // their string representation completely matches; - // it also make the code depending on FieldDescriptor easier to - // write if it can count on the ordering being always consistent - Collections.sort(descriptors, new Comparator<FieldDescriptor>() { - @Override - public int compare(FieldDescriptor o1, FieldDescriptor o2) { - return o1.getName().compareTo(o2.getName()); - } - }); - - return Collections.unmodifiableList(descriptors); - - } catch (IntrospectionException e) { - throw new RuntimeException(e); - } - } - - static <T extends Pojo> Value getValue(T bean, FieldDescriptor descriptor) - { - try { - PropertyDescriptor property = - new PropertyDescriptor(descriptor.getName(), - bean.getClass()); - Method method = property.getReadMethod(); - method.setAccessible(true); - - return new Value(method.invoke(bean, new Object[0])); - - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - static <T extends Pojo> void setData(PreparedStatement<T> prepared, - Value value, int index) - { - Class type = value.get().getClass(); - if (type.isAssignableFrom(int.class) || - type.isAssignableFrom(Integer.class)) - { - prepared.setInt(index, ((Integer) value.get()).intValue()); - - } else if (type.isAssignableFrom(long.class) || - type.isAssignableFrom(Long.class)) - { - prepared.setLong(index, ((Long) value.get()).longValue()); - - } else if (type.isAssignableFrom(boolean.class) || - type.isAssignableFrom(Boolean.class)) - { - prepared.setBoolean(index, ((Boolean) value.get()).booleanValue()); - - } else if (type.isAssignableFrom(String.class)) { - prepared.setString(index, (String) value.get()); - } - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/TypeMapper.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,142 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.util.HashMap; -import java.util.Map; - -/** - * - */ -public class TypeMapper { - - private static final Map<Class, String> map = new HashMap<>(); - static { - map.put(String.class, "?s"); - map.put(int.class, "?i"); - map.put(long.class, "?l"); - map.put(boolean.class, "?b"); - } - - static String get(Class type) { - return map.get(type); - } - - static enum Symbol { - - QuerySeparator("AND"), - InsertSeparator(","), - - Quote("'"), - Space(" "), - - Sort("SORT"), - Limit("LIMIT"), - - ; - - private final String definition; - Symbol(String definition) { - this.definition = definition; - } - - public String id() { - return definition; - } - } - - public static enum Criteria { - Less("<"), - LessEqual("<="), - - Equal("="), - - Greater(">"), - GreaterEqual(">="), - - ; - - private final String definition; - Criteria(String definition) { - this.definition = definition; - } - - public String id() { - return definition; - } - } - - public static enum Sort { - Ascending("ASC"), - Descending("DSC"), - ; - - private final String definition; - Sort(String definition) { - this.definition = definition; - } - - public String id() { - return definition; - } - } - - static enum Statement { - Query, - Insert, - } - - static enum Clause { - - Add("ADD"), - Set("SET"), - - Query("QUERY"), - Where("WHERE"), - - ; - - private final String definition; - Clause(String definition) { - this.definition = definition; - } - - public String id() { - return definition; - } - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/Value.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -public class Value { - private Object value; - - public Value(Object value) { - this.value = value; - } - - public Object get() { - return value; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/statement/support/WhereCriterion.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -/** - * - */ -public class WhereCriterion implements Criterion { - - private Id id; - - private FieldDescriptor descriptor; - private TypeMapper.Criteria criteria; - - public WhereCriterion(Id id, - FieldDescriptor descriptor, - TypeMapper.Criteria criteria) - { - this.descriptor = descriptor; - this.criteria = criteria; - this.id = id; - } - - @Override - public Class<?> getType() { - return descriptor.getType(); - } - - @Override - public Id getId() { - return id; - } - - public FieldDescriptor getFieldDescriptor() { - return descriptor; - } - - public TypeMapper.Criteria getCriteria() { - return criteria; - } - - @Override - public String toString() { - return "[" + criteria.name() + " -: " + descriptor + "]"; - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/model/SessionID.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/model/SessionID.java Wed Dec 03 16:01:36 2014 +0100 @@ -36,7 +36,7 @@ package com.redhat.thermostat.thread.model; -import com.redhat.thermostat.thread.dao.impl.statement.support.Id; +import com.redhat.thermostat.storage.core.experimental.statement.Id; /** *
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSession.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSession.java Wed Dec 03 16:01:36 2014 +0100 @@ -41,8 +41,8 @@ import com.redhat.thermostat.storage.model.BasePojo; import com.redhat.thermostat.storage.model.TimeStampedPojo; import com.redhat.thermostat.thread.dao.impl.ThreadDaoCategories; -import com.redhat.thermostat.thread.dao.impl.statement.support.Category; -import com.redhat.thermostat.thread.dao.impl.statement.support.Indexed; +import com.redhat.thermostat.storage.core.experimental.statement.Category; +import com.redhat.thermostat.storage.core.experimental.statement.Indexed; /** *
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSummary.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSummary.java Wed Dec 03 16:01:36 2014 +0100 @@ -41,8 +41,8 @@ import com.redhat.thermostat.storage.model.BasePojo; import com.redhat.thermostat.storage.model.TimeStampedPojo; import com.redhat.thermostat.thread.dao.impl.ThreadDaoCategories; -import com.redhat.thermostat.thread.dao.impl.statement.support.Category; -import com.redhat.thermostat.thread.dao.impl.statement.support.Indexed; +import com.redhat.thermostat.storage.core.experimental.statement.Category; +import com.redhat.thermostat.storage.core.experimental.statement.Indexed; @Category(ThreadDaoCategories.Categories.SUMMARY) @Entity
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategoriesTest.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategoriesTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -38,8 +38,8 @@ import com.redhat.thermostat.storage.core.Storage; import com.redhat.thermostat.storage.model.Pojo; -import com.redhat.thermostat.thread.dao.impl.statement.support.Category; -import com.redhat.thermostat.thread.dao.impl.statement.support.CategoryBuilder; +import com.redhat.thermostat.storage.core.experimental.statement.Category; +import com.redhat.thermostat.storage.core.experimental.statement.CategoryBuilder; import java.util.ArrayList; import java.util.HashSet; import java.util.List;
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/SessionQueryTest.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/SessionQueryTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -36,8 +36,8 @@ package com.redhat.thermostat.thread.dao.impl.statement; -import com.redhat.thermostat.thread.dao.impl.statement.support.BeanAdapter; -import com.redhat.thermostat.thread.dao.impl.statement.support.BeanAdapterBuilder; +import com.redhat.thermostat.storage.core.experimental.statement.BeanAdapter; +import com.redhat.thermostat.storage.core.experimental.statement.BeanAdapterBuilder; import com.redhat.thermostat.thread.model.ThreadSession; import java.util.Set; import org.junit.Test;
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/SummaryQueryTest.java Mon Dec 01 08:51:43 2014 -0500 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/SummaryQueryTest.java Wed Dec 03 16:01:36 2014 +0100 @@ -36,8 +36,8 @@ package com.redhat.thermostat.thread.dao.impl.statement; -import com.redhat.thermostat.thread.dao.impl.statement.support.BeanAdapter; -import com.redhat.thermostat.thread.dao.impl.statement.support.BeanAdapterBuilder; +import com.redhat.thermostat.storage.core.experimental.statement.BeanAdapter; +import com.redhat.thermostat.storage.core.experimental.statement.BeanAdapterBuilder; import com.redhat.thermostat.thread.model.ThreadSummary; import java.util.Set; import org.junit.Test;
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/BeanAdapterBuilderTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,171 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.model.TimeStampedPojo; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class BeanAdapterBuilderTest { - - @Category("testCategory") - private static class SampleBean implements TimeStampedPojo { - - private long timeStamp; - private int vmId; - private String name; - - @Indexed - @Persist - public int getVmId() { - return vmId; - } - - @Indexed - @Persist - public void setVmId(int vmId) { - this.vmId = vmId; - } - - @Persist - public String getName() { - return name; - } - - @Persist - public void setName(String name) { - this.name = name; - } - - @Indexed - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Indexed - @Persist - @Override - public long getTimeStamp() { - return timeStamp; - } - } - - private List<Query<SampleBean>> queries; - - @Before - public void setUp() { - queries = new ArrayList<>(); - - List<FieldDescriptor> descriptors = StatementUtils.createDescriptors(SampleBean.class); - final Map<String, FieldDescriptor> map = StatementUtils.createDescriptorMap(descriptors); - Query query = new Query<SampleBean>() { - @Override - protected void describe(Criteria criterias) { - criterias.add(new WhereCriterion(new Id("name"), map.get("name"), TypeMapper.Criteria.Equal)); - criterias.add(new WhereCriterion(new Id("vmId>="), map.get("vmId"), TypeMapper.Criteria.GreaterEqual)); - criterias.add(new WhereCriterion(new Id("vmId<="), map.get("vmId"), TypeMapper.Criteria.LessEqual)); - criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Ascending)); - criterias.add(new LimitCriterion(new Id("limit"))); - } - - @Override - public Id getId() { - return new Id("SortByTimeStamp"); - } - }; - queries.add(query); - - query = new Query<SampleBean>() { - @Override - protected void describe(Criteria criterias) { - criterias.add(new WhereCriterion(new Id("name"), map.get("name"), TypeMapper.Criteria.Equal)); - criterias.add(new WhereCriterion(new Id("timeStamp>="), map.get("timeStamp"), TypeMapper.Criteria.GreaterEqual)); - criterias.add(new WhereCriterion(new Id("timeStamp<="), map.get("timeStamp"), TypeMapper.Criteria.LessEqual)); - criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Ascending)); - criterias.add(new LimitCriterion(new Id("limit"))); - } - - @Override - public Id getId() { - return new Id("RangedQuery"); - } - }; - queries.add(query); - } - - @Test - public void testBeanAdapterBuilder() throws Exception { - - BeanAdapterBuilder<SampleBean> builder = - new BeanAdapterBuilder<>(SampleBean.class, queries); - - BeanAdapter<SampleBean> adapter = builder.build(); - assertNotNull(adapter); - - com.redhat.thermostat.storage.core.Category<SampleBean> category = - adapter.getCategory(); - - assertNotNull(category); - assertEquals("testCategory", category.getName()); - - String expected = "ADD testCategory SET 'name' = ?s , 'timeStamp' = ?l , 'vmId' = ?i"; - Set<String> statements = adapter.describeStatements(); - - assertEquals(3, statements.size()); - assertTrue(statements.contains(expected)); - - expected = "QUERY testCategory WHERE 'name' = ?s AND 'vmId' >= ?i AND 'vmId' <= ?i SORT 'timeStamp' ASC LIMIT ?i"; - assertTrue(statements.contains(expected)); - - expected = "QUERY testCategory WHERE 'name' = ?s AND 'timeStamp' >= ?l AND 'timeStamp' <= ?l SORT 'timeStamp' ASC LIMIT ?i"; - assertTrue(statements.contains(expected)); - - for (String s : statements) { - DescriptorTester.testStatement(category, s); - } - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/BeanAdapterTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,287 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Cursor; -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.core.StatementDescriptor; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.storage.model.TimeStampedPojo; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -public class BeanAdapterTest { - - @com.redhat.thermostat.thread.dao.impl.statement.support.Category("testCategory") - private static class SampleBean implements TimeStampedPojo { - - private long timeStamp; - private int vmId; - private String name; - - @Indexed - @Persist - public int getVmId() { - return vmId; - } - - @Indexed - @Persist - public void setVmId(int vmId) { - this.vmId = vmId; - } - - @Persist - public String getName() { - return name; - } - - @Persist - public void setName(String name) { - this.name = name; - } - - @Indexed - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Indexed - @Persist - @Override - public long getTimeStamp() { - return timeStamp; - } - } - - private Storage storage; - private PreparedStatement<SampleBean> insert; - private PreparedStatement<SampleBean> query; - - private List<Query<SampleBean>> queries; - - private static final Id SORT = new Id("SortByTimeStamp"); - private static final Id RANGE = new Id("RangedQuery"); - - @Before - public void setUp() throws Exception { - storage = mock(Storage.class); - insert = mock(PreparedStatement.class); - query = mock(PreparedStatement.class); - - queries = new ArrayList<>(); - - List<FieldDescriptor> descriptors = StatementUtils.createDescriptors(SampleBean.class); - final Map<String, FieldDescriptor> map = StatementUtils.createDescriptorMap(descriptors); - Query query = new Query<SampleBean>() { - @Override - protected void describe(Criteria criterias) { - criterias.add(new WhereCriterion(new Id("0"), map.get("name"), TypeMapper.Criteria.Equal)); - criterias.add(new WhereCriterion(new Id("1"), map.get("vmId"), TypeMapper.Criteria.GreaterEqual)); - criterias.add(new WhereCriterion(new Id("2"), map.get("vmId"), TypeMapper.Criteria.LessEqual)); - criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Ascending)); - criterias.add(new LimitCriterion(new Id("3"))); - } - - @Override - public Id getId() { - return SORT; - } - }; - queries.add(query); - - query = new Query<SampleBean>() { - @Override - protected void describe(Criteria criterias) { - criterias.add(new WhereCriterion(new Id("0"), map.get("name"), TypeMapper.Criteria.Equal)); - criterias.add(new WhereCriterion(new Id("1"), map.get("timeStamp"), TypeMapper.Criteria.GreaterEqual)); - criterias.add(new WhereCriterion(new Id("2"), map.get("timeStamp"), TypeMapper.Criteria.LessEqual)); - criterias.add(new SortCriterion(map.get("timeStamp"), TypeMapper.Sort.Descending)); - criterias.add(new LimitCriterion(new Id("3"))); - } - - @Override - public Id getId() { - return RANGE; - } - }; - queries.add(query); - } - - @Test - public void testHasQueries() throws Exception { - BeanAdapterBuilder<SampleBean> builder = - new BeanAdapterBuilder<>(SampleBean.class, queries); - - BeanAdapter<SampleBean> adapter = builder.build(); - - Query query = adapter.getQuery(SORT); - assertSame(queries.get(0), query); - - query = adapter.getQuery(RANGE); - assertSame(queries.get(1), query); - } - - @Test - public void testInsert() throws Exception { - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(insert); - - BeanAdapterBuilder<SampleBean> builder = - new BeanAdapterBuilder<>(SampleBean.class, queries); - - BeanAdapter<SampleBean> adapter = builder.build(); - - SampleBean bean = new SampleBean(); - bean.setName("fluff"); - bean.setTimeStamp(1000l); - bean.setVmId(0xcafe); - - adapter.insert(bean, storage); - - verify(storage).prepareStatement(any(StatementDescriptor.class)); - - // the order is given by the bean methods, they are sorted in - // alphabetical order, this is not really important though, - // since the order is based on the same FieldDescriptors used by - // both the builder and the adapter, so it's transparent to the user - - verify(insert).setString(0, "fluff"); - verify(insert).setLong(1, 1000l); - verify(insert).setInt(2, 0xcafe); - - verify(insert).execute(); - verifyNoMoreInteractions(insert); - } - - @Test - public void testSortQuery() throws Exception { - - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(query); - Cursor cursor = mock(Cursor.class); - SampleBean sample = mock(SampleBean.class); - when(query.executeQuery()).thenReturn(cursor); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(sample); - - BeanAdapterBuilder<SampleBean> builder = - new BeanAdapterBuilder<>(SampleBean.class, queries); - - BeanAdapter<SampleBean> adapter = builder.build(); - Query sort = adapter.getQuery(SORT); - assertNotNull(sort); - assertSame(sort, queries.get(0)); - - QueryValues values = sort.createValues(); - values.set(new Id("0"), "fluff"); - values.set(new Id("1"), 5); - values.set(new Id("2"), 5); - values.set(new Id("3"), 2); - - final boolean [] called = new boolean[1]; - ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() { - @Override - public void onResult(SampleBean result) { - called[0] = true; - } - }; - - adapter.query(values, handler, storage); - - verify(query).setString(0, "fluff"); - verify(query).setInt(1, 5); - verify(query).setInt(2, 5); - verify(query).setInt(3, 2); - - assertTrue(called[0]); - } - - @Test - public void testRangeQuery() throws Exception { - - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(query); - Cursor cursor = mock(Cursor.class); - SampleBean sample = mock(SampleBean.class); - when(query.executeQuery()).thenReturn(cursor); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(sample); - - BeanAdapterBuilder<SampleBean> builder = - new BeanAdapterBuilder<>(SampleBean.class, queries); - - BeanAdapter<SampleBean> adapter = builder.build(); - Query range = adapter.getQuery(RANGE); - assertNotNull(range); - assertSame(range, queries.get(1)); - - QueryValues values = range.createValues(); - values.set(new Id("0"), "fluff"); - values.set(new Id("1"), 10l); - values.set(new Id("2"), 20l); - values.set(new Id("3"), 2); - - final boolean [] called = new boolean[1]; - ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() { - @Override - public void onResult(SampleBean result) { - called[0] = true; - } - }; - - adapter.query(values, handler, storage); - - verify(query).setString(0, "fluff"); - verify(query).setLong(1, 10); - verify(query).setLong(2, 20); - verify(query).setInt(3, 2); - - assertTrue(called[0]); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/CategoryBuilderTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,125 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.model.TimeStampedPojo; -import java.util.Collection; -import java.util.List; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class CategoryBuilderTest { - - private static final String CATEGORY_NAME = "builder-category-test"; - - @Category(CATEGORY_NAME) - private static class SampleBean implements TimeStampedPojo { - - private long timeStamp; - private String vmId; - private String name; - - @Indexed - @Persist - public String getVmId() { - return vmId; - } - - @Indexed - @Persist - public void setVmId(String vmId) { - this.vmId = vmId; - } - - @Persist - public String getName() { - return name; - } - - @Persist - public void setName(String name) { - this.name = name; - } - - @Indexed - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Indexed - @Persist - @Override - public long getTimeStamp() { - return timeStamp; - } - } - - @Test - public void testBuild() throws Exception { - CategoryBuilder builder = new CategoryBuilder(SampleBean.class); - com.redhat.thermostat.storage.core.Category<SampleBean> category = - builder.build(); - - assertNotNull(category); - assertEquals(CATEGORY_NAME, category.getName()); - assertEquals(SampleBean.class, category.getDataClass()); - - List<Key<?>> indexed = category.getIndexedKeys(); - assertEquals(2, indexed.size()); - - Key key0 = new Key("vmId"); - Key key1 = new Key("timeStamp"); - - assertTrue(indexed.contains(key0)); - assertTrue(indexed.contains(key1)); - - Collection<Key<?>> keys = category.getKeys(); - assertEquals(3, keys.size()); - - Key key2 = new Key("name"); - - assertTrue(keys.contains(key0)); - assertTrue(keys.contains(key1)); - assertTrue(keys.contains(key2)); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/DescriptorTester.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Category; -import com.redhat.thermostat.storage.core.StatementDescriptor; -import com.redhat.thermostat.storage.model.Pojo; -import com.redhat.thermostat.storage.testutils.StatementDescriptorTester; - -/** - * - */ -public class DescriptorTester { - - public static <C extends Pojo> void testStatement(Category<C> category, String statement) throws Exception { - - StatementDescriptorTester<C> tester = new StatementDescriptorTester<>(); - StatementDescriptor<C> desc = new StatementDescriptor<>(category, statement); - tester.testParseBasic(desc); - tester.testParseSemantic(desc); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/InsertEngineTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,87 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class InsertEngineTest { - private com.redhat.thermostat.storage.core.Category category; - private FieldDescriptor descriptor0; - private FieldDescriptor descriptor1; - private FieldDescriptor descriptor2; - - @Before - public void setup() { - category = mock(com.redhat.thermostat.storage.core.Category.class); - when(category.getName()).thenReturn("testCategory"); - - descriptor0 = mock(FieldDescriptor.class); - when(descriptor0.getName()).thenReturn("descriptor0"); - Class type = String.class; - when(descriptor0.getType()).thenReturn(type); - - descriptor1 = mock(FieldDescriptor.class); - when(descriptor1.getName()).thenReturn("descriptor1"); - type = long.class; - when(descriptor1.getType()).thenReturn(type); - - descriptor2 = mock(FieldDescriptor.class); - when(descriptor2.getName()).thenReturn("descriptor2"); - type = int.class; - when(descriptor2.getType()).thenReturn(type); - } - - @Test - public void testBuildInsert() throws Exception { - InsertEngine engine = new InsertEngine(); - - engine.prologue(category); - engine.add(descriptor0); - engine.add(descriptor1); - engine.add(descriptor2); - - Statement statement = engine.build(); - - String expected = "ADD testCategory SET 'descriptor0' = ?s , 'descriptor1' = ?l , 'descriptor2' = ?i"; - assertEquals(expected, statement.get()); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/QueryEngineTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class QueryEngineTest { - private com.redhat.thermostat.storage.core.Category category; - private FieldDescriptor descriptor0; - private FieldDescriptor descriptor1; - private FieldDescriptor descriptor2; - - @Before - public void setup() { - category = mock(com.redhat.thermostat.storage.core.Category.class); - when(category.getName()).thenReturn("testCategory"); - - descriptor0 = mock(FieldDescriptor.class); - when(descriptor0.getName()).thenReturn("descriptor0"); - Class type = String.class; - when(descriptor0.getType()).thenReturn(type); - - descriptor1 = mock(FieldDescriptor.class); - when(descriptor1.getName()).thenReturn("descriptor1"); - type = long.class; - when(descriptor1.getType()).thenReturn(type); - - descriptor2 = mock(FieldDescriptor.class); - when(descriptor2.getName()).thenReturn("descriptor2"); - type = int.class; - when(descriptor2.getType()).thenReturn(type); - } - - @Test - public void testBuildQuery() throws Exception { - QueryEngine engine = new QueryEngine(); - - engine.prologue(category); - engine.add(descriptor0, TypeMapper.Criteria.Equal); - engine.add(descriptor1, TypeMapper.Criteria.GreaterEqual); - engine.add(descriptor2, TypeMapper.Criteria.LessEqual); - - engine.limit(); - engine.sort(descriptor1, TypeMapper.Sort.Ascending); - - Statement statement = engine.build(); - - String expected = "QUERY testCategory WHERE 'descriptor0' = ?s AND 'descriptor1' >= ?l AND 'descriptor2' <= ?i SORT 'descriptor1' ASC LIMIT ?i"; - - assertEquals(expected, statement.get()); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/QueryValuesTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import java.util.ArrayList; -import java.util.List; -import org.junit.Before; -import org.junit.Test; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class QueryValuesTest { - - private Query query; - private List<Criterion> criterias; - - private Criterion criterion0; - private Criterion criterion1; - private Criterion criterion2; - - @Before - public void setup() { - query = mock(Query.class); - criterias = new ArrayList<>(); - - criterion0 = mock(Criterion.class); - when(criterion0.getType()).thenReturn((Class) int.class); - when(criterion0.getId()).thenReturn(new Id("0")); - - criterion1 = mock(Criterion.class); - when(criterion1.getType()).thenReturn((Class) String.class); - when(criterion1.getId()).thenReturn(new Id("1")); - - criterion2 = mock(Criterion.class); - when(criterion2.getType()).thenReturn((Class) long.class); - when(criterion2.getId()).thenReturn(new Id("2")); - - criterias.add(criterion0); - criterias.add(criterion1); - criterias.add(criterion2); - } - - @Test(expected = IllegalArgumentException.class) - public void testSetWithWrongArguments() throws Exception { - - QueryValues values = new QueryValues(query); - values.addCriteria(criterias); - - values.set(criterion0, "wrong type"); - } - - @Test - public void testSet() throws Exception { - - QueryValues values = new QueryValues(query); - values.addCriteria(criterias); - - values.set(criterion0, 10); - values.set(criterion1, "test"); - values.set(criterion2, 42l); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/StatementTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class StatementTest { - - @Test - public void testEquals() throws Exception { - Statement statement = new Statement("test"); - Statement statement2 = new Statement("test"); - - assertTrue(statement.equals("test")); - assertTrue(statement.equals(statement2)); - - statement2 = new Statement("fluff"); - assertFalse(statement.equals("fluff")); - assertFalse(statement.equals(statement2)); - } - - @Test - public void testHashCode() throws Exception { - Statement statement = new Statement("test"); - Statement statement2 = new Statement("test"); - - assertEquals(statement.hashCode(), statement2.hashCode()); - - statement2 = new Statement("fluff"); - assertFalse(statement.hashCode() == statement2.hashCode()); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/statement/support/StatementUtilsTest.java Mon Dec 01 08:51:43 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,146 +0,0 @@ -/* - * Copyright 2012-2014 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.dao.impl.statement.support; - -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.model.TimeStampedPojo; -import java.util.List; -import java.util.Map; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class StatementUtilsTest { - - @Category("testCategory") - private static class SampleBean implements TimeStampedPojo { - - private long timeStamp; - private int vmId; - private String name; - - @Indexed - @Persist - public int getVmId() { - return vmId; - } - - @Indexed - @Persist - public void setVmId(int vmId) { - this.vmId = vmId; - } - - @Persist - public String getName() { - return name; - } - - @Persist - public void setName(String name) { - this.name = name; - } - - @Indexed - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Indexed - @Persist - @Override - public long getTimeStamp() { - return timeStamp; - } - } - - @Category("testCategory2") - private static class InvalidSampleBean implements TimeStampedPojo { - - private long timeStamp; - private int vmId; - private String name; - - public int getVmId() { - return vmId; - } - - @Indexed - @Persist - public void setVmId(int vmId) { - this.vmId = vmId; - } - - @Persist - public String getName() { - return name; - } - - @Persist - public void setName(String name) { - this.name = name; - } - - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Indexed - @Persist - @Override - public long getTimeStamp() { - return timeStamp; - } - } - - @Test - public void testAnnotationSupport() { - List<FieldDescriptor> descriptors = - StatementUtils.createDescriptors(SampleBean.class); - Map<String, FieldDescriptor> map = - StatementUtils.createDescriptorMap(descriptors); - - assertTrue(map.containsKey("timeStamp")); - - descriptors = StatementUtils.createDescriptors(InvalidSampleBean.class); - map = StatementUtils.createDescriptorMap(descriptors); - - assertFalse(map.containsKey("timeStamp")); - } -}