Factor out KeyType interface from H2CacheImpl
* Move type to new file. * Extract an interface and an implementation KeyTypeImpl. Change-Id: I52c7994509bba79ec61d4bce9ecfceef81439bb3
This commit is contained in:
@@ -22,16 +22,10 @@ import com.google.common.cache.CacheStats;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.hash.BloomFilter;
|
||||
import com.google.common.hash.Funnel;
|
||||
import com.google.common.hash.Funnels;
|
||||
import com.google.common.hash.PrimitiveSink;
|
||||
import com.google.gerrit.common.TimeUtil;
|
||||
import com.google.gerrit.server.cache.PersistentCache;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import java.io.IOException;
|
||||
import java.io.InvalidClassException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@@ -254,71 +248,6 @@ public class H2CacheImpl<K, V> extends AbstractLoadingCache<K, V> implements Per
|
||||
}
|
||||
}
|
||||
|
||||
private static class KeyType<K> {
|
||||
String columnType() {
|
||||
return "OTHER";
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
K get(ResultSet rs, int col) throws SQLException {
|
||||
return (K) rs.getObject(col);
|
||||
}
|
||||
|
||||
void set(PreparedStatement ps, int col, K value) throws SQLException {
|
||||
ps.setObject(col, value, Types.JAVA_OBJECT);
|
||||
}
|
||||
|
||||
Funnel<K> funnel() {
|
||||
return new Funnel<K>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public void funnel(K from, PrimitiveSink into) {
|
||||
try (ObjectOutputStream ser = new ObjectOutputStream(new SinkOutputStream(into))) {
|
||||
ser.writeObject(from);
|
||||
ser.flush();
|
||||
} catch (IOException err) {
|
||||
throw new RuntimeException("Cannot hash as Serializable", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <K> KeyType<K> create(TypeLiteral<K> type) {
|
||||
if (type.getRawType() == String.class) {
|
||||
return (KeyType<K>) STRING;
|
||||
}
|
||||
return (KeyType<K>) OTHER;
|
||||
}
|
||||
|
||||
static final KeyType<?> OTHER = new KeyType<>();
|
||||
static final KeyType<String> STRING =
|
||||
new KeyType<String>() {
|
||||
@Override
|
||||
String columnType() {
|
||||
return "VARCHAR(4096)";
|
||||
}
|
||||
|
||||
@Override
|
||||
String get(ResultSet rs, int col) throws SQLException {
|
||||
return rs.getString(col);
|
||||
}
|
||||
|
||||
@Override
|
||||
void set(PreparedStatement ps, int col, String value) throws SQLException {
|
||||
ps.setString(col, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
Funnel<String> funnel() {
|
||||
Funnel<?> s = Funnels.unencodedCharsFunnel();
|
||||
return (Funnel<String>) s;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static class SqlStore<K, V> {
|
||||
private final String url;
|
||||
private final KeyType<K> keyType;
|
||||
@@ -332,7 +261,7 @@ public class H2CacheImpl<K, V> extends AbstractLoadingCache<K, V> implements Per
|
||||
|
||||
SqlStore(String jdbcUrl, TypeLiteral<K> keyType, long maxSize, long expireAfterWrite) {
|
||||
this.url = jdbcUrl;
|
||||
this.keyType = KeyType.create(keyType);
|
||||
this.keyType = KeyTypeImpl.create(keyType);
|
||||
this.maxSize = maxSize;
|
||||
this.expireAfterWrite = expireAfterWrite;
|
||||
|
||||
@@ -694,22 +623,4 @@ public class H2CacheImpl<K, V> extends AbstractLoadingCache<K, V> implements Per
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SinkOutputStream extends OutputStream {
|
||||
private final PrimitiveSink sink;
|
||||
|
||||
SinkOutputStream(PrimitiveSink sink) {
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
sink.putByte((byte) b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int p, int n) {
|
||||
sink.putBytes(b, p, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
java/com/google/gerrit/server/cache/h2/KeyType.java
vendored
Normal file
30
java/com/google/gerrit/server/cache/h2/KeyType.java
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2018 The Android Open Source Project
|
||||
//
|
||||
// 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.google.gerrit.server.cache.h2;
|
||||
|
||||
import com.google.common.hash.Funnel;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
interface KeyType<K> {
|
||||
String columnType();
|
||||
|
||||
K get(ResultSet rs, int col) throws SQLException;
|
||||
|
||||
void set(PreparedStatement ps, int col, K key) throws SQLException;
|
||||
|
||||
Funnel<K> funnel();
|
||||
}
|
||||
114
java/com/google/gerrit/server/cache/h2/KeyTypeImpl.java
vendored
Normal file
114
java/com/google/gerrit/server/cache/h2/KeyTypeImpl.java
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
// Copyright (C) 2018 The Android Open Source Project
|
||||
//
|
||||
// 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.google.gerrit.server.cache.h2;
|
||||
|
||||
import com.google.common.hash.Funnel;
|
||||
import com.google.common.hash.Funnels;
|
||||
import com.google.common.hash.PrimitiveSink;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
class KeyTypeImpl<K> implements KeyType<K> {
|
||||
private static final KeyType<?> OTHER = new KeyTypeImpl<>();
|
||||
private static final KeyType<String> STRING =
|
||||
new KeyTypeImpl<String>() {
|
||||
@Override
|
||||
public String columnType() {
|
||||
return "VARCHAR(4096)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(ResultSet rs, int col) throws SQLException {
|
||||
return rs.getString(col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(PreparedStatement ps, int col, String value) throws SQLException {
|
||||
ps.setString(col, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Funnel<String> funnel() {
|
||||
Funnel<?> s = Funnels.unencodedCharsFunnel();
|
||||
return (Funnel<String>) s;
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <K> KeyType<K> create(TypeLiteral<K> type) {
|
||||
if (type.getRawType() == String.class) {
|
||||
return (KeyType<K>) STRING;
|
||||
}
|
||||
return (KeyType<K>) OTHER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String columnType() {
|
||||
return "OTHER";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public K get(ResultSet rs, int col) throws SQLException {
|
||||
return (K) rs.getObject(col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(PreparedStatement ps, int col, K key) throws SQLException {
|
||||
ps.setObject(col, key, Types.JAVA_OBJECT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Funnel<K> funnel() {
|
||||
return new Funnel<K>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public void funnel(K from, PrimitiveSink into) {
|
||||
try (ObjectOutputStream ser = new ObjectOutputStream(new SinkOutputStream(into))) {
|
||||
ser.writeObject(from);
|
||||
ser.flush();
|
||||
} catch (IOException err) {
|
||||
throw new RuntimeException("Cannot hash as Serializable", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static class SinkOutputStream extends OutputStream {
|
||||
private final PrimitiveSink sink;
|
||||
|
||||
SinkOutputStream(PrimitiveSink sink) {
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
sink.putByte((byte) b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int p, int n) {
|
||||
sink.putBytes(b, p, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user