/* * 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; import com.wplatform.ddal.value.ValueLong; /** * Represents a row in a table. */ public class Row implements SearchRow { public static final int MEMORY_CALCULATE = -1; public static final Row[] EMPTY_ARRAY = {}; private final Value[] data; private long key; private int memory; private int version; private boolean deleted; private int sessionId; public Row(Value[] data, int memory) { this.data = data; this.memory = memory; } /** * Get a copy of the row that is distinct from (not equal to) this row. * This is used for FOR UPDATE to allow pseudo-updating a row. * * @return a new row with the same data */ public Row getCopy() { Value[] d2 = new Value[data.length]; System.arraycopy(data, 0, d2, 0, data.length); Row r2 = new Row(d2, memory); r2.key = key; r2.version = version + 1; r2.sessionId = sessionId; return r2; } @Override public void setKeyAndVersion(SearchRow row) { setKey(row.getKey()); setVersion(row.getVersion()); } @Override public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } @Override public long getKey() { return key; } @Override public void setKey(long key) { this.key = key; } @Override public Value getValue(int i) { return i == -1 ? ValueLong.get(key) : data[i]; } @Override public void setValue(int i, Value v) { if (i == -1) { this.key = v.getLong(); } else { data[i] = v; } } public boolean isEmpty() { return data == null; } @Override public int getColumnCount() { return data.length; } @Override public int getMemory() { if (memory != MEMORY_CALCULATE) { return memory; } int m = Constants.MEMORY_ROW; if (data != null) { int len = data.length; m += Constants.MEMORY_OBJECT + len * Constants.MEMORY_POINTER; for (int i = 0; i < len; i++) { Value v = data[i]; if (v != null) { m += v.getMemory(); } } } this.memory = m; return m; } @Override public String toString() { StatementBuilder buff = new StatementBuilder("( /* key:"); buff.append(getKey()); if (version != 0) { buff.append(" v:" + version); } if (isDeleted()) { buff.append(" deleted"); } buff.append(" */ "); if (data != null) { for (Value v : data) { buff.appendExceptFirst(", "); buff.append(v == null ? "null" : v.getTraceSQL()); } } return buff.append(')').toString(); } public int getSessionId() { return sessionId; } public void setSessionId(int sessionId) { this.sessionId = sessionId; } /** * This record has been committed. The session id is reset. */ public void commit() { this.sessionId = 0; } public boolean isDeleted() { return deleted; } public void setDeleted(boolean deleted) { this.deleted = deleted; } public Value[] getValueList() { return data; } }