From 60d5e4c6c6379515b91e8cba5be311cb5f45269f Mon Sep 17 00:00:00 2001 From: Igor Cher Date: Thu, 13 Dec 2012 11:15:52 +0200 Subject: [PATCH] Fix issue #9 --- src/main/java/hrider/data/ObjectType.java | 8 ++++++-- src/main/java/hrider/hbase/Operator.java | 17 ++++++++++++++++- src/main/java/hrider/hbase/Query.java | 7 ++++++- src/main/java/hrider/hbase/QueryScanner.java | 7 ++++++- src/main/java/hrider/ui/forms/ScanDialog.java | 16 +++++++--------- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/main/java/hrider/data/ObjectType.java b/src/main/java/hrider/data/ObjectType.java index a7a2c16..1b6f8b9 100644 --- a/src/main/java/hrider/data/ObjectType.java +++ b/src/main/java/hrider/data/ObjectType.java @@ -43,6 +43,10 @@ public enum ObjectType { Xml, Json; + //region Constants + private static final byte[] EMPTY_BYTES_ARRAY = new byte[0]; + //endregion + //region Public Methods /** @@ -108,7 +112,7 @@ public Object toObject(String value) { */ public byte[] fromString(String value) { if (value == null) { - return null; + return EMPTY_BYTES_ARRAY; } switch (this) { @@ -186,7 +190,7 @@ public Object fromByteArray(byte[] value) { */ public byte[] fromObject(Object value) { if (value == null) { - return null; + return EMPTY_BYTES_ARRAY; } switch (this) { diff --git a/src/main/java/hrider/hbase/Operator.java b/src/main/java/hrider/hbase/Operator.java index 5287899..4fe8d8e 100644 --- a/src/main/java/hrider/hbase/Operator.java +++ b/src/main/java/hrider/hbase/Operator.java @@ -31,10 +31,21 @@ public enum Operator { Equal, NotEqual, GreaterOrEqual, - Greater; + Greater, + IsNull, + IsNotNull; //region Public Methods + /** + * Indicates if the operator is an unary operator. + * + * @return True if the operator represents an unary operator or False otherwise. + */ + public boolean isUnary() { + return this == IsNull || this == IsNotNull; + } + /** * Gets a filter according to the operator type. * @return A filter to be used in query on hbase. @@ -57,6 +68,10 @@ public CompareFilter.CompareOp toFilter() { return CompareFilter.CompareOp.GREATER_OR_EQUAL; case Greater: return CompareFilter.CompareOp.GREATER; + case IsNull: + return CompareFilter.CompareOp.EQUAL; + case IsNotNull: + return CompareFilter.CompareOp.NOT_EQUAL; default: throw new IllegalArgumentException(String.format("The specified operator type '%s' is not supported.", this)); } diff --git a/src/main/java/hrider/hbase/Query.java b/src/main/java/hrider/hbase/Query.java index dab27aa..f7562a2 100644 --- a/src/main/java/hrider/hbase/Query.java +++ b/src/main/java/hrider/hbase/Query.java @@ -324,7 +324,12 @@ public byte[] getWordAsByteArray() { * @param value A new value to set. */ public void setWord(String value) { - this.word = value; + if (value != null && !value.isEmpty()) { + this.word = value; + } + else { + this.word = null; + } } /** diff --git a/src/main/java/hrider/hbase/QueryScanner.java b/src/main/java/hrider/hbase/QueryScanner.java index 94cb536..d03c740 100644 --- a/src/main/java/hrider/hbase/QueryScanner.java +++ b/src/main/java/hrider/hbase/QueryScanner.java @@ -35,6 +35,7 @@ public class QueryScanner extends Scanner { * A constant representing a size of the unicode character. */ private static final int UNICODE_CHAR_SIZE = 4; + private static final byte[] EMPTY_BYTES_ARRAY = new byte[0]; //endregion //region Variables @@ -110,7 +111,7 @@ protected Scan getScanner() throws IOException { scan.setTimeRange(this.query.getStartDate().getTime(), this.query.getEndDate().getTime()); } - if (this.query.getWord() != null) { + if (this.query.getWord() != null || this.query.getOperator().isUnary()) { WritableByteArrayComparable comparator; switch (this.query.getOperator()) { @@ -131,6 +132,10 @@ protected Scan getScanner() throws IOException { case Greater: comparator = new BinaryComparator(this.query.getWordAsByteArray()); break; + case IsNull: + case IsNotNull: + comparator = new BinaryComparator(EMPTY_BYTES_ARRAY); + break; default: throw new IllegalArgumentException(String.format("The specified operator type '%s' is not supported.", this.query.getOperator())); } diff --git a/src/main/java/hrider/ui/forms/ScanDialog.java b/src/main/java/hrider/ui/forms/ScanDialog.java index 8c443f5..1cb2fbf 100644 --- a/src/main/java/hrider/ui/forms/ScanDialog.java +++ b/src/main/java/hrider/ui/forms/ScanDialog.java @@ -142,17 +142,15 @@ public Query getQuery() { query.setEndDate(this.endTimeDatePicker.getDate()); } - if (!this.textFieldWord.getText().trim().isEmpty()) { - String column = (String)this.comboBoxColumns.getSelectedItem(); + String column = (String)this.comboBoxColumns.getSelectedItem(); - String[] parts = column.split(":"); - query.setFamily(parts[0]); - query.setColumn(parts[1]); + String[] parts = column.split(":"); + query.setFamily(parts[0]); + query.setColumn(parts[1]); - query.setOperator((Operator)this.comboBoxOperator.getSelectedItem()); - query.setWord(this.textFieldWord.getText().trim()); - query.setWordType((ObjectType)this.comboBoxWordType.getSelectedItem()); - } + query.setOperator((Operator)this.comboBoxOperator.getSelectedItem()); + query.setWord(this.textFieldWord.getText().trim()); + query.setWordType((ObjectType)this.comboBoxWordType.getSelectedItem()); return query; }