|
| 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 | +// modified from original source see README at the top level of this project |
| 17 | + |
| 18 | +#ifndef _ANDROID__DATABASE_WINDOW_H |
| 19 | +#define _ANDROID__DATABASE_WINDOW_H |
| 20 | + |
| 21 | +#include "ALog-priv.h" |
| 22 | +#include <cstddef> |
| 23 | +#include <cstdint> |
| 24 | + |
| 25 | +#include "Errors.h" |
| 26 | + |
| 27 | +namespace android { |
| 28 | + |
| 29 | +/** |
| 30 | + * This class stores a set of rows from a database in a buffer. The beginning of the |
| 31 | + * window has first chunk of RowSlots, which are offsets to the row directory, followed by |
| 32 | + * an offset to the next chunk in a linked-list of additional chunk of RowSlots in case |
| 33 | + * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a |
| 34 | + * FieldSlot per column, which has the size, offset, and type of the data for that field. |
| 35 | + * Note that the data types come from sqlite3.h. |
| 36 | + * |
| 37 | + * Strings are stored in UTF-8. |
| 38 | + */ |
| 39 | + class CursorWindow { |
| 40 | + CursorWindow(const char* name, void* data, size_t size, bool readOnly); |
| 41 | + |
| 42 | + public: |
| 43 | + /* Field types. */ |
| 44 | + enum { |
| 45 | + FIELD_TYPE_NULL = 0, |
| 46 | + FIELD_TYPE_INTEGER = 1, |
| 47 | + FIELD_TYPE_FLOAT = 2, |
| 48 | + FIELD_TYPE_STRING = 3, |
| 49 | + FIELD_TYPE_BLOB = 4, |
| 50 | + }; |
| 51 | + |
| 52 | + /* Opaque type that describes a field slot. */ |
| 53 | + struct FieldSlot { |
| 54 | + private: |
| 55 | + int32_t type; |
| 56 | + union { |
| 57 | + double d; |
| 58 | + int64_t l; |
| 59 | + struct { |
| 60 | + uint32_t offset; |
| 61 | + uint32_t size; |
| 62 | + } buffer; |
| 63 | + } data; |
| 64 | + |
| 65 | + friend class CursorWindow; |
| 66 | + } __attribute((packed)); |
| 67 | + |
| 68 | + ~CursorWindow(); |
| 69 | + |
| 70 | + static status_t create(const char* name, size_t size, CursorWindow** outCursorWindow); |
| 71 | + |
| 72 | + inline const char* name() { return mName; } |
| 73 | + inline size_t size() const { return mSize; } |
| 74 | + inline size_t freeSpace() { return mSize - mHeader->freeOffset; } |
| 75 | + inline uint32_t getNumRows() { return mHeader->numRows; } |
| 76 | + inline uint32_t getNumColumns() { return mHeader->numColumns; } |
| 77 | + |
| 78 | + status_t clear(); |
| 79 | + status_t setNumColumns(uint32_t numColumns); |
| 80 | + |
| 81 | + /** |
| 82 | + * Allocate a row slot and its directory. |
| 83 | + * The row is initialized will null entries for each field. |
| 84 | + */ |
| 85 | + status_t allocRow(); |
| 86 | + status_t freeLastRow(); |
| 87 | + status_t maybeInflate(); |
| 88 | + |
| 89 | + status_t putBlob(uint32_t row, uint32_t column, const void* value, size_t size); |
| 90 | + status_t putString(uint32_t row, uint32_t column, const char* value, size_t sizeIncludingNull); |
| 91 | + status_t putLong(uint32_t row, uint32_t column, int64_t value); |
| 92 | + status_t putDouble(uint32_t row, uint32_t column, double value); |
| 93 | + status_t putNull(uint32_t row, uint32_t column); |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the field slot at the specified row and column. |
| 97 | + * Returns null if the requested row or column is not in the window. |
| 98 | + */ |
| 99 | + FieldSlot* getFieldSlot(uint32_t row, uint32_t column); |
| 100 | + |
| 101 | + static inline int32_t getFieldSlotType(FieldSlot* fieldSlot) { |
| 102 | + return fieldSlot->type; |
| 103 | + } |
| 104 | + |
| 105 | + static inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) { |
| 106 | + return fieldSlot->data.l; |
| 107 | + } |
| 108 | + |
| 109 | + static inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) { |
| 110 | + return fieldSlot->data.d; |
| 111 | + } |
| 112 | + |
| 113 | + inline const char* getFieldSlotValueString(FieldSlot* fieldSlot, |
| 114 | + size_t* outSizeIncludingNull) { |
| 115 | + *outSizeIncludingNull = fieldSlot->data.buffer.size; |
| 116 | + return static_cast<char*>(offsetToPtr(fieldSlot->data.buffer.offset)); |
| 117 | + } |
| 118 | + |
| 119 | + inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot, size_t* outSize) { |
| 120 | + *outSize = fieldSlot->data.buffer.size; |
| 121 | + return offsetToPtr(fieldSlot->data.buffer.offset); |
| 122 | + } |
| 123 | + |
| 124 | + private: |
| 125 | + static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100; |
| 126 | + static const size_t CURSOR_SIZE_EXTRA = 512; |
| 127 | + |
| 128 | + struct Header { |
| 129 | + // Offset of the lowest unused byte in the window. |
| 130 | + uint32_t freeOffset; |
| 131 | + |
| 132 | + // Offset of the first row slot chunk. |
| 133 | + uint32_t firstChunkOffset; |
| 134 | + |
| 135 | + uint32_t numRows; |
| 136 | + uint32_t numColumns; |
| 137 | + }; |
| 138 | + |
| 139 | + struct RowSlot { |
| 140 | + uint32_t offset; |
| 141 | + }; |
| 142 | + |
| 143 | + struct RowSlotChunk { |
| 144 | + RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS]; |
| 145 | + uint32_t nextChunkOffset; |
| 146 | + }; |
| 147 | + |
| 148 | + char* mName; |
| 149 | + void* mData; |
| 150 | + size_t mSize; |
| 151 | + bool mReadOnly; |
| 152 | + Header* mHeader; |
| 153 | + |
| 154 | + inline void* offsetToPtr(uint32_t offset) { |
| 155 | + return static_cast<uint8_t*>(mData) + offset; |
| 156 | + } |
| 157 | + |
| 158 | + inline uint32_t offsetFromPtr(void* ptr) { |
| 159 | + return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Allocate a portion of the window. Returns the offset |
| 164 | + * of the allocation, or 0 if there isn't enough space. |
| 165 | + * If aligned is true, the allocation gets 4 byte alignment. |
| 166 | + */ |
| 167 | + uint32_t alloc(size_t size, bool aligned = false); |
| 168 | + |
| 169 | + RowSlot* getRowSlot(uint32_t row); |
| 170 | + RowSlot* allocRowSlot(); |
| 171 | + |
| 172 | + status_t putBlobOrString(uint32_t row, uint32_t column, |
| 173 | + const void* value, size_t size, int32_t type); |
| 174 | + }; |
| 175 | + |
| 176 | +} // namespace android |
| 177 | + |
| 178 | +#endif |
0 commit comments