Skip to content

Commit edf9219

Browse files
committed
kvspace-c: complete issue 329 headlenpow storage
1 parent 92c1bd5 commit edf9219

19 files changed

Lines changed: 1523 additions & 1607 deletions

‎CMakeLists.txt‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ set(CMAKE_C_EXTENSIONS OFF)
88
include(GNUInstallDirs)
99

1010
add_library(kvspace-c SHARED
11-
src/xvalue.c
11+
src/coord.c
1212
src/xvalue_head.c
13+
src/xvalue_meta.c
1314
src/kvspace.c
1415
src/durable_abi.c
1516
)
@@ -20,7 +21,7 @@ set_target_properties(kvspace-c PROPERTIES
2021
SOVERSION 1
2122
)
2223

23-
# 无公共头:内部头在 src/(kvspace_shm.h、xvalue.h),权威 ABI 头在 kvspace 仓库。
24+
# Internal headers are in src/; the public ABI header is in kvspace.
2425
# 两个依赖库走 header-only(src/kvspace.c 里 #define *_IMPLEMENTATION),故不链接 .so;
2526
# 头的搜索路径由系统 include(/usr/include/blockmalloc、/usr/include/slotsboxmalloc)提供。
2627

@@ -56,7 +57,11 @@ if(KVSPACE_BUILD_TESTS)
5657
target_link_libraries(test_handle_cache PRIVATE kvspace-c)
5758
add_test(NAME handle_cache COMMAND test_handle_cache)
5859

59-
add_executable(test_xvalue_head tests/test_xvalue_head.c src/xvalue_head.c)
60+
add_executable(test_xvalue_head tests/test_xvalue_head.c src/xvalue_head.c src/xvalue_meta.c)
6061
target_include_directories(test_xvalue_head PRIVATE src)
6162
add_test(NAME xvalue_head COMMAND test_xvalue_head)
63+
add_executable(test_new_wire_shm tests/test_new_wire_shm.c)
64+
target_include_directories(test_new_wire_shm PRIVATE src)
65+
target_link_libraries(test_new_wire_shm PRIVATE kvspace-c)
66+
add_test(NAME new_wire_shm COMMAND test_new_wire_shm)
6267
endif()

‎README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ Two surfaces:
3434

3535
## XValue
3636

37-
kindexpr TLV head, byte-identical to `kvspace-durable` (`include/kvspace/xvalue.h`):
37+
kindexpr XValue uses the headlenpow wire format:
3838

3939
```
40-
[1B kindexprlen][kindexpr + 0x00 pad][1B ro][4B vid LE][4B raw_len LE][raw]
40+
[pow:u8][flags:u8][a:u64le][b:u64le][langtype, padded to 2^pow][body]
4141
```
4242

43-
- kindexpr first byte: `*` = soft link (raw = target path), `@` = ext handle, otherwise inline.
44-
- `[d0,d1]kind` carries ndim+dims; bare `kind` is a scalar; `char/*` is always a 1-D sequence (`[n]`).
45-
- `None` is encoded as NULL / length 0.
43+
- The low two flag bits select fixed, sized, tensor, or extension storage; bit 2 marks a pointer.
44+
- Directory members come from physical key prefixes. `@ext` stores its locator in class 3.
45+
- `ro` and `vid` live at `/.kvspace-meta/<hex-encoded key>`.
4646

47-
Kinds: `bool`, `int8..int64`, `uint8..uint64`, `float32/64`, `char/utf32|utf8|ascii`, `stringkeymap`, `index`, `extindex`, `rwir`, `rwfunc`, `defrwir`, `scope`, `time`, `duration`.
47+
The codec accepts `None`, scalars, sized byte and character arrays, tensors, maps, and code values.
4848

4949
## Tutorial
5050

‎src/coord.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "coord.h"
2+
#include <stdbool.h>
3+
#include <string.h>
4+
5+
int kvspaceCoordIsCoord(const char *name) {
6+
if (!name || name[0] != '[')
7+
return 0;
8+
size_t n = strlen(name);
9+
if (n < 3 || name[n - 1] != ']')
10+
return 0;
11+
for (size_t i = 1; i + 1 < n; i++)
12+
if (name[i] == '[' || name[i] == ']')
13+
return 0;
14+
return 1;
15+
}
16+
17+
/* Parse integer coordinates; return -1 on mismatch. */
18+
int kvspaceParseCoord(const char *name, int64_t *coords, int maxn) {
19+
if (!name || name[0] != '[')
20+
return -1;
21+
int n = 0;
22+
int64_t cur = 0;
23+
bool has = false;
24+
for (const char *p = name + 1; *p; p++) {
25+
if (*p >= '0' && *p <= '9') {
26+
cur = cur * 10 + (*p - '0');
27+
has = true;
28+
} else if (*p == ',') {
29+
if (!has || n >= maxn)
30+
return -1;
31+
coords[n++] = cur;
32+
cur = 0;
33+
has = false;
34+
} else if (*p == ']') {
35+
if (!has || n >= maxn)
36+
return -1;
37+
coords[n++] = cur;
38+
return (p[1] == '\0') ? n : -1;
39+
} else {
40+
return -1;
41+
}
42+
}
43+
return -1;
44+
}
45+
46+
int kvspaceCoordCmp(const char *a, const char *b) {
47+
int ia = kvspaceCoordIsCoord(a), ib = kvspaceCoordIsCoord(b);
48+
if (!ia && !ib)
49+
return strcmp(a, b);
50+
if (!ia)
51+
return 1; /* 非坐标段排后 */
52+
if (!ib)
53+
return -1;
54+
int64_t ca[8], cb[8];
55+
int na = kvspaceParseCoord(a, ca, 8);
56+
int nb = kvspaceParseCoord(b, cb, 8);
57+
if (na < 0 || nb < 0)
58+
return strcmp(a, b); /* 含小数/字符串坐标 → 字典序 */
59+
for (int i = 0; i < na && i < nb; i++) {
60+
if (ca[i] != cb[i])
61+
return ca[i] < cb[i] ? -1 : 1;
62+
}
63+
if (na != nb)
64+
return na < nb ? -1 : 1;
65+
return 0;
66+
}

‎src/coord.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef KVSPACE_COORD_H
2+
#define KVSPACE_COORD_H
3+
#include <stdint.h>
4+
int kvspaceCoordIsCoord(const char *name);
5+
int kvspaceParseCoord(const char *name, int64_t *coords, int maxn);
6+
int kvspaceCoordCmp(const char *a, const char *b);
7+
#endif

0 commit comments

Comments
 (0)