本文整理汇总了Golang中github.com/djbarber/ipfs-hack/Godeps/_workspace/src/github.com/jbenet/go-datastore.NewKey函数的典型用法代码示例。如果您正苦于以下问题:Golang NewKey函数的具体用法?Golang NewKey怎么用?Golang NewKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestCoalesceSamePutDiffPut
func TestCoalesceSamePutDiffPut(t *testing.T) {
m := setup()
done := make(chan struct{})
go func() {
m.ds.Put(ds.NewKey("foo"), "bar")
done <- struct{}{}
}()
go func() {
m.ds.Put(ds.NewKey("foo"), "bar")
done <- struct{}{}
}()
go func() {
m.ds.Put(ds.NewKey("foo"), "bar2")
done <- struct{}{}
}()
go func() {
m.ds.Put(ds.NewKey("foo"), "bar3")
done <- struct{}{}
}()
<-done
<-done
<-done
<-done
if m.inside != 3 {
t.Error("incalls should be 3", m.inside)
}
if m.outside != 4 {
t.Error("outcalls should be 4", m.outside)
}
}
示例2: TestBasic
func (ks *DSSuite) TestBasic(c *C) {
var size = 1000
d, err := lru.NewDatastore(size)
c.Check(err, Equals, nil)
for i := 0; i < size; i++ {
err := d.Put(ds.NewKey(strconv.Itoa(i)), i)
c.Check(err, Equals, nil)
}
for i := 0; i < size; i++ {
j, err := d.Get(ds.NewKey(strconv.Itoa(i)))
c.Check(j, Equals, i)
c.Check(err, Equals, nil)
}
for i := 0; i < size; i++ {
err := d.Put(ds.NewKey(strconv.Itoa(i+size)), i)
c.Check(err, Equals, nil)
}
for i := 0; i < size; i++ {
j, err := d.Get(ds.NewKey(strconv.Itoa(i)))
c.Check(j, Equals, nil)
c.Check(err, Equals, ds.ErrNotFound)
}
for i := 0; i < size; i++ {
j, err := d.Get(ds.NewKey(strconv.Itoa(i + size)))
c.Check(j, Equals, i)
c.Check(err, Equals, nil)
}
}
示例3: TestCoalesceHas
func TestCoalesceHas(t *testing.T) {
m := setup()
done := make(chan struct{})
errs := make(chan error, 30)
m.ds.Put(ds.NewKey("foo1"), "bar")
m.ds.Put(ds.NewKey("foo2"), "baz")
for i := 0; i < 10; i++ {
go func() {
v, err := m.ds.Has(ds.NewKey("foo1"))
if err != nil {
errs <- err
}
if !v {
errs <- fmt.Errorf("should have foo1")
}
done <- struct{}{}
}()
}
for i := 0; i < 10; i++ {
go func() {
v, err := m.ds.Has(ds.NewKey("foo2"))
if err != nil {
errs <- err
}
if !v {
errs <- fmt.Errorf("should have foo2")
}
done <- struct{}{}
}()
}
for i := 0; i < 10; i++ {
go func() {
v, err := m.ds.Has(ds.NewKey("foo3"))
if err != nil {
errs <- err
}
if v {
errs <- fmt.Errorf("should not have foo3")
}
done <- struct{}{}
}()
}
for i := 0; i < 30; i++ {
<-done
}
if m.inside != 5 {
t.Error("incalls should be 3", m.inside)
}
if m.outside != 32 {
t.Error("outcalls should be 30", m.outside)
}
}
示例4: openDatastore
// openDatastore returns an error if the config file is not present.
func (r *FSRepo) openDatastore() error {
leveldbPath := path.Join(r.path, leveldbDirectory)
var err error
// save leveldb reference so it can be neatly closed afterward
leveldbDS, err := levelds.NewDatastore(leveldbPath, &levelds.Options{
Compression: ldbopts.NoCompression,
})
if err != nil {
return errors.New("unable to open leveldb datastore")
}
// 4TB of 256kB objects ~=17M objects, splitting that 256-way
// leads to ~66k objects per dir, splitting 256*256-way leads to
// only 256.
//
// The keys seen by the block store have predictable prefixes,
// including "/" from datastore.Key and 2 bytes from multihash. To
// reach a uniform 256-way split, we need approximately 4 bytes of
// prefix.
blocksDS, err := flatfs.New(path.Join(r.path, flatfsDirectory), 4)
if err != nil {
return errors.New("unable to open flatfs datastore")
}
// Add our PeerID to metrics paths to keep them unique
//
// As some tests just pass a zero-value Config to fsrepo.Init,
// cope with missing PeerID.
id := r.config.Identity.PeerID
if id == "" {
// the tests pass in a zero Config; cope with it
id = fmt.Sprintf("uninitialized_%p", r)
}
prefix := "fsrepo." + id + ".datastore."
metricsBlocks := measure.New(prefix+"blocks", blocksDS)
metricsLevelDB := measure.New(prefix+"leveldb", leveldbDS)
mountDS := mount.New([]mount.Mount{
{
Prefix: ds.NewKey("/blocks"),
Datastore: metricsBlocks,
},
{
Prefix: ds.NewKey("/"),
Datastore: metricsLevelDB,
},
})
// Make sure it's ok to claim the virtual datastore from mount as
// threadsafe. There's no clean way to make mount itself provide
// this information without copy-pasting the code into two
// variants. This is the same dilemma as the `[].byte` attempt at
// introducing const types to Go.
var _ ds.ThreadSafeDatastore = blocksDS
var _ ds.ThreadSafeDatastore = leveldbDS
r.ds = ds2.ClaimThreadSafe{mountDS}
return nil
}
示例5: TestCoalesceSameGet
func TestCoalesceSameGet(t *testing.T) {
m := setup()
done := make(chan struct{})
errs := make(chan error, 30)
m.ds.Put(ds.NewKey("foo1"), "bar")
m.ds.Put(ds.NewKey("foo2"), "baz")
for i := 0; i < 10; i++ {
go func() {
v, err := m.ds.Get(ds.NewKey("foo1"))
if err != nil {
errs <- err
}
if v != "bar" {
errs <- fmt.Errorf("v is not bar", v)
}
done <- struct{}{}
}()
}
for i := 0; i < 10; i++ {
go func() {
v, err := m.ds.Get(ds.NewKey("foo2"))
if err != nil {
errs <- err
}
if v != "baz" {
errs <- fmt.Errorf("v is not baz", v)
}
done <- struct{}{}
}()
}
for i := 0; i < 10; i++ {
go func() {
_, err := m.ds.Get(ds.NewKey("foo3"))
if err == nil {
errs <- fmt.Errorf("no error")
}
done <- struct{}{}
}()
}
for i := 0; i < 30; i++ {
<-done
}
if m.inside != 5 {
t.Error("incalls should be 3", m.inside)
}
if m.outside != 32 {
t.Error("outcalls should be 30", m.outside)
}
}
示例6: TestBasic
func (ks *DSSuite) TestBasic(c *C) {
mpds := ds.NewMapDatastore()
nsds := ns.Wrap(mpds, ds.NewKey("abc"))
keys := strsToKeys([]string{
"foo",
"foo/bar",
"foo/bar/baz",
"foo/barb",
"foo/bar/bazb",
"foo/bar/baz/barb",
})
for _, k := range keys {
err := nsds.Put(k, []byte(k.String()))
c.Check(err, Equals, nil)
}
for _, k := range keys {
v1, err := nsds.Get(k)
c.Check(err, Equals, nil)
c.Check(bytes.Equal(v1.([]byte), []byte(k.String())), Equals, true)
v2, err := mpds.Get(ds.NewKey("abc").Child(k))
c.Check(err, Equals, nil)
c.Check(bytes.Equal(v2.([]byte), []byte(k.String())), Equals, true)
}
run := func(d ds.Datastore, q dsq.Query) []ds.Key {
r, err := d.Query(q)
c.Check(err, Equals, nil)
e, err := r.Rest()
c.Check(err, Equals, nil)
return ds.EntryKeys(e)
}
listA := run(mpds, dsq.Query{})
listB := run(nsds, dsq.Query{})
c.Check(len(listA), Equals, len(listB))
// sort them cause yeah.
sort.Sort(ds.KeySlice(listA))
sort.Sort(ds.KeySlice(listB))
for i, kA := range listA {
kB := listB[i]
c.Check(nsds.InvertKey(kA), Equals, kB)
c.Check(kA, Equals, nsds.ConvertKey(kB))
}
}
示例7: lookup
func (d *Datastore) lookup(key ds.Key) (ds.Datastore, ds.Key, ds.Key) {
d.lk.Lock()
defer d.lk.Unlock()
for _, m := range d.mounts {
if m.Prefix.Equal(key) || m.Prefix.IsAncestorOf(key) {
s := strings.TrimPrefix(key.String(), m.Prefix.String())
k := ds.NewKey(s)
return m.Datastore, m.Prefix, k
}
}
return nil, ds.NewKey("/"), key
}
示例8: ConvertKey
// ConvertKey returns a B58 encoded Datastore key
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
func (b58KeyConverter) ConvertKey(dsk ds.Key) ds.Key {
k := ds.NewKey("/")
for _, n := range dsk.Namespaces() {
k = k.ChildString(b58.Encode([]byte(n)))
}
return k
}
示例9: Query
// Query implements Query, inverting keys on the way back out.
func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
qr, err := d.raw.Query(q)
if err != nil {
return nil, err
}
ch := make(chan dsq.Result)
go func() {
defer close(ch)
defer qr.Close()
for r := range qr.Next() {
if r.Error != nil {
ch <- r
continue
}
k := ds.NewKey(r.Entry.Key)
if !d.prefix.IsAncestorOf(k) {
continue
}
r.Entry.Key = d.Datastore.InvertKey(k).String()
ch <- r
}
}()
return dsq.DerivedResults(qr, ch), nil
}
示例10: strsToKeys
func strsToKeys(strs []string) []ds.Key {
keys := make([]ds.Key, len(strs))
for i, s := range strs {
keys[i] = ds.NewKey(s)
}
return keys
}
示例11: Query
// Query implements Datastore.Query
func (d *Datastore) Query(q query.Query) (query.Results, error) {
results := make(chan query.Result)
walkFn := func(path string, info os.FileInfo, err error) error {
// remove ds path prefix
if strings.HasPrefix(path, d.path) {
path = path[len(d.path):]
}
if !info.IsDir() {
if strings.HasSuffix(path, ObjectKeySuffix) {
path = path[:len(path)-len(ObjectKeySuffix)]
}
key := ds.NewKey(path)
entry := query.Entry{Key: key.String(), Value: query.NotFetched}
results <- query.Result{Entry: entry}
}
return nil
}
go func() {
filepath.Walk(d.path, walkFn)
close(results)
}()
r := query.ResultsWithChan(q, results)
r = query.NaiveQueryApply(q, r)
return r, nil
}
示例12: TestDatastoreGetNotAllowedAfterClose
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
t.Parallel()
path := testRepoPath("test", t)
assert.True(!IsInitialized(path), t, "should NOT be initialized")
assert.Nil(Init(path, &config.Config{}), t, "should initialize successfully")
r, err := Open(path)
assert.Nil(err, t, "should open successfully")
k := "key"
data := []byte(k)
assert.Nil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")
assert.Nil(r.Close(), t)
_, err = r.Datastore().Get(datastore.NewKey(k))
assert.Err(err, t, "after closer, Get should be fail")
}
示例13: lookupBatch
func (mt *mountBatch) lookupBatch(key ds.Key) (ds.Batch, ds.Key, error) {
mt.lk.Lock()
defer mt.lk.Unlock()
child, loc, rest := mt.d.lookup(key)
t, ok := mt.mounts[loc.String()]
if !ok {
bds, ok := child.(ds.Batching)
if !ok {
return nil, ds.NewKey(""), ds.ErrBatchUnsupported
}
var err error
t, err = bds.Batch()
if err != nil {
return nil, ds.NewKey(""), err
}
mt.mounts[loc.String()] = t
}
return t, rest, nil
}
示例14: Example
func Example() {
mp := ds.NewMapDatastore()
ns := nsds.Wrap(mp, ds.NewKey("/foo/bar"))
k := ds.NewKey("/beep")
v := "boop"
ns.Put(k, v)
fmt.Printf("ns.Put %s %s\n", k, v)
v2, _ := ns.Get(k)
fmt.Printf("ns.Get %s -> %s\n", k, v2)
k3 := ds.NewKey("/foo/bar/beep")
v3, _ := mp.Get(k3)
fmt.Printf("mp.Get %s -> %s\n", k3, v3)
// Output:
// ns.Put /beep boop
// ns.Get /beep -> boop
// mp.Get /foo/bar/beep -> boop
}
示例15: addTestCases
func addTestCases(t *testing.T, d *datastore, testcases map[string]string) {
for k, v := range testcases {
dsk := ds.NewKey(k)
if err := d.Put(dsk, []byte(v)); err != nil {
t.Fatal(err)
}
}
for k, v := range testcases {
dsk := ds.NewKey(k)
v2, err := d.Get(dsk)
if err != nil {
t.Fatal(err)
}
v2b := v2.([]byte)
if string(v2b) != v {
t.Errorf("%s values differ: %s != %s", k, v, v2)
}
}
}