swarm/storage: Add accesscount-indexcount index on put

This commit is contained in:
lash 2018-10-03 08:36:18 +02:00
parent dcc062205b
commit 481f0ab1c4

View file

@ -61,6 +61,7 @@ var (
keyData = byte(6) keyData = byte(6)
keyDistanceCnt = byte(7) keyDistanceCnt = byte(7)
keySchema = []byte{8} keySchema = []byte{8}
keyAccessIdx = []byte{9} // access to chunk data index, used by garbage collection in ascending order from first entry
) )
var ( var (
@ -225,6 +226,14 @@ func getDataKey(idx uint64, po uint8) []byte {
return key return key
} }
func getAccessIdxKey(idx *dpaDBIndex) []byte {
key := make([]byte, 17)
key[0] = keyAccessIdx[0]
binary.BigEndian.PutUint64(key[1:], index.Access)
binary.BigEndian.PutUint64(key[9:], index.Idx)
return key
}
func encodeIndex(index *dpaDBIndex) []byte { func encodeIndex(index *dpaDBIndex) []byte {
data, _ := rlp.EncodeToBytes(index) data, _ := rlp.EncodeToBytes(index)
return data return data
@ -602,6 +611,9 @@ func (s *LDBStore) Put(ctx context.Context, chunk Chunk) error {
idata = encodeIndex(&index) idata = encodeIndex(&index)
s.batch.Put(ikey, idata) s.batch.Put(ikey, idata)
// add the access-chunkindex index for garbage collection
accessIdx := getAccessIdxKey(&index)
s.batch.Put(accessIdx, nil)
s.lock.Unlock() s.lock.Unlock()
select { select {
@ -813,16 +825,6 @@ func newMockGetDataFunc(mockStore *mock.NodeStore) func(addr Address) (data []by
} }
} }
func (s *LDBStore) updateAccessCnt(addr Address) {
s.lock.Lock()
defer s.lock.Unlock()
var index dpaDBIndex
s.tryAccessIdx(getIndexKey(addr), &index) // result_chn == nil, only update access cnt
}
func (s *LDBStore) setCapacity(c uint64) { func (s *LDBStore) setCapacity(c uint64) {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()