refactor(server): indexer & worker & sync perf (#15504)

This commit is contained in:
DarkSky
2026-08-21 08:11:37 +08:00
committed by GitHub
parent 8c9aad9a9b
commit c57004ea2c
259 changed files with 16530 additions and 17793 deletions
@@ -32,6 +32,20 @@ public extension JSValueContainer {
return doub
}
func getInt64Ensure(_ key: String) throws -> Int64 {
guard let value = getDouble(key), let integer = Int64(exactly: value) else {
throw RequestParamError.request(key: key)
}
return integer
}
func getUInt32Ensure(_ key: String) throws -> UInt32 {
guard let value = getDouble(key), let integer = UInt32(exactly: value) else {
throw RequestParamError.request(key: key)
}
return integer
}
func getBoolEnsure(_ key: String) throws -> Bool {
guard let bool = getBool(key) else {
throw RequestParamError.request(key: key)
@@ -37,13 +37,17 @@ public class NbStorePlugin: CAPPlugin, CAPBridgedPlugin {
CAPPluginMethod(name: "getBlobUploadedAt", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setBlobUploadedAt", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "crawlDocData", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsAddDocument", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsDeleteDocument", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsSearch", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsGetDocument", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsGetMatches", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsFlushIndex", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "ftsIndexVersion", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getDocIndexedClock", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setDocIndexedClock", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setDocIndexedClocks", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "clearDocIndexedClock", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexUpsert", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexDelete", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexSearch", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexAggregate", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexDeleteByQuery", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexFlush", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "indexVersion", returnType: CAPPluginReturnPromise),
]
@objc func connect(_ call: CAPPluginCall) {
@@ -585,138 +589,225 @@ public class NbStorePlugin: CAPPlugin, CAPBridgedPlugin {
}
}
@objc func ftsAddDocument(_ call: CAPPluginCall) {
@objc func getDocIndexedClock(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let indexName = try call.getStringEnsure("indexName")
let docId = try call.getStringEnsure("docId")
let text = try call.getStringEnsure("text")
guard let index = call.getBool("index") else {
call.reject("index is required", nil, nil)
let clock = try await docStoragePool.getDocIndexedClock(
universalId: try call.getStringEnsure("id"),
docId: try call.getStringEnsure("docId")
)
guard let clock else {
call.resolve()
return
}
try await docStoragePool.ftsAddDocument(
universalId: id,
indexName: indexName,
docId: docId,
text: text,
index: index
)
call.resolve()
call.resolve(indexedClockJson(clock))
} catch {
call.reject("Failed to add document to fts, \(error)", nil, error)
call.reject("Failed to get indexed clock, \(error)", nil, error)
}
}
}
@objc func ftsDeleteDocument(_ call: CAPPluginCall) {
@objc func setDocIndexedClock(_ call: CAPPluginCall) {
Task {
do {
let clock = DocIndexedClock(
docId: try call.getStringEnsure("docId"),
timestamp: try call.getInt64Ensure("indexedClock"),
indexerVersion: try call.getInt64Ensure("indexerVersion")
)
try await docStoragePool.setDocIndexedClock(universalId: try call.getStringEnsure("id"), clock: clock)
call.resolve()
} catch {
call.reject("Failed to set indexed clock, \(error)", nil, error)
}
}
}
@objc func setDocIndexedClocks(_ call: CAPPluginCall) {
Task {
do {
let clocks = try call.getArrayEnsure("clocks", JSObject.self).map { value in
guard
let docId = value["docId"] as? String,
let timestamp = value["timestamp"] as? Double,
let timestamp = Int64(exactly: timestamp),
let indexerVersion = value["indexerVersion"] as? Double,
let indexerVersion = Int64(exactly: indexerVersion)
else {
throw RequestParamError.request(key: "clocks")
}
return DocIndexedClock(
docId: docId,
timestamp: timestamp,
indexerVersion: indexerVersion
)
}
try await docStoragePool.setDocIndexedClocks(universalId: try call.getStringEnsure("id"), clocks: clocks)
call.resolve()
} catch {
call.reject("Failed to commit indexed clocks, \(error)", nil, error)
}
}
}
@objc func clearDocIndexedClock(_ call: CAPPluginCall) {
Task {
do {
try await docStoragePool.clearDocIndexedClock(
universalId: try call.getStringEnsure("id"),
docId: try call.getStringEnsure("docId")
)
call.resolve()
} catch {
call.reject("Failed to clear indexed clock, \(error)", nil, error)
}
}
}
@objc func indexUpsert(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let indexName = try call.getStringEnsure("indexName")
let docId = try call.getStringEnsure("docId")
try await docStoragePool.ftsDeleteDocument(
let table = try call.getStringEnsure("table")
let document = try jsonString(call, "document")
try await docStoragePool.indexUpsert(
universalId: id,
indexName: indexName,
table: table,
document: document
)
call.resolve()
} catch {
call.reject("Failed to upsert index document, \(error)", nil, error)
}
}
}
@objc func indexDelete(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let table = try call.getStringEnsure("table")
let docId = try call.getStringEnsure("docId")
try await docStoragePool.indexDelete(
universalId: id,
table: table,
docId: docId
)
call.resolve()
} catch {
call.reject("Failed to delete document from fts, \(error)", nil, error)
call.reject("Failed to delete index document, \(error)", nil, error)
}
}
}
@objc func ftsSearch(_ call: CAPPluginCall) {
@objc func indexSearch(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let indexName = try call.getStringEnsure("indexName")
let query = try call.getStringEnsure("query")
let results = try await docStoragePool.ftsSearch(
let table = try call.getStringEnsure("table")
let result = try await docStoragePool.indexSearch(
universalId: id,
indexName: indexName,
query: query
table: table,
query: try jsonString(call, "query"),
options: try jsonString(call, "options")
)
let mapped = results.map {
[
"id": $0.id,
"score": $0.score,
"terms": $0.terms,
] as [String: Any]
call.resolve(["total": result.total, "hits": result.hits.map(indexHitJson)])
} catch {
call.reject("Failed to search index, \(error)", nil, error)
}
}
}
@objc func indexAggregate(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let table = try call.getStringEnsure("table")
let result = try await docStoragePool.indexAggregate(
universalId: id,
table: table,
query: try jsonString(call, "query"),
field: try call.getStringEnsure("field"),
limit: try call.getUInt32Ensure("limit"),
offset: try call.getUInt32Ensure("offset"),
hits: try optionalJsonString(call, "hits")
)
let buckets = result.buckets.map { bucket in
["key": bucket.key, "count": bucket.count, "score": bucket.score, "hits": bucket.hits.map(indexHitJson)]
}
call.resolve(["results": mapped])
call.resolve(["total": result.total, "buckets": buckets])
} catch {
call.reject("Failed to search fts, \(error)", nil, error)
call.reject("Failed to aggregate index, \(error)", nil, error)
}
}
}
@objc func ftsGetDocument(_ call: CAPPluginCall) {
@objc func indexDeleteByQuery(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let indexName = try call.getStringEnsure("indexName")
let docId = try call.getStringEnsure("docId")
let text = try await docStoragePool.ftsGetDocument(
let deleted = try await docStoragePool.indexDeleteByQuery(
universalId: id,
indexName: indexName,
docId: docId
table: try call.getStringEnsure("table"),
query: try jsonString(call, "query")
)
call.resolve(["text": text ?? NSNull()])
call.resolve(["deleted": deleted])
} catch {
call.reject("Failed to get fts document, \(error)", nil, error)
call.reject("Failed to delete index documents, \(error)", nil, error)
}
}
}
@objc func ftsGetMatches(_ call: CAPPluginCall) {
@objc func indexFlush(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
let indexName = try call.getStringEnsure("indexName")
let docId = try call.getStringEnsure("docId")
let query = try call.getStringEnsure("query")
let matches = try await docStoragePool.ftsGetMatches(
universalId: id,
indexName: indexName,
docId: docId,
query: query
)
let mapped = matches.map {
[
"start": $0.start,
"end": $0.end,
]
}
call.resolve(["matches": mapped])
} catch {
call.reject("Failed to get fts matches, \(error)", nil, error)
}
}
}
@objc func ftsFlushIndex(_ call: CAPPluginCall) {
Task {
do {
let id = try call.getStringEnsure("id")
try await docStoragePool.ftsFlushIndex(universalId: id)
try await docStoragePool.indexFlush(universalId: id)
call.resolve()
} catch {
call.reject("Failed to flush fts index, \(error)", nil, error)
call.reject("Failed to flush index, \(error)", nil, error)
}
}
}
@objc func ftsIndexVersion(_ call: CAPPluginCall) {
@objc func indexVersion(_ call: CAPPluginCall) {
Task {
do {
let version = try await docStoragePool.ftsIndexVersion()
let version = try await docStoragePool.indexVersion()
call.resolve(["indexVersion": version])
} catch {
call.reject("Failed to get fts index version, \(error)", nil, error)
call.reject("Failed to get index version, \(error)", nil, error)
}
}
}
}
private func jsonString(_ call: CAPPluginCall, _ key: String) throws -> String {
guard let value = call.getObject(key) else { throw RequestParamError.request(key: key) }
return String(data: try JSONSerialization.data(withJSONObject: value), encoding: .utf8)!
}
private func optionalJsonString(_ call: CAPPluginCall, _ key: String) throws -> String? {
guard call.getObject(key) != nil else { return nil }
return try jsonString(call, key)
}
private func indexHitJson(_ hit: IndexHit) -> [String: Any] {
[
"id": hit.id,
"score": hit.score,
"fields": hit.fields.map { ["field": $0.field, "values": $0.values] },
"highlights": hit.highlights.map { highlight in
[
"field": highlight.field,
"values": highlight.values.map { value in
["valueIndex": value.valueIndex, "spans": value.spans.map { ["start": $0.start, "end": $0.end] }]
},
]
},
]
}
private func indexedClockJson(_ clock: DocIndexedClock) -> [String: Any] {
["docId": clock.docId, "timestamp": clock.timestamp, "indexerVersion": clock.indexerVersion]
}
File diff suppressed because it is too large Load Diff
@@ -266,6 +266,11 @@ void uniffi_affine_mobile_native_fn_free_docstoragepool(void*_Nonnull ptr, RustC
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_clear_clocks(void*_Nonnull ptr, RustBuffer universal_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_CLEAR_DOC_INDEXED_CLOCK
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_CLEAR_DOC_INDEXED_CLOCK
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_clear_doc_indexed_clock(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer doc_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_CONNECT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_CONNECT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_connect(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer path
@@ -291,41 +296,6 @@ uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_delete_doc(void*_N
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_disconnect(void*_Nonnull ptr, RustBuffer universal_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_ADD_DOCUMENT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_ADD_DOCUMENT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_add_document(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer index_name, RustBuffer doc_id, RustBuffer text, int8_t index
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_DELETE_DOCUMENT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_DELETE_DOCUMENT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_delete_document(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer index_name, RustBuffer doc_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_FLUSH_INDEX
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_FLUSH_INDEX
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_flush_index(void*_Nonnull ptr, RustBuffer universal_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_GET_DOCUMENT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_GET_DOCUMENT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_get_document(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer index_name, RustBuffer doc_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_GET_MATCHES
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_GET_MATCHES
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_get_matches(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer index_name, RustBuffer doc_id, RustBuffer query
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_INDEX_VERSION
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_INDEX_VERSION
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_index_version(void*_Nonnull ptr
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_SEARCH
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_FTS_SEARCH
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_fts_search(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer index_name, RustBuffer query
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_GET_BLOB
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_GET_BLOB
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_blob(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer key
@@ -346,6 +316,11 @@ uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_doc_clock(void
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_doc_clocks(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer after
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_GET_DOC_INDEXED_CLOCK
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_GET_DOC_INDEXED_CLOCK
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_doc_indexed_clock(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer doc_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_GET_DOC_SNAPSHOT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_GET_DOC_SNAPSHOT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_doc_snapshot(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer doc_id
@@ -386,6 +361,41 @@ uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_peer_remote_cl
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_get_peer_remote_clocks(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer peer
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_AGGREGATE
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_AGGREGATE
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_aggregate(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer table, RustBuffer query, RustBuffer field, uint32_t limit, uint32_t offset, RustBuffer hits
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_DELETE
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_DELETE
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_delete(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer table, RustBuffer doc_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_DELETE_BY_QUERY
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_DELETE_BY_QUERY
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_delete_by_query(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer table, RustBuffer query
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_FLUSH
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_FLUSH
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_flush(void*_Nonnull ptr, RustBuffer universal_id
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_SEARCH
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_SEARCH
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_search(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer table, RustBuffer query, RustBuffer options
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_UPSERT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_UPSERT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_upsert(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer table, RustBuffer document
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_VERSION
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_INDEX_VERSION
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_index_version(void*_Nonnull ptr
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_LIST_BLOBS
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_LIST_BLOBS
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_list_blobs(void*_Nonnull ptr, RustBuffer universal_id
@@ -416,6 +426,16 @@ uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_set_blob(void*_Non
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_set_blob_uploaded_at(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer peer, RustBuffer blob_id, RustBuffer uploaded_at
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCK
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCK
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_set_doc_indexed_clock(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer clock
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCKS
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCKS
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_set_doc_indexed_clocks(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer clocks
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_SET_DOC_SNAPSHOT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_FN_METHOD_DOCSTORAGEPOOL_SET_DOC_SNAPSHOT
uint64_t uniffi_affine_mobile_native_fn_method_docstoragepool_set_doc_snapshot(void*_Nonnull ptr, RustBuffer universal_id, RustBuffer snapshot
@@ -770,6 +790,12 @@ uint16_t uniffi_affine_mobile_native_checksum_func_render_typst_preview_svg(void
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_CLEAR_CLOCKS
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_clear_clocks(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_CLEAR_DOC_INDEXED_CLOCK
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_CLEAR_DOC_INDEXED_CLOCK
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_clear_doc_indexed_clock(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_CONNECT
@@ -800,48 +826,6 @@ uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_delete_doc(v
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_DISCONNECT
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_disconnect(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_ADD_DOCUMENT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_ADD_DOCUMENT
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_add_document(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_DELETE_DOCUMENT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_DELETE_DOCUMENT
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_delete_document(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_FLUSH_INDEX
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_FLUSH_INDEX
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_flush_index(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_GET_DOCUMENT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_GET_DOCUMENT
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_get_document(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_GET_MATCHES
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_GET_MATCHES
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_get_matches(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_INDEX_VERSION
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_INDEX_VERSION
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_index_version(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_SEARCH
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_FTS_SEARCH
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_fts_search(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_GET_BLOB
@@ -866,6 +850,12 @@ uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_get_doc_cloc
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_GET_DOC_CLOCKS
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_get_doc_clocks(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_GET_DOC_INDEXED_CLOCK
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_GET_DOC_INDEXED_CLOCK
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_get_doc_indexed_clock(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_GET_DOC_SNAPSHOT
@@ -914,6 +904,48 @@ uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_get_peer_rem
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_GET_PEER_REMOTE_CLOCKS
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_get_peer_remote_clocks(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_AGGREGATE
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_AGGREGATE
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_aggregate(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_DELETE
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_DELETE
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_delete(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_DELETE_BY_QUERY
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_DELETE_BY_QUERY
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_delete_by_query(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_FLUSH
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_FLUSH
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_flush(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_SEARCH
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_SEARCH
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_search(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_UPSERT
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_UPSERT
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_upsert(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_VERSION
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_INDEX_VERSION
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_index_version(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_LIST_BLOBS
@@ -950,6 +982,18 @@ uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_set_blob(voi
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_SET_BLOB_UPLOADED_AT
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_set_blob_uploaded_at(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCK
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCK
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_set_doc_indexed_clock(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCKS
#define UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_SET_DOC_INDEXED_CLOCKS
uint16_t uniffi_affine_mobile_native_checksum_method_docstoragepool_set_doc_indexed_clocks(void
);
#endif
#ifndef UNIFFI_FFIDEF_UNIFFI_AFFINE_MOBILE_NATIVE_CHECKSUM_METHOD_DOCSTORAGEPOOL_SET_DOC_SNAPSHOT
@@ -1,4 +1,22 @@
import type { CrawlResult, DocIndexedClock } from '@affine/nbstore';
import type {
NativeIndexField,
NativeIndexHit,
NativeIndexQuery,
NativeIndexSearchOptions,
NativeIndexSearchResult,
} from '@affine/nbstore/sqlite';
type NativeIndexDocument = { id: string; fields: NativeIndexField[] };
type NativeIndexAggregateResult = {
total: number;
buckets: {
key: string;
count: number;
score: number;
hits: NativeIndexHit[];
}[];
};
export interface Blob {
key: string;
@@ -157,38 +175,38 @@ export interface NbStorePlugin {
id: string;
docId: string;
}) => Promise<CrawlResult>;
ftsAddDocument: (options: {
indexUpsert: (options: {
id: string;
indexName: string;
docId: string;
text: string;
index: boolean;
table: string;
document: NativeIndexDocument;
}) => Promise<void>;
ftsDeleteDocument: (options: {
indexDelete: (options: {
id: string;
indexName: string;
table: string;
docId: string;
}) => Promise<void>;
ftsSearch: (options: {
indexSearch: (options: {
id: string;
indexName: string;
query: string;
}) => Promise<{
results: { id: string; score: number; terms: Array<string> }[];
}>;
ftsGetDocument: (options: {
table: string;
query: NativeIndexQuery;
options: NativeIndexSearchOptions;
}) => Promise<NativeIndexSearchResult>;
indexAggregate: (options: {
id: string;
indexName: string;
docId: string;
}) => Promise<{ text?: string | null }>;
ftsGetMatches: (options: {
table: string;
query: NativeIndexQuery;
field: string;
limit: number;
offset: number;
hits?: NativeIndexSearchOptions;
}) => Promise<NativeIndexAggregateResult>;
indexDeleteByQuery: (options: {
id: string;
indexName: string;
docId: string;
query: string;
}) => Promise<{ matches: { start: number; end: number }[] }>;
ftsFlushIndex: (options: { id: string }) => Promise<void>;
ftsIndexVersion: () => Promise<{ indexVersion: number }>;
table: string;
query: NativeIndexQuery;
}) => Promise<{ deleted: number }>;
indexFlush: (options: { id: string }) => Promise<void>;
indexVersion: () => Promise<{ indexVersion: number }>;
getDocIndexedClock: (options: {
id: string;
docId: string;
@@ -199,6 +217,10 @@ export interface NbStorePlugin {
indexedClock: number;
indexerVersion: number;
}) => Promise<void>;
setDocIndexedClocks: (options: {
id: string;
clocks: Array<{ docId: string; timestamp: number; indexerVersion: number }>;
}) => Promise<void>;
clearDocIndexedClock: (options: {
id: string;
docId: string;
@@ -2,7 +2,6 @@ import {
base64ToUint8Array,
uint8ArrayToBase64,
} from '@affine/core/modules/workspace-engine';
import { normalizeNativeOptional } from '@affine/mobile-shared/nbstore/optional';
import {
decodePayload,
MOBILE_BLOB_FILE_PREFIX,
@@ -358,77 +357,74 @@ export const NbStoreNativeDBApis: NativeDBApis = {
): Promise<CrawlResult> {
return await NbStore.crawlDocData({ id, docId });
},
ftsAddDocument: async function (
indexUpsert: async function (
id: string,
indexName: string,
docId: string,
text: string,
index: boolean
table: string,
document
): Promise<void> {
await NbStore.ftsAddDocument({
await NbStore.indexUpsert({
id,
indexName,
docId,
text,
index,
table,
document,
});
},
ftsDeleteDocument: async function (
indexDelete: async function (
id: string,
indexName: string,
table: string,
docId: string
): Promise<void> {
await NbStore.ftsDeleteDocument({
await NbStore.indexDelete({
id,
indexName,
table,
docId,
});
},
ftsSearch: async function (
id: string,
indexName: string,
query: string
): Promise<{ id: string; score: number; terms: Array<string> }[]> {
const { results } = await NbStore.ftsSearch({
indexSearch: async function (id: string, table: string, query, options) {
return await NbStore.indexSearch({
id,
indexName,
table,
query,
options,
});
},
indexAggregate: async function (
id: string,
table: string,
query,
field: string,
limit: number,
offset: number,
hits
) {
return await NbStore.indexAggregate({
id,
table,
query,
field,
limit,
offset,
hits,
});
},
indexDeleteByQuery: async function (
id: string,
table: string,
query
): Promise<number> {
const { deleted } = await NbStore.indexDeleteByQuery({
id,
table,
query,
});
return results ?? [];
return deleted;
},
ftsGetDocument: async function (
id: string,
indexName: string,
docId: string
): Promise<string | null> {
const result = await NbStore.ftsGetDocument({
id,
indexName,
docId,
});
return normalizeNativeOptional(result.text);
},
ftsGetMatches: async function (
id: string,
indexName: string,
docId: string,
query: string
): Promise<{ start: number; end: number }[]> {
const { matches } = await NbStore.ftsGetMatches({
id,
indexName,
docId,
query,
});
return matches ?? [];
},
ftsFlushIndex: async function (id: string): Promise<void> {
await NbStore.ftsFlushIndex({
indexFlush: async function (id: string): Promise<void> {
await NbStore.indexFlush({
id,
});
},
ftsIndexVersion: function (): Promise<number> {
return NbStore.ftsIndexVersion().then(res => res.indexVersion);
indexVersion: function (): Promise<number> {
return NbStore.indexVersion().then(res => res.indexVersion);
},
getDocIndexedClock: function (
id: string,
@@ -451,6 +447,15 @@ export const NbStoreNativeDBApis: NativeDBApis = {
indexerVersion,
});
},
setDocIndexedClocks: function (id, clocks): Promise<void> {
return NbStore.setDocIndexedClocks({
id,
clocks: clocks.map(clock => ({
...clock,
timestamp: clock.timestamp.getTime(),
})),
});
},
clearDocIndexedClock: function (id: string, docId: string): Promise<void> {
return NbStore.clearDocIndexedClock({
id,