Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to skip chunks using QDataStream?
Forum Updated to NodeBB v4.3 + New Features

How to skip chunks using QDataStream?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 3.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Crag_Hack
    wrote on last edited by JKSH
    #1

    [Forked from https://forum.qt.io/topic/117372/best-way-to-compress-data-to-append-to-a-file/ --JKSH]

    Thanks guys I have a followup question - is there any way to skip a QByteArray chunk when reading and parsing the QFile containing the compressed QByteArrays with a QDataStream? I know how many chunks to skip and only need to read in one chunk at a time into memory.

    Pablo J. RoginaP 1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/qdatastream.html#skipRawData

      First, read 4 bytes of the file as int32_t. This value is the number of bytes occupied by the next byte array (and therefore it tells you how many bytes to skip). Repeat as many times as necessary.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      6
      • C Offline
        C Offline
        Crag_Hack
        wrote on last edited by
        #3

        How do I read 4 bytes as int32_t? QDataStream readRawData?

        JKSHJ 1 Reply Last reply
        0
        • C Crag_Hack

          How do I read 4 bytes as int32_t? QDataStream readRawData?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Crag_Hack said in How to skip chunks using QDataStream?:

          How do I read 4 bytes as int32_t?

          int32_t n;
          stream >> n;
          

          QDataStream readRawData?

          readRawData() won't convert the data for you.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          4
          • C Offline
            C Offline
            Crag_Hack
            wrote on last edited by
            #5

            Well that was easy :) Thx!

            1 Reply Last reply
            1
            • C Offline
              C Offline
              Crag_Hack
              wrote on last edited by Crag_Hack
              #6

              Is this the only way to skip data when reading from a QFile using a QDataStream? Each compressed QByteArray chunk is preceded by several QStrings. I'd like to skip those as well until arriving at the desired QStrings & QByteArray chunk grouped together.

              JKSHJ J.HilkJ 2 Replies Last reply
              0
              • C Crag_Hack

                Is this the only way to skip data when reading from a QFile using a QDataStream? Each compressed QByteArray chunk is preceded by several QStrings. I'd like to skip those as well until arriving at the desired QStrings & QByteArray chunk grouped together.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #7

                @Crag_Hack said in How to skip chunks using QDataStream?:

                Is this the only way to skip data when reading from a QFile using a QDataStream?

                Yes.

                Each compressed QByteArray chunk is preceded by several QStrings. I'd like to skip those as well until arriving at the desired QStrings & QByteArray chunk grouped together.

                Then you must skip the QStrings too, using the same technique.

                However, it sounds like your requirements are:

                • A file to store heterogenous binary and text data
                • The ability to append new data to the file
                • The ability to read out specific data chunks on demand (non-sequential reading)

                Have you considered using a different file format? For example, an SQLite file can let you specify exactly which chunks to extract without manually skipping.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                SGaistS 1 Reply Last reply
                4
                • C Crag_Hack

                  Is this the only way to skip data when reading from a QFile using a QDataStream? Each compressed QByteArray chunk is preceded by several QStrings. I'd like to skip those as well until arriving at the desired QStrings & QByteArray chunk grouped together.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #8

                  @Crag_Hack said in How to skip chunks using QDataStream?:

                  Is this the only way to skip data when reading from a QFile using a QDataStream?

                  No, QDataStream has skipRawData function,

                  but, that requires you to actually know, how many bytes you have to skip.

                  In case of a QString, you would have to analyze the QString header and at that point, you can simply stream it out into a local (and soon to be discarded) variable


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  4
                  • JKSHJ JKSH

                    @Crag_Hack said in How to skip chunks using QDataStream?:

                    Is this the only way to skip data when reading from a QFile using a QDataStream?

                    Yes.

                    Each compressed QByteArray chunk is preceded by several QStrings. I'd like to skip those as well until arriving at the desired QStrings & QByteArray chunk grouped together.

                    Then you must skip the QStrings too, using the same technique.

                    However, it sounds like your requirements are:

                    • A file to store heterogenous binary and text data
                    • The ability to append new data to the file
                    • The ability to read out specific data chunks on demand (non-sequential reading)

                    Have you considered using a different file format? For example, an SQLite file can let you specify exactly which chunks to extract without manually skipping.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JKSH said in How to skip chunks using QDataStream?:

                    @Crag_Hack said in How to skip chunks using QDataStream?:

                    Is this the only way to skip data when reading from a QFile using a QDataStream?

                    Yes.

                    Each compressed QByteArray chunk is preceded by several QStrings. I'd like to skip those as well until arriving at the desired QStrings & QByteArray chunk grouped together.

                    Then you must skip the QStrings too, using the same technique.

                    However, it sounds like your requirements are:

                    • A file to store heterogenous binary and text data
                    • The ability to append new data to the file
                    • The ability to read out specific data chunks on demand (non-sequential reading)

                    Have you considered using a different file format? For example, an SQLite file can let you specify exactly which chunks to extract without manually skipping.

                    Or maybe something like HDF5 which allows you to store various type of data in a structured way.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      Crag_Hack
                      wrote on last edited by
                      #10

                      Thanks guys.
                      @JKSH Can I compress the data chunks in an SQLite file? If so can you point me in a direction to find how to do what you proposed, perhaps a tutorial somewhere? I am already familiar with SQL and read an SQLite tutorial today.

                      B mrjjM 2 Replies Last reply
                      0
                      • C Crag_Hack

                        Thanks guys.
                        @JKSH Can I compress the data chunks in an SQLite file? If so can you point me in a direction to find how to do what you proposed, perhaps a tutorial somewhere? I am already familiar with SQL and read an SQLite tutorial today.

                        B Offline
                        B Offline
                        Bonnie
                        wrote on last edited by
                        #11

                        @Crag_Hack
                        You can save compressed QByteArray as BLOB type

                        1 Reply Last reply
                        4
                        • C Crag_Hack

                          Thanks guys.
                          @JKSH Can I compress the data chunks in an SQLite file? If so can you point me in a direction to find how to do what you proposed, perhaps a tutorial somewhere? I am already familiar with SQL and read an SQLite tutorial today.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Crag_Hack
                          Hi
                          There is
                          https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite
                          that stores an image. It could also be any binary blob.

                          1 Reply Last reply
                          1
                          • C Offline
                            C Offline
                            Crag_Hack
                            wrote on last edited by Crag_Hack
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Crag_Hack
                              wrote on last edited by
                              #14

                              Will the performance of an SQLite file be comparable to using the QDataStream QByteArray data chunk skip method? Does the SQLite setup do sequential processing behind the scenes?

                              With SQLite indexing will I see better performance? Do I need to recreate the index every time I update the file? Can I store the index in the file or do I have create it on the fly every time?

                              The size of the file storing the data in either method will probably be comparable right?

                              Anything else that might be relevant?

                              mrjjM JonBJ 2 Replies Last reply
                              0
                              • C Crag_Hack

                                Will the performance of an SQLite file be comparable to using the QDataStream QByteArray data chunk skip method? Does the SQLite setup do sequential processing behind the scenes?

                                With SQLite indexing will I see better performance? Do I need to recreate the index every time I update the file? Can I store the index in the file or do I have create it on the fly every time?

                                The size of the file storing the data in either method will probably be comparable right?

                                Anything else that might be relevant?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Hi

                                Will the performance of an SQLite file be comparable to using the QDataStream QByteArray data chunk skip method?

                                Depending on how you actually need to access the data, it should be as fast or even faster than datastream if we talking GB size file.

                                But that depends on that you can select with sql statement the data you want and then read the subset compared having to open this
                                GB file and do lots of skipping.

                                < Does the SQLite setup do sequential processing behind the scenes?
                                Im not sure what you mean here ?
                                what type of sequential processing ?

                                With SQLite indexing will I see better performance?

                                Only if you can create index on a column that helps you look up a row faster. For any blobs, i don't think it will help at all.

                                Do I need to recreate the index every time I update the file? Can I store the index in the file or do I have to create it on the fly every time?

                                You basically just tell it to create it and then its automatically handled.

                                The size of the file storing the data in either method will probably be comparable right?

                                Yes the overhead of the DB is not that much.

                                Anything else that might be relevant?

                                • Use transactions when you update data. It will save your file
                                  if the app crash or power cut while writing.
                                  If one of the benefits of using db system versus file directly.

                                • Make sure you check the path of the db file when opening the db. if you point to nonexisting file, then it will create a new one which can be mighty confusing.

                                This tool is really helpful for managing/testing the db
                                https://sqlitebrowser.org/

                                1 Reply Last reply
                                0
                                • C Crag_Hack

                                  Will the performance of an SQLite file be comparable to using the QDataStream QByteArray data chunk skip method? Does the SQLite setup do sequential processing behind the scenes?

                                  With SQLite indexing will I see better performance? Do I need to recreate the index every time I update the file? Can I store the index in the file or do I have create it on the fly every time?

                                  The size of the file storing the data in either method will probably be comparable right?

                                  Anything else that might be relevant?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @Crag_Hack said in How to skip chunks using QDataStream?:

                                  Does the SQLite setup do sequential processing behind the scenes?

                                  No, with a database there will be no "sequential processing", it will be "random access" instead, meaning it will "jump to" the data you want directly, without reading through other data.

                                  You could create a table consisting of two columns: an "id" column (an integer to identify each row uniquely) and a "blob" column to hold a piece of associated data of any size. SQLite will handle the "id" column for you if you make it an "auto-increment", it will just assign numbers 1, 2, 3... for you. You then have to know, somehow, that what you want is "row number #2", and you can ask it to give you that row's "blob" column data directly. It will be able to hand you back that value without having to read through any other rows/columns.

                                  1 Reply Last reply
                                  3
                                  • C Offline
                                    C Offline
                                    Crag_Hack
                                    wrote on last edited by Crag_Hack
                                    #17

                                    Thanks guys.

                                    For this data set I will be first looking up records based on a QString with a name identifying a subset of the data, then further with a qint64 representing a QDateTime identifying a further subset of that data. Sounds to me like the aforementioned rules still apply.

                                    Also I might need to delete some of the records in the beginning of the database if it gets too large. Will SQLite reconstruct the autoincrement id column with up to date values?

                                    And I need to iterate over the entire database pulling out names and qint64 datetime values but I guess this won't complicate anything will it?

                                    Anything else else that might be relevant? :)

                                    JKSHJ 1 Reply Last reply
                                    0
                                    • C Crag_Hack

                                      Thanks guys.

                                      For this data set I will be first looking up records based on a QString with a name identifying a subset of the data, then further with a qint64 representing a QDateTime identifying a further subset of that data. Sounds to me like the aforementioned rules still apply.

                                      Also I might need to delete some of the records in the beginning of the database if it gets too large. Will SQLite reconstruct the autoincrement id column with up to date values?

                                      And I need to iterate over the entire database pulling out names and qint64 datetime values but I guess this won't complicate anything will it?

                                      Anything else else that might be relevant? :)

                                      JKSHJ Offline
                                      JKSHJ Offline
                                      JKSH
                                      Moderators
                                      wrote on last edited by JKSH
                                      #18

                                      @Crag_Hack said in How to skip chunks using QDataStream?:

                                      I will be first looking up records based on a QString with a name identifying a subset of the data, then further with a qint64 representing a QDateTime identifying a further subset of that data....

                                      If the name and timestamp are guaranteed to be unique, then you can use those as the "key". Your SQL query code will look like this:

                                      SELECT compressed_bytes FROM main_table WHERE name='XYZ' AND timestamp=12345678;

                                      Also I might need to delete some of the records in the beginning of the database if it gets too large. Will SQLite reconstruct the autoincrement id column with up to date values?

                                      No, SQLite won't auto-update the IDs when you delete some data.

                                      What's the purpose of updating the IDs? Why not just keep the original values?

                                      And I need to iterate over the entire database pulling out names and qint64 datetime values but I guess this won't complicate anything will it?

                                      You don't "iterate over the database". You ask the database to give you all the data that you're interested in, using only a single query.

                                      After that, you can iterate over the returned dataset :)

                                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                      1 Reply Last reply
                                      1
                                      • C Offline
                                        C Offline
                                        Crag_Hack
                                        wrote on last edited by Crag_Hack
                                        #19

                                        Awesome JKSH. Time to get to work.

                                        So after some more reading looks like indexing is only for searching without an AUTOINCREMENT setup or default rowid.

                                        And for locating a database record with it's rowid or autoincrement column a binary search is used which is far better than using the whole sequential parsing of a file with QDataSream to find your data.

                                        1 Reply Last reply
                                        1
                                        • C Offline
                                          C Offline
                                          Crag_Hack
                                          wrote on last edited by
                                          #20
                                          This post is deleted!
                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved