/*
* Copyright 2014-2015 the original author or authors
*
* 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.
*/
package com.wplatform.ddal.result;
import com.wplatform.ddal.engine.Constants;
import com.wplatform.ddal.util.StatementBuilder;
import com.wplatform.ddal.value.Value;
/**
* Represents a simple row without state.
*/
public class SimpleRow implements SearchRow {
private final Value[] data;
private long key;
private int version;
private int memory;
public SimpleRow(Value[] data) {
this.data = data;
}
@Override
public int getColumnCount() {
return data.length;
}
@Override
public long getKey() {
return key;
}
@Override
public void setKey(long key) {
this.key = key;
}
@Override
public void setKeyAndVersion(SearchRow row) {
key = row.getKey();
version = row.getVersion();
}
@Override
public int getVersion() {
return version;
}
@Override
public void setValue(int i, Value v) {
data[i] = v;
}
@Override
public Value getValue(int i) {
return data[i];
}
@Override
public String toString() {
StatementBuilder buff = new StatementBuilder("( /* key:");
buff.append(getKey());
if (version != 0) {
buff.append(" v:" + version);
}
buff.append(" */ ");
for (Value v : data) {
buff.appendExceptFirst(", ");
buff.append(v == null ? "null" : v.getTraceSQL());
}
return buff.append(')').toString();
}
@Override
public int getMemory() {
if (memory == 0) {
int len = data.length;
memory = Constants.MEMORY_OBJECT + len * Constants.MEMORY_POINTER;
for (int i = 0; i < len; i++) {
Value v = data[i];
if (v != null) {
memory += v.getMemory();
}
}
}
return memory;
}
}