Skip to content

Commit

Permalink
Remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Dec 6, 2021
1 parent 53d7c1b commit 9a48e21
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 296 deletions.
83 changes: 0 additions & 83 deletions document/array.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package document

import (
"sort"

"github.com/buger/jsonparser"
"github.com/genjidb/genji/internal/errors"
"github.com/genjidb/genji/types"
Expand Down Expand Up @@ -241,84 +239,3 @@ func (vb *ValueBuffer) UnmarshalJSON(data []byte) error {

return nil
}

func (vb *ValueBuffer) Types() []types.ValueType {
types := make([]types.ValueType, len(vb.Values))

for i, v := range vb.Values {
types[i] = v.Type()
}

return types
}

// IsEqual compares two ValueBuffer and returns true if and only if
// both each values and types are respectively equal.
func (vb *ValueBuffer) IsEqual(other *ValueBuffer) bool {
if vb.Len() != other.Len() {
return false
}

// empty buffers are always equal eh
if vb.Len() == 0 && other.Len() == 0 {
return true
}

otherTypes := other.Types()
tps := vb.Types()

for i, typ := range tps {
if typ != otherTypes[i] {
return false
}
}

for i, v := range vb.Values {
if eq, err := types.IsEqual(v, other.Values[i]); err != nil || !eq {
return false
}
}

return true
}

func (vb *ValueBuffer) Swap(i, j int) {
vb.Values[i], vb.Values[j] = vb.Values[j], vb.Values[i]
}

func (vb *ValueBuffer) Less(i, j int) (ok bool) {
it, jt := vb.Values[i].Type(), vb.Values[j].Type()
if it == jt || (it.IsNumber() && jt.IsNumber()) {
// TODO(asdine) make the types package work with static documents
// to avoid having to deal with errors?
ok, _ = types.IsLesserThan(vb.Values[i], vb.Values[j])
return
}

return it < jt
}

// SortArray creates a new sorted array.
// Types are sorted in the following ascending order:
// - NULL
// - Booleans
// - Numbers
// - Text
// - Blob
// - Arrays
// - Documents
// It doesn't sort nested arrays.
func SortArray(a types.Array) (*ValueBuffer, error) {
vb, ok := a.(*ValueBuffer)
if !ok {
vb := NewValueBuffer()
err := vb.Copy(a)
if err != nil {
return nil, err
}
}

sort.Sort(vb)

return vb, nil
}
27 changes: 0 additions & 27 deletions document/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,6 @@ func TestArrayContains(t *testing.T) {
require.False(t, ok)
}

func TestSortArray(t *testing.T) {
tests := []struct {
name string
arr string
expected string
}{
{"empty array", `[]`, `[]`},
{"numbers", `[1.4,3,2.1,-5]`, `[-5,1.4,2.1,3]`},
{"text", `["foo","bar",""]`, `["","bar","foo"]`},
{"arrays", `[[1, 2],[-1,10],[]]`, `[[],[-1,10],[1,2]]`},
{"documents", `[{"z":10},{"a":40},{}]`, `[{},{"a":40},{"z":10}]`},
{"mixed", `["foo",["a"],{},null,true,10]`, `[null,true,10,"foo",["a"],{}]`},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var arr document.ValueBuffer
assert.NoError(t, arr.UnmarshalJSON([]byte(test.arr)))
output, err := document.SortArray(&arr)
assert.NoError(t, err)
actual, err := json.Marshal(output)
assert.NoError(t, err)
require.Equal(t, test.expected, string(actual))
})
}
}

