Skip to content

Commit 972ae83

Browse files
Merlin Rabensmerlinrabens
authored andcommitted
Add MapDBRepository
1 parent 9943d3c commit 972ae83

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.intenthq.action_processor.integrationsV2.config
2+
3+
import java.nio.file.{Path, Paths}
4+
5+
case class MapDBSettings(dbPath: Path, startDbSize: Long, incSize: Long, segments: Int, nodeSize: Int, levels: Int)
6+
7+
object MapDBSettings {
8+
9+
val Default: MapDBSettings = MapDBSettings(
10+
dbPath = Paths.get("/tmp"),
11+
startDbSize = 5L,
12+
incSize = 1L,
13+
segments = 16,
14+
nodeSize = 128,
15+
levels = 4
16+
)
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.intenthq.action_processor.integrationsV2.repositories
2+
3+
import java.io.File
4+
import java.nio.file.Paths
5+
import java.time.LocalDateTime
6+
7+
import cats.effect.{IO, Resource}
8+
import com.intenthq.action_processor.integrationsV2.config.MapDBSettings
9+
import org.mapdb.{DB, DBMaker}
10+
11+
object MapDBRepository {
12+
def load(mapDBSettings: MapDBSettings): Resource[IO, DB] = {
13+
val dbCreated = for {
14+
now <- IO.delay(LocalDateTime.now())
15+
dbFile <- IO.delay(new File(Paths.get(mapDBSettings.dbPath.toString, s"dbb-${now.toLocalDate}-${now.toLocalTime}").toUri))
16+
_ <- IO.delay(dbFile.deleteOnExit())
17+
createDb <- IO.delay {
18+
DBMaker
19+
.fileDB(dbFile.getAbsolutePath)
20+
.allocateStartSize(mapDBSettings.startDbSize * 1024 * 1024 * 1024)
21+
.allocateIncrement(mapDBSettings.incSize * 1024 * 1024 * 1024)
22+
.fileMmapEnableIfSupported()
23+
.fileMmapPreclearDisable()
24+
.closeOnJvmShutdown()
25+
.fileDeleteAfterClose()
26+
.make()
27+
}
28+
} yield createDb
29+
Resource.make(dbCreated)(db => IO.delay(db.close()))
30+
}
31+
}

0 commit comments

Comments
 (0)