Android native Cursorwindow Data Preservation principle

Source: Internet
Author: User

The data set we get from the URI query database is stored in the Cursorwindow of the native layer. The essence of Cursorwindow is the abstraction of shared memory to enable cross-process data sharing. Shared memory is implemented in a file-map manner.

The data set queried on the ContentProvider side through the Sqlitedatabase package is saved in the shared memory pointed to by Cursorwindow. The shared memory is then passed to the Contentresolver end, the query side, via Binder.

This allows the client to access the data set in the shared memory through the cursor.

So how did Cursorwindow achieve it?

1. Create a static function by creating a

status_t cursorwindow::create (const string8& name, size_t size, cursorwindow** outcursorwindow) {String8 ashmemNam    E ("Cursorwindow:");    Ashmemname.append (name);//file name status_t result;    int ashmemfd = Ashmem_create_region (ashmemname.string (), size);    if (ASHMEMFD < 0) {result =-errno; } else {result = Ashmem_set_prot_region (ASHMEMFD, Prot_read |        Prot_write); If (Result >= 0) {void* data =:: Mmap (NULL, size, Prot_read |            Prot_write, map_shared, ASHMEMFD, 0);//file map if (data = = map_failed) {result =-errno;                } else {result = Ashmem_set_prot_region (ASHMEMFD, Prot_read);                            If (Result >= 0) {cursorwindow* window = new Cursorwindow (name, ASHMEMFD,                    data, size, false/*readonly*/);                    result = Window->clear (); if (!result) {Log_window ("Created New Cursorwindow: freeoffset=%d, "numrows=%d, numcolumns=%d, msize=%d, mdata=%p",                                Window->mheader->freeoffset, Window->mheader->numrows,                        Window->mheader->numcolumns, Window->msize, Window->mdata);                        *outcursorwindow = window;                    return OK;                } Delete window;        }}:: Munmap (data, size);    }:: Close (ASHMEMFD);    } *outcursorwindow = NULL; return result;}

2. Create by using a file handle. Such a case should be used in the creation of a Cursorwindow by the client.

status_t Cursorwindow::createfromparcel (parcel* Parcel, cursorwindow** outcursorwindow) {String8 name = Parcel->rea    DString8 ();    status_t result;    int ashmemfd = Parcel->readfiledescriptor ();    if (ASHMEMFD = = Int (bad_type)) {result = Bad_type;        } else {ssize_t size = ashmem_get_size_region (ASHMEMFD);        if (Size < 0) {result = Unknown_error;            } else {int dupashmemfd =::d up (ASHMEMFD);            if (DUPASHMEMFD < 0) {result =-errno;                } else {void* data =:: Mmap (NULL, size, Prot_read, map_shared, DUPASHMEMFD, 0);                if (data = = map_failed) {result =-errno; } else {cursorwindow* window = new Cursorwindow (name, DUPASHMEMFD, data, si                    Ze, true/*readonly*/); Log_window ("Created Cursorwindow from parcel:freeoffset=%d," "numrows=%d, Numcolumns=%d, msize=%d, mdata=%p ", Window->mheader->freeoffset, win Dow->mheader->numrows, Window->mheader->numcolumns, Wind                    Ow->msize, Window->mdata);                    *outcursorwindow = window;                return OK;            }:: Close (DUPASHMEMFD);    }}} *outcursorwindow = NULL; return result;}

How does Cursorwindow save a query to a dataset?

Schematic diagrams such as the following:

None of the corresponding data is represented by the Fieldslot array.

The yellow section of the figure represents the corresponding data for a row. Assume that the corresponding data for the column is a long or double. Then it is stored directly in the Fieldslot, assuming it is a blob or string, then the inexpensive amount of data stored in the Fieldslot.

The definition of Filedslot is as follows:

    struct Fieldslot {    private:        int32_t the type of data corresponding to the type;//column        Union {            Double D;            int64_t l;            struct {                uint32_t offset;                uint32_t size;            } buffer;        } The data type of the Data;//data is union, when the type represents a string or blob. Offset represents the offset of the buffer that holds the real data. The size of the buffer is a        friend class Cursorwindow;    } __attribute ((packed));



Android native Cursorwindow Data Preservation principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.