本文整理匯總了Golang中camlistore/org/pkg/sorted.KeyValue.Set方法的典型用法代碼示例。如果您正苦於以下問題:Golang KeyValue.Set方法的具體用法?Golang KeyValue.Set怎麽用?Golang KeyValue.Set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類camlistore/org/pkg/sorted.KeyValue
的用法示例。
在下文中一共展示了KeyValue.Set方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestSorted
func TestSorted(t *testing.T, kv sorted.KeyValue) {
defer test.TLog(t)()
if !isEmpty(t, kv) {
t.Fatal("kv for test is expected to be initially empty")
}
set := func(k, v string) {
if err := kv.Set(k, v); err != nil {
t.Fatalf("Error setting %q to %q: %v", k, v, err)
}
}
set("foo", "bar")
if isEmpty(t, kv) {
t.Fatalf("iterator reports the kv is empty after adding foo=bar; iterator must be broken")
}
if v, err := kv.Get("foo"); err != nil || v != "bar" {
t.Errorf("get(foo) = %q, %v; want bar", v, err)
}
if v, err := kv.Get("NOT_EXIST"); err != sorted.ErrNotFound {
t.Errorf("get(NOT_EXIST) = %q, %v; want error sorted.ErrNotFound", v, err)
}
for i := 0; i < 2; i++ {
if err := kv.Delete("foo"); err != nil {
t.Errorf("Delete(foo) (on loop %d/2) returned error %v", i+1, err)
}
}
set("a", "av")
set("b", "bv")
set("c", "cv")
testEnumerate(t, kv, "", "", "av", "bv", "cv")
testEnumerate(t, kv, "a", "", "av", "bv", "cv")
testEnumerate(t, kv, "b", "", "bv", "cv")
testEnumerate(t, kv, "a", "c", "av", "bv")
testEnumerate(t, kv, "a", "b", "av")
testEnumerate(t, kv, "a", "a")
testEnumerate(t, kv, "d", "")
testEnumerate(t, kv, "d", "e")
// Verify that < comparison works identically for all DBs (because it is affected by collation rules)
// http://postgresql.1045698.n5.nabble.com/String-comparison-and-the-SQL-standard-td5740721.html
set("foo|abc", "foo|abcv")
testEnumerate(t, kv, "foo|", "", "foo|abcv")
testEnumerate(t, kv, "foo|", "foo}", "foo|abcv")
// Verify that the value isn't being used instead of the key in the range comparison.
set("y", "x:foo")
testEnumerate(t, kv, "x:", "x~")
testInsertLarge(t, kv)
testInsertTooLarge(t, kv)
// TODO: test batch commits
// Deleting a non-existent item in a batch should not be an error
testDeleteNotFoundBatch(t, kv)
testDeletePartialNotFoundBatch(t, kv)
}
示例2: testInsertTooLarge
func testInsertTooLarge(t *testing.T, kv sorted.KeyValue) {
largeKey := make([]byte, sorted.MaxKeySize+1)
largeValue := make([]byte, sorted.MaxValueSize+1)
if err := kv.Set(string(largeKey), "whatever"); err == nil || err != sorted.ErrKeyTooLarge {
t.Fatalf("Insertion of too large a key should have failed, but err was %v", err)
}
if err := kv.Set("whatever", string(largeValue)); err == nil || err != sorted.ErrValueTooLarge {
t.Fatalf("Insertion of too large a value should have failed, but err was %v", err)
}
}
示例3: testDeletePartialNotFoundBatch
func testDeletePartialNotFoundBatch(t *testing.T, kv sorted.KeyValue) {
if err := kv.Set(butIExistKey, "whatever"); err != nil {
t.Fatal(err)
}
b := kv.BeginBatch()
b.Delete(notExistKey)
b.Delete(butIExistKey)
if err := kv.CommitBatch(b); err != nil {
t.Fatalf("Batch deletion with one non existing key returned an error: %v", err)
}
if val, err := kv.Get(butIExistKey); err != sorted.ErrNotFound || val != "" {
t.Fatalf("Key %q should have been batch deleted", butIExistKey)
}
}
示例4: TestSorted
func TestSorted(t *testing.T, kv sorted.KeyValue) {
defer test.TLog(t)()
if !isEmpty(t, kv) {
t.Fatal("kv for test is expected to be initially empty")
}
set := func(k, v string) {
if err := kv.Set(k, v); err != nil {
t.Fatalf("Error setting %q to %q: %v", k, v, err)
}
}
set("foo", "bar")
if isEmpty(t, kv) {
t.Fatalf("iterator reports the kv is empty after adding foo=bar; iterator must be broken")
}
if v, err := kv.Get("foo"); err != nil || v != "bar" {
t.Errorf("get(foo) = %q, %v; want bar", v, err)
}
if v, err := kv.Get("NOT_EXIST"); err != sorted.ErrNotFound {
t.Errorf("get(NOT_EXIST) = %q, %v; want error sorted.ErrNotFound", v, err)
}
for i := 0; i < 2; i++ {
if err := kv.Delete("foo"); err != nil {
t.Errorf("Delete(foo) (on loop %d/2) returned error %v", i+1, err)
}
}
set("a", "av")
set("b", "bv")
set("c", "cv")
testEnumerate(t, kv, "", "", "av", "bv", "cv")
testEnumerate(t, kv, "a", "", "av", "bv", "cv")
testEnumerate(t, kv, "b", "", "bv", "cv")
testEnumerate(t, kv, "a", "c", "av", "bv")
testEnumerate(t, kv, "a", "b", "av")
testEnumerate(t, kv, "a", "a")
testEnumerate(t, kv, "d", "")
testEnumerate(t, kv, "d", "e")
// Verify that the value isn't being used instead of the key in the range comparison.
set("y", "x:foo")
testEnumerate(t, kv, "x:", "x~")
// TODO: test batch commits
}
示例5: testInsertTooLarge
func testInsertTooLarge(t *testing.T, kv sorted.KeyValue) {
largeKey := make([]byte, sorted.MaxKeySize+1)
largeValue := make([]byte, sorted.MaxValueSize+1)
err := kv.Set(string(largeKey), "whatever")
if err != nil {
t.Fatalf("Insertion of too large a key should have skipped with some logging, but err was %v", err)
}
_, err = kv.Get(string(largeKey))
if err == nil {
t.Fatal("large key should not have been inserted")
}
err = kv.Set("testInsertTooLarge", string(largeValue))
if err != nil {
t.Fatalf("Insertion of too large a value should have skipped with some logging, but err was %v", err)
}
_, err = kv.Get("testInsertTooLarge")
if err == nil {
t.Fatal("large value should not have been inserted")
}
}
示例6: testInsertLarge
func testInsertLarge(t *testing.T, kv sorted.KeyValue) {
largeKey := make([]byte, sorted.MaxKeySize-1)
// setting all the bytes because postgres whines about an invalid byte sequence
// otherwise
for k, _ := range largeKey {
largeKey[k] = 'A'
}
largeKey[sorted.MaxKeySize-2] = 'B'
largeValue := make([]byte, sorted.MaxValueSize-1)
for k, _ := range largeValue {
largeValue[k] = 'A'
}
largeValue[sorted.MaxValueSize-2] = 'B'
// insert with large key
if err := kv.Set(string(largeKey), "whatever"); err != nil {
t.Fatalf("Insertion of large key failed: %v", err)
}
// and verify we can get it back, i.e. that the key hasn't been truncated.
it := kv.Find(string(largeKey), "")
if !it.Next() || it.Key() != string(largeKey) || it.Value() != "whatever" {
it.Close()
t.Fatalf("Find(largeKey) = %q, %q; want %q, %q", it.Key(), it.Value(), largeKey, "whatever")
}
it.Close()
// insert with large value
if err := kv.Set("whatever", string(largeValue)); err != nil {
t.Fatalf("Insertion of large value failed: %v", err)
}
// and verify we can get it back, i.e. that the value hasn't been truncated.
if v, err := kv.Get("whatever"); err != nil || v != string(largeValue) {
t.Fatalf("get(\"whatever\") = %q, %v; want %q", v, err, largeValue)
}
// insert with large key and large value
if err := kv.Set(string(largeKey), string(largeValue)); err != nil {
t.Fatalf("Insertion of large key and value failed: %v", err)
}
// and verify we can get them back
it = kv.Find(string(largeKey), "")
defer it.Close()
if !it.Next() || it.Key() != string(largeKey) || it.Value() != string(largeValue) {
t.Fatalf("Find(largeKey) = %q, %q; want %q, %q", it.Key(), it.Value(), largeKey, largeValue)
}
}