GORA-478 Address Sonar issues within gora-accumulo Encoder classes
Project: http://git-wip-us.apache.org/repos/asf/gora/repo Commit: http://git-wip-us.apache.org/repos/asf/gora/commit/fc00bb5b Tree: http://git-wip-us.apache.org/repos/asf/gora/tree/fc00bb5b Diff: http://git-wip-us.apache.org/repos/asf/gora/diff/fc00bb5b Branch: refs/heads/master Commit: fc00bb5b9b5223a43d0246d4fd478d90603e5f08 Parents: c0c8c61 Author: Lewis John McGibbney <lewis.mcgibb...@gmail.com> Authored: Thu May 26 00:02:41 2016 -0700 Committer: Lewis John McGibbney <lewis.mcgibb...@gmail.com> Committed: Thu May 26 00:02:41 2016 -0700 ---------------------------------------------------------------------- .../gora/accumulo/encoders/BinaryEncoder.java | 248 ++++++------------- .../apache/gora/accumulo/encoders/Encoder.java | 38 +-- .../gora/accumulo/encoders/HexEncoder.java | 6 +- .../accumulo/encoders/SignedBinaryEncoder.java | 66 ++--- .../apache/gora/accumulo/encoders/Utils.java | 6 +- .../gora/accumulo/store/AccumuloStore.java | 30 +-- .../gora/accumulo/store/PartitionTest.java | 6 +- .../accumulo/util/SignedBinaryEncoderTest.java | 15 +- .../org/apache/gora/memory/store/MemStore.java | 32 ++- .../apache/gora/memory/store/MemStoreTest.java | 39 ++- 10 files changed, 222 insertions(+), 264 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/BinaryEncoder.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/BinaryEncoder.java b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/BinaryEncoder.java index 7ce93d9..00aa5cf 100644 --- a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/BinaryEncoder.java +++ b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/BinaryEncoder.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. @@ -27,261 +27,163 @@ import org.apache.gora.accumulo.util.FixedByteArrayOutputStream; * */ public class BinaryEncoder implements Encoder { - public byte[] encodeShort(short s) { + + @Override + public byte[] encodeShort(short s) throws IOException { return encodeShort(s, new byte[2]); } - - public byte[] encodeShort(short s, byte ret[]) { - DataOutputStream dos = null; - try { - dos = new DataOutputStream(new FixedByteArrayOutputStream(ret)); + + @Override + public byte[] encodeShort(short s, byte[] ret) throws IOException { + try (DataOutputStream dos = new DataOutputStream(new FixedByteArrayOutputStream(ret))){ dos.writeShort(s); dos.close(); return ret; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dos.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public short decodeShort(byte[] a) { - DataInputStream dis = null; - try { - dis = new DataInputStream(new ByteArrayInputStream(a)); + + @Override + public short decodeShort(byte[] a) throws IOException { + try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a))){ short s = dis.readShort(); dis.close(); return s; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dis.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public byte[] encodeInt(int i) { + + @Override + public byte[] encodeInt(int i) throws IOException { return encodeInt(i, new byte[4]); } - - public byte[] encodeInt(int i, byte ret[]) { - DataOutputStream dos = null; - try { - dos = new DataOutputStream(new FixedByteArrayOutputStream(ret)); + + @Override + public byte[] encodeInt(int i, byte[] ret) throws IOException { + try (DataOutputStream dos = new DataOutputStream(new FixedByteArrayOutputStream(ret))){ dos.writeInt(i); dos.close(); return ret; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dos.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public int decodeInt(byte[] a) { - DataInputStream dis = null; - try { - dis = new DataInputStream(new ByteArrayInputStream(a)); + + @Override + public int decodeInt(byte[] a) throws IOException { + try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a))){ int i = dis.readInt(); dis.close(); return i; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dis.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public byte[] encodeLong(long l) { + + @Override + public byte[] encodeLong(long l) throws IOException { return encodeLong(l, new byte[8]); } - - public byte[] encodeLong(long l, byte ret[]) { - DataOutputStream dos = null; - try { - dos = new DataOutputStream(new FixedByteArrayOutputStream(ret)); + + @Override + public byte[] encodeLong(long l, byte[] ret) throws IOException { + try (DataOutputStream dos = new DataOutputStream(new FixedByteArrayOutputStream(ret))){ dos.writeLong(l); dos.close(); return ret; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dos.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public long decodeLong(byte[] a) { - DataInputStream dis = null; - try { - dis = new DataInputStream(new ByteArrayInputStream(a)); + + @Override + public long decodeLong(byte[] a) throws IOException { + try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a))){ long l = dis.readLong(); dis.close(); return l; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dis.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public byte[] encodeDouble(double d) { + + @Override + public byte[] encodeDouble(double d) throws IOException { return encodeDouble(d, new byte[8]); } - - public byte[] encodeDouble(double d, byte[] ret) { - DataOutputStream dos = null; - try { + + @Override + public byte[] encodeDouble(double d, byte[] ret) throws IOException { + try (DataOutputStream dos = new DataOutputStream(new FixedByteArrayOutputStream(ret))){ long l = Double.doubleToRawLongBits(d); - dos = new DataOutputStream(new FixedByteArrayOutputStream(ret)); dos.writeLong(l); dos.close(); return ret; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dos.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public double decodeDouble(byte[] a) { - DataInputStream dis = null; - try { - dis = new DataInputStream(new ByteArrayInputStream(a)); + + @Override + public double decodeDouble(byte[] a) throws IOException { + try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a))){ long l = dis.readLong(); dis.close(); return Double.longBitsToDouble(l); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dis.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public byte[] encodeFloat(float d) { + + @Override + public byte[] encodeFloat(float d) throws IOException { return encodeFloat(d, new byte[4]); } - - public byte[] encodeFloat(float f, byte[] ret) { - DataOutputStream dos = null; - try { + + @Override + public byte[] encodeFloat(float f, byte[] ret) throws IOException { + try (DataOutputStream dos = new DataOutputStream(new FixedByteArrayOutputStream(ret))){ int i = Float.floatToRawIntBits(f); - dos = new DataOutputStream(new FixedByteArrayOutputStream(ret)); dos.writeInt(i); return ret; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dos.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public float decodeFloat(byte[] a) { - DataInputStream dis = null; - try { - dis = new DataInputStream(new ByteArrayInputStream(a)); + + @Override + public float decodeFloat(byte[] a) throws IOException { + try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a))){ int i = dis.readInt(); return Float.intBitsToFloat(i); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dis.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - + + @Override public byte[] encodeByte(byte b, byte[] ret) { ret[0] = 0; return ret; } - + + @Override public byte[] encodeByte(byte b) { return encodeByte(b, new byte[1]); } - + + @Override public byte decodeByte(byte[] a) { return a[0]; } - - public boolean decodeBoolean(byte[] a) { - DataInputStream dis = null; - try { - dis = new DataInputStream(new ByteArrayInputStream(a)); + + @Override + public boolean decodeBoolean(byte[] a) throws IOException { + try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a))){ return dis.readBoolean(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dis.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - - public byte[] encodeBoolean(boolean b) { + + @Override + public byte[] encodeBoolean(boolean b) throws IOException { return encodeBoolean(b, new byte[1]); } - - public byte[] encodeBoolean(boolean b, byte[] ret) { - DataOutputStream dos = null; - try { - dos = new DataOutputStream(new FixedByteArrayOutputStream(ret)); + + @Override + public byte[] encodeBoolean(boolean b, byte[] ret) throws IOException { + try (DataOutputStream dos = new DataOutputStream(new FixedByteArrayOutputStream(ret))){ dos.writeBoolean(b); return ret; - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } finally { - try { - dos.close(); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } } } - + @Override public byte[] lastPossibleKey(int size, byte[] er) { return Utils.lastPossibleKey(size, er); } - + @Override public byte[] followingKey(int size, byte[] per) { return Utils.followingKey(size, per); http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Encoder.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Encoder.java b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Encoder.java index 7f79e8a..550639d 100644 --- a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Encoder.java +++ b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Encoder.java @@ -16,6 +16,8 @@ */ package org.apache.gora.accumulo.encoders; +import java.io.IOException; + /** * */ @@ -27,41 +29,41 @@ public interface Encoder { public byte decodeByte(byte[] a); - public byte[] encodeShort(short s); + public byte[] encodeShort(short s) throws IOException; - public byte[] encodeShort(short s, byte ret[]); + public byte[] encodeShort(short s, byte[] ret) throws IOException; - public short decodeShort(byte[] a); + public short decodeShort(byte[] a) throws IOException; - public byte[] encodeInt(int i); + public byte[] encodeInt(int i) throws IOException; - public byte[] encodeInt(int i, byte ret[]); + public byte[] encodeInt(int i, byte[] ret) throws IOException; - public int decodeInt(byte[] a); + public int decodeInt(byte[] a) throws IOException; - public byte[] encodeLong(long l); + public byte[] encodeLong(long l) throws IOException; - public byte[] encodeLong(long l, byte ret[]); + public byte[] encodeLong(long l, byte[] ret) throws IOException; - public long decodeLong(byte[] a); + public long decodeLong(byte[] a) throws IOException; - public byte[] encodeDouble(double d); + public byte[] encodeDouble(double d) throws IOException; - public byte[] encodeDouble(double d, byte[] ret); + public byte[] encodeDouble(double d, byte[] ret) throws IOException; - public double decodeDouble(byte[] a); + public double decodeDouble(byte[] a) throws IOException; - public byte[] encodeFloat(float d); + public byte[] encodeFloat(float d) throws IOException; - public byte[] encodeFloat(float f, byte[] ret); + public byte[] encodeFloat(float f, byte[] ret) throws IOException; - public float decodeFloat(byte[] a); + public float decodeFloat(byte[] a) throws IOException; - public boolean decodeBoolean(byte[] val); + public boolean decodeBoolean(byte[] val) throws IOException; - public byte[] encodeBoolean(boolean b); + public byte[] encodeBoolean(boolean b) throws IOException; - public byte[] encodeBoolean(boolean b, byte[] ret); + public byte[] encodeBoolean(boolean b, byte[] ret) throws IOException; byte[] followingKey(int size, byte[] per); http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/HexEncoder.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/HexEncoder.java b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/HexEncoder.java index 6db5295..092b532 100644 --- a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/HexEncoder.java +++ b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/HexEncoder.java @@ -22,7 +22,7 @@ package org.apache.gora.accumulo.encoders; public class HexEncoder implements Encoder { - private byte chars[] = new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + private byte[] chars = new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private void encode(byte[] a, long l) { for (int i = a.length - 1; i >= 0; i--) { @@ -33,9 +33,9 @@ public class HexEncoder implements Encoder { private int fromChar(byte b) { if (b >= '0' && b <= '9') { - return (b - '0'); + return b - '0'; } else if (b >= 'a' && b <= 'f') { - return (b - 'a' + 10); + return b - 'a' + 10; } throw new IllegalArgumentException("Bad char " + b); http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/SignedBinaryEncoder.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/SignedBinaryEncoder.java b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/SignedBinaryEncoder.java index 87ebcd6..e7a4042 100644 --- a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/SignedBinaryEncoder.java +++ b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/SignedBinaryEncoder.java @@ -16,6 +16,7 @@ */ package org.apache.gora.accumulo.encoders; +import java.io.IOException; /** * This class transforms this bits within a primitive type so that @@ -24,71 +25,80 @@ package org.apache.gora.accumulo.encoders; * before positive numbers, when compared lexographically. */ public class SignedBinaryEncoder extends BinaryEncoder { - - public byte[] encodeShort(short s, byte ret[]){ + + @Override + public byte[] encodeShort(short s, byte[] ret) throws IOException{ s = (short)((s & 0xffff) ^ 0x8000); return super.encodeShort(s, ret); } - - public short decodeShort(byte[] a){ + + @Override + public short decodeShort(byte[] a) throws IOException{ short s = super.decodeShort(a); s = (short)((s & 0xffff) ^ 0x8000); return s; } - - public byte[] encodeInt(int i, byte ret[]){ + + @Override + public byte[] encodeInt(int i, byte[] ret) throws IOException{ i = i ^ 0x80000000; return super.encodeInt(i, ret); } - - public int decodeInt(byte[] a){ + + @Override + public int decodeInt(byte[] a) throws IOException{ int i = super.decodeInt(a); i = i ^ 0x80000000; return i; } - - public byte[] encodeLong(long l, byte ret[]){ - l = l ^ 0x8000000000000000l; + + @Override + public byte[] encodeLong(long l, byte[] ret) throws IOException{ + l = l ^ 0x8000000000000000L; return super.encodeLong(l, ret); } - - public long decodeLong(byte[] a) { + + @Override + public long decodeLong(byte[] a) throws IOException { long l = super.decodeLong(a); - l = l ^ 0x8000000000000000l; + l = l ^ 0x8000000000000000L; return l; } - - - public byte[] encodeDouble(double d, byte[] ret) { + + @Override + public byte[] encodeDouble(double d, byte[] ret) throws IOException { long l = Double.doubleToRawLongBits(d); if(l < 0) l = ~l; else - l = l ^ 0x8000000000000000l; + l = l ^ 0x8000000000000000L; return super.encodeLong(l,ret); } - - public double decodeDouble(byte[] a){ + + @Override + public double decodeDouble(byte[] a) throws IOException{ long l = super.decodeLong(a); if(l < 0) - l = l ^ 0x8000000000000000l; + l = l ^ 0x8000000000000000L; else l = ~l; return Double.longBitsToDouble(l); } - - public byte[] encodeFloat(float f, byte[] ret) { + + @Override + public byte[] encodeFloat(float f, byte[] ret) throws IOException { int i = Float.floatToRawIntBits(f); if(i < 0) i = ~i; else i = i ^ 0x80000000; - + return super.encodeInt(i, ret); - + } - - public float decodeFloat(byte[] a){ + + @Override + public float decodeFloat(byte[] a) throws IOException{ int i = super.decodeInt(a); if(i < 0) i = i ^ 0x80000000; @@ -96,5 +106,5 @@ public class SignedBinaryEncoder extends BinaryEncoder { i = ~i; return Float.intBitsToFloat(i); } - + } http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Utils.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Utils.java b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Utils.java index 5fa7de0..99c8f21 100644 --- a/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Utils.java +++ b/gora-accumulo/src/main/java/org/apache/gora/accumulo/encoders/Utils.java @@ -43,7 +43,7 @@ public class Utils { bi = bi.subtract(BigInteger.ONE); - byte ret[] = new byte[size]; + byte[] ret = new byte[size]; Arrays.fill(ret, (byte) 0xff); System.arraycopy(getBytes(bi, er.length), 0, ret, 0, er.length); @@ -56,13 +56,13 @@ public class Utils { if (ret[0] == 0) { // remove leading 0 that makes num positive - byte copy[] = new byte[ret.length - 1]; + byte[] copy = new byte[ret.length - 1]; System.arraycopy(ret, 1, copy, 0, copy.length); ret = copy; } // leading digits are dropped - byte copy[] = new byte[minLen]; + byte[] copy = new byte[minLen]; if (bi.compareTo(BigInteger.ZERO) < 0) { Arrays.fill(copy, (byte) 0xff); } http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/main/java/org/apache/gora/accumulo/store/AccumuloStore.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/main/java/org/apache/gora/accumulo/store/AccumuloStore.java b/gora-accumulo/src/main/java/org/apache/gora/accumulo/store/AccumuloStore.java index e6b6e65..2a8af02 100644 --- a/gora-accumulo/src/main/java/org/apache/gora/accumulo/store/AccumuloStore.java +++ b/gora-accumulo/src/main/java/org/apache/gora/accumulo/store/AccumuloStore.java @@ -116,6 +116,8 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T protected static final String PASSWORD_PROPERTY = "accumulo.password"; protected static final String DEFAULT_MAPPING_FILE = "gora-accumulo-mapping.xml"; + private final static String UNKOWN = "Unknown type "; + private Connector conn; private BatchWriter batchWriter; private AccumuloMapping mapping; @@ -124,7 +126,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T public static final Logger LOG = LoggerFactory.getLogger(AccumuloStore.class); - public Object fromBytes(Schema schema, byte data[]) throws GoraException { + public Object fromBytes(Schema schema, byte[] data) throws IOException { Schema fromSchema = null; if (schema.getType() == Type.UNION) { try { @@ -149,7 +151,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T return fromBytes(encoder, fromSchema, data); } - public static Object fromBytes(Encoder encoder, Schema schema, byte data[]) { + public static Object fromBytes(Encoder encoder, Schema schema, byte data[]) throws IOException { switch (schema.getType()) { case BOOLEAN: return encoder.decodeBoolean(data); @@ -182,7 +184,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T default: break; } - throw new IllegalArgumentException("Unknown type " + schema.getType()); + throw new IllegalArgumentException(UNKOWN + schema.getType()); } @@ -222,7 +224,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T return (K) new Utf8(val); } - throw new IllegalArgumentException("Unknown type " + clazz.getName()); + throw new IllegalArgumentException(UNKOWN + clazz.getName()); } catch (IOException ioe) { LOG.error(ioe.getMessage()); throw new RuntimeException(ioe); @@ -231,7 +233,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T private static byte[] copyIfNeeded(byte b[], int offset, int len) { if (len != b.length || offset != 0) { - byte copy[] = new byte[len]; + byte[] copy = new byte[len]; System.arraycopy(b, offset, copy, 0, copy.length); b = copy; } @@ -322,7 +324,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T throw new RuntimeException(ioe); } - throw new IllegalArgumentException("Uknown type " + o.getClass().getName()); + throw new IllegalArgumentException(UNKOWN + o.getClass().getName()); } private BatchWriter getBatchWriter() throws IOException { @@ -330,7 +332,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T try { BatchWriterConfig batchWriterConfig = new BatchWriterConfig(); batchWriterConfig.setMaxMemory(10000000); - batchWriterConfig.setMaxLatency(60000l, TimeUnit.MILLISECONDS); + batchWriterConfig.setMaxLatency(60000L, TimeUnit.MILLISECONDS); batchWriterConfig.setMaxWriteThreads(4); batchWriter = conn.createBatchWriter(mapping.tableName, batchWriterConfig); } catch (TableNotFoundException e) { @@ -351,7 +353,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T mapping = readMapping(mappingFile); - if (mapping.encoder == null || mapping.encoder.equals("")) { + if (mapping.encoder == null || "".equals(mapping.encoder)) { encoder = new BinaryEncoder(); } else { try { @@ -409,7 +411,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T String name = fieldElement.getAttribute("name"); String family = fieldElement.getAttribute("family"); String qualifier = fieldElement.getAttribute("qualifier"); - if (qualifier.equals("")) + if ("".equals(qualifier)) qualifier = null; Pair<Text,Text> col = new Pair<>(new Text(family), qualifier == null ? null : new Text(qualifier)); @@ -603,7 +605,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T return fieldName; } - private void setFetchColumns(Scanner scanner, String fields[]) { + private void setFetchColumns(Scanner scanner, String[] fields) { fields = getFieldsToQuery(fields); for (String field : fields) { Pair<Text,Text> col = mapping.fieldMap.get(field); @@ -614,7 +616,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T scanner.fetchColumn(col.getFirst(), col.getSecond()); } } else { - LOG.error("Mapping not found for field: " + field); + LOG.error("Mapping not found for field: {}", field); } } } @@ -733,7 +735,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T Object mapKey = ((Entry<?, ?>) entry).getKey(); Object mapVal = ((Entry<?, ?>) entry).getValue(); if ((o instanceof DirtyMapWrapper && ((DirtyMapWrapper<?, ?>)o).isDirty()) - || !(o instanceof DirtyMapWrapper)) { //mapVal instanceof Dirtyable && ((Dirtyable)mapVal).isDirty()) { + || !(o instanceof DirtyMapWrapper)) { m.put(col.getFirst(), new Text(toBytes(mapKey)), new Value(toBytes(valueType, mapVal))); count++; } @@ -971,7 +973,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T return fromBytes(encoder, clazz, er); } - throw new IllegalArgumentException("Unknown type " + clazz.getName()); + throw new IllegalArgumentException(UNKOWN + clazz.getName()); } @@ -1004,7 +1006,7 @@ public class AccumuloStore<K,T extends PersistentBase> extends DataStoreBase<K,T return fromBytes(encoder, clazz, Arrays.copyOf(per, per.length + 1)); } - throw new IllegalArgumentException("Unknown type " + clazz.getName()); + throw new IllegalArgumentException(UNKOWN + clazz.getName()); } @Override http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/test/java/org/apache/gora/accumulo/store/PartitionTest.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/test/java/org/apache/gora/accumulo/store/PartitionTest.java b/gora-accumulo/src/test/java/org/apache/gora/accumulo/store/PartitionTest.java index b1ffd67..d201e51 100644 --- a/gora-accumulo/src/test/java/org/apache/gora/accumulo/store/PartitionTest.java +++ b/gora-accumulo/src/test/java/org/apache/gora/accumulo/store/PartitionTest.java @@ -34,7 +34,7 @@ public class PartitionTest { private static Encoder encoder = new SignedBinaryEncoder(); - static long encl(long l) { + static long encl(long l) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { @@ -47,7 +47,7 @@ public class PartitionTest { } @Test - public void test1() { + public void test1() throws IOException { assertEquals(encl(0x006f000000000000l), (long) AccumuloStore.followingKey(encoder, Long.class, new byte[] {0x00, 0x6f})); assertEquals(encl(1l), (long) AccumuloStore.followingKey(encoder, Long.class, new byte[] {0, 0, 0, 0, 0, 0, 0, 0})); assertEquals(encl(0x106f000000000001l), (long) AccumuloStore.followingKey(encoder, Long.class, new byte[] {0x10, 0x6f, 0, 0, 0, 0, 0, 0})); @@ -75,7 +75,7 @@ public class PartitionTest { } @Test - public void test2() { + public void test2() throws IOException { assertEquals(encl(0x00ffffffffffffffl), (long) AccumuloStore.lastPossibleKey(encoder, Long.class, new byte[] {0x01})); assertEquals(encl(0x006effffffffffffl), (long) AccumuloStore.lastPossibleKey(encoder, Long.class, new byte[] {0x00, 0x6f})); assertEquals(encl(0xff6effffffffffffl), (long) AccumuloStore.lastPossibleKey(encoder, Long.class, new byte[] {(byte) 0xff, 0x6f})); http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-accumulo/src/test/java/org/apache/gora/accumulo/util/SignedBinaryEncoderTest.java ---------------------------------------------------------------------- diff --git a/gora-accumulo/src/test/java/org/apache/gora/accumulo/util/SignedBinaryEncoderTest.java b/gora-accumulo/src/test/java/org/apache/gora/accumulo/util/SignedBinaryEncoderTest.java index 0ae2c47..d6d3611 100644 --- a/gora-accumulo/src/test/java/org/apache/gora/accumulo/util/SignedBinaryEncoderTest.java +++ b/gora-accumulo/src/test/java/org/apache/gora/accumulo/util/SignedBinaryEncoderTest.java @@ -16,6 +16,7 @@ */ package org.apache.gora.accumulo.util; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -30,7 +31,7 @@ import static org.junit.Assert.assertTrue; */ public class SignedBinaryEncoderTest { @Test - public void testShort() { + public void testShort() throws IOException { short s = Short.MIN_VALUE; Text prev = null; @@ -49,7 +50,7 @@ public class SignedBinaryEncoderTest { } } - private void testInt(int start, int finish) { + private void testInt(int start, int finish) throws IOException { int i = start; Text prev = null; @@ -69,13 +70,13 @@ public class SignedBinaryEncoderTest { } @Test - public void testInt() { + public void testInt() throws IOException { testInt(Integer.MIN_VALUE, Integer.MIN_VALUE + (1 << 16)); testInt(-(1 << 15), (1 << 15)); testInt(Integer.MAX_VALUE - (1 << 16), Integer.MAX_VALUE); } - private void testLong(long start, long finish) { + private void testLong(long start, long finish) throws IOException { long l = start; Text prev = null; @@ -95,14 +96,14 @@ public class SignedBinaryEncoderTest { } @Test - public void testLong() { + public void testLong() throws IOException { testLong(Long.MIN_VALUE, Long.MIN_VALUE + (1 << 16)); testLong(-(1 << 15), (1 << 15)); testLong(Long.MAX_VALUE - (1 << 16), Long.MAX_VALUE); } @Test - public void testDouble() { + public void testDouble() throws IOException { ArrayList<Double> testData = new ArrayList<>(); testData.add(Double.NEGATIVE_INFINITY); @@ -133,7 +134,7 @@ public class SignedBinaryEncoderTest { } @Test - public void testFloat() { + public void testFloat() throws IOException { ArrayList<Float> testData = new ArrayList<>(); testData.add(Float.NEGATIVE_INFINITY); http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java ---------------------------------------------------------------------- diff --git a/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java b/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java index 81dc3b9..b67c7c1 100644 --- a/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java +++ b/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java @@ -106,13 +106,31 @@ public class MemStore<K, T extends PersistentBase> extends DataStoreBase<K, T> { @Override public long deleteByQuery(Query<K, T> query) { - try{ + try { long deletedRows = 0; - Result<K,T> result = query.execute(); + Result<K, T> result = query.execute(); + + String[] fields = getFieldsToQuery(query.getFields()); + boolean isAllFields = Arrays.equals(fields, getFields()); - while(result.next()) { - if(delete(result.getKey())) - deletedRows++; + while (result.next()) { + if (isAllFields) { + if (delete(result.getKey())) { + deletedRows++; + } + } else { + ArrayList<String> excludedFields = new ArrayList<>(); + for (String field : getFields()) { + if (!Arrays.asList(fields).contains(field)) { + excludedFields.add(field); + } + } + T newClonedObj = getPersistent(result.get(),excludedFields.toArray(new String[excludedFields.size()])); + if (delete(result.getKey())) { + put(result.getKey(),newClonedObj); + deletedRows++; + } + } } return deletedRows; } catch (Exception e) { @@ -179,7 +197,9 @@ public class MemStore<K, T extends PersistentBase> extends DataStoreBase<K, T> { return obj; } T newObj = AvroUtils.deepClonePersistent(obj); - for (Field otherField : otherFields) { + newObj.clear(); + for (String field : fields) { + Field otherField = obj.getSchema().getField(field); int index = otherField.pos(); newObj.put(index, obj.get(index)); } http://git-wip-us.apache.org/repos/asf/gora/blob/fc00bb5b/gora-core/src/test/java/org/apache/gora/memory/store/MemStoreTest.java ---------------------------------------------------------------------- diff --git a/gora-core/src/test/java/org/apache/gora/memory/store/MemStoreTest.java b/gora-core/src/test/java/org/apache/gora/memory/store/MemStoreTest.java index 87eea71..8e94662 100644 --- a/gora-core/src/test/java/org/apache/gora/memory/store/MemStoreTest.java +++ b/gora-core/src/test/java/org/apache/gora/memory/store/MemStoreTest.java @@ -22,6 +22,8 @@ import java.io.IOException; import org.apache.gora.examples.WebPageDataCreator; import org.apache.gora.examples.generated.Employee; import org.apache.gora.examples.generated.WebPage; +import org.apache.gora.persistency.BeanFactory; +import org.apache.gora.persistency.impl.BeanFactoryImpl; import org.apache.gora.query.Query; import org.apache.gora.store.DataStore; import org.apache.gora.store.DataStoreFactory; @@ -36,6 +38,7 @@ import org.slf4j.LoggerFactory; import static org.apache.gora.examples.WebPageDataCreator.SORTED_URLS; import static org.apache.gora.examples.WebPageDataCreator.URLS; +import static org.apache.gora.examples.WebPageDataCreator.URL_INDEXES; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -50,7 +53,7 @@ import static org.junit.Assume.assumeTrue; public class MemStoreTest extends DataStoreTestBase { private static final Logger LOG = LoggerFactory.getLogger(MemStoreTest.class); - + private static final int NUM_KEYS = 4; private Configuration conf; @@ -106,15 +109,33 @@ public class MemStoreTest extends DataStoreTestBase { @Test public void testDeleteByQueryFields() {} - @Ignore("GORA-447") @Test - public void testGetWithFields() {} + public void testGetWithFields() throws Exception { + + DataStore<String, WebPage> store = new MemStore<>(); + BeanFactory<String, WebPage> beanFactory = new BeanFactoryImpl<>(String.class, WebPage.class); + store.setBeanFactory(beanFactory); + WebPageDataCreator.createWebPageData(store); + String[] interestFields = new String[2]; + interestFields[0] = "url"; + interestFields[1] = "content"; + WebPage page = store.get(URLS[1], interestFields); + assertNotNull(page); + assertNotNull(page.getUrl()); + assertEquals(page.getUrl().toString(), URLS[1]); + assertNotNull(page.getContent()); + assertEquals("Map of Outlinks should have a size of '0' as it is omitted at retrieval", + 0, page.getOutlinks().size()); + assertEquals("Map of Parsed Content should have a size of '0' as it is omitted at retrieval", + 0, page.getParsedContent().size()); + } - @Ignore("GORA-447") @Test public void testMemStoreDeleteByQueryFields() throws Exception { DataStore<String, WebPage> store = new MemStore<>(); + BeanFactory<String, WebPage> beanFactory = new BeanFactoryImpl<>(String.class, WebPage.class); + store.setBeanFactory(beanFactory); Query<String, WebPage> query; //test 5 - delete all with some fields @@ -122,11 +143,11 @@ public class MemStoreTest extends DataStoreTestBase { query = store.newQuery(); query.setFields("outlinks", "parsedContent", "content"); - + Query<String, WebPage> newQuery = store.newQuery(); - newQuery.setStartKey(URLS[0]); - newQuery.setEndKey(URLS[9]); - //newQuery.setFields("outlinks", "parsedContent", "content"); + newQuery.setStartKey(SORTED_URLS[0]); + newQuery.setEndKey(SORTED_URLS[9]); + newQuery.setFields("outlinks", "parsedContent", "content"); DataStoreTestUtil.assertNumResults(newQuery, URLS.length); store.deleteByQuery(query); @@ -178,7 +199,7 @@ public class MemStoreTest extends DataStoreTestBase { for (int i = 0; i < URLS.length; i++) { WebPage page = store.get(URLS[i]); assertNotNull(page); - if( URLS[i].compareTo(startKey) < 0 || URLS[i].compareTo(endKey) >= 0) { + if( URLS[i].compareTo(startKey) < 0 || URLS[i].compareTo(endKey) > 0) { //not deleted DataStoreTestUtil.assertWebPage(page, i); } else {