@@ -60,21 +60,94 @@ test('compact', async (t) => {
60
60
index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 1 } } ) ,
61
61
index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 2 } } ) ,
62
62
index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 2 } } ) ,
63
- index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 1 } } ) ,
64
- // compact will return entries with a null integrity
65
- index . insert ( CACHE , KEY , null , { metadata : { rev : 3 } } )
63
+ index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 1 } } )
66
64
] )
67
65
68
66
const bucket = index . bucketPath ( CACHE , KEY )
69
67
const entries = await index . bucketEntries ( bucket )
70
- t . equal ( entries . length , 5 , 'started with 5 entries' )
68
+ t . equal ( entries . length , 4 , 'started with 4 entries' )
71
69
72
70
const filter = ( entryA , entryB ) => entryA . metadata . rev === entryB . metadata . rev
73
71
const compacted = await index . compact ( CACHE , KEY , filter )
74
- t . equal ( compacted . length , 3 , 'should return only three entries' )
72
+ t . equal ( compacted . length , 2 , 'should return only two entries' )
73
+
74
+ const newEntries = await index . bucketEntries ( bucket )
75
+ t . equal ( newEntries . length , 2 , 'bucket was deduplicated' )
76
+ } )
77
+
78
+ test ( 'compact: treats null integrity without validateEntry as a delete' , async ( t ) => {
79
+ t . teardown ( ( ) => {
80
+ index . delete . sync ( CACHE , KEY )
81
+ } )
82
+ // this one does not use Promise.all because we want to be certain
83
+ // things are written in the right order
84
+ await index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 1 } } )
85
+ await index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 2 } } )
86
+ // this is a delete, revs 1, 2 and 3 will be omitted
87
+ await index . insert ( CACHE , KEY , null , { metadata : { rev : 3 } } )
88
+ await index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 4 } } )
89
+
90
+ const bucket = index . bucketPath ( CACHE , KEY )
91
+ const entries = await index . bucketEntries ( bucket )
92
+ t . equal ( entries . length , 4 , 'started with 4 entries' )
93
+
94
+ const filter = ( entryA , entryB ) => entryA . metadata . rev === entryB . metadata . rev
95
+ const compacted = await index . compact ( CACHE , KEY , filter )
96
+ t . equal ( compacted . length , 1 , 'should return only one entry' )
97
+ t . equal ( compacted [ 0 ] . metadata . rev , 4 , 'kept rev 4' )
98
+
99
+ const newEntries = await index . bucketEntries ( bucket )
100
+ t . equal ( newEntries . length , 1 , 'bucket was deduplicated' )
101
+ } )
102
+
103
+ test ( 'compact: leverages validateEntry to skip invalid entries' , async ( t ) => {
104
+ t . teardown ( ( ) => {
105
+ index . delete . sync ( CACHE , KEY )
106
+ } )
107
+ await Promise . all ( [
108
+ index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 1 } } ) ,
109
+ index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 2 } } ) ,
110
+ index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 2 } } ) ,
111
+ index . insert ( CACHE , KEY , INTEGRITY , { metadata : { rev : 1 } } )
112
+ ] )
113
+
114
+ const bucket = index . bucketPath ( CACHE , KEY )
115
+ const entries = await index . bucketEntries ( bucket )
116
+ t . equal ( entries . length , 4 , 'started with 4 entries' )
117
+
118
+ const matchFn = ( entryA , entryB ) => entryA . metadata . rev === entryB . metadata . rev
119
+ const validateEntry = ( entry ) => entry . metadata . rev > 1
120
+ const compacted = await index . compact ( CACHE , KEY , matchFn , { validateEntry } )
121
+ t . equal ( compacted . length , 1 , 'should return only one entries' )
122
+ t . equal ( compacted [ 0 ] . metadata . rev , 2 , 'kept the rev 2 entry' )
123
+
124
+ const newEntries = await index . bucketEntries ( bucket )
125
+ t . equal ( newEntries . length , 1 , 'bucket was deduplicated' )
126
+ } )
127
+
128
+ test ( 'compact: validateEntry allows for keeping null integrity' , async ( t ) => {
129
+ t . teardown ( ( ) => {
130
+ index . delete . sync ( CACHE , KEY )
131
+ } )
132
+ await Promise . all ( [
133
+ index . insert ( CACHE , KEY , null , { metadata : { rev : 1 } } ) ,
134
+ index . insert ( CACHE , KEY , null , { metadata : { rev : 2 } } ) ,
135
+ index . insert ( CACHE , KEY , null , { metadata : { rev : 2 } } ) ,
136
+ index . insert ( CACHE , KEY , null , { metadata : { rev : 1 } } )
137
+ ] )
138
+
139
+ const bucket = index . bucketPath ( CACHE , KEY )
140
+ const entries = await index . bucketEntries ( bucket )
141
+ t . equal ( entries . length , 4 , 'started with 4 entries' )
142
+
143
+ const matchFn = ( entryA , entryB ) => entryA . metadata . rev === entryB . metadata . rev
144
+ const validateEntry = ( entry ) => entry . metadata . rev > 1
145
+ const compacted = await index . compact ( CACHE , KEY , matchFn , { validateEntry } )
146
+ t . equal ( compacted . length , 1 , 'should return only one entry' )
147
+ t . equal ( compacted [ 0 ] . metadata . rev , 2 , 'kept the rev 2 entry' )
75
148
76
149
const newEntries = await index . bucketEntries ( bucket )
77
- t . equal ( newEntries . length , 3 , 'bucket was deduplicated' )
150
+ t . equal ( newEntries . length , 1 , 'bucket was deduplicated' )
78
151
} )
79
152
80
153
test ( 'compact: ENOENT in chownr does not cause failure' , async ( t ) => {
0 commit comments