Skip to content

Commit

Permalink
conduit: Reorganize ahead of repo split. (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
winder authored Mar 3, 2023
1 parent 93f02f5 commit bf12a7e
Show file tree
Hide file tree
Showing 40 changed files with 290 additions and 289 deletions.
45 changes: 0 additions & 45 deletions accounting/accounting.go

This file was deleted.

3 changes: 1 addition & 2 deletions cmd/algorand-indexer/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
_ "github.com/algorand/indexer/conduit/plugins/exporters/postgresql"
_ "github.com/algorand/indexer/conduit/plugins/importers/algod"
"github.com/algorand/indexer/config"
"github.com/algorand/indexer/fetcher"
"github.com/algorand/indexer/idb"
iutil "github.com/algorand/indexer/util"
)
Expand Down Expand Up @@ -305,7 +304,7 @@ func runDaemon(daemonConfig *daemonConfig) error {
}

if daemonConfig.algodDataDir != "" {
daemonConfig.algodAddr, daemonConfig.algodToken, _, err = fetcher.AlgodArgsForDataDir(daemonConfig.algodDataDir)
daemonConfig.algodAddr, daemonConfig.algodToken, _, err = config.AlgodArgsForDataDir(daemonConfig.algodDataDir)
if err != nil {
return fmt.Errorf("algod data dir err, %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/algorand-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/algorand/indexer/idb"
"github.com/algorand/indexer/idb/dummy"
_ "github.com/algorand/indexer/idb/postgres"
_ "github.com/algorand/indexer/util/disabledeadlock"
"github.com/algorand/indexer/util/metrics"
"github.com/algorand/indexer/version"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/conduit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

"github.com/algorand/indexer/cmd/conduit/internal/list"
"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/loggers"
"github.com/algorand/indexer/conduit/pipeline"
_ "github.com/algorand/indexer/conduit/plugins/exporters/all"
_ "github.com/algorand/indexer/conduit/plugins/importers/all"
_ "github.com/algorand/indexer/conduit/plugins/processors/all"
"github.com/algorand/indexer/loggers"
"github.com/algorand/indexer/version"
)

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion conduit/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package conduit
import (
"github.com/prometheus/client_golang/prometheus"

"github.com/algorand/indexer/data"
"github.com/algorand/indexer/conduit/data"
)

// OnCompleteFunc is the signature for the Completed functional interface.
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion conduit/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"gopkg.in/yaml.v3"

"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/conduit/plugins/importers"
"github.com/algorand/indexer/conduit/plugins/processors"
"github.com/algorand/indexer/data"
"github.com/algorand/indexer/util"
"github.com/algorand/indexer/util/metrics"

Expand Down
2 changes: 1 addition & 1 deletion conduit/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/stretchr/testify/mock"

"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/conduit/plugins/importers"
"github.com/algorand/indexer/conduit/plugins/processors"
"github.com/algorand/indexer/data"
_ "github.com/algorand/indexer/util/metrics"
)

Expand Down
2 changes: 1 addition & 1 deletion conduit/plugins/exporters/example/example_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/sirupsen/logrus"

"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/data"
)

// This is our exporter object. It should store all the in memory data required to run the Exporter.
Expand Down
2 changes: 1 addition & 1 deletion conduit/plugins/exporters/example/example_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

"github.com/stretchr/testify/assert"

"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/data"
)

var exCons = exporters.ExporterConstructorFunc(func() exporters.Exporter {
Expand Down
2 changes: 1 addition & 1 deletion conduit/plugins/exporters/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/sirupsen/logrus"

"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/data"
)

// Exporter defines the interface for plugins
Expand Down
5 changes: 2 additions & 3 deletions conduit/plugins/exporters/filewriter/file_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
"gopkg.in/yaml.v3"

"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/data"
"github.com/algorand/indexer/util"
)

