@@ -69,8 +69,8 @@ type Config struct {
6969 ReadCacheStrategy ffi.CacheStrategy
7070}
7171
72- // Note that `FilePath` is not specificied , and must always be set by the user.
73- var Defaults = & Config {
72+ // Note that `FilePath` is not specified , and must always be set by the user.
73+ var Defaults = Config {
7474 CleanCacheSize : 1024 * 1024 , // 1MB
7575 FreeListCacheEntries : 40_000 ,
7676 Revisions : 100 ,
@@ -97,15 +97,20 @@ type Database struct {
9797// Any error during creation will cause the program to exit.
9898func New (config * Config ) * Database {
9999 if config == nil {
100- config = Defaults
100+ log . Crit ( "firewood: config must be provided" )
101101 }
102102
103- fwConfig , err := validatePath (config )
103+ err := validatePath (config . FilePath )
104104 if err != nil {
105105 log .Crit ("firewood: error validating config" , "error" , err )
106106 }
107107
108- fw , err := ffi .New (config .FilePath , fwConfig )
108+ fw , err := ffi .New (config .FilePath , & ffi.Config {
109+ NodeCacheEntries : uint (config .CleanCacheSize ) / 256 , // TODO: estimate 256 bytes per node
110+ FreeListCacheEntries : config .FreeListCacheEntries ,
111+ Revisions : config .Revisions ,
112+ ReadCacheStrategy : config .ReadCacheStrategy ,
113+ })
109114 if err != nil {
110115 log .Crit ("firewood: error creating firewood database" , "error" , err )
111116 }
@@ -124,34 +129,25 @@ func New(config *Config) *Database {
124129 }
125130}
126131
127- func validatePath (trieConfig * Config ) ( * ffi. Config , error ) {
128- if trieConfig . FilePath == "" {
129- return nil , errors .New ("firewood database file path must be set" )
132+ func validatePath (path string ) error {
133+ if path == "" {
134+ return errors .New ("firewood database file path must be set" )
130135 }
131136
132137 // Check that the directory exists
133- dir := filepath .Dir (trieConfig . FilePath )
138+ dir := filepath .Dir (path )
134139 _ , err := os .Stat (dir )
135- if err != nil {
136- if os .IsNotExist (err ) {
137- log .Info ("Database directory not found, creating" , "path" , dir )
138- if err := os .MkdirAll (dir , 0o755 ); err != nil {
139- return nil , fmt .Errorf ("error creating database directory: %w" , err )
140- }
141- } else {
142- return nil , fmt .Errorf ("error checking database directory: %w" , err )
143- }
140+ if err == nil {
141+ return nil // Directory exists
144142 }
145-
146- // Create the Firewood config from the provided config.
147- config := & ffi.Config {
148- NodeCacheEntries : uint (trieConfig .CleanCacheSize ) / 256 , // TODO: estimate 256 bytes per node
149- FreeListCacheEntries : trieConfig .FreeListCacheEntries ,
150- Revisions : trieConfig .Revisions ,
151- ReadCacheStrategy : trieConfig .ReadCacheStrategy ,
143+ if ! os .IsNotExist (err ) {
144+ return fmt .Errorf ("error checking database directory: %w" , err )
152145 }
153-
154- return config , nil
146+ log .Info ("Database directory not found, creating" , "path" , dir )
147+ if err := os .MkdirAll (dir , 0o755 ); err != nil {
148+ return fmt .Errorf ("error creating database directory: %w" , err )
149+ }
150+ return nil
155151}
156152
157153// Scheme returns the scheme of the database.
0 commit comments