func TestValueBufferCopy(t *testing.T) {
tests := []struct {
name string
Expand Down
31 changes: 0 additions & 31 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package document

import (
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -57,16 +56,6 @@ func Length(d types.Document) (int, error) {
return len, err
}

// Fields returns a list of all the fields at the root of the document
// sorted lexicographically.
func Fields(d types.Document) ([]string, error) {
if fb, ok := d.(*FieldBuffer); ok {
return fb.Fields(), nil
}

return types.Fields(d)
}

// FieldBuffer stores a group of fields in memory. It implements the Document interface.
type FieldBuffer struct {
fields []fieldValue
Expand Down Expand Up @@ -342,14 +331,6 @@ func (fb *FieldBuffer) Copy(d types.Document) error {
return nil
}

// Clone the buffer.
func (fb *FieldBuffer) Clone() *FieldBuffer {
var newFb FieldBuffer

_ = newFb.Copy(fb)
return &newFb
}

// Apply a function to all the values of the buffer.
func (fb *FieldBuffer) Apply(fn func(p Path, v types.Value) (types.Value, error)) error {
path := Path{PathFragment{}}
Expand Down Expand Up @@ -415,18 +396,6 @@ func (fb *FieldBuffer) Reset() {
fb.fields = fb.fields[:0]
}

// Fields returns a sorted list of root field names.
func (fb *FieldBuffer) Fields() []string {
fields := make([]string, len(fb.fields))

for i := range fb.fields {
fields[i] = fb.fields[i].Field
}

sort.Strings(fields)
return fields
}

// A Path represents the path to a particular value within a document.
type Path []PathFragment

Expand Down
5 changes: 0 additions & 5 deletions document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ func TestFieldBuffer(t *testing.T) {
require.Zero(t, v)
})

t.Run("Fields", func(t *testing.T) {
require.Equal(t, []string{}, document.NewFieldBuffer().Fields())
require.Equal(t, []string{"a", "b"}, buf.Fields())
})

t.Run("Set", func(t *testing.T) {
tests := []struct {
name string
Expand Down
8 changes: 4 additions & 4 deletions internal/database/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func TestTableInsert(t *testing.T) {
defer cleanup()

doc := newDocument()
key1, _, err := tb.Insert(doc.Clone())
key1, _, err := tb.Insert(testutil.CloneDocument(t, doc))
assert.NoError(t, err)
require.NotEmpty(t, key1)

key2, _, err := tb.Insert(doc.Clone())
key2, _, err := tb.Insert(testutil.CloneDocument(t, doc))
assert.NoError(t, err)
require.NotEmpty(t, key2)

Expand Down Expand Up @@ -532,9 +532,9 @@ func TestTableDelete(t *testing.T) {
doc1.Add("fieldc", types.NewIntegerValue(40))
doc2 := newDocument()

key1, _, err := tb.Insert(doc1.Clone())
key1, _, err := tb.Insert(testutil.CloneDocument(t, doc1))
assert.NoError(t, err)
key2, _, err := tb.Insert(doc2.Clone())
key2, _, err := tb.Insert(testutil.CloneDocument(t, doc2))
assert.NoError(t, err)

// delete the document
Expand Down
99 changes: 0 additions & 99 deletions internal/environment/env.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package environment

import (
"strings"

"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/database"
"github.com/genjidb/genji/internal/stringutil"
Expand All @@ -12,7 +10,6 @@ import (
var (
TableKey = document.Path{document.PathFragment{FieldName: "$table"}}
DocPKKey = document.Path{document.PathFragment{FieldName: "$pk"}}
// OriginalDocumentKey = document.Path{document.PathFragment{FieldName: "$originalDocument"}}
)

// A Param represents a parameter passed by the user to the statement.
Expand Down Expand Up @@ -162,99 +159,3 @@ func (e *Environment) GetCatalog() *database.Catalog {

return nil
}

func (e *Environment) Clone() (*Environment, error) {
var newEnv Environment

newEnv.Params = e.Params
newEnv.Tx = e.Tx
newEnv.Catalog = e.Catalog

if e.Doc != nil {
fb := document.NewFieldBuffer()
err := fb.Copy(e.Doc)
if err != nil {
return nil, err
}

newEnv.Doc = fb
}

if e.Vars != nil {
fb := document.NewFieldBuffer()
err := fb.Copy(e.Vars)
if err != nil {
return nil, err
}

newEnv.Vars = fb
}

if e.Outer != nil {
newOuter, err := e.Outer.Clone()
if err != nil {
return nil, err
}
newEnv.Outer = newOuter
}

return &newEnv, nil
}

func (e *Environment) MarshalJSON() ([]byte, error) {
var sb strings.Builder

var needComa bool
sb.WriteByte('{')
if e.Doc != nil {
sb.WriteString("\"Doc\":")
b, err := types.NewDocumentValue(e.Doc).MarshalJSON()
if err != nil {
return nil, err
}
sb.Write(b)
needComa = true
}

if e.Vars != nil {
if needComa {
sb.WriteByte(',')
}
sb.WriteString("\"Vars\":")
b, err := types.NewDocumentValue(e.Vars).MarshalJSON()
if err != nil {
return nil, err
}
sb.Write(b)
needComa = true
}

if e.Params != nil {
if needComa {
sb.WriteByte(',')
}
sb.WriteString("\"Params\":")
for i, p := range e.Params {
if i > 0 {
sb.WriteByte(',')
}
sb.WriteString(stringutil.Sprintf("{\"Name\":\"%s\",\"Value\":%v}", p.Name, p.Value))
}

needComa = true
}

if e.Outer != nil {
if needComa {
sb.WriteByte(',')
}
sb.WriteString("\"Outer\":")
b, _ := e.Outer.MarshalJSON()
sb.Write(b)
needComa = true
}

sb.WriteByte('}')

return []byte(sb.String()), nil
}
45 changes: 0 additions & 45 deletions internal/environment/env_test.go

This file was deleted.

Loading

0 comments on commit 9a48e21

Please sign in to comment.