package org.basex.data;

import org.basex.core.*;
import org.basex.index.*;
import org.basex.index.name.*;
import org.basex.index.path.*;
import org.basex.index.value.*;
import org.basex.io.random.*;
import org.basex.util.*;

/**
 * This class stores and organizes the database table and the index structures
 * for textual content in a compressed memory structure.
 * The table mapping is documented in {@link Data}.
 *
 * @author BaseX Team 2005-12, BSD License
 * @author Christian Gruen
 */
public final class MemData extends Data {
  /**
   * Constructor.
   * @param ps path summary
   * @param ns namespaces
   * @param pr database properties
   */
  public MemData(final PathSummary ps, final Namespaces ns, final Prop pr) {
    this(null, null, ps, ns, pr, null, null);
  }

  /**
   * Constructor.
   * @param tag tag index
   * @param att attribute name index
   * @param ps path summary
   * @param ns namespaces
   * @param pr database properties
   * @param txt text index
   * @param atv attribute value index
   */
  public MemData(final Names tag, final Names att, final PathSummary ps,
      final Namespaces ns, final Prop pr, final Index txt, final Index atv) {

    meta = new MetaData(pr);
    table = new TableMemAccess(meta);
    if(meta.updindex && (txt == null || atv == null)) {
      idmap = new IdPreMap(meta.lastid);
      txtindex = new UpdatableMemValues(this);
      atvindex = new UpdatableMemValues(this);
    } else {
      txtindex = txt == null ? new MemValues(this) : txt;
      atvindex = atv == null ? new MemValues(this) : atv;
    }
    tagindex = tag == null ? new Names(meta) : tag;
    atnindex = att == null ? new Names(meta) : att;
    paths = ps == null ? new PathSummary(this) : ps;
    nspaces = ns == null ? new Namespaces() : ns;
  }

  /**
   * Light-weight constructor, adopting data structures from the specified database.
   * @param data data reference
   */
  public MemData(final Data data) {
    this(data.tagindex, data.atnindex, data.paths, null, data.meta.prop,
        data.txtindex, data.atvindex);
  }

  /**
   * Constructor, creating a new, empty database.
   * @param pr property reference
   */
  public MemData(final Prop pr) {
    this(null, null, pr);
  }

  @Override
  public void close() { }

  @Override
  public void closeIndex(final IndexType type) { }

  @Override
  public void setIndex(final IndexType type, final Index index) { }

  @Override
  public boolean startUpdate() { return true; }

  @Override
  public void finishUpdate() { }

  @Override
  public byte[] text(final int pre, final boolean text) {
    return ((MemValues) (text ? txtindex : atvindex)).key((int) textOff(pre));
  }

  @Override
  public long textItr(final int pre, final boolean text) {
    return Token.toLong(text(pre, text));
  }

  @Override
  public double textDbl(final int pre, final boolean text) {
    return Token.toDouble(text(pre, text));
  }

  @Override
  public int textLen(final int pre, final boolean text) {
    return text(pre, text).length;
  }

  // UPDATE OPERATIONS ========================================================

  @Override
  protected void delete(final int pre, final boolean text) { }

  @Override
  public void updateText(final int pre, final byte[] val, final int kind) {
    final int id = id(pre);
    if(meta.updindex) {
      final boolean txt = kind != ATTR;
      ((MemValues) (txt ? txtindex : atvindex)).delete(text(pre, txt), id);
    }
    textOff(pre, index(pre, id, val, kind));
  }

  @Override
  protected long index(final int pre, final int id, final byte[] txt,
      final int kind) {
    return ((MemValues) (kind == ATTR ? atvindex : txtindex)).
        index(txt, meta.updindex ? id : pre);
  }

  @Override
  protected void indexDelete(final int pre, final int size) {
    final int l = pre + size;
    for(int p = pre; p < l; ++p) {
      final int k = kind(p);
      final boolean isAttr = k == ATTR;
      // skip nodes which are not attribute, text, comment, or proc. instruction
      if(isAttr || k == TEXT || k == COMM || k == PI) {
        final byte[] key = text(p, !isAttr);
        ((MemValues) (isAttr ? atvindex : txtindex)).delete(key, id(p));
      }
    }
  }
}

@@ -1311,45 +1311,13 @@
 ndex
- && (txt == null %7C%7C atv == null)
 ) %7B%0D%0A
+
     
@@ -1366,24 +1366,38 @@
   txtindex =
+ txt == null ?
  new Updatab
@@ -1409,24 +1409,30 @@
 Values(this)
+ : txt
 ;%0D%0A      atv
@@ -1438,16 +1438,30 @@
 vindex =
+ atv == null ?
  new Upd
@@ -1481,16 +1481,22 @@
 es(this)
+ : atv
 ;%0D%0A    %7D

initialize IdPreMap if UPDINDEX is true