Compare commits
12 Commits
Author | SHA1 | Date |
---|---|---|
|
8cc8e6a464 | 2 years ago |
|
e398bcb7b0 | 2 years ago |
|
96b44a3df9 | 3 years ago |
|
336e7e14d3 | 3 years ago |
|
73ba8fc3b8 | 3 years ago |
|
d25f03e80c | 3 years ago |
|
3c12794d9c | 3 years ago |
|
d516f0aaae | 3 years ago |
|
3e6855d429 | 3 years ago |
|
c609ca7b9a | 3 years ago |
|
573ef316b4 | 3 years ago |
|
adbb5dbc43 | 3 years ago |
@ -0,0 +1,7 @@
|
|||||||
|
pipeline:
|
||||||
|
build:
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- go test ./...
|
||||||
|
environment:
|
||||||
|
- GOPRIVATE=git.sg.caj.me/caj
|
@ -1 +1,7 @@
|
|||||||
package dataswamp
|
package dataswamp
|
||||||
|
|
||||||
|
import (
|
||||||
|
// "caj-larsson/bog/dataswamp/namespace"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AdminService struct{}
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package dataswamp
|
|
||||||
|
|
||||||
type Logger interface {
|
|
||||||
Debug(format string, a ...interface{})
|
|
||||||
Info(format string, a ...interface{})
|
|
||||||
Warn(format string, a ...interface{})
|
|
||||||
}
|
|
@ -0,0 +1,111 @@
|
|||||||
|
package namespace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"caj-larsson/bog/util"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Clock interface {
|
||||||
|
Now() time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type NamespaceService struct {
|
||||||
|
repo Repository
|
||||||
|
outboxes []func(util.Event)
|
||||||
|
logger util.Logger
|
||||||
|
clock Clock
|
||||||
|
default_ttl time.Duration
|
||||||
|
default_quota_bytes int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNamespaceService(repo Repository, logger util.Logger, clock Clock, default_ttl time.Duration, default_quota_bytes int64) *NamespaceService {
|
||||||
|
return &NamespaceService{
|
||||||
|
repo,
|
||||||
|
nil,
|
||||||
|
logger,
|
||||||
|
clock,
|
||||||
|
default_ttl,
|
||||||
|
default_quota_bytes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NamespaceService) GetOrCreateNs(name string) *Namespace {
|
||||||
|
ns, err := s.repo.GetByName(name)
|
||||||
|
|
||||||
|
if err == ErrNotExists {
|
||||||
|
new_ns := Namespace{
|
||||||
|
0,
|
||||||
|
name,
|
||||||
|
s.clock.Now(),
|
||||||
|
s.default_ttl,
|
||||||
|
FileSizeQuota{s.default_quota_bytes, 0},
|
||||||
|
FileStat{0, 0},
|
||||||
|
FileStat{0, 0},
|
||||||
|
}
|
||||||
|
created_ns, err := s.repo.Create(new_ns)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return created_ns
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return ns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NamespaceService) Wire(reg func(string, util.EventHandler), outbox func(ev util.Event)) {
|
||||||
|
reg("FileUsed", s.handleFileUsed)
|
||||||
|
s.outboxes = append(s.outboxes, outbox)
|
||||||
|
|
||||||
|
reg("FileUsed", s.handleFileUsed)
|
||||||
|
reg("FileDeleted", s.handleFileDeleted)
|
||||||
|
reg("FileRecieved", s.handleFileRecieved)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NamespaceService) All() []Namespace {
|
||||||
|
nss, err := s.repo.All()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return nss
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NamespaceService) handleFileUsed(payload interface{}) {
|
||||||
|
var payload_s = payload.(struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
})
|
||||||
|
fmt.Printf("file used %v\n", payload_s)
|
||||||
|
ns := s.GetOrCreateNs(payload_s.Name)
|
||||||
|
ns.FileQuota = ns.FileQuota.Add(payload_s.Size)
|
||||||
|
s.repo.Update(ns.ID, *ns)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NamespaceService) handleFileDeleted(payload interface{}) {
|
||||||
|
var payload_s = payload.(struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
})
|
||||||
|
fmt.Printf("file deleted %v\n", payload_s)
|
||||||
|
ns := s.GetOrCreateNs(payload_s.Name)
|
||||||
|
ns.FileQuota = ns.FileQuota.Add(-payload_s.Size)
|
||||||
|
fmt.Printf("file usage %v\n", ns.FileQuota)
|
||||||
|
s.repo.Update(ns.ID, *ns)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NamespaceService) handleFileRecieved(payload interface{}) {
|
||||||
|
var payload_s = payload.(struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
})
|
||||||
|
fmt.Printf("file recieved %v\n", payload_s)
|
||||||
|
ns := s.GetOrCreateNs(payload_s.Name)
|
||||||
|
ns.FileQuota = ns.FileQuota.Add(payload_s.Size)
|
||||||
|
fmt.Printf("file usage %v\n", ns.FileQuota)
|
||||||
|
s.repo.Update(ns.ID, *ns)
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package namespace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"caj-larsson/bog/util"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEventTest(t *testing.T) {
|
||||||
|
eb := util.NewEventBus()
|
||||||
|
svc := NamespaceService{}
|
||||||
|
|
||||||
|
svc.Wire(eb.Register, eb.Handle)
|
||||||
|
|
||||||
|
events := []util.Event{
|
||||||
|
*util.NewEvent("FileUsed", struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
}{
|
||||||
|
"asd",
|
||||||
|
int64(12),
|
||||||
|
}),
|
||||||
|
*util.NewEvent("FileDeleted", struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
}{
|
||||||
|
"asd",
|
||||||
|
int64(12),
|
||||||
|
}),
|
||||||
|
*util.NewEvent("FileRecieved", struct {
|
||||||
|
Name string
|
||||||
|
Size int64
|
||||||
|
}{
|
||||||
|
"asd",
|
||||||
|
int64(12),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
util.AcceptsMessage(t, eb, events)
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
@ -0,0 +1,11 @@
|
|||||||
|
package system_time
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Clock struct{}
|
||||||
|
|
||||||
|
func (c Clock) Now() time.Time {
|
||||||
|
return time.Now()
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
eventName string
|
||||||
|
payload interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEvent(eventName string, payload interface{}) *Event {
|
||||||
|
return &Event{
|
||||||
|
eventName,
|
||||||
|
payload,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Event) EventName() string {
|
||||||
|
return e.eventName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Event) Payload() interface{} {
|
||||||
|
return e.payload
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventHandler func(payload interface{})
|
||||||
|
|
||||||
|
type EventBus struct {
|
||||||
|
handlers map[string][]EventHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEventBus() *EventBus {
|
||||||
|
return &EventBus{make(map[string][]EventHandler)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (eb *EventBus) Register(eventName string, handler EventHandler) {
|
||||||
|
eb.handlers[eventName] = append(eb.handlers[eventName], handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (eb *EventBus) Handle(e Event) {
|
||||||
|
for _, handler := range eb.handlers[e.EventName()] {
|
||||||
|
handler(e.Payload())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/matryer/is"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (eb *EventBus) Handled(e Event) bool {
|
||||||
|
// TODO: figure out how to verify the event signature here.
|
||||||
|
handlers, exists := eb.handlers[e.EventName()]
|
||||||
|
if !exists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(handlers) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func AcceptsMessage(t *testing.T, eb *EventBus, es []Event) {
|
||||||
|
is := is.New(t)
|
||||||
|
for _, e := range es {
|
||||||
|
is.True(eb.Handled(e))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Debug(format string, a ...interface{})
|
||||||
|
Info(format string, a ...interface{})
|
||||||
|
Warn(format string, a ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestLogger struct{}
|
||||||
|
|
||||||
|
func (t TestLogger) Debug(format string, a ...interface{}) {}
|
||||||
|
func (t TestLogger) Info(format string, a ...interface{}) {}
|
||||||
|
func (t TestLogger) Warn(format string, a ...interface{}) {}
|
Loading…
Reference in New Issue