const (
Expand Down Expand Up @@ -95,7 +94,7 @@ func (exp *fileExporter) Receive(exportData data.BlockData) error {
}

blockFile := path.Join(exp.cfg.BlocksDir, fmt.Sprintf(exp.cfg.FilenamePattern, exportData.Round()))
err := util.EncodeJSONToFile(blockFile, exportData, true)
err := EncodeJSONToFile(blockFile, exportData, true)
if err != nil {
return fmt.Errorf("Receive(): failed to write file %s: %w", blockFile, err)
}
Expand Down
11 changes: 5 additions & 6 deletions conduit/plugins/exporters/filewriter/file_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/data"
"github.com/algorand/indexer/util"
testutil "github.com/algorand/indexer/util/test"
"github.com/algorand/indexer/conduit/plugins/tools/testutil"

sdk "github.com/algorand/go-algorand-sdk/v2/types"
)
Expand Down Expand Up @@ -176,7 +175,7 @@ func TestExporterReceive(t *testing.T) {
assert.NotContains(t, string(blockBytes), " 0: ")

var blockData data.BlockData
err = util.DecodeJSONFromFile(path, &blockData, true)
err = DecodeJSONFromFile(path, &blockData, true)
require.Equal(t, sdk.Round(i), blockData.BlockHeader.Round)
require.NoError(t, err)
require.NotNil(t, blockData.Certificate)
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestPatternOverride(t *testing.T) {
assert.FileExists(t, path)

var blockData data.BlockData
err := util.DecodeJSONFromFile(path, &blockData, true)
err := DecodeJSONFromFile(path, &blockData, true)
require.Equal(t, sdk.Round(i), blockData.BlockHeader.Round)
require.NoError(t, err)
require.NotNil(t, blockData.Certificate)
Expand All @@ -234,7 +233,7 @@ func TestDropCertificate(t *testing.T) {
path := fmt.Sprintf("%s/%s", tempdir, filename)
assert.FileExists(t, path)
var blockData data.BlockData
err := util.DecodeJSONFromFile(path, &blockData, true)
err := DecodeJSONFromFile(path, &blockData, true)
assert.NoError(t, err)
assert.Nil(t, blockData.Certificate)
}
Expand Down
97 changes: 97 additions & 0 deletions conduit/plugins/exporters/filewriter/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package filewriter

import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

"github.com/algorand/go-codec/codec"

"github.com/algorand/go-algorand-sdk/v2/encoding/json"
)

var prettyHandle *codec.JsonHandle
var jsonStrictHandle *codec.JsonHandle

func init() {
prettyHandle = new(codec.JsonHandle)
prettyHandle.ErrorIfNoField = json.CodecHandle.ErrorIfNoField
prettyHandle.ErrorIfNoArrayExpand = json.CodecHandle.ErrorIfNoArrayExpand
prettyHandle.Canonical = json.CodecHandle.Canonical
prettyHandle.RecursiveEmptyCheck = json.CodecHandle.RecursiveEmptyCheck
prettyHandle.Indent = json.CodecHandle.Indent
prettyHandle.HTMLCharsAsIs = json.CodecHandle.HTMLCharsAsIs
prettyHandle.MapKeyAsString = true
prettyHandle.Indent = 2

jsonStrictHandle = new(codec.JsonHandle)
jsonStrictHandle.ErrorIfNoField = prettyHandle.ErrorIfNoField
jsonStrictHandle.ErrorIfNoArrayExpand = prettyHandle.ErrorIfNoArrayExpand
jsonStrictHandle.Canonical = prettyHandle.Canonical
jsonStrictHandle.RecursiveEmptyCheck = prettyHandle.RecursiveEmptyCheck
jsonStrictHandle.Indent = prettyHandle.Indent
jsonStrictHandle.HTMLCharsAsIs = prettyHandle.HTMLCharsAsIs
jsonStrictHandle.MapKeyAsString = true
}

// EncodeJSONToFile is used to encode an object to a file. If the file ends in .gz it will be gzipped.
func EncodeJSONToFile(filename string, v interface{}, pretty bool) error {
var writer io.Writer

file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("EncodeJSONToFile(): failed to create %s: %w", filename, err)
}
defer file.Close()

if strings.HasSuffix(filename, ".gz") {
gz := gzip.NewWriter(file)
gz.Name = filename
defer gz.Close()
writer = gz
} else {
writer = file
}

var handle *codec.JsonHandle
if pretty {
handle = prettyHandle
} else {
handle = jsonStrictHandle
}
enc := codec.NewEncoder(writer, handle)
return enc.Encode(v)
}

