All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.DataReader.RecordSetImpl Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.DataReader;

import Alachisoft.NCache.Common.DataStructures.ColumnDataType;
import Alachisoft.NCache.Common.Queries.AverageResult;
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.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 RecordSetImpl implements RecordSet, InternalCompactSerializable {
    private ColumnCollection _columns;
    private RowCollection _rows;
    private SubsetInfo _subsetInfo;
    private int _nextIndex = 0;

    public RecordSetImpl() {
        _columns = new ColumnCollection();
        _rows = new RowCollection(_columns);
    }

    public RecordSetImpl(ColumnCollection columnMetaData) {
        _columns = columnMetaData;
        _rows = new RowCollection(_columns);
    }

    /**
     * @param obj
     * @param dataType
     * @return
     */
    public static String getString(Object obj, ColumnDataType dataType) {
        switch (dataType) {
            case DateTime:
                return String.format("%d", (LocalDateTime) obj);
            case String:
                return (String) obj;
            default:
                return obj.toString();

        }
    }

    public final int getNextIndex() {
        return _nextIndex;
    }

    public final void setNextIndex(int value) {
        _nextIndex = value;
    }

    public final SubsetInfo getSubsetInfo() {
        return _subsetInfo;
    }

    public final void setSubsetInfo(SubsetInfo value) {
        _subsetInfo = value;
    }

    /**
     * Returns  associated with current 
     */
    public final ColumnCollection getColumns() {
        return _columns;
    }


    //IRecordSet Implementation

    /**
     * Returns  present in current 
     */
    public final RowCollection getRows() {
        return _rows;
    }

    /**
     * Return a sub  with same column matadata as current  but contains only specified rows.
     *
     * @param startingRowIndex Starting row index for sub  generation.
     * @param count            Total number of rows to be included in sub .
     * @return Sub 
     */
    public final RecordSet getSubRecordSet(int startingRowIndex, int count) {
        RecordSetImpl subRecordSet = new RecordSetImpl(this._columns);
        int i = -1;
        for (i = startingRowIndex; i < startingRowIndex + count && i < this.getRowCount(); i++) {
            subRecordSet.addRow((RecordRow) this.getRow(i).clone());
        }
        subRecordSet.setSubsetInfo(new SubsetInfo());
        subRecordSet.getSubsetInfo().setLastAccessedRowID(i < this.getRowCount() ? i - 1 : this.getRowCount() - 1);
        return subRecordSet;
    }

    /**
     * Gets size of current 
     *
     * @return Size in bytes of 
     */
    public final int getSize() {
        return 0;
    }

    /**
     * Adds  in current 
     *
     * @param column  to be added
     */
    public final void addColumn(RecordColumn column) {
        this._columns.add(column);
    }

    /**
     * Returns new  with column matadata of current 
     *
     * @return Newly created 
     */
    public final RecordRow createRow() {
        RecordRow row = new RecordRow(_columns);
        return row;
    }

    /**
     * Adds row to current 
     *
     * @param row  to be added in current 
     */
    public final void addRow(RecordRow row) {
        _rows.add(row);
    }

    /**
     * Gets  associated with rowID
     *
     * @param rowID Index of  required
     * @return Required  accoring to rowID
     */
    public final RecordRow getRow(int rowID) {
        return _rows.getRow(rowID);
    }

    public final boolean containsRow(int rowID) {
        return _rows.contains(rowID);
    }

    /**
     * Removes  associated with rowID
     *
     * @param rowID Index of  to be removed
     */
    public final void removeRow(int rowID) {
        _rows.removeRow(rowID);
    }

    /**
     * Removes specified rows range from current 
     *
     * @param startingIndex Starting index of row's range to be removed.
     * @param count         Total number of rows to be removed.
     * @return Number of rows removed
     */
    public final int removeRows(int startingIndex, int count) {
        if (startingIndex < 0 || count < 0) {
            return 0;
        }
        int removed = 0;
        for (int i = startingIndex; i < startingIndex + count; i++) {
            if (_rows.removeRow(i)) {
                removed++;
            }
        }
        return removed;
    }

    //Utility Functions for RecordSet

    public final ColumnCollection getColumnMetaData() {
        return _columns;
    }

    /**
     * Gets number of rows in current 
     */
    public final int getRowCount() {
        return _rows.getCount();
    }

    public final RecordSetEnumerator getEnumerator() {
        return new RecordSetEnumeratorImpl(this);
    }

    @Override
    public void Deserialize(CompactReader reader) throws IOException, ClassNotFoundException {
        Object tempVar = reader.ReadObject();
        _columns = tempVar instanceof ColumnCollection ? (ColumnCollection) tempVar : null;
        Object tempVar2 = reader.ReadObject();
        _rows = tempVar2 instanceof RowCollection ? (RowCollection) tempVar2 : null;
        Object tempVar3 = reader.ReadObject();
        _subsetInfo = tempVar3 instanceof SubsetInfo ? (SubsetInfo) tempVar3 : null;
    }

    @Override
    public void Serialize(CompactWriter writer) throws IOException {
        writer.WriteObject(_columns);
        writer.WriteObject(_rows);
        writer.WriteObject(_subsetInfo);
    }

}








© 2015 - 2024 Weber Informatics LLC | Privacy Policy