test_class.declarator: COMPRESSION = 100
test_class.modifier: private final static
test_class.original_string: private final static short COMPRESSION = 100;
test_class.type: short
test_class.var_name: COMPRESSION
test_class.declarator: key = makeKey("mapLoaderTest")
test_class.modifier: private
test_class.original_string: private HistogramKey key = makeKey("mapLoaderTest");
test_class.type: HistogramKey
test_class.var_name: key
test_class.declarator: digest = new AgentDigest(COMPRESSION, 100L)
test_class.modifier: private
test_class.original_string: private AgentDigest digest = new AgentDigest(COMPRESSION, 100L);
test_class.type: AgentDigest
test_class.var_name: digest
test_class.declarator: file
test_class.modifier: private
test_class.original_string: private File file;
test_class.type: File
test_class.var_name: file
test_class.declarator: loader
test_class.modifier: private
test_class.original_string: private MapLoader<HistogramKey, AgentDigest, HistogramKeyMarshaller, AgentDigestMarshaller> loader;
test_class.type: MapLoader<HistogramKey, AgentDigest, HistogramKeyMarshaller, AgentDigestMarshaller>
test_class.var_name: loader
test_class.file: proxy/src/test/java/com/wavefront/agent/histogram/MapLoaderTest.java
test_class.identifier: MapLoaderTest
test_case.body: @Test
    public void testFileDoesNotExist() throws Exception {
      file.delete();
      ConcurrentMap<HistogramKey, AgentDigest> map = loader.get(file);
      assertThat(((VanillaChronicleMap)map).file()).isNotNull();
      testPutRemove(map);
    }
