Skip to content

Commit d47694e

Browse files
committed
Remove ByteUnits dep, add ByteUnit enum, add setMapSize() overload
1 parent 167cdad commit d47694e

15 files changed

+228
-109
lines changed

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<agrona.version>1.22.0</agrona.version>
3030
<assertj.version>3.27.6</assertj.version>
3131
<buildnumber-maven-plugin.version>3.2.1</buildnumber-maven-plugin.version>
32-
<byteunits.version>0.9.1</byteunits.version>
3332
<central-publishing-maven-plugin.version>0.9.0</central-publishing-maven-plugin.version>
3433
<fmt-maven-plugin.version>2.29</fmt-maven-plugin.version>
3534
<google-java-format.version>1.28.0</google-java-format.version>
@@ -85,12 +84,6 @@
8584
<version>${guava.version}</version>
8685
<scope>test</scope>
8786
</dependency>
88-
<dependency>
89-
<groupId>com.jakewharton.byteunits</groupId>
90-
<artifactId>byteunits</artifactId>
91-
<version>${byteunits.version}</version>
92-
<scope>test</scope>
93-
</dependency>
9487
<dependency>
9588
<groupId>io.netty</groupId>
9689
<artifactId>netty-buffer</artifactId>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright © 2016-2025 The LmdbJava Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.lmdbjava;
17+
18+
/**
19+
* Simple {@link Enum} for converting various IEC and SI byte units down a number of bytes.
20+
*/
21+
public enum ByteUnit {
22+
23+
BYTES(1L),
24+
25+
/**
26+
* IEC byte unit for 1024 bytes.
27+
*/
28+
KIBIBYTES(1024L),
29+
/**
30+
* IEC byte unit for 1024^2 bytes.
31+
*/
32+
MEBIBYTES(1_0485_76L),
33+
/**
34+
* IEC byte unit for 1024^3 bytes.
35+
*/
36+
GIBIBYTES(1_073_741_824L),
37+
/**
38+
* IEC byte unit for 1024^4 bytes.
39+
*/
40+
TEBIBYTES(1_099_511_627_776L),
41+
/**
42+
* IEC byte unit for 1024^5 bytes.
43+
*/
44+
PEBIBYTES(1_125_899_906_842_624L),
45+
46+
/**
47+
* SI byte unit for 1000 bytes.
48+
*/
49+
KILOBYTES(1_000L),
50+
/**
51+
* SI byte unit for 1000^2 bytes.
52+
*/
53+
MEGABYTES(1_000_000L),
54+
/**
55+
* SI byte unit for 1000^3 bytes.
56+
*/
57+
GIGABYTES(1_000_000_000L),
58+
/**
59+
* SI byte unit for 1000^4 bytes.
60+
*/
61+
TERABYTES(1_000_000_000_000L),
62+
/**
63+
* SI byte unit for 1000^5 bytes.
64+
*/
65+
PETABYTES(1_000_000_000_000_000L),
66+
;
67+
68+
private final long factor;
69+
70+
ByteUnit(long factor) {
71+
this.factor = factor;
72+
}
73+
74+
/**
75+
* Convert the value in this byte unit into bytes.
76+
*
77+
* @param value The value to convert.
78+
* @return The number of bytes.
79+
*/
80+
public long toBytes(final long value) {
81+
return value * factor;
82+
}
83+
84+
/**
85+
* @return The factor to apply when converting this unit into bytes.
86+
*/
87+
public long getFactor() {
88+
return factor;
89+
}
90+
}

