本文整理汇总了Golang中github.com/youtube/vitess/go/vt/topo.Impl.Delete方法的典型用法代码示例。如果您正苦于以下问题:Golang Impl.Delete方法的具体用法?Golang Impl.Delete怎么用?Golang Impl.Delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/youtube/vitess/go/vt/topo.Impl
的用法示例。
在下文中一共展示了Impl.Delete方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: checkDirectoryInCell
func checkDirectoryInCell(t *testing.T, ts topo.Impl, cell string) {
t.Logf("=== checkDirectoryInCell %v", cell)
ctx := context.Background()
// ListDir root: nothing
checkListDir(ctx, t, ts, cell, "/", nil)
// Create a topolevel entry
version, err := ts.Create(ctx, cell, "/MyFile", []byte{'a'})
if err != nil {
t.Fatalf("cannot create toplevel file: %v", err)
}
// ListDir should return it.
checkListDir(ctx, t, ts, cell, "/", []string{"MyFile"})
// Delete it, it should be gone.
if err := ts.Delete(ctx, cell, "/MyFile", version); err != nil {
t.Fatalf("cannot delete toplevel file: %v", err)
}
checkListDir(ctx, t, ts, cell, "/", nil)
// Create a file 3 layers down.
version, err = ts.Create(ctx, cell, "/types/name/MyFile", []byte{'a'})
if err != nil {
t.Fatalf("cannot create deep file: %v", err)
}
// Check listing at all levels.
checkListDir(ctx, t, ts, cell, "/", []string{"types"})
checkListDir(ctx, t, ts, cell, "/types/", []string{"name"})
checkListDir(ctx, t, ts, cell, "/types/name/", []string{"MyFile"})
// Add a second file
version2, err := ts.Create(ctx, cell, "/types/othername/MyFile", []byte{'a'})
if err != nil {
t.Fatalf("cannot create deep file2: %v", err)
}
// Check entries at all levels
checkListDir(ctx, t, ts, cell, "/", []string{"types"})
checkListDir(ctx, t, ts, cell, "/types/", []string{"name", "othername"})
checkListDir(ctx, t, ts, cell, "/types/name/", []string{"MyFile"})
checkListDir(ctx, t, ts, cell, "/types/othername/", []string{"MyFile"})
// Delete the first file, expect all lists to return the second one.
if err := ts.Delete(ctx, cell, "/types/name/MyFile", version); err != nil {
t.Fatalf("cannot delete deep file: %v", err)
}
checkListDir(ctx, t, ts, cell, "/", []string{"types"})
checkListDir(ctx, t, ts, cell, "/types/", []string{"othername"})
checkListDir(ctx, t, ts, cell, "/types/name/", nil)
checkListDir(ctx, t, ts, cell, "/types/othername/", []string{"MyFile"})
// Delete the second file, expect all lists to return nothing.
if err := ts.Delete(ctx, cell, "/types/othername/MyFile", version2); err != nil {
t.Fatalf("cannot delete second deep file: %v", err)
}
for _, dir := range []string{"/", "/types/", "/types/name/", "/types/othername/"} {
checkListDir(ctx, t, ts, cell, dir, nil)
}
}
示例2: checkFileInCell
func checkFileInCell(t *testing.T, ts topo.Impl, cell string) {
t.Logf("=== checkFileInCell %v", cell)
ctx := context.Background()
// ListDir root: nothing.
checkListDir(ctx, t, ts, cell, "/", nil)
// Get with no file -> ErrNoNode.
contents, version, err := ts.Get(ctx, cell, "/myfile")
if err != topo.ErrNoNode {
t.Errorf("Get(non-existent) didn't return ErrNoNode but: %v", err)
}
// Create a file.
version, err = ts.Create(ctx, cell, "/myfile", []byte{'a'})
if err != nil {
t.Fatalf("Create('/myfile') failed: %v", err)
}
// See it in the listing now.
checkListDir(ctx, t, ts, cell, "/", []string{"myfile"})
// Get should work, get the right contents and version.
contents, getVersion, err := ts.Get(ctx, cell, "/myfile")
if err != nil {
t.Errorf("Get('/myfile') returned an error: %v", err)
} else {
if len(contents) != 1 || contents[0] != 'a' {
t.Errorf("Get('/myfile') returned bad content: %v", contents)
}
if !reflect.DeepEqual(getVersion, version) {
t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, version)
}
}
// Update it, make sure version changes.
newVersion, err := ts.Update(ctx, cell, "/myfile", []byte{'b'}, version)
if err != nil {
t.Fatalf("Update('/myfile') failed: %v", err)
}
if reflect.DeepEqual(version, newVersion) {
t.Errorf("Version didn't change, stayed %v", newVersion)
}
// Get should work, get the right contents and version.
contents, getVersion, err = ts.Get(ctx, cell, "/myfile")
if err != nil {
t.Errorf("Get('/myfile') returned an error: %v", err)
} else {
if len(contents) != 1 || contents[0] != 'b' {
t.Errorf("Get('/myfile') returned bad content: %v", contents)
}
if !reflect.DeepEqual(getVersion, newVersion) {
t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, newVersion)
}
}
// Try to update again with wrong version, should fail.
if _, err = ts.Update(ctx, cell, "/myfile", []byte{'b'}, version); err != topo.ErrBadVersion {
t.Errorf("Update(bad version) didn't return ErrBadVersion but: %v", err)
}
// Try to update again with nil version, should work.
newVersion, err = ts.Update(ctx, cell, "/myfile", []byte{'c'}, nil)
if err != nil {
t.Errorf("Update(nil version) should have worked but got: %v", err)
}
// Get should work, get the right contents and version.
contents, getVersion, err = ts.Get(ctx, cell, "/myfile")
if err != nil {
t.Errorf("Get('/myfile') returned an error: %v", err)
} else {
if len(contents) != 1 || contents[0] != 'c' {
t.Errorf("Get('/myfile') returned bad content: %v", contents)
}
if !reflect.DeepEqual(getVersion, newVersion) {
t.Errorf("Get('/myfile') returned bad version: got %v expected %v", getVersion, newVersion)
}
}
// Try to delete with wrong version, should fail.
if err = ts.Delete(ctx, cell, "/myfile", version); err != topo.ErrBadVersion {
t.Errorf("Delete('/myfile', wrong version) returned bad error: %v", err)
}
// Now delete it.
if err = ts.Delete(ctx, cell, "/myfile", newVersion); err != nil {
t.Fatalf("Delete('/myfile') failed: %v", err)
}
// Try to delete again, should fail.
if err = ts.Delete(ctx, cell, "/myfile", newVersion); err != topo.ErrNoNode {
t.Errorf("Delete(already gone) returned bad error: %v", err)
}
// Create again, with unconditional update.
version, err = ts.Update(ctx, cell, "/myfile", []byte{'d'}, nil)
if err != nil {
t.Fatalf("Update('/myfile', nil) failed: %v", err)
//.........这里部分代码省略.........