Files
mattermost/model/plugin_kvset_options.go
Jesse Hallam 17f2cd665d MM-21672: KVCompareAndSet improvements (#13858)
* allow ExpireInSeconds < 0

Allow `ExpireInSeconds < 0` for use with `KVSetWithOptions`. While this has no practical use in reality, it's much easier to thoroughly unit tests the underlying functionality if we can match the semantics of CompareAndSet. Strictly speaking, this is a breaking change, but not relative to the advertised semantics. Anyway, it's also not entirely unreasonable to treat a negative `ExpireInSeconds` as having already expired vs. marking it as never expired.

* updated tests, to break apart

* honour expiry in CompareAndSet

* honour expiry in CompareAndDelete

* honour expiry in List

* fail unique constraint exception for SaveOrUpdate

A unique constraint error on a `SaveOrUpdate` should not be ignored: we did not save or update the requested value, as someone else managed to write the record first.

Note this is handled differently in `CompareAndSet`, where we correctly swallow the error and return `false` to indicate we did not successfully save the value.

* unexport DEFAULT_PLUGIN_KEY_FETCH_LIMIT

* s/InternalServerError/BadRequest/ for failed SaveOrUpdate

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
2020-02-18 16:32:46 -04:00

48 lines
1.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"net/http"
)
// PluginKVSetOptions contains information on how to store a value in the plugin KV store.
type PluginKVSetOptions struct {
Atomic bool // Only store the value if the current value matches the oldValue
OldValue []byte // The value to compare with the current value. Only used when Atomic is true
ExpireInSeconds int64 // Set an expire counter
}
// IsValid returns nil if the chosen options are valid.
func (opt *PluginKVSetOptions) IsValid() *AppError {
if !opt.Atomic && opt.OldValue != nil {
return NewAppError(
"PluginKVSetOptions.IsValid",
"model.plugin_kvset_options.is_valid.old_value.app_error",
nil,
"",
http.StatusBadRequest,
)
}
return nil
}
// NewPluginKeyValueFromOptions return a PluginKeyValue given a pluginID, a KV pair and options.
func NewPluginKeyValueFromOptions(pluginId, key string, value []byte, opt PluginKVSetOptions) (*PluginKeyValue, *AppError) {
expireAt := int64(0)
if opt.ExpireInSeconds != 0 {
expireAt = GetMillis() + (opt.ExpireInSeconds * 1000)
}
kv := &PluginKeyValue{
PluginId: pluginId,
Key: key,
Value: value,
ExpireAt: expireAt,
}
return kv, nil
}