/*
 * Copyright 2006-2010 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * 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.amazon.carbonado.repo.indexed;

import java.util.Comparator;
import java.util.Map;
import java.util.WeakHashMap;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;

import com.amazon.carbonado.Storable;
import com.amazon.carbonado.SupportException;
import com.amazon.carbonado.info.StorableIndex;
import com.amazon.carbonado.info.StorableProperty;
import com.amazon.carbonado.synthetic.SyntheticStorableReferenceAccess;
import com.amazon.carbonado.synthetic.SyntheticStorableReferenceBuilder;

/**
 * IndexEntryGenerator creates new kinds of Storables suitable for indexing a
 * master Storable.
 *
 * @author Brian S O'Neill
 * @author Don Schneider
 *
 */
class IndexEntryGenerator <S extends Storable> {

    // cache for access classes
    private static Map<StorableIndex, Reference<SyntheticStorableReferenceAccess>> cCache =
        new WeakHashMap<StorableIndex, Reference<SyntheticStorableReferenceAccess>>();

    /**
     * Returns a new or cached index access instance. The caching of accessors
     * is soft, so if no references remain to a given instance it may be
     * garbage collected. A subsequent call will return a newly created
     * instance.
     *
     * <p>In addition to generating an index entry storable, the accessor
     * contains methods to operate on it. Care must be taken to ensure that the
     * index entry instances are of the same type that the accessor expects.
     * Since the accessor may be garbage collected freely of the generated
     * index entry class, it is possible for index entries to be passed to a
     * accessor instance that does not understand it. For example:
     *
     * <pre>
     * StorableIndex index = ...
     * Class indexEntryClass = IndexEntryGenerator.getIndexAccess(index).getReferenceClass();
     * ...
     * garbage collection
     * ...
     * Storable indexEntry = instance of indexEntryClass
     * // Might fail because generator instance is new
     * IndexEntryGenerator.getIndexAccess(index).copyFromMaster(indexEntry, master);
     * </pre>
     *
     * The above code can be fixed by saving a local reference to the accessor:
     *
     * <pre>
     * StorableIndex index = ...
     * SyntheticStorableReferenceAccess access = IndexEntryGenerator.getIndexAccess(index);
     * Class indexEntryClass = access.getReferenceClass();
     * ...
     * Storable indexEntry = instance of indexEntryClass
     * access.copyFromMaster(indexEntry, master);
     * </pre>
     *
     * @throws SupportException if any non-primary key property doesn't have a
     * public read method.
     */
    public static <S extends Storable>
        SyntheticStorableReferenceAccess<S> getIndexAccess(StorableIndex<S> index)
        throws SupportException
    {
        synchronized (cCache) {
            SyntheticStorableReferenceAccess<S> access;
            Reference<SyntheticStorableReferenceAccess> ref = cCache.get(index);
            if (ref != null) {
                access = ref.get();
                if (access != null) {
                    return access;
                }
            }

            // Need to try to find the base type.  This is an awkward way to do
            // it, but we have nothing better available to us
            Class<S> type = index.getProperty(0).getEnclosingType();

            SyntheticStorableReferenceBuilder<S> builder =
                new SyntheticStorableReferenceBuilder<S>(type, index.isUnique());

            for (int i=0; i<index.getPropertyCount(); i++) {
                StorableProperty<S> source = index.getProperty(i);
                builder.addKeyProperty(source.getName(), index.getPropertyDirection(i));
            }

            builder.build();
            access = builder.getReferenceAccess();

            cCache.put(index, new SoftReference<SyntheticStorableReferenceAccess>(access));

            return access;
        }
    }

    private IndexEntryGenerator() {
    }
}

@@ -851,151 +851,48 @@
 ort 
-java.util.Comparator;%0D%0Aimport java.util.Map;%0D%0Aimport java.util.WeakHashMap;%0D%0Aimport java.lang.ref.Reference;%0D%0Aimport java.lang.ref.SoftReferenc
+com.amazon.carbonado.util.SoftValuedCach
 e;%0D%0A
@@ -1461,59 +1461,44 @@
     
-// cache for access classes%0D%0A    private static Map
+private static final SoftValuedCache
 %3CSto
@@ -1505,34 +1505,24 @@
 rableIndex, 
-Reference%3C
 SyntheticSto
@@ -1542,17 +1542,16 @@
 eAccess%3E
-%3E
  cCache 
@@ -1565,84 +1565,35 @@
     
-new WeakHashMap%3CStorableIndex, Reference%3CSyntheticStorableReferenceAccess%3E%3E(
+SoftValuedCache.newCache(11
 );%0D%0A
@@ -3464,88 +3464,140 @@
     
-synchronized (cCache) %7B%0D%0A            SyntheticStorableReferenceAccess%3CS%3E access;
+// Strip out index property not related to its identity.%0D%0A        index = index.clustered(false);%0D%0A%0D%0A        synchronized (cCache) %7B
 %0D%0A  
@@ -3606,26 +3606,16 @@
         
-Reference%3C
 Syntheti
@@ -3642,13 +3642,18 @@
 cess
-%3E ref
+%3CS%3E access
  = c
@@ -3675,81 +3675,8 @@
 );%0D%0A
-            if (ref != null) %7B%0D%0A                access = ref.get();%0D%0A    
     
@@ -3702,28 +3702,24 @@
 != null) %7B%0D%0A
-    
             
@@ -3742,27 +3742,8 @@
 s;%0D%0A
-                %7D%0D%0A
     
@@ -4471,68 +4471,15 @@
 ex, 
-new SoftReference%3CSyntheticStorableReferenceAccess%3E(
 access)
-)
 ;%0D%0A%0D

Fix index entry class generation when using mix of repository types.