-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_array.cc
More file actions
241 lines (206 loc) · 6.09 KB
/
fixed_array.cc
File metadata and controls
241 lines (206 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* File: fixed_array.cc
* Author: zhujun
*
* Created on 2014年9月29日, 下午3:59
*/
#include <string>
#include "fcache/consts.h"
#include "fcache/file_utils.h"
#include "fcache/log.h"
#include "fixed_array.h"
#include "deep_copy.h"
const char* FixedArray::EXT_NAME = "pa";
FixedArray::FixedArray(zval* v, size_t s, int loc) : value(v), size(s), data_loc(loc) {
}
FixedArray::~FixedArray() {
if(data_loc == DATA_LOC_HEAP)
free_copy(value);
}
void FixedArray::CopyZVal(zval *src, zval **dst) const {
MAKE_STD_ZVAL(*dst);
**dst = *src;
switch(Z_TYPE_P(src)) {
case IS_CONSTANT:
case IS_OBJECT:
case IS_STRING:
ZVAL_STRINGL(*dst, REALADDR(char*, value, Z_STRVAL_P(src)), Z_STRLEN_P(src), 1);
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY:
case IS_RESOURCE:
efree(*dst);
*dst = NULL;
break;
default:
break;
}
}
const Bucket* FixedArray::Head() const {
HashTable *ht;
ht = REALADDR(HashTable*, value, Z_ARRVAL_P(value));
return REALADDR(Bucket*, value, ht->pListHead);
}
const Bucket* FixedArray::Tail() const {
HashTable *ht;
ht = REALADDR(HashTable*, value, Z_ARRVAL_P(value));
return REALADDR(Bucket*, value, ht->pListTail);
}
const Bucket* FixedArray::NextBucket(const Bucket *current) const {
return REALADDR(Bucket*, value, current->pListNext);
}
const Bucket* FixedArray::PrevBucket(const Bucket *current) const {
return REALADDR(Bucket*, value, current->pListLast);
}
bool FixedArray::Key(const Bucket *current, zval **k) const {
const char *key;
if(!current) {
*k = NULL;
return false;
}
MAKE_STD_ZVAL(*k);
if(current->nKeyLength == 0) {
ZVAL_LONG(*k, current->h);
}
else {
key = REALADDR(const char*, value, current->arKey);
ZVAL_STRINGL(*k, key, current->nKeyLength, 1);
}
return true;
}
bool FixedArray::Current(const Bucket *current, zval **v) const {
if(!current) {
*v = NULL;
return false;
}
CopyZVal(REALADDR(zval*, value, current->pDataPtr), v);
if(*v == NULL)
return false;
return true;
}
bool FixedArray::Get(const std::string& offset, zval **v) const {
ulong h;
uint nIndex;
Bucket *p;
HashTable *ht;
Bucket **bs;
const char *key;
ht = REALADDR(HashTable*, value, Z_ARRVAL_P(value));
h = zend_inline_hash_func(offset.c_str(), offset.size());
nIndex = h & ht->nTableMask;
bs = REALADDR(Bucket**, value, ht->arBuckets);
p = REALADDR(Bucket*, value, bs[nIndex]);
while (p != NULL) {
key = REALADDR(const char*, value, p->arKey);
if ((p->h == h) && (p->nKeyLength == offset.size()) && !memcmp(key, offset.c_str(), offset.size())) {
CopyZVal(REALADDR(zval*, value, p->pDataPtr), v);
if(*v == NULL)
return false;
return true;
}
p = REALADDR(Bucket*, value, p->pNext);
}
return false;
}
bool FixedArray::Get(ulong h, zval **v) const {
Bucket *p;
Bucket **bs;
uint nIndex;
HashTable *ht;
ht = REALADDR(HashTable*, value, Z_ARRVAL_P(value));
nIndex = h & ht->nTableMask;
bs = REALADDR(Bucket**, value, ht->arBuckets);
p = REALADDR(Bucket*, value, bs[nIndex]);
while (p != NULL) {
if ((p->h == h) && (p->nKeyLength == 0)) {
CopyZVal(REALADDR(zval*, value, p->pDataPtr), v);
if(*v == NULL)
return false;
return true;
}
p = REALADDR(Bucket*, value, p->pNext);
}
return false;
}
long FixedArray::Count() const {
HashTable *ht;
ht = REALADDR(HashTable*, value, Z_ARRVAL_P(value));
return ht->nNumOfElements;
}
/**
* Save array data to file.
*
* [data_type 8 bytes]
* [memory_align 8 bytes]
* [data_size 8 bytes]
* [data......]
*
* @param filepath
*/
bool FixedArray::Save(const std::string& filepath) const {
FILE* idx = OpenFile(filepath.c_str(), "w+");
if(idx == NULL) {
DoLog(Logger::ERROR, "Open FixedArray file failed %d (%s:%d)", errno, __FILE__, __LINE__);
return false;
}
int fd = fileno(idx);
int ret;
uint64_t sec;
sec = FIXED_ARRAY;
ret = WriteFD(fd, reinterpret_cast<const char*>(&sec), sizeof(sec));
if(ret < 0) {
DoLog(Logger::ERROR, "write FixedArray file failed %d (%s:%d)", errno, __FILE__, __LINE__);
CloseFile(idx);
return false;
}
sec = FCACHE_ALIGNMENT;
ret = WriteFD(fd, reinterpret_cast<const char*>(&sec), sizeof(sec));
if(ret < 0) {
DoLog(Logger::ERROR, "write FixedArray file failed %d (%s:%d)", errno, __FILE__, __LINE__);
CloseFile(idx);
return false;
}
ret = WriteFD(fd, reinterpret_cast<const char*>(&size), sizeof(size));
if(ret < 0) {
DoLog(Logger::ERROR, "write FixedArray file failed %d (%s:%d)", errno, __FILE__, __LINE__);
CloseFile(idx);
return false;
}
ret = WriteFD(fd, reinterpret_cast<const char*>(value), size);
if(ret < 0) {
DoLog(Logger::ERROR, "write FixedArray file failed %d (%s:%d)", errno, __FILE__, __LINE__);
CloseFile(idx);
return false;
}
CloseFile(idx);
return true;
}
//static
//Load object from array
FixedArray* FixedArray::Load(zval *value) {
zval* z;
z = NULL;
size_t size = deep_copy(value, &z);
if(size == 0 || z == NULL) {
return NULL;
}
fixup_value(z);
return new FixedArray(z, size, DATA_LOC_HEAP);
}
//Load object from shared memory
FixedArray* FixedArray::Load(void* addr, size_t size) {
uint64_t *sec, zval_size;
if(size == 0) {
DoLog(Logger::WARNING, "size equal zero (%s:%d)", __FILE__, __LINE__);
return NULL;
}
sec = reinterpret_cast<uint64_t*>(addr);
if(*sec++ != FIXED_ARRAY) {
DoLog(Logger::WARNING, "file invalid (%s:%d)", __FILE__, __LINE__);
return NULL;
}
sec++; //skip memory align area
zval_size = *sec++;
zval *value = reinterpret_cast<zval*>(sec);
return new FixedArray(value, zval_size, DATA_LOC_SHARED);
}