MCPcopy
hub / github.com/olric-data/olric / atomicIncrByFloat

Method atomicIncrByFloat

internal/dmap/atomic.go:171–220  ·  view source on GitHub ↗
(e *env, delta float64)

Source from the content-addressed store, hash-verified

169}
170
171func (dm *DMap) atomicIncrByFloat(e *env, delta float64) (float64, error) {
172 atomicKey := e.dmap + e.key
173 dm.s.locker.Lock(atomicKey)
174 defer func() {
175 err := dm.s.locker.Unlock(atomicKey)
176 if err != nil {
177 dm.s.log.V(3).Printf("[ERROR] Failed to release the fine grained lock for key: %s on DMap: %s: %v", e.key, e.dmap, err)
178 }
179 }()
180
181 var current float64
182 entry, err := dm.Get(e.ctx, e.key)
183 if errors.Is(err, ErrKeyNotFound) {
184 err = nil
185 }
186 if err != nil {
187 return 0, err
188 }
189
190 if entry != nil {
191 current, err = util.ParseFloat(entry.Value(), 64)
192 if err != nil {
193 return 0, err
194 }
195 }
196
197 latest := current + delta
198 if err != nil {
199 return 0, err
200 }
201
202 valueBuf := pool.Get()
203 defer pool.Put(valueBuf)
204
205 enc := resp.New(valueBuf)
206 err = enc.Encode(latest)
207 if err != nil {
208 return 0, err
209 }
210 e.value = valueBuf.Bytes()
211 e.value = make([]byte, valueBuf.Len())
212 copy(e.value, valueBuf.Bytes())
213
214 err = dm.put(e)
215 if err != nil {
216 return 0, err
217 }
218
219 return latest, nil
220}
221
222// IncrByFloat atomically increments key by delta. The return value is the new value after being incremented or an error.
223func (dm *DMap) IncrByFloat(ctx context.Context, key string, delta float64) (float64, error) {

Callers 2

IncrByFloatMethod · 0.95

Implementers 2

ClusterDMapcluster_client.go
EmbeddedDMapembedded_client.go

Calls 12

GetMethod · 0.95
putMethod · 0.95
ParseFloatFunction · 0.92
NewFunction · 0.92
PrintfMethod · 0.80
VMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
ValueMethod · 0.65
GetMethod · 0.65
PutMethod · 0.65
EncodeMethod · 0.65

Tested by

no test coverage detected