/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.index;

import java.io.*;
import org.apache.log4j.Logger;
import org.apache.lucene.store.*;

class CurrentFile
{
  /*--------------------------------------------------------------------------------
   * Internal class implementing the random-access write capability
   *--------------------------------------------------------------------------------
   */

  private class MyOutput extends org.apache.lucene.store.OutputStream
  {
    /*====================================================================
     * Attributes
     *====================================================================
     */

    private RandomAccessFile m_fptr;

    /*====================================================================
     * Constructor
     *====================================================================
     */

    MyOutput() throws IOException
    {
      super();
      m_fptr = new RandomAccessFile(m_file,"rw");
      addRef();

    } // end constructor

    /*====================================================================
     * Abstract implementations from class OutputStream
     *====================================================================
     */

    protected void flushBuffer(byte[] b, int len) throws IOException
    {
      if ((len>0) && (b!=null))
      { // write data
	m_fptr.write(b,0,len);
	m_mtime = System.currentTimeMillis();
	m_length = m_fptr.length();
	m_modified = true;

      } // end if

    } // end flushBuffer

    public long length() throws IOException
    {
      return m_fptr.length();

    } // end length

    /*====================================================================
     * Overrides from class OutputStream
     *====================================================================
     */

    public void close() throws IOException
    {
      super.close();
      m_fptr.close();
      release();

    } // end close

    public void seek(long pos) throws IOException
    {
      super.seek(pos);
      m_fptr.seek(pos);

    } // end seek

  } // end class MyOutput

  /*--------------------------------------------------------------------------------
   * Internal class implementing the random-access read capability
   *--------------------------------------------------------------------------------
   */

  private class MyInput extends org.apache.lucene.store.InputStream
  {
    /*====================================================================
     * Attributes
     *====================================================================
     */

    private RandomAccessFile m_fptr;

    /*====================================================================
     * Constructor
     *====================================================================
     */

    MyInput() throws IOException
    {
      super();
      m_fptr = new RandomAccessFile(m_file,"r");
      addRef();

    } // end constructor

    /*====================================================================
     * Abstract implementations from class InputStream
     *====================================================================
     */

    protected void readInternal(byte[] b, int offset, int length) throws IOException
    {
      m_fptr.readFully(b,offset,length);

    } // end readInternal
    
    public void close() throws IOException
    {
      m_fptr.close();
      release();

    } // end close

    protected void seekInternal(long pos) throws IOException
    {
      m_fptr.seek(pos);

    } // end seekInternal

    /*====================================================================
     * Overrides from class InputStream
     *====================================================================
     */

    public Object clone()
    {
      try
      { // return a new input object
	return new MyInput();

      } // end try
      catch (IOException e)
      { // whoops!
	logger.error("CurrentFile.MyInput.clone(): unable to clone file",e);
	return null;

      } // end catch

    } // end clone

  } // end class MyInput

  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(CurrentFile.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private IndexDirectoryImpl m_base;
  private File m_file;
  private String m_name;
  private long m_length;
  private long m_mtime;
  private int m_refcount = 1;
  private boolean m_modified = false;
  private boolean m_noupdate = true;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  CurrentFile(IndexDirectoryImpl base, File file, String name, long length, long mtime)
  {
    m_base = base;
    m_file = file;
    m_name = name;
    m_length = length;
    m_mtime = mtime;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  synchronized int addRef()
  {
    return ++m_refcount;

  } // end addRef

  synchronized int release() throws IOException
  {
    int rc = --m_refcount;
    if (rc==0)
    { // dump this file back to the database
      if (m_modified && !m_noupdate)
	m_base.storeFile(m_name,m_mtime,m_file);
      m_base.detach(m_name);
      m_base = null;
      m_file.delete();
      m_file = null;

    } // end if

    return rc;

  } // end release

  void abandon()
  {
    m_base.detach(m_name);
    m_base = null;
    m_file.delete();
    m_file = null;

  } // end abandon

  org.apache.lucene.store.OutputStream openOutput() throws IOException
  {
    return new MyOutput();

  } // end openOutput

  org.apache.lucene.store.InputStream openInput() throws IOException
  {
    return new MyInput();

  } // end openInput

  long getModTime()
  {
    return m_mtime;

  } // end getModTime

  void setModTime(long val)
  {
    m_mtime = val;
    m_modified = true;

  } // end setModTime

  void noUpdate()
  {
    m_noupdate = true;

  } // end noUpdate

  void setName(String s)
  {
    m_name = s;

  } // end setName

  long length()
  {
    return m_length;

  } // end length

} // end class CurrentFile
