@peerbit/log 1.0.15 → 2.0.1

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/src/trim.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Cache } from "@peerbit/cache";
2
2
  import PQueue from "p-queue";
3
- import { Entry } from "./entry.js";
3
+ import { Entry, ShallowEntry } from "./entry.js";
4
4
  import { EntryNode, Values } from "./values.js";
5
5
 
6
6
  const trimOptionsEqual = (a: TrimOptions, b: TrimOptions) => {
@@ -68,9 +68,10 @@ export type TrimCondition =
68
68
  | TrimToByteLengthOption
69
69
  | TrimToLengthOption
70
70
  | TrimToTime;
71
+
71
72
  export type TrimCanAppendOption = {
72
73
  filter?: {
73
- canTrim: (gid: string) => Promise<boolean> | boolean;
74
+ canTrim: (entry: ShallowEntry) => Promise<boolean> | boolean;
74
75
  cacheId?: () => string | number;
75
76
  };
76
77
  };
@@ -98,7 +99,7 @@ export class Trim<T> {
98
99
  }
99
100
 
100
101
  deleteFromCache(entry: Entry<T>) {
101
- if (this._canTrimCacheLastNode?.value.hash === entry.hash) {
102
+ if (this._canTrimCacheLastNode?.value === entry.hash) {
102
103
  this._canTrimCacheLastNode = this._canTrimCacheLastNode.prev;
103
104
  }
104
105
  }
@@ -116,7 +117,7 @@ export class Trim<T> {
116
117
  /// TODO Make this method less ugly
117
118
  const deleted: Entry<T>[] = [];
118
119
 
119
- let done: () => Promise<boolean> | boolean;
120
+ let done: () => boolean;
120
121
  const values = this._log.values();
121
122
  if (option.type === "length") {
122
123
  const to = option.to;
@@ -136,18 +137,18 @@ export class Trim<T> {
136
137
  } else if (option.type == "time") {
137
138
  const s0 = BigInt(+new Date() * 1e6);
138
139
  const maxAge = option.maxAge * 1e6;
139
- done = async () => {
140
+ done = () => {
140
141
  if (!values.tail) {
141
142
  return true;
142
143
  }
143
144
 
144
- const nodeValue = await values.getEntry(values.tail);
145
+ const nodeValue = values.entryIndex.getShallow(values.tail.value);
145
146
 
146
147
  if (!nodeValue) {
147
148
  return true;
148
149
  }
149
150
 
150
- return s0 - nodeValue.metadata.clock.timestamp.wallTime < maxAge;
151
+ return s0 - nodeValue.meta.clock.timestamp.wallTime < maxAge;
151
152
  };
152
153
  } else {
153
154
  return [];
@@ -194,7 +195,7 @@ export class Trim<T> {
194
195
  // TODO only go through heads?
195
196
  while (
196
197
  node &&
197
- !(await done()) &&
198
+ !done() &&
198
199
  values.length > 0 &&
199
200
  node &&
200
201
  (!looped || node !== startNode)
@@ -202,15 +203,16 @@ export class Trim<T> {
202
203
  let deleteAble: boolean | undefined = true;
203
204
  if (option.filter?.canTrim) {
204
205
  canTrimByGid = canTrimByGid || new Map();
205
- deleteAble = canTrimByGid.get(node.value.gid);
206
+ const indexedEntry = values.entryIndex.getShallow(node.value)!; // TODO check undefined
207
+ deleteAble = canTrimByGid.get(indexedEntry.meta.gid);
206
208
  if (deleteAble === undefined) {
207
- deleteAble = await option.filter?.canTrim(node.value.gid);
208
- canTrimByGid.set(node.value.gid, deleteAble);
209
+ deleteAble = await option.filter?.canTrim(indexedEntry);
210
+ canTrimByGid.set(indexedEntry.meta.gid, deleteAble);
209
211
  }
210
212
 
211
213
  if (!deleteAble && cacheProgress) {
212
214
  // ignore it
213
- this._canTrimCacheHashBreakpoint.add(node.value.hash, true);
215
+ this._canTrimCacheHashBreakpoint.add(node.value, true);
214
216
  }
215
217
  }
216
218
 
package/src/values.ts CHANGED
@@ -1,25 +1,15 @@
1
- import { Entry } from "./entry";
1
+ import { Entry, ShallowEntry } from "./entry";
2
2
  import { ISortFunction } from "./log-sorting";
3
3
  import yallist from "yallist";
4
4
  import { EntryIndex } from "./entry-index";
5
5
 
6
- type Storage<T> = (
7
- hash: string
8
- ) => Promise<Entry<T> | undefined> | Entry<T> | undefined;
9
-
10
- interface Value {
11
- hash: string;
12
- gid: string;
13
- byteLength: number;
14
- }
15
-
16
- export type EntryNode = yallist.Node<Value>;
6
+ export type EntryNode = yallist.Node<string>;
17
7
 
18
8
  export class Values<T> {
19
9
  /**
20
10
  * Keep track of sorted elements in descending sort order (i.e. newest elements)
21
11
  */
22
- private _values: yallist<Value>;
12
+ private _values: yallist<string>;
23
13
  private _sortFn: ISortFunction;
24
14
  private _byteLength: number;
25
15
  private _entryIndex: EntryIndex<T>;
@@ -36,11 +26,15 @@ export class Values<T> {
36
26
  .reverse()
37
27
  .map((x) => {
38
28
  if (!x.hash) throw new Error("Unexpected");
39
- return {
29
+ return x.hash; /* {
40
30
  hash: x.hash,
41
31
  byteLength: x._payload.byteLength,
42
- gid: x.gid,
43
- };
32
+ meta: {
33
+ gids: x.gids,
34
+ gid: x.gid,
35
+ data: x.meta.data,
36
+ },
37
+ }; */
44
38
  })
45
39
  );
46
40
  this._byteLength = 0;
@@ -53,7 +47,7 @@ export class Values<T> {
53
47
 
54
48
  toArray(): Promise<Entry<T>[]> {
55
49
  return Promise.all(
56
- this._values.toArrayReverse().map((x) => this._entryIndex.get(x.hash))
50
+ this._values.toArrayReverse().map((x) => this._entryIndex.get(x))
57
51
  ).then((arr) => arr.filter((x) => !!x)) as Promise<Entry<T>[]>; // we do reverse because we assume the log is only meaningful if we read it from start to end
58
52
  }
59
53
 
@@ -68,6 +62,10 @@ export class Values<T> {
68
62
  }
69
63
 
70
64
  private _putPromise: Map<string, Promise<any>> = new Map();
65
+
66
+ get entryIndex(): EntryIndex<T> {
67
+ return this._entryIndex;
68
+ }
71
69
  async put(value: Entry<T>) {
72
70
  let promise = this._putPromise.get(value.hash);
73
71
  if (promise) {
@@ -85,7 +83,7 @@ export class Values<T> {
85
83
  let walker = this._values.head;
86
84
  let last: EntryNode | undefined = undefined;
87
85
  while (walker) {
88
- const walkerValue = await this.getEntry(walker);
86
+ const walkerValue = await this._entryIndex.getShallow(walker.value);
89
87
  if (!walkerValue) {
90
88
  throw new Error("Missing walker value");
91
89
  }
@@ -106,11 +104,7 @@ export class Values<T> {
106
104
  throw new Error("Unexpected");
107
105
  }
108
106
 
109
- _insertAfter(this._values, last, {
110
- byteLength: value._payload.byteLength,
111
- gid: value.gid,
112
- hash: value.hash,
113
- });
107
+ _insertAfter(this._values, last, value.hash);
114
108
  }
115
109
 
116
110
  async delete(value: Entry<T> | string) {
@@ -119,7 +113,7 @@ export class Values<T> {
119
113
 
120
114
  let walker = this._values.tail;
121
115
  while (walker) {
122
- const walkerValue = await this.getEntry(walker);
116
+ const walkerValue = this._entryIndex.getShallow(walker.value);
123
117
 
124
118
  if (!walkerValue) {
125
119
  throw new Error("Missing walker value");
@@ -127,7 +121,7 @@ export class Values<T> {
127
121
 
128
122
  if (walkerValue.hash === hash) {
129
123
  this._values.removeNode(walker);
130
- this._byteLength -= walkerValue._payload.byteLength;
124
+ this._byteLength -= walkerValue.payloadByteLength;
131
125
  return;
132
126
  }
133
127
  walker = walker.prev; // prev will be undefined if you do removeNode(walker)
@@ -143,14 +137,16 @@ export class Values<T> {
143
137
 
144
138
  deleteNode(node: EntryNode) {
145
139
  this._values.removeNode(node);
146
- this._byteLength -= node.value.byteLength;
140
+ this._byteLength -= this._entryIndex.getShallow(
141
+ node.value
142
+ )!.payloadByteLength;
147
143
  return;
148
144
  }
149
145
 
150
146
  pop() {
151
147
  const value = this._values.pop();
152
148
  if (value) {
153
- this._byteLength -= value.byteLength;
149
+ this._byteLength -= this._entryIndex.getShallow(value)!.payloadByteLength;
154
150
  }
155
151
  return value;
156
152
  }
@@ -158,16 +154,12 @@ export class Values<T> {
158
154
  get byteLength() {
159
155
  return this._byteLength;
160
156
  }
161
-
162
- async getEntry(node: EntryNode) {
163
- return this._entryIndex.get(node.value.hash);
164
- }
165
157
  }
166
158
 
167
159
  function _insertAfter(
168
160
  self: yallist<any>,
169
161
  node: EntryNode | undefined,
170
- value: Value
162
+ value: string
171
163
  ) {
172
164
  const inserted = !node
173
165
  ? new yallist.Node(
@@ -1 +0,0 @@
1
- export declare const isDefined: (arg: any) => boolean;
@@ -1,2 +0,0 @@
1
- export const isDefined = (arg) => arg !== undefined && arg !== null;
2
- //# sourceMappingURL=is-defined.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"is-defined.js","sourceRoot":"","sources":["../../src/is-defined.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,CAAC"}
package/src/is-defined.ts DELETED
@@ -1 +0,0 @@
1
- export const isDefined = (arg: any) => arg !== undefined && arg !== null;