|
| 1 | +/* |
| 2 | + * Copyright (C) 2012-2015 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 | + |
| 17 | +package com.datastax.driver.core; |
| 18 | + |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.security.MessageDigest; |
| 21 | +import java.security.NoSuchAlgorithmException; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 25 | +import com.datastax.driver.core.exceptions.PagingStateException; |
| 26 | +import com.datastax.driver.core.utils.Bytes; |
| 27 | + |
| 28 | +/** |
| 29 | + * The paging state of a query. |
| 30 | + * |
| 31 | + * This object represents the next page to be fetched if the query is |
| 32 | + * multi page. It can be saved and reused later on the same statement. |
| 33 | + * |
| 34 | + * The PagingState can be serialized and deserialized either as a String |
| 35 | + * or as a byte array. |
| 36 | + * |
| 37 | + * @see Statement#setPagingState(PagingState) |
| 38 | + */ |
| 39 | +public class PagingState { |
| 40 | + |
| 41 | + private byte[] pagingState; |
| 42 | + private byte[] hash; |
| 43 | + |
| 44 | + PagingState(ByteBuffer pagingState, Statement statement) { |
| 45 | + this.pagingState = Bytes.getArray(pagingState); |
| 46 | + this.hash = hash(statement); |
| 47 | + } |
| 48 | + |
| 49 | + private PagingState(byte[] complete) { |
| 50 | + // Check the sizes in the beginning of the buffer, otherwise we cannot build the paging state object |
| 51 | + ByteBuffer pagingStateBB = ByteBuffer.wrap(complete); |
| 52 | + int pagingSize = pagingStateBB.getShort(); |
| 53 | + int hashSize = pagingStateBB.getShort(); |
| 54 | + if (pagingSize + hashSize != pagingStateBB.remaining()) { |
| 55 | + throw new PagingStateException("Cannot deserialize paging state, invalid format. " |
| 56 | + + "The serialized form was corrupted, or not initially generated from a PagingState object."); |
| 57 | + } |
| 58 | + this.pagingState = extractPagingState(complete); |
| 59 | + this.hash = extractHashDigest(complete); |
| 60 | + } |
| 61 | + |
| 62 | + private byte[] hash(Statement statement) { |
| 63 | + byte[] digest; |
| 64 | + ByteBuffer[] values; |
| 65 | + MessageDigest md; |
| 66 | + assert !(statement instanceof BatchStatement); |
| 67 | + try { |
| 68 | + md = MessageDigest.getInstance("MD5"); |
| 69 | + if (statement instanceof BoundStatement) { |
| 70 | + BoundStatement bs = ((BoundStatement)statement); |
| 71 | + md.update(bs.preparedStatement().getQueryString().getBytes()); |
| 72 | + values = bs.values; |
| 73 | + } else { |
| 74 | + //it is a RegularStatement since Batch statements are not allowed |
| 75 | + RegularStatement rs = (RegularStatement)statement; |
| 76 | + md.update(rs.getQueryString().getBytes()); |
| 77 | + values = rs.getValues(); |
| 78 | + } |
| 79 | + if (values != null) { |
| 80 | + for (ByteBuffer value : values) { |
| 81 | + md.update(value.duplicate()); |
| 82 | + } |
| 83 | + } |
| 84 | + md.update(this.pagingState); |
| 85 | + digest = md.digest(); |
| 86 | + |
| 87 | + } catch (NoSuchAlgorithmException e) { |
| 88 | + throw new RuntimeException("MD5 doesn't seem to be available on this JVM", e); |
| 89 | + } |
| 90 | + return digest; |
| 91 | + } |
| 92 | + |
| 93 | + boolean matches(Statement statement) { |
| 94 | + byte[] toTest = hash(statement); |
| 95 | + return Arrays.equals(toTest, this.hash); |
| 96 | + } |
| 97 | + |
| 98 | + private static byte[] extractPagingState(byte[] complete) { |
| 99 | + ByteBuffer completeBB = ByteBuffer.wrap(complete); |
| 100 | + short size = completeBB.getShort(); |
| 101 | + byte[] array = new byte[size]; |
| 102 | + // Go forward of 2 bytes |
| 103 | + completeBB.getShort(); |
| 104 | + completeBB.get(array); |
| 105 | + return array; |
| 106 | + } |
| 107 | + |
| 108 | + private static byte[] extractHashDigest(byte[] complete) { |
| 109 | + ByteBuffer completeBB = ByteBuffer.wrap(complete); |
| 110 | + short pagingSize = completeBB.getShort(); |
| 111 | + short hashSize = completeBB.getShort(); |
| 112 | + completeBB.position(pagingSize + 4); |
| 113 | + byte[] array = new byte[hashSize]; |
| 114 | + completeBB.get(array); |
| 115 | + return array; |
| 116 | + } |
| 117 | + |
| 118 | + private ByteBuffer generateCompleteOutput() { |
| 119 | + ByteBuffer res = ByteBuffer.allocate(pagingState.length + hash.length + 4); |
| 120 | + |
| 121 | + res.putShort((short)pagingState.length); |
| 122 | + res.putShort((short)hash.length); |
| 123 | + |
| 124 | + res.put(pagingState); |
| 125 | + res.put(hash); |
| 126 | + |
| 127 | + res.rewind(); |
| 128 | + |
| 129 | + return res; |
| 130 | + } |
| 131 | + |
| 132 | + ByteBuffer getRawState() { |
| 133 | + return ByteBuffer.wrap(this.pagingState); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String toString() { |
| 138 | + return Bytes.toRawHexString(generateCompleteOutput()); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Create a PagingState object from a string previously generated with {@link #toString()}. |
| 143 | + * |
| 144 | + * @param string the string value. |
| 145 | + * @return the PagingState object created. |
| 146 | + * |
| 147 | + * @throws PagingStateException if the string does not have the correct format. |
| 148 | + */ |
| 149 | + public static PagingState fromString(String string) { |
| 150 | + try { |
| 151 | + byte[] complete = Bytes.fromRawHexString(string, 0); |
| 152 | + return new PagingState(complete); |
| 153 | + } catch (Exception e) { |
| 154 | + throw new PagingStateException("Cannot deserialize paging state, invalid format. " |
| 155 | + + "The serialized form was corrupted, or not initially generated from a PagingState object.", e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Return a representation of the paging state object as a byte array. |
| 161 | + * |
| 162 | + * @return the paging state as a byte array. |
| 163 | + */ |
| 164 | + public byte[] toBytes() { |
| 165 | + return generateCompleteOutput().array(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Create a PagingState object from a byte array previously generated with {@link #toBytes()}. |
| 170 | + * |
| 171 | + * @param pagingState The byte array representation. |
| 172 | + * @return the PagingState object created. |
| 173 | + * |
| 174 | + * @throws PagingStateException if the byte array does not have the correct format. |
| 175 | + */ |
| 176 | + public static PagingState fromBytes(byte[] pagingState) { |
| 177 | + return new PagingState(pagingState); |
| 178 | + } |
| 179 | +} |
0 commit comments