blob: 5d490ed988af31ee3b81b4087aa8669cdb872acf [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
Vasu Nori8b0dd7d2010-05-18 11:54:31 -07004 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 *
Vasu Nori8b0dd7d2010-05-18 11:54:31 -07008 * http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 *
Vasu Nori8b0dd7d2010-05-18 11:54:31 -070010 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014 * limitations under the License.
15 */
16
17#ifndef _ANDROID__DATABASE_WINDOW_H
18#define _ANDROID__DATABASE_WINDOW_H
19
20#include <cutils/log.h>
21#include <stddef.h>
22#include <stdint.h>
23
Jeff Brown0cde89f2011-10-10 14:50:10 -070024#include <binder/Parcel.h>
25#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27#if LOG_NDEBUG
28
29#define IF_LOG_WINDOW() if (false)
30#define LOG_WINDOW(...)
31
32#else
33
34#define IF_LOG_WINDOW() IF_LOG(LOG_DEBUG, "CursorWindow")
35#define LOG_WINDOW(...) LOG(LOG_DEBUG, "CursorWindow", __VA_ARGS__)
36
37#endif
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039namespace android {
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
42 * This class stores a set of rows from a database in a buffer. The begining of the
Jeff Brown0cde89f2011-10-10 14:50:10 -070043 * window has first chunk of RowSlots, which are offsets to the row directory, followed by
44 * an offset to the next chunk in a linked-list of additional chunk of RowSlots in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a
Jeff Brown0cde89f2011-10-10 14:50:10 -070046 * FieldSlot per column, which has the size, offset, and type of the data for that field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * Note that the data types come from sqlite3.h.
Jeff Brown0cde89f2011-10-10 14:50:10 -070048 *
49 * Strings are stored in UTF-8.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 */
Jeff Brown0cde89f2011-10-10 14:50:10 -070051class CursorWindow {
52 CursorWindow(const String8& name, int ashmemFd,
53 void* data, size_t size, bool readOnly);
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055public:
Jeff Brown0cde89f2011-10-10 14:50:10 -070056 /* Field types. */
57 enum {
58 FIELD_TYPE_NULL = 0,
59 FIELD_TYPE_INTEGER = 1,
60 FIELD_TYPE_FLOAT = 2,
61 FIELD_TYPE_STRING = 3,
62 FIELD_TYPE_BLOB = 4,
63 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Jeff Brown0cde89f2011-10-10 14:50:10 -070065 /* Opaque type that describes a field slot. */
66 struct FieldSlot {
67 private:
68 int32_t type;
69 union {
70 double d;
71 int64_t l;
72 struct {
73 uint32_t offset;
74 uint32_t size;
75 } buffer;
76 } data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Jeff Brown0cde89f2011-10-10 14:50:10 -070078 friend class CursorWindow;
79 } __attribute((packed));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Jeff Brown0cde89f2011-10-10 14:50:10 -070081 ~CursorWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Jeff Brown0cde89f2011-10-10 14:50:10 -070083 static status_t create(const String8& name, size_t size, bool localOnly,
84 CursorWindow** outCursorWindow);
85 static status_t createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Jeff Brown0cde89f2011-10-10 14:50:10 -070087 status_t writeToParcel(Parcel* parcel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Jeff Brown0cde89f2011-10-10 14:50:10 -070089 inline String8 name() { return mName; }
90 inline size_t size() { return mSize; }
91 inline size_t freeSpace() { return mSize - mHeader->freeOffset; }
92 inline uint32_t getNumRows() { return mHeader->numRows; }
93 inline uint32_t getNumColumns() { return mHeader->numColumns; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Jeff Brown0cde89f2011-10-10 14:50:10 -070095 status_t clear();
96 status_t setNumColumns(uint32_t numColumns);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
98 /**
Jeff Brown0cde89f2011-10-10 14:50:10 -070099 * Allocate a row slot and its directory.
100 * The row is initialized will null entries for each field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 */
Jeff Brown0cde89f2011-10-10 14:50:10 -0700102 status_t allocRow();
103 status_t freeLastRow();
104
105 status_t putBlob(uint32_t row, uint32_t column, const void* value, size_t size);
106 status_t putString(uint32_t row, uint32_t column, const char* value, size_t sizeIncludingNull);
107 status_t putLong(uint32_t row, uint32_t column, int64_t value);
108 status_t putDouble(uint32_t row, uint32_t column, double value);
109 status_t putNull(uint32_t row, uint32_t column);
110
111 /**
112 * Gets the field slot at the specified row and column.
113 * Returns null if the requested row or column is not in the window.
114 */
115 FieldSlot* getFieldSlot(uint32_t row, uint32_t column);
116
117 inline int32_t getFieldSlotType(FieldSlot* fieldSlot) {
118 return fieldSlot->type;
119 }
120
121 inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) {
122 return fieldSlot->data.l;
123 }
124
125 inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) {
126 return fieldSlot->data.d;
127 }
128
129 inline const char* getFieldSlotValueString(FieldSlot* fieldSlot,
130 size_t* outSizeIncludingNull) {
131 *outSizeIncludingNull = fieldSlot->data.buffer.size;
132 return static_cast<char*>(offsetToPtr(fieldSlot->data.buffer.offset));
133 }
134
135 inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot, size_t* outSize) {
136 *outSize = fieldSlot->data.buffer.size;
137 return offsetToPtr(fieldSlot->data.buffer.offset);
138 }
139
140private:
141 static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100;
142
143 struct Header {
144 // Offset of the lowest unused byte in the window.
145 uint32_t freeOffset;
146
147 // Offset of the first row slot chunk.
148 uint32_t firstChunkOffset;
149
150 uint32_t numRows;
151 uint32_t numColumns;
152 };
153
154 struct RowSlot {
155 uint32_t offset;
156 };
157
158 struct RowSlotChunk {
159 RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS];
160 uint32_t nextChunkOffset;
161 };
162
163 String8 mName;
164 int mAshmemFd;
165 void* mData;
166 size_t mSize;
167 bool mReadOnly;
168 Header* mHeader;
169
170 inline void* offsetToPtr(uint32_t offset) {
171 return static_cast<uint8_t*>(mData) + offset;
172 }
173
174 inline uint32_t offsetFromPtr(void* ptr) {
175 return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData);
176 }
177
178 /**
179 * Allocate a portion of the window. Returns the offset
180 * of the allocation, or 0 if there isn't enough space.
181 * If aligned is true, the allocation gets 4 byte alignment.
182 */
183 uint32_t alloc(size_t size, bool aligned = false);
184
185 RowSlot* getRowSlot(uint32_t row);
186 RowSlot* allocRowSlot();
187
188 status_t putBlobOrString(uint32_t row, uint32_t column,
189 const void* value, size_t size, int32_t type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190};
191
192}; // namespace android
193
194#endif