File size: 2,096 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
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
99
100
101
102
103
104
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package indexcounter

import (
	"encoding/binary"
	"fmt"
	"os"
	"sync"

	"github.com/pkg/errors"
)

type Counter struct {
	count uint64
	sync.Mutex
	f *os.File
}

func New(shardPath string) (*Counter, error) {
	fileName := fmt.Sprintf("%s/indexcount", shardPath)
	f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0o666)
	if err != nil {
		return nil, err
	}

	stat, err := f.Stat()
	if err != nil {
		return nil, err
	}

	var initialCount uint64 = 0
	if stat.Size() > 0 {
		// the file has existed before, we need to initialize with its content
		err := binary.Read(f, binary.LittleEndian, &initialCount)
		if err != nil {
			return nil, errors.Wrap(err, "read initial count from file")
		}

	}

	return &Counter{
		count: initialCount,
		f:     f,
	}, nil
}

func (c *Counter) Get() uint64 {
	c.Lock()
	defer c.Unlock()
	return c.count
}

func (c *Counter) GetAndInc() (uint64, error) {
	c.Lock()
	defer c.Unlock()
	before := c.count
	c.count++
	c.f.Seek(0, 0)
	err := binary.Write(c.f, binary.LittleEndian, &c.count)
	if err != nil {
		return 0, errors.Wrap(err, "increase counter on disk")
	}
	c.f.Seek(0, 0)
	return before, nil
}

// PreviewNext can be used to check if there is data present in the index, if
// it returns 0, you can be certain that no data exists
func (c *Counter) PreviewNext() uint64 {
	c.Lock()
	defer c.Unlock()

	return c.count
}

func (c *Counter) Drop() error {
	c.Lock()
	defer c.Unlock()
	if c.f == nil {
		return nil
	}
	filename := c.FileName()
	c.f.Close()
	err := os.Remove(filename)
	if err != nil {
		return errors.Wrap(err, "drop counter file")
	}
	return nil
}

func (c *Counter) FileName() string {
	return c.f.Name()
}