@@ -71,20 +71,20 @@ func New(path string) (s *Store, err error) {
71
71
}
72
72
// Index storing actual chunk address, data and store timestamp.
73
73
s .retrievalIndex , err = db .NewIndex ("Address->StoreTimestamp|Data" , shed.IndexFuncs {
74
- EncodeKey : func (fields shed.IndexItem ) (key []byte , err error ) {
74
+ EncodeKey : func (fields shed.Item ) (key []byte , err error ) {
75
75
return fields .Address , nil
76
76
},
77
- DecodeKey : func (key []byte ) (e shed.IndexItem , err error ) {
77
+ DecodeKey : func (key []byte ) (e shed.Item , err error ) {
78
78
e .Address = key
79
79
return e , nil
80
80
},
81
- EncodeValue : func (fields shed.IndexItem ) (value []byte , err error ) {
81
+ EncodeValue : func (fields shed.Item ) (value []byte , err error ) {
82
82
b := make ([]byte , 8 )
83
83
binary .BigEndian .PutUint64 (b , uint64 (fields .StoreTimestamp ))
84
84
value = append (b , fields .Data ... )
85
85
return value , nil
86
86
},
87
- DecodeValue : func (value []byte ) (e shed.IndexItem , err error ) {
87
+ DecodeValue : func (keyItem shed. Item , value []byte ) (e shed.Item , err error ) {
88
88
e .StoreTimestamp = int64 (binary .BigEndian .Uint64 (value [:8 ]))
89
89
e .Data = value [8 :]
90
90
return e , nil
@@ -96,19 +96,19 @@ func New(path string) (s *Store, err error) {
96
96
// Index storing access timestamp for a particular address.
97
97
// It is needed in order to update gc index keys for iteration order.
98
98
s .accessIndex , err = db .NewIndex ("Address->AccessTimestamp" , shed.IndexFuncs {
99
- EncodeKey : func (fields shed.IndexItem ) (key []byte , err error ) {
99
+ EncodeKey : func (fields shed.Item ) (key []byte , err error ) {
100
100
return fields .Address , nil
101
101
},
102
- DecodeKey : func (key []byte ) (e shed.IndexItem , err error ) {
102
+ DecodeKey : func (key []byte ) (e shed.Item , err error ) {
103
103
e .Address = key
104
104
return e , nil
105
105
},
106
- EncodeValue : func (fields shed.IndexItem ) (value []byte , err error ) {
106
+ EncodeValue : func (fields shed.Item ) (value []byte , err error ) {
107
107
b := make ([]byte , 8 )
108
108
binary .BigEndian .PutUint64 (b , uint64 (fields .AccessTimestamp ))
109
109
return b , nil
110
110
},
111
- DecodeValue : func (value []byte ) (e shed.IndexItem , err error ) {
111
+ DecodeValue : func (keyItem shed. Item , value []byte ) (e shed.Item , err error ) {
112
112
e .AccessTimestamp = int64 (binary .BigEndian .Uint64 (value ))
113
113
return e , nil
114
114
},
@@ -118,23 +118,23 @@ func New(path string) (s *Store, err error) {
118
118
}
119
119
// Index with keys ordered by access timestamp for garbage collection prioritization.
120
120
s .gcIndex , err = db .NewIndex ("AccessTimestamp|StoredTimestamp|Address->nil" , shed.IndexFuncs {
121
- EncodeKey : func (fields shed.IndexItem ) (key []byte , err error ) {
121
+ EncodeKey : func (fields shed.Item ) (key []byte , err error ) {
122
122
b := make ([]byte , 16 , 16 + len (fields .Address ))
123
123
binary .BigEndian .PutUint64 (b [:8 ], uint64 (fields .AccessTimestamp ))
124
124
binary .BigEndian .PutUint64 (b [8 :16 ], uint64 (fields .StoreTimestamp ))
125
125
key = append (b , fields .Address ... )
126
126
return key , nil
127
127
},
128
- DecodeKey : func (key []byte ) (e shed.IndexItem , err error ) {
128
+ DecodeKey : func (key []byte ) (e shed.Item , err error ) {
129
129
e .AccessTimestamp = int64 (binary .BigEndian .Uint64 (key [:8 ]))
130
130
e .StoreTimestamp = int64 (binary .BigEndian .Uint64 (key [8 :16 ]))
131
131
e .Address = key [16 :]
132
132
return e , nil
133
133
},
134
- EncodeValue : func (fields shed.IndexItem ) (value []byte , err error ) {
134
+ EncodeValue : func (fields shed.Item ) (value []byte , err error ) {
135
135
return nil , nil
136
136
},
137
- DecodeValue : func (value []byte ) (e shed.IndexItem , err error ) {
137
+ DecodeValue : func (keyItem shed. Item , value []byte ) (e shed.Item , err error ) {
138
138
return e , nil
139
139
},
140
140
})
@@ -146,7 +146,7 @@ func New(path string) (s *Store, err error) {
146
146
147
147
// Put stores the chunk and sets it store timestamp.
148
148
func (s * Store ) Put (_ context.Context , ch storage.Chunk ) (err error ) {
149
- return s .retrievalIndex .Put (shed.IndexItem {
149
+ return s .retrievalIndex .Put (shed.Item {
150
150
Address : ch .Address (),
151
151
Data : ch .Data (),
152
152
StoreTimestamp : time .Now ().UTC ().UnixNano (),
@@ -161,7 +161,7 @@ func (s *Store) Get(_ context.Context, addr storage.Address) (c storage.Chunk, e
161
161
batch := new (leveldb.Batch )
162
162
163
163
// Get the chunk data and storage timestamp.
164
- item , err := s .retrievalIndex .Get (shed.IndexItem {
164
+ item , err := s .retrievalIndex .Get (shed.Item {
165
165
Address : addr ,
166
166
})
167
167
if err != nil {
@@ -172,13 +172,13 @@ func (s *Store) Get(_ context.Context, addr storage.Address) (c storage.Chunk, e
172
172
}
173
173
174
174
// Get the chunk access timestamp.
175
- accessItem , err := s .accessIndex .Get (shed.IndexItem {
175
+ accessItem , err := s .accessIndex .Get (shed.Item {
176
176
Address : addr ,
177
177
})
178
178
switch err {
179
179
case nil :
180
180
// Remove gc index entry if access timestamp is found.
181
- err = s .gcIndex .DeleteInBatch (batch , shed.IndexItem {
181
+ err = s .gcIndex .DeleteInBatch (batch , shed.Item {
182
182
Address : item .Address ,
183
183
StoreTimestamp : accessItem .AccessTimestamp ,
184
184
AccessTimestamp : item .StoreTimestamp ,
@@ -197,7 +197,7 @@ func (s *Store) Get(_ context.Context, addr storage.Address) (c storage.Chunk, e
197
197
accessTimestamp := time .Now ().UTC ().UnixNano ()
198
198
199
199
// Put new access timestamp in access index.
200
- err = s .accessIndex .PutInBatch (batch , shed.IndexItem {
200
+ err = s .accessIndex .PutInBatch (batch , shed.Item {
201
201
Address : addr ,
202
202
AccessTimestamp : accessTimestamp ,
203
203
})
@@ -206,7 +206,7 @@ func (s *Store) Get(_ context.Context, addr storage.Address) (c storage.Chunk, e
206
206
}
207
207
208
208
// Put new access timestamp in gc index.
209
- err = s .gcIndex .PutInBatch (batch , shed.IndexItem {
209
+ err = s .gcIndex .PutInBatch (batch , shed.Item {
210
210
Address : item .Address ,
211
211
AccessTimestamp : accessTimestamp ,
212
212
StoreTimestamp : item .StoreTimestamp ,
@@ -244,7 +244,7 @@ func (s *Store) CollectGarbage() (err error) {
244
244
// New batch for a new cg round.
245
245
trash := new (leveldb.Batch )
246
246
// Iterate through all index items and break when needed.
247
- err = s .gcIndex .IterateAll (func (item shed.IndexItem ) (stop bool , err error ) {
247
+ err = s .gcIndex .Iterate (func (item shed.Item ) (stop bool , err error ) {
248
248
// Remove the chunk.
249
249
err = s .retrievalIndex .DeleteInBatch (trash , item )
250
250
if err != nil {
@@ -265,7 +265,7 @@ func (s *Store) CollectGarbage() (err error) {
265
265
return true , nil
266
266
}
267
267
return false , nil
268
- })
268
+ }, nil )
269
269
if err != nil {
270
270
return err
271
271
}
0 commit comments