|
1 | 1 | package com.scalar.db.storage.jdbc; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
3 | 5 | import com.scalar.db.api.DistributedStorageColumnValueIntegrationTestBase; |
| 6 | +import com.scalar.db.api.Get; |
| 7 | +import com.scalar.db.api.Put; |
| 8 | +import com.scalar.db.api.PutBuilder; |
| 9 | +import com.scalar.db.api.Result; |
| 10 | +import com.scalar.db.api.TableMetadata; |
4 | 11 | import com.scalar.db.config.DatabaseConfig; |
| 12 | +import com.scalar.db.exception.storage.ExecutionException; |
| 13 | +import com.scalar.db.io.BigIntColumn; |
| 14 | +import com.scalar.db.io.BlobColumn; |
| 15 | +import com.scalar.db.io.BooleanColumn; |
5 | 16 | import com.scalar.db.io.Column; |
6 | 17 | import com.scalar.db.io.DataType; |
| 18 | +import com.scalar.db.io.DateColumn; |
| 19 | +import com.scalar.db.io.DoubleColumn; |
| 20 | +import com.scalar.db.io.FloatColumn; |
| 21 | +import com.scalar.db.io.IntColumn; |
| 22 | +import com.scalar.db.io.Key; |
| 23 | +import com.scalar.db.io.TextColumn; |
| 24 | +import com.scalar.db.io.TimeColumn; |
| 25 | +import com.scalar.db.io.TimestampColumn; |
| 26 | +import com.scalar.db.io.TimestampTZColumn; |
7 | 27 | import com.scalar.db.util.TestUtils; |
| 28 | +import java.util.Optional; |
8 | 29 | import java.util.Properties; |
9 | 30 | import java.util.Random; |
| 31 | +import java.util.stream.Stream; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.Arguments; |
| 35 | +import org.junit.jupiter.params.provider.MethodSource; |
10 | 36 |
|
11 | 37 | public class JdbcDatabaseColumnValueIntegrationTest |
12 | 38 | extends DistributedStorageColumnValueIntegrationTestBase { |
@@ -68,4 +94,163 @@ protected Column<?> getColumnWithMaxValue(String columnName, DataType dataType) |
68 | 94 | } |
69 | 95 | return super.getColumnWithMaxValue(columnName, dataType); |
70 | 96 | } |
| 97 | + |
| 98 | + @ParameterizedTest() |
| 99 | + @MethodSource("provideBlobSizes") |
| 100 | + public void put_largeBlobData_ShouldWorkCorrectly(int blobSize, String humanReadableBlobSize) |
| 101 | + throws ExecutionException { |
| 102 | + String tableName = TABLE + "_large_single_blob_single"; |
| 103 | + try { |
| 104 | + // Arrange |
| 105 | + TableMetadata.Builder metadata = |
| 106 | + TableMetadata.newBuilder() |
| 107 | + .addColumn(COL_NAME1, DataType.INT) |
| 108 | + .addColumn(COL_NAME2, DataType.BLOB) |
| 109 | + .addPartitionKey(COL_NAME1); |
| 110 | + |
| 111 | + admin.createTable(namespace, tableName, metadata.build(), true, getCreationOptions()); |
| 112 | + admin.truncateTable(namespace, tableName); |
| 113 | + byte[] blobData = createLargeBlob(blobSize); |
| 114 | + Put put = |
| 115 | + Put.newBuilder() |
| 116 | + .namespace(namespace) |
| 117 | + .table(tableName) |
| 118 | + .partitionKey(Key.ofInt(COL_NAME1, 1)) |
| 119 | + .blobValue(COL_NAME2, blobData) |
| 120 | + .build(); |
| 121 | + |
| 122 | + // Act |
| 123 | + storage.put(put); |
| 124 | + |
| 125 | + // Assert |
| 126 | + Optional<Result> optionalResult = |
| 127 | + storage.get( |
| 128 | + Get.newBuilder() |
| 129 | + .namespace(namespace) |
| 130 | + .table(tableName) |
| 131 | + .partitionKey(Key.ofInt(COL_NAME1, 1)) |
| 132 | + .build()); |
| 133 | + assertThat(optionalResult).isPresent(); |
| 134 | + Result result = optionalResult.get(); |
| 135 | + assertThat(result.getColumns().get(COL_NAME2).getBlobValueAsBytes()).isEqualTo(blobData); |
| 136 | + } finally { |
| 137 | + admin.dropTable(namespace, tableName, true); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + Stream<Arguments> provideBlobSizes() { |
| 142 | + return Stream.of( |
| 143 | + Arguments.of(32_766, "32,766 KB"), |
| 144 | + Arguments.of(32_767, "32,767 KB"), |
| 145 | + Arguments.of(100_000_000, "100 MB")); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void put_largeBlobData_WithMultipleBlobColumnsShouldWorkCorrectly() |
| 150 | + throws ExecutionException { |
| 151 | + String tableName = TABLE + "_large_multiples_blob"; |
| 152 | + try { |
| 153 | + // Arrange |
| 154 | + TableMetadata.Builder metadata = |
| 155 | + TableMetadata.newBuilder() |
| 156 | + .addColumn(COL_NAME1, DataType.INT) |
| 157 | + .addColumn(COL_NAME2, DataType.BLOB) |
| 158 | + .addColumn(COL_NAME3, DataType.BLOB) |
| 159 | + .addPartitionKey(COL_NAME1); |
| 160 | + |
| 161 | + admin.createTable(namespace, tableName, metadata.build(), true, getCreationOptions()); |
| 162 | + admin.truncateTable(namespace, tableName); |
| 163 | + byte[] blobDataCol2 = createLargeBlob(32_766); |
| 164 | + byte[] blobDataCol3 = createLargeBlob(5000); |
| 165 | + Put put = |
| 166 | + Put.newBuilder() |
| 167 | + .namespace(namespace) |
| 168 | + .table(tableName) |
| 169 | + .partitionKey(Key.ofInt(COL_NAME1, 1)) |
| 170 | + .blobValue(COL_NAME2, blobDataCol2) |
| 171 | + .blobValue(COL_NAME3, blobDataCol3) |
| 172 | + .build(); |
| 173 | + |
| 174 | + // Act |
| 175 | + storage.put(put); |
| 176 | + |
| 177 | + // Assert |
| 178 | + Optional<Result> optionalResult = |
| 179 | + storage.get( |
| 180 | + Get.newBuilder() |
| 181 | + .namespace(namespace) |
| 182 | + .table(tableName) |
| 183 | + .partitionKey(Key.ofInt(COL_NAME1, 1)) |
| 184 | + .build()); |
| 185 | + assertThat(optionalResult).isPresent(); |
| 186 | + Result result = optionalResult.get(); |
| 187 | + assertThat(result.getColumns().get(COL_NAME2).getBlobValueAsBytes()).isEqualTo(blobDataCol2); |
| 188 | + assertThat(result.getColumns().get(COL_NAME3).getBlobValueAsBytes()).isEqualTo(blobDataCol3); |
| 189 | + } finally { |
| 190 | + admin.dropTable(namespace, tableName, true); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void put_largeBlobData_WithAllColumnsTypesShouldWorkCorrectly() throws ExecutionException { |
| 196 | + // Arrange |
| 197 | + IntColumn partitionKeyValue = (IntColumn) getColumnWithMaxValue(PARTITION_KEY, DataType.INT); |
| 198 | + BooleanColumn col1Value = (BooleanColumn) getColumnWithMaxValue(COL_NAME1, DataType.BOOLEAN); |
| 199 | + IntColumn col2Value = (IntColumn) getColumnWithMaxValue(COL_NAME2, DataType.INT); |
| 200 | + BigIntColumn col3Value = (BigIntColumn) getColumnWithMaxValue(COL_NAME3, DataType.BIGINT); |
| 201 | + FloatColumn col4Value = (FloatColumn) getColumnWithMaxValue(COL_NAME4, DataType.FLOAT); |
| 202 | + DoubleColumn col5Value = (DoubleColumn) getColumnWithMaxValue(COL_NAME5, DataType.DOUBLE); |
| 203 | + TextColumn col6Value = (TextColumn) getColumnWithMaxValue(COL_NAME6, DataType.TEXT); |
| 204 | + BlobColumn col7Value = BlobColumn.of(COL_NAME7, createLargeBlob(32_766)); |
| 205 | + DateColumn col8Value = (DateColumn) getColumnWithMaxValue(COL_NAME8, DataType.DATE); |
| 206 | + TimeColumn col9Value = (TimeColumn) getColumnWithMaxValue(COL_NAME9, DataType.TIME); |
| 207 | + TimestampTZColumn col10Value = |
| 208 | + (TimestampTZColumn) getColumnWithMaxValue(COL_NAME10, DataType.TIMESTAMPTZ); |
| 209 | + TimestampColumn column11Value = null; |
| 210 | + if (isTimestampTypeSupported()) { |
| 211 | + column11Value = (TimestampColumn) getColumnWithMaxValue(COL_NAME11, DataType.TIMESTAMP); |
| 212 | + } |
| 213 | + |
| 214 | + PutBuilder.Buildable put = |
| 215 | + Put.newBuilder() |
| 216 | + .namespace(namespace) |
| 217 | + .table(TABLE) |
| 218 | + .partitionKey(Key.newBuilder().add(partitionKeyValue).build()) |
| 219 | + .value(col1Value) |
| 220 | + .value(col2Value) |
| 221 | + .value(col3Value) |
| 222 | + .value(col4Value) |
| 223 | + .value(col5Value) |
| 224 | + .value(col6Value) |
| 225 | + .value(col7Value) |
| 226 | + .value(col8Value) |
| 227 | + .value(col9Value) |
| 228 | + .value(col10Value); |
| 229 | + if (isTimestampTypeSupported()) { |
| 230 | + put.value(column11Value); |
| 231 | + } |
| 232 | + // Act |
| 233 | + storage.put(put.build()); |
| 234 | + |
| 235 | + // Assert |
| 236 | + assertResult( |
| 237 | + partitionKeyValue, |
| 238 | + col1Value, |
| 239 | + col2Value, |
| 240 | + col3Value, |
| 241 | + col4Value, |
| 242 | + col5Value, |
| 243 | + col6Value, |
| 244 | + col7Value, |
| 245 | + col8Value, |
| 246 | + col9Value, |
| 247 | + col10Value, |
| 248 | + column11Value); |
| 249 | + } |
| 250 | + |
| 251 | + private byte[] createLargeBlob(int size) { |
| 252 | + byte[] blob = new byte[size]; |
| 253 | + random.nextBytes(blob); |
| 254 | + return blob; |
| 255 | + } |
71 | 256 | } |
0 commit comments