@@ -81,7 +81,10 @@ type HAMTDirectory struct {
81
81
dserv ipld.DAGService
82
82
}
83
83
84
- // NewDirectory returns a Directory. It needs a `DAGService` to add the children.
84
+ // NewDirectory returns a Directory that can either be a HAMTDirectory if the
85
+ // UseHAMTSharding is set, or otherwise an UpgradeableDirectory containing a
86
+ // BasicDirectory that can be converted to a HAMTDirectory if the option is
87
+ // set in the future.
85
88
func NewDirectory (dserv ipld.DAGService ) Directory {
86
89
if UseHAMTSharding {
87
90
dir := new (HAMTDirectory )
@@ -94,10 +97,10 @@ func NewDirectory(dserv ipld.DAGService) Directory {
94
97
return dir
95
98
}
96
99
97
- dir := new (BasicDirectory )
98
- dir .node = format .EmptyDirNode ()
99
- dir .dserv = dserv
100
- return dir
100
+ basicDir := new (BasicDirectory )
101
+ basicDir .node = format .EmptyDirNode ()
102
+ basicDir .dserv = dserv
103
+ return UpgradeableDirectory { basicDir }
101
104
}
102
105
103
106
// ErrNotADir implies that the given node was not a unixfs directory
@@ -294,3 +297,27 @@ func (d *HAMTDirectory) GetNode() (ipld.Node, error) {
294
297
func (d * HAMTDirectory ) GetCidBuilder () cid.Builder {
295
298
return d .shard .CidBuilder ()
296
299
}
300
+
301
+ // UpgradeableDirectory wraps a Directory interface and provides extra logic
302
+ // to upgrade from its BasicDirectory implementation to HAMTDirectory.
303
+ type UpgradeableDirectory struct {
304
+ Directory
305
+ }
306
+
307
+ var _ Directory = (* UpgradeableDirectory )(nil )
308
+
309
+ // AddChild implements the `Directory` interface. We check when adding new entries
310
+ // if we should switch to HAMTDirectory according to global option(s).
311
+ func (d UpgradeableDirectory ) AddChild (ctx context.Context , name string , nd ipld.Node ) error {
312
+ if UseHAMTSharding {
313
+ if basicDir , ok := d .Directory .(* BasicDirectory ); ok {
314
+ hamtDir , err := basicDir .SwitchToSharding (ctx )
315
+ if err != nil {
316
+ return err
317
+ }
318
+ d .Directory = hamtDir
319
+ }
320
+ }
321
+
322
+ return d .Directory .AddChild (ctx , name , nd )
323
+ }
0 commit comments