diff --git a/cache/config.go b/cache/config.go index 2baf84a..9e9a205 100644 --- a/cache/config.go +++ b/cache/config.go @@ -13,6 +13,7 @@ type cacheOptions struct { BlockRestartInterval int WriteBufferSizeM int BlockSizeK int + MaxFileSizeM int } type coordsCacheOptions struct { @@ -36,6 +37,7 @@ const defaultConfig = ` "WriteBufferSizeM": 64, "BlockSizeK": 0, "MaxOpenFiles": 64, + "MaxFileSizeM": 32, "BlockRestartInterval": 256, "BunchSize": 32, "BunchCacheCapacity": 8096 @@ -45,6 +47,7 @@ const defaultConfig = ` "WriteBufferSizeM": 64, "BlockSizeK": 0, "MaxOpenFiles": 64, + "MaxFileSizeM": 32, "BlockRestartInterval": 128 }, "Ways": { @@ -52,6 +55,7 @@ const defaultConfig = ` "WriteBufferSizeM": 64, "BlockSizeK": 0, "MaxOpenFiles": 64, + "MaxFileSizeM": 32, "BlockRestartInterval": 128 }, "Relations": { @@ -59,6 +63,7 @@ const defaultConfig = ` "WriteBufferSizeM": 64, "BlockSizeK": 0, "MaxOpenFiles": 64, + "MaxFileSizeM": 32, "BlockRestartInterval": 128 }, "CoordsIndex": { @@ -66,6 +71,7 @@ const defaultConfig = ` "WriteBufferSizeM": 128, "BlockSizeK": 0, "MaxOpenFiles": 256, + "MaxFileSizeM": 8, "BlockRestartInterval": 256 }, "WaysIndex": { @@ -73,6 +79,7 @@ const defaultConfig = ` "WriteBufferSizeM": 64, "BlockSizeK": 0, "MaxOpenFiles": 64, + "MaxFileSizeM": 8, "BlockRestartInterval": 128 } } diff --git a/cache/ldb_post_121.go b/cache/ldb_post_121.go new file mode 100644 index 0000000..e316b75 --- /dev/null +++ b/cache/ldb_post_121.go @@ -0,0 +1,18 @@ +// +build ldbpost121 + +package cache + +// #cgo LDFLAGS: -lleveldb +// #include "leveldb/c.h" +import "C" + +import ( + "unsafe" + + "github.com/jmhodges/levigo" +) + +func setMaxFileSize(o *levigo.Options, maxFileSize int) { + p := (*C.struct_leveldb_options_t)(unsafe.Pointer(o.Opt)) + C.leveldb_options_set_max_file_size(p, C.size_t(maxFileSize)) +} diff --git a/cache/ldb_pre_121.go b/cache/ldb_pre_121.go new file mode 100644 index 0000000..c12a23f --- /dev/null +++ b/cache/ldb_pre_121.go @@ -0,0 +1,15 @@ +// +build !ldbpost121 + +package cache + +// #cgo LDFLAGS: -lleveldb +// #include "leveldb/c.h" +import "C" + +import ( + "github.com/jmhodges/levigo" +) + +func setMaxFileSize(o *levigo.Options, maxFileSize int) { + // setMaxFileSize is only available with LevelDB 1.21 and higher. +} diff --git a/cache/osm.go b/cache/osm.go index e8a8f89..58ba864 100644 --- a/cache/osm.go +++ b/cache/osm.go @@ -175,6 +175,11 @@ func (c *cache) open(path string) error { if c.options.BlockSizeK > 0 { opts.SetBlockSize(c.options.BlockSizeK * 1024) } + if c.options.MaxFileSizeM > 0 { + // max file size option is only available with LevelDB 1.21 and higher + // build with -tags="ldppost121" to enable this option. + setMaxFileSize(opts, c.options.MaxFileSizeM*1024*1024) + } db, err := levigo.Open(path, opts) if err != nil { @@ -183,6 +188,7 @@ func (c *cache) open(path string) error { c.db = db c.wo = levigo.NewWriteOptions() c.ro = levigo.NewReadOptions() + return nil } @@ -213,5 +219,4 @@ func (c *cache) Close() { c.cache.Close() c.cache = nil } - }