本文整理匯總了Golang中github.com/portworx/kvdb.Kvdb.Snapshot方法的典型用法代碼示例。如果您正苦於以下問題:Golang Kvdb.Snapshot方法的具體用法?Golang Kvdb.Snapshot怎麽用?Golang Kvdb.Snapshot使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/portworx/kvdb.Kvdb
的用法示例。
在下文中一共展示了Kvdb.Snapshot方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: snapshot
func snapshot(kv kvdb.Kvdb, t *testing.T) {
fmt.Println("snapshot")
prefix := "snapshot/"
kv.DeleteTree(prefix)
defer func() {
kv.DeleteTree(prefix)
}()
inputData := make(map[string]string)
inputDataVersion := make(map[string]uint64)
key := "key"
value := "bar"
count := 100
doneUpdate := make(chan bool, 2)
updateFn := func(count int, v string) {
for i := 0; i < count; i++ {
suffix := strconv.Itoa(i)
inputKey := prefix + key + suffix
inputValue := v
kv, err := kv.Put(inputKey, []byte(inputValue), 0)
assert.NoError(t, err, "Unexpected error on Put")
inputData[inputKey] = inputValue
inputDataVersion[inputKey] = kv.ModifiedIndex
}
doneUpdate <- true
}
updateFn(count, value)
<-doneUpdate
newValue := "bar2"
go updateFn(50, newValue)
snap, snapVersion, err := kv.Snapshot(prefix)
assert.NoError(t, err, "Unexpected error on Snapshot")
<-doneUpdate
kvPairs, err := snap.Enumerate(prefix)
assert.NoError(t, err, "Unexpected error on Enumerate")
assert.Equal(t, len(kvPairs), count,
"Expecting %d keys under %s got: %d, kv: %v",
count, prefix, len(kvPairs), kvPairs)
for i := range kvPairs {
currValue, ok1 := inputData[kvPairs[i].Key]
mapVersion, ok2 := inputDataVersion[kvPairs[i].Key]
assert.True(t, ok1 && ok2, "unexpected kvpair (%s)->(%s)",
kvPairs[i].Key, kvPairs[i].Value)
expectedValue := value
if mapVersion <= snapVersion {
expectedValue = currValue
}
assert.Equal(t, expectedValue, string(kvPairs[i].Value),
"Invalid kvpair %v (%s)->(%s) expect value %s"+
" snap version: %v kvVersion: %v",
i, kvPairs[i].Key, kvPairs[i].Value, expectedValue,
snapVersion, mapVersion)
assert.True(t, mapVersion > snapVersion || mapVersion == kvPairs[i].ModifiedIndex,
"Invalid kvpair %v (%s)->(%s) expect version %v"+
" snap version: %v kvVersion: %v",
i, kvPairs[i].Key, kvPairs[i].Value, mapVersion,
snapVersion, kvPairs[i].KVDBIndex)
}
}