|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]> |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package dgo |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + apiv25 "github.com/dgraph-io/dgo/v250/protos/api.v25" |
| 12 | +) |
| 13 | + |
| 14 | +// AllocateUIDs allocates a given number of Node UIDs in the Graph and returns a start and end UIDs, |
| 15 | +// end excluded. The UIDs in the range [start, end) can then be used by the client in the mutations |
| 16 | +// going forward. Note that, each node in a Graph is assigned a UID in Dgraph. Dgraph ensures that |
| 17 | +// these UIDs are not allocated anywhere else throughout the operation of this cluster. This is useful |
| 18 | +// in bulk loader or live loader or similar applications. |
| 19 | +func (d *Dgraph) AllocateUIDs(ctx context.Context, howMany uint64) (uint64, uint64, error) { |
| 20 | + return d.allocateIDs(ctx, howMany, apiv25.LeaseType_UID) |
| 21 | +} |
| 22 | + |
| 23 | +// AllocateTimestamps gets a sequence of timestamps allocated from Dgraph. These timestamps can be |
| 24 | +// used in bulk loader and similar applications. |
| 25 | +func (d *Dgraph) AllocateTimestamps(ctx context.Context, howMany uint64) (uint64, uint64, error) { |
| 26 | + return d.allocateIDs(ctx, howMany, apiv25.LeaseType_TS) |
| 27 | +} |
| 28 | + |
| 29 | +// AllocateNamespaces allocates a given number of namespaces in the Graph and returns a start and end |
| 30 | +// namespaces, end excluded. The namespaces in the range [start, end) can then be used by the client. |
| 31 | +// Dgraph ensures that these namespaces are NOT allocated anywhere else throughout the operation of |
| 32 | +// this cluster. This is useful in bulk loader or live loader or similar applications. |
| 33 | +func (d *Dgraph) AllocateNamespaces(ctx context.Context, howMany uint64) (uint64, uint64, error) { |
| 34 | + return d.allocateIDs(ctx, howMany, apiv25.LeaseType_NS) |
| 35 | +} |
| 36 | + |
| 37 | +func (d *Dgraph) allocateIDs(ctx context.Context, howMany uint64, |
| 38 | + leaseType apiv25.LeaseType) (uint64, uint64, error) { |
| 39 | + |
| 40 | + req := &apiv25.AllocateIDsRequest{HowMany: howMany, LeaseType: leaseType} |
| 41 | + resp, err := doWithRetryLogin(ctx, d, func(dc apiv25.DgraphClient) (*apiv25.AllocateIDsResponse, error) { |
| 42 | + return dc.AllocateIDs(d.getContext(ctx), req) |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return 0, 0, err |
| 46 | + } |
| 47 | + return resp.Start, resp.End, nil |
| 48 | +} |
0 commit comments