Commit e6ad2191 authored by MarkG's avatar MarkG Committed by TIGERs GitLab
Browse files

Resolve "Fix Berkeley size analyzer for recent JDKs"

Closes #1713

See merge request main/Sumatra!1479

sumatra-commit: 47d1dae3bf00b4bacd887eaf33a0df5d5786f764
parent bf3b7d6c
Pipeline #17308 passed with stage
in 12 minutes and 6 seconds
/*
* Copyright (c) 2009 - 2019, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2022, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.export;
import edu.tigers.sumatra.data.collector.IExportable;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
......@@ -15,12 +20,6 @@ import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import edu.tigers.sumatra.data.collector.IExportable;
/**
* With {@link CSVExporter} you can export user-defined values to csv-files on disc.
......@@ -50,9 +49,9 @@ public final class CSVExporter implements Closeable
/**
* @param folder the target folder
* @param folder the target folder
* @param baseFileName subtract-dir and name of exported file without .csv ending
* @param mode the write mode
* @param mode the write mode
*/
public CSVExporter(final String folder, final String baseFileName, final EMode mode)
{
......@@ -62,6 +61,19 @@ public final class CSVExporter implements Closeable
}
/**
* @param folder the target folder
* @param baseFileName subtract-dir and name of exported file without .csv ending
* @param mode the write mode
*/
public CSVExporter(final Path folder, final String baseFileName, final EMode mode)
{
this.folder = folder;
this.mode = mode;
this.fileName = getFileName(baseFileName);
}
private String getFileName(final String baseFileName)
{
switch (mode)
......@@ -94,9 +106,9 @@ public final class CSVExporter implements Closeable
/**
* @param folder the target folder
* @param folder the target folder
* @param fileName the target file name
* @param list the list of data entries
* @param list the list of data entries
*/
public static void exportCollection(
final String folder,
......@@ -115,7 +127,7 @@ public final class CSVExporter implements Closeable
*
* @param values list of values. note: count of values has to match the header
*/
public void addValues(final Collection<? extends Number> values)
public void addValues(final Collection<?> values)
{
try
{
......
/*
* Copyright (c) 2009 - 2019, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2022, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.persistence;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.sleepycat.je.CursorConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
/**
......@@ -67,7 +67,7 @@ public class BerkeleyAccessor<T> implements IBerkeleyAccessor<T>
frameByTimestamp.put(element);
} catch (Exception err)
{
log.error("Could not write element: " + element, err);
log.error("Could not write element: {}", element, err);
}
}
......@@ -84,6 +84,16 @@ public class BerkeleyAccessor<T> implements IBerkeleyAccessor<T>
}
@Override
public synchronized void forEach(Consumer<T> consumer)
{
try (EntityCursor<T> entities = frameByTimestamp.entities(null, CursorConfig.READ_UNCOMMITTED))
{
entities.forEach(consumer);
}
}
@Override
public synchronized Long getNearestKey(final long key)
{
......
/*
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2022, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.persistence;
import edu.tigers.sumatra.model.SumatraModel;
import lombok.extern.log4j.Log4j2;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
......@@ -25,16 +24,17 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import java.util.function.Consumer;
/**
* Berkeley database
*/
@Log4j2
public class BerkeleyDb
{
private static final Logger log = LogManager.getLogger(BerkeleyDb.class.getName());
private final BerkeleyEnv env = new BerkeleyEnv();
private final Path dbPath;
private final Map<Class<?>, IBerkeleyAccessor<?>> accessors = new HashMap<>();
......@@ -65,13 +65,11 @@ public class BerkeleyDb
private String determineFolderName(final File file)
{
try
try (ZipFile zipFile = new ZipFile(file))
{
ZipFile zipFile = new ZipFile(file);
String topLevel = null;
for (Object obj : zipFile.getFileHeaders())
for (FileHeader fh : zipFile.getFileHeaders())
{
FileHeader fh = (FileHeader) obj;
String root = fh.getFileName().split("/")[0];
if (root.equals(topLevel) || topLevel == null)
{
......@@ -83,7 +81,7 @@ public class BerkeleyDb
}
}
return topLevel;
} catch (ZipException e)
} catch (IOException e)
{
log.error("Error while processing zip recording: ", e);
}
......@@ -137,6 +135,12 @@ public class BerkeleyDb
}
public Set<Class<?>> getAccessorTypes()
{
return Collections.unmodifiableSet(accessors.keySet());
}
public <T> T get(Class<T> clazz, long key)
{
IBerkeleyAccessor<T> accessor = getAccessor(clazz);
......@@ -159,6 +163,17 @@ public class BerkeleyDb
}
public <T> void forEach(Class<T> clazz, Consumer<T> consumer)
{
IBerkeleyAccessor<T> accessor = getAccessor(clazz);
if (accessor == null)
{
return;
}
accessor.forEach(consumer);
}
public <T> long size(Class<T> clazz)
{
IBerkeleyAccessor<T> accessor = getAccessor(clazz);
......@@ -200,9 +215,8 @@ public class BerkeleyDb
private void unpackDatabase(final File file, final String folderName)
{
log.info("Unpacking database: {}", file);
try
try (ZipFile zipFile = new ZipFile(file))
{
ZipFile zipFile = new ZipFile(file);
zipFile.extractAll(dbPath.toFile().getParent());
File extracted = Paths.get(dbPath.toFile().getParent(), folderName).toFile();
......
/*
* Copyright (c) 2009 - 2018, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2022, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.persistence;
import com.sleepycat.persist.EntityStore;
import java.util.Collection;
import java.util.List;
import com.sleepycat.persist.EntityStore;
import java.util.function.Consumer;
/**
* Accessor interface to retrieve elements from berkeley db
*
*
* @param <T> the element type of this storage
*/
public interface IBerkeleyAccessor<T>
{
/**
* Open the accessor
*
*
* @param entityStore
*/
void open(EntityStore entityStore);
/**
* @return the number of elements stored
*/
long size();
/**
* Load all stored elements. Use this carefully, as there might not be sufficient memory!
*
* @return all stored elements
*/
List<T> load();
/**
* @return action to apply on each entity
*/
void forEach(Consumer<T> consumer);
/**
* Write elements to database
*
*
* @param elements
*/
void write(final Collection<T> elements);
/**
* Write a single element to database
*
*
* @param element
*/
void write(T element);
/**
* @param key
* @return the element nearest to given key
*/
T get(long key);
/**
* @return the very first (smallest) key in this storage
*/
Long getFirstKey();
/**
* @return the very last (largest) key in this storage
*/
Long getLastKey();
/**
* @param key some key
* @return the nearest key to the given one
*/
Long getNearestKey(long key);
/**
* @return the key after given key
*/
Long getNextKey(long key);
/**
* @return the key before given key
*/
Long getPreviousKey(long key);
/**
* @return true, if the key is a Sumatra timestamp
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment