$darkmode
DENOPTIM
SizeControlledSet.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.utils;
20
21import java.io.File;
22import java.io.IOException;
23import java.util.Collections;
24import java.util.HashSet;
25import java.util.Set;
26
27import denoptim.exception.DENOPTIMException;
28import denoptim.files.FileUtils;
29import denoptim.io.DenoptimIO;
30
40{
45 private int maxSize;
46
51 private File dataOnDisk;
52
56 private File allData;
57
61 private boolean usingDisk = false;
62
66 private int entriesInFile = 0;
67
71 private Set<String> data;
72
73//------------------------------------------------------------------------------
74
84 public SizeControlledSet(int maxSize, String memoryFile, String allUIDsFile)
85 {
86 this.maxSize = maxSize;
87 this.dataOnDisk = new File(memoryFile);
88 if (allUIDsFile!=null)
89 this.allData = new File(allUIDsFile);
90 data = Collections.synchronizedSet(new HashSet<String>());
91 }
92
93//------------------------------------------------------------------------------
94
106 public synchronized boolean addNewUniqueEntry(String entry) throws IOException
107 {
108 synchronized (data)
109 {
110 boolean wasNew = false;
111 if (usingDisk)
112 {
113 if (data.contains(entry))
114 {
115 return false;
116 }
117 wasNew = !FileUtils.isLineInTxtFile(entry, dataOnDisk, true);
118 if (wasNew)
119 {
121 }
122 } else {
123 wasNew = data.add(entry);
124 if (data.size()>=maxSize)
125 usingDisk = true;
126 }
127 if (wasNew && allData!=null)
128 {
129 try
130 {
131 DenoptimIO.writeData(allData.getAbsolutePath(), entry,
132 true);
133 } catch (DENOPTIMException e)
134 {
135 throw ((IOException) e.getCause());
136 }
137 }
138 return wasNew;
139 }
140 }
141
142//------------------------------------------------------------------------------
143
152 public synchronized boolean contains(String entry) throws IOException
153 {
154 synchronized (data)
155 {
156 boolean foundInMemory = data.contains(entry);
157 boolean foundInDisk = false;
158 if (usingDisk && !foundInMemory)
159 {
160 foundInDisk = FileUtils.isLineInTxtFile(entry, dataOnDisk,
161 false);
162 }
163 return foundInMemory || foundInDisk;
164 }
165 }
166
167//------------------------------------------------------------------------------
168
173 public synchronized int size()
174 {
175 synchronized (data)
176 {
177 return data.size() + entriesInFile;
178 }
179 }
180
181//------------------------------------------------------------------------------
182
183}
static boolean isLineInTxtFile(String query, File file, boolean add)
Search in a file for a line matching the given string query.
Definition: FileUtils.java:132
Utility methods for input/output.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Class meant to collect unique strings without leading to memory overflow.
synchronized int size()
Returns the number of unique entries.
int entriesInFile
Number of entries written to file.
synchronized boolean contains(String entry)
Checks if an entry is contained in this collection.
File allData
The file used to store all entries on disk.
File dataOnDisk
The file used to deal with entries that do not fit in the maximum size of this set.
synchronized boolean addNewUniqueEntry(String entry)
Checks if the given entry is already container in the set of known entries and, if not,...
SizeControlledSet(int maxSize, String memoryFile, String allUIDsFile)
Constructor for a size-controlled storage of unique Strings.
boolean usingDisk
Flag signalling the use of the disk.
Set< String > data
The actual data collection.
int maxSize
Maximum size of the set.