/*
 * Copyright 2017 the original author or authors.
 *
 * 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 org.gradle.api.internal.changedetection.state;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import org.gradle.cache.PersistentIndexedCache;
import org.gradle.caching.internal.BuildCacheHasher;
import org.gradle.caching.internal.DefaultBuildCacheHasher;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;

/**
 * Caches the result of hashing a {@link RegularFileSnapshot} with a {@link ResourceHasher}.
 * It does not cache the result of hashing {@link ZipEntry}s.
 * It also caches the absence of a hash.
 */
public class CachingResourceHasher implements ResourceHasher {
    private static final HashCode NO_HASH = Hashing.md5().hashString(CachingResourceHasher.class.getName() + " : no hash", Charsets.UTF_8);
    private final ResourceHasher delegate;
    private final PersistentIndexedCache<HashCode, HashCode> persistentCache;
    private final byte[] delegateImplementationHash;

    public CachingResourceHasher(ResourceHasher delegate, PersistentIndexedCache<HashCode, HashCode> persistentCache) {
        this.delegate = delegate;
        this.persistentCache = persistentCache;
        BuildCacheHasher hasher = new DefaultBuildCacheHasher();
        delegate.appendToHasher(hasher);
        this.delegateImplementationHash = hasher.hash().asBytes();
    }

    @Override
    public HashCode hash(RegularFileSnapshot fileSnapshot) {
        HashCode cacheKey = cacheKey(fileSnapshot);

        HashCode hash = persistentCache.get(cacheKey);
        if (hash != null) {
            if (hash.equals(NO_HASH)) {
                return null;
            }
            return hash;
        }

        hash = delegate.hash(fileSnapshot);

        if (hash != null) {
            persistentCache.put(cacheKey, hash);
        } else {
            persistentCache.put(cacheKey, NO_HASH);
        }
        return hash;
    }

    private HashCode cacheKey(RegularFileSnapshot fileSnapshot) {
        BuildCacheHasher hasher = new DefaultBuildCacheHasher();
        hasher.putBytes(delegateImplementationHash);
        hasher.putBytes(fileSnapshot.getContent().getContentMd5().asBytes());
        return hasher.hash();
    }

    @Override
    public HashCode hash(ZipEntry zipEntry, InputStream zipInput) throws IOException {
        return delegate.hash(zipEntry, zipInput);
    }

    @Override
    public void appendToHasher(BuildCacheHasher hasher) {
        delegate.appendToHasher(hasher);
    }
}

@@ -2095,20 +2095,44 @@
 ode 
-cacheKey = c
+resourceHashCacheKey = resourceHashC
 ache
@@ -2168,17 +2168,25 @@
 ashCode 
-h
+resourceH
 ash = pe
@@ -2203,17 +2203,29 @@
 che.get(
-c
+resourceHashC
 acheKey)
@@ -2230,33 +2230,41 @@
 y);%0A        if (
-h
+resourceH
 ash != null) %7B%0A 
@@ -2278,17 +2278,25 @@
     if (
-h
+resourceH
 ash.equa
@@ -2364,33 +2364,41 @@
          return 
-h
+resourceH
 ash;%0A        %7D%0A%0A
@@ -2405,17 +2405,25 @@
         
-h
+resourceH
 ash = de
@@ -2462,17 +2462,25 @@
     if (
-h
+resourceH
 ash != n
@@ -2522,19 +2522,39 @@
 put(
-cacheKey, h
+resourceHashCacheKey, resourceH
 ash)
@@ -2604,17 +2604,29 @@
 che.put(
-c
+resourceHashC
 acheKey,
@@ -2661,17 +2661,25 @@
  return 
-h
+resourceH
 ash;%0A   
@@ -2703,17 +2703,29 @@
 ashCode 
-c
+resourceHashC
 acheKey(

Clarify usage of cache key in CachingResourceHasher