@peerbit/string 4.1.26-efee9d3 → 5.0.0-2bc15a6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/string-index.d.ts.map +1 -1
- package/dist/src/string-index.js +2 -2
- package/dist/src/string-index.js.map +1 -1
- package/dist/src/string-store.d.ts +6 -6
- package/dist/src/string-store.d.ts.map +1 -1
- package/dist/src/string-store.js +23 -23
- package/dist/src/string-store.js.map +1 -1
- package/package.json +11 -10
- package/src/string-index.ts +4 -4
- package/src/string-store.ts +38 -38
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-index.d.ts","sourceRoot":"","sources":["../../src/string-index.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,KAAK,MAAM,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,qBACa,eAAe;IAE3B,KAAK,EAAE,KAAK,CAAC;IAGb,KAAK,CAAC,EAAE,MAAM,CAAC;gBAEH,KAAK,CAAC,EAAE;QAAE,KAAK,EAAE,KAAK,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;CAMpD;AACD,eAAO,MAAM,QAAQ,kDAAkC,CAAC;AAExD,qBACa,WAAY,SAAQ,OAAO;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,GAAG,CAAC,eAAe,CAAC,CAAC;;IAM3B,IAAI,MAAM,IAAI,MAAM,CAEnB;IACK,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,eAAe,CAAC;IAItC,KAAK,CAAC,MAAM,EAAE,GAAG;IAMX,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,eAAe,CAAC;CAGlD;AAED,eAAO,MAAM,eAAe,WACnB,MAAM,cACF,
|
|
1
|
+
{"version":3,"file":"string-index.d.ts","sourceRoot":"","sources":["../../src/string-index.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,KAAK,MAAM,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,qBACa,eAAe;IAE3B,KAAK,EAAE,KAAK,CAAC;IAGb,KAAK,CAAC,EAAE,MAAM,CAAC;gBAEH,KAAK,CAAC,EAAE;QAAE,KAAK,EAAE,KAAK,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;CAMpD;AACD,eAAO,MAAM,QAAQ,kDAAkC,CAAC;AAExD,qBACa,WAAY,SAAQ,OAAO;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,GAAG,CAAC,eAAe,CAAC,CAAC;;IAM3B,IAAI,MAAM,IAAI,MAAM,CAEnB;IACK,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,eAAe,CAAC;IAItC,KAAK,CAAC,MAAM,EAAE,GAAG;IAMX,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,eAAe,CAAC;CAGlD;AAED,eAAO,MAAM,eAAe,WACnB,MAAM,cACF,KAAK,CAAC,eAAe,CAAC,EAAE,KAClC,OAAO,CAAC,MAAM,CAYhB,CAAC;AACF,eAAO,MAAM,cAAc,MACvB,MAAM,aACE,eAAe,KACxB,MAcF,CAAC"}
|
package/dist/src/string-index.js
CHANGED
|
@@ -55,7 +55,7 @@ let StringIndex = class StringIndex extends Program {
|
|
|
55
55
|
return super.close(parent);
|
|
56
56
|
}
|
|
57
57
|
async updateIndex(_change) {
|
|
58
|
-
this._string = await applyOperations("", await this._log.
|
|
58
|
+
this._string = await applyOperations("", await this._log.toArray()); // TODO improve performance
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
StringIndex = __decorate([
|
|
@@ -77,7 +77,7 @@ export const applyOperations = async (string, operations) => {
|
|
|
77
77
|
export const applyOperation = (s, operation) => {
|
|
78
78
|
// TODO check bounds number
|
|
79
79
|
const to = Number(operation.index.offset) + Number(operation.index.length);
|
|
80
|
-
if (operation.value
|
|
80
|
+
if (operation.value !== undefined) {
|
|
81
81
|
s = s.padEnd(to);
|
|
82
82
|
s =
|
|
83
83
|
s.slice(0, Number(operation.index.offset)) +
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-index.js","sourceRoot":"","sources":["../../src/string-index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAe,GAAG,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAG5B,IAAM,eAAe,GAArB,MAAM,eAAe;IAE3B,KAAK,CAAQ;IAGb,KAAK,CAAU;IAEf,YAAY,KAAwC;QACnD,IAAI,KAAK,EAAE,CAAC;YACX,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;YACzB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QAC1B,CAAC;IACF,CAAC;CACD,CAAA;AAXA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;8BAChB,KAAK;8CAAC;AAGb;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;;8CACnB;AALH,eAAe;IAD3B,OAAO,CAAC,CAAC,CAAC;;GACE,eAAe,CAa3B;;AACD,MAAM,CAAC,MAAM,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C,EAAE,CAAC,CAAC;IAC9D,CAAC;IACD,OAAO,CAAC,CAAC;AACV,CAAC,CAAC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { type AppendOptions, type Change, Entry } from "@peerbit/log";
|
|
2
|
-
import { SharedLog, type SharedLogOptions } from "@peerbit/shared-log";
|
|
3
1
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
2
|
+
import { type AppendOptions, type Change, Entry } from "@peerbit/log";
|
|
4
3
|
import { Program, type ProgramEvents } from "@peerbit/program";
|
|
5
|
-
import { type RPCRequestOptions,
|
|
6
|
-
import {
|
|
4
|
+
import { RPC, type RPCRequestOptions, type RequestContext } from "@peerbit/rpc";
|
|
5
|
+
import { SharedLog, type SharedLogOptions } from "@peerbit/shared-log";
|
|
7
6
|
import { AbstractSearchResult, SearchRequest } from "./query.js";
|
|
8
7
|
import { Range } from "./range.js";
|
|
8
|
+
import { StringIndex, StringOperation } from "./string-index.js";
|
|
9
9
|
export declare const STRING_STORE_TYPE = "string_store";
|
|
10
10
|
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
11
11
|
export interface StringEvents {
|
|
@@ -33,11 +33,11 @@ export declare class DString extends Program<Args, StringEvents & ProgramEvents>
|
|
|
33
33
|
private _canPerform;
|
|
34
34
|
add(value: string, index: Range, options?: AppendOptions<StringOperation>): Promise<{
|
|
35
35
|
entry: Entry<StringOperation>;
|
|
36
|
-
removed:
|
|
36
|
+
removed: import("@peerbit/log").ShallowOrFullEntry<StringOperation>[];
|
|
37
37
|
}>;
|
|
38
38
|
del(index: Range, options?: AppendOptions<StringOperation>): Promise<{
|
|
39
39
|
entry: Entry<StringOperation>;
|
|
40
|
-
removed:
|
|
40
|
+
removed: import("@peerbit/log").ShallowOrFullEntry<StringOperation>[];
|
|
41
41
|
}>;
|
|
42
42
|
queryHandler(query: SearchRequest, ctx: RequestContext): Promise<AbstractSearchResult | undefined>;
|
|
43
43
|
getValue(options?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-store.d.ts","sourceRoot":"","sources":["../../src/string-store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"string-store.d.ts","sourceRoot":"","sources":["../../src/string-store.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAoB,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,GAAG,EAAE,KAAK,iBAAiB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvE,OAAO,EACN,oBAAoB,EAIpB,aAAa,EAGb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAY,MAAM,mBAAmB,CAAC;AAI3E,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAehD,MAAM,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;AAE1E,MAAM,WAAW,YAAY;IAC5B,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC;CAC7C;AAED,MAAM,MAAM,UAAU,GAAG,CACxB,SAAS,EAAE,eAAe,EAC1B,OAAO,EAAE,kBAAkB,KACvB,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;AAEhC,MAAM,MAAM,IAAI,GAAG;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,GAAG,CAAC,EAAE,gBAAgB,CAAC,eAAe,CAAC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAChC,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC;CAC9B,CAAC;AACF,qBACa,OAAQ,SAAQ,OAAO,CAAC,IAAI,EAAE,YAAY,GAAG,aAAa,CAAC;IAEvE,IAAI,EAAE,SAAS,CAAC,eAAe,CAAC,CAAC;IAGjC,KAAK,EAAE,GAAG,CAAC,aAAa,EAAE,oBAAoB,CAAC,CAAC;IAGhD,MAAM,EAAE,WAAW,CAAC;IAEpB,QAAQ,CAAC,EAAE,OAAO,CAAC;gBAEP,UAAU,EAAE;QACvB,EAAE,CAAC,EAAE,UAAU,CAAC;QAChB,KAAK,CAAC,EAAE,GAAG,CAAC,aAAa,EAAE,oBAAoB,CAAC,CAAC;KACjD;IAOK,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI;YAyCX,WAAW;IAgBnB,GAAG,CACR,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,KAAK,EACZ,OAAO,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC;;;;IAcnC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC;;;;IAI1D,YAAY,CACjB,KAAK,EAAE,aAAa,EACpB,GAAG,EAAE,cAAc,GACjB,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC;IAkDtC,QAAQ,CAAC,OAAO,CAAC,EAAE;QACxB,MAAM,EAAE;YACP,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,GAAG,CAAC;YAClC,YAAY,EAAE,iBAAiB,CAAC,oBAAoB,CAAC,CAAC;SACtD,CAAC;KACF,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;CAqC/B"}
|
package/dist/src/string-store.js
CHANGED
|
@@ -8,17 +8,17 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { field, variant } from "@dao-xyz/borsh";
|
|
11
|
-
import {
|
|
12
|
-
import { SharedLog } from "@peerbit/shared-log";
|
|
11
|
+
import { CustomEvent } from "@libp2p/interface";
|
|
13
12
|
import { PublicSignKey, sha256Base64Sync } from "@peerbit/crypto";
|
|
13
|
+
import { Entry } from "@peerbit/log";
|
|
14
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
14
15
|
import { Program } from "@peerbit/program";
|
|
15
16
|
import { RPC } from "@peerbit/rpc";
|
|
16
|
-
import {
|
|
17
|
-
import { StringOperation, StringIndex, encoding } from "./string-index.js";
|
|
18
|
-
import { AbstractSearchResult, NoAccess, RangeMetadata, RangeMetadatas, StringMatch, SearchRequest, StringResult } from "./query.js";
|
|
19
|
-
import { CustomEvent } from "@libp2p/interface";
|
|
17
|
+
import { SharedLog } from "@peerbit/shared-log";
|
|
20
18
|
import { concat, fromString } from "uint8arrays";
|
|
19
|
+
import { AbstractSearchResult, NoAccess, RangeMetadata, RangeMetadatas, SearchRequest, StringMatch, StringResult, } from "./query.js";
|
|
21
20
|
import { Range } from "./range.js";
|
|
21
|
+
import { StringIndex, StringOperation, encoding } from "./string-index.js";
|
|
22
22
|
const logger = loggerFn({ module: "string" });
|
|
23
23
|
export const STRING_STORE_TYPE = "string_store";
|
|
24
24
|
const findAllOccurrences = (str, substr) => {
|
|
@@ -47,7 +47,7 @@ let DString = class DString extends Program {
|
|
|
47
47
|
await this._log.open({
|
|
48
48
|
encoding,
|
|
49
49
|
replicas: {
|
|
50
|
-
min: 0xffffffff // assume a document can not be sharded?
|
|
50
|
+
min: 0xffffffff, // assume a document can not be sharded?
|
|
51
51
|
},
|
|
52
52
|
canAppend: async (entry) => {
|
|
53
53
|
const operation = await entry.getPayloadValue();
|
|
@@ -61,17 +61,18 @@ let DString = class DString extends Program {
|
|
|
61
61
|
onChange: async (change) => {
|
|
62
62
|
await this._index.updateIndex(change);
|
|
63
63
|
this.events.dispatchEvent(new CustomEvent("change", {
|
|
64
|
-
detail: change
|
|
64
|
+
detail: change,
|
|
65
65
|
}));
|
|
66
|
-
}
|
|
66
|
+
},
|
|
67
67
|
});
|
|
68
68
|
await this.query.open({
|
|
69
69
|
...options,
|
|
70
70
|
topic: sha256Base64Sync(concat([this._log.log.id, fromString("/dstring")])),
|
|
71
71
|
responseHandler: this.queryHandler.bind(this),
|
|
72
72
|
queryType: SearchRequest,
|
|
73
|
-
responseType: StringResult
|
|
73
|
+
responseType: StringResult,
|
|
74
74
|
});
|
|
75
|
+
await this._log.reload(); // TODO make it so that it does not reload the whole log (i.e. persist the generated string in some way)
|
|
75
76
|
}
|
|
76
77
|
async _canPerform(operation, context) {
|
|
77
78
|
if (this._log.log.length === 0 || context.entry.next.length === 0) {
|
|
@@ -79,7 +80,7 @@ let DString = class DString extends Program {
|
|
|
79
80
|
}
|
|
80
81
|
else {
|
|
81
82
|
for (const next of context.entry.next) {
|
|
82
|
-
if (this._log.log.has(next)) {
|
|
83
|
+
if (await this._log.log.has(next)) {
|
|
83
84
|
return true;
|
|
84
85
|
}
|
|
85
86
|
}
|
|
@@ -89,10 +90,10 @@ let DString = class DString extends Program {
|
|
|
89
90
|
async add(value, index, options) {
|
|
90
91
|
return this._log.append(new StringOperation({
|
|
91
92
|
index,
|
|
92
|
-
value
|
|
93
|
+
value,
|
|
93
94
|
}), {
|
|
94
95
|
...options,
|
|
95
|
-
meta: { ...options?.meta, next: await this._log.log.getHeads() }
|
|
96
|
+
meta: { ...options?.meta, next: await this._log.log.getHeads().all() }, // TODO: optimize
|
|
96
97
|
});
|
|
97
98
|
}
|
|
98
99
|
async del(index, options) {
|
|
@@ -100,7 +101,7 @@ let DString = class DString extends Program {
|
|
|
100
101
|
}
|
|
101
102
|
async queryHandler(query, ctx) {
|
|
102
103
|
logger.debug("Recieved query");
|
|
103
|
-
if (query instanceof SearchRequest
|
|
104
|
+
if (query instanceof SearchRequest === false) {
|
|
104
105
|
logger.debug("Recieved query which is not a StringQueryRequest");
|
|
105
106
|
return;
|
|
106
107
|
}
|
|
@@ -110,10 +111,10 @@ let DString = class DString extends Program {
|
|
|
110
111
|
}
|
|
111
112
|
const content = this._index.string;
|
|
112
113
|
const relaventQueries = stringQuery.query.filter((x) => x instanceof StringMatch);
|
|
113
|
-
if (relaventQueries.length
|
|
114
|
+
if (relaventQueries.length === 0) {
|
|
114
115
|
logger.debug("Responding with all");
|
|
115
116
|
return new StringResult({
|
|
116
|
-
string: content
|
|
117
|
+
string: content,
|
|
117
118
|
});
|
|
118
119
|
}
|
|
119
120
|
const ranges = relaventQueries
|
|
@@ -122,27 +123,27 @@ let DString = class DString extends Program {
|
|
|
122
123
|
return occurances.map((ix) => {
|
|
123
124
|
return new RangeMetadata({
|
|
124
125
|
offset: BigInt(ix),
|
|
125
|
-
length: BigInt(query.value.length)
|
|
126
|
+
length: BigInt(query.value.length),
|
|
126
127
|
});
|
|
127
128
|
});
|
|
128
129
|
})
|
|
129
130
|
.flat(1);
|
|
130
|
-
if (ranges.length
|
|
131
|
+
if (ranges.length === 0) {
|
|
131
132
|
logger.debug("Could not find any matches");
|
|
132
133
|
return;
|
|
133
134
|
}
|
|
134
135
|
return new StringResult({
|
|
135
136
|
string: content,
|
|
136
137
|
metadatas: new RangeMetadatas({
|
|
137
|
-
metadatas: ranges
|
|
138
|
-
})
|
|
138
|
+
metadatas: ranges,
|
|
139
|
+
}),
|
|
139
140
|
});
|
|
140
141
|
}
|
|
141
142
|
async getValue(options) {
|
|
142
143
|
if (options?.remote) {
|
|
143
144
|
const counter = new Map();
|
|
144
145
|
const responses = await this.query.request(new SearchRequest({
|
|
145
|
-
query: []
|
|
146
|
+
query: [],
|
|
146
147
|
}), options.remote.queryOptions);
|
|
147
148
|
for (const response of responses) {
|
|
148
149
|
if (response.response instanceof NoAccess) {
|
|
@@ -150,8 +151,7 @@ let DString = class DString extends Program {
|
|
|
150
151
|
continue;
|
|
151
152
|
}
|
|
152
153
|
else if (response.response instanceof StringResult) {
|
|
153
|
-
options?.remote.callback
|
|
154
|
-
options?.remote.callback(response.response.string);
|
|
154
|
+
options?.remote.callback?.(response.response.string);
|
|
155
155
|
counter.set(response.response.string, (counter.get(response.response.string) || 0) + 1);
|
|
156
156
|
}
|
|
157
157
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-store.js","sourceRoot":"","sources":["../../src/string-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,
|
|
1
|
+
{"version":3,"file":"string-store.js","sourceRoot":"","sources":["../../src/string-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAmC,KAAK,EAAE,MAAM,cAAc,CAAC;AACtE,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAsB,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,GAAG,EAA+C,MAAM,cAAc,CAAC;AAChF,OAAO,EAAE,SAAS,EAAyB,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EACN,oBAAoB,EACpB,QAAQ,EACR,aAAa,EACb,cAAc,EACd,aAAa,EACb,WAAW,EACX,YAAY,GACZ,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE3E,MAAM,MAAM,GAAG,QAAQ,CA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O,GAAG,CAAC;QACZ,CAAC;aAAM,CAAC;YACP,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QAC3B,CAAC;IACF,CAAC;CACD,CAAA;AAjMA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;8BACrB,SAAS;qCAAkB;AAGjC;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;8BACd,GAAG;sCAAsC;AAGhD;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;8BACrB,WAAW;uCAAC;AARR,OAAO;IADnB,OAAO,CAAC,SAAS,CAAC;;GACN,OAAO,CAmMnB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/string",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-2bc15a6",
|
|
4
4
|
"description": "String store",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"eslintConfig": {
|
|
38
|
-
"extends": "
|
|
38
|
+
"extends": "peerbit",
|
|
39
39
|
"parserOptions": {
|
|
40
40
|
"project": true,
|
|
41
41
|
"sourceType": "module"
|
|
@@ -52,19 +52,20 @@
|
|
|
52
52
|
"scripts": {
|
|
53
53
|
"clean": "aegir clean",
|
|
54
54
|
"build": "aegir build --no-bundle",
|
|
55
|
-
"test": "aegir test -t node"
|
|
55
|
+
"test": "aegir test -t node",
|
|
56
|
+
"lint": "aegir lint"
|
|
56
57
|
},
|
|
57
58
|
"author": "dao.xyz",
|
|
58
59
|
"license": "MIT",
|
|
59
60
|
"dependencies": {
|
|
60
|
-
"@dao-xyz/borsh": "^5.2.
|
|
61
|
-
"@peerbit/logger": "1.0.
|
|
62
|
-
"@peerbit/program": "
|
|
63
|
-
"@peerbit/shared-log": "
|
|
64
|
-
"@peerbit/rpc": "
|
|
65
|
-
"@peerbit/time": "2.0.
|
|
61
|
+
"@dao-xyz/borsh": "^5.2.3",
|
|
62
|
+
"@peerbit/logger": "1.0.3-2bc15a6",
|
|
63
|
+
"@peerbit/program": "5.0.0-2bc15a6",
|
|
64
|
+
"@peerbit/shared-log": "9.0.0-2bc15a6",
|
|
65
|
+
"@peerbit/rpc": "5.0.0-2bc15a6",
|
|
66
|
+
"@peerbit/time": "2.0.7-2bc15a6"
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
68
|
-
"@peerbit/test-utils": "2.0.
|
|
69
|
+
"@peerbit/test-utils": "2.0.34-2bc15a6"
|
|
69
70
|
}
|
|
70
71
|
}
|
package/src/string-index.ts
CHANGED
|
@@ -44,13 +44,13 @@ export class StringIndex extends Program {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
async updateIndex(_change: Change<StringOperation>) {
|
|
47
|
-
this._string = await applyOperations("", await this._log.
|
|
47
|
+
this._string = await applyOperations("", await this._log.toArray()); // TODO improve performance
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export const applyOperations = async (
|
|
52
52
|
string: string,
|
|
53
|
-
operations: Entry<StringOperation>[]
|
|
53
|
+
operations: Entry<StringOperation>[],
|
|
54
54
|
): Promise<string> => {
|
|
55
55
|
await Promise.all(operations.map((operation) => operation.getPayload()));
|
|
56
56
|
|
|
@@ -66,11 +66,11 @@ export const applyOperations = async (
|
|
|
66
66
|
};
|
|
67
67
|
export const applyOperation = (
|
|
68
68
|
s: string,
|
|
69
|
-
operation: StringOperation
|
|
69
|
+
operation: StringOperation,
|
|
70
70
|
): string => {
|
|
71
71
|
// TODO check bounds number
|
|
72
72
|
const to = Number(operation.index.offset) + Number(operation.index.length);
|
|
73
|
-
if (operation.value
|
|
73
|
+
if (operation.value !== undefined) {
|
|
74
74
|
s = s.padEnd(to);
|
|
75
75
|
s =
|
|
76
76
|
s.slice(0, Number(operation.index.offset)) +
|
package/src/string-store.ts
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import { field, variant } from "@dao-xyz/borsh";
|
|
2
|
-
import {
|
|
3
|
-
import { SharedLog, type SharedLogOptions } from "@peerbit/shared-log";
|
|
2
|
+
import { CustomEvent } from "@libp2p/interface";
|
|
4
3
|
import { PublicSignKey, sha256Base64Sync } from "@peerbit/crypto";
|
|
5
|
-
import {
|
|
6
|
-
import { type RPCRequestOptions, RPC, type RequestContext } from "@peerbit/rpc";
|
|
4
|
+
import { type AppendOptions, type Change, Entry } from "@peerbit/log";
|
|
7
5
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
8
|
-
import {
|
|
6
|
+
import { Program, type ProgramEvents } from "@peerbit/program";
|
|
7
|
+
import { RPC, type RPCRequestOptions, type RequestContext } from "@peerbit/rpc";
|
|
8
|
+
import { SharedLog, type SharedLogOptions } from "@peerbit/shared-log";
|
|
9
|
+
import { concat, fromString } from "uint8arrays";
|
|
9
10
|
import {
|
|
10
11
|
AbstractSearchResult,
|
|
11
12
|
NoAccess,
|
|
12
13
|
RangeMetadata,
|
|
13
14
|
RangeMetadatas,
|
|
14
|
-
StringMatch,
|
|
15
15
|
SearchRequest,
|
|
16
|
-
|
|
16
|
+
StringMatch,
|
|
17
|
+
StringResult,
|
|
17
18
|
} from "./query.js";
|
|
18
|
-
import { CustomEvent } from "@libp2p/interface";
|
|
19
|
-
import { concat, fromString } from "uint8arrays";
|
|
20
|
-
|
|
21
19
|
import { Range } from "./range.js";
|
|
20
|
+
import { StringIndex, StringOperation, encoding } from "./string-index.js";
|
|
22
21
|
|
|
23
22
|
const logger = loggerFn({ module: "string" });
|
|
24
23
|
|
|
@@ -45,7 +44,7 @@ export interface StringEvents {
|
|
|
45
44
|
|
|
46
45
|
export type CanPerform = (
|
|
47
46
|
operation: StringOperation,
|
|
48
|
-
context: TransactionContext
|
|
47
|
+
context: TransactionContext,
|
|
49
48
|
) => Promise<boolean> | boolean;
|
|
50
49
|
|
|
51
50
|
export type Args = {
|
|
@@ -86,7 +85,7 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
86
85
|
await this._log.open({
|
|
87
86
|
encoding,
|
|
88
87
|
replicas: {
|
|
89
|
-
min: 0xffffffff // assume a document can not be sharded?
|
|
88
|
+
min: 0xffffffff, // assume a document can not be sharded?
|
|
90
89
|
},
|
|
91
90
|
canAppend: async (entry) => {
|
|
92
91
|
const operation = await entry.getPayloadValue();
|
|
@@ -102,32 +101,34 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
102
101
|
await this._index.updateIndex(change);
|
|
103
102
|
this.events.dispatchEvent(
|
|
104
103
|
new CustomEvent("change", {
|
|
105
|
-
detail: change
|
|
106
|
-
})
|
|
104
|
+
detail: change,
|
|
105
|
+
}),
|
|
107
106
|
);
|
|
108
|
-
}
|
|
107
|
+
},
|
|
109
108
|
});
|
|
110
109
|
|
|
111
110
|
await this.query.open({
|
|
112
111
|
...options,
|
|
113
112
|
topic: sha256Base64Sync(
|
|
114
|
-
concat([this._log.log.id, fromString("/dstring")])
|
|
113
|
+
concat([this._log.log.id, fromString("/dstring")]),
|
|
115
114
|
),
|
|
116
115
|
responseHandler: this.queryHandler.bind(this),
|
|
117
116
|
queryType: SearchRequest,
|
|
118
|
-
responseType: StringResult
|
|
117
|
+
responseType: StringResult,
|
|
119
118
|
});
|
|
119
|
+
|
|
120
|
+
await this._log.reload(); // TODO make it so that it does not reload the whole log (i.e. persist the generated string in some way)
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
private async _canPerform(
|
|
123
124
|
operation: StringOperation,
|
|
124
|
-
context: TransactionContext
|
|
125
|
+
context: TransactionContext,
|
|
125
126
|
): Promise<boolean> {
|
|
126
127
|
if (this._log.log.length === 0 || context.entry.next.length === 0) {
|
|
127
128
|
return true;
|
|
128
129
|
} else {
|
|
129
130
|
for (const next of context.entry.next) {
|
|
130
|
-
if (this._log.log.has(next)) {
|
|
131
|
+
if (await this._log.log.has(next)) {
|
|
131
132
|
return true;
|
|
132
133
|
}
|
|
133
134
|
}
|
|
@@ -138,17 +139,17 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
138
139
|
async add(
|
|
139
140
|
value: string,
|
|
140
141
|
index: Range,
|
|
141
|
-
options?: AppendOptions<StringOperation
|
|
142
|
+
options?: AppendOptions<StringOperation>,
|
|
142
143
|
) {
|
|
143
144
|
return this._log.append(
|
|
144
145
|
new StringOperation({
|
|
145
146
|
index,
|
|
146
|
-
value
|
|
147
|
+
value,
|
|
147
148
|
}),
|
|
148
149
|
{
|
|
149
150
|
...options,
|
|
150
|
-
meta: { ...options?.meta, next: await this._log.log.getHeads() }
|
|
151
|
-
}
|
|
151
|
+
meta: { ...options?.meta, next: await this._log.log.getHeads().all() }, // TODO: optimize
|
|
152
|
+
},
|
|
152
153
|
);
|
|
153
154
|
}
|
|
154
155
|
|
|
@@ -158,10 +159,10 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
158
159
|
|
|
159
160
|
async queryHandler(
|
|
160
161
|
query: SearchRequest,
|
|
161
|
-
ctx: RequestContext
|
|
162
|
+
ctx: RequestContext,
|
|
162
163
|
): Promise<AbstractSearchResult | undefined> {
|
|
163
164
|
logger.debug("Recieved query");
|
|
164
|
-
if (query instanceof SearchRequest
|
|
165
|
+
if (query instanceof SearchRequest === false) {
|
|
165
166
|
logger.debug("Recieved query which is not a StringQueryRequest");
|
|
166
167
|
return;
|
|
167
168
|
}
|
|
@@ -173,30 +174,30 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
173
174
|
|
|
174
175
|
const content = this._index.string;
|
|
175
176
|
const relaventQueries = stringQuery.query.filter(
|
|
176
|
-
(x) => x instanceof StringMatch
|
|
177
|
+
(x) => x instanceof StringMatch,
|
|
177
178
|
) as StringMatch[];
|
|
178
|
-
if (relaventQueries.length
|
|
179
|
+
if (relaventQueries.length === 0) {
|
|
179
180
|
logger.debug("Responding with all");
|
|
180
181
|
return new StringResult({
|
|
181
|
-
string: content
|
|
182
|
+
string: content,
|
|
182
183
|
});
|
|
183
184
|
}
|
|
184
185
|
const ranges = relaventQueries
|
|
185
186
|
.map((query) => {
|
|
186
187
|
const occurances = findAllOccurrences(
|
|
187
188
|
query.preprocess(content),
|
|
188
|
-
query.preprocess(query.value)
|
|
189
|
+
query.preprocess(query.value),
|
|
189
190
|
);
|
|
190
191
|
return occurances.map((ix) => {
|
|
191
192
|
return new RangeMetadata({
|
|
192
193
|
offset: BigInt(ix),
|
|
193
|
-
length: BigInt(query.value.length)
|
|
194
|
+
length: BigInt(query.value.length),
|
|
194
195
|
});
|
|
195
196
|
});
|
|
196
197
|
})
|
|
197
198
|
.flat(1);
|
|
198
199
|
|
|
199
|
-
if (ranges.length
|
|
200
|
+
if (ranges.length === 0) {
|
|
200
201
|
logger.debug("Could not find any matches");
|
|
201
202
|
return;
|
|
202
203
|
}
|
|
@@ -204,8 +205,8 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
204
205
|
return new StringResult({
|
|
205
206
|
string: content,
|
|
206
207
|
metadatas: new RangeMetadatas({
|
|
207
|
-
metadatas: ranges
|
|
208
|
-
})
|
|
208
|
+
metadatas: ranges,
|
|
209
|
+
}),
|
|
209
210
|
});
|
|
210
211
|
}
|
|
211
212
|
|
|
@@ -219,20 +220,19 @@ export class DString extends Program<Args, StringEvents & ProgramEvents> {
|
|
|
219
220
|
const counter: Map<string, number> = new Map();
|
|
220
221
|
const responses = await this.query.request(
|
|
221
222
|
new SearchRequest({
|
|
222
|
-
query: []
|
|
223
|
+
query: [],
|
|
223
224
|
}),
|
|
224
|
-
options.remote.queryOptions
|
|
225
|
+
options.remote.queryOptions,
|
|
225
226
|
);
|
|
226
227
|
for (const response of responses) {
|
|
227
228
|
if (response.response instanceof NoAccess) {
|
|
228
229
|
logger.error("Missing access");
|
|
229
230
|
continue;
|
|
230
231
|
} else if (response.response instanceof StringResult) {
|
|
231
|
-
options?.remote.callback
|
|
232
|
-
options?.remote.callback(response.response.string);
|
|
232
|
+
options?.remote.callback?.(response.response.string);
|
|
233
233
|
counter.set(
|
|
234
234
|
response.response.string,
|
|
235
|
-
(counter.get(response.response.string) || 0) + 1
|
|
235
|
+
(counter.get(response.response.string) || 0) + 1,
|
|
236
236
|
);
|
|
237
237
|
} else {
|
|
238
238
|
throw new Error("Unsupported type: " + response?.constructor?.name);
|