You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

  1. // Copyright (c) 2017 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vellum
  15. import (
  16. "encoding/binary"
  17. "fmt"
  18. "io"
  19. )
  20. const headerSize = 16
  21. type encoderConstructor func(w io.Writer) encoder
  22. type decoderConstructor func([]byte) decoder
  23. var encoders = map[int]encoderConstructor{}
  24. var decoders = map[int]decoderConstructor{}
  25. type encoder interface {
  26. start() error
  27. encodeState(s *builderNode, addr int) (int, error)
  28. finish(count, rootAddr int) error
  29. reset(w io.Writer)
  30. }
  31. func loadEncoder(ver int, w io.Writer) (encoder, error) {
  32. if cons, ok := encoders[ver]; ok {
  33. return cons(w), nil
  34. }
  35. return nil, fmt.Errorf("no encoder for version %d registered", ver)
  36. }
  37. func registerEncoder(ver int, cons encoderConstructor) {
  38. encoders[ver] = cons
  39. }
  40. type decoder interface {
  41. getRoot() int
  42. getLen() int
  43. stateAt(addr int, prealloc fstState) (fstState, error)
  44. }
  45. func loadDecoder(ver int, data []byte) (decoder, error) {
  46. if cons, ok := decoders[ver]; ok {
  47. return cons(data), nil
  48. }
  49. return nil, fmt.Errorf("no decoder for version %d registered", ver)
  50. }
  51. func registerDecoder(ver int, cons decoderConstructor) {
  52. decoders[ver] = cons
  53. }
  54. func decodeHeader(header []byte) (ver int, typ int, err error) {
  55. if len(header) < headerSize {
  56. err = fmt.Errorf("invalid header < 16 bytes")
  57. return
  58. }
  59. ver = int(binary.LittleEndian.Uint64(header[0:8]))
  60. typ = int(binary.LittleEndian.Uint64(header[8:16]))
  61. return
  62. }
  63. // fstState represents a state inside the FTS runtime
  64. // It is the main contract between the FST impl and the decoder
  65. // The FST impl should work only with this interface, while only the decoder
  66. // impl knows the physical representation.
  67. type fstState interface {
  68. Address() int
  69. Final() bool
  70. FinalOutput() uint64
  71. NumTransitions() int
  72. TransitionFor(b byte) (int, int, uint64)
  73. TransitionAt(i int) byte
  74. }