本文整理匯總了Golang中github.com/dgraph-io/dgraph/posting.GetOrCreate函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetOrCreate函數的具體用法?Golang GetOrCreate怎麽用?Golang GetOrCreate使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetOrCreate函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handleNQuads
func (s *state) handleNQuads(wg *sync.WaitGroup) {
for nq := range s.cnq {
if farm.Fingerprint64([]byte(nq.Subject))%s.mod != 0 {
// Ignore due to mod sampling.
atomic.AddUint64(&s.ctr.ignored, 1)
continue
}
edge, err := nq.ToEdge()
for err != nil {
// Just put in a retry loop to tackle temporary errors.
if err == posting.E_TMP_ERROR {
time.Sleep(time.Microsecond)
} else {
glog.WithError(err).WithField("nq", nq).
Error("While converting to edge")
return
}
edge, err = nq.ToEdge()
}
key := posting.Key(edge.Entity, edge.Attribute)
plist := posting.GetOrCreate(key)
plist.AddMutation(edge, posting.Set)
atomic.AddUint64(&s.ctr.processed, 1)
}
wg.Done()
}
示例2: allocateUniqueUid
// allocateUniqueUid returns an integer in range:
// [minIdx, maxIdx] derived based on numInstances and instanceIdx.
// which hasn't already been allocated to other xids. It does this by
// taking the fingerprint of the xid appended with zero or more spaces
// until the obtained integer is unique.
func allocateUniqueUid(instanceIdx uint64, numInstances uint64) uint64 {
mod := math.MaxUint64 / numInstances
minIdx := instanceIdx * mod
buf := make([]byte, 128)
for {
_, err := rand.Read(buf)
x.Checkf(err, "rand.Read shouldn't throw an error")
uidb := farm.Fingerprint64(buf) // Generate from hash.
uid := (uidb % mod) + minIdx
if uid == math.MaxUint64 || !lmgr.isNew(uid) {
continue
}
// Check if this uid has already been allocated.
key := x.DataKey("_uid_", uid)
pl, decr := posting.GetOrCreate(key)
defer decr()
if pl.Length(0) == 0 {
return uid
}
}
log.Fatalf("This shouldn't be reached.")
return 0
}
示例3: handleNQuads
func (s *state) handleNQuads(wg *sync.WaitGroup) {
for nq := range s.cnq {
edge, err := nq.ToEdge(s.instanceIdx, s.numInstances)
for err != nil {
// Just put in a retry loop to tackle temporary errors.
if err == posting.E_TMP_ERROR {
time.Sleep(time.Microsecond)
} else {
glog.WithError(err).WithField("nq", nq).
Error("While converting to edge")
return
}
edge, err = nq.ToEdge(s.instanceIdx, s.numInstances)
}
// Only handle this edge if the attribute satisfies the modulo rule
if farm.Fingerprint64([]byte(edge.Attribute))%s.numInstances ==
s.instanceIdx {
key := posting.Key(edge.Entity, edge.Attribute)
plist := posting.GetOrCreate(key, dataStore)
plist.AddMutation(edge, posting.Set)
atomic.AddUint64(&s.ctr.processed, 1)
} else {
atomic.AddUint64(&s.ctr.ignored, 1)
}
}
wg.Done()
}
示例4: ExternalId
func ExternalId(uid uint64) (xid string, rerr error) {
key := posting.Key(uid, "_xid_") // uid -> "_xid_" -> xid
pl := posting.GetOrCreate(key, uidStore)
if pl.Length() == 0 {
return "", errors.New("NO external id")
}
if pl.Length() > 1 {
glog.WithField("uid", uid).Fatal("This shouldn't be happening.")
return "", errors.New("Multiple external ids for this uid.")
}
var p types.Posting
if ok := pl.Get(&p, 0); !ok {
glog.WithField("uid", uid).Error("While retrieving posting")
return "", errors.New("While retrieving posting")
}
if p.Uid() != math.MaxUint64 {
glog.WithField("uid", uid).Fatal("Value uid must be MaxUint64.")
}
var t interface{}
rerr = posting.ParseValue(&t, p.ValueBytes())
xid = t.(string)
return xid, rerr
}
示例5: addEdgeToUID
func addEdgeToUID(t *testing.T, ps *store.Store, attr string, src uint64, dst uint64) {
edge := &task.DirectedEdge{
ValueId: dst,
Label: "testing",
Attr: attr,
Entity: src,
}
l, _ := posting.GetOrCreate(x.DataKey(attr, src))
require.NoError(t,
l.AddMutationWithIndex(context.Background(), edge, posting.Set))
}
示例6: markTaken
func markTaken(ctx context.Context, uid uint64) {
mu := &task.DirectedEdge{
Entity: uid,
Attr: "_uid_",
Value: []byte("_"), // not txid
Label: "_loader_",
}
key := x.DataKey("_uid_", uid)
plist, decr := posting.GetOrCreate(key)
plist.AddMutation(ctx, mu, posting.Set)
decr()
}
示例7: addEdgeToTypedValue
func addEdgeToTypedValue(t *testing.T, ps *store.Store, attr string, src uint64,
typ types.TypeID, value []byte) {
edge := &task.DirectedEdge{
Value: value,
ValueType: uint32(typ),
Label: "testing",
Attr: attr,
Entity: src,
}
l, _ := posting.GetOrCreate(x.DataKey(attr, src))
require.NoError(t,
l.AddMutationWithIndex(context.Background(), edge, posting.Set))
}
示例8: handleNQuads
// handleNQuads converts the nQuads that satisfy the modulo
// rule into posting lists.
func (s *state) handleNQuads(wg *sync.WaitGroup) {
defer wg.Done()
// Check if we need to mark used UIDs.
markUids := s.groupsMap[group.BelongsTo("_uid_")]
ctx := context.Background()
for nq := range s.cnq {
if s.Error() != nil {
return
}
// Only handle this edge if the attribute satisfies the modulo rule
if !s.groupsMap[group.BelongsTo(nq.Predicate)] {
atomic.AddUint64(&s.ctr.ignored, 1)
continue
}
edge, err := nq.ToEdge()
for err != nil {
// Just put in a retry loop to tackle temporary errors.
if err == posting.ErrRetry {
time.Sleep(time.Microsecond)
} else {
s.SetError(err)
glog.WithError(err).WithField("nq", nq).
Error("While converting to edge")
return
}
edge, err = nq.ToEdge()
}
key := x.DataKey(edge.Attr, edge.Entity)
plist, decr := posting.GetOrCreate(key)
plist.AddMutationWithIndex(ctx, edge, posting.Set)
decr() // Don't defer, just call because we're in a channel loop.
// Mark UIDs and XIDs as taken
if markUids {
// Mark entity UID.
markTaken(ctx, edge.Entity)
// Mark the Value UID.
if edge.ValueId != 0 {
markTaken(ctx, edge.ValueId)
}
}
atomic.AddUint64(&s.ctr.processed, 1)
}
}
示例9: ProcessTask
func ProcessTask(query []byte) (result []byte, rerr error) {
uo := flatbuffers.GetUOffsetT(query)
q := new(task.Query)
q.Init(query, uo)
b := flatbuffers.NewBuilder(0)
voffsets := make([]flatbuffers.UOffsetT, q.UidsLength())
uoffsets := make([]flatbuffers.UOffsetT, q.UidsLength())
attr := string(q.Attr())
for i := 0; i < q.UidsLength(); i++ {
uid := q.Uids(i)
key := posting.Key(uid, attr)
pl := posting.GetOrCreate(key, dataStore)
var valoffset flatbuffers.UOffsetT
if val, err := pl.Value(); err != nil {
valoffset = b.CreateByteVector(x.Nilbyte)
} else {
valoffset = b.CreateByteVector(val)
}
task.ValueStart(b)
task.ValueAddVal(b, valoffset)
voffsets[i] = task.ValueEnd(b)
ulist := pl.GetUids()
uoffsets[i] = x.UidlistOffset(b, ulist)
}
task.ResultStartValuesVector(b, len(voffsets))
for i := len(voffsets) - 1; i >= 0; i-- {
b.PrependUOffsetT(voffsets[i])
}
valuesVent := b.EndVector(len(voffsets))
task.ResultStartUidmatrixVector(b, len(uoffsets))
for i := len(uoffsets) - 1; i >= 0; i-- {
b.PrependUOffsetT(uoffsets[i])
}
matrixVent := b.EndVector(len(uoffsets))
task.ResultStart(b)
task.ResultAddValues(b, valuesVent)
task.ResultAddUidmatrix(b, matrixVent)
rend := task.ResultEnd(b)
b.Finish(rend)
return b.Bytes[b.Head():], nil
}
示例10: allocateUniqueUid
func allocateUniqueUid(xid string, instanceIdx uint64,
numInstances uint64) (uid uint64, rerr error) {
mod := math.MaxUint64 / numInstances
minIdx := instanceIdx * mod
for sp := ""; ; sp += " " {
txid := xid + sp
uid1 := farm.Fingerprint64([]byte(txid)) // Generate from hash.
uid = (uid1 % mod) + minIdx
glog.WithField("txid", txid).WithField("uid", uid).Debug("Generated")
if uid == math.MaxUint64 {
glog.Debug("Hit uint64max while generating fingerprint. Ignoring...")
continue
}
// Check if this uid has already been allocated.
key := posting.Key(uid, "_xid_") // uid -> "_xid_" -> xid
pl := posting.GetOrCreate(key, uidStore)
if pl.Length() > 0 {
// Something already present here.
var p types.Posting
pl.Get(&p, 0)
var tmp interface{}
posting.ParseValue(&tmp, p.ValueBytes())
glog.Debug("Found existing xid: [%q]. Continuing...", tmp.(string))
continue
}
// Uid hasn't been assigned yet.
t := x.DirectedEdge{
Value: xid, // not txid
Source: "_assigner_",
Timestamp: time.Now(),
}
rerr = pl.AddMutation(t, posting.Set)
if rerr != nil {
glog.WithError(rerr).Error("While adding mutation")
}
return uid, rerr
}
return 0, errors.New("Some unhandled route lead me here." +
" Wake the stupid developer up.")
}
示例11: runMutations
// runMutations goes through all the edges and applies them. It returns the
// mutations which were not applied in left.
func runMutations(ctx context.Context, edges []*task.DirectedEdge, op uint32) error {
for _, edge := range edges {
if !groups().ServesGroup(group.BelongsTo(edge.Attr)) {
return x.Errorf("Predicate fingerprint doesn't match this instance")
}
key := x.DataKey(edge.Attr, edge.Entity)
plist, decr := posting.GetOrCreate(key)
defer decr()
if err := plist.AddMutationWithIndex(ctx, edge, op); err != nil {
x.Printf("Error while adding mutation: %v %v", edge, err)
return err // abort applying the rest of them.
}
}
return nil
}
示例12: writePLs
func writePLs(t *testing.T, pred string, count int, vid uint64, ps *store.Store) {
for i := 0; i < count; i++ {
k := x.DataKey(pred, uint64(i))
list, _ := posting.GetOrCreate(k)
de := &task.DirectedEdge{
ValueId: vid,
Label: "test",
}
list.AddMutation(context.TODO(), de, posting.Set)
if merged, err := list.CommitIfDirty(context.TODO()); err != nil {
t.Errorf("While merging: %v", err)
} else if !merged {
t.Errorf("No merge happened")
}
}
}
示例13: GetOrAssign
func GetOrAssign(xid string, instanceIdx uint64, numInstances uint64) (uid uint64, rerr error) {
key := stringKey(xid)
pl := posting.GetOrCreate(key)
if pl.Length() == 0 {
return assignNew(pl, xid, instanceIdx, numInstances)
} else if pl.Length() > 1 {
glog.Fatalf("We shouldn't have more than 1 uid for xid: %v\n", xid)
} else {
// We found one posting.
var p types.Posting
if ok := pl.Get(&p, 0); !ok {
return 0, errors.New("While retrieving entry from posting list")
}
return p.Uid(), nil
}
return 0, errors.New("Some unhandled route lead me here." +
" Wake the stupid developer up.")
}
示例14: getPostingValue
// getPostingValue looks up key, gets the value, converts it. If any error is
// encountered, we return nil. This is used in some filtering where we do not
// want to waste time creating errors.
func getPostingValue(key []byte, scalarType types.Scalar) *types.Value {
pl, decr := posting.GetOrCreate(key)
defer decr()
valBytes, vType, err := pl.Value()
if bytes.Equal(valBytes, nil) {
return nil
}
val := types.ValueForType(types.TypeID(vType))
if val == nil {
return nil
}
if err := val.UnmarshalBinary(valBytes); err != nil {
return nil
}
// Convert to schema type.
sv, err := scalarType.Convert(val)
if err != nil {
return nil
}
return &sv
}
示例15: fetchValue
// fetchValue gets the value for a given UID.
func fetchValue(uid uint64, attr string, scalar types.Scalar) (types.Value, error) {
pl, decr := posting.GetOrCreate(x.DataKey(attr, uid))
defer decr()
valBytes, vType, err := pl.Value()
if err != nil {
return nil, err
}
val := types.ValueForType(types.TypeID(vType))
if val == nil {
return nil, x.Errorf("Invalid type: %v", vType)
}
err = val.UnmarshalBinary(valBytes)
if err != nil {
return nil, err
}
schemaVal, err := scalar.Convert(val)
if err != nil {
return nil, err
}
return schemaVal, nil
}