Files
zeekay f5aaeb712e Hanzo Graph — the engine, on zapdb, ZAP-native
Derived from Dgraph (github.com/dgraph-io/dgraph) at commit 64804bd2, Apache-2.0.
See NOTICE.

WHAT THIS IS. The graph engine as a Go library: posting lists, the DQL parser,
the type and tokenizer system, the index and schema layers, and the wire types.
39,170 lines of the upstream's 166,337.

THE STORE is github.com/luxfi/zapdb, a Badger fork that kept the package name,
the subpackages (options, pb, y) and the API — Open, OpenManaged, DB, Options —
so the 74 import sites are aliased rather than rewritten.

THE CODEC is ZAP. zapdb serializes with ZAP, not protobuf: its pb package builds
the ZAP types by default and the protobuf ones only under `-tags grpc`, a path
that does not compile (table/builder.go calls pb.Marshal, which that tag
removes). One call site in posting needed converting; every other type here is
Hanzo's own protobuf and is untouched.

NO TRANSPORT. An embedded engine dials nothing, so nothing here imports
google.golang.org/grpc. Removed: 20 transport functions in x, the generated
service stubs for Raft, Zero and Worker (1,657 lines), and the JWT helper —
identity belongs to IAM. The core called none of them.

NOT KEPT: worker, conn, raftwal, the cluster commands, the GraphQL layer and the
test harnesses. `query`, the DQL execution engine, went with them because it
depends on worker; parsing DQL is available here, executing it is not.

The history of the upstream project lives at github.com/dgraph-io/dgraph. This
repository begins at the derivation because three quarters of that history
describes code this fork does not carry.
2026-08-20 21:26:27 -07:00

59 lines
1.6 KiB
Go

/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package index
import (
"encoding/binary"
"math"
"reflect"
"unsafe"
c "github.com/hanzoai/graph/tok/constraints"
"github.com/golang/glog"
)
// BytesAsFloatArray[T c.Float](encoded) converts encoded into a []T,
// where T is either float32 or float64, depending on the value of floatBits.
// Let floatBytes = floatBits/8. If len(encoded) % floatBytes is
// not 0, it will ignore any trailing bytes, and simply convert floatBytes
// bytes at a time to generate the entries.
// The result is appended to the given retVal slice. If retVal is nil
// then a new slice is created and appended to.
func BytesAsFloatArray[T c.Float](encoded []byte, retVal *[]T, floatBits int) {
floatBytes := floatBits / 8
if len(encoded) == 0 {
*retVal = []T{}
return
}
// Ensure the byte slice length is a multiple of 8 (size of float64)
if len(encoded)%floatBytes != 0 {
glog.Errorf("Invalid byte slice length %d %v", len(encoded), encoded)
return
}
if retVal == nil {
*retVal = make([]T, len(encoded)/floatBytes)
}
*retVal = (*retVal)[:0]
header := (*reflect.SliceHeader)(unsafe.Pointer(retVal))
header.Data = uintptr(unsafe.Pointer(&encoded[0]))
header.Len = len(encoded) / floatBytes
header.Cap = len(encoded) / floatBytes
}
func BytesToFloat[T c.Float](encoded []byte, floatBits int) T {
if floatBits == 32 {
bits := binary.LittleEndian.Uint32(encoded)
return T(math.Float32frombits(bits))
} else if floatBits == 64 {
bits := binary.LittleEndian.Uint64(encoded)
return T(math.Float64frombits(bits))
}
panic("Invalid floatBits")
}