Repository navigation
Expand file tree
/
Copy pathmpdb.c
More file actions
513 lines (457 loc) · 17.8 KB
/
Copy pathmpdb.c
File metadata and controls
513 lines (457 loc) · 17.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-11-10 think the first version
* 2026-5-26 think the 2.0 version
*/
#include "mpdb.h"
#include <string.h>
#define BLOCK_OFFSET(block) (block * MPDB_BLOCK_SIZE)
#define DATA_BLOCK_INDEX_OFFSET(index) (index + 1)
#define DATA_BLOCK_LEN(quantity) (quantity - 1)
// get block index offset from block index, bio = block index offset
static inline u32 get_bio_from_block_index(u32 index, u32 block_head_index, u32 data_block_len)
{
const int block_head_index_offset = (block_head_index);
return ((block_head_index_offset + index) % data_block_len);
}
static inline u32 get_bio_from_index(u32 index, u32 block_head_index, u32 block_data_max, u32 data_block_len)
{
const int block_offset = index / block_data_max;
return get_bio_from_block_index(block_offset, block_head_index, data_block_len);
}
static inline void update_block_head_index(Mpdb *mpdb, u32 data_block_quantity)
{
mpdb->block_head_index = (mpdb->block_head_index + 1) % data_block_quantity;
}
static inline const struct mpdb_head_prm *get_db_head_prm(Mpdb *mpdb)
{
Mpdb_read_port(0, mpdb->buffer, sizeof(struct mpdb_head_prm));
return (struct mpdb_head_prm *)mpdb->buffer;
}
static inline struct mpdb_head *get_db_head(Mpdb *mpdb)
{
Mpdb_read_port(0, mpdb->buffer, MPDB_BLOCK_SIZE);
return (struct mpdb_head *)mpdb->buffer;
}
static inline u8 split_bit(u8 c, u8 n)
{
return (c >> n) & 1;
}
static inline void set_zero(u8 *s, u8 n)
{
*s = *s & ~(1 << n);
}
static u32 deleted_itar(Mpdb *mpdb, u32 index, u32 block_data_max, u32 block_len)
{
u32 itar_count = 0;
for (int i = 0; i < DATA_BLOCK_LEN(block_len); i++)
{
MPDB_PRINT("read block index = %d | %d\n", index, get_bio_from_block_index(i, mpdb->block_head_index, DATA_BLOCK_LEN(block_len)));
Mpdb_read_port(
MPDB_DATA_OFFSET + BLOCK_OFFSET(get_bio_from_block_index(i, mpdb->block_head_index, DATA_BLOCK_LEN(block_len))),
mpdb->buffer,
sizeof(struct mpdb_block_head));
struct mpdb_block_head *data_head_p = (struct mpdb_block_head *)mpdb->buffer;
if (data_head_p->deleted_flag == 0)
{
int j = 0, g = 0;
while (1)
{
if ((j + g) >= block_data_max)
{
MPDB_PRINT("break block_data_max=%d\n", (j + g));
break;
}
if (itar_count > index)
{
MPDB_PRINT("break itar_count=%d\n", itar_count);
break;
}
itar_count++;
if (split_bit(data_head_p->delete_table[(j + g) / 8], (j + g) % 8) == 0)
{
index++;
MPDB_PRINT("delete_index = %d | %d | %X\n", index, (j + g), data_head_p->delete_table[(j + g) / 8]);
g++;
continue;
}
MPDB_PRINT("itar_index = %d | %d | %d | %X\n", itar_count, (j + g), split_bit(data_head_p->delete_table[(j + g) / 8], (j + g) % 8), data_head_p->delete_table[(j + g) / 8]);
j++;
}
}
else
{
itar_count += block_data_max;
}
if (itar_count > index)
{
break;
}
}
return index;
}
static void mpdb_writeHead(struct mpdb_head *head, u32 data_size)
{
strcpy(head->prm.name, "mdb");
head->prm.version = MPDB_VERSION;
head->prm.capacity = Mpdb_capacity_port();
head->prm.data_size = data_size;
head->prm.block_quantity = head->prm.capacity / MPDB_BLOCK_SIZE;
head->prm.block_data_max = (MPDB_BLOCK_SIZE - 32) / head->prm.data_size;
head->prm.data_max = head->prm.block_data_max * (head->prm.block_quantity - 1);
MPDB_MEMSET(head->reserve, 0, sizeof(head->reserve));
MPDB_MEMSET(head->count, 0xFF, sizeof(head->count));
Mpdb_write_port(0, (u8 *)head, MPDB_BLOCK_SIZE);
}
static void mpdb_save_count(Mpdb *mpdb, struct mpdb_head *head)
{
mpdb->data_tail++;
mpdb->data_tail %= mpdb->capacity;
if (mpdb->count_tail == 1000)
{
MPDB_MEMSET(head->count, 0xFF, sizeof(head->count));
Mpdb_erase_port(0, MPDB_BLOCK_SIZE);
mpdb->count_tail = 0;
}
head->count[mpdb->count_tail] = mpdb->data_tail;
Mpdb_write_port(0, (u8 *)head, MPDB_BLOCK_SIZE);
mpdb->count_tail++;
}
static inline void mpdb_block_head_init(struct mpdb_block_head *head)
{
head->init_flag = 0;
head->empty_flag = 0;
head->deleted_flag = 1;
head->full_flag = 1;
head->head_flag = 1;
head->reserve = 0x7;
}
static MpdbErr mpdb_save_data(Mpdb *mpdb, u32 block_data_max, u32 block_quantity, u32 save_index, void *data, u32 data_size)
{
u32 block_index = save_index / block_data_max;
u32 data_block_addr = MPDB_DATA_OFFSET + BLOCK_OFFSET(block_index);
// warning: mpdb->buffer update, head_prm lost !!!
Mpdb_read_port(data_block_addr, mpdb->buffer, sizeof(struct mpdb_block_head));
struct mpdb_block_head *head = (struct mpdb_block_head *)mpdb->buffer;
u8 next_block_setting_head_flag = 0;
if (head->head_flag == 0 && head->full_flag == 0)
{
Mpdb_erase_port(data_block_addr, MPDB_BLOCK_SIZE);
MPDB_MEMSET(head, 0xFF, sizeof(struct mpdb_block_head));
next_block_setting_head_flag = 1;
update_block_head_index(mpdb, DATA_BLOCK_LEN(block_quantity));
mpdb->data_count -= block_data_max;
}
if (head->init_flag == 1)
{
mpdb_block_head_init(head);
Mpdb_write_port(data_block_addr, (u8 *)head, sizeof(struct mpdb_block_head));
}
if (save_index % block_data_max == block_data_max - 1)
{
head->full_flag = 0;
Mpdb_write_port(data_block_addr, (u8 *)head, sizeof(struct mpdb_block_head));
}
u32 save_data_p = data_block_addr + sizeof(struct mpdb_block_head) + (save_index % block_data_max) * data_size;
Mpdb_write_port(save_data_p, data, data_size);
MPDB_PRINT(">bi=%d, block_addr=0x%X, data_addr=0x%x\n", block_index, data_block_addr, save_data_p);
if (next_block_setting_head_flag == 1)
{
data_block_addr = MPDB_DATA_OFFSET + BLOCK_OFFSET(mpdb->block_head_index);
Mpdb_read_port(data_block_addr, mpdb->buffer, sizeof(struct mpdb_block_head));
head->head_flag = 0;
Mpdb_write_port(data_block_addr, (u8 *)head, sizeof(struct mpdb_block_head));
MPDB_PRINT("block head move to %d\n", mpdb->block_head_index);
}
return MpdbErr_OK;
}
MpdbErr Mpdb_init(Mpdb *mpdb, MpdbQueryMode query_mode)
{
if (mpdb == NULL)
{
return MpdbErr_NULL;
}
mpdb->query_mode = query_mode;
struct mpdb_head *db_head = get_db_head(mpdb);
MPDB_PRINT("version=%d\n", db_head->prm.version);
MPDB_PRINT("data_max=%d\n", db_head->prm.data_max);
MPDB_PRINT("data_size=%d\n", db_head->prm.data_size);
MPDB_PRINT("bloac_max=%d\n", db_head->prm.block_quantity);
MPDB_PRINT("bloac_data_max=%d\n", db_head->prm.block_data_max);
if (strcmp(db_head->prm.name, "mdb") != 0)
{
if (strcmp(db_head->prm.name, "\xff\xff\xff\xff") == 0)
{
return MpdbErr_INIT;
}
else
{
return MpdbErr_HEAD;
}
}
if (db_head->prm.version != MPDB_VERSION)
{
return MpdbErr_VERSION;
}
mpdb->block_head_index = 0;
mpdb->capacity = db_head->prm.data_max;
for (int i = 0; i < 1000; i++)
{
// MPDB_PRINT("[%d]count_table=%d\n", i, db_head->count[i]);
if (db_head->count[i] == 0xFFFFFFFF)
{
mpdb->count_tail = i;
MPDB_PRINT("[%d]count_table break\n", i);
break;
}
mpdb->data_tail = db_head->count[i];
}
const u32 block_mex = db_head->prm.block_quantity;
const u32 block_data_max = db_head->prm.block_data_max;
const u32 data_head_size = sizeof(struct mpdb_block_head);
const u32 data_size = db_head->prm.data_size;
for (int b_i = 0; b_i < DATA_BLOCK_LEN(block_mex); b_i++)
{
const u32 data_block_addr = MPDB_DATA_OFFSET + BLOCK_OFFSET(b_i);
Mpdb_read_port(data_block_addr, mpdb->buffer, data_head_size);
struct mpdb_block_head *block_head = (struct mpdb_block_head *)mpdb->buffer;
if (block_head->init_flag == 1 || block_head->empty_flag == 1)
{
MPDB_PRINT("init=%d, empty_flag=%d\n", block_head->init_flag, block_head->empty_flag);
break;
}
if (block_head->head_flag == 0)
{
MPDB_PRINT("head_flag invalid=%d\n", block_head->head_flag);
mpdb->block_head_index = b_i;
}
if (
mpdb->query_mode == MpdbQueryMode_NORMAL &&
block_head->deleted_flag == 0 &&
block_head->full_flag == 0)
{
u32 delete_count = 0;
for (int i = 0; i < block_data_max / 8 + 1; i++)
{
if (block_head->delete_table[i] == 0xff)
{
continue;
}
for (int j = 0; j < 8; j++)
{
if (split_bit(block_head->delete_table[i], j) == 0)
{
delete_count++;
}
}
}
mpdb->data_count += block_data_max - delete_count;
continue;
}
if (block_head->full_flag == 0)
{
mpdb->data_count += block_data_max;
continue;
}
MPDB_PRINT("itar data block=%d\n", b_i);
u8 *data_p = mpdb->buffer + data_head_size;
Mpdb_read_port(data_block_addr + data_head_size, data_p, MPDB_BLOCK_SIZE - data_head_size);
for (int i = 0; i < block_data_max; i++)
{
if (
mpdb->query_mode == MpdbQueryMode_NORMAL &&
block_head->deleted_flag == 0 &&
split_bit(block_head->delete_table[i / 8], i % 8) == 0)
{
continue;
}
u8 *data_item_p = data_p + data_size * i;
u32 data_valid = 0;
for (int j = 0; j < data_size; j++)
{
if (data_item_p[j] != 0xff)
{
data_valid++;
break;
}
}
if (data_valid == 0)
{
break;
}
mpdb->data_count++;
}
}
MPDB_PRINT("count_tail=%d\n", mpdb->count_tail);
MPDB_PRINT("data_tail=%d\n", mpdb->data_tail);
MPDB_PRINT("data_count=%d\n", mpdb->data_count);
MPDB_PRINT("block_head_index=%d\n", mpdb->block_head_index);
return MpdbErr_OK;
}
MpdbErr Mpdb_add(Mpdb *const mpdb, void *data)
{
struct mpdb_head *head = get_db_head(mpdb);
u32 save_index = mpdb->data_tail;
mpdb_save_count(mpdb, head);
MPDB_PRINT("count_tail=%d ", mpdb->count_tail);
MPDB_PRINT("data_tail=%d ", mpdb->data_tail);
MPDB_PRINT("data_count=%d\n", mpdb->data_count);
u32 data_size = head->prm.data_size;
MpdbErr ret = mpdb_save_data(
mpdb,
head->prm.block_data_max,
head->prm.block_quantity,
save_index,
data,
data_size);
if (ret != MpdbErr_OK)
{
return ret;
}
mpdb->data_count++;
return ret;
}
u32 Mpdb_count(const Mpdb *const mpdb)
{
return mpdb->data_count;
}
u32 Mpdb_capacity(const Mpdb *const mpdb)
{
return mpdb->capacity;
}
MpdbErr Mpdb_positioning(Mpdb *mpdb, u32 index, MpdbItarContext *context)
{
if (index >= mpdb->data_count)
{
MPDB_PRINT("index out of range=%d\n", index);
context->index = 0xFFFFFFFF;
context->block_index = 0xFFFFFFFF;
context->block_data_max = 0xFFFFFFFF;
context->data_size = 0xFFFFFFFF;
context->block_quantity = 0xFFFFFFFF;
return MpdbErr_ERROR;
}
const struct mpdb_head_prm *prm = get_db_head_prm(mpdb);
const u32 data_size = prm->data_size;
const u32 block_data_max = prm->block_data_max;
const u32 block_len = prm->block_quantity;
if(mpdb->query_mode == MpdbQueryMode_NORMAL){
context->index = deleted_itar(mpdb, index, block_data_max, block_len);
}
else if(mpdb->query_mode == MpdbQueryMode_FAST){
context->index = index;
}
context->block_index = get_bio_from_index(context->index, mpdb->block_head_index, block_data_max, DATA_BLOCK_LEN(block_len));
context->block_data_max = block_data_max;
context->data_size = data_size;
context->block_quantity = block_len;
Mpdb_read_port(MPDB_DATA_OFFSET + BLOCK_OFFSET(context->block_index), mpdb->buffer, sizeof(struct mpdb_block_head));
return MpdbErr_OK;
}
MpdbErr Mpdb_get(Mpdb *const mpdb, MpdbItarContext *context, void *data)
{
if (context->index == 0xFFFFFFFF && context->block_index == 0xFFFFFFFF && context->block_data_max == 0xFFFFFFFF && context->data_size == 0xFFFFFFFF && context->block_quantity == 0xFFFFFFFF)
{
return MpdbErr_ERROR;
}
struct mpdb_block_head *data_head = (struct mpdb_block_head *)mpdb->buffer;
// What if it collides with the data head?
for(;;)
{
u32 block_data_index = get_bio_from_index(context->index, mpdb->block_head_index, context->block_data_max, DATA_BLOCK_LEN(context->block_quantity));
if (context->block_index != block_data_index)
{
Mpdb_read_port(MPDB_DATA_OFFSET + BLOCK_OFFSET(block_data_index), mpdb->buffer, sizeof(struct mpdb_block_head));
context->block_index = block_data_index;
}
if (mpdb->query_mode == MpdbQueryMode_NORMAL && split_bit(data_head->delete_table[(context->index % context->block_data_max) / 8], (context->index % context->block_data_max) % 8) == 0)
{
MPDB_PRINT("data deleted, index=%d | %d | 0x%X\n", context->index, (context->index % context->block_data_max) / 8, data_head->delete_table[(context->index % context->block_data_max) / 8]);
context->index++;
continue;
}
break;
}
u32 data_p = MPDB_DATA_OFFSET + BLOCK_OFFSET(context->block_index) + sizeof(struct mpdb_block_head) + (context->index % context->block_data_max * context->data_size);
MPDB_PRINT("block_index=%d, data_p=0x%X\n", context->block_index, data_p);
Mpdb_read_port(data_p, data, context->data_size);
context->index++;
return MpdbErr_OK;
}
MpdbErr Mpdb_delete(Mpdb *mpdb, u32 index)
{
const struct mpdb_head_prm *prm = get_db_head_prm(mpdb);
const u32 block_data_max = prm->block_data_max;
const u32 block_len = prm->block_quantity;
const u32 data_index = deleted_itar(mpdb, index, block_data_max, block_len);
const u32 data_block_index = get_bio_from_index(data_index, mpdb->block_head_index, block_data_max, DATA_BLOCK_LEN(block_len));
MPDB_PRINT("delete index=%d, data_index=%d\n", index, data_index);
Mpdb_read_port(MPDB_DATA_OFFSET + BLOCK_OFFSET(data_block_index), mpdb->buffer, sizeof(struct mpdb_block_head));
MPDB_PRINT("data_index=%d, data_block_index=%d\n", data_index, data_block_index);
struct mpdb_block_head *data_head = (struct mpdb_block_head *)mpdb->buffer;
data_head->deleted_flag = 0;
MPDB_PRINT("detele_table=0x%X\n", data_head->delete_table[(data_index % block_data_max) / 8]);
set_zero(&data_head->delete_table[(data_index % block_data_max) / 8], (data_index % block_data_max) % 8);
MPDB_PRINT("detele_table=0x%X\n", data_head->delete_table[(data_index % block_data_max) / 8]);
Mpdb_write_port(MPDB_DATA_OFFSET + BLOCK_OFFSET(data_block_index), mpdb->buffer, sizeof(struct mpdb_block_head));
mpdb->data_count--;
return MpdbErr_OK;
}
MpdbErr Mpdb_reset(Mpdb *mpdb, u32 data_size)
{
MpdbErr ret = Mpdb_erase_all_port();
if (ret != MpdbErr_OK)
{
return ret;
}
struct mpdb_head *head = (struct mpdb_head *)mpdb->buffer;
mpdb_writeHead(head, data_size);
mpdb->count_tail = 0;
mpdb->data_tail = 0;
mpdb->data_count = 0;
mpdb->capacity = head->prm.data_max;
struct mpdb_block_head *data_head = (struct mpdb_block_head *)mpdb->buffer;
MPDB_MEMSET(data_head, 0xFF, sizeof(struct mpdb_block_head));
mpdb_block_head_init(data_head);
data_head->head_flag = 0;
Mpdb_write_port(MPDB_DATA_OFFSET, (u8 *)data_head, sizeof(struct mpdb_block_head));
return ret;
}
MpdbErr Mpdb_export(Mpdb *mpdb)
{
if (Mpdb_export_open_port() != MpdbErr_OK)
{
return MpdbErr_ERROR;
}
const struct mpdb_head_prm *prm = get_db_head_prm(mpdb);
const u32 block_quantity = prm->block_quantity;
const u8 empty_flag[] = {0xff, 0xff, 0xff, 0xff};
MPDB_PRINT("block quantity=%d\n", block_quantity);
for (int i = 0; i < block_quantity; i++)
{
MPDB_PRINT("[%d/%d]export=0x%x\n", i, block_quantity, i * MPDB_BLOCK_SIZE);
Mpdb_read_port(i * MPDB_BLOCK_SIZE, mpdb->buffer, MPDB_BLOCK_SIZE);
if (MPDB_MEMCMP(mpdb->buffer, empty_flag, sizeof(empty_flag)) == 0)
{
break;
}
if (Mpdb_export_write_port(i * MPDB_BLOCK_SIZE, mpdb->buffer, MPDB_BLOCK_SIZE) != MpdbErr_OK)
{
return MpdbErr_ERROR;
}
}
Mpdb_export_close_port();
return MpdbErr_OK;
}
#ifdef MPDB_TEST
#include <stdio.h>
void Mpdb_test()
{
printf("MPDB test function.\n");
}
#endif