本文整理匯總了Golang中github.com/blevesearch/bleve/index/store.KVStore.Writer方法的典型用法代碼示例。如果您正苦於以下問題:Golang KVStore.Writer方法的具體用法?Golang KVStore.Writer怎麽用?Golang KVStore.Writer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/blevesearch/bleve/index/store.KVStore
的用法示例。
在下文中一共展示了KVStore.Writer方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NullTestKVStore
// NullTestKVStore has very different expectations
// compared to CommonTestKVStore
func NullTestKVStore(t *testing.T, s store.KVStore) {
writer, err := s.Writer()
if err != nil {
t.Error(err)
}
batch := writer.NewBatch()
batch.Set([]byte("b"), []byte("val-b"))
batch.Set([]byte("c"), []byte("val-c"))
batch.Set([]byte("d"), []byte("val-d"))
batch.Set([]byte("e"), []byte("val-e"))
batch.Set([]byte("f"), []byte("val-f"))
batch.Set([]byte("g"), []byte("val-g"))
batch.Set([]byte("h"), []byte("val-h"))
batch.Set([]byte("i"), []byte("val-i"))
batch.Set([]byte("j"), []byte("val-j"))
err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()
it := reader.RangeIterator([]byte("b"), nil)
key, val, valid := it.Current()
if valid {
t.Fatalf("valid true, expected false")
}
if key != nil {
t.Fatalf("expected key nil, got %s", key)
}
if val != nil {
t.Fatalf("expected value nil, got %s", val)
}
err = it.Close()
if err != nil {
t.Fatal(err)
}
err = s.Close()
if err != nil {
t.Fatal(err)
}
}
示例2: CommonTestMerge
func CommonTestMerge(t *testing.T, s store.KVStore) {
testKey := []byte("k1")
data := []struct {
key []byte
val []byte
}{
{testKey, encodeUint64(1)},
{testKey, encodeUint64(1)},
}
// open a writer
writer, err := s.Writer()
if err != nil {
t.Fatal(err)
}
// write the data
batch := writer.NewBatch()
for _, row := range data {
batch.Merge(row.key, row.val)
}
err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}
// close the writer
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// open a reader
reader, err := s.Reader()
if err != nil {
t.Fatal(err)
}
// read key
returnedVal, err := reader.Get(testKey)
if err != nil {
t.Fatal(err)
}
// check the value
mergedval := binary.LittleEndian.Uint64(returnedVal)
if mergedval != 2 {
t.Errorf("expected 2, got %d", mergedval)
}
// close the reader
err = reader.Close()
if err != nil {
t.Fatal(err)
}
}
示例3: batchWriteRows
func batchWriteRows(s store.KVStore, rows []testRow) error {
// open a writer
writer, err := s.Writer()
if err != nil {
return err
}
// write the data
batch := writer.NewBatch()
for _, row := range rows {
batch.Set(row.key, row.val)
}
err = writer.ExecuteBatch(batch)
if err != nil {
return err
}
// close the writer
err = writer.Close()
if err != nil {
return err
}
return nil
}
示例4: CommonTestKVCrud
func CommonTestKVCrud(t *testing.T, s store.KVStore) {
writer, err := s.Writer()
if err != nil {
t.Error(err)
}
batch := writer.NewBatch()
batch.Set([]byte("a"), []byte("val-a"))
batch.Set([]byte("z"), []byte("val-z"))
batch.Delete([]byte("z"))
err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}
batch.Reset()
batch.Set([]byte("b"), []byte("val-b"))
batch.Set([]byte("c"), []byte("val-c"))
batch.Set([]byte("d"), []byte("val-d"))
batch.Set([]byte("e"), []byte("val-e"))
batch.Set([]byte("f"), []byte("val-f"))
batch.Set([]byte("g"), []byte("val-g"))
batch.Set([]byte("h"), []byte("val-h"))
batch.Set([]byte("i"), []byte("val-i"))
batch.Set([]byte("j"), []byte("val-j"))
err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()
it := reader.RangeIterator([]byte("b"), nil)
key, val, valid := it.Current()
if !valid {
t.Fatalf("valid false, expected true")
}
if string(key) != "b" {
t.Fatalf("expected key b, got %s", key)
}
if string(val) != "val-b" {
t.Fatalf("expected value val-b, got %s", val)
}
it.Next()
key, val, valid = it.Current()
if !valid {
t.Fatalf("valid false, expected true")
}
if string(key) != "c" {
t.Fatalf("expected key c, got %s", key)
}
if string(val) != "val-c" {
t.Fatalf("expected value val-c, got %s", val)
}
it.Seek([]byte("i"))
key, val, valid = it.Current()
if !valid {
t.Fatalf("valid false, expected true")
}
if string(key) != "i" {
t.Fatalf("expected key i, got %s", key)
}
if string(val) != "val-i" {
t.Fatalf("expected value val-i, got %s", val)
}
err = it.Close()
if err != nil {
t.Fatal(err)
}
}
示例5: CommonTestReaderIsolation
func CommonTestReaderIsolation(t *testing.T, s store.KVStore) {
// insert a kv pair
writer, err := s.Writer()
if err != nil {
t.Error(err)
}
err = writer.Set([]byte("a"), []byte("val-a"))
if err != nil {
t.Fatal(err)
}
// **************************************************
// this is a hack to try to pre-emptively overflow
// boltdb writes *MAY* block a long reader
// in particular, if the write requires additional
// allocation, it must acquire the same lock as
// the reader, thus cannot continue until that
// reader is closed.
// in general this is not a problem for bleve
// (though it may affect performance in some cases)
// but it is a problem for this test which attemps
// to easily verify that readers are isolated
// this hack writes enough initial data such that
// the subsequent writes do not require additional
// space
hackSize := 1000
for i := 0; i < hackSize; i++ {
k := fmt.Sprintf("x%d", i)
err = writer.Set([]byte(k), []byte("filler"))
if err != nil {
t.Fatal(err)
}
}
// **************************************************
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// create an isolated reader
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()
// verify that we see the value already inserted
val, err := reader.Get([]byte("a"))
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(val, []byte("val-a")) {
t.Errorf("expected val-a, got nil")
}
// verify that an iterator sees it
count := 0
it := reader.Iterator([]byte{0})
defer func() {
err := it.Close()
if err != nil {
t.Fatal(err)
}
}()
for it.Valid() {
it.Next()
count++
}
if count != hackSize+1 {
t.Errorf("expected iterator to see 1, saw %d", count)
}
// add something after the reader was created
writer, err = s.Writer()
if err != nil {
t.Error(err)
}
err = writer.Set([]byte("b"), []byte("val-b"))
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// ensure that a newer reader sees it
newReader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := newReader.Close()
if err != nil {
//.........這裏部分代碼省略.........
示例6: CommonTestReaderIsolation
func CommonTestReaderIsolation(t *testing.T, s store.KVStore) {
// insert a kv pair
writer, err := s.Writer()
if err != nil {
t.Error(err)
}
err = writer.Set([]byte("a"), []byte("val-a"))
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// create an isolated reader
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()
// verify that we see the value already inserted
val, err := reader.Get([]byte("a"))
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(val, []byte("val-a")) {
t.Errorf("expected val-a, got nil")
}
// verify that an iterator sees it
count := 0
it := reader.Iterator([]byte{0})
defer func() {
err := it.Close()
if err != nil {
t.Fatal(err)
}
}()
for it.Valid() {
it.Next()
count++
}
if count != 1 {
t.Errorf("expected iterator to see 1, saw %d", count)
}
// add something after the reader was created
writer, err = s.Writer()
if err != nil {
t.Error(err)
}
err = writer.Set([]byte("b"), []byte("val-b"))
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// ensure that a newer reader sees it
newReader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := newReader.Close()
if err != nil {
t.Fatal(err)
}
}()
val, err = newReader.Get([]byte("b"))
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(val, []byte("val-b")) {
t.Errorf("expected val-b, got nil")
}
// ensure that the director iterator sees it
count = 0
it2 := newReader.Iterator([]byte{0})
defer func() {
err := it2.Close()
if err != nil {
t.Fatal(err)
}
}()
for it2.Valid() {
it2.Next()
count++
}
if count != 2 {
//.........這裏部分代碼省略.........
示例7: CommonTestReaderOwnsGetBytes
// CommonTestReaderOwnsGetBytes attempts to mutate the returned bytes
// first, while the reader is still open, second after that reader is
// closed, then the original key is read again, to ensure these
// modifications did not cause panic, or mutate the stored value
func CommonTestReaderOwnsGetBytes(t *testing.T, s store.KVStore) {
originalKey := []byte("key")
originalVal := []byte("val")
// open a writer
writer, err := s.Writer()
if err != nil {
t.Fatal(err)
}
// write key/val
batch := writer.NewBatch()
batch.Set(originalKey, originalVal)
err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}
// close the writer
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// open a reader
reader, err := s.Reader()
if err != nil {
t.Fatal(err)
}
// read key
returnedVal, err := reader.Get(originalKey)
if err != nil {
t.Fatal(err)
}
// check that it is the expected value
if !reflect.DeepEqual(returnedVal, originalVal) {
t.Fatalf("expected value: %v for '%s', got %v", originalVal, originalKey, returnedVal)
}
// mutate the returned value with reader still open
for i := range returnedVal {
returnedVal[i] = '1'
}
// read the key again
returnedVal2, err := reader.Get(originalKey)
if err != nil {
t.Fatal(err)
}
// check that it is the expected value
if !reflect.DeepEqual(returnedVal2, originalVal) {
t.Fatalf("expected value: %v for '%s', got %v", originalVal, originalKey, returnedVal2)
}
// close the reader
err = reader.Close()
if err != nil {
t.Fatal(err)
}
// mutate the original returned value again
for i := range returnedVal {
returnedVal[i] = '2'
}
// open another reader
reader, err = s.Reader()
if err != nil {
t.Fatal(err)
}
// read the key again
returnedVal3, err := reader.Get(originalKey)
if err != nil {
t.Fatal(err)
}
// check that it is the expected value
if !reflect.DeepEqual(returnedVal3, originalVal) {
t.Fatalf("expected value: %v for '%s', got %v", originalVal, originalKey, returnedVal3)
}
// close the reader
err = reader.Close()
if err != nil {
t.Fatal(err)
}
// finally check that the value we mutated still has what we set it to
for i := range returnedVal {
if returnedVal[i] != '2' {
t.Errorf("expected byte to be '2', got %v", returnedVal[i])
//.........這裏部分代碼省略.........
示例8: CommonTestWriterOwnsBytes
func CommonTestWriterOwnsBytes(t *testing.T, s store.KVStore) {
keyBuffer := make([]byte, 5)
valBuffer := make([]byte, 5)
// open a writer
writer, err := s.Writer()
if err != nil {
t.Fatal(err)
}
// write key/val pairs reusing same buffer
batch := writer.NewBatch()
for i := 0; i < 10; i++ {
keyBuffer[0] = 'k'
keyBuffer[1] = 'e'
keyBuffer[2] = 'y'
keyBuffer[3] = '-'
keyBuffer[4] = byte('0' + i)
valBuffer[0] = 'v'
valBuffer[1] = 'a'
valBuffer[2] = 'l'
valBuffer[3] = '-'
valBuffer[4] = byte('0' + i)
batch.Set(keyBuffer, valBuffer)
}
err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}
// close the writer
err = writer.Close()
if err != nil {
t.Fatal(err)
}
// open a reader
reader, err := s.Reader()
if err != nil {
t.Fatal(err)
}
// check that we can read back what we expect
allks := make([][]byte, 0)
allvs := make([][]byte, 0)
iter := reader.RangeIterator(nil, nil)
for iter.Valid() {
// if we want to keep bytes from iteration we must copy
k := iter.Key()
copyk := make([]byte, len(k))
copy(copyk, k)
allks = append(allks, copyk)
v := iter.Key()
copyv := make([]byte, len(v))
copy(copyv, v)
allvs = append(allvs, copyv)
iter.Next()
}
err = iter.Close()
if err != nil {
t.Fatal(err)
}
if len(allks) != 10 {
t.Fatalf("expected 10 k/v pairs, got %d", len(allks))
}
for i, key := range allks {
val := allvs[i]
if !bytes.HasSuffix(key, []byte{byte('0' + i)}) {
t.Errorf("expected key %v to end in %d", key, []byte{byte('0' + i)})
}
if !bytes.HasSuffix(val, []byte{byte('0' + i)}) {
t.Errorf("expected val %v to end in %d", val, []byte{byte('0' + i)})
}
}
// close the reader
err = reader.Close()
if err != nil {
t.Fatal(err)
}
// open a writer
writer, err = s.Writer()
if err != nil {
t.Fatal(err)
}
// now delete using same approach
batch = writer.NewBatch()
for i := 0; i < 10; i++ {
keyBuffer[0] = 'k'
keyBuffer[1] = 'e'
keyBuffer[2] = 'y'
keyBuffer[3] = '-'
keyBuffer[4] = byte('0' + i)
batch.Delete(keyBuffer)
}
err = writer.ExecuteBatch(batch)
//.........這裏部分代碼省略.........
示例9: CommonTestKVStoreIterator
func CommonTestKVStoreIterator(t *testing.T, s store.KVStore) {
writer, err := s.Writer()
if err != nil {
t.Error(err)
}
data := []struct {
k []byte
v []byte
}{
{[]byte("t\x09\x00paint\xff/sponsor/gold/thumbtack/"), []byte("a")},
{[]byte("t\x09\x00party\xff/sponsor/gold/thumbtack/"), []byte("a")},
{[]byte("t\x09\x00personal\xff/sponsor/gold/thumbtack/"), []byte("a")},
{[]byte("t\x09\x00plan\xff/sponsor/gold/thumbtack/"), []byte("a")},
}
batch := writer.NewBatch()
for _, d := range data {
batch.Set(d.k, d.v)
}
err = batch.Execute()
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()
it := reader.Iterator([]byte("a"))
keys := make([][]byte, 0, len(data))
key, _, valid := it.Current()
for valid {
keys = append(keys, key)
it.Next()
key, _, valid = it.Current()
}
if len(keys) != len(data) {
t.Errorf("expected same number of keys, got %d != %d", len(keys), len(data))
}
for i, dk := range data {
if !reflect.DeepEqual(dk.k, keys[i]) {
t.Errorf("expected key %s got %s", dk.k, keys[i])
}
}
err = it.Close()
if err != nil {
t.Fatal(err)
}
}