timshannon / badgerhold

BadgerHold is an embeddable NoSQL store for querying Go types built on Badger
MIT License
520 stars 51 forks source link

Triplestore structure (querying sub-properties) #16

Closed davedbase closed 4 years ago

davedbase commented 4 years ago

I was wondering if a structure like this would be possible or if you someone suggest a better structure:

type Property struct {
    Value     interface{}
    Reference uuid.UUID
}
type Triple struct {
    Subject Property
    Predicate string
    Object Property
}
docID := Property{ Reference: uuid.v4() }
err = store.Insert(badgerhold.NextSequence(), &Triple{
    Subject: docID,
    Predicate: "Document",
    Object: Property{Value: "Blog Post"},
})

Basically with this layout I'd have to do the following:

store.Find(&results, badgerhold.Where("Predicate").Eq("Document").And("Object.Reference").Eq(uuid)
for _, doc := range results {
    fmt.Println(doc.O)
}

The reason for this structure is that my Subject and Object properties can actually be references to other records. Perhaps it's best to keep Subject and Object as interfaces{} and use a MatchFunc to check the type is a UUID?

timshannon commented 4 years ago

Yep, you can query nested fields like that.

https://github.com/timshannon/badgerhold/blob/master/query.go#L251

It basically splits the field name on . and loops to get nested field values. Is that not working for you?

davedbase commented 4 years ago

Thanks Tim. Once I get home I'll have a better look at it but when I originally tested it, the period split didn't work. I'll confirm and supply a testable case.

Will the field split if one of my fields is map[string]interface{}? ie:

type Document struct {
    Type       string
    Title      string
    Content    string
    Excerpt    string
    Reference  string
    Status     string
    Metadata map[string]interface{}
    Created    time.Time
    Updated    time.Time
}

Judging by the fieldValue method, looks like it's only reflecting on the struct fields and not maps.

Just sorting out a couple different methods to work with BadgerHold. I'd like to use the Triplestore method but I'm also sensitive to performance.

I was going to implement a document store and a triplestore to see how effective either is for my use case. I'm assuming the document store would be faster.

timshannon commented 4 years ago

Yep, currently it only works with Stuct fields.

davedbase commented 4 years ago

Disregard, I had a bug in my logic. The dot notation for properties seems to be working fine. :-)