1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.suggest.popular.impl.chronicle; 24 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.FileOutputStream; 28 import java.io.IOException; 29 import java.io.ObjectInputFilter.Status; 30 import java.io.ObjectInputStream; 31 import java.io.ObjectOutputStream; 32 import java.io.Serializable; 33 import java.nio.file.Path; 34 import java.util.logging.Level; 35 import java.util.logging.Logger; 36 37 /** 38 * {@link net.openhft.chronicle.map.ChronicleMap} does not have the configuration stored in the file. Therefore, 39 * other means were necessary to remember the configuration. 40 */ 41 public class ChronicleMapConfiguration implements Serializable { 42 43 private static final long serialVersionUID = -18408710536443827L; 44 45 private static final Logger logger = Logger.getLogger(ChronicleMapConfiguration.class.getName()); 46 47 private static final String FILE_NAME_SUFFIX = "_map.cfg"; 48 49 private int entries; 50 51 private double averageKeySize; 52 ChronicleMapConfiguration(final int entries, final double averageKeySize)53 public ChronicleMapConfiguration(final int entries, final double averageKeySize) { 54 this.entries = entries; 55 this.averageKeySize = averageKeySize; 56 } 57 getEntries()58 public int getEntries() { 59 return entries; 60 } 61 getAverageKeySize()62 public double getAverageKeySize() { 63 return averageKeySize; 64 } 65 setEntries(int entries)66 public void setEntries(int entries) { 67 this.entries = entries; 68 } 69 setAverageKeySize(double averageKeySize)70 public void setAverageKeySize(double averageKeySize) { 71 this.averageKeySize = averageKeySize; 72 } 73 74 /** 75 * Stores this into a file. 76 * @param dir directory where to store the file 77 * @param field field this configuration is for 78 */ save(final Path dir, final String field)79 public void save(final Path dir, final String field) { 80 try (FileOutputStream fos = new FileOutputStream(getFile(dir, field)); 81 ObjectOutputStream oos = new ObjectOutputStream(fos)) { 82 oos.writeObject(this); 83 } catch (IOException e) { 84 logger.log(Level.SEVERE, "Could not save chronicle map configuration", e); 85 } 86 } 87 88 /** 89 * Loads configuration from file. 90 * @param dir directory from which to load the configuration 91 * @param field field this configuration is for 92 * @return loaded configuration or {@code null} if this configuration could not be loaded 93 */ load(final Path dir, final String field)94 public static ChronicleMapConfiguration load(final Path dir, final String field) { 95 File f = getFile(dir, field); 96 if (!f.exists()) { 97 return null; 98 } 99 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) { 100 ois.setObjectInputFilter(filterInfo -> 101 filterInfo.serialClass() == null || filterInfo.serialClass() == ChronicleMapConfiguration.class ? 102 Status.ALLOWED : Status.REJECTED); 103 return (ChronicleMapConfiguration) ois.readObject(); 104 } catch (IOException | ClassNotFoundException e) { 105 logger.log(Level.SEVERE, "Could not load chronicle map configuration", e); 106 } 107 return null; 108 } 109 getFile(final Path dir, final String field)110 private static File getFile(final Path dir, final String field) { 111 return dir.resolve(field + FILE_NAME_SUFFIX).toFile(); 112 } 113 114 } 115