當前位置: 首頁>>代碼示例>>Golang>>正文


Golang go-datastore.Datastore類代碼示例

本文整理匯總了Golang中github.com/djbarber/ipfs-hack/Godeps/_workspace/src/github.com/jbenet/go-datastore.Datastore的典型用法代碼示例。如果您正苦於以下問題:Golang Datastore類的具體用法?Golang Datastore怎麽用?Golang Datastore使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Datastore類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: storeSet

// helpers to marshal / unmarshal a pin set
func storeSet(d ds.Datastore, k ds.Key, val interface{}) error {
	buf, err := json.Marshal(val)
	if err != nil {
		return err
	}

	return d.Put(k, buf)
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:9,代碼來源:pin.go

示例2: testNotHas

func testNotHas(t *testing.T, d ds.Datastore, k ds.Key) {
	if _, err := d.Get(k); err == nil {
		t.Error("should not have it", d, k)
	}

	if has, err := d.Has(k); err != nil {
		t.Error(err)
	} else if has {
		t.Error("should not have it", d, k)
	}
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:11,代碼來源:timecache_test.go

示例3: putRoutingRecord

func putRoutingRecord(ds datastore.Datastore, k key.Key, value *dhtpb.Record) error {
	data, err := proto.Marshal(value)
	if err != nil {
		return err
	}
	dskey := k.DsKey()
	// TODO namespace
	if err := ds.Put(dskey, data); err != nil {
		return err
	}
	return nil
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:12,代碼來源:server.go

示例4: loadSet

func loadSet(d ds.Datastore, k ds.Key, val interface{}) error {
	buf, err := d.Get(k)
	if err != nil {
		return err
	}

	bf, ok := buf.([]byte)
	if !ok {
		return errors.New("invalid pin set value in datastore")
	}
	return json.Unmarshal(bf, val)
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:12,代碼來源:pin.go

示例5: testHas

func testHas(t *testing.T, d ds.Datastore, k ds.Key, v interface{}) {
	if v2, err := d.Get(k); err != nil {
		t.Error(err)
	} else if v2 != v {
		t.Error("value incorrect", d, k, v, v2)
	}

	if has, err := d.Has(k); err != nil {
		t.Error(err)
	} else if !has {
		t.Error("should have it", d, k, v)
	}
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:13,代碼來源:timecache_test.go

示例6: getRoutingProviders

func getRoutingProviders(ds datastore.Datastore, k key.Key) ([]*dhtpb.Message_Peer, error) {
	e := log.EventBegin(context.Background(), "getProviders", &k)
	defer e.Done()
	var providers []*dhtpb.Message_Peer
	if v, err := ds.Get(providerKey(k)); err == nil {
		if data, ok := v.([]byte); ok {
			var msg dhtpb.Message
			if err := proto.Unmarshal(data, &msg); err != nil {
				return nil, err
			}
			providers = append(providers, msg.GetProviderPeers()...)
		}
	}
	return providers, nil
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:15,代碼來源:server.go

示例7: getRoutingRecord

func getRoutingRecord(ds datastore.Datastore, k key.Key) (*dhtpb.Record, error) {
	dskey := k.DsKey()
	val, err := ds.Get(dskey)
	if err != nil {
		return nil, err
	}
	recordBytes, ok := val.([]byte)
	if !ok {
		return nil, fmt.Errorf("datastore had non byte-slice value for %v", dskey)
	}
	var record dhtpb.Record
	if err := proto.Unmarshal(recordBytes, &record); err != nil {
		return nil, errors.New("failed to unmarshal dht record from datastore")
	}
	return &record, nil
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:16,代碼來源:server.go

示例8: putRoutingProviders

func putRoutingProviders(ds datastore.Datastore, k key.Key, newRecords []*dhtpb.Message_Peer) error {
	log.Event(context.Background(), "putRoutingProviders", &k)
	oldRecords, err := getRoutingProviders(ds, k)
	if err != nil {
		return err
	}
	mergedRecords := make(map[string]*dhtpb.Message_Peer)
	for _, provider := range oldRecords {
		mergedRecords[provider.GetId()] = provider // add original records
	}
	for _, provider := range newRecords {
		mergedRecords[provider.GetId()] = provider // overwrite old record if new exists
	}
	var protomsg dhtpb.Message
	protomsg.ProviderPeers = make([]*dhtpb.Message_Peer, 0, len(mergedRecords))
	for _, provider := range mergedRecords {
		protomsg.ProviderPeers = append(protomsg.ProviderPeers, provider)
	}
	data, err := proto.Marshal(&protomsg)
	if err != nil {
		return err
	}
	return ds.Put(providerKey(k), data)
}
開發者ID:djbarber,項目名稱:ipfs-hack,代碼行數:24,代碼來源:server.go


注:本文中的github.com/djbarber/ipfs-hack/Godeps/_workspace/src/github.com/jbenet/go-datastore.Datastore類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。