Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.cache.h2; 16 : 17 : import com.google.common.hash.Funnel; 18 : import com.google.common.hash.Funnels; 19 : import com.google.common.hash.PrimitiveSink; 20 : import com.google.gerrit.server.cache.serialize.CacheSerializer; 21 : import java.io.IOException; 22 : import java.sql.PreparedStatement; 23 : import java.sql.ResultSet; 24 : import java.sql.SQLException; 25 : 26 : class ObjectKeyTypeImpl<K> implements KeyType<K> { 27 : private final CacheSerializer<K> serializer; 28 : 29 15 : ObjectKeyTypeImpl(CacheSerializer<K> serializer) { 30 15 : this.serializer = serializer; 31 15 : } 32 : 33 : @Override 34 : public String columnType() { 35 15 : return "OTHER"; 36 : } 37 : 38 : @Override 39 : public K get(ResultSet rs, int col) throws IOException, SQLException { 40 15 : return serializer.deserialize(rs.getBytes(col)); 41 : } 42 : 43 : @Override 44 : public void set(PreparedStatement ps, int col, K key) throws IOException, SQLException { 45 15 : ps.setBytes(col, serializer.serialize(key)); 46 15 : } 47 : 48 : @Override 49 : public Funnel<K> funnel() { 50 15 : return new Funnel<>() { 51 : private static final long serialVersionUID = 1L; 52 : 53 : @Override 54 : public void funnel(K from, PrimitiveSink into) { 55 15 : Funnels.byteArrayFunnel().funnel(serializer.serialize(from), into); 56 15 : } 57 : }; 58 : } 59 : }