-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
328 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
var EOF = io.EOF | ||
|
||
// StateReader interface placeholder | ||
type StateReader interface { | ||
GetSequence() uint32 | ||
// Read should return next ledger entry. If there are no more | ||
// entries it should return `io.EOF` error. | ||
Read() (xdr.LedgerEntry, error) | ||
} | ||
|
||
// StateWriteCloser interface placeholder | ||
type StateWriteCloser interface { | ||
Write(xdr.LedgerEntry) error | ||
// Close should be called when there are no more entries | ||
// to write. | ||
Close() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,42 @@ | ||
package pipeline | ||
|
||
import ( | ||
"io" | ||
"github.com/stellar/go/xdr" | ||
"github.com/stellar/go/ingest/io" | ||
) | ||
|
||
const bufferSize = 4 | ||
const bufferSize = 2000 | ||
|
||
func (b *BufferedStateReadWriteCloser) init() { | ||
b.buffer = make(chan byte, bufferSize) | ||
b.closed = make(chan bool) | ||
func (b *bufferedStateReadWriteCloser) init() { | ||
b.buffer = make(chan xdr.LedgerEntry, bufferSize) | ||
} | ||
|
||
func (b *BufferedStateReadWriteCloser) Read(p []byte) (n int, err error) { | ||
b.initOnce.Do(b.init) | ||
func (b *bufferedStateReadWriteCloser) GetSequence() uint32 { | ||
return 0 | ||
} | ||
|
||
// This is to make sure to drain channel first if b.closed is ready. | ||
// TODO move `case` contents to another method | ||
select { | ||
case rb := <-b.buffer: | ||
p[0] = rb | ||
return 1, nil | ||
default: | ||
} | ||
func (b *bufferedStateReadWriteCloser) Read() (xdr.LedgerEntry, error) { | ||
b.initOnce.Do(b.init) | ||
|
||
select { | ||
case rb := <-b.buffer: | ||
p[0] = rb | ||
return 1, nil | ||
case <-b.closed: | ||
return 0, io.EOF | ||
entry, more := <-b.buffer | ||
if more { | ||
return entry, nil | ||
} else { | ||
return xdr.LedgerEntry{}, io.EOF | ||
} | ||
} | ||
|
||
func (b *BufferedStateReadWriteCloser) Write(p []byte) (n int, err error) { | ||
func (b *bufferedStateReadWriteCloser) Write(entry xdr.LedgerEntry) error { | ||
b.initOnce.Do(b.init) | ||
b.buffer <- p[0] | ||
return 1, nil | ||
b.buffer <- entry | ||
return nil | ||
} | ||
|
||
func (b *BufferedStateReadWriteCloser) Close() error { | ||
func (b *bufferedStateReadWriteCloser) Close() error { | ||
b.initOnce.Do(b.init) | ||
b.closed <- true | ||
close(b.closed) | ||
close(b.buffer) | ||
return nil | ||
} | ||
|
||
func (b *BufferedStateReadWriteCloser) WriteCloseString(s string) { | ||
b.initOnce.Do(b.init) | ||
|
||
for _, rb := range s { | ||
_, err := b.Write([]byte{byte(rb)}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
err := b.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
var _ io.StateReader = &bufferedStateReadWriteCloser{} | ||
var _ io.StateWriteCloser = &bufferedStateReadWriteCloser{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.