@@ -68,8 +68,8 @@ type Config struct {
6868 ReadCacheStrategy ffi.CacheStrategy
6969}
7070
71- // Note that `FilePath` is not specificied , and must always be set by the user.
72- var Defaults = & Config {
71+ // Note that `FilePath` is not specified , and must always be set by the user.
72+ var Defaults = Config {
7373 CleanCacheSize : 1024 * 1024 , // 1MB
7474 FreeListCacheEntries : 40_000 ,
7575 Revisions : 100 ,
@@ -96,15 +96,20 @@ type Database struct {
9696// Any error during creation will cause the program to exit.
9797func New (config * Config ) * Database {
9898 if config == nil {
99- config = Defaults
99+ log . Crit ( "firewood: config must be provided" )
100100 }
101101
102- fwConfig , err := validatePath (config )
102+ err := validatePath (config . FilePath )
103103 if err != nil {
104104 log .Crit ("firewood: error validating config" , "error" , err )
105105 }
106106
107- fw , err := ffi .New (config .FilePath , fwConfig )
107+ fw , err := ffi .New (config .FilePath , & ffi.Config {
108+ NodeCacheEntries : uint (config .CleanCacheSize ) / 256 , // TODO: estimate 256 bytes per node
109+ FreeListCacheEntries : config .FreeListCacheEntries ,
110+ Revisions : config .Revisions ,
111+ ReadCacheStrategy : config .ReadCacheStrategy ,
112+ })
108113 if err != nil {
109114 log .Crit ("firewood: error creating firewood database" , "error" , err )
110115 }
@@ -123,33 +128,25 @@ func New(config *Config) *Database {
123128 }
124129}
125130
126- func validatePath (trieConfig * Config ) ( * ffi. Config , error ) {
127- if trieConfig . FilePath == "" {
128- return nil , errors .New ("firewood database file path must be set" )
131+ func validatePath (path string ) error {
132+ if path == "" {
133+ return errors .New ("firewood database file path must be set" )
129134 }
130135
131136 // Check that the directory exists
132- dir := filepath .Dir (trieConfig . FilePath )
137+ dir := filepath .Dir (path )
133138 _ , err := os .Stat (dir )
134- if err != nil {
135- if ! os .IsNotExist (err ) {
136- return nil , fmt .Errorf ("error checking database directory: %w" , err )
137- }
138- log .Info ("Database directory not found, creating" , "path" , dir )
139- if err := os .MkdirAll (dir , 0o755 ); err != nil {
140- return nil , fmt .Errorf ("error creating database directory: %w" , err )
141- }
139+ if err == nil {
140+ return nil // Directory exists
142141 }
143-
144- // Create the Firewood config from the provided config.
145- config := & ffi.Config {
146- NodeCacheEntries : uint (trieConfig .CleanCacheSize ) / 256 , // TODO: estimate 256 bytes per node
147- FreeListCacheEntries : trieConfig .FreeListCacheEntries ,
148- Revisions : trieConfig .Revisions ,
149- ReadCacheStrategy : trieConfig .ReadCacheStrategy ,
142+ if ! os .IsNotExist (err ) {
143+ return fmt .Errorf ("error checking database directory: %w" , err )
150144 }
151-
152- return config , nil
145+ log .Info ("Database directory not found, creating" , "path" , dir )
146+ if err := os .MkdirAll (dir , 0o755 ); err != nil {
147+ return fmt .Errorf ("error creating database directory: %w" , err )
148+ }
149+ return nil
153150}
154151
155152// Scheme returns the scheme of the database.
0 commit comments