Skip to content

Commit 1620b95

Browse files
Include CursorWindow/Errors header files
1 parent c153d20 commit 1620b95

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (C) 2007 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_ERRORS_H
18+
#define ANDROID_ERRORS_H
19+
20+
#include <sys/types.h>
21+
#include <errno.h>
22+
23+
namespace android {
24+
25+
// use this type to return error codes
26+
#ifdef HAVE_MS_C_RUNTIME
27+
typedef int status_t;
28+
#else
29+
typedef int32_t status_t;
30+
#endif
31+
32+
/* the MS C runtime lacks a few error codes */
33+
34+
/*
35+
* Error codes.
36+
* All error codes are negative values.
37+
*/
38+
39+
// Win32 #defines NO_ERROR as well. It has the same value, so there's no
40+
// real conflict, though it's a bit awkward.
41+
#ifdef _WIN32
42+
# undef NO_ERROR
43+
#endif
44+
45+
enum {
46+
OK = 0, // Everything's swell.
47+
NO_ERROR = 0, // No errors.
48+
49+
UNKNOWN_ERROR = 0x80000000,
50+
51+
NO_MEMORY = -ENOMEM,
52+
INVALID_OPERATION = -ENOSYS,
53+
BAD_VALUE = -EINVAL,
54+
BAD_TYPE = 0x80000001,
55+
NAME_NOT_FOUND = -ENOENT,
56+
PERMISSION_DENIED = -EPERM,
57+
NO_INIT = -ENODEV,
58+
ALREADY_EXISTS = -EEXIST,
59+
DEAD_OBJECT = -EPIPE,
60+
FAILED_TRANSACTION = 0x80000002,
61+
JPARKS_BROKE_IT = -EPIPE,
62+
#if !defined(HAVE_MS_C_RUNTIME)
63+
BAD_INDEX = -EOVERFLOW,
64+
NOT_ENOUGH_DATA = -ENODATA,
65+
WOULD_BLOCK = -EWOULDBLOCK,
66+
TIMED_OUT = -ETIMEDOUT,
67+
UNKNOWN_TRANSACTION = -EBADMSG,
68+
#else
69+
BAD_INDEX = -E2BIG,
70+
NOT_ENOUGH_DATA = 0x80000003,
71+
WOULD_BLOCK = 0x80000004,
72+
TIMED_OUT = 0x80000005,
73+
UNKNOWN_TRANSACTION = 0x80000006,
74+
#endif
75+
FDS_NOT_ALLOWED = 0x80000007,
76+
};
77+
78+
// Restore define; enumeration is in "android" namespace, so the value defined
79+
// there won't work for Win32 code in a different namespace.
80+
#ifdef _WIN32
81+
# define NO_ERROR 0L
82+
#endif
83+
84+
}; // namespace android
85+
86+
// ---------------------------------------------------------------------------
87+
88+
#endif // ANDROID_ERRORS_H

0 commit comments

Comments
 (0)