@@ -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 /**
0 commit comments