package com.dbxml.xml.dtsm;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: CompressedTable.java,v 1.2 2004/02/12 00:17:58 bradford Exp $
 */

import com.dbxml.db.core.data.Value;
import com.dbxml.util.ByteArray;
import com.dbxml.xml.SymbolTable;

/**
 * CompressedTable
 */

public final class CompressedTable implements DocumentTable {
   private SymbolTable symbols;

   private byte[] data;
   private int entryCount;
   private int entryOffset;
   private int valueCount;
   private int valueOffset;
   private int dataOffset;

   public CompressedTable(byte[] data, SymbolTable symbols) {
      this.data = data;
      this.symbols = symbols;

      entryCount = ByteArray.readInt(data, 0);
      valueCount = ByteArray.readInt(data, Constants.INT_SIZE);

      entryOffset = Constants.INT_SIZE * 2;
      valueOffset = entryOffset + (entryCount * DocumentTableEntry.SIZE);
      dataOffset = valueOffset + (valueCount * ValueTableEntry.SIZE);
   }

   public byte[] getData() {
      return data;
   }

   public void setSymbolTable(SymbolTable symbols) {
      this.symbols = symbols;
   }

   public SymbolTable getSymbolTable() {
      return symbols;
   }

   public int getEntryCount() {
      return entryCount;
   }

   public DocumentTableEntry getEntry(int idx) {
      int offset = entryOffset + (idx * DocumentTableEntry.SIZE);
      return new DocumentTableEntry(data, offset);
   }

   public int getValueCount() {
      return valueCount;
   }

   public ValueTableEntry getValueEntry(int idx) {
      int offset = valueOffset + (idx * ValueTableEntry.SIZE);
      return new ValueTableEntry(data, offset);
   }

   public Value getValue(int idx) {
      ValueTableEntry vte = getValueEntry(idx);
      return getValue(vte);
   }

   public Value getValue(ValueTableEntry vte) {
      return new Value(data, dataOffset+vte.getOffset(), vte.getLength());
   }
}
