arc_cache.go 2.83 KB
Newer Older
1 2 3 4
package blockstore

import (
	"github.com/ipfs/go-ipfs/blocks"
George Antoniadis's avatar
George Antoniadis committed
5
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
6

7 8
	lru "gx/ipfs/QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy/golang-lru"
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
George Antoniadis's avatar
George Antoniadis committed
9
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
)

type arccache struct {
	arc        *lru.ARCCache
	blockstore Blockstore
}

func arcCached(bs Blockstore, lruSize int) (*arccache, error) {
	arc, err := lru.NewARC(lruSize)
	if err != nil {
		return nil, err
	}

	return &arccache{arc: arc, blockstore: bs}, nil
}

func (b *arccache) DeleteBlock(k key.Key) error {
	if has, ok := b.hasCached(k); ok && !has {
		return ErrNotFound
	}

	b.arc.Remove(k) // Invalidate cache before deleting.
	err := b.blockstore.DeleteBlock(k)
	switch err {
34
	case nil, ds.ErrNotFound, ErrNotFound:
35
		b.arc.Add(k, false)
36
		return err
37 38 39 40 41 42 43 44 45
	default:
		return err
	}
}

// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *arccache) hasCached(k key.Key) (has bool, ok bool) {
	if k == "" {
46 47
		// Return cache invalid so the call to blockstore happens
		// in case of invalid key and correct error is created.
48 49
		return false, false
	}
50

51 52
	h, ok := b.arc.Get(k)
	if ok {
53
		return h.(bool), true
54
	}
55
	return false, false
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
}

func (b *arccache) Has(k key.Key) (bool, error) {
	if has, ok := b.hasCached(k); ok {
		return has, nil
	}

	res, err := b.blockstore.Has(k)
	if err == nil {
		b.arc.Add(k, res)
	}
	return res, err
}

func (b *arccache) Get(k key.Key) (blocks.Block, error) {
	if has, ok := b.hasCached(k); ok && !has {
		return nil, ErrNotFound
	}

	bl, err := b.blockstore.Get(k)
	if bl == nil && err == ErrNotFound {
		b.arc.Add(k, false)
	} else if bl != nil {
		b.arc.Add(k, true)
	}
	return bl, err
}

func (b *arccache) Put(bl blocks.Block) error {
	if has, ok := b.hasCached(bl.Key()); ok && has {
		return nil
	}

	err := b.blockstore.Put(bl)
	if err == nil {
		b.arc.Add(bl.Key(), true)
	}
	return err
}

func (b *arccache) PutMany(bs []blocks.Block) error {
	var good []blocks.Block
	for _, block := range bs {
99 100
		// call put on block if result is inconclusive or we are sure that
		// the block isn't in storage
101 102 103 104
		if has, ok := b.hasCached(block.Key()); !ok || (ok && !has) {
			good = append(good, block)
		}
	}
105
	err := b.blockstore.PutMany(good)
106 107
	if err != nil {
		return err
108
	}
109
	for _, block := range good {
110 111 112
		b.arc.Add(block.Key(), true)
	}
	return nil
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

func (b *arccache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
	return b.blockstore.AllKeysChan(ctx)
}

func (b *arccache) GCLock() Unlocker {
	return b.blockstore.(GCBlockstore).GCLock()
}

func (b *arccache) PinLock() Unlocker {
	return b.blockstore.(GCBlockstore).PinLock()
}

func (b *arccache) GCRequested() bool {
	return b.blockstore.(GCBlockstore).GCRequested()
}