diff --git a/api.go b/api.go index 7aa022582..5b073d1ae 100644 --- a/api.go +++ b/api.go @@ -104,7 +104,7 @@ func (api *API) Query(ctx context.Context, req *QueryRequest) (QueryResponse, er if err != nil { return resp, errors.Wrap(err, "parsing") } - execOpts := &ExecOptions{ + execOpts := &execOptions{ Remote: req.Remote, ExcludeRowAttrs: req.ExcludeRowAttrs, ExcludeColumns: req.ExcludeColumns, diff --git a/cluster.go b/cluster.go index f084da33c..dd7de8198 100644 --- a/cluster.go +++ b/cluster.go @@ -262,8 +262,8 @@ type cluster struct { InternalClient InternalClient } -// NewCluster returns a new instance of Cluster with defaults. -func NewCluster() *cluster { +// newCluster returns a new instance of Cluster with defaults. +func newCluster() *cluster { return &cluster{ Hasher: &jmphasher{}, partitionN: DefaultPartitionN, @@ -708,7 +708,7 @@ func (c *cluster) fragSources(to *cluster, idx *Index) (map[string][]*internal.R // require that a replica fragment be the source data. srcCluster := c if action == resizeJobActionAdd && c.ReplicaN > 1 { - srcCluster = NewCluster() + srcCluster = newCluster() srcCluster.Nodes = Nodes(c.Nodes).Clone() srcCluster.Hasher = c.Hasher srcCluster.partitionN = c.partitionN @@ -1109,7 +1109,7 @@ func (c *cluster) generateResizeJobByAction(nodeAction nodeAction) (*resizeJob, j.Broadcaster = c.broadcaster // toCluster is a clone of Cluster with the new node added/removed for comparison. - toCluster := NewCluster() + toCluster := newCluster() toCluster.Nodes = Nodes(c.Nodes).Clone() toCluster.Hasher = c.Hasher toCluster.partitionN = c.partitionN diff --git a/cluster_internal_test.go b/cluster_internal_test.go index d607cd883..845fdd888 100644 --- a/cluster_internal_test.go +++ b/cluster_internal_test.go @@ -43,7 +43,7 @@ func TestFragCombos(t *testing.T) { node0 := &Node{ID: "node0", URI: *uri0} node1 := &Node{ID: "node1", URI: *uri1} - c := NewCluster() + c := newCluster() c.addNodeBasicSorted(node0) c.addNodeBasicSorted(node1) @@ -120,29 +120,29 @@ func TestFragSources(t *testing.T) { node2 := &Node{ID: "node2", URI: *uri2} node3 := &Node{ID: "node3", URI: *uri3} - c1 := NewCluster() + c1 := newCluster() c1.ReplicaN = 1 c1.addNodeBasicSorted(node0) c1.addNodeBasicSorted(node1) - c2 := NewCluster() + c2 := newCluster() c2.ReplicaN = 1 c2.addNodeBasicSorted(node0) c2.addNodeBasicSorted(node1) c2.addNodeBasicSorted(node2) - c3 := NewCluster() + c3 := newCluster() c3.ReplicaN = 2 c3.addNodeBasicSorted(node0) c3.addNodeBasicSorted(node1) - c4 := NewCluster() + c4 := newCluster() c4.ReplicaN = 2 c4.addNodeBasicSorted(node0) c4.addNodeBasicSorted(node1) c4.addNodeBasicSorted(node2) - c5 := NewCluster() + c5 := newCluster() c5.ReplicaN = 2 c5.addNodeBasicSorted(node0) c5.addNodeBasicSorted(node1) @@ -340,7 +340,7 @@ func TestCluster_Owners(t *testing.T) { // Ensure the partitioner can assign a fragment to a partition. func TestCluster_Partition(t *testing.T) { if err := quick.Check(func(index string, shard uint64, partitionN int) bool { - c := NewCluster() + c := newCluster() c.partitionN = partitionN partitionID := c.partition(index, shard) @@ -457,10 +457,10 @@ func TestCluster_Coordinator(t *testing.T) { node1 := &Node{ID: "node1", URI: uri1} node2 := &Node{ID: "node2", URI: uri2} - c1 := *NewCluster() + c1 := *newCluster() c1.Node = node1 c1.Coordinator = node1.ID - c2 := *NewCluster() + c2 := *newCluster() c2.Node = node2 c2.Coordinator = node1.ID diff --git a/executor.go b/executor.go index 032bf4225..4194e7b4e 100644 --- a/executor.go +++ b/executor.go @@ -80,7 +80,7 @@ func newExecutor(opts ...executorOption) *executor { } // Execute executes a PQL query. -func (e *executor) Execute(ctx context.Context, index string, q *pql.Query, shards []uint64, opt *ExecOptions) ([]interface{}, error) { +func (e *executor) Execute(ctx context.Context, index string, q *pql.Query, shards []uint64, opt *execOptions) ([]interface{}, error) { // Verify that an index is set. if index == "" { return nil, ErrIndexRequired @@ -98,7 +98,7 @@ func (e *executor) Execute(ctx context.Context, index string, q *pql.Query, shar // Default options. if opt == nil { - opt = &ExecOptions{} + opt = &execOptions{} } // Translate query keys to ids, if necessary. @@ -123,7 +123,7 @@ func (e *executor) Execute(ctx context.Context, index string, q *pql.Query, shar return results, nil } -func (e *executor) execute(ctx context.Context, index string, q *pql.Query, shards []uint64, opt *ExecOptions) ([]interface{}, error) { +func (e *executor) execute(ctx context.Context, index string, q *pql.Query, shards []uint64, opt *execOptions) ([]interface{}, error) { // Don't bother calculating shards for query types that don't require it. needsShards := needsShards(q.Calls) @@ -162,7 +162,7 @@ func (e *executor) execute(ctx context.Context, index string, q *pql.Query, shar } // executeCall executes a call. -func (e *executor) executeCall(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) (interface{}, error) { +func (e *executor) executeCall(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) (interface{}, error) { if err := e.validateCallArgs(c); err != nil { return nil, errors.Wrap(err, "validating args") } @@ -220,7 +220,7 @@ func (e *executor) validateCallArgs(c *pql.Call) error { } // executeSum executes a Sum() call. -func (e *executor) executeSum(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) (ValCount, error) { +func (e *executor) executeSum(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) (ValCount, error) { if field := c.Args["field"]; field == "" { return ValCount{}, errors.New("Sum(): field required") } @@ -253,7 +253,7 @@ func (e *executor) executeSum(ctx context.Context, index string, c *pql.Call, sh } // executeMin executes a Min() call. -func (e *executor) executeMin(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) (ValCount, error) { +func (e *executor) executeMin(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) (ValCount, error) { if field := c.Args["field"]; field == "" { return ValCount{}, errors.New("Min(): field required") } @@ -286,7 +286,7 @@ func (e *executor) executeMin(ctx context.Context, index string, c *pql.Call, sh } // executeMax executes a Max() call. -func (e *executor) executeMax(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) (ValCount, error) { +func (e *executor) executeMax(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) (ValCount, error) { if field := c.Args["field"]; field == "" { return ValCount{}, errors.New("Max(): field required") } @@ -319,7 +319,7 @@ func (e *executor) executeMax(ctx context.Context, index string, c *pql.Call, sh } // executeBitmapCall executes a call that returns a bitmap. -func (e *executor) executeBitmapCall(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) (*Row, error) { +func (e *executor) executeBitmapCall(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) (*Row, error) { // Execute calls in bulk on each remote node and merge. mapFn := func(shard uint64) (interface{}, error) { return e.executeBitmapCallShard(ctx, index, c, shard) @@ -521,7 +521,7 @@ func (e *executor) executeMaxShard(ctx context.Context, index string, c *pql.Cal // executeTopN executes a TopN() call. // This first performs the TopN() to determine the top results and then // requeries to retrieve the full counts for each of the top results. -func (e *executor) executeTopN(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) ([]Pair, error) { +func (e *executor) executeTopN(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) ([]Pair, error) { idsArg, _, err := c.UintSliceArg("ids") if err != nil { return nil, fmt.Errorf("executeTopN: %v", err) @@ -560,7 +560,7 @@ func (e *executor) executeTopN(ctx context.Context, index string, c *pql.Call, s return trimmedList, nil } -func (e *executor) executeTopNShards(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) ([]Pair, error) { +func (e *executor) executeTopNShards(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) ([]Pair, error) { // Execute calls in bulk on each remote node and merge. mapFn := func(shard uint64) (interface{}, error) { return e.executeTopNShard(ctx, index, c, shard) @@ -964,7 +964,7 @@ func (e *executor) executeXorShard(ctx context.Context, index string, c *pql.Cal } // executeCount executes a count() call. -func (e *executor) executeCount(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *ExecOptions) (uint64, error) { +func (e *executor) executeCount(ctx context.Context, index string, c *pql.Call, shards []uint64, opt *execOptions) (uint64, error) { if len(c.Children) == 0 { return 0, errors.New("Count() requires an input bitmap") } else if len(c.Children) > 1 { @@ -996,7 +996,7 @@ func (e *executor) executeCount(ctx context.Context, index string, c *pql.Call, } // executeClearBit executes a Clear() call. -func (e *executor) executeClearBit(ctx context.Context, index string, c *pql.Call, opt *ExecOptions) (bool, error) { +func (e *executor) executeClearBit(ctx context.Context, index string, c *pql.Call, opt *execOptions) (bool, error) { fieldName, err := c.FieldArg() if err != nil { return false, errors.New("Clear() argument required: field") @@ -1031,7 +1031,7 @@ func (e *executor) executeClearBit(ctx context.Context, index string, c *pql.Cal } // executeClearBitField executes a Clear() call for a single view. -func (e *executor) executeClearBitField(ctx context.Context, index string, c *pql.Call, f *Field, colID, rowID uint64, opt *ExecOptions) (bool, error) { +func (e *executor) executeClearBitField(ctx context.Context, index string, c *pql.Call, f *Field, colID, rowID uint64, opt *execOptions) (bool, error) { shard := colID / ShardWidth ret := false for _, node := range e.Cluster.shardNodes(index, shard) { @@ -1061,7 +1061,7 @@ func (e *executor) executeClearBitField(ctx context.Context, index string, c *pq } // executeSetBit executes a Set() call. -func (e *executor) executeSetBit(ctx context.Context, index string, c *pql.Call, opt *ExecOptions) (bool, error) { +func (e *executor) executeSetBit(ctx context.Context, index string, c *pql.Call, opt *execOptions) (bool, error) { fieldName, err := c.FieldArg() if err != nil { return false, errors.New("Set() argument required: field") @@ -1106,7 +1106,7 @@ func (e *executor) executeSetBit(ctx context.Context, index string, c *pql.Call, } // executeSetBitField executes a Set() call for a specific view. -func (e *executor) executeSetBitField(ctx context.Context, index string, c *pql.Call, f *Field, colID, rowID uint64, timestamp *time.Time, opt *ExecOptions) (bool, error) { +func (e *executor) executeSetBitField(ctx context.Context, index string, c *pql.Call, f *Field, colID, rowID uint64, timestamp *time.Time, opt *execOptions) (bool, error) { shard := colID / ShardWidth ret := false @@ -1138,7 +1138,7 @@ func (e *executor) executeSetBitField(ctx context.Context, index string, c *pql. } // executeSetValue executes a SetValue() call. -func (e *executor) executeSetValue(ctx context.Context, index string, c *pql.Call, opt *ExecOptions) error { +func (e *executor) executeSetValue(ctx context.Context, index string, c *pql.Call, opt *execOptions) error { // Parse labels. columnID, ok, err := c.UintArg(columnLabel) if err != nil { @@ -1198,7 +1198,7 @@ func (e *executor) executeSetValue(ctx context.Context, index string, c *pql.Cal } // executeSetRowAttrs executes a SetRowAttrs() call. -func (e *executor) executeSetRowAttrs(ctx context.Context, index string, c *pql.Call, opt *ExecOptions) error { +func (e *executor) executeSetRowAttrs(ctx context.Context, index string, c *pql.Call, opt *execOptions) error { fieldName, ok := c.Args["_field"].(string) if !ok { return errors.New("SetRowAttrs() field required") @@ -1255,7 +1255,7 @@ func (e *executor) executeSetRowAttrs(ctx context.Context, index string, c *pql. } // executeBulkSetRowAttrs executes a set of SetRowAttrs() calls. -func (e *executor) executeBulkSetRowAttrs(ctx context.Context, index string, calls []*pql.Call, opt *ExecOptions) ([]interface{}, error) { +func (e *executor) executeBulkSetRowAttrs(ctx context.Context, index string, calls []*pql.Call, opt *execOptions) ([]interface{}, error) { // Collect attributes by field/id. m := make(map[string]map[uint64]map[string]interface{}) for _, c := range calls { @@ -1342,7 +1342,7 @@ func (e *executor) executeBulkSetRowAttrs(ctx context.Context, index string, cal } // executeSetColumnAttrs executes a SetColumnAttrs() call. -func (e *executor) executeSetColumnAttrs(ctx context.Context, index string, c *pql.Call, opt *ExecOptions) error { +func (e *executor) executeSetColumnAttrs(ctx context.Context, index string, c *pql.Call, opt *execOptions) error { // Retrieve index. idx := e.Holder.Index(index) if idx == nil { @@ -1390,7 +1390,7 @@ func (e *executor) executeSetColumnAttrs(ctx context.Context, index string, c *p } // exec executes a PQL query remotely for a set of shards on a node. -func (e *executor) remoteExec(ctx context.Context, node *Node, index string, q *pql.Query, shards []uint64, opt *ExecOptions) (results []interface{}, err error) { +func (e *executor) remoteExec(ctx context.Context, node *Node, index string, q *pql.Query, shards []uint64, opt *execOptions) (results []interface{}, err error) { // Encode request object. pbreq := &internal.QueryRequest{ Query: q.String(), @@ -1461,7 +1461,7 @@ loop: // // If a mapping of shards to a node fails then the shards are resplit across // secondary nodes and retried. This continues to occur until all nodes are exhausted. -func (e *executor) mapReduce(ctx context.Context, index string, shards []uint64, c *pql.Call, opt *ExecOptions, mapFn mapFunc, reduceFn reduceFunc) (interface{}, error) { +func (e *executor) mapReduce(ctx context.Context, index string, shards []uint64, c *pql.Call, opt *execOptions, mapFn mapFunc, reduceFn reduceFunc) (interface{}, error) { ch := make(chan mapResponse) // Wrap context with a cancel to kill goroutines on exit. @@ -1520,7 +1520,7 @@ func (e *executor) mapReduce(ctx context.Context, index string, shards []uint64, } } -func (e *executor) mapper(ctx context.Context, ch chan mapResponse, nodes []*Node, index string, shards []uint64, c *pql.Call, opt *ExecOptions, mapFn mapFunc, reduceFn reduceFunc) error { +func (e *executor) mapper(ctx context.Context, ch chan mapResponse, nodes []*Node, index string, shards []uint64, c *pql.Call, opt *execOptions, mapFn mapFunc, reduceFn reduceFunc) error { // Group shards together by nodes. m, err := e.shardsByNode(nodes, index, shards) if err != nil { @@ -1710,8 +1710,8 @@ type mapResponse struct { err error } -// ExecOptions represents an execution context for a single Execute() call. -type ExecOptions struct { +// execOptions represents an execution context for a single Execute() call. +type execOptions struct { Remote bool ExcludeRowAttrs bool ExcludeColumns bool diff --git a/server.go b/server.go index 43af168ba..fe99abf4c 100644 --- a/server.go +++ b/server.go @@ -228,7 +228,7 @@ func OptServerClusterHasher(h Hasher) ServerOption { func NewServer(opts ...ServerOption) (*Server, error) { s := &Server{ closing: make(chan struct{}), - cluster: NewCluster(), + cluster: newCluster(), holder: NewHolder(), diagnostics: NewDiagnosticsCollector(DefaultDiagnosticServer), systemInfo: NewNopSystemInfo(), diff --git a/translate.go b/translate.go index 6cb940d53..660269ea0 100644 --- a/translate.go +++ b/translate.go @@ -212,7 +212,7 @@ func (s *TranslateFile) applyEntry(entry *LogEntry, offset int64) error { key := entry.Keys[i] // Determine key offset based on ID size. - sz := int64(UvarintSize(id)) + sz := int64(uVarintSize(id)) idx.insert(id, offset+sz) // Move sequence forward. @@ -221,7 +221,7 @@ func (s *TranslateFile) applyEntry(entry *LogEntry, offset int64) error { } // Move offset forward. - offset += sz + int64(UvarintSize(uint64(len(key)))) + int64(len(key)) + offset += sz + int64(uVarintSize(uint64(len(key)))) + int64(len(key)) } return nil @@ -560,11 +560,11 @@ type LogEntry struct { // HeaderSize returns the number of bytes required for size, type, index, frame, & pair count. func (e *LogEntry) HeaderSize() int64 { - sz := UvarintSize(e.Length) + // total entry length + sz := uVarintSize(e.Length) + // total entry length 1 + // type - UvarintSize(uint64(len(e.Index))) + len(e.Index) + // Index length and data - UvarintSize(uint64(len(e.Frame))) + len(e.Frame) + // Frame length and data - UvarintSize(uint64(len(e.IDs))) // ID/Key pair count + uVarintSize(uint64(len(e.Index))) + len(e.Index) + // Index length and data + uVarintSize(uint64(len(e.Frame))) + len(e.Frame) + // Frame length and data + uVarintSize(uint64(len(e.IDs))) // ID/Key pair count return int64(sz) } @@ -574,13 +574,13 @@ func (e *LogEntry) ReadFrom(r io.Reader) (_ int64, err error) { // Read the entry length. if e.Length, err = binary.ReadUvarint(br); err != nil { - return int64(UvarintSize(e.Length)), err + return int64(uVarintSize(e.Length)), err } // Slurp entire entry and replace reader. buf := make([]byte, e.Length) n, err := io.ReadFull(r, buf) - n64 := int64(n + UvarintSize(e.Length)) + n64 := int64(n + uVarintSize(e.Length)) if err != nil { return n64, err } @@ -706,8 +706,8 @@ func (e *LogEntry) WriteTo(w io.Writer) (_ int64, err error) { return int64(sz) + n, err } -// ValidLogEntriesLen returns the maximum length of p that contains valid entries. -func ValidLogEntriesLen(p []byte) (n int) { +// validLogEntriesLen returns the maximum length of p that contains valid entries. +func validLogEntriesLen(p []byte) (n int) { r := bytes.NewReader(p) for { if sz, err := binary.ReadUvarint(r); err != nil { @@ -984,13 +984,13 @@ func (r *TranslateFileReader) read(p []byte) (n int, err error) { // Read data from file at offset. // Limit the number of bytes read to only whole entries. n, err = r.file.ReadAt(p, r.offset) - n = ValidLogEntriesLen(p[:n]) + n = validLogEntriesLen(p[:n]) r.offset += int64(n) return n, err } // Copied & modified from encoding/binary. -func UvarintSize(x uint64) (i int) { +func uVarintSize(x uint64) (i int) { for x >= 0x80 { x >>= 7 i++ diff --git a/utils_internal_test.go b/utils_internal_test.go index 171955b4e..1d88c3d32 100644 --- a/utils_internal_test.go +++ b/utils_internal_test.go @@ -34,7 +34,7 @@ func NewTestCluster(n int) *cluster { panic(err) } - c := NewCluster() + c := newCluster() c.ReplicaN = 1 c.Hasher = NewTestModHasher() c.Path = path @@ -222,7 +222,7 @@ func (t *ClusterCluster) addCluster(i int, saveTopology bool) (*cluster, error) h.Path = path // cluster - c := NewCluster() + c := newCluster() c.ReplicaN = 1 c.Hasher = NewTestModHasher() c.Path = path