// DecodeJSONFromFile is used to decode a file to an object.
func DecodeJSONFromFile(filename string, v interface{}, strict bool) error {
// Streaming into the decoder was slow.
fileBytes, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("DecodeJSONFromFile(): failed to read %s: %w", filename, err)
}

var reader io.Reader = bytes.NewReader(fileBytes)

if strings.HasSuffix(filename, ".gz") {
gz, err := gzip.NewReader(reader)
defer gz.Close()
if err != nil {
return fmt.Errorf("DecodeJSONFromFile(): failed to make gzip reader: %w", err)
}
reader = gz
}
var handle *codec.JsonHandle
if strict {
handle = json.CodecHandle
} else {
handle = json.LenientCodecHandle
}

enc := codec.NewDecoder(reader, handle)
return enc.Decode(v)
}
63 changes: 63 additions & 0 deletions conduit/plugins/exporters/filewriter/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package filewriter

import (
"io/ioutil"
"path"
"testing"

"github.com/stretchr/testify/require"
)

func TestEncodeToAndFromFile(t *testing.T) {
tempdir := t.TempDir()

type test struct {
One string `json:"one"`
Two int `json:"two"`
Strict map[int]string `json:"strict"`
}
data := test{
One: "one",
Two: 2,
Strict: map[int]string{
0: "int-key",
},
}

{
pretty := path.Join(tempdir, "pretty.json")
err := EncodeJSONToFile(pretty, data, true)
require.NoError(t, err)
require.FileExists(t, pretty)
var testDecode test
err = DecodeJSONFromFile(pretty, &testDecode, false)
require.Equal(t, data, testDecode)

// Check the pretty printing
bytes, err := ioutil.ReadFile(pretty)
require.NoError(t, err)
require.Contains(t, string(bytes), " \"one\": \"one\",\n")
require.Contains(t, string(bytes), `"0": "int-key"`)
}

{
small := path.Join(tempdir, "small.json")
err := EncodeJSONToFile(small, data, false)
require.NoError(t, err)
require.FileExists(t, small)
var testDecode test
err = DecodeJSONFromFile(small, &testDecode, false)
require.Equal(t, data, testDecode)
}

// gzip test
{
small := path.Join(tempdir, "small.json.gz")
err := EncodeJSONToFile(small, data, false)
require.NoError(t, err)
require.FileExists(t, small)
var testDecode test
err = DecodeJSONFromFile(small, &testDecode, false)
require.Equal(t, data, testDecode)
}
}
2 changes: 1 addition & 1 deletion conduit/plugins/exporters/noop/noop_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"gopkg.in/yaml.v3"

"github.com/algorand/indexer/conduit"
"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/data"
)

// PluginName to use when configuring.
Expand Down
4 changes: 2 additions & 2 deletions conduit/plugins/exporters/noop/noop_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"

"github.com/algorand/indexer/conduit/data"
"github.com/algorand/indexer/conduit/plugins"
"github.com/algorand/indexer/conduit/plugins/exporters"
"github.com/algorand/indexer/data"
testutil "github.com/algorand/indexer/util/test"
"github.com/algorand/indexer/conduit/plugins/tools/testutil"

sdk "github.com/algorand/go-algorand-sdk/v2/types"
)
Expand Down
Loading

0 comments on commit bf12a7e

Please sign in to comment.