diff --git a/cmd/main/main.go b/cmd/main/main.go index 27bcd5e..8da8015 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -19,6 +19,7 @@ func main() { } channel := sonic.IngesterChannel{Connection: connection} - e = channel.Push("test", "default", "captain", "lol is truth") - fmt.Println(e) + c, e := channel.Count("test", "default", "captain") + + fmt.Println(c, e) } diff --git a/sonic/ingester.go b/sonic/ingester.go index 7c20d29..be9d5cc 100644 --- a/sonic/ingester.go +++ b/sonic/ingester.go @@ -1,6 +1,10 @@ package sonic -import "fmt" +import ( + "fmt" + "strconv" + "strings" +) type Ingestable interface { Push(collection, bucket, object, text string) (err error) @@ -42,11 +46,42 @@ func (i IngesterChannel) Push(collection, bucket, object, text string) (err erro } func (i IngesterChannel) Pop(collection, bucket, object, text string) (err error) { - panic("implement me") + err = i.write(fmt.Sprintf("%s %s %s %s \"%s\"", pop, collection, bucket, object, text)) + if err != nil { + return err + } + + // sonic should sent OK + _, err = i.read() + if err != nil { + return err + } + return nil } -func (i IngesterChannel) Count(collection, bucket, object string) (count int, err error) { - panic("implement me") +func (i IngesterChannel) Count(collection, bucket, object string) (cnt int, err error) { + err = i.write(fmt.Sprintf("%s %s %s", count, collection, buildCountQuery(bucket, object))) + if err != nil { + return 0, err + } + + // RESULT NUMBER + r, err := i.read() + if err != nil { + return 0, err + } + return strconv.Atoi(r[7:]) +} + +func buildCountQuery(bucket, object string) string { + builder := strings.Builder{} + if bucket != "" { + builder.WriteString(bucket) + if object != "" { + builder.WriteString(" " + object) + } + } + return builder.String() } func (i IngesterChannel) FlushCollection(collection string) (err error) {