Skip to content

Commit 35cfc18

Browse files
committed
Progrès tests
1 parent fa00b06 commit 35cfc18

File tree

2 files changed

+108
-16
lines changed

2 files changed

+108
-16
lines changed

src/ordered-keyvalue.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type Storage,
66
type MetaData,
77
type DagCborEncodable,
8+
type LogEntry,
89
} from "@orbitdb/core";
910
import type { HeliaLibp2p } from "helia";
1011

@@ -106,15 +107,17 @@ const OrderedKeyValue =
106107
key: string;
107108
value: unknown;
108109
position: number;
110+
clock: number;
109111
hash: string;
110112
},
111113
void,
112114
unknown
113115
> {
114116
const keys: { [key: string]: boolean } = {};
115-
const positions: { [key: string]: number } = {};
117+
const positions: { [key: string]: {position: number; clock: number } } = {};
116118

117119
let count = 0;
120+
let clock = 0;
118121
for await (const entry of log.traverse()) {
119122
const { op, key, value } = entry.payload;
120123
if (!key) return;
@@ -129,14 +132,16 @@ const OrderedKeyValue =
129132
positions[key] !== undefined
130133
? positions[key]
131134
: putValue.position !== undefined
132-
? putValue.position
133-
: 0;
135+
? {position: putValue.position, clock}
136+
: {position: -1, clock};
134137
positions[key] = position;
135138

136139
count++;
137-
yield { key, value: putValue.value, position, hash };
138-
} else if (op === "MOVE" && !keys[key]) {
139-
positions[key] = value as number;
140+
clock--;
141+
yield { key, value: putValue.value, ...position, hash };
142+
} else if (op === "MOVE" && !keys[key] && !positions[key]) { // À faire ici
143+
positions[key] = {position: value as number, clock};
144+
clock--;
140145
} else if (op === "DEL" && !keys[key]) {
141146
keys[key] = true;
142147
}
@@ -152,19 +157,30 @@ const OrderedKeyValue =
152157
value: unknown;
153158
hash: string;
154159
position: number;
160+
clock: number;
155161
}[] = [];
156162
for await (const entry of iterator()) {
157163
values.unshift(entry);
158164
}
159-
160-
return values
161-
.sort((a, b) =>
162-
a.position > b.position ? 1 : a.position === b.position ? 0 : -1,
165+
const nonNegativePositionValues = values.map(
166+
v => ({
167+
...v,
168+
position: v.position >= 0 ? v.position : values.length + (v.position)
169+
})
170+
)
171+
console.log(nonNegativePositionValues)
172+
173+
return nonNegativePositionValues
174+
.sort((a, b) =>{
175+
return a.position > b.position ? 1 : a.position === b.position ? (
176+
a.clock - b.clock
177+
) : -1
178+
}
163179
)
164-
.map((x) => ({
165-
key: x.key,
166-
value: x.value,
167-
hash: x.hash,
180+
.map((v) => ({
181+
key: v.key,
182+
value: v.value,
183+
hash: v.hash,
168184
}));
169185
};
170186

test/ordered-keyvalue.spec.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ describe("OrderedKeyValue Database", () => {
174174
]);
175175
});
176176

177+
it("add a value - index 0", async () => {
178+
const value = "value1";
179+
const key = "key1";
180+
181+
const value2 = "value2";
182+
const key2 = "key2";
183+
184+
const hash = await db.put(key, value);
185+
const hash2 = await db.put(key2, value2, 0);
186+
187+
const actual = await db.all();
188+
189+
expect(actual).to.deep.equal([
190+
{ value: value2, key: key2, hash: hash2 },
191+
{ value: value, key, hash },
192+
]);
193+
});
194+
177195
it("add a value - negative index", async () => {
178196
const value = "value1";
179197
const key = "key1";
@@ -187,8 +205,8 @@ describe("OrderedKeyValue Database", () => {
187205
const actual = await db.all();
188206

189207
expect(actual).to.deep.equal([
190-
{ value: value2, key: key2, hash: hash2 },
191208
{ value: value, key, hash },
209+
{ value: value2, key: key2, hash: hash2 },
192210
]);
193211
});
194212

@@ -228,6 +246,24 @@ describe("OrderedKeyValue Database", () => {
228246
]);
229247
});
230248

249+
it("move a value - index 0", async () => {
250+
const value = "value1";
251+
const key = "key1";
252+
253+
const value2 = "value2";
254+
const key2 = "key2";
255+
256+
const hash = await db.put(key, value);
257+
const hash2 = await db.put(key2, value2);
258+
await db.move(key2, 0);
259+
const actual = await db.all();
260+
261+
expect(actual).to.deep.equal([
262+
{ value: value2, key: key2, hash: hash2 },
263+
{ value: value, key, hash },
264+
]);
265+
});
266+
231267
it("move a value - negative index", async () => {
232268
const value = "value1";
233269
const key = "key1";
@@ -237,7 +273,7 @@ describe("OrderedKeyValue Database", () => {
237273

238274
const hash = await db.put(key, value);
239275
const hash2 = await db.put(key2, value2);
240-
await db.move(key2, -1);
276+
await db.move(key, -1);
241277
const actual = await db.all();
242278

243279
expect(actual).to.deep.equal([
@@ -305,46 +341,86 @@ describe("OrderedKeyValue Database", () => {
305341
]);
306342
});
307343

344+
it.skip("move a value twice", async () => {
345+
const value = "value1";
346+
const key = "key1";
347+
348+
const value2 = "value2";
349+
const key2 = "key2";
350+
351+
const value3 = "value3";
352+
const key3 = "key3";
353+
354+
const hash = await db.put(key, value);
355+
const hash2 = await db.put(key2, value2);
356+
const hash3 = await db.put(key3, value3);
357+
await db.move(key, 1);
358+
359+
const actual1 = await db.all();
360+
expect(actual1).to.deep.equal([
361+
{ value: value2, key: key2, hash: hash2 },
362+
{ value: value, key, hash },
363+
{ value: value3, key: key3, hash: hash3 },
364+
]);
365+
366+
await db.move(key, 2);
367+
368+
const actual2 = await db.all();
369+
expect(actual2).to.deep.equal([
370+
{ value: value2, key: key2, hash: hash2 },
371+
{ value: value3, key: key3, hash: hash3 },
372+
{ value: value, key, hash },
373+
]);
374+
});
375+
308376
it("returns all values", async () => {
309377
const keyvalue: {
310378
value: DBElements;
311379
key: string;
312380
position: number;
381+
clock: number;
313382
hash?: string;
314383
}[] = [
315384
{
316385
key: "key1",
317386
position: 0,
387+
clock: -6,
318388
value: "init",
319389
},
320390
{
321391
key: "key2",
322392
position: 1,
393+
clock: -5,
323394
value: true,
324395
},
325396
{
326397
key: "key3",
327398
position: 2,
399+
clock: -4,
328400
value: "hello",
329401
},
330402
{
331403
key: "key4",
332404
position: 3,
405+
clock: -3,
333406
value: "friend",
334407
},
335408
{
336409
key: "key5",
337410
position: 4,
411+
clock: -2,
338412
value: "12345",
339413
},
340414
{
341415
key: "key6",
342416
position: 5,
417+
clock: -1,
343418
value: "empty",
344419
},
345420
{
346421
key: "key7",
347422
position: 6,
423+
clock: 0,
348424
value: "friend33",
349425
},
350426
];

0 commit comments

Comments
 (0)