test_case.class_method_signature: MapLoaderTest.testFileDoesNotExist()
test_case.full_signature: @Test public void testFileDoesNotExist()
test_case.identifier: testFileDoesNotExist
test_case.invocations: delete get isNotNull assertThat file testPutRemove
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testFileDoesNotExist()
focal_class.declarator: logger = Logger.getLogger(MapLoader.class.getCanonicalName())
focal_class.modifier: private static final
focal_class.original_string: private static final Logger logger = Logger.getLogger(MapLoader.class.getCanonicalName());
focal_class.type: Logger
focal_class.var_name: logger
focal_class.declarator: MAX_BLOAT_FACTOR = 1000
focal_class.modifier: private static final
focal_class.original_string: private static final double MAX_BLOAT_FACTOR = 1000;
focal_class.type: double
focal_class.var_name: MAX_BLOAT_FACTOR
focal_class.declarator: JSON_PARSER = new ObjectMapper()
focal_class.modifier: private static final
focal_class.original_string: private static final ObjectMapper JSON_PARSER = new ObjectMapper();
focal_class.type: ObjectMapper
focal_class.var_name: JSON_PARSER
focal_class.declarator: keyClass
focal_class.modifier: private final
focal_class.original_string: private final Class<K> keyClass;
focal_class.type: Class<K>
focal_class.var_name: keyClass
focal_class.declarator: valueClass
focal_class.modifier: private final
focal_class.original_string: private final Class<V> valueClass;
focal_class.type: Class<V>
focal_class.var_name: valueClass
focal_class.declarator: entries
focal_class.modifier: private final
focal_class.original_string: private final long entries;
focal_class.type: long
focal_class.var_name: entries
focal_class.declarator: avgKeySize
focal_class.modifier: private final
focal_class.original_string: private final double avgKeySize;
focal_class.type: double
focal_class.var_name: avgKeySize
focal_class.declarator: avgValueSize
focal_class.modifier: private final
focal_class.original_string: private final double avgValueSize;
focal_class.type: double
focal_class.var_name: avgValueSize
focal_class.declarator: keyMarshaller
focal_class.modifier: private final
focal_class.original_string: private final KM keyMarshaller;
focal_class.type: KM
focal_class.var_name: keyMarshaller
focal_class.declarator: valueMarshaller
focal_class.modifier: private final
focal_class.original_string: private final VM valueMarshaller;
focal_class.type: VM
focal_class.var_name: valueMarshaller
focal_class.declarator: doPersist
focal_class.modifier: private final
focal_class.original_string: private final boolean doPersist;
focal_class.type: boolean
focal_class.var_name: doPersist
focal_class.declarator: maps =
        CacheBuilder.newBuilder().build(new CacheLoader<File, ChronicleMap<K, V>>() {
  
          private ChronicleMap<K, V> newPersistedMap(File file) throws IOException {
            return ChronicleMap.of(keyClass, valueClass)
                .keyMarshaller(keyMarshaller)
                .valueMarshaller(valueMarshaller)
                .entries(entries)
                .averageKeySize(avgKeySize)
                .averageValueSize(avgValueSize)
                .maxBloatFactor(MAX_BLOAT_FACTOR)
                .createPersistedTo(file);
          }
  
          private ChronicleMap<K, V> newInMemoryMap() {
            return ChronicleMap.of(keyClass, valueClass)
                .keyMarshaller(keyMarshaller)
                .valueMarshaller(valueMarshaller)
                .entries(entries)
                .averageKeySize(avgKeySize)
                .averageValueSize(avgValueSize)
                .maxBloatFactor(MAX_BLOAT_FACTOR)
                .create();
          }
  
          private MapSettings loadSettings(File file) throws IOException {
            return JSON_PARSER.readValue(new FileReader(file), MapSettings.class);
          }
  
          private void saveSettings(MapSettings settings, File file) throws IOException {
            Writer writer = new FileWriter(file);
            JSON_PARSER.writeValue(writer, settings);
            writer.close();
          }
  
          @Override
          public ChronicleMap<K, V> load(@Nonnull File file) throws Exception {
            if (!doPersist) {
              logger.log(Level.WARNING, "Accumulator persistence is disabled, unflushed histograms " +
                  "will be lost on proxy shutdown.");
              return newInMemoryMap();
            }
  
            MapSettings newSettings = new MapSettings(entries, avgKeySize, avgValueSize);
            File settingsFile = new File(file.getAbsolutePath().concat(".settings"));
            try {
              if (file.exists()) {
                if (settingsFile.exists()) {
                  MapSettings settings = loadSettings(settingsFile);
                  if (!settings.equals(newSettings)) {
                    logger.info(file.getName() + " settings changed, reconfiguring (this may take a few moments)...");
                    File originalFile = new File(file.getAbsolutePath());
                    File oldFile = new File(file.getAbsolutePath().concat(".temp"));
                    if (oldFile.exists()) {
                      //noinspection ResultOfMethodCallIgnored
                      oldFile.delete();
                    }
                    //noinspection ResultOfMethodCallIgnored
                    file.renameTo(oldFile);
  
                    ChronicleMap<K, V> toMigrate = ChronicleMap
                        .of(keyClass, valueClass)
                        .entries(settings.getEntries())
                        .averageKeySize(settings.getAvgKeySize())
                        .averageValueSize(settings.getAvgValueSize())
                        .recoverPersistedTo(oldFile, false);
  
                    ChronicleMap<K, V> result = newPersistedMap(originalFile);
  
                    if (toMigrate.size() > 0) {
                      logger.info(originalFile.getName() + " starting data migration (" + toMigrate.size() + " records)");
                      for (K key : toMigrate.keySet()) {
                        result.put(key, toMigrate.get(key));
                      }
                      toMigrate.close();
                      logger.info(originalFile.getName() + " data migration finished");
                    }
  
                    saveSettings(newSettings, settingsFile);
                    //noinspection ResultOfMethodCallIgnored
                    oldFile.delete();
                    logger.info(originalFile.getName() + " reconfiguration finished");
  
                    return result;
                  }
                }
  
                logger.fine("Restoring accumulator state from " + file.getAbsolutePath());
                // Note: this relies on an uncorrupted header, which according to the docs would be due to a hardware error or fs bug.
                ChronicleMap<K, V> result = ChronicleMap
                    .of(keyClass, valueClass)
                    .entries(entries)
                    .averageKeySize(avgKeySize)
                    .averageValueSize(avgValueSize)
                    .recoverPersistedTo(file, false);
  
                if (result.isEmpty()) {
                  // Create a new map with the supplied settings to be safe.
                  result.close();
                  //noinspection ResultOfMethodCallIgnored
                  file.delete();
                  logger.fine("Empty accumulator - reinitializing: " + file.getName());
                  result = newPersistedMap(file);
                } else {
                  // Note: as of 3.10 all instances are.
                  if (result instanceof VanillaChronicleMap) {
                    logger.fine("Accumulator map restored from " + file.getAbsolutePath());
                    VanillaChronicleMap vcm = (VanillaChronicleMap) result;
                    if (!vcm.keyClass().equals(keyClass) ||
                        !vcm.valueClass().equals(valueClass)) {
                      throw new IllegalStateException("Persisted map params are not matching expected map params "
                          + " key " + "exp: " + keyClass.getSimpleName() + " act: " + vcm.keyClass().getSimpleName()
                          + " val " + "exp: " + valueClass.getSimpleName() + " act: " + vcm.valueClass().getSimpleName());
                    }
                  }
                }
                saveSettings(newSettings, settingsFile);
                return result;
  
              } else {
                logger.fine("Accumulator map initialized as " + file.getName());
                saveSettings(newSettings, settingsFile);
                return newPersistedMap(file);
              }
            } catch (Exception e) {
              logger.log(Level.SEVERE, "Failed to load/create map from '" + file.getAbsolutePath() +
                      "'. Please move or delete the file and restart the proxy! Reason: ", e);
              throw new RuntimeException(e);
            }
          }
        })
