Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.codeforces.inmemo</groupId>
<artifactId>inmemo</artifactId>
<version>2.0.2-SNAPSHOT</version>
<version>2.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>inmemo</name>
<distributionManagement>
Expand Down Expand Up @@ -46,6 +46,7 @@
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.argLine>-Dfile.encoding=UTF-8 -Xmx1200M</surefire.argLine>
</properties>
<build>
<plugins>
Expand Down Expand Up @@ -76,16 +77,22 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
<argLine>-Xmx1200M</argLine>
<!-- Uncomment to build with JDK 9+ -->
<!--
<argLine>&#45;&#45;add&#45;opens java.base/java.lang=ALL&#45;UNNAMED &#45;&#45;add&#45;opens java.base/java.util=ALL&#45;UNNAMED &#45;&#45;add&#45;opens java.base/java.io=ALL&#45;UNNAMED</argLine>
-->
<argLine>${surefire.argLine}</argLine>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>jdk9-plus-tests</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<surefire.argLine>-Dfile.encoding=UTF-8 -Xmx1200M --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED</surefire.argLine>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.xerial.snappy</groupId>
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/codeforces/inmemo/JournalFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.codeforces.inmemo;

import org.apache.log4j.Logger;
import org.xerial.snappy.Snappy;

import java.nio.charset.StandardCharsets;

final class JournalFormat {
static final byte[] MAGIC = "INMEMO_JOURNAL_2".getBytes(StandardCharsets.US_ASCII);
static final int VERSION = 1;

static final int MAX_BLOCK_ROWS = 1 << 20;
static final int MAX_BLOCK_RAW_BYTES = 128 * 1024 * 1024;
static final int MAX_BLOCK_COMPRESSED_BYTES = Snappy.maxCompressedLength(MAX_BLOCK_RAW_BYTES);
static final int MAX_HEADER_STRING_BYTES = 65_536;

static final int DEFAULT_BLOCK_ROWS = 65_536;
static final int DEFAULT_TARGET_RAW_BYTES = 64 * 1024 * 1024;
static final int MIN_TARGET_RAW_BYTES = 1 * 1024 * 1024;

static final String BLOCK_ROWS_PROPERTY = "Inmemo.JournalBlockRows";
static final String TARGET_RAW_BYTES_PROPERTY = "Inmemo.JournalBlockTargetBytes";
static final String MAX_AGE_HOURS_PROPERTY = "Inmemo.JournalMaxAgeHours";

private static final long DEFAULT_MAX_AGE_HOURS = 36L;

private JournalFormat() {
// No operations.
}

static int getBlockRows(Logger logger) {
return getIntProperty(logger, BLOCK_ROWS_PROPERTY, DEFAULT_BLOCK_ROWS, 1, MAX_BLOCK_ROWS);
}

static int getTargetRawBytes(Logger logger) {
return getIntProperty(logger, TARGET_RAW_BYTES_PROPERTY, DEFAULT_TARGET_RAW_BYTES,
MIN_TARGET_RAW_BYTES, MAX_BLOCK_RAW_BYTES / 2);
}

static long getMaxAgeMillis(Logger logger) {
long hours = getLongProperty(logger, MAX_AGE_HOURS_PROPERTY, DEFAULT_MAX_AGE_HOURS, 1L, 24L * 365L);
return hours * 60L * 60L * 1000L;
}

private static int getIntProperty(Logger logger, String name, int defaultValue, int minValue, int maxValue) {
String value = System.getProperty(name);
if (value == null) {
return defaultValue;
}

try {
int parsedValue = Integer.parseInt(value.trim());
if (parsedValue < minValue) {
logger.warn("Property " + name + "=" + value + " is too small, using " + minValue + '.');
return minValue;
}
if (parsedValue > maxValue) {
logger.warn("Property " + name + "=" + value + " is too large, using " + maxValue + '.');
return maxValue;
}
return parsedValue;
} catch (NumberFormatException e) {
logger.warn("Property " + name + "=" + value + " is invalid, using " + defaultValue + '.', e);
return defaultValue;
}
}

private static long getLongProperty(Logger logger, String name, long defaultValue, long minValue, long maxValue) {
String value = System.getProperty(name);
if (value == null) {
return defaultValue;
}

try {
long parsedValue = Long.parseLong(value.trim());
if (parsedValue < minValue) {
logger.warn("Property " + name + "=" + value + " is too small, using " + minValue + '.');
return minValue;
}
if (parsedValue > maxValue) {
logger.warn("Property " + name + "=" + value + " is too large, using " + maxValue + '.');
return maxValue;
}
return parsedValue;
} catch (NumberFormatException e) {
logger.warn("Property " + name + "=" + value + " is invalid, using " + defaultValue + '.', e);
return defaultValue;
}
}
}
Loading
Loading