/*
 * Copyright (c) 2011-2013 Spotify AB
 *
 * 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.spotify.sparkey.extra;

import com.spotify.sparkey.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * A thread-safe Sparkey Reader.
 */
public class ThreadLocalSparkeyReader extends AbstractDelegatingSparkeyReader {
  private final Collection<SparkeyReader> readers = new ArrayList<>();
  private volatile ThreadLocal<SparkeyReader> threadLocalReader;
  private final AtomicBoolean firstRead = new AtomicBoolean(false);

  public ThreadLocalSparkeyReader(File indexFile) throws IOException {
    this(Sparkey.openSingleThreadedReader(indexFile), true);
  }

  public ThreadLocalSparkeyReader(final SparkeyReader reader) {
    this(reader, false);
  }

  private ThreadLocalSparkeyReader(final SparkeyReader reader, final boolean owner) {
    Objects.requireNonNull(reader, "reader may not be null");
    this.readers.add(reader);
    this.threadLocalReader = ThreadLocal.withInitial(() -> {
      if (owner && firstRead.compareAndSet(false, true)) {
        return reader; // No need to duplicate the reader for the first usage if we are the owner of the reader
      }

      SparkeyReader r = reader.duplicate();
      synchronized (readers) {
        readers.add(r);
      }
      return r;
    });
  }

  @Override
  public void close() {
    this.threadLocalReader = null;
    synchronized (readers) {
      for (SparkeyReader reader : readers) {
        reader.close();
      }
      readers.clear();
    }
  }

  @Override
  public SparkeyReader duplicate() {
    if (threadLocalReader == null) {
      throw new IllegalStateException("reader is closed");
    }
    return this;
  }

  @Override
  protected SparkeyReader getDelegateReader() {
    if (threadLocalReader == null) {
      throw new IllegalStateException("reader is closed");
    }
    return threadLocalReader.get();
  }

}

@@ -782,23 +782,118 @@
 va.util.
-Objects
+HashSet;%0Aimport java.util.Objects;%0Aimport java.util.Set;%0Aimport java.util.concurrent.ConcurrentHashMap
 ;%0Aimport
@@ -1078,59 +1078,102 @@
 nal 
-Collection%3CSparkeyReader%3E readers = new ArrayList%3C%3E
+SparkeyReader reader;%0A  private final Set%3CSparkeyReader%3E readers = ConcurrentHashMap.newKeySet
 ();%0A
@@ -1240,76 +1240,8 @@
 der;
-%0A  private final AtomicBoolean firstRead = new AtomicBoolean(false);
 %0A%0A  
@@ -1365,534 +1365,167 @@
 ile)
-, true);%0A  %7D%0A%0A  public ThreadLocalSparkeyReader(final SparkeyReader reader) %7B%0A    this(reader, false);%0A  %7D%0A%0A  private ThreadLocalSparkeyReader(final SparkeyReader reader, final boolean owner) %7B%0A    Objects.requireNonNull(reader, %22reader may not be null%22);%0A    this.readers.add(reader);%0A    this.threadLocalReader = ThreadLocal.withInitial(() -%3E %7B%0A      if (owner && firstRead.compareAndSet(false, true)) %7B%0A        return reader; // No need to duplicate the reader for the first usage if we are the owner of the reader%0A      %7D%0A
+);%0A  %7D%0A%0A  private ThreadLocalSparkeyReader(final SparkeyReader reader) %7B%0A    this.reader = reader;%0A    this.threadLocalReader = ThreadLocal.withInitial(() -%3E %7B
 %0A   
@@ -1569,41 +1569,8 @@
 ();%0A
-      synchronized (readers) %7B%0A  
     
@@ -1587,24 +1587,16 @@
 add(r);%0A
-      %7D%0A
       re
@@ -1712,24 +1712,46 @@
 (readers) %7B%0A
+      reader.close();%0A
       for (S
@@ -2078,24 +2078,99 @@
 eReader() %7B%0A
+    final ThreadLocal%3CSparkeyReader%3E threadLocal = this.threadLocalReader;%0A
     if (thre
@@ -2168,38 +2168,32 @@
  if (threadLocal
-Reader
  == null) %7B%0A    
@@ -2275,22 +2275,16 @@
 eadLocal
-Reader
 .get();%0A

Simplify ThreadLocalSparkeyReader