Ejemplo n.º 1
0
// Open a CDataFile
func OpenRawDataFile(name string, readOnly bool, byteOrder binary.ByteOrder, byteAlignment uint64, valueSize int) (*RawDataFile, error) {
	flag := os.O_RDWR
	if readOnly {
		flag = os.O_RDONLY
	}

	file, err := os.OpenFile(name, flag, 0644)
	if err != nil {
		return nil, err
	}

	var univalToBytes func([]byte, unival)
	var bytesToUnival func([]byte) unival
	switch valueSize {
	case 8:
		univalToBytes = func(dst []byte, src unival) {
			byteOrder.PutUint64(dst, src.AsUnsignedLong())
		}
		bytesToUnival = func(src []byte) unival {
			return unival(byteOrder.Uint64(src))
		}
	default:
		return nil, errors.Errorf("Invalid value size %d", valueSize)
	}
	return &RawDataFile{
		file: file,
		//		byteOrder:     byteOrder,
		byteAlignment: byteAlignment,
		valueSize:     valueSize,
		univalToBytes: univalToBytes,
		bytesToUnival: bytesToUnival,
	}, nil
}
Ejemplo n.º 2
0
// PutUint64 serializes the provided uint64 using the given byte order into a
// buffer from the free list and writes the resulting eight bytes to the given
// writer.
func (l binaryFreeList) PutUint64(w io.Writer, byteOrder binary.ByteOrder, val uint64) error {
	buf := l.Borrow()[:8]
	byteOrder.PutUint64(buf, val)
	_, err := w.Write(buf)
	l.Return(buf)
	return err
}
Ejemplo n.º 3
0
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
	trie := newEmpty()
	k := make([]byte, 32)
	for i := 0; i < b.N; i++ {
		e.PutUint64(k, uint64(i))
		trie.Update(k, k)
	}
	return trie
}
Ejemplo n.º 4
0
func benchHash(b *testing.B, e binary.ByteOrder) {
	trie := newEmpty()
	k := make([]byte, 32)
	for i := 0; i < benchElemCount; i++ {
		e.PutUint64(k, uint64(i))
		trie.Update(k, k)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		trie.Hash()
	}
}
Ejemplo n.º 5
0
func packUint(order binary.ByteOrder, v interface{}) (data []byte) {
	switch val := v.(type) {
	case uint64:
		data = make([]byte, 8)
		order.PutUint64(data, val)
	case uint32:
		data = make([]byte, 4)
		order.PutUint32(data, val)
	case uint16:
		data = make([]byte, 2)
		order.PutUint16(data, val)
	case uint8:
		data = []byte{byte(val)}
	default:
		panic("unsupported type")
	}
	return data
}
Ejemplo n.º 6
0
func newSimpleProtocol(n int, byteOrder binary.ByteOrder) *simpleProtocol {
	protocol := &simpleProtocol{
		n:  n,
		bo: byteOrder,
	}

	switch n {
	case 1:
		protocol.encodeHead = func(buffer []byte) {
			buffer[0] = byte(len(buffer) - n)
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(buffer[0])
		}
	case 2:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint16(buffer, uint16(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint16(buffer))
		}
	case 4:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint32(buffer, uint32(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint32(buffer))
		}
	case 8:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint64(buffer, uint64(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint64(buffer))
		}
	default:
		panic("unsupported packet head size")
	}

	return protocol
}
Ejemplo n.º 7
0
func (b *Buffer) WriteUint64(u uint64, order binary.ByteOrder) error {
	i := b.Grows(8)
	order.PutUint64(b.Buf[i:], u)
	return nil
}
Ejemplo n.º 8
0
func writeFloat(buf []byte, byteOrder binary.ByteOrder, value float64) {
	u := math.Float64bits(value)
	byteOrder.PutUint64(buf, u)
}