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

61 lines
1.3 KiB
Go

/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package x
import (
"sync"
"sync/atomic"
)
// SafeMutex can be used in place of sync.RWMutex. It allows code to assert
// whether the mutex is locked.
type SafeMutex struct {
m sync.RWMutex
// m deadlock.RWMutex // Useful during debugging and testing for detecting locking issues.
writer int32
readers int32
}
// AlreadyLocked returns true if safe mutex is already being held.
func (s *SafeMutex) AlreadyLocked() bool {
return atomic.LoadInt32(&s.writer) > 0
}
// Lock locks the safe mutex.
func (s *SafeMutex) Lock() {
s.m.Lock()
AssertTrue(atomic.AddInt32(&s.writer, 1) == 1)
}
// Unlock unlocks the safe mutex.
func (s *SafeMutex) Unlock() {
AssertTrue(atomic.AddInt32(&s.writer, -1) == 0)
s.m.Unlock()
}
// AssertLock asserts whether the lock is being held.
func (s *SafeMutex) AssertLock() {
AssertTrue(s.AlreadyLocked())
}
// RLock holds the reader lock.
func (s *SafeMutex) RLock() {
s.m.RLock()
atomic.AddInt32(&s.readers, 1)
}
// RUnlock releases the reader lock.
func (s *SafeMutex) RUnlock() {
atomic.AddInt32(&s.readers, -1)
s.m.RUnlock()
}
// AssertRLock asserts whether the reader lock is being held.
func (s *SafeMutex) AssertRLock() {
AssertTrue(atomic.LoadInt32(&s.readers) > 0 ||
atomic.LoadInt32(&s.writer) == 1)
}