Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ set(CMAKE_C_EXTENSIONS OFF)
include(GNUInstallDirs)

add_library(kvspace-c SHARED
src/xvalue.c
src/coord.c
src/xvalue_head.c
src/xvalue_meta.c
src/kvspace.c
src/durable_abi.c
)
Expand All @@ -19,7 +21,7 @@ set_target_properties(kvspace-c PROPERTIES
SOVERSION 1
)

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

Expand Down Expand Up @@ -54,4 +56,12 @@ if(KVSPACE_BUILD_TESTS)
target_include_directories(test_handle_cache PRIVATE src)
target_link_libraries(test_handle_cache PRIVATE kvspace-c)
add_test(NAME handle_cache COMMAND test_handle_cache)

add_executable(test_xvalue_head tests/test_xvalue_head.c src/xvalue_head.c src/xvalue_meta.c)
target_include_directories(test_xvalue_head PRIVATE src)
add_test(NAME xvalue_head COMMAND test_xvalue_head)
add_executable(test_new_wire_shm tests/test_new_wire_shm.c)
target_include_directories(test_new_wire_shm PRIVATE src)
target_link_libraries(test_new_wire_shm PRIVATE kvspace-c)
add_test(NAME new_wire_shm COMMAND test_new_wire_shm)
endif()
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ Two surfaces:

## XValue

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

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

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

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

## Tutorial

Expand Down
66 changes: 66 additions & 0 deletions src/coord.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "coord.h"
#include <stdbool.h>
#include <string.h>

int kvspaceCoordIsCoord(const char *name) {
if (!name || name[0] != '[')
return 0;
size_t n = strlen(name);
if (n < 3 || name[n - 1] != ']')
return 0;
for (size_t i = 1; i + 1 < n; i++)
if (name[i] == '[' || name[i] == ']')
return 0;
return 1;
}

/* Parse integer coordinates; return -1 on mismatch. */
int kvspaceParseCoord(const char *name, int64_t *coords, int maxn) {
if (!name || name[0] != '[')
return -1;
int n = 0;
int64_t cur = 0;
bool has = false;
for (const char *p = name + 1; *p; p++) {
if (*p >= '0' && *p <= '9') {
cur = cur * 10 + (*p - '0');
has = true;
} else if (*p == ',') {
if (!has || n >= maxn)
return -1;
coords[n++] = cur;
cur = 0;
has = false;
} else if (*p == ']') {
if (!has || n >= maxn)
return -1;
coords[n++] = cur;
return (p[1] == '\0') ? n : -1;
} else {
return -1;
}
}
return -1;
}

int kvspaceCoordCmp(const char *a, const char *b) {
int ia = kvspaceCoordIsCoord(a), ib = kvspaceCoordIsCoord(b);
if (!ia && !ib)
return strcmp(a, b);
if (!ia)
return 1; /* 非坐标段排后 */
if (!ib)
return -1;
int64_t ca[8], cb[8];
int na = kvspaceParseCoord(a, ca, 8);
int nb = kvspaceParseCoord(b, cb, 8);
if (na < 0 || nb < 0)
return strcmp(a, b); /* 含小数/字符串坐标 → 字典序 */
for (int i = 0; i < na && i < nb; i++) {
if (ca[i] != cb[i])
return ca[i] < cb[i] ? -1 : 1;
}
if (na != nb)
return na < nb ? -1 : 1;
return 0;
}
7 changes: 7 additions & 0 deletions src/coord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef KVSPACE_COORD_H
#define KVSPACE_COORD_H
#include <stdint.h>
int kvspaceCoordIsCoord(const char *name);
int kvspaceParseCoord(const char *name, int64_t *coords, int maxn);
int kvspaceCoordCmp(const char *a, const char *b);
#endif
Loading
Loading