本文整理汇总了Golang中github.com/attic-labs/noms/go/datas.NewDatabase函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDatabase函数的具体用法?Golang NewDatabase怎么用?Golang NewDatabase使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDatabase函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestSync
func (s *testSuite) TestSync() {
source1 := dataset.NewDataset(datas.NewDatabase(chunks.NewLevelDBStore(s.LdbDir, "", 1, false)), "foo")
source1, err := source1.Commit(types.Number(42))
s.NoError(err)
source2, err := source1.Commit(types.Number(43))
s.NoError(err)
source1HeadRef := source1.Head().Hash()
source2.Database().Close() // Close Database backing both Datasets
sourceSpec := test_util.CreateValueSpecString("ldb", s.LdbDir, source1HeadRef.String())
ldb2dir := path.Join(s.TempDir, "ldb2")
sinkDatasetSpec := test_util.CreateValueSpecString("ldb", ldb2dir, "bar")
out := s.Run(main, []string{sourceSpec, sinkDatasetSpec})
s.Equal("", out)
dest := dataset.NewDataset(datas.NewDatabase(chunks.NewLevelDBStore(ldb2dir, "", 1, false)), "bar")
s.True(types.Number(42).Equals(dest.HeadValue()))
dest.Database().Close()
sourceDataset := test_util.CreateValueSpecString("ldb", s.LdbDir, "foo")
out = s.Run(main, []string{sourceDataset, sinkDatasetSpec})
s.Equal("", out)
dest = dataset.NewDataset(datas.NewDatabase(chunks.NewLevelDBStore(ldb2dir, "", 1, false)), "bar")
s.True(types.Number(43).Equals(dest.HeadValue()))
dest.Database().Close()
}
开发者ID:willhite,项目名称:noms-old,代码行数:27,代码来源:noms_sync_test.go
示例2: Database
func (spec DatabaseSpec) Database() (ds datas.Database, err error) {
switch spec.Protocol {
case "http", "https":
ds = datas.NewRemoteDatabase(spec.String(), "Bearer "+spec.accessToken)
case "ldb":
ds = datas.NewDatabase(chunks.NewLevelDBStoreUseFlags(spec.Path, ""))
case "mem":
ds = datas.NewDatabase(chunks.NewMemoryStore())
default:
err = fmt.Errorf("Invalid path prototocol: %s", spec.Protocol)
}
return
}
开发者ID:willhite,项目名称:noms-old,代码行数:13,代码来源:dataspec.go
示例3: TestCSVImporterFromBlob
func (s *testSuite) TestCSVImporterFromBlob() {
test := func(pathFlag string) {
defer os.RemoveAll(s.LdbDir)
newDB := func() datas.Database {
cs := chunks.NewLevelDBStore(s.LdbDir, "", 1, false)
return datas.NewDatabase(cs)
}
db := newDB()
rawDS := dataset.NewDataset(db, "raw")
csv := &bytes.Buffer{}
writeCSV(csv)
rawDS.CommitValue(types.NewBlob(csv))
db.Close()
stdout, stderr := s.Run(main, []string{
"--no-progress", "--column-types", "String,Number",
pathFlag, spec.CreateValueSpecString("ldb", s.LdbDir, "raw.value"),
spec.CreateValueSpecString("ldb", s.LdbDir, "csv"),
})
s.Equal("", stdout)
s.Equal("", stderr)
db = newDB()
defer db.Close()
csvDS := dataset.NewDataset(db, "csv")
validateCSV(s, csvDS.HeadValue().(types.List))
}
test("--path")
test("-p")
}
开发者ID:Richardphp,项目名称:noms,代码行数:32,代码来源:importer_test.go
示例4: TestReadRef
func TestReadRef(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir(os.TempDir(), "")
assert.NoError(err)
datasetId := "dsName"
ldbPath := path.Join(dir, "/name")
cs1 := chunks.NewLevelDBStoreUseFlags(ldbPath, "")
database1 := datas.NewDatabase(cs1)
dataset1 := dataset.NewDataset(database1, datasetId)
commit := types.String("Commit Value")
dataset1, err = dataset1.Commit(commit)
assert.NoError(err)
r1 := dataset1.Head().Hash()
dataset1.Database().Close()
spec2 := fmt.Sprintf("ldb:%s::%s", ldbPath, r1.String())
sp2, err := ParsePathSpec(spec2)
assert.NoError(err)
database, v2, err := sp2.Value()
assert.NoError(err)
assert.EqualValues(r1.String(), v2.Hash().String())
database.Close()
}
开发者ID:willhite,项目名称:noms-old,代码行数:26,代码来源:dataspec_test.go
示例5: TestCSVImporterWithPipe
func (s *testSuite) TestCSVImporterWithPipe() {
input, err := ioutil.TempFile(s.TempDir, "")
d.Chk.NoError(err)
defer input.Close()
defer os.Remove(input.Name())
_, err = input.WriteString("a|b\n1|2\n")
d.Chk.NoError(err)
setName := "csv"
dataspec := spec.CreateValueSpecString("ldb", s.LdbDir, setName)
stdout, stderr := s.Run(main, []string{"--no-progress", "--column-types", "String,Number", "--delimiter", "|", input.Name(), dataspec})
s.Equal("", stdout)
s.Equal("", stderr)
cs := chunks.NewLevelDBStore(s.LdbDir, "", 1, false)
ds := dataset.NewDataset(datas.NewDatabase(cs), setName)
defer ds.Database().Close()
defer os.RemoveAll(s.LdbDir)
l := ds.HeadValue().(types.List)
s.Equal(uint64(1), l.Len())
v := l.Get(0)
st := v.(types.Struct)
s.Equal(types.String("1"), st.Get("a"))
s.Equal(types.Number(2), st.Get("b"))
}
开发者ID:Richardphp,项目名称:noms,代码行数:27,代码来源:importer_test.go
示例6: TestCSVImporterWithExternalHeader
func (s *testSuite) TestCSVImporterWithExternalHeader() {
input, err := ioutil.TempFile(s.TempDir, "")
d.Chk.NoError(err)
defer input.Close()
defer os.Remove(input.Name())
_, err = input.WriteString("7,8\n")
d.Chk.NoError(err)
setName := "csv"
dataspec := test_util.CreateValueSpecString("ldb", s.LdbDir, setName)
out := s.Run(main, []string{"-no-progress", "-column-types", "String,Number", "-header", "x,y", dataspec, input.Name()})
s.Equal("", out)
cs := chunks.NewLevelDBStore(s.LdbDir, "", 1, false)
ds := dataset.NewDataset(datas.NewDatabase(cs), setName)
defer ds.Database().Close()
defer os.RemoveAll(s.LdbDir)
l := ds.HeadValue().(types.List)
s.Equal(uint64(1), l.Len())
v := l.Get(0)
st := v.(types.Struct)
s.Equal(types.String("7"), st.Get("x"))
s.Equal(types.Number(8), st.Get("y"))
}
开发者ID:willhite,项目名称:noms-old,代码行数:26,代码来源:importer_test.go
示例7: TestCSVImporterToMap
func (s *testSuite) TestCSVImporterToMap() {
input, err := ioutil.TempFile(s.TempDir, "")
d.Chk.NoError(err)
defer input.Close()
defer os.Remove(input.Name())
_, err = input.WriteString("a,b,c\n")
d.Chk.NoError(err)
for i := 0; i < 20; i++ {
_, err = input.WriteString(fmt.Sprintf("a%d,%d,%d\n", i, i, i*2))
d.Chk.NoError(err)
}
_, err = input.Seek(0, 0)
d.Chk.NoError(err)
setName := "csv"
dataspec := test_util.CreateValueSpecString("ldb", s.LdbDir, setName)
out := s.Run(main, []string{"-no-progress", "-column-types", "String,Number,Number", "-dest-type", "map:1", dataspec, input.Name()})
s.Equal("", out)
cs := chunks.NewLevelDBStore(s.LdbDir, "", 1, false)
ds := dataset.NewDataset(datas.NewDatabase(cs), setName)
defer ds.Database().Close()
defer os.RemoveAll(s.LdbDir)
m := ds.HeadValue().(types.Map)
s.Equal(uint64(20), m.Len())
for i := 0; i < 20; i++ {
m.Get(types.Number(i)).(types.Struct).Equals(types.NewStruct("", map[string]types.Value{
"a": types.String(fmt.Sprintf("a%d", i)),
"c": types.Number(i * 2),
}))
}
}
开发者ID:willhite,项目名称:noms-old,代码行数:35,代码来源:importer_test.go
示例8: TestLDBDatabase
func TestLDBDatabase(t *testing.T) {
assert := assert.New(t)
d1 := os.TempDir()
dir, err := ioutil.TempDir(d1, "flags")
assert.NoError(err)
ldbDir := path.Join(dir, "store")
spec := fmt.Sprintf("ldb:%s", path.Join(dir, "store"))
cs := chunks.NewLevelDBStoreUseFlags(ldbDir, "")
ds := datas.NewDatabase(cs)
s1 := types.String("A String")
s1Ref := ds.WriteValue(s1)
ds.Commit("testDs", datas.NewCommit().Set(datas.ValueField, s1Ref))
ds.Close()
sp, errRead := ParseDatabaseSpec(spec)
assert.NoError(errRead)
store, err := sp.Database()
assert.NoError(err)
assert.Equal(s1, store.ReadValue(s1.Hash()))
store.Close()
os.Remove(dir)
}
开发者ID:willhite,项目名称:noms-old,代码行数:25,代码来源:dataspec_test.go
示例9: TestCSVImportSkipRecordsCustomHeader
func (s *testSuite) TestCSVImportSkipRecordsCustomHeader() {
input, err := ioutil.TempFile(s.TempDir, "")
d.Chk.NoError(err)
defer input.Close()
defer os.Remove(input.Name())
_, err = input.WriteString("a,b\n")
d.Chk.NoError(err)
_, err = input.WriteString("7,8\n")
d.Chk.NoError(err)
setName := "csv"
dataspec := spec.CreateValueSpecString("ldb", s.LdbDir, setName)
stdout, stderr := s.Run(main, []string{"--no-progress", "--skip-records", "1", "--header", "x,y", input.Name(), dataspec})
s.Equal("", stdout)
s.Equal("", stderr)
cs := chunks.NewLevelDBStore(s.LdbDir, "", 1, false)
ds := dataset.NewDataset(datas.NewDatabase(cs), setName)
defer ds.Database().Close()
defer os.RemoveAll(s.LdbDir)
l := ds.HeadValue().(types.List)
s.Equal(uint64(1), l.Len())
v := l.Get(0)
st := v.(types.Struct)
s.Equal(types.String("7"), st.Get("x"))
s.Equal(types.String("8"), st.Get("y"))
}
开发者ID:Richardphp,项目名称:noms,代码行数:30,代码来源:importer_test.go
示例10: TestReadToList
func TestReadToList(t *testing.T) {
assert := assert.New(t)
ds := datas.NewDatabase(chunks.NewMemoryStore())
dataString := `a,1,true
b,2,false
`
r := NewCSVReader(bytes.NewBufferString(dataString), ',')
headers := []string{"A", "B", "C"}
kinds := KindSlice{types.StringKind, types.NumberKind, types.BoolKind}
l, typ := ReadToList(r, "test", headers, kinds, ds)
assert.Equal(uint64(2), l.Len())
assert.Equal(types.StructKind, typ.Kind())
desc, ok := typ.Desc.(types.StructDesc)
assert.True(ok)
assert.Equal(desc.Len(), 3)
assert.Equal(types.StringKind, desc.Field("A").Kind())
assert.Equal(types.NumberKind, desc.Field("B").Kind())
assert.Equal(types.BoolKind, desc.Field("C").Kind())
assert.True(l.Get(0).(types.Struct).Get("A").Equals(types.String("a")))
assert.True(l.Get(1).(types.Struct).Get("A").Equals(types.String("b")))
assert.True(l.Get(0).(types.Struct).Get("B").Equals(types.Number(1)))
assert.True(l.Get(1).(types.Struct).Get("B").Equals(types.Number(2)))
assert.True(l.Get(0).(types.Struct).Get("C").Equals(types.Bool(true)))
assert.True(l.Get(1).(types.Struct).Get("C").Equals(types.Bool(false)))
}
开发者ID:willhite,项目名称:noms-old,代码行数:33,代码来源:read_test.go
示例11: TestReadToMap
func TestReadToMap(t *testing.T) {
assert := assert.New(t)
ds := datas.NewDatabase(chunks.NewMemoryStore())
dataString := `a,1,true
b,2,false
`
r := NewCSVReader(bytes.NewBufferString(dataString), ',')
headers := []string{"A", "B", "C"}
kinds := KindSlice{types.StringKind, types.NumberKind, types.BoolKind}
m := ReadToMap(r, headers, 0, kinds, ds)
assert.Equal(uint64(2), m.Len())
assert.True(m.Type().Equals(
types.MakeMapType(types.StringType, types.MakeStructType("", map[string]*types.Type{
"B": types.NumberType,
"C": types.BoolType,
}))))
assert.True(m.Get(types.String("a")).Equals(types.NewStruct("", map[string]types.Value{
"B": types.Number(1),
"C": types.Bool(true),
})))
assert.True(m.Get(types.String("b")).Equals(types.NewStruct("", map[string]types.Value{
"B": types.Number(2),
"C": types.Bool(false),
})))
}
开发者ID:willhite,项目名称:noms-old,代码行数:29,代码来源:read_test.go
示例12: TestLDBObject
func TestLDBObject(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir(os.TempDir(), "")
assert.NoError(err)
ldbpath := path.Join(dir, "xx-yy")
dsId := "dsId"
cs1 := chunks.NewLevelDBStoreUseFlags(ldbpath, "")
store1 := datas.NewDatabase(cs1)
dataset1 := dataset.NewDataset(store1, dsId)
s1 := types.String("Commit Value")
r1 := store1.WriteValue(s1)
_, err = dataset1.Commit(r1)
assert.NoError(err)
store1.Close()
spec2 := fmt.Sprintf("ldb:%s::%s", ldbpath, dsId)
assert.NoError(err)
sp1, err := ParseDatasetSpec(spec2)
assert.NoError(err)
dataset2, err := sp1.Dataset()
assert.NoError(err)
r2 := dataset2.HeadValue()
s2 := r2.(types.Ref).TargetValue(dataset2.Database())
assert.Equal(s1, s2)
dataset2.Database().Close()
spec3 := fmt.Sprintf("ldb:%s::%s", ldbpath, s1.Hash().String())
sp3, err := ParsePathSpec(spec3)
database, v3, err := sp3.Value()
assert.Equal(s1, v3)
database.Close()
}
开发者ID:willhite,项目名称:noms-old,代码行数:33,代码来源:dataspec_test.go
示例13: TestLDBDataset
func TestLDBDataset(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir(os.TempDir(), "")
assert.NoError(err)
ldbPath := path.Join(dir, "name")
cs := chunks.NewLevelDBStoreUseFlags(ldbPath, "")
ds := datas.NewDatabase(cs)
id := "dsName"
set := dataset.NewDataset(ds, id)
commit := types.String("Commit Value")
set, err = set.Commit(commit)
assert.NoError(err)
ds.Close()
spec := fmt.Sprintf("ldb:%s::%s", ldbPath, id)
sp, err := ParseDatasetSpec(spec)
assert.NoError(err)
dataset, err := sp.Dataset()
assert.NoError(err)
assert.EqualValues(commit, dataset.HeadValue())
os.Remove(dir)
}
开发者ID:willhite,项目名称:noms-old,代码行数:25,代码来源:dataspec_test.go
示例14: TestWriteValue
func TestWriteValue(t *testing.T) {
assert := assert.New(t)
factory := chunks.NewMemoryStoreFactory()
defer factory.Shutter()
router = setupWebServer(factory)
defer func() { router = nil }()
testString := "Now, what?"
authKey = "anauthkeyvalue"
w := httptest.NewRecorder()
r, err := newRequest("GET", dbName+constants.RootPath, nil)
assert.NoError(err)
router.ServeHTTP(w, r)
lastRoot := w.Body
assert.Equal(http.StatusOK, w.Code)
tval := types.Bool(true)
wval := types.String(testString)
chunk1 := types.EncodeValue(tval, nil)
chunk2 := types.EncodeValue(wval, nil)
refList := types.NewList(types.NewRef(tval), types.NewRef(wval))
chunk3 := types.EncodeValue(refList, nil)
body := &bytes.Buffer{}
// we would use this func, but it's private so use next line instead: serializeHints(body, map[ref.Ref]struct{}{hint: struct{}{}})
err = binary.Write(body, binary.BigEndian, uint32(0))
assert.NoError(err)
chunks.Serialize(chunk1, body)
chunks.Serialize(chunk2, body)
chunks.Serialize(chunk3, body)
w = httptest.NewRecorder()
r, err = newRequest("POST", dbName+constants.WriteValuePath+"?access_token="+authKey, ioutil.NopCloser(body))
assert.NoError(err)
router.ServeHTTP(w, r)
assert.Equal(http.StatusCreated, w.Code)
w = httptest.NewRecorder()
args := fmt.Sprintf("&last=%s¤t=%s", lastRoot, types.NewRef(refList).TargetHash())
r, _ = newRequest("POST", dbName+constants.RootPath+"?access_token="+authKey+args, ioutil.NopCloser(body))
router.ServeHTTP(w, r)
assert.Equal(http.StatusOK, w.Code)
whash := wval.Hash()
hints := map[hash.Hash]struct{}{whash: struct{}{}}
rdr := buildGetRefsRequestBody(hints)
r, _ = newRequest("POST", dbName+constants.GetRefsPath, rdr)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
router.ServeHTTP(w, r)
assert.Equal(http.StatusOK, w.Code)
ms := chunks.NewMemoryStore()
chunks.Deserialize(w.Body, ms, nil)
v := types.DecodeValue(ms.Get(whash), datas.NewDatabase(ms))
assert.Equal(testString, string(v.(types.String)))
}
开发者ID:kalman,项目名称:noms-pre-release,代码行数:59,代码来源:web_server_test.go
示例15: Database
func (spec databaseSpec) Database() (ds datas.Database, err error) {
switch spec.Protocol {
case "http", "https":
err = d.Unwrap(d.Try(func() {
ds = datas.NewRemoteDatabase(spec.String(), "Bearer "+spec.accessToken)
}))
case "ldb":
err = d.Unwrap(d.Try(func() {
ds = datas.NewDatabase(getLDBStore(spec.Path))
}))
case "mem":
ds = datas.NewDatabase(chunks.NewMemoryStore())
default:
err = fmt.Errorf("Invalid path prototocol: %s", spec.Protocol)
}
return
}
开发者ID:Richardphp,项目名称:noms,代码行数:17,代码来源:dataspec.go
示例16: TestDuplicateHeaderName
func TestDuplicateHeaderName(t *testing.T) {
assert := assert.New(t)
ds := datas.NewDatabase(chunks.NewMemoryStore())
dataString := "1,2\n3,4\n"
r := NewCSVReader(bytes.NewBufferString(dataString), ',')
headers := []string{"A", "A"}
kinds := KindSlice{types.StringKind, types.StringKind}
assert.Panics(func() { ReadToList(r, "test", headers, kinds, ds) })
}
开发者ID:willhite,项目名称:noms-old,代码行数:9,代码来源:read_test.go
示例17: TestCSVExporter
// FIXME: run with pipe
func (s *testSuite) TestCSVExporter() {
setName := "csv"
header := []string{"a", "b", "c"}
payload := [][]string{
[]string{"5", "7", "100"},
[]string{"4", "10", "255"},
[]string{"512", "12", "55"},
}
structName := "SomeStruct"
// Setup data store
cs := chunks.NewLevelDBStore(s.LdbDir, "", 1, false)
ds := dataset.NewDataset(datas.NewDatabase(cs), setName)
// Build Struct fields based on header
f := make(types.TypeMap, len(header))
for _, key := range header {
f[key] = types.StringType
}
typ := types.MakeStructType(structName, f)
// Build data rows
structs := make([]types.Value, len(payload))
for i, row := range payload {
fields := make(map[string]types.Value)
for j, v := range row {
name := header[j]
fields[name] = types.String(v)
}
structs[i] = types.NewStructWithType(typ, fields)
}
ds.Commit(types.NewList(structs...))
ds.Database().Close()
// Run exporter
dataspec := test_util.CreateValueSpecString("ldb", s.LdbDir, setName)
out := s.Run(main, []string{dataspec})
// Verify output
csvReader := csv.NewReader(strings.NewReader(out))
row, err := csvReader.Read()
d.Chk.NoError(err)
s.Equal(header, row)
for i := 0; i < len(payload); i++ {
row, err := csvReader.Read()
d.Chk.NoError(err)
s.Equal(payload[i], row)
}
_, err = csvReader.Read()
s.Equal(io.EOF, err)
}
开发者ID:willhite,项目名称:noms-old,代码行数:57,代码来源:exporter_test.go
示例18: TestEscapeFieldNames
func TestEscapeFieldNames(t *testing.T) {
assert := assert.New(t)
ds := datas.NewDatabase(chunks.NewMemoryStore())
dataString := "1\n"
r := NewCSVReader(bytes.NewBufferString(dataString), ',')
headers := []string{"A A"}
kinds := KindSlice{types.NumberKind}
l, _ := ReadToList(r, "test", headers, kinds, ds)
assert.Equal(uint64(1), l.Len())
assert.Equal(types.Number(1), l.Get(0).(types.Struct).Get(types.EscapeStructField("A A")))
}
开发者ID:willhite,项目名称:noms-old,代码行数:11,代码来源:read_test.go
示例19: TestIdValidation
func TestIdValidation(t *testing.T) {
assert := assert.New(t)
store := datas.NewDatabase(chunks.NewMemoryStore())
invalidDatasetNames := []string{" ", "", "a ", " a", "$", "#", ":", "\n", "💩"}
for _, id := range invalidDatasetNames {
assert.Panics(func() {
NewDataset(store, id)
})
}
}
开发者ID:Richardphp,项目名称:noms,代码行数:11,代码来源:dataset_test.go
示例20: TestEmptyNomsDs
func (s *testSuite) TestEmptyNomsDs() {
dir := s.LdbDir
cs := chunks.NewLevelDBStore(dir+"/name", "", 24, false)
ds := datas.NewDatabase(cs)
ds.Close()
dbSpec := test_util.CreateDatabaseSpecString("ldb", dir+"/name")
rtnVal := s.Run(main, []string{dbSpec})
s.Equal("", rtnVal)
}
开发者ID:willhite,项目名称:noms-old,代码行数:12,代码来源:noms_ds_test.go
注:本文中的github.com/attic-labs/noms/go/datas.NewDatabase函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论