|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com> |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package hooks |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "sync" |
| 11 | + "sync/atomic" |
| 12 | + |
| 13 | + "github.com/dgraph-io/dgo/v250/protos/api" |
| 14 | + "github.com/dgraph-io/dgraph/v25/protos/pb" |
| 15 | +) |
| 16 | + |
| 17 | +type ZeroHooks interface { |
| 18 | + AssignUIDs(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) |
| 19 | + AssignTimestamps(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) |
| 20 | + AssignNsIDs(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) |
| 21 | + CommitOrAbort(ctx context.Context, tc *api.TxnContext) (*api.TxnContext, error) |
| 22 | + ApplyMutations(ctx context.Context, m *pb.Mutations) (*api.TxnContext, error) |
| 23 | +} |
| 24 | + |
| 25 | +type ZeroHooksFns struct { |
| 26 | + AssignUIDsFn func(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) |
| 27 | + AssignTimestampsFn func(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) |
| 28 | + AssignNsIDsFn func(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) |
| 29 | + CommitOrAbortFn func(ctx context.Context, tc *api.TxnContext) (*api.TxnContext, error) |
| 30 | + ApplyMutationsFn func(ctx context.Context, m *pb.Mutations) (*api.TxnContext, error) |
| 31 | +} |
| 32 | + |
| 33 | +func (h ZeroHooksFns) AssignUIDs(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) { |
| 34 | + return h.AssignUIDsFn(ctx, num) |
| 35 | +} |
| 36 | + |
| 37 | +func (h ZeroHooksFns) AssignTimestamps(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) { |
| 38 | + return h.AssignTimestampsFn(ctx, num) |
| 39 | +} |
| 40 | + |
| 41 | +func (h ZeroHooksFns) AssignNsIDs(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) { |
| 42 | + return h.AssignNsIDsFn(ctx, num) |
| 43 | +} |
| 44 | + |
| 45 | +func (h ZeroHooksFns) CommitOrAbort(ctx context.Context, tc *api.TxnContext) (*api.TxnContext, error) { |
| 46 | + return h.CommitOrAbortFn(ctx, tc) |
| 47 | +} |
| 48 | + |
| 49 | +func (h ZeroHooksFns) ApplyMutations(ctx context.Context, m *pb.Mutations) (*api.TxnContext, error) { |
| 50 | + return h.ApplyMutationsFn(ctx, m) |
| 51 | +} |
| 52 | + |
| 53 | +// Config holds the configuration for embedded mode operation. |
| 54 | +type Config struct { |
| 55 | + // Hooks for bypassing Zero operations |
| 56 | + ZeroHooks ZeroHooks |
| 57 | + |
| 58 | + // DataDir is the directory where data files are stored |
| 59 | + DataDir string |
| 60 | + |
| 61 | + // CacheSizeMB is the size of the in-memory cache in megabytes |
| 62 | + CacheSizeMB int64 |
| 63 | +} |
| 64 | + |
| 65 | +var ( |
| 66 | + // globalConfig holds the current embedded configuration |
| 67 | + globalConfig atomic.Pointer[Config] |
| 68 | + |
| 69 | + defaultZeroHooks atomic.Value |
| 70 | + |
| 71 | + // enabled tracks whether embedded mode is active |
| 72 | + enabled atomic.Bool |
| 73 | + |
| 74 | + // mu protects initialization |
| 75 | + mu sync.Mutex |
| 76 | +) |
| 77 | + |
| 78 | +// Enable activates embedded mode with the given configuration. |
| 79 | +// This must be called before any Dgraph operations. |
| 80 | +func Enable(cfg *Config) { |
| 81 | + mu.Lock() |
| 82 | + defer mu.Unlock() |
| 83 | + |
| 84 | + globalConfig.Store(cfg) |
| 85 | + enabled.Store(true) |
| 86 | +} |
| 87 | + |
| 88 | +// Disable deactivates embedded mode. |
| 89 | +func Disable() { |
| 90 | + mu.Lock() |
| 91 | + defer mu.Unlock() |
| 92 | + |
| 93 | + enabled.Store(false) |
| 94 | + globalConfig.Store(nil) |
| 95 | +} |
| 96 | + |
| 97 | +// IsEnabled returns true if embedded mode is currently active. |
| 98 | +func IsEnabled() bool { |
| 99 | + return enabled.Load() |
| 100 | +} |
| 101 | + |
| 102 | +// GetConfig returns the current embedded configuration, or nil if not enabled. |
| 103 | +func GetConfig() *Config { |
| 104 | + return globalConfig.Load() |
| 105 | +} |
| 106 | + |
| 107 | +func SetDefaultZeroHooks(h ZeroHooks) { |
| 108 | + defaultZeroHooks.Store(h) |
| 109 | +} |
| 110 | + |
| 111 | +// GetHooks returns the active Zero hooks. |
| 112 | +// If embedded mode is not enabled, it returns the default hooks implementation. |
| 113 | +func GetHooks() ZeroHooks { |
| 114 | + cfg := globalConfig.Load() |
| 115 | + if cfg != nil && cfg.ZeroHooks != nil { |
| 116 | + return cfg.ZeroHooks |
| 117 | + } |
| 118 | + if h := defaultZeroHooks.Load(); h != nil { |
| 119 | + return h.(ZeroHooks) |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
0 commit comments