|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.driver.mapper; |
| 17 | + |
| 18 | +import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import com.datastax.oss.driver.api.core.CqlIdentifier; |
| 22 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 23 | +import com.datastax.oss.driver.api.core.PagingIterable; |
| 24 | +import com.datastax.oss.driver.api.core.cql.BoundStatementBuilder; |
| 25 | +import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
| 26 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 27 | +import com.datastax.oss.driver.api.mapper.MapperContext; |
| 28 | +import com.datastax.oss.driver.api.mapper.annotations.ClusteringColumn; |
| 29 | +import com.datastax.oss.driver.api.mapper.annotations.Dao; |
| 30 | +import com.datastax.oss.driver.api.mapper.annotations.DaoFactory; |
| 31 | +import com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace; |
| 32 | +import com.datastax.oss.driver.api.mapper.annotations.Entity; |
| 33 | +import com.datastax.oss.driver.api.mapper.annotations.Insert; |
| 34 | +import com.datastax.oss.driver.api.mapper.annotations.Mapper; |
| 35 | +import com.datastax.oss.driver.api.mapper.annotations.PartitionKey; |
| 36 | +import com.datastax.oss.driver.api.mapper.annotations.QueryProvider; |
| 37 | +import com.datastax.oss.driver.api.mapper.entity.EntityHelper; |
| 38 | +import com.datastax.oss.driver.api.querybuilder.select.Select; |
| 39 | +import com.datastax.oss.driver.api.testinfra.ccm.CcmRule; |
| 40 | +import com.datastax.oss.driver.api.testinfra.session.SessionRule; |
| 41 | +import com.datastax.oss.driver.categories.ParallelizableTests; |
| 42 | +import java.util.Objects; |
| 43 | +import org.junit.BeforeClass; |
| 44 | +import org.junit.ClassRule; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.experimental.categories.Category; |
| 47 | +import org.junit.rules.RuleChain; |
| 48 | +import org.junit.rules.TestRule; |
| 49 | + |
| 50 | +@Category(ParallelizableTests.class) |
| 51 | +public class QueryProviderIT { |
| 52 | + |
| 53 | + private static CcmRule ccm = CcmRule.getInstance(); |
| 54 | + private static SessionRule<CqlSession> sessionRule = SessionRule.builder(ccm).build(); |
| 55 | + @ClassRule public static TestRule chain = RuleChain.outerRule(ccm).around(sessionRule); |
| 56 | + |
| 57 | + private static SensorDao dao; |
| 58 | + |
| 59 | + @BeforeClass |
| 60 | + public static void setup() { |
| 61 | + CqlSession session = sessionRule.session(); |
| 62 | + |
| 63 | + session.execute( |
| 64 | + SimpleStatement.builder( |
| 65 | + "CREATE TABLE sensor_reading(id int, month int, day int, value double, " |
| 66 | + + "PRIMARY KEY (id, month, day)) " |
| 67 | + + "WITH CLUSTERING ORDER BY (month DESC, day DESC)") |
| 68 | + .setExecutionProfile(sessionRule.slowProfile()) |
| 69 | + .build()); |
| 70 | + |
| 71 | + SensorMapper mapper = new QueryProviderIT_SensorMapperBuilder(session).build(); |
| 72 | + dao = mapper.sensorDao(sessionRule.keyspace()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void should_invoke_query_provider() { |
| 77 | + SensorReading readingFeb3 = new SensorReading(1, 2, 3, 9.3); |
| 78 | + SensorReading readingFeb2 = new SensorReading(1, 2, 2, 8.6); |
| 79 | + SensorReading readingFeb1 = new SensorReading(1, 2, 1, 8.7); |
| 80 | + SensorReading readingJan31 = new SensorReading(1, 1, 31, 8.2); |
| 81 | + dao.save(readingFeb3); |
| 82 | + dao.save(readingFeb2); |
| 83 | + dao.save(readingFeb1); |
| 84 | + dao.save(readingJan31); |
| 85 | + |
| 86 | + assertThat(dao.findSlice(1, null, null).all()) |
| 87 | + .containsExactly(readingFeb3, readingFeb2, readingFeb1, readingJan31); |
| 88 | + assertThat(dao.findSlice(1, 2, null).all()) |
| 89 | + .containsExactly(readingFeb3, readingFeb2, readingFeb1); |
| 90 | + assertThat(dao.findSlice(1, 2, 3).all()).containsExactly(readingFeb3); |
| 91 | + } |
| 92 | + |
| 93 | + @Mapper |
| 94 | + public interface SensorMapper { |
| 95 | + @DaoFactory |
| 96 | + SensorDao sensorDao(@DaoKeyspace CqlIdentifier keyspace); |
| 97 | + } |
| 98 | + |
| 99 | + @Dao |
| 100 | + public interface SensorDao { |
| 101 | + @QueryProvider(providerClass = FindSliceProvider.class, entityHelpers = SensorReading.class) |
| 102 | + PagingIterable<SensorReading> findSlice(int id, Integer month, Integer day); |
| 103 | + |
| 104 | + @Insert |
| 105 | + void save(SensorReading reading); |
| 106 | + } |
| 107 | + |
| 108 | + public static class FindSliceProvider { |
| 109 | + private final CqlSession session; |
| 110 | + private final EntityHelper<SensorReading> sensorReadingHelper; |
| 111 | + private final Select selectStart; |
| 112 | + |
| 113 | + public FindSliceProvider( |
| 114 | + MapperContext context, EntityHelper<SensorReading> sensorReadingHelper) { |
| 115 | + this.session = context.getSession(); |
| 116 | + this.sensorReadingHelper = sensorReadingHelper; |
| 117 | + this.selectStart = |
| 118 | + sensorReadingHelper.selectStart().whereColumn("id").isEqualTo(bindMarker()); |
| 119 | + } |
| 120 | + |
| 121 | + public PagingIterable<SensorReading> findSlice(int id, Integer month, Integer day) { |
| 122 | + if (month == null && day != null) { |
| 123 | + throw new IllegalArgumentException("Can't specify day if month is null"); |
| 124 | + } |
| 125 | + |
| 126 | + Select select = this.selectStart; |
| 127 | + if (month != null) { |
| 128 | + select = select.whereColumn("month").isEqualTo(bindMarker()); |
| 129 | + if (day != null) { |
| 130 | + select = select.whereColumn("day").isEqualTo(bindMarker()); |
| 131 | + } |
| 132 | + } |
| 133 | + PreparedStatement preparedStatement = session.prepare(select.build()); |
| 134 | + BoundStatementBuilder boundStatementBuilder = |
| 135 | + preparedStatement.boundStatementBuilder().setInt("id", id); |
| 136 | + if (month != null) { |
| 137 | + boundStatementBuilder = boundStatementBuilder.setInt("month", month); |
| 138 | + if (day != null) { |
| 139 | + boundStatementBuilder = boundStatementBuilder.setInt("day", day); |
| 140 | + } |
| 141 | + } |
| 142 | + return session.execute(boundStatementBuilder.build()).map(sensorReadingHelper::get); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Entity |
| 147 | + public static class SensorReading { |
| 148 | + @PartitionKey private int id; |
| 149 | + |
| 150 | + @ClusteringColumn(1) |
| 151 | + private int month; |
| 152 | + |
| 153 | + @ClusteringColumn(2) |
| 154 | + private int day; |
| 155 | + |
| 156 | + private double value; |
| 157 | + |
| 158 | + public SensorReading() {} |
| 159 | + |
| 160 | + public SensorReading(int id, int month, int day, double value) { |
| 161 | + this.id = id; |
| 162 | + this.month = month; |
| 163 | + this.day = day; |
| 164 | + this.value = value; |
| 165 | + } |
| 166 | + |
| 167 | + public int getId() { |
| 168 | + return id; |
| 169 | + } |
| 170 | + |
| 171 | + public void setId(int id) { |
| 172 | + this.id = id; |
| 173 | + } |
| 174 | + |
| 175 | + public int getMonth() { |
| 176 | + return month; |
| 177 | + } |
| 178 | + |
| 179 | + public void setMonth(int month) { |
| 180 | + this.month = month; |
| 181 | + } |
| 182 | + |
| 183 | + public int getDay() { |
| 184 | + return day; |
| 185 | + } |
| 186 | + |
| 187 | + public void setDay(int day) { |
| 188 | + this.day = day; |
| 189 | + } |
| 190 | + |
| 191 | + public double getValue() { |
| 192 | + return value; |
| 193 | + } |
| 194 | + |
| 195 | + public void setValue(double value) { |
| 196 | + this.value = value; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public boolean equals(Object other) { |
| 201 | + if (other == this) { |
| 202 | + return true; |
| 203 | + } else if (other instanceof SensorReading) { |
| 204 | + SensorReading that = (SensorReading) other; |
| 205 | + return this.id == that.id |
| 206 | + && this.month == that.month |
| 207 | + && this.day == that.day |
| 208 | + && this.value == that.value; |
| 209 | + } else { |
| 210 | + return false; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public int hashCode() { |
| 216 | + return Objects.hash(id, month, day, value); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public String toString() { |
| 221 | + return String.format("%d %d/%d %f", id, month, day, value); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments