package com.caucho.lucene;

import com.caucho.env.system.RootDirectorySystem;
import io.baratine.core.Journal;
import io.baratine.core.OnDestroy;
import io.baratine.core.OnLookup;
import io.baratine.core.Service;
import io.baratine.core.Startup;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

@Service("pod://lucene/lucene-manager")
@Journal
@Startup
public class LuceneManagerImpl
{
  private static Logger log
    = Logger.getLogger(LuceneManagerImpl.class.getName());

  private Map<String,LuceneIndexImpl> _map = new HashMap<>();

  private String _indexDirectory;

  public LuceneManagerImpl()
  {
    String baratineData = RootDirectorySystem.getCurrentDataDirectory()
                                             .getFullPath();
    _indexDirectory = baratineData + File.separatorChar + "lucene";
  }

  public void setIndexDirectory(String indexDirectory)
  {
    indexDirectory = indexDirectory.replace('\\', '/');

    if (isAbsolute(indexDirectory)) {
      _indexDirectory = indexDirectory;
    }
    else if (_indexDirectory.endsWith(File.separator)) {
      _indexDirectory = _indexDirectory + indexDirectory;
    }
    else {
      _indexDirectory = _indexDirectory + File.separatorChar + indexDirectory;
    }
  }

  private boolean isAbsolute(String path)
  {

    if (path.indexOf('/') == 0
        || (Character.isLetter(path.charAt(0))
            && path.charAt(1) == ':'
            && path.charAt(2) == '/')) {
      return true;
    }

    return false;
  }

  @OnLookup
  public Object lookup(final String path) throws IOException
  {
    LuceneIndexImpl lucene = _map.get(path);

    if (lucene == null) {
      String address = path.substring(path.lastIndexOf("///") + 3);

      lucene = new LuceneIndexImpl(address, _indexDirectory);

      _map.put(path, lucene);
    }

    return lucene;
  }

  @OnDestroy
  public void destroy()
  {
    log.finer("destroying " + this);

    _map.values().forEach(l -> {
      try {
        l.destroy();
      } catch (Exception e) {
      }
    });
  }

  @Override
  public String toString()
  {
    return this.getClass().getSimpleName() + "[]";
  }
}

@@ -108,43 +108,8 @@
 al;%0A
-import io.baratine.core.OnDestroy;%0A
 impo
@@ -138,16 +138,16 @@
 Lookup;%0A
+
 import i
@@ -258,56 +258,8 @@
 on;%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A
 impo
@@ -407,16 +407,16 @@
 ger log%0A
+
     = Lo
@@ -471,71 +471,8 @@
 );%0A%0A
-  private Map%3CString,LuceneIndexImpl%3E _map = new HashMap%3C%3E();%0A%0A
   pr
@@ -1496,82 +1496,8 @@
   %7B%0A
-    LuceneIndexImpl lucene = _map.get(path);%0A%0A    if (lucene == null) %7B%0A  
     
@@ -1567,18 +1567,14 @@
     
-  lucene =
+return
  new
@@ -1620,261 +1620,8 @@
 ry);
-%0A%0A      _map.put(path, lucene);%0A    %7D%0A%0A    return lucene;%0A  %7D%0A%0A  @OnDestroy%0A  public void destroy()%0A  %7B%0A    log.finer(%22destroying %22 + this);%0A%0A    _map.values().forEach(l -%3E %7B%0A      try %7B%0A        l.destroy();%0A      %7D catch (Exception e) %7B%0A      %7D%0A    %7D);
 %0A  %7D

remove internal map of services. (this is supported by baratine lookup)