-
Notifications
You must be signed in to change notification settings - Fork 3
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
47 changed files
with
4,616 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/nrc-no/core/pkg/server/data" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// serveDataCmd represents the data command | ||
var serveDataCmd = &cobra.Command{ | ||
Use: "data", | ||
Short: "starts the data server", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := serveDb(ctx, | ||
data.Options{ | ||
ServerOptions: coreOptions.Serve.Login, | ||
}); err != nil { | ||
return err | ||
} | ||
<-doneSignal | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
serveCmd.AddCommand(serveDataCmd) | ||
} | ||
|
||
func serveDb(ctx context.Context, options data.Options) error { | ||
server, err := data.NewServer(options) | ||
if err != nil { | ||
return err | ||
} | ||
server.Start(ctx) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Changes struct { | ||
Items []ChangeItem `json:"items"` | ||
} | ||
|
||
func (c Changes) String() string { | ||
jsonBytes, err := json.Marshal(c) | ||
if err != nil { | ||
return fmt.Sprintf("%v", err) | ||
} | ||
return string(jsonBytes) | ||
} | ||
|
||
type ChangeItem struct { | ||
Sequence int64 `json:"sequence"` | ||
TableName string `json:"table_name"` | ||
RecordID string `json:"record_id"` | ||
RecordRevision Revision `json:"record_revision"` | ||
} | ||
|
||
func (c ChangeItem) String() string { | ||
jsonBytes, err := json.Marshal(c) | ||
if err != nil { | ||
return fmt.Sprintf("%v", err) | ||
} | ||
return string(jsonBytes) | ||
} |
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,13 @@ | ||
package api | ||
|
||
const ( | ||
KeyRecordID = "_id" | ||
KeyRevision = "_rev" | ||
KeyPrevision = "_prev" | ||
KeyDeleted = "_deleted" | ||
ChangeStreamTableName = "_changes" | ||
KeyCSSequence = "seq" | ||
KeyCSTableName = "table_name" | ||
KeyCSRecordID = "record_id" | ||
KeyCSRecordRevision = "record_rev" | ||
) |
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,102 @@ | ||
package api | ||
|
||
type Error struct { | ||
Message string | ||
Code ErrorCode | ||
} | ||
|
||
type ErrorCode uint8 | ||
|
||
const ( | ||
ErrCodeInvalidRevision ErrorCode = iota | ||
ErrCodeInvalidPrevision | ||
ErrCodeInvalidRecordID | ||
ErrCodeDuplicateField | ||
ErrCodeMissingRevision | ||
ErrCodeInvalidTable | ||
ErrCodeInvalidColumnType | ||
ErrCodeEmptyTableColumns | ||
ErrCodeDuplicateColumnName | ||
ErrCodeInvalidColumnName | ||
ErrCodeRecordNotFound | ||
ErrCodeFieldNotFound | ||
ErrCodeInternalError | ||
ErrCodeTableAlreadyExists | ||
ErrCodeUnsupportedDialect | ||
ErrCodeInvalidTimestamp | ||
ErrCodeMissingId | ||
) | ||
|
||
func (e *Error) Error() string { | ||
return e.Message | ||
} | ||
|
||
func (e *Error) ErrorCode() ErrorCode { | ||
return e.Code | ||
} | ||
|
||
func (e *Error) Is(other error) bool { | ||
if other == nil { | ||
return false | ||
} | ||
if e == other { | ||
return true | ||
} | ||
if o, ok := other.(*Error); ok { | ||
return e.Code == o.Code | ||
} | ||
return false | ||
} | ||
|
||
func IsError(err error, code ErrorCode) bool { | ||
if err == nil { | ||
return false | ||
} | ||
if e, ok := err.(*Error); ok { | ||
return e.Code == code | ||
} | ||
return false | ||
} | ||
|
||
func NewError(code ErrorCode, message string) *Error { | ||
return &Error{ | ||
Message: message, | ||
Code: code, | ||
} | ||
} | ||
|
||
var ( | ||
ErrInvalidRevision = NewError(ErrCodeInvalidRevision, "invalid revision") | ||
ErrInvalidPrevision = NewError(ErrCodeInvalidPrevision, "invalid previous revision") | ||
ErrInvalidRecordID = NewError(ErrCodeInvalidRecordID, "invalid record id") | ||
ErrDuplicateField = NewError(ErrCodeDuplicateField, "duplicate field") | ||
ErrMissingRevision = NewError(ErrCodeMissingRevision, "missing revision") | ||
ErrInvalidTableName = NewError(ErrCodeInvalidTable, "invalid table name") | ||
ErrEmptyColumns = NewError(ErrCodeEmptyTableColumns, "empty columns") | ||
ErrDuplicateColumnName = NewError(ErrCodeDuplicateColumnName, "duplicate column name") | ||
ErrInvalidColumnName = NewError(ErrCodeInvalidColumnName, "invalid column name") | ||
ErrRecordNotFound = NewError(ErrCodeRecordNotFound, "record not found") | ||
ErrFieldNotFound = NewError(ErrCodeFieldNotFound, "field not found") | ||
ErrInvalidColumnType = NewError(ErrCodeInvalidColumnType, "invalid column type") | ||
ErrTableAlreadyExists = NewError(ErrCodeTableAlreadyExists, "table already exists") | ||
ErrUnsupportedDialect = NewError(ErrCodeUnsupportedDialect, "unsupported dialect") | ||
ErrInvalidValueType = NewError(ErrCodeInternalError, "invalid value type") | ||
ErrInvalidTimestamp = NewError(ErrCodeInvalidTimestamp, "invalid timestamp") | ||
ErrMissingId = NewError(ErrCodeMissingId, "missing id") | ||
) | ||
|
||
func NewDuplicateColumnNameErr(name string) *Error { | ||
return NewError(ErrCodeDuplicateColumnName, "duplicate column name: "+name) | ||
} | ||
|
||
func NewInvalidColumnNameErr(name string) *Error { | ||
return NewError(ErrCodeInvalidColumnName, "invalid column name: "+name) | ||
} | ||
|
||
func NewInvalidColumnTypeErr(name string) *Error { | ||
return NewError(ErrCodeInvalidColumnType, "invalid column type: "+name) | ||
} | ||
|
||
func NewTableAlreadyExistsErr(name string) *Error { | ||
return NewError(ErrCodeTableAlreadyExists, "table already exists: "+name) | ||
} |
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,13 @@ | ||
package api | ||
|
||
type PutRecordOptions struct { | ||
IsNew bool | ||
} | ||
|
||
type PutRecordOption func(o *PutRecordOptions) | ||
|
||
var IsNew = func(isNew bool) PutRecordOption { | ||
return func(o *PutRecordOptions) { | ||
o.IsNew = isNew | ||
} | ||
} |
Oops, something went wrong.