当前位置: 首页>>代码示例>>Golang>>正文


Golang Impl.Create方法代码示例

本文整理汇总了Golang中github.com/youtube/vitess/go/vt/topo.Impl.Create方法的典型用法代码示例。如果您正苦于以下问题:Golang Impl.Create方法的具体用法?Golang Impl.Create怎么用?Golang Impl.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/youtube/vitess/go/vt/topo.Impl的用法示例。


在下文中一共展示了Impl.Create方法的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)
	}
}
开发者ID:erzel,项目名称:vitess,代码行数:62,代码来源:directory.go

示例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)
//.........这里部分代码省略.........
开发者ID:erzel,项目名称:vitess,代码行数:101,代码来源:file.go


注:本文中的github.com/youtube/vitess/go/vt/topo.Impl.Create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。