예제 #1
0
// Build creates the defRefsIndex.
func (x *defRefsIndex) Build(refs []*graph.Ref, fbr fileByteRanges, ofs byteOffsets) error {
	vlog.Printf("defRefsIndex: building inverted def->ref index (%d refs)...", len(refs))
	defToRefOfs := map[graph.RefDefKey]byteOffsets{}
	for i, ref := range refs {
		defToRefOfs[ref.RefDefKey()] = append(defToRefOfs[ref.RefDefKey()], ofs[i])
	}

	vlog.Printf("defRefsIndex: adding %d index phtable keys...", len(defToRefOfs))
	b := phtable.Builder(len(fbr))
	for def, refOfs := range defToRefOfs {
		v, err := binary.Marshal(refOfs)
		if err != nil {
			return err
		}

		k, err := proto.Marshal(&def)
		if err != nil {
			return err
		}

		b.Add([]byte(k), v)
	}
	vlog.Printf("defRefsIndex: building index phtable...")
	h, err := b.Build()
	if err != nil {
		return err
	}
	h.StoreKeys = true // so defRefUnitsIndex can enumerate defs pointed to by this unit's refs
	x.phtable = h
	x.ready = true
	vlog.Printf("defRefsIndex: done building index.")
	return nil
}
예제 #2
0
파일: varint.go 프로젝트: sombr/ccat
func (w *varintWriter) WriteMsg(msg proto.Message) (uint64, error) {
	var data []byte
	if m, ok := msg.(marshaler); ok {
		n := m.Size()
		if n >= len(w.buf) {
			w.buf = make([]byte, n)
		}
		if _, err := m.MarshalTo(w.buf); err != nil {
			return 0, err
		}
		data = w.buf[:n]
	} else {
		var err error
		data, err = proto.Marshal(msg)
		if err != nil {
			return 0, err
		}
	}
	length := uint64(len(data))
	n := binary.PutUvarint(w.lenBuf, length)
	if _, err := w.w.Write(w.lenBuf[:n]); err != nil {
		return 0, err
	}
	_, err := w.w.Write(data)
	return uint64(n) + length, err
}
예제 #3
0
파일: text_test.go 프로젝트: sombr/ccat
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.MyMessage_BLUE.Enum(),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}
	greetings := []string{"adg", "easy", "cow"}
	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 201, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 202, b)

	return msg
}
예제 #4
0
func (x *defRefsIndex) getByDef(def graph.RefDefKey) (byteOffsets, bool, error) {
	c_defRefsIndex_getByDef++
	if x.phtable == nil {
		panic("phtable not built/read")
	}

	k, err := proto.Marshal(&def)
	if err != nil {
		return nil, false, err
	}

	v := x.phtable.Get(k)
	if v == nil {
		return nil, false, nil
	}

	var ofs byteOffsets
	if err := binary.Unmarshal(v, &ofs); err != nil {
		return nil, true, err
	}
	return ofs, true, nil
}