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. The implementation of shared memory is file mapping.

At the ContentProvider end, the data set queried through the Sqlitedatabase package is stored in the shared memory pointed to by Cursorwindow, and then the shared memory is passed to the Contentresolver side by Binder, the query side. This allows the client to access the data set in the shared memory through the cursor.

So how is Cursorwindow implemented?

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);//filename 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. This case should be used on the client side to create a cursorwindow.

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 diagram is as follows:

The data corresponding to a row is represented by an Fieldslot array. The yellow section of the figure represents the data for a row. If the data for a column is a long or double, it is stored directly in the Fieldslot, and if it is a blob or string, the amount of data stored in the Fieldslot is cheap.

Filedslot is defined 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, and when type represents a string or blob, offset represents the offsets of the buffer that holds the real data, and the size represents a buffer of magnitude        friend class Cursorwindow;    } __attribute ((packed));



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.