|
| 1 | +/* |
| 2 | + * Copyright (C) 2006 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef _ANDROID__DATABASE_WINDOW_H |
| 18 | +#define _ANDROID__DATABASE_WINDOW_H |
| 19 | + |
| 20 | +#include <stdint.h> |
| 21 | +#include <sys/types.h> |
| 22 | +#include <sys/mman.h> |
| 23 | + |
| 24 | + |
| 25 | +#include <cutils/log.h> |
| 26 | +#include <stddef.h> |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +#include <binder/IMemory.h> |
| 30 | +#include <utils/RefBase.h> |
| 31 | + |
| 32 | +#include <jni.h> |
| 33 | + |
| 34 | +#define DEFAULT_WINDOW_SIZE 4096 |
| 35 | +#define MAX_WINDOW_SIZE (1024 * 1024) |
| 36 | +#define WINDOW_ALLOCATION_SIZE 4096 |
| 37 | + |
| 38 | +#define ROW_SLOT_CHUNK_NUM_ROWS 16 |
| 39 | + |
| 40 | +// Row slots are allocated in chunks of ROW_SLOT_CHUNK_NUM_ROWS, |
| 41 | +// with an offset after the rows that points to the next chunk |
| 42 | +#define ROW_SLOT_CHUNK_SIZE ((ROW_SLOT_CHUNK_NUM_ROWS * sizeof(row_slot_t)) + sizeof(uint32_t)) |
| 43 | + |
| 44 | + |
| 45 | +#if LOG_NDEBUG |
| 46 | + |
| 47 | +#define IF_LOG_WINDOW() if (false) |
| 48 | +#define LOG_WINDOW(...) |
| 49 | + |
| 50 | +#else |
| 51 | + |
| 52 | +#define IF_LOG_WINDOW() IF_LOG(LOG_DEBUG, "CursorWindow") |
| 53 | +#define LOG_WINDOW(...) LOG(LOG_DEBUG, "CursorWindow", __VA_ARGS__) |
| 54 | + |
| 55 | +#endif |
| 56 | + |
| 57 | + |
| 58 | +// When defined to true strings are stored as UTF8, otherwise they're UTF16 |
| 59 | +#define WINDOW_STORAGE_UTF8 1 |
| 60 | + |
| 61 | +// When defined to true numberic values are stored inline in the field_slot_t, otherwise they're allocated in the window |
| 62 | +#define WINDOW_STORAGE_INLINE_NUMERICS 1 |
| 63 | + |
| 64 | +namespace guardianproject { |
| 65 | + |
| 66 | +typedef struct |
| 67 | +{ |
| 68 | + uint32_t numRows; |
| 69 | + uint32_t numColumns; |
| 70 | +} window_header_t; |
| 71 | + |
| 72 | +typedef struct |
| 73 | +{ |
| 74 | + uint32_t offset; |
| 75 | +} row_slot_t; |
| 76 | + |
| 77 | +typedef struct |
| 78 | +{ |
| 79 | + uint8_t type; |
| 80 | + union { |
| 81 | + double d; |
| 82 | + int64_t l; |
| 83 | + struct { |
| 84 | + uint32_t offset; |
| 85 | + uint32_t size; |
| 86 | + } buffer; |
| 87 | + } data; |
| 88 | +} __attribute__((packed)) field_slot_t; |
| 89 | + |
| 90 | +#define FIELD_TYPE_INTEGER 1 |
| 91 | +#define FIELD_TYPE_FLOAT 2 |
| 92 | +#define FIELD_TYPE_STRING 3 |
| 93 | +#define FIELD_TYPE_BLOB 4 |
| 94 | +#define FIELD_TYPE_NULL 5 |
| 95 | + |
| 96 | +/** |
| 97 | + * This class stores a set of rows from a database in a buffer. The begining of the |
| 98 | + * window has first chunk of row_slot_ts, which are offsets to the row directory, followed by |
| 99 | + * an offset to the next chunk in a linked-list of additional chunk of row_slot_ts in case |
| 100 | + * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a |
| 101 | + * field_slot_t per column, which has the size, offset, and type of the data for that field. |
| 102 | + * Note that the data types come from sqlite3.h. |
| 103 | + */ |
| 104 | +class CursorWindow |
| 105 | +{ |
| 106 | +public: |
| 107 | + CursorWindow(size_t maxSize); |
| 108 | + CursorWindow(){} |
| 109 | + bool setMemory(const sp<IMemory>&); |
| 110 | + ~CursorWindow(); |
| 111 | + |
| 112 | + bool initBuffer(bool localOnly); |
| 113 | + sp<IMemory> getMemory() {return mMemory;} |
| 114 | + |
| 115 | + size_t size() {return mSize;} |
| 116 | + uint8_t * data() {return mData;} |
| 117 | + uint32_t getNumRows() {return mHeader->numRows;} |
| 118 | + uint32_t getNumColumns() {return mHeader->numColumns;} |
| 119 | + void freeLastRow() { |
| 120 | + if (mHeader->numRows > 0) { |
| 121 | + mHeader->numRows--; |
| 122 | + } |
| 123 | + } |
| 124 | + bool setNumColumns(uint32_t numColumns) |
| 125 | + { |
| 126 | + uint32_t cur = mHeader->numColumns; |
| 127 | + if (cur > 0 && cur != numColumns) { |
| 128 | + LOGE("Trying to go from %d columns to %d", cur, numColumns); |
| 129 | + return false; |
| 130 | + } |
| 131 | + mHeader->numColumns = numColumns; |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + int32_t freeSpace(); |
| 136 | + |
| 137 | + void clear(); |
| 138 | + |
| 139 | + /** |
| 140 | + * Allocate a row slot and its directory. The returned |
| 141 | + * pointer points to the begining of the row's directory |
| 142 | + * or NULL if there wasn't room. The directory is |
| 143 | + * initialied with NULL entries for each field. |
| 144 | + */ |
| 145 | + field_slot_t * allocRow(); |
| 146 | + |
| 147 | + /** |
| 148 | + * Allocate a portion of the window. Returns the offset |
| 149 | + * of the allocation, or 0 if there isn't enough space. |
| 150 | + * If aligned is true, the allocation gets 4 byte alignment. |
| 151 | + */ |
| 152 | + uint32_t alloc(size_t size, bool aligned = false); |
| 153 | + |
| 154 | + uint32_t read_field_slot(int row, int column, field_slot_t * slot); |
| 155 | + |
| 156 | + /** |
| 157 | + * Copy data into the window at the given offset. |
| 158 | + */ |
| 159 | + void copyIn(uint32_t offset, uint8_t const * data, size_t size); |
| 160 | + void copyIn(uint32_t offset, int64_t data); |
| 161 | + void copyIn(uint32_t offset, double data); |
| 162 | + |
| 163 | + void copyOut(uint32_t offset, uint8_t * data, size_t size); |
| 164 | + int64_t copyOutLong(uint32_t offset); |
| 165 | + double copyOutDouble(uint32_t offset); |
| 166 | + |
| 167 | + bool putLong(unsigned int row, unsigned int col, int64_t value); |
| 168 | + bool putDouble(unsigned int row, unsigned int col, double value); |
| 169 | + bool putNull(unsigned int row, unsigned int col); |
| 170 | + |
| 171 | + bool getLong(unsigned int row, unsigned int col, int64_t * valueOut); |
| 172 | + bool getDouble(unsigned int row, unsigned int col, double * valueOut); |
| 173 | + bool getNull(unsigned int row, unsigned int col, bool * valueOut); |
| 174 | + |
| 175 | + uint8_t * offsetToPtr(uint32_t offset) {return mData + offset;} |
| 176 | + |
| 177 | + row_slot_t * allocRowSlot(); |
| 178 | + |
| 179 | + row_slot_t * getRowSlot(int row); |
| 180 | + |
| 181 | + /** |
| 182 | + * return NULL if Failed to find rowSlot or |
| 183 | + * Invalid rowSlot |
| 184 | + */ |
| 185 | + field_slot_t * getFieldSlotWithCheck(int row, int column); |
| 186 | + field_slot_t * getFieldSlot(int row, int column) |
| 187 | + { |
| 188 | + int fieldDirOffset = getRowSlot(row)->offset; |
| 189 | + return ((field_slot_t *)offsetToPtr(fieldDirOffset)) + column; |
| 190 | + } |
| 191 | + |
| 192 | +private: |
| 193 | + uint8_t * mData; |
| 194 | + size_t mSize; |
| 195 | + size_t mMaxSize; |
| 196 | + window_header_t * mHeader; |
| 197 | + sp<IMemory> mMemory; |
| 198 | + |
| 199 | + /** |
| 200 | + * Offset of the lowest unused data byte in the array. |
| 201 | + */ |
| 202 | + uint32_t mFreeOffset; |
| 203 | +}; |
| 204 | + |
| 205 | +}; // namespace guardianproject |
| 206 | + |
| 207 | +#endif |
0 commit comments