Skip to content

ReQL proposal: blob type #2612

Closed
Closed

Description

@coffeemug

This proposal covers the binary data part of #137.

A blob is a non-streaming, reasonably small (< ~10MB), base64 encoded binary data type. The user can store a blob in a document.

  • For now, any time the document gets loaded, the blob gets loaded (this might change if we allow not loading fields if the user plucks them out). EDIT: It'd be really nice to optimize that right away, but I don't know how hard that is.
  • Any time the document gets copied, the blob gets copied.
  • Hopefully if only non-blob fields in a document get updated, the blob doesn't get rewritten.
  • For now, the only way to modify a blob is to rewrite it entirely.
> r.expr({ 'binary': Uint8Array(...) }).run(conn)
{ 'binary': Uint8Array(...) }

> r.expr({ 'binary': Uint8Array(...) }).run(conn, { blobFormat: 'native' })
{ 'binary': Uint8Array(...) }

> r.expr({ 'binary': Uint8Array(...) }).run(conn, { blobFormat: 'raw' })
{ 'binary': {"$reql_type$":"BLOB", data: "..."}

I propose not including any of the following in v1:

  • Reading part of a blob
  • Modifying part of a blob without rewriting the whole thing
  • Appending to a blob without rewriting the whole thing

Activity

  1. added this to the 1.14 milestone on Jun 26, 2014
  2. mlucy commented on Jun 26, 2014

    @mlucy
    Member

    I agree with this interface (although I might call the pseudotype "BINARY" or "BASE64" instead of "BLOB").

    One question with this is what native types should be turned into binary pseudotypes. In Ruby, people generally just put binary data in strings (there's no separate type for it, although strings can have encodings).

    If we go with turning binary strings in Ruby into binary pseudotypes (and vice-versa), we might also want to offer an r.binary(...) command to turn a non-binary string into a blob (e.g. r.binary("abc")).

  3. coffeemug commented on Jun 26, 2014

    @coffeemug
    ContributorAuthor

    One question with this is what native types should be turned into binary pseudotypes.

    Embarrasingly, I don't know either of the three driver languages well enough to know this. @neumino @deontologician @larkost @Tryneus can speak about their respective languages.

    we might also want to offer an r.binary(...) command

    👍

  4. srh commented on Jun 26, 2014

    @srh
    Contributor

    we might also want to offer an r.binary(...) command to turn a non-binary string into a blob (e.g. r.binary("abc")

    How would it do that?

  5. larkost commented on Jun 26, 2014

    @larkost
    Collaborator

    Python3 has a binary type (type(b'23') is <class 'bytes'>), but while Python2.x technically supports the b'23' format, it converts it into a string immediately. So such a thing would generally be needed for Python as well. There is a valid question about whether we should implicitly convert <class 'bytes'> objects, and what we should present on fetch.

    The argument against natively supporting <class 'bytes'> is that some surprising (but correct) places return bytes objects in Python3 such as subprocess STDERR and STDOUT. If we natively bring those in as binary types then the same code in Python2.x would put it into the database as a binary type, while in Python3.x it would insert a binary.

    The same arguments hold for what we present when fetching. In order to be consistent I think we should present r.binary objects rather than native items in Python3. One good option would be to have the r.binary class be a subclass of bytes (this maps to `<type 'str'> on Python2.x), which would "do the right thing" in most cases.

  6. neumino commented on Jun 26, 2014

    @neumino
    Member

    Node has a buffer class, that should work fine.

    The dataexplorer will probably drop the blobs and replace them with a string <blob> or something like that.

  7. larkost commented on Jun 26, 2014

    @larkost
    Collaborator

    @srh For Python2.x we are pretty much stuck converting non-binary objects since even a read on a file in binary mode results in a plain string object. Probably the best approach would be to use the same format at the bytes instantiator uses in Python3.x:

    class bytes(object)
     |  bytes(iterable_of_ints) -> bytes
     |  bytes(string, encoding[, errors]) -> bytes
     |  bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
     |  bytes(int) -> bytes object of size given by the parameter initialized with null bytes
     |  bytes() -> empty bytes object
    

    Just subclassing the bytes object gets us this already on Python3. However, on Python2 bytes is just an alias to str, and so there we probably would have to call the encode methods ourselves.

  8. deontologician commented on Jun 26, 2014

    @deontologician
    Contributor

    Python2 str is the same as Python3 bytes. Neither holds any encoding information. The bytes() function and b"123" syntax are just dummy operations to make migrating to Python3 easier. I'd say only those two respective types should be base64'd in Python. Implicitly converting unicode strings to bytes is probably a bad idea since the user doesn't get to pick the encoding, and when you retrieve it again you need to know that to decode it.

  9. mlucy commented on Jun 26, 2014

    @mlucy
    Member

    How would it do that?

    r.binary would be a term on the server that takes a string and returns a binary pseudotype. (If passed a binary pseudotype, it just returns what it was passed.)

    I think we should try hard to turn binary pseudotypes into a reasonable native type in the clients. In Ruby, if I have x = "a\0b".encode('binary'), I really want r(x).run to give me back that same string with a binary encoding.

  10. mlucy commented on Jun 26, 2014

    @mlucy
    Member

    @deontologician -- that makes sense. Converting str in Python 2 and bytes in Python 3 to binary pseudotypes automatically seems right. (And turning them back, obviously.)

  11. AtnNn commented on Jun 26, 2014

    @AtnNn
    Member

    Converting str in Python 2 to binary pseudotypes

    I would love to do this, but it is a bad idea. It is a big break from the current behaviour, and will break in unexpected ways when the rethinkdb driver is used alongside other libraries that return str instead of unicode values.

  12. deontologician commented on Jun 26, 2014

    @deontologician
    Contributor

    @AtnNn Ah ok. I think I see the issue, I misunderstood that we were talking about what to coerce to a blob without the r.binary() wrapper. I agree that you can only really do that in Python3, since python2 strings are just overloaded with no way to discern the meaning.

    I was talking more about r.binary() not coercing a unicode string to a byte array.

  13. mlucy commented on Jun 26, 2014

    @mlucy
    Member

    So you think we should automatically convert in Python 3, but not automatically convert anything in Python 2?

    I sort of feel like r.binary should be able to turn any string into a binary blob. That feels like a common task. But if that seems crazy, I'll defer to the people who actually use Python regularly.

  14. AtnNn commented on Jun 26, 2014

    @AtnNn
    Member

    In Python 2, strings are bytes by default. I think r.binary should be required to convert them to blobs.

    In Python 3, strings are unicode by default. I think r.binary should be a no-op on bytes and should fail on unicode strings.

  15. 33 remaining items

  16. srh commented on Jul 7, 2014

    @srh
    Contributor

    Result sets are not ordered.

  17. coffeemug commented on Jul 8, 2014

    @coffeemug
    ContributorAuthor

    Marking as settled. To clarify:

    • We'll use count to get the number of bytes and not add a length command
    • We'll call the type binary and the command r.binary
    • We'll accept both RFC 3548 and RFC 2045, and send back the common subset
    • We'll define count, info, type_of, coerce_to and the comparison operators to start with
  18. self-assigned this
    on Jul 9, 2014
  19. hardkrash commented on Jul 11, 2014

    @hardkrash

    Please contemplate on how you would retrieve documents without the binary components. That is how would someone perform a query and get all the documents without the binary elements.

    Pretend that I have a document with a handful of random metadata entries and 1 to 3 binary entries. for speed reasons I want to look at all the meta data and my binary elements are 20MiB images. clearly if I have 100k images I may want to look at all the metadata without the large images. Excluding a key in the document may not be enough. Those images might have different keys in each document. Clearly in that scenario exclusion via type would be useful.

  20. coffeemug commented on Jul 11, 2014

    @coffeemug
    ContributorAuthor

    You'd say r.table('foo').without('blob_field_name').

  21. mlucy commented on Jul 23, 2014

    @mlucy
    Member

    I'd like to propose that we re-introduce slice since @Tryneus already implemented it in the CR . The use, meaning, and edge cases should be the same as array.slice.

  22. mlucy commented on Jul 23, 2014

    @mlucy
    Member

    (From an administrative perspective, this issue is temporarily back in its discussion period for that one aspect.)

  23. mlucy commented on Jul 24, 2014

    @mlucy
    Member

    @coffeemug: any opinion on slice?

  24. coffeemug commented on Jul 24, 2014

    @coffeemug
    ContributorAuthor

    Adding slice would be great. I talked to @Tryneus and I thought he already implemented it, but I'm not entirely sure.

  25. mlucy commented on Jul 25, 2014

    @mlucy
    Member

    Alright, it's been 2-3 days with no objections and the code's already written, marking as settled once more.

  26. Tryneus commented on Jul 25, 2014

    @Tryneus
    Member

    Ok, r.binary has been approved and merged to next in commits b878582, d7730a5, b0285bc, a0f0973, and 9f7247b. Review was 1799. Will be in release 1.14.

    A few notes about the final state of the feature:

    • base64 is only used on the wire; as soon as it is converted to a datum_t, the base64 is translated to a wire_string_t. The reverse applies when sending r.binary to a client.
    • We support the common subset of RFC 3548 and RFC 2045, but not base64url.
    • The r.binary term is client-only. There is no use case I could find for running r.binary on existing data that is not fulfilled by using coerce_to('binary').
    • count works on r.binary objects, similar to arrays.
    • slice works on r.binary objects, with the same semantics as when used on an array.
    • info, type_of, and coerce_to are all implemented (the type string is BINARY).

    Drivers:

    • The python driver will implicitly convert bytes to r.binary unless bytes is the same as str (in python2).
    • The python driver will return r.binary objects as a thin wrapper of bytes (to allow implicit conversion when re-inserting in python2).
    • The javascript driver will implicitly convert Buffer to r.binary.
    • The javascript driver will return r.binary objects as Buffers.
    • The ruby driver does not implicitly convert data to r.binary, like python2.
    • The ruby driver will return r.binary objects as a thin wrapper of String (to allow implicit conversion when re-inserting).
  27. coffeemug commented on Jul 25, 2014

    @coffeemug
    ContributorAuthor

    A few more questions.

    • Can I pass binary_format='raw' to get the pseudotype in the driver? (if not, I think we should do that, both for clarity and consistency).
    • How is the wrapper r.binary object specified? What methods can I call on it/what can I do with it, etc.?
  28. Tryneus commented on Jul 25, 2014

    @Tryneus
    Member

    @coffeemug:

    • I did not implement binary_format='raw' (I didn't see much of a use case), but that's a simple change - opened Add binary_format='raw in the drivers. #2762 for this.
    • The thin wrappers in Ruby and Python simply inherit from String and bytes, respectively, without redefining anything. They are functionally identical to a normal string, but we can tell if it was the result of r.binary in an earlier query.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions