/**
* Copyright 2011-2012 Akiban Technologies, Inc.
*
* 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.persistit;
import com.persistit.util.Util;
/**
* An immutable, inactive representation of the state of a {@link Tree} object.
* Instances of this class are displayable and are what is generated by the
* {@link Value} class when fetching members of a directory tree.
*/
public class TreeState {
private String _name;
private long _rootPageAddr;
private int _depth;
private long _changeCount;
private final TreeStatistics _treeStatistics = new TreeStatistics();
public String getName() {
return _name;
}
/**
* Returns the page address of the root page of this <code>Tree</code>. The
* root page will be a data page if the <code>Tree</code> has only one page,
* or will be the top index page of the B-Tree.
*
* @return The root page address
*/
public long getRootPageAddr() {
return _rootPageAddr;
}
/**
* @return The number of key-value insert/delete operations performed on
* this tree; does not including replacement of an existing value
*/
public long getChangeCount() {
return _changeCount;
}
/**
* @return the number of levels of the <code>Tree</code>.
*/
public int getDepth() {
return _depth;
}
/**
* Load the state of an existing Tree from the directory from backing byte
* array. Note: only {@link Tree} object should have a
* {@link Tree#store(byte[], int)} operation. This class is only for display
* and reference.
*
* @param value
*/
int load(final byte[] bytes, final int index, final int length) {
final int nameLength = length < 20 ? -1 : Util.getShort(bytes, index + 18);
if (nameLength < 1 || nameLength + 20 > length) {
throw new IllegalStateException("Invalid tree record is too short for tree " + _name + ": " + length);
}
_name = new String(bytes, index + 20, nameLength);
_rootPageAddr = Util.getLong(bytes, index);
_changeCount = Util.getLong(bytes, index + 8);
_depth = Util.getShort(bytes, index + 16);
final int at = index + 20 + nameLength;
final int slen = length - (20 + nameLength);
if (slen > 8) {
_treeStatistics.load(bytes, at, slen);
}
return length;
}
public TreeStatistics getTreeStatistics() {
return _treeStatistics;
}
/**
* Returns a displayable description of the <code>Tree</code>, including its
* name, its internal tree index, its root page address, and its depth.
*
* @return A displayable summary
*/
@Override
public String toString() {
return String.format("%s root=%,d depth=%d changes=%,d", _name, _rootPageAddr, _depth, _changeCount);
}
}