mirror of
https://github.com/hanzoai/graph.git
synced 2026-10-30 18:35:37 +00:00
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.
113 lines
2.0 KiB
Go
113 lines
2.0 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
// Package types contains some very common utilities used by Dgraph. These utilities
|
|
// are of "miscellaneous" nature, e.g., error checking.
|
|
package types
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type ShardedMap struct {
|
|
shards []map[uint64]Val
|
|
}
|
|
|
|
const NumShards = 30
|
|
|
|
func NewShardedMap() *ShardedMap {
|
|
shards := make([]map[uint64]Val, NumShards)
|
|
for i := range shards {
|
|
shards[i] = make(map[uint64]Val)
|
|
}
|
|
return &ShardedMap{shards: shards}
|
|
}
|
|
|
|
func (s *ShardedMap) Merge(other *ShardedMap, ag func(a, b Val) Val) {
|
|
// TODO: ideally othermap should be the one which is smaller in size.
|
|
var wg sync.WaitGroup
|
|
for i := range s.shards {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
for k, v := range other.shards[i] {
|
|
if _, ok := s.shards[i][k]; ok {
|
|
s.shards[i][k] = ag(s.shards[i][k], v)
|
|
} else {
|
|
s.shards[i][k] = v
|
|
}
|
|
}
|
|
wg.Done()
|
|
}(i)
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func (s *ShardedMap) IsEmpty() bool {
|
|
if s == nil {
|
|
return true
|
|
}
|
|
return len(s.shards) == 0
|
|
}
|
|
|
|
func (s *ShardedMap) GetShardOrNil(key int) map[uint64]Val {
|
|
if s == nil {
|
|
return make(map[uint64]Val)
|
|
}
|
|
return s.shards[key]
|
|
}
|
|
|
|
func (s *ShardedMap) init() {
|
|
if s == nil {
|
|
*s = *NewShardedMap()
|
|
}
|
|
}
|
|
|
|
func (s *ShardedMap) getShard(key uint64) map[uint64]Val {
|
|
return s.shards[key%NumShards]
|
|
}
|
|
|
|
func (s *ShardedMap) Set(key uint64, value Val) {
|
|
if s == nil {
|
|
s.init()
|
|
}
|
|
shard := s.getShard(key)
|
|
shard[key] = value
|
|
}
|
|
|
|
func (s *ShardedMap) Get(key uint64) (Val, bool) {
|
|
if s == nil {
|
|
return Val{}, false
|
|
}
|
|
shard := s.getShard(key)
|
|
val, ok := shard[key]
|
|
return val, ok
|
|
}
|
|
|
|
func (s *ShardedMap) Len() int {
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
var count int
|
|
for _, shard := range s.shards {
|
|
count += len(shard)
|
|
}
|
|
return count
|
|
}
|
|
|
|
func (s *ShardedMap) Iterate(f func(uint64, Val) error) error {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
for _, shard := range s.shards {
|
|
for k, v := range shard {
|
|
if err := f(k, v); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|