focal_class.modifier: private final
focal_class.original_string: private final LoadingCache<File, ChronicleMap<K, V>> maps =
        CacheBuilder.newBuilder().build(new CacheLoader<File, ChronicleMap<K, V>>() {
  
          private ChronicleMap<K, V> newPersistedMap(File file) throws IOException {
            return ChronicleMap.of(keyClass, valueClass)
                .keyMarshaller(keyMarshaller)
                .valueMarshaller(valueMarshaller)
                .entries(entries)
                .averageKeySize(avgKeySize)
                .averageValueSize(avgValueSize)
                .maxBloatFactor(MAX_BLOAT_FACTOR)
                .createPersistedTo(file);
          }
  
          private ChronicleMap<K, V> newInMemoryMap() {
            return ChronicleMap.of(keyClass, valueClass)
                .keyMarshaller(keyMarshaller)
                .valueMarshaller(valueMarshaller)
                .entries(entries)
                .averageKeySize(avgKeySize)
                .averageValueSize(avgValueSize)
                .maxBloatFactor(MAX_BLOAT_FACTOR)
                .create();
          }
  
          private MapSettings loadSettings(File file) throws IOException {
            return JSON_PARSER.readValue(new FileReader(file), MapSettings.class);
          }
  
          private void saveSettings(MapSettings settings, File file) throws IOException {
            Writer writer = new FileWriter(file);
            JSON_PARSER.writeValue(writer, settings);
            writer.close();
          }
  
          @Override
          public ChronicleMap<K, V> load(@Nonnull File file) throws Exception {
            if (!doPersist) {
              logger.log(Level.WARNING, "Accumulator persistence is disabled, unflushed histograms " +
                  "will be lost on proxy shutdown.");
              return newInMemoryMap();
            }
  
            MapSettings newSettings = new MapSettings(entries, avgKeySize, avgValueSize);
            File settingsFile = new File(file.getAbsolutePath().concat(".settings"));
            try {
              if (file.exists()) {
                if (settingsFile.exists()) {
                  MapSettings settings = loadSettings(settingsFile);
                  if (!settings.equals(newSettings)) {
                    logger.info(file.getName() + " settings changed, reconfiguring (this may take a few moments)...");
                    File originalFile = new File(file.getAbsolutePath());
                    File oldFile = new File(file.getAbsolutePath().concat(".temp"));
                    if (oldFile.exists()) {
                      //noinspection ResultOfMethodCallIgnored
                      oldFile.delete();
                    }
                    //noinspection ResultOfMethodCallIgnored
                    file.renameTo(oldFile);
  
                    ChronicleMap<K, V> toMigrate = ChronicleMap
                        .of(keyClass, valueClass)
                        .entries(settings.getEntries())
                        .averageKeySize(settings.getAvgKeySize())
                        .averageValueSize(settings.getAvgValueSize())
                        .recoverPersistedTo(oldFile, false);
  
                    ChronicleMap<K, V> result = newPersistedMap(originalFile);
  
                    if (toMigrate.size() > 0) {
                      logger.info(originalFile.getName() + " starting data migration (" + toMigrate.size() + " records)");
                      for (K key : toMigrate.keySet()) {
                        result.put(key, toMigrate.get(key));
                      }
                      toMigrate.close();
                      logger.info(originalFile.getName() + " data migration finished");
                    }
  
                    saveSettings(newSettings, settingsFile);
                    //noinspection ResultOfMethodCallIgnored
                    oldFile.delete();
                    logger.info(originalFile.getName() + " reconfiguration finished");
  
                    return result;
                  }
                }
  
                logger.fine("Restoring accumulator state from " + file.getAbsolutePath());
                // Note: this relies on an uncorrupted header, which according to the docs would be due to a hardware error or fs bug.
                ChronicleMap<K, V> result = ChronicleMap
                    .of(keyClass, valueClass)
                    .entries(entries)
                    .averageKeySize(avgKeySize)
                    .averageValueSize(avgValueSize)
                    .recoverPersistedTo(file, false);
  
                if (result.isEmpty()) {
                  // Create a new map with the supplied settings to be safe.
                  result.close();
                  //noinspection ResultOfMethodCallIgnored
                  file.delete();
                  logger.fine("Empty accumulator - reinitializing: " + file.getName());
                  result = newPersistedMap(file);
                } else {
                  // Note: as of 3.10 all instances are.
                  if (result instanceof VanillaChronicleMap) {
                    logger.fine("Accumulator map restored from " + file.getAbsolutePath());
                    VanillaChronicleMap vcm = (VanillaChronicleMap) result;
                    if (!vcm.keyClass().equals(keyClass) ||
                        !vcm.valueClass().equals(valueClass)) {
                      throw new IllegalStateException("Persisted map params are not matching expected map params "
                          + " key " + "exp: " + keyClass.getSimpleName() + " act: " + vcm.keyClass().getSimpleName()
                          + " val " + "exp: " + valueClass.getSimpleName() + " act: " + vcm.valueClass().getSimpleName());
                    }
                  }
                }
                saveSettings(newSettings, settingsFile);
                return result;
  
              } else {
                logger.fine("Accumulator map initialized as " + file.getName());
                saveSettings(newSettings, settingsFile);
                return newPersistedMap(file);
              }
            } catch (Exception e) {
              logger.log(Level.SEVERE, "Failed to load/create map from '" + file.getAbsolutePath() +
                      "'. Please move or delete the file and restart the proxy! Reason: ", e);
              throw new RuntimeException(e);
            }
          }
        });
