Alachisoft.NCache.Common.DataReader.RecordRow Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
package Alachisoft.NCache.Common.DataReader;
import Alachisoft.NCache.Common.DataStructures.ColumnType;
import Alachisoft.NCache.Common.Queries.AverageResult;
import Alachisoft.NCache.Common.Queries.Order;
import Alachisoft.NCache.Common.Queries.OrderByArgument;
import com.alachisoft.ncache.serialization.core.io.InternalCompactSerializable;
import com.alachisoft.ncache.serialization.standard.io.CompactReader;
import com.alachisoft.ncache.serialization.standard.io.CompactWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
// Copyright (c) 2020 Alachisoft
//
// 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
public class RecordRow implements Cloneable, InternalCompactSerializable {
public boolean isSurrogate;
private ColumnCollection _columns;
private TwoDimensionalArray _objects;
private Object _tag;
public RecordRow(ColumnCollection columns) {
_columns = columns;
_objects = new TwoDimensionalArray(columns.getCount());
}
public final Object get(int index) {
return _objects.get(index);
}
public final void set(int index, Object value) {
_objects.set(index, value);
}
public final Object get(String columnName) {
return _objects.get(_columns.getColumnIndex(columnName));
}
public final void set(String columnName, Object value) {
_objects.set(_columns.getColumnIndex(columnName), value);
}
public final Object getTag() {
return _tag;
}
public final void setTag(Object value) {
_tag = value;
}
public final ColumnCollection getColumns() {
return _columns;
}
public final void setColumns(ColumnCollection value) {
_columns = value;
}
public final void setColumnValue(String columnName, Object value) {
_objects.set(_columns.getColumnIndex(columnName), value);
}
public final void setColumnValue(int columnIndex, Object value) {
_objects.set(columnIndex, value);
}
public final Object getColumnValue(String columnName) {
return _objects.get(_columns.getColumnIndex(columnName));
}
public final Object getColumnValue(int columnIndex) {
return _objects.get(columnIndex);
}
public final Object[] getAll() {
return (Object[]) _objects.clone();
}
public final void setAll(ArrayList values) {
System.arraycopy(values,0,_objects,0,values.size());
}
public final int getSize() {
int size = 0;
for (int i = 0; i < _columns.getCount(); i++) {
switch (_columns.get(i).getDataType()) {
case AverageResult:
size += 32;
break;
case Bool:
size += 1;
break;
case Byte:
size += 1;
break;
case Char:
size += 2;
break;
case DateTime:
size += 64;
break;
case Decimal:
size += 16;
break;
case Double:
size += 8;
break;
case Float:
size += 4;
break;
case Int16:
size += 2;
break;
case Int32:
size += 4;
break;
case Int64:
size += 8;
break;
case Object:
break;
case SByte:
size += 1;
break;
case String:
String obj = _objects.get(i) instanceof String ? (String) _objects.get(i) : null;
if (obj != null) {
size += obj.length() * 2;
}
break;
case UInt16:
size += 2;
break;
case UInt32:
size += 4;
break;
case UInt64:
size += 8;
break;
}
}
return size;
}
public Object clone()
{
RecordRow row = new RecordRow(this._columns);
row._objects = (TwoDimensionalArray)this._objects.clone();
return row;
}
public final int compareOrder(RecordRow row, ArrayList orderBy) {
int result = 0;
for (OrderByArgument oba : orderBy) {
Object columValue = getColumnValue(oba.getAttributeName());
Object rowValue = row.getColumnValue(oba.getAttributeName());
switch (_columns.get(oba.getAttributeName()).getDataType()) {
case Bool:
result = ((Boolean) columValue).compareTo(((Boolean) rowValue));
break;
case Byte:
case SByte:
result = Byte.compare((byte) columValue, (byte) rowValue);
break;
case Char:
Character c1 = (char) columValue;
Character c2 = (char) rowValue;
result = c1.compareTo(c2);
break;
case DateTime:
result = ((Date) columValue).compareTo(((Date) rowValue));
break;
case Decimal:
result = ((BigDecimal) columValue).compareTo(((BigDecimal) rowValue));
break;
case Double:
result = ((Double) columValue).compareTo(((Double) rowValue));
break;
case Float:
result = ((Float) columValue).compareTo(((Float) rowValue));
break;
case Int16:
case UInt16:
result = Short.compare((short) columValue, (short) rowValue);
break;
case Int32:
case UInt32:
result = ((Integer) columValue).compareTo(((Integer) rowValue));
break;
case Int64:
case UInt64:
result = ((Long) columValue).compareTo(((Long) rowValue));
break;
case String:
result = compareString((String) columValue, (String) rowValue);
case CompressedValueEntry:
case Object:
break;
}
if (result != 0) {
if (oba.getOrder() == Order.DESC) {
result = -result;
}
break;
}
}
return result;
}
public final void merge(RecordRow row) {
for (int i = 0; i < this._columns.getCount(); i++) {
if (this._columns.get(i).getColumnType() == ColumnType.AggregateResultColumn) {
switch (this._columns.get(i).getAggregateFunctionType()) {
case SUM:
Object thisVal = this._objects.get(i);
Object otherVal = row._objects.get(i);
BigDecimal sum = null;
if (thisVal == null && otherVal != null) {
sum = (BigDecimal) otherVal;
} else if (thisVal != null && otherVal == null) {
sum = (BigDecimal) thisVal;
} else if (thisVal != null && otherVal != null) {
BigDecimal a = (BigDecimal) thisVal;
BigDecimal b = (BigDecimal) otherVal;
sum = a.add(b);
}
if (sum != null) {
this._objects.set(i, sum);
} else {
this._objects.set(i, null);
}
break;
case COUNT:
BigDecimal a = (BigDecimal) this._objects.get(i);
BigDecimal b = (BigDecimal) row._objects.get(i);
BigDecimal count = a.add(b);
this._objects.set(i, count);
break;
case MIN:
java.lang.Comparable thisValue = (java.lang.Comparable) this._objects.get(i);
java.lang.Comparable otherValue = (java.lang.Comparable) row._objects.get(i);
java.lang.Comparable min = thisValue;
if (thisValue == null && otherValue != null) {
min = otherValue;
} else if (thisValue != null && otherValue == null) {
min = thisValue;
} else if (thisValue == null && otherValue == null) {
min = null;
} else if (otherValue.compareTo(thisValue) < 0) {
min = otherValue;
}
this._objects.set(i, min);
break;
case MAX:
thisValue = (java.lang.Comparable) this._objects.get(i);
otherValue = (java.lang.Comparable) row._objects.get(i);
java.lang.Comparable max = thisValue;
if (thisValue == null && otherValue != null) {
max = otherValue;
} else if (thisValue != null && otherValue == null) {
max = thisValue;
} else if (thisValue == null && otherValue == null) {
max = null;
} else if (otherValue.compareTo(thisValue) > 0) {
max = otherValue;
}
this._objects.set(i, max);
break;
case AVG:
thisVal = this._objects.get(i);
otherVal = row._objects.get(i);
AverageResult avg = null;
if (thisVal == null && otherVal != null) {
avg = (AverageResult) otherVal;
} else if (thisVal != null && otherVal == null) {
avg = (AverageResult) thisVal;
} else if (thisVal != null && otherVal != null) {
AverageResult thisResult = (AverageResult) thisVal;
AverageResult otherResult = (AverageResult) otherVal;
avg = new AverageResult();
avg.setSum(thisResult.getSum().add(otherResult.getSum()));
avg.setCount(thisResult.getCount().add((otherResult.getCount())));
}
this._objects.set(i, avg);
break;
// This will be always merging non negative integers.
case TERMFREQ:
thisVal = this._objects.get(i);
otherVal = row._objects.get(i);
long freqSum = 0;
if (thisVal == null && otherVal != null) {
freqSum = (long) otherVal;
} else if (thisVal != null && otherVal == null) {
freqSum = (long) thisVal;
} else if (thisVal != null && otherVal != null) {
freqSum = (long) thisVal + (long) otherVal;
}
_objects.set(i, freqSum);
break;
}
}
}
}
private int compareString(String valueOne, String valueTwo) {
int result;
if (valueOne == null) {
if (valueTwo == null) {
result = 0;
} else {
result = -1;
}
} else {
if (valueTwo == null) {
result = 1;
} else {
result = valueOne.compareTo(valueTwo);
}
}
return result;
}
@Override
public void Deserialize(CompactReader reader) throws IOException, ClassNotFoundException {
isSurrogate = reader.ReadBoolean();
Object tempVar = reader.ReadObject();
_columns = tempVar instanceof ColumnCollection ? (ColumnCollection) tempVar : null;
_objects = new TwoDimensionalArray(_columns.getCount());
for (int i = 0; i < _objects.size(); i++) {
_objects.set(i, reader.ReadObject());
}
}
@Override
public void Serialize(CompactWriter writer) throws IOException {
writer.Write(isSurrogate);
writer.WriteObject(_columns);
for (int i = 0; i < _objects.size(); i++) {
writer.WriteObject(_objects.get(i));
}
}
}