From 74ffcfe6b2c80b7cf459798dc42bd278075ccb50 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Thu, 29 Dec 2022 22:32:16 -0500 Subject: experiment with write JSON to database and munge it for collections --- models/activity_streams_object.go | 19 ++++++++++--------- models/persister.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'models') diff --git a/models/activity_streams_object.go b/models/activity_streams_object.go index 85a7d09..98c69bb 100644 --- a/models/activity_streams_object.go +++ b/models/activity_streams_object.go @@ -1,6 +1,8 @@ package models import ( + "bytes" + "encoding/json" "fmt" "time" @@ -37,17 +39,16 @@ func (a *ActivityStreamsObject) Keybase() string { } func (a *ActivityStreamsObject) content() []byte { - var contentVal string - if a.wrappedObject.GetActivityStreamsContent() != nil { - content := a.wrappedObject.GetActivityStreamsContent() - for contentIter := content.Begin(); contentIter != content.End(); contentIter = contentIter.Next() { - contentVal = contentIter.GetXMLSchemaString() - } - } - return []byte(contentVal) + objMap, _ := a.wrappedObject.Serialize() + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + encoder.Encode(objMap) + return bytes.TrimRight(buffer.Bytes(), "\n") } func (a *ActivityStreamsObject) Save(txn *badger.Txn) error { - e := badger.NewEntry(a.keyName(), a.content()) + content := a.content() + e := badger.NewEntry(a.keyName(), content) return txn.SetEntry(e) } diff --git a/models/persister.go b/models/persister.go index 71cddc8..3729c81 100644 --- a/models/persister.go +++ b/models/persister.go @@ -35,3 +35,23 @@ func (p *Persister) Count(model model) (int, error) { }) return count, err } + +func (p *Persister) Collect(model model) ([]string, error) { + var result []string + err := p.db.View(func(txn *badger.Txn) error { + opts := badger.DefaultIteratorOptions + opts.PrefetchValues = false // TODO Maybe we want true here + it := txn.NewIterator(opts) + defer it.Close() + prefix := []byte(model.Keybase()) + for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { + item := it.Item() + item.Value(func(v []byte) error { + result = append(result, string(v)) + return nil + }) + } + return nil + }) + return result, err +} -- cgit v1.2.3