Skip to content

Commit 34b588d

Browse files
committed
version
1 parent 9a83b14 commit 34b588d

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

src/ordered-keyvalue.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import type { HeliaLibp2p } from "helia";
1313
import type { Libp2p } from "libp2p";
1414
import type { ServiceMap } from "@libp2p/interface";
15-
import itAll from 'it-all'
15+
import itAll from "it-all";
1616
import { getScalePosition } from "./utils.js";
1717

1818
export type OrderedKeyValueDatabaseType = Awaited<
@@ -92,7 +92,6 @@ export const OrderedKeyValueApi = ({
9292
}: {
9393
database: InternalDatabase;
9494
}) => {
95-
9695
const put = async (
9796
key: string,
9897
value: DagCborEncodable,
@@ -102,26 +101,24 @@ export const OrderedKeyValueApi = ({
102101
const entries = await itAll(iterator());
103102
const entryValue: { value: DagCborEncodable; position: number } = {
104103
value,
105-
position: await getScalePosition({entries, key, position}),
104+
position: await getScalePosition({ entries, key, position }),
106105
};
107106
return database.addOperation({ op: "PUT", key, value: entryValue });
108107
};
109108

110109
const move = async (key: string, position: number): Promise<void> => {
111110
// Somewhat inefficient, I suppose, but we need to know which entries are already present.
112111
const entries = await itAll(iterator());
113-
position = await getScalePosition({entries, key, position});
114-
112+
position = await getScalePosition({ entries, key, position });
113+
115114
await database.addOperation({ op: "MOVE", key, value: position });
116115
};
117116

118117
const del = async (key: string): Promise<string> => {
119118
return database.addOperation({ op: "DEL", key, value: null });
120119
};
121120

122-
const get = async (
123-
key: string,
124-
): Promise<DagCborEncodable | undefined> => {
121+
const get = async (key: string): Promise<DagCborEncodable | undefined> => {
125122
for await (const entry of database.log.traverse()) {
126123
const { op, key: k, value } = entry.payload;
127124
if (op === "PUT" && k === key) {
@@ -146,8 +143,8 @@ export const OrderedKeyValueApi = ({
146143
unknown
147144
> {
148145
let count = 0;
149-
const keys: {[key: string]: true} = {};
150-
const positions: {[key: string]: number} = {};
146+
const keys: { [key: string]: true } = {};
147+
const positions: { [key: string]: number } = {};
151148

152149
for await (const entry of database.log.traverse()) {
153150
const { op, key, value } = entry.payload;
@@ -157,17 +154,16 @@ export const OrderedKeyValueApi = ({
157154
if (op === "PUT") {
158155
const hash = entry.hash;
159156
const putValue = value as { value: unknown; position?: number };
160-
157+
161158
keys[key] = true;
162159
count++;
163-
160+
164161
yield {
165162
key,
166163
value: putValue.value,
167164
position: positions[key] ?? putValue.position ?? -1,
168165
hash,
169-
}
170-
166+
};
171167
} else if (op === "MOVE") {
172168
if (positions[key] !== undefined || keys[key]) continue;
173169
positions[key] = value as number;
@@ -178,7 +174,6 @@ export const OrderedKeyValueApi = ({
178174
break;
179175
}
180176
}
181-
182177
};
183178

184179
const all = async () => {
@@ -191,11 +186,13 @@ export const OrderedKeyValueApi = ({
191186
for await (const entry of iterator()) {
192187
entries.push(entry);
193188
}
194-
return entries.sort((a, b) => a.position - b.position).map(e=>({
195-
key: e.key,
196-
value: e.value,
197-
hash: e.hash,
198-
}));
189+
return entries
190+
.sort((a, b) => a.position - b.position)
191+
.map((e) => ({
192+
key: e.key,
193+
value: e.value,
194+
hash: e.hash,
195+
}));
199196
};
200197

201198
return {

src/utils.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1-
export const getScalePosition = async ({ entries, key, position }: { entries: {
1+
export const getScalePosition = async ({
2+
entries,
3+
key,
4+
position,
5+
}: {
6+
entries: {
27
key: string;
38
value: unknown;
49
position: number;
510
hash: string;
6-
}[]; key: string; position: number }): Promise<number> => {
7-
// Firstly, sort entries by position
8-
entries = entries.sort((a, b) => a.position - b.position);
11+
}[];
12+
key: string;
13+
position: number;
14+
}): Promise<number> => {
15+
// Firstly, sort entries by position
16+
entries = entries.sort((a, b) => a.position - b.position);
917

10-
// Negative values mean insert from end of list.
11-
if (position < 0) position = entries.length - (position + 1)
18+
// Negative values mean insert from end of list.
19+
if (position < 0) position = entries.length - (position + 1);
1220

13-
// Find any previous position
14-
const previousPosition = entries.find(x=>x.key === key)?.position;
21+
// Find any previous position
22+
const previousPosition = entries.find((x) => x.key === key)?.position;
1523

16-
// If we are moving upwards, need to add 1 to adjust for the now-deleted slot where our entry used to be
17-
if (previousPosition !== undefined && position > previousPosition) position = position + 1
24+
// If we are moving upwards, need to add 1 to adjust for the now-deleted slot where our entry used to be
25+
if (previousPosition !== undefined && position > previousPosition)
26+
position = position + 1;
1827

19-
// Calculate scale positions of previous and next entries, if they exist
20-
const beforePosition = entries[Math.min(position, entries.length) - 1]?.position;
21-
const afterPosition = entries[Math.max(position, 0)]?.position;
28+
// Calculate scale positions of previous and next entries, if they exist
29+
const beforePosition =
30+
entries[Math.min(position, entries.length) - 1]?.position;
31+
const afterPosition = entries[Math.max(position, 0)]?.position;
2232

23-
// Insert to beginning of list if there is no preceding entry
24-
if (beforePosition === undefined) return afterPosition === undefined ? 0 : afterPosition - 1;
33+
// Insert to beginning of list if there is no preceding entry
34+
if (beforePosition === undefined)
35+
return afterPosition === undefined ? 0 : afterPosition - 1;
2536

26-
// Insert to end of list if there is no following entry, or somewhere between adjacent entries
27-
// Note: use Math.random() rather than mean to reduce risk of collisions in concurrent edits
28-
return afterPosition === undefined ? beforePosition + 1 : beforePosition + (afterPosition - beforePosition) * Math.random()
29-
}
37+
// Insert to end of list if there is no following entry, or somewhere between adjacent entries
38+
// Note: use Math.random() rather than mean to reduce risk of collisions in concurrent edits
39+
return afterPosition === undefined
40+
? beforePosition + 1
41+
: beforePosition + (afterPosition - beforePosition) * Math.random();
42+
};

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Generated by genversion.
2-
export const version = "1.2.2";
2+
export const version = "1.3.0";

0 commit comments

Comments
 (0)