MCPcopy
hub / github.com/dgraph-io/dgraph / Parse

Function Parse

x/keys.go:530–620  ·  view source on GitHub ↗

Parse would parse the key. ParsedKey does not reuse the key slice, so the key slice can change without affecting the contents of ParsedKey.

(key []byte)

Source from the content-addressed store, hash-verified

528// Parse would parse the key. ParsedKey does not reuse the key slice, so the key slice can change
529// without affecting the contents of ParsedKey.
530func Parse(key []byte) (ParsedKey, error) {
531 var p ParsedKey
532
533 if len(key) < 9 {
534 return p, errors.New("Key length less than 9")
535 }
536 p.bytePrefix = key[0]
537 namespace := key[1:9]
538 key = key[9:]
539 if p.bytePrefix == ByteUnused {
540 return p, nil
541 }
542
543 p.HasStartUid = p.bytePrefix == ByteSplit
544
545 if len(key) < 3 {
546 return p, errors.Errorf("Invalid format for key %v", key)
547 }
548 sz := int(binary.BigEndian.Uint16(key[:2]))
549 k := key[2:]
550
551 if len(k) < sz {
552 return p, errors.Errorf("Invalid size %v for key %v", sz, key)
553 }
554 p.Attr = NamespaceAttr(binary.BigEndian.Uint64(namespace), string(k[:sz]))
555 k = k[sz:]
556
557 switch p.bytePrefix {
558 case ByteSchema, ByteType:
559 return p, nil
560 default:
561 }
562
563 p.ByteType = k[0]
564 k = k[1:]
565
566 switch p.ByteType {
567 case ByteData, ByteReverse:
568 if len(k) < 8 {
569 return p, errors.Errorf("uid length < 8 for key: %q, parsed key: %+v", key, p)
570 }
571 p.Uid = binary.BigEndian.Uint64(k)
572 if p.Uid == 0 {
573 return p, errors.Errorf("Invalid UID with value 0 for key: %v", key)
574 }
575 if !p.HasStartUid {
576 break
577 }
578
579 if len(k) != 16 {
580 return p, errors.Errorf("StartUid length != 8 for key: %q, parsed key: %+v", key, p)
581 }
582
583 k = k[8:]
584 p.StartUid = binary.BigEndian.Uint64(k)
585 case ByteIndex:
586 if !p.HasStartUid {
587 p.Term = string(k)

Callers 15

SubscribeForAclUpdatesFunction · 0.92
getPredicatesMethod · 0.92
toListMethod · 0.92
addCountEntryMethod · 0.92
writeIndexMethod · 0.92
uidToValFunction · 0.92
showAllPostingsAtFunction · 0.92
historyFunction · 0.92
printKeysFunction · 0.92
printSummaryFunction · 0.92
runFunction · 0.92
WriteBackupMethod · 0.92

Calls 2

NamespaceAttrFunction · 0.85
ErrorfMethod · 0.45

Tested by 15

tokensForTestFunction · 0.74
TestDataKeyFunction · 0.56
TestIndexKeyFunction · 0.56
TestIndexKeyWithStartUidFunction · 0.56
TestReverseKeyFunction · 0.56
TestCountKeyFunction · 0.56
TestCountKeyWithStartUidFunction · 0.56
TestSchemaKeyFunction · 0.56
TestTypeKeyFunction · 0.56