From 7156d733fa62b34ebe39f0c2f238dea38095a50c Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 10 Oct 2017 15:29:48 -0400 Subject: [PATCH] Also support ordering in clustering columns for materialized views (an oversight). --- .../core/querybuilder/IsNotNullClause.java | 15 +++++++++++++++ .../schemabuilder/CreateMaterializedView.java | 7 ++++++- src/main/java/net/helenus/core/SchemaUtil.java | 18 +++++++++++++++--- .../integration/core/views/CyclistsByAge.java | 9 ++++----- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java b/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java index efaeeaf..9ff2596 100644 --- a/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java +++ b/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2015 The Helenus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.datastax.driver.core.querybuilder; import com.datastax.driver.core.CodecRegistry; diff --git a/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java b/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java index 2ed030e..919becc 100644 --- a/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java +++ b/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java @@ -8,13 +8,15 @@ public class CreateMaterializedView extends Create { private String viewName; private Select.Where selection; private String primaryKey; + private String clustering; public CreateMaterializedView( - String keyspaceName, String viewName, Select.Where selection, String primaryKey) { + String keyspaceName, String viewName, Select.Where selection, String primaryKey, String clustering) { super(keyspaceName, viewName); this.viewName = viewName; this.selection = selection; this.primaryKey = primaryKey; + this.clustering = clustering; } public String getQueryString(CodecRegistry codecRegistry) { @@ -37,6 +39,9 @@ public class CreateMaterializedView extends Create { createStatement.setLength(createStatement.length() - 1); createStatement.append(" "); createStatement.append(primaryKey); + if (clustering != null) { + createStatement.append(" ").append(clustering); + } createStatement.append(";"); return createStatement.toString(); diff --git a/src/main/java/net/helenus/core/SchemaUtil.java b/src/main/java/net/helenus/core/SchemaUtil.java index 4e65814..6a228e9 100644 --- a/src/main/java/net/helenus/core/SchemaUtil.java +++ b/src/main/java/net/helenus/core/SchemaUtil.java @@ -27,6 +27,7 @@ import java.util.stream.Collectors; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.*; import net.helenus.mapping.ColumnType; +import net.helenus.mapping.annotation.ClusteringColumn; import net.helenus.mapping.type.OptionalColumnMetadata; import net.helenus.support.CqlUtil; import net.helenus.support.HelenusMappingException; @@ -171,11 +172,12 @@ public final class SchemaUtil { String columnName = prop.getColumnName(); selection = selection.column(columnName); } - String tableName = - Helenus.entity(entity.getMappingInterface().getInterfaces()[0]).getName().toCql(); + Class iface = entity.getMappingInterface(); + String tableName = Helenus.entity(iface.getInterfaces()[0]).getName().toCql(); Select.Where where = selection.from(tableName).where(); List p = new ArrayList(props.size()); List c = new ArrayList(props.size()); + List o = new ArrayList(props.size()); for (HelenusPropertyNode prop : props) { String columnName = prop.getColumnName(); @@ -184,9 +186,15 @@ public final class SchemaUtil { p.add(columnName); where = where.and(new IsNotNullClause(columnName)); break; + case CLUSTERING_COLUMN: c.add(columnName); where = where.and(new IsNotNullClause(columnName)); + + ClusteringColumn clusteringColumn = prop.getProperty().getGetterMethod().getAnnotation(ClusteringColumn.class); + if (clusteringColumn != null && clusteringColumn.ordering() != null) { + o.add(columnName + " " + clusteringColumn.ordering().cql()); + } break; default: break; @@ -201,7 +209,11 @@ public final class SchemaUtil { : "") + ")"; - return new CreateMaterializedView(keyspace, viewName, where, primaryKey); + String clustering = ""; + if (o.size() > 0) { + clustering = "WITH CLUSTERING ORDER BY (" + String.join(", ", o) + ")"; + } + return new CreateMaterializedView(keyspace, viewName, where, primaryKey, clustering); } public static SchemaStatement dropMaterializedView( diff --git a/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java b/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java index bff85e2..02b414c 100644 --- a/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java +++ b/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java @@ -2,17 +2,16 @@ package net.helenus.test.integration.core.views; import java.util.Date; import java.util.UUID; -import net.helenus.mapping.annotation.ClusteringColumn; -import net.helenus.mapping.annotation.Index; -import net.helenus.mapping.annotation.MaterializedView; -import net.helenus.mapping.annotation.PartitionKey; + +import net.helenus.mapping.OrderingDirection; +import net.helenus.mapping.annotation.*; @MaterializedView public interface CyclistsByAge extends Cyclist { @PartitionKey UUID cid(); - @ClusteringColumn + @ClusteringColumn(ordering = OrderingDirection.ASC) int age(); Date birthday();