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

com.alachisoft.ncache.client.internal.caching.ClientDataReader Maven / Gradle / Ivy

package com.alachisoft.ncache.client.internal.caching;

import Alachisoft.NCache.Caching.CompressedValueEntry;
import Alachisoft.NCache.Common.Queries.QueryKeyWords;

import com.alachisoft.ncache.client.CacheReader;
import Alachisoft.NCache.Common.ErrorHandling.ErrorCodes;
import Alachisoft.NCache.Common.ErrorHandling.ErrorMessages;
import com.alachisoft.ncache.runtime.exceptions.CacheException;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import com.alachisoft.ncache.runtime.JSON.JsonUtil;

import java.io.IOException;

class ClientDataReader extends DataReaderCommon implements CacheReader
{
    private ClientRecordSetEnumerator _clientRecordSetEnumerator;
    private Object[] _currentRow;
    private static final String keyColumnName = "$KEY$";
    private static final String valueColumnName = "$VALUE$";

    public ClientDataReader(ClientRecordSetEnumerator clientRecordSetEnumerator)
    {
        this(null, clientRecordSetEnumerator);
    }

    public ClientDataReader(CacheImpl cache, ClientRecordSetEnumerator clientRecordSetEnumerator)
    {
        _cache = cache;
        _clientRecordSetEnumerator = clientRecordSetEnumerator;
    }

    public void close()
    {
        if (_clientRecordSetEnumerator != null)
        {
            _clientRecordSetEnumerator.dispose();
        }
    }

    public boolean read() throws CacheException {
        if (_clientRecordSetEnumerator == null)
        {
            return false;
        }
        boolean next = _clientRecordSetEnumerator.moveNext();
        if (next)
        {
            _currentRow = _clientRecordSetEnumerator.getCurrent();
        }
        return next;
    }

    public boolean getBoolean(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return (boolean)(_currentRow[index]);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }
    }

    public String getString(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return _currentRow[index] == null ? null: _currentRow[index].toString();
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }

    }

    public java.math.BigDecimal getBigDecimal(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return (java.math.BigDecimal)(_currentRow[index]);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }
    }

    public double getDouble(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return (double)(_currentRow[index]);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }
    }

    public short getShort(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return (short)(_currentRow[index]);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }

    }

    public int getInt(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return (int)(_currentRow[index]);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }

    }

    public long getLong(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            return (long)(_currentRow[index]);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }

    }


    public  T getValue(int index, Class cls) throws ClassNotFoundException, OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }

        if (index >= 0 && index < getFieldCount())
        {
            Object obj = _currentRow[index];
            CompressedValueEntry cmpEntry = (CompressedValueEntry)((obj instanceof CompressedValueEntry) ? obj : null);

            if (cmpEntry != null)
            {
                try {
                    obj = extractFromCompressedValueEntry(cmpEntry, cls);
                } catch (IOException e) {
                    throw new OperationFailedException(e);
                }
            }
            return JsonUtil.getValueAs(obj, cls);
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }
    }
    @Override
    public  T getValue(String columnName, Class cls) throws IOException, ClassNotFoundException, OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }

        int index = getOrdinal(columnName);
        Object obj = _currentRow[index];
        CompressedValueEntry cmpEntry = (CompressedValueEntry)((obj instanceof CompressedValueEntry) ? obj : null);

        if (cmpEntry != null)
        {
            obj = extractFromCompressedValueEntry(cmpEntry, cls);
        }
        return JsonUtil.getValueAs(obj,cls);
    }

    public int getValues(Object[] objects, Class cls) throws ClassNotFoundException, OperationFailedException {
        if (objects == null)
        {
            throw new IllegalArgumentException("objects");
        }
        if (objects.length == 0)
        {
            throw new IllegalArgumentException("Index was outside the bounds of the array.");
        }

        if (objects.length < this.getFieldCount())
        {
            for (int i = 0; i < objects.length; i++)
            {
                objects[i] = this.getSingleValue(i,(cls==null?Object.class:cls));
            }
            return objects.length;
        }
        else
        {
            for (int i = 0; i < this.getFieldCount(); i++)
            {
                objects[i] = this.getSingleValue(i,(cls==null?Object.class:cls));
            }
            return this.getFieldCount();
        }

    }

    private  T getSingleValue(int index, Class cls) throws ClassNotFoundException, OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            Object obj = _currentRow[index];
            CompressedValueEntry cmpEntry = (CompressedValueEntry)((obj instanceof CompressedValueEntry) ? obj : null);

            if (cmpEntry != null)
            {
                try {
                    obj = extractObjectFromCompressedValueEntry(cmpEntry, cls );
                } catch (IOException e) {
                    throw new OperationFailedException(e);
                }
            }

            return (T) obj;
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }
    }

    public String getName(int columnIndex)
    {
        if (columnIndex >= 0 && columnIndex < getFieldCount())
        {
            String columnName = "";
            switch (columnIndex)
            {
                case 0:
                    columnName = keyColumnName;
                    break;
                case 1:
                    columnName = valueColumnName;
                    break;
            }
            return columnName;
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }
    }

    public int getOrdinal(String columnName)
    {
        if (columnName.equalsIgnoreCase(keyColumnName))
        {
            return 0;
        }
        else if (columnName.equalsIgnoreCase(valueColumnName))
        {
            return 1;
        }
        else
        {
            throw new IllegalArgumentException("Invalid columnName. Specified column does not exist in RecordSet.");
        }
    }

    public boolean getIsClosed()
    {
        return _clientRecordSetEnumerator == null;
    }

    public java.util.Date getDate(int index) throws OperationFailedException {
        if (_currentRow == null)
        {
            throw new OperationFailedException(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED, ErrorMessages.getErrorMessage(ErrorCodes.Queries.CURRENT_STATE_NOT_SUPPORTED));
        }
        if (index >= 0 && index < getFieldCount())
        {
            Object obj = _currentRow[index];
            return (java.util.Date)obj;
        }
        else
        {
            throw new IllegalArgumentException("Invalid index. Specified index does not exist in RecordSet.");
        }

    }

    public int getFieldCount()
    {
        return _clientRecordSetEnumerator == null ? 0 : _clientRecordSetEnumerator.getFieldCount();
    }

    private void dispose(boolean disposing)
    {
        close();
        _currentRow = null;
    }


    public void dispose()
    {
        dispose(true);
    }

    protected void finalize() throws Throwable
    {
        dispose(false);
    }

    @Override
    protected boolean tryGetKeyFromCurrentRow(tangible.RefObject key)
    {
        Object possiblyKey = _currentRow[getOrdinal(QueryKeyWords.KeyColumn)];
        key.argvalue = possiblyKey == null ? null : possiblyKey.toString();
        return key.argvalue != null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy