mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
We add endpoints and protobuf encode/decode to allow for sending sharded requests over the wire in protobuf, so we can take our sharded data and send it to other nodes if needed. This is a squash of >15 other commits, so a bit of history is relevant: The Request type had FieldTypes in it because the field type information was needed for sharding because sorting requires that information. We change this around to make the external sharding operation require the field types, and curry that through the codec -- the codec is needed to tell the request how it shards. (This is because the correct sorting order varies by field type.) Requests (and ShardedRequests) no longer have that table in them. And then we hit a nasty bug in production and RCA showed that our testing wasn't good enough and we need to be more careful, and I discovered that test coverage in this package was around 70%. So, the other big thing here is coverage testing; in order to make coverage testing viable and programmatically testable, we have added the ability to render requests *back* to JSON. This is not a great idea, but it does allow us to do a lot of sanity-checking and verify that the encodings we're using are consistent and correct. This, plus some specific tests of decoding specific flawed inputs, has caught a number of issues. Which are now fixed! A lot of internal API surface got slightly changed, in ways that make it simpler to work with. For instance, the (*FieldOperation).TranslateUnsigned function doesn't really need to exist; we can just have a non-method translate function for unsigned and for signed, and use them based on field type. The stable translation hack used for testing had a bug that could allow it to end up producing incorrect results if you asked it to translate an ID first rather than exclusively asking it to translate strings first, this has been corrected. (This is a bug fix in code that was added partway through creating this, but is tricky enough to mention its own comment.) Test coverage is now just over 90%, and a lot of what's left is error-check returns that may well be actually unreachable unless, say, the documentation for encoding/json is full of lies. Which it probably is.
135 lines
3.9 KiB
Go
135 lines
3.9 KiB
Go
// Copyright 2021 Molecula Corp.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package ingest
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
"unsafe"
|
|
)
|
|
|
|
// StringTable is a mapping of strings to temporary IDs.
|
|
// All mapped-to IDs fall in the range [0, len). The zero value,
|
|
// a nil map, instead just parses the numbers.
|
|
//
|
|
// We keep the array of names in creation order because we want reproducibility;
|
|
// the first key we see is always key 0. Otherwise, the keys are created in
|
|
// arbitrary orders.
|
|
type StringTable struct {
|
|
names []string
|
|
values map[string]uint64
|
|
}
|
|
|
|
// NewStringTable just creates a string table with a non-nil map.
|
|
func NewStringTable() *StringTable {
|
|
return &StringTable{values: map[string]uint64{}}
|
|
}
|
|
|
|
// unsafe is
|
|
func pretendByteIsString(data []byte) (result string) {
|
|
dH := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
|
sH := (*reflect.StringHeader)(unsafe.Pointer(&result))
|
|
sH.Data = dH.Data
|
|
sH.Len = dH.Len
|
|
return result
|
|
}
|
|
|
|
// ID returns an ID associated to the string, adding it to the table if it is not already present,
|
|
// or parsing an integer if there's no table.
|
|
func (tbl *StringTable) ID(in []byte) (uint64, error) {
|
|
str := pretendByteIsString(in)
|
|
if tbl != nil {
|
|
id, ok := tbl.values[str]
|
|
if !ok {
|
|
id = uint64(len(tbl.values))
|
|
tbl.values[str] = id
|
|
tbl.names = append(tbl.names, str)
|
|
}
|
|
return id, nil
|
|
}
|
|
return strconv.ParseUint(str, 10, 64)
|
|
}
|
|
|
|
// SignedID returns an ID associated to the string, adding it to the table if it is not already present,
|
|
// or parsing an integer if there's no table. It yields signed values only.
|
|
func (tbl *StringTable) IntID(in []byte) (int64, error) {
|
|
str := pretendByteIsString(in)
|
|
if tbl != nil {
|
|
id, ok := tbl.values[str]
|
|
if !ok {
|
|
id = uint64(len(tbl.values))
|
|
tbl.values[str] = id
|
|
tbl.names = append(tbl.names, str)
|
|
}
|
|
return int64(id), nil
|
|
}
|
|
return strconv.ParseInt(str, 10, 64)
|
|
}
|
|
|
|
// MapForStringTable, given a string table mapping strings to consecutive
|
|
// integers and a translation function from strings to "real" keys, yields
|
|
// a translation/lookup slice. If it cannot translate all the keys, it
|
|
// returns an error.
|
|
func (tbl *StringTable) MakeIDMap(keys KeyTranslator) ([]uint64, error) {
|
|
lookedUp, err := keys.TranslateKeys(tbl.names...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(lookedUp) != len(tbl.names) {
|
|
return nil, fmt.Errorf("missing keys: expected %d keys, got %d", len(tbl.values), len(lookedUp))
|
|
}
|
|
out := make([]uint64, len(tbl.names))
|
|
for i, v := range tbl.names {
|
|
out[i] = lookedUp[v]
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// translateSigned replaces values from 0 to len(mapping)-1 with the
|
|
// elements of mapping. It yields an error if any values aren't
|
|
// mapped.
|
|
func translateSigned(mapping []uint64, values []int64) error {
|
|
oops := 0
|
|
for i, v := range values {
|
|
if v >= int64(len(mapping)) {
|
|
oops++
|
|
} else {
|
|
values[i] = int64(mapping[v])
|
|
}
|
|
}
|
|
if oops > 0 {
|
|
return fmt.Errorf("encountered %d out-of-range signed values when applying translation mapping", oops)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// translateUnsigned replaces values from 0 to len(mapping)-1 with the
|
|
// elements of mapping. It yields an error if any values aren't
|
|
// mapped.
|
|
func translateUnsigned(mapping []uint64, values []uint64) error {
|
|
oops := 0
|
|
for i, v := range values {
|
|
if v >= uint64(len(mapping)) {
|
|
oops++
|
|
} else {
|
|
values[i] = mapping[v]
|
|
}
|
|
}
|
|
if oops > 0 {
|
|
return fmt.Errorf("encountered %d out-of-range signed values when applying translation mapping", oops)
|
|
}
|
|
return nil
|
|
}
|