Skip to content

Commit

Permalink
fix atomic counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Termina1 committed Oct 11, 2024
1 parent b7db9b0 commit ad29466
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions atomic_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

var ErrNotCounter error = fmt.Errorf("not a counter")
var ErrCounterNotLoaded error = fmt.Errorf("counter not loaded")

type AtomicCounter struct {
db *Chotki
Expand Down Expand Up @@ -74,9 +75,10 @@ func (a *AtomicCounter) Increment(ctx context.Context, val int64) (int64, error)
if err != nil {
return 0, err
}
if val == 2 {
fmt.Println(1)
if !a.loaded.Load() {
return 0, ErrCounterNotLoaded
}

rdt := a.rdt.Load().(byte)
a.localValue.Add(val)
var dtlv []byte
Expand All @@ -88,13 +90,14 @@ func (a *AtomicCounter) Increment(ctx context.Context, val int64) (int64, error)
case rdx.ZCounter:
dtlv = rdx.Zdelta(tlv, a.localValue.Load(), a.db.Clock())
default:
a.lock.Unlock()
return 0, ErrNotCounter
}
a.tlv.Store(dtlv)
a.lock.Unlock()
changes := make(protocol.Records, 0)
changes = append(changes, protocol.Record('F', rdx.ZipUint64(uint64(a.offset))))
changes = append(changes, protocol.Record(rdt, dtlv))
a.db.CommitPacket(ctx, 'E', a.rid, changes)
a.db.CommitPacket(ctx, 'E', a.rid.ZeroOff(), changes)
return a.localValue.Load(), nil
}

0 comments on commit ad29466

Please sign in to comment.