本文整理汇总了Golang中github.com/qedus/nds.PutMulti函数的典型用法代码示例。如果您正苦于以下问题:Golang PutMulti函数的具体用法?Golang PutMulti怎么用?Golang PutMulti使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PutMulti函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestRunInTransaction
func TestRunInTransaction(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "Entity", "", 3, nil)
keys := []*datastore.Key{key}
entity := testEntity{42}
entities := []testEntity{entity}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
err := nds.RunInTransaction(c, func(tc context.Context) error {
entities := make([]testEntity, 1, 1)
if err := nds.GetMulti(tc, keys, entities); err != nil {
t.Fatal(err)
}
entity := entities[0]
if entity.Val != 42 {
t.Fatalf("entity.Val != 42: %d", entity.Val)
}
entities[0].Val = 43
putKeys, err := nds.PutMulti(tc, keys, entities)
if err != nil {
t.Fatal(err)
} else if len(putKeys) != 1 {
t.Fatal("putKeys should be len 1")
} else if !putKeys[0].Equal(key) {
t.Fatal("keys not equal")
}
return nil
}, nil)
if err != nil {
t.Fatal(err)
}
entities = make([]testEntity, 1, 1)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entity = entities[0]
if entity.Val != 43 {
t.Fatalf("entity.Val != 43: %d", entity.Val)
}
}
开发者ID:jongillham,项目名称:nds,代码行数:54,代码来源:nds_test.go
示例2: TestPutMulti
func TestPutMulti(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type TestEntity struct {
Value int
}
for _, count := range []int{499, 500, 501} {
keys := make([]*datastore.Key, count)
entities := make([]TestEntity, count)
for i := range keys {
keys[i] = datastore.NewKey(c, "TestEntity", strconv.Itoa(i), 0, nil)
entities[i] = TestEntity{i}
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entities = make([]TestEntity, count)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:27,代码来源:put_test.go
示例3: TestPutPropertyLoadSaver
func TestPutPropertyLoadSaver(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int
}
te := &testEntity{2}
pl, err := datastore.SaveStruct(te)
if err != nil {
t.Fatal(err)
}
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
pls := datastore.PropertyList(pl)
if _, err := nds.PutMulti(c, keys,
[]datastore.PropertyLoadSaver{&pls}); err != nil {
t.Fatal(err)
}
getPl := datastore.PropertyList{}
if err := nds.GetMulti(c,
keys, []datastore.PropertyLoadSaver{&getPl}); err != nil {
t.Fatal(err)
}
getTe := &testEntity{}
if err := datastore.LoadStruct(getTe, getPl); err != nil {
t.Fatal(err)
}
if te.IntVal != getTe.IntVal {
t.Fatal("expected same IntVal", getTe.IntVal)
}
}
开发者ID:zeekay,项目名称:nds,代码行数:35,代码来源:put_test.go
示例4: TestGetMultiLockReturnEntitySetValueFail
func TestGetMultiLockReturnEntitySetValueFail(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Fail to unmarshal test.
memcacheGetChan := make(chan func(c context.Context, keys []string) (
map[string]*memcache.Item, error), 2)
memcacheGetChan <- memcache.GetMulti
memcacheGetChan <- func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
items, err := memcache.GetMulti(c, keys)
if err != nil {
return nil, err
}
pl := datastore.PropertyList{
datastore.Property{"One", 1, false, false},
}
value, err := nds.MarshalPropertyList(pl)
if err != nil {
return nil, err
}
items[keys[0]].Flags = nds.EntityItem
items[keys[0]].Value = value
items[keys[1]].Flags = nds.EntityItem
items[keys[1]].Value = value
return items, nil
}
nds.SetMemcacheGetMulti(func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
f := <-memcacheGetChan
return f(c, keys)
})
response := make([]testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
defer nds.SetMemcacheGetMulti(memcache.GetMulti)
for i := 0; i < len(keys); i++ {
if entities[i].IntVal != response[i].IntVal {
t.Fatal("IntVal not equal")
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:60,代码来源:get_test.go
示例5: generateRandom
// this will generate some random data for the given day
// 24 * 12 on appengine (288)
// 24 only on development
func generateRandom(c context.Context, day time.Time) error {
var x int
if appengine.IsDevAppServer() {
x = 1
} else {
x = 12
}
keys := make([]*datastore.Key, 24*x)
photos := make([]*Photo, 24*x)
id := 0
for h := 0; h < 24; h++ {
taken := day.Add(time.Duration(h) * time.Hour)
for i := 0; i < x; i++ {
photographer := photographers[rand.Int31n(4)]
photos[id] = &Photo{
Photographer: photographer,
Uploaded: time.Now().UTC(),
Width: 8000,
Height: 6000,
Taken: taken,
TakenDay: day,
}
keys[id] = datastore.NewIncompleteKey(c, "photo", nil)
id++
}
}
nds.PutMulti(c, keys, photos)
return nil
}
开发者ID:CaptainCodeman,项目名称:datastore-mapper,代码行数:33,代码来源:warmup.go
示例6: TestDeleteMemcacheFail
func TestDeleteMemcacheFail(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
keys := []*datastore.Key{key}
entities := make([]testEntity, 1)
entities[0].Val = 43
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
nds.SetMemcacheSetMulti(func(c context.Context,
items []*memcache.Item) error {
return errors.New("expected error")
})
defer func() {
nds.SetMemcacheSetMulti(memcache.SetMulti)
}()
if err := nds.DeleteMulti(c, keys); err == nil {
t.Fatal("expected DeleteMulti error")
}
}
开发者ID:zeekay,项目名称:nds,代码行数:30,代码来源:delete_test.go
示例7: TestPutMultiZeroKeys
func TestPutMultiZeroKeys(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
if _, err := nds.PutMulti(c, []*datastore.Key{},
[]interface{}{}); err != nil {
t.Fatal(err)
}
}
开发者ID:zeekay,项目名称:nds,代码行数:9,代码来源:put_test.go
示例8: TestGetMultiInterface
func TestGetMultiInterface(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Get from datastore.
response := make([]interface{}, len(keys))
for i := 0; i < len(response); i++ {
response[i] = &testEntity{}
}
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if te, ok := response[i].(*testEntity); ok {
if te.IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
} else {
t.Fatal("incorrect type")
}
}
// Get from cache.
response = make([]interface{}, len(keys))
for i := 0; i < len(response); i++ {
response[i] = &testEntity{}
}
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if te, ok := response[i].(*testEntity); ok {
if te.IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
} else {
t.Fatal("incorrect type")
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:56,代码来源:get_test.go
示例9: TestPutMultiNoPropertyList
func TestPutMultiNoPropertyList(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
pl := datastore.PropertyList{datastore.Property{}}
if _, err := nds.PutMulti(c, keys, pl); err == nil {
t.Fatal("expecting no PropertyList error")
}
}
开发者ID:zeekay,项目名称:nds,代码行数:11,代码来源:put_test.go
示例10: TestGetMultiLockReturnUnknown
func TestGetMultiLockReturnUnknown(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
memcacheGetChan := make(chan func(c context.Context, keys []string) (
map[string]*memcache.Item, error), 2)
memcacheGetChan <- memcache.GetMulti
memcacheGetChan <- func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
items, err := memcache.GetMulti(c, keys)
if err != nil {
return nil, err
}
// Unknown lock values.
items[keys[0]].Flags = 23
items[keys[1]].Flags = 24
return items, nil
}
nds.SetMemcacheGetMulti(func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
f := <-memcacheGetChan
return f(c, keys)
})
response := make([]testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
defer nds.SetMemcacheGetMulti(memcache.GetMulti)
for i := 0; i < len(keys); i++ {
if entities[i].IntVal != response[i].IntVal {
t.Fatal("IntVal not equal")
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:52,代码来源:get_test.go
示例11: TestDeleteMulti
func TestDeleteMulti(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type TestEntity struct {
Value int
}
for _, count := range []int{499, 500, 501} {
keys := make([]*datastore.Key, count)
entities := make([]TestEntity, count)
for i := range keys {
keys[i] = datastore.NewKey(c, "TestEntity", strconv.Itoa(i), 0, nil)
entities[i] = TestEntity{i}
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Prime cache.
entities = make([]TestEntity, count)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
if err := nds.DeleteMulti(c, keys); err != nil {
t.Fatal(err)
}
err := nds.GetMulti(c, keys, make([]TestEntity, count))
if err == nil {
t.Fatal("expect error")
}
me, ok := err.(appengine.MultiError)
if !ok {
t.Fatal("should be MultiError")
}
for _, e := range me {
if e != datastore.ErrNoSuchEntity {
t.Fatal("expected ErrNoSuchEntity")
}
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:48,代码来源:delete_test.go
示例12: NewGaeStore
// Creates and configures a store that stores entities in Google AppEngines memcache and datastore.
// github.com/qedus/nds is used for strongly consistent automatic caching.
func NewGaeStore(kind string, ctx context.Context, idf sus.IdFactory, vf sus.VersionFactory, ei sus.EntityInitializer) sus.Store {
getKey := func(ctx context.Context, id string) *datastore.Key {
return datastore.NewKey(ctx, kind, id, 0, nil)
}
getMulti := func(ids []string) (vs []sus.Version, err error) {
count := len(ids)
vs = make([]sus.Version, count, count)
ks := make([]*datastore.Key, count, count)
for i := 0; i < count; i++ {
vs[i] = vf()
ks[i] = getKey(ctx, ids[i])
}
err = nds.GetMulti(ctx, ks, vs)
return
}
putMulti := func(ids []string, vs []sus.Version) (err error) {
count := len(ids)
ks := make([]*datastore.Key, count, count)
for i := 0; i < count; i++ {
ks[i] = getKey(ctx, ids[i])
}
_, err = nds.PutMulti(ctx, ks, vs)
return
}
delMulti := func(ids []string) error {
count := len(ids)
ks := make([]*datastore.Key, count, count)
for i := 0; i < count; i++ {
ks[i] = getKey(ctx, ids[i])
}
return nds.DeleteMulti(ctx, ks)
}
isNonExtantError := func(err error) bool {
return err.Error() == datastore.ErrNoSuchEntity.Error()
}
rit := func(tran sus.Transaction) error {
return nds.RunInTransaction(ctx, func(ctx context.Context) error {
return tran()
}, &datastore.TransactionOptions{XG: true})
}
return sus.NewStore(getMulti, putMulti, delMulti, idf, vf, ei, isNonExtantError, rit)
}
开发者ID:robsix,项目名称:rps,代码行数:50,代码来源:gus.go
示例13: TestGetMultiInterfaceError
func TestGetMultiInterfaceError(t *testing.T) {
c, closeFunc := NewContext(t, nil)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Get from datastore.
// No errors expected.
response := []interface{}{&testEntity{}, &testEntity{}}
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if te, ok := response[i].(*testEntity); ok {
if te.IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
} else {
t.Fatal("incorrect type")
}
}
// Get from cache.
// Errors expected.
response = []interface{}{&testEntity{}, testEntity{}}
if err := nds.GetMulti(c, keys, response); err == nil {
t.Fatal("expected invalid entity type error")
}
}
开发者ID:k2wanko,项目名称:nds,代码行数:43,代码来源:get_test.go
示例14: TestGetMultiNoErrors
func TestGetMultiNoErrors(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
for _, count := range []int{999, 1000, 1001} {
// Create entities.
keys := []*datastore.Key{}
entities := []*testEntity{}
for i := 0; i < count; i++ {
key := datastore.NewKey(c, "Test", strconv.Itoa(i), 0, nil)
keys = append(keys, key)
entities = append(entities, &testEntity{i})
}
// Save entities.
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
respEntities := []testEntity{}
for range keys {
respEntities = append(respEntities, testEntity{})
}
if err := nds.GetMulti(c, keys, respEntities); err != nil {
t.Fatal(err)
}
// Check respEntities are in order.
for i, re := range respEntities {
if re.Val != entities[i].Val {
t.Fatalf("respEntities in wrong order, %d vs %d", re.Val,
entities[i].Val)
}
}
}
}
开发者ID:jongillham,项目名称:nds,代码行数:42,代码来源:nds_test.go
示例15: TestGetMultiStructPtrNil
func TestGetMultiStructPtrNil(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Get from datastore.
response := make([]*testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if response[i].IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
}
// Get from cache.
response = make([]*testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if response[i].IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:41,代码来源:get_test.go
示例16: TestDelete
func TestDelete(t *testing.T) {
c, closeFunc := NewContext(t, nil)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
keys := []*datastore.Key{key}
entities := make([]testEntity, 1)
entities[0].Val = 43
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entities = make([]testEntity, 1)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entity := entities[0]
if entity.Val != 43 {
t.Fatal("incorrect entity.Val", entity.Val)
}
if err := nds.DeleteMulti(c, keys); err != nil {
t.Fatal(err)
}
keys = []*datastore.Key{key}
entities = make([]testEntity, 1)
err := nds.GetMulti(c, keys, entities)
if me, ok := err.(appengine.MultiError); ok {
if me[0] != datastore.ErrNoSuchEntity {
t.Fatal("entity should be deleted", entities)
}
} else {
t.Fatal("expected appengine.MultiError")
}
}
开发者ID:robsix,项目名称:rps,代码行数:41,代码来源:delete_test.go
示例17: TestPutMultiError
func TestPutMultiError(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
expectedErrs := appengine.MultiError{
nil,
errors.New("expected error"),
}
nds.SetDatastorePutMulti(func(c context.Context,
keys []*datastore.Key, vals interface{}) ([]*datastore.Key, error) {
return keys, expectedErrs
})
defer nds.SetDatastorePutMulti(datastore.PutMulti)
keys := []*datastore.Key{
datastore.NewKey(c, "Test", "", 1, nil),
datastore.NewKey(c, "Test", "", 2, nil),
}
type TestEntity struct {
Value int
}
entities := []TestEntity{
{1},
{2},
}
_, err := nds.PutMulti(c, keys, entities)
me, ok := err.(appengine.MultiError)
if !ok {
t.Fatal("expected appengine.MultiError")
}
for i, e := range me {
if e != expectedErrs[i] {
t.Fatal("error incorrect")
}
}
}
开发者ID:zeekay,项目名称:nds,代码行数:39,代码来源:put_test.go
示例18: TestPutMultiUnlockMemcacheSuccess
// Make sure PutMulti still works if we have a memcache unlock failure.
func TestPutMultiUnlockMemcacheSuccess(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int
}
nds.SetMemcacheDeleteMulti(func(c context.Context, keys []string) error {
return errors.New("expected error")
})
defer func() {
nds.SetMemcacheDeleteMulti(memcache.DeleteMulti)
}()
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
vals := []testEntity{testEntity{42}}
if _, err := nds.PutMulti(c, keys, vals); err != nil {
t.Fatal(err)
}
}
开发者ID:zeekay,项目名称:nds,代码行数:24,代码来源:put_test.go
示例19: TestPutMultiLockFailure
func TestPutMultiLockFailure(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int
}
nds.SetMemcacheSetMulti(func(c context.Context,
items []*memcache.Item) error {
return errors.New("expected error")
})
defer func() {
nds.SetMemcacheSetMulti(memcache.SetMulti)
}()
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
vals := []testEntity{testEntity{42}}
if _, err := nds.PutMulti(c, keys, vals); err == nil {
t.Fatal("expected nds.PutMulti error")
}
}
开发者ID:zeekay,项目名称:nds,代码行数:24,代码来源:put_test.go
示例20: TestGetMultiPaths
//.........这里部分代码省略.........
},
{
"memcache get no entity unmarshal fail",
2,
1,
[]memcacheGetMultiFunc{
memcache.GetMulti,
func(c context.Context, keys []string) (
map[string]*memcache.Item, error) {
items, err := memcache.GetMulti(c, keys)
// Corrupt flags with unknown number.
for _, item := range items {
item.Flags = nds.EntityItem
}
return items, err
},
},
memcache.AddMulti,
memcache.CompareAndSwapMulti,
datastore.GetMulti,
nds.MarshalPropertyList,
[]unmarshalFunc{
nds.UnmarshalPropertyList,
nds.UnmarshalPropertyList,
},
[]error{nil},
},
}
for _, test := range tests {
t.Log("Start", test.description)
keys, putVals := keysVals(c, test.keyCount)
if _, err := nds.PutMulti(c, keys, putVals); err != nil {
t.Fatal(err)
}
memcacheGetChan := make(chan memcacheGetMultiFunc,
len(test.memcacheGetMultis))
for _, fn := range test.memcacheGetMultis {
memcacheGetChan <- fn
}
nds.SetMemcacheGetMulti(func(c context.Context, keys []string) (
map[string]*memcache.Item, error) {
fn := <-memcacheGetChan
return fn(c, keys)
})
nds.SetMemcacheAddMulti(test.memcacheAddMulti)
nds.SetMemcacheCompareAndSwapMulti(test.memcacheCompareAndSwapMulti)
nds.SetDatastoreGetMulti(test.datastoreGetMulti)
nds.SetMarshal(test.marshal)
unmarshalChan := make(chan unmarshalFunc,
len(test.unmarshals))
for _, fn := range test.unmarshals {
unmarshalChan <- fn
}
nds.SetUnmarshal(func(data []byte, pl *datastore.PropertyList) error {
fn := <-unmarshalChan
开发者ID:zeekay,项目名称:nds,代码行数:67,代码来源:get_test.go
注:本文中的github.com/qedus/nds.PutMulti函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论