This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
12 changed files
with
180 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type ReadEvent struct { | ||
Stream string | ||
Cursor string | ||
Amount int | ||
} | ||
|
||
const minTimeuuid = "00000000-0000-1000-8080-808080808080" | ||
|
||
func (re ReadEvent) Run(c Context) error { | ||
persistenceProvider := c.Persistence() | ||
session, err := persistenceProvider.Session() | ||
if err != nil { | ||
return errors.Wrap(err, "Obtaining session failed") | ||
} | ||
if len(re.Cursor) == 0 { | ||
re.Cursor = minTimeuuid | ||
} | ||
events, err := session.ReadEvents(re.Stream, re.Cursor, re.Amount) | ||
for _, ev := range events { | ||
js := string(ev.Blob) | ||
fmt.Printf("%s - %s: {%v} %s\n", ev.Stream, ev.ID, ev.Meta, js) | ||
} | ||
return errors.Wrap(err, fmt.Sprintf("Error reading event from stream %s", re.Stream)) | ||
} |
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,23 @@ | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type WriteEvent struct { | ||
Stream string | ||
Payload []byte | ||
Meta map[string]string | ||
} | ||
|
||
func (we WriteEvent) Run(c Context) error { | ||
persistenceProvider := c.Persistence() | ||
session, err := persistenceProvider.Session() | ||
if err != nil { | ||
return errors.Wrap(err, "Obtaining session failed") | ||
} | ||
we.Meta["version"] = c.Version() | ||
return errors.Wrap(session.WriteEvent(we.Stream, we.Payload, we.Meta), fmt.Sprintf("Error writing event to stream %s", we.Stream)) | ||
} |
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,52 @@ | ||
package actions | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type WriteEventFromStream struct { | ||
Stream string | ||
Input io.Reader | ||
} | ||
|
||
// Read up to Mb from stream | ||
const MAX_READ = 1024 * 1024 | ||
|
||
func readJSONFromStream(input io.Reader) ([]byte, error) { | ||
inputBuf := make([]byte, MAX_READ) | ||
_, err := input.Read(inputBuf) | ||
if err != nil { | ||
return inputBuf, errors.Wrap(err, "Input read failed") | ||
} | ||
for i, v := range inputBuf { | ||
if v == '\x00' { | ||
inputBuf = inputBuf[:i] | ||
break | ||
} | ||
} | ||
var js map[string]interface{} | ||
err = json.Unmarshal(inputBuf, &js) | ||
if err != nil { | ||
return inputBuf, errors.Wrap(err, "Input is not JSON") | ||
} | ||
out, err := json.Marshal(js) | ||
if err != nil { | ||
return inputBuf, errors.Wrap(err, "Marshalling as JSON failed") | ||
} | ||
return out, nil | ||
} | ||
|
||
func (wes WriteEventFromStream) Run(c Context) error { | ||
we := WriteEvent{Stream: wes.Stream, Meta: make(map[string]string)} | ||
we.Meta["origin"] = "stream" | ||
we.Meta["compressed"] = "false" | ||
var err error | ||
we.Payload, err = readJSONFromStream(wes.Input) | ||
if err != nil { | ||
return errors.Wrap(err, "Failed reading input") | ||
} | ||
return errors.Wrap(we.Run(c), "Failed running WriteEvent action") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,33 @@ | ||
package persistence | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gocql/gocql" | ||
) | ||
|
||
type Event struct { | ||
ID string | ||
Stream string | ||
Blob []byte | ||
Meta map[string]string | ||
} | ||
|
||
func eventFromRow(row map[string]interface{}) (Event, error) { | ||
var conv bool | ||
ev := Event{} | ||
id, conv := row["id"].(gocql.UUID) | ||
if !conv { | ||
return ev, fmt.Errorf("Failed converting %v to UUID", row["id"]) | ||
} | ||
ev.ID = id.String() | ||
ev.Blob, conv = row["blob"].([]byte) | ||
if !conv { | ||
return ev, fmt.Errorf("Failed converting %v to blob", row["blob"]) | ||
} | ||
ev.Meta, conv = row["meta"].(map[string]string) | ||
if !conv { | ||
return ev, fmt.Errorf("Failed converting %v to map", row["map"]) | ||
} | ||
return ev, nil | ||
} |
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
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