public boolean setEntryValid(CacheEntry cacheEntry, FetchWork fetchWork) {
    Path queryResultsPath = null;
    Path cachedResultsPath = null;

    try {
      // if we are here file sink op should have created files to fetch from
      assert(fetchWork.getFilesToFetch() != null );

      boolean requiresCaching = true;
      queryResultsPath = fetchWork.getTblDir();
      FileSystem resultsFs = queryResultsPath.getFileSystem(conf);

      long resultSize = 0;
      for(FileStatus fs:fetchWork.getFilesToFetch()) {
        if(resultsFs.exists(fs.getPath())) {
          resultSize +=  fs.getLen();
        } else {
          // No actual result directory, no need to cache anything.
          requiresCaching = false;
          break;
        }
      }

      if (!shouldEntryBeAdded(cacheEntry, resultSize)) {
        return false;
      }

      // Synchronize on the cache entry so that no one else can invalidate this entry
      // while we are in the process of setting it to valid.
      synchronized (cacheEntry) {
        if (cacheEntry.getStatus() == CacheEntryStatus.INVALID) {
          // Entry either expired, or was invalidated due to table updates
          return false;
        }

        if (requiresCaching) {
          cacheEntry.cachedResultPaths = new HashSet<>();
            for(FileStatus fs:fetchWork.getFilesToFetch()) {
              cacheEntry.cachedResultPaths.add(fs);
            }
          LOG.info("Cached query result paths located at {} (size {}) for query '{}'",
              queryResultsPath, resultSize, cacheEntry.getQueryText());
        }

        // Create a new FetchWork to reference the new cache location.
        FetchWork fetchWorkForCache =
            new FetchWork(fetchWork.getTblDir(), fetchWork.getTblDesc(), fetchWork.getLimit());
        fetchWorkForCache.setCachedResult(true);
        fetchWorkForCache.setFilesToFetch(fetchWork.getFilesToFetch());
        cacheEntry.fetchWork = fetchWorkForCache;
        //cacheEntry.cachedResultsPath = cachedResultsPath;
        cacheEntry.size = resultSize;
        this.cacheSize += resultSize;

        cacheEntry.setStatus(CacheEntryStatus.VALID);
        // Mark this entry as being in use. Caller will need to release later.
        cacheEntry.addReader();

        scheduleEntryInvalidation(cacheEntry);

        // Notify any queries waiting on this cacheEntry to become valid.
        cacheEntry.notifyAll();
      }

      incrementMetric(MetricsConstant.QC_VALID_ENTRIES);
      incrementMetric(MetricsConstant.QC_TOTAL_ENTRIES_ADDED);
    } catch (Exception err) {
      String queryText = cacheEntry.getQueryText();
      LOG.error("Failed to create cache entry for query results for query: " + queryText, err);
      cacheEntry.size = 0;
      cacheEntry.cachedResultsPath = null;
      // Invalidate the entry. Rely on query cleanup to remove from lookup.
      cacheEntry.invalidate();
      return false;
    }

    return true;
  }
Updates a pending cache entry with a FetchWork result from a finished query.
If successful the cache entry will be set to valid status and be usable for cached queries.
Important: Adding the entry to the cache will increment the reader count for the cache entry.
CacheEntry.releaseReader() should be called when the caller is done with the cache entry.
@param cacheEntry
@param fetchWork
@return
ql/src/java/org/apache/hadoop/hive/ql/cache/results/QueryResultsCache.java