Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: ReadBytes #110

Open
Allendar opened this issue Jul 1, 2016 · 1 comment
Open

Proposal: ReadBytes #110

Allendar opened this issue Jul 1, 2016 · 1 comment

Comments

@Allendar
Copy link

Allendar commented Jul 1, 2016

Because I use github.com/buger/jsonparser, which can search through JSON bytes without memory-overhead I had to make an extra method to just get the bytes back. I was thinking if it might be suitable for the tiedot package itself too. Maybe jsonparser can also be used for lookup stuff under the hood to speed things up even more?

This is the method :

// Find and retrieve bytes of a document by ID.
func (col *Col) ReadBytes(id int) (docB []byte, err error) {
    col.db.schemaLock.RLock()
    part := col.parts[id%col.db.numParts]
    part.Lock.RLock()
    docB, err = part.Read(id)
    part.Lock.RUnlock()
    col.db.schemaLock.RUnlock()
    return
}

It gave me a 1.6x speed boost when just wanting to lookup one field from the result-set (eg password), which was really significant in my API, easily upping requests p/second by hundreds.

Would love to hear your feedback.

Example loop comparison:

k := ""

for id := range queryResult {
    // user, err := App.Cols["Users"].Read(id)
    // if nil != err {
    //  panic(err)
    // }
    //
    // k = user[DBFieldUserPassword].(string)

    user, err := App.Cols["Users"].ReadBytes(id)
    if nil != err {
        panic(err)
    }
    k, err = jsonparser.GetUnsafeString(user, DBFieldUserPassword)
    if nil != err {
        panic(err)
    }
}
@HouzuoGuo
Copy link
Owner

That's a very good idea. Would you be interested to write a unit test for it as well please? Otherwise I'll perhaps do it in another week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants