-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeather_core.cpp
More file actions
110 lines (94 loc) · 4.22 KB
/
feather_core.cpp
File metadata and controls
110 lines (94 loc) · 4.22 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
#include "../include/feather.h"
#include <vector>
#include <memory>
extern "C" {
void* feather_open(const char* path, size_t dim) {
try {
auto db = feather::DB::open(path, dim);
return new std::unique_ptr<feather::DB>(std::move(db));
} catch (...) { return nullptr; }
}
void feather_add(void* db_ptr, uint64_t id, const float* vec, size_t len) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
db->add(id, std::vector<float>(vec, vec + len), feather::Metadata(), "text");
}
void feather_add_with_meta(void* db_ptr, uint64_t id, const float* vec, size_t len,
int64_t timestamp, float importance, uint8_t type,
const char* source, const char* content, const char* modality) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
feather::Metadata meta;
meta.timestamp = timestamp;
meta.importance = importance;
meta.type = static_cast<feather::ContextType>(type);
if (source) meta.source = source;
if (content) meta.content = content;
std::string mod = modality ? modality : "text";
db->add(id, std::vector<float>(vec, vec + len), meta, mod);
}
void feather_link(void* db_ptr, uint64_t from_id, uint64_t to_id) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
db->link(from_id, to_id);
}
void feather_touch(void* db_ptr, uint64_t id) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
db->touch(id);
}
void feather_search(void* db_ptr, const float* query, size_t len, size_t k,
uint64_t* out_ids, float* out_dists, const char* modality) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
std::string mod = modality ? modality : "text";
auto results = db->search(std::vector<float>(query, query + len), k, nullptr, nullptr, mod);
for (size_t i = 0; i < results.size() && i < k; ++i) {
out_ids[i] = results[i].id;
out_dists[i] = results[i].score;
}
}
void feather_search_with_filter(void* db_ptr, const float* query, size_t len, size_t k,
uint8_t type_filter, const char* source_filter,
uint64_t* out_ids, float* out_dists, const char* modality) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
feather::SearchFilter filter;
if (type_filter != 255) { // 255 = no filter
filter.types = std::vector<feather::ContextType>{static_cast<feather::ContextType>(type_filter)};
}
if (source_filter && strlen(source_filter) > 0) {
filter.source = source_filter;
}
std::string mod = modality ? modality : "text";
auto results = db->search(std::vector<float>(query, query + len), k, &filter, nullptr, mod);
for (size_t i = 0; i < results.size() && i < k; ++i) {
out_ids[i] = results[i].id;
out_dists[i] = results[i].score;
}
}
void feather_save(void* db_ptr) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
db->save();
}
void feather_close(void* db_ptr) {
if (db_ptr) delete static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
}
// Phase 6: memory lifecycle
void feather_forget(void* db_ptr, uint64_t id) {
if (!db_ptr) return;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
db->forget(id);
}
size_t feather_purge(void* db_ptr, const char* namespace_id) {
if (!db_ptr || !namespace_id) return 0;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
return db->purge(namespace_id);
}
size_t feather_forget_expired(void* db_ptr) {
if (!db_ptr) return 0;
auto& db = *static_cast<std::unique_ptr<feather::DB>*>(db_ptr);
return db->forget_expired();
}
}