aboutsummaryrefslogtreecommitdiff
path: root/object/store/packed/lookup.go
blob: e06870a971a7471b66dec672285934390ddf7190 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package packed

import (
	"fmt"

	"lindenii.org/go/furgit/object/id"
	"lindenii.org/go/furgit/object/store"
	"lindenii.org/go/lgo/intconv"
)

// lookup finds the pack containing objectID
// and the entry offset within it,
// probing packs in most-recently-used-ish order.
//
// Labels: Life-Parent.
func (packed *Packed) lookup(objectID id.ObjectID) (*pack, int, error) {
	if objectID.ObjectFormat() != packed.objectFormat {
		return nil, 0, fmt.Errorf(
			"%w: got %s want %s",
			id.ErrInvalidObjectFormat, objectID.ObjectFormat(), packed.objectFormat,
		)
	}

	oid := objectID.RawBytes()

	for _, p := range packed.order.Keys() {
		if p.filter != nil && !p.filter.MayContain(oid) {
			continue
		}

		offsetU, found, err := p.idx.Lookup(oid)
		if err != nil {
			return nil, 0, fmt.Errorf("%w: pack %q: %w", ErrMalformedPackedStore, p.name, err)
		}

		if !found {
			continue
		}

		offset, err := intconv.Uint64ToInt(offsetU)
		if err != nil {
			return nil, 0, fmt.Errorf("%w: pack %q: entry offset overflows int: %w", ErrMalformedPackedStore, p.name, err)
		}

		packed.order.Touch(p)

		return p, offset, nil
	}

	return nil, 0, store.ErrObjectNotFound
}