focal_class.type: LoadingCache<File, ChronicleMap<K, V>>
focal_class.var_name: maps
focal_class.file: proxy/src/main/java/com/wavefront/agent/histogram/MapLoader.java
focal_class.identifier: MapLoader
focal_class.class_method_signature: MapLoader.MapLoader(Class<K> keyClass,
                     Class<V> valueClass,
                     long entries,
                     double avgKeySize,
                     double avgValueSize,
                     KM keyMarshaller,
                     VM valueMarshaller,
                     boolean doPersist)
focal_class.full_signature: public  MapLoader(Class<K> keyClass,
                     Class<V> valueClass,
                     long entries,
                     double avgKeySize,
                     double avgValueSize,
                     KM keyMarshaller,
                     VM valueMarshaller,
                     boolean doPersist)
focal_class.identifier: MapLoader
focal_class.modifiers: public
focal_class.parameters: (Class<K> keyClass,
                     Class<V> valueClass,
                     long entries,
                     double avgKeySize,
                     double avgValueSize,
                     KM keyMarshaller,
                     VM valueMarshaller,
                     boolean doPersist)
focal_class.signature:  MapLoader(Class<K> keyClass,
                     Class<V> valueClass,
                     long entries,
                     double avgKeySize,
                     double avgValueSize,
                     KM keyMarshaller,
                     VM valueMarshaller,
                     boolean doPersist)
focal_class.class_method_signature: MapLoader.get(File f)
focal_class.full_signature: public ChronicleMap<K, V> get(File f)
focal_class.identifier: get
focal_class.modifiers: public
focal_class.parameters: (File f)
focal_class.return: ChronicleMap<K, V>
focal_class.signature: ChronicleMap<K, V> get(File f)
focal_class.class_method_signature: MapLoader.toString()
focal_class.full_signature: @Override public String toString()
focal_class.identifier: toString
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String toString()
focal_method.body: public ChronicleMap<K, V> get(File f) throws Exception {
      Preconditions.checkNotNull(f);
      return maps.get(f);
    }
focal_method.class_method_signature: MapLoader.get(File f)
focal_method.full_signature: public ChronicleMap<K, V> get(File f)
focal_method.identifier: get
focal_method.invocations: checkNotNull get
focal_method.modifiers: public
focal_method.parameters: (File f)
focal_method.return: ChronicleMap<K, V>
focal_method.signature: ChronicleMap<K, V> get(File f)