@@ -36,7 +36,7 @@ type Location struct {
36
36
lib * Library
37
37
checkpoint * checkpoint
38
38
txer * transactioner
39
- metadata * LocationMetadata
39
+ metadata * locationMetadata
40
40
41
41
// references and config cache
42
42
refs memory.ReferenceStorage
@@ -58,10 +58,14 @@ func newLocation(
58
58
path string ,
59
59
create bool ,
60
60
) (* Location , error ) {
61
- metadata , err := loadLocationMetadata (lib .fs , locationMetadataPath (path ))
62
- if err != nil {
63
- // TODO: skip metadata if corrupted? log a warning?
64
- return nil , err
61
+ var metadata * locationMetadata
62
+ if lib .metadata != nil {
63
+ var err error
64
+ metadata , err = loadOrCreateLocationMetadata (lib .fs , string (id ))
65
+ if err != nil {
66
+ // TODO: skip metadata if corrupted? log a warning?
67
+ return nil , err
68
+ }
65
69
}
66
70
67
71
cp , err := newCheckpoint (lib .fs , path , create )
@@ -104,7 +108,11 @@ func (l *Location) checkAndUpdate() error {
104
108
return err
105
109
}
106
110
107
- version := l .lib .Version ()
111
+ version , err := l .lib .Version ()
112
+ if err != nil {
113
+ return err
114
+ }
115
+
108
116
if l .fSize == stat .Size () && l .fTime == stat .ModTime () &&
109
117
l .version == version && l .checkpoint .Offset () == cp .Offset () {
110
118
return nil
@@ -175,9 +183,18 @@ func (l *Location) fs(mode borges.Mode, cp *checkpoint) (sivafs.SivaFS, error) {
175
183
return nil , borges .ErrLocationNotExists .New (string (l .id ))
176
184
}
177
185
178
- if l .metadata != nil {
179
- version := l .lib .Version ()
180
- if o := l .metadata .Offset (version ); o > 0 {
186
+ if l .lib .metadata != nil {
187
+ version , err := l .lib .Version ()
188
+ if err != nil {
189
+ return nil , err
190
+ }
191
+
192
+ o , err := l .metadata .offset (version )
193
+ if err != nil {
194
+ return nil , err
195
+ }
196
+
197
+ if o > 0 {
181
198
offset = o
182
199
}
183
200
}
@@ -561,40 +578,40 @@ func (l *Location) repository(
561
578
return newRepository (id , sto , fs , mode , l .lib .options .Transactional , l )
562
579
}
563
580
564
- func (l * Location ) createMetadata () {
565
- if l .metadata == nil {
566
- l .metadata = NewLocationMetadata (make (map [int ]Version ))
567
- }
568
- }
569
-
570
581
// LastVersion returns the last defined version number in metadata or -1 if
571
582
// there are not versions.
572
583
func (l * Location ) LastVersion () int {
573
- return l .metadata .Last ()
584
+ return l .metadata .last ()
574
585
}
575
586
576
- // Version returns an specific version. Second return value is false if the
577
- // version does not exist.
578
- func (l * Location ) Version (v int ) (Version , bool ) {
579
- return l .metadata .Version (v )
587
+ // Version returns an specific version. If the given version does not exist
588
+ // an error is returned.
589
+ func (l * Location ) Version (v int ) (* Version , error ) {
590
+ if l .lib .metadata != nil {
591
+ return l .metadata .version (v )
592
+ }
593
+
594
+ return nil , errLocVersionNotExists .New ()
580
595
}
581
596
582
597
// SetVersion adds or changes a version to the location.
583
- func (l * Location ) SetVersion (n int , v Version ) {
584
- l .createMetadata ()
585
- l .metadata .SetVersion (n , v )
598
+ func (l * Location ) SetVersion (n int , v * Version ) {
599
+ if l .lib .metadata != nil {
600
+ l .metadata .setVersion (n , v )
601
+ }
586
602
}
587
603
588
604
// DeleteVersion removes the given version number.
589
605
func (l * Location ) DeleteVersion (n int ) {
590
- l .createMetadata ()
591
- l .metadata .DeleteVersion (n )
606
+ if l .lib .metadata != nil {
607
+ l .metadata .deleteVersion (n )
608
+ }
592
609
}
593
610
594
611
// SaveMetadata writes the location metadata to disk.
595
612
func (l * Location ) SaveMetadata () error {
596
- if l .metadata != nil && l . metadata . Dirty () {
597
- return l .metadata .Save ( l . lib . fs , l . path )
613
+ if l .metadata != nil {
614
+ return l .metadata .save ( )
598
615
}
599
616
600
617
return nil
0 commit comments