src/main/java/org/lmdbjava/Env.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ public final class Env<T> implements AutoCloseable {
6363
*/
6464
public static final boolean SHOULD_CHECK = !getBoolean(DISABLE_CHECKS_PROP);
6565

66-
private static final long KIBIBYTES = 1_024L;
67-
private static final long MEBIBYTES = KIBIBYTES * 1_024L;
68-
private static final long GIBIBYTES = MEBIBYTES * 1_024L;
69-
private static final long TEBIBYTES = GIBIBYTES * 1_024L;
70-
7166
private boolean closed;
7267
private final int maxKeySize;
7368
private final boolean noSubDir;
@@ -120,8 +115,9 @@ public static <T> Builder<T> create(final BufferProxy<T> proxy) {
120115
*/
121116
@Deprecated
122117
public static Env<ByteBuffer> open(final File path, final int size, final EnvFlags... flags) {
118+
123119
return new Builder<>(PROXY_OPTIMAL)
124-
.setMapSize(size * MEBIBYTES)
120+
.setMapSize(size, ByteUnit.MEBIBYTES)
125121
.open(path, flags);
126122
}
127123

@@ -218,9 +214,22 @@ public List<byte[]> getDbiNames() {
218214
* @param mapSize the new size, in bytes
219215
*/
220216
public void setMapSize(final long mapSize) {
217+
if (mapSize < 0) {
218+
throw new IllegalArgumentException("Negative value; overflow?");
219+
}
221220
checkRc(LIB.mdb_env_set_mapsize(ptr, mapSize));
222221
}
223222

223+
/**
224+
* Set the size of the data memory map.
225+
*
226+
* @param mapSize the new size, in bytes
227+
*/
228+
public void setMapSize(final long mapSize, final ByteUnit byteUnit) {
229+
requireNonNull(byteUnit);
230+
setMapSize(byteUnit.toBytes(mapSize));
231+
}
232+
224233
/**
225234
* Get the maximum size of keys and MDB_DUPSORT data we can write.
226235
*
@@ -668,7 +677,8 @@ public AlreadyOpenException() {
668677
public static final class Builder<T> {
669678

670679
static final int MAX_READERS_DEFAULT = 126;
671-
private long mapSize = 1_024 * 1_024;
680+
static final long MAP_SIZE_DEFAULT = ByteUnit.MEBIBYTES.toBytes(1);
681+
private long mapSize = MAP_SIZE_DEFAULT;
672682
private int maxDbs = 1;
673683
private int maxReaders = MAX_READERS_DEFAULT;
674684
private boolean opened;
@@ -772,43 +782,17 @@ public Builder<T> setMapSize(final long mapSize) {
772782
}
773783

774784
/**
775-
* Sets the map size in kibibytes
776-
*
777-
* @param mapSizeKb new limit in kibibytes
778-
* @return the builder
779-
*/
780-
public Builder<T> setMapSizeKb(final long mapSizeKb) {
781-
return setMapSize(mapSizeKb * KIBIBYTES);
782-
}
783-
784-
/**
785-
* Sets the map size in mebibytes.
785+
* Sets the map size in the supplied unit.
786786
*
787-
* @param mapSizeMb new limit in mebibytes.
787+
* @param mapSize new limit in
788788
* @return the builder
789789
*/
790-
public Builder<T> setMapSizeMb(final long mapSizeMb) {
791-
return setMapSize(mapSizeMb * MEBIBYTES);
792-
}
793-
794-
/**
795-
* Sets the map size in gibibytes
796-
*
797-
* @param mapSizeGb new limit in gibibytes
798-
* @return the builder
799-
*/
800-
public Builder<T> setMapSizeGb(final long mapSizeGb) {
801-
return setMapSize(mapSizeGb * GIBIBYTES);
802-
}
803-
804-
/**
805-
* Sets the map size in tebibytes.
806-
*
807-
* @param mapSizeTb new limit in tebibytes.
808-
* @return the builder
809-
*/
810-
public Builder<T> setMapSizeTb(final long mapSizeTb) {
811-
return setMapSize(mapSizeTb * TEBIBYTES);
790+
public Builder<T> setMapSize(final long mapSize, final ByteUnit byteUnit) {
791+
requireNonNull(byteUnit);
792+
if (mapSize < 0) {
793+
throw new IllegalArgumentException("Negative value; overflow?");
794+
}
795+
return setMapSize(byteUnit.toBytes(mapSize));
812796
}
813797

814798
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright © 2016-2025 The LmdbJava Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.lmdbjava;
17+
18+
import org.assertj.core.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
class ByteUnitTest {
22+
23+
@Test
24+
void test() {
25+
Assertions.assertThat(ByteUnit.BYTES.toBytes(2)).isEqualTo(2);
26+
27+
// BYTES
28+
Assertions.assertThat(ByteUnit.BYTES.toBytes(2)).isEqualTo(2L);
29+
Assertions.assertThat(ByteUnit.BYTES.toBytes(0)).isEqualTo(0L);
30+
31+
// IEC Units
32+
Assertions.assertThat(ByteUnit.KIBIBYTES.toBytes(1)).isEqualTo(1024L);
33+
Assertions.assertThat(ByteUnit.KIBIBYTES.toBytes(2)).isEqualTo(2048L);
34+
35+
Assertions.assertThat(ByteUnit.MEBIBYTES.toBytes(1)).isEqualTo(1048576L);
36+
Assertions.assertThat(ByteUnit.MEBIBYTES.toBytes(2)).isEqualTo(2097152L);
37+
38+
Assertions.assertThat(ByteUnit.GIBIBYTES.toBytes(1)).isEqualTo(1073741824L);
39+
Assertions.assertThat(ByteUnit.GIBIBYTES.toBytes(2)).isEqualTo(2147483648L);
40+
41+
Assertions.assertThat(ByteUnit.TEBIBYTES.toBytes(1)).isEqualTo(1099511627776L);
42+
Assertions.assertThat(ByteUnit.TEBIBYTES.toBytes(2)).isEqualTo(2199023255552L);
43+
44+
Assertions.assertThat(ByteUnit.PEBIBYTES.toBytes(1)).isEqualTo(1125899906842624L);
45+
Assertions.assertThat(ByteUnit.PEBIBYTES.toBytes(2)).isEqualTo(2251799813685248L);
46+
47+
// SI Units
48+
Assertions.assertThat(ByteUnit.KILOBYTES.toBytes(1)).isEqualTo(1000L);
49+
Assertions.assertThat(ByteUnit.KILOBYTES.toBytes(2)).isEqualTo(2000L);
50+
51+
Assertions.assertThat(ByteUnit.MEGABYTES.toBytes(1)).isEqualTo(1000000L);
52+
Assertions.assertThat(ByteUnit.MEGABYTES.toBytes(2)).isEqualTo(2000000L);
53+
54+
Assertions.assertThat(ByteUnit.GIGABYTES.toBytes(1)).isEqualTo(1000000000L);
55+
Assertions.assertThat(ByteUnit.GIGABYTES.toBytes(2)).isEqualTo(2000000000L);
56+
57+
Assertions.assertThat(ByteUnit.TERABYTES.toBytes(1)).isEqualTo(1000000000000L);
58+
Assertions.assertThat(ByteUnit.TERABYTES.toBytes(2)).isEqualTo(2000000000000L);
59+
60+
Assertions.assertThat(ByteUnit.PETABYTES.toBytes(1)).isEqualTo(1000000000000000L);
61+
Assertions.assertThat(ByteUnit.PETABYTES.toBytes(2)).isEqualTo(2000000000000000L);
62+
63+
}
64+
}

src/test/java/org/lmdbjava/CursorIterableIntegerDupTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.lmdbjava;
1717

18-
import static com.jakewharton.byteunits.BinaryByteUnit.KIBIBYTES;
1918
import static org.assertj.core.api.Assertions.assertThat;
2019
import static org.lmdbjava.DbiFlags.MDB_CREATE;
2120
import static org.lmdbjava.DbiFlags.MDB_DUPSORT;
@@ -123,7 +122,7 @@ public void before() throws IOException {
123122
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
124123
env =
125124
create(bufferProxy)
126-
.setMapSize(KIBIBYTES.toBytes(256))
125+
.setMapSize(256, ByteUnit.KIBIBYTES)
127126
.setMaxReaders(1)
128127
.setMaxDbs(3)
129128
.setEnvFlags(MDB_NOSUBDIR)

src/test/java/org/lmdbjava/CursorIterableIntegerKeyTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.lmdbjava;
1717

18-
import static com.jakewharton.byteunits.BinaryByteUnit.KIBIBYTES;
1918
import static java.util.Arrays.asList;
2019
import static org.assertj.core.api.Assertions.assertThat;
2120
import static org.lmdbjava.DbiFlags.MDB_CREATE;
@@ -104,7 +103,7 @@ public void before() throws IOException {
104103
file = FileUtil.createTempFile();
105104
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
106105
env = Env.create(bufferProxy)
107-
.setMapSize(KIBIBYTES.toBytes(256))
106+
.setMapSize(256, ByteUnit.KIBIBYTES)
108107
.setMaxReaders(1)
109108
.setMaxDbs(3)
110109
.setEnvFlags(MDB_NOSUBDIR)

src/test/java/org/lmdbjava/CursorIterablePerfTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.lmdbjava;
1717

18-
import static com.jakewharton.byteunits.BinaryByteUnit.GIBIBYTES;
1918
import static org.lmdbjava.DbiFlags.MDB_CREATE;
2019
import static org.lmdbjava.Env.create;
2120
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
@@ -51,7 +50,7 @@ public void before() throws IOException {
5150
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
5251
env =
5352
create(bufferProxy)
54-
.setMapSize(GIBIBYTES.toBytes(1))
53+
.setMapSize(1, ByteUnit.GIBIBYTES)
5554
.setMaxReaders(1)
5655
.setMaxDbs(3)
5756
.setEnvFlags(MDB_NOSUBDIR)

src/test/java/org/lmdbjava/CursorIterableTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.lmdbjava;
1818

19-
import static com.jakewharton.byteunits.BinaryByteUnit.KIBIBYTES;
2019
import static java.util.Arrays.asList;
2120
import static org.assertj.core.api.Assertions.assertThat;
2221
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -98,7 +97,7 @@ void beforeEach() {
9897
file = FileUtil.createTempFile();
9998
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
10099
env = create(bufferProxy)
101-
.setMapSize(KIBIBYTES.toBytes(256))
100+
.setMapSize(256, ByteUnit.KIBIBYTES)
102101
.setMaxReaders(1)
103102
.setMaxDbs(3)
104103
.setEnvFlags(MDB_NOSUBDIR)

src/test/java/org/lmdbjava/CursorParamTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.lmdbjava;
1818

19-
import static com.jakewharton.byteunits.BinaryByteUnit.MEBIBYTES;
2019
import static java.lang.Long.BYTES;
2120
import static java.nio.charset.StandardCharsets.UTF_8;
2221
import static org.assertj.core.api.Assertions.assertThat;
@@ -158,7 +157,7 @@ public final void execute(final Path tmp) {
158157

159158
private Env<T> env(final Path tmp) {
160159
return create(proxy)
161-
.setMapSize(MEBIBYTES.toBytes(1))
160+
.setMapSize(1, ByteUnit.MEBIBYTES)
162161
.setMaxReaders(1)
163162
.setMaxDbs(1)
164163
.setEnvFlags(MDB_NOSUBDIR)

src/test/java/org/lmdbjava/CursorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.lmdbjava;
1818

19-
import static com.jakewharton.byteunits.BinaryByteUnit.MEBIBYTES;
2019
import static java.lang.Long.BYTES;
2120
import static java.lang.Long.MIN_VALUE;
2221
import static java.nio.ByteBuffer.allocateDirect;
@@ -61,7 +60,7 @@ void beforeEach() {
6160
file = FileUtil.createTempFile();
6261
env =
6362
create(PROXY_OPTIMAL)
64-
.setMapSize(MEBIBYTES.toBytes(1))
63+
.setMapSize(1, ByteUnit.MEBIBYTES)
6564
.setMaxReaders(1)
6665
.setMaxDbs(1)
6766
.setEnvFlags(MDB_NOSUBDIR)

0 commit comments

Comments
 (0)