2525import java .util .List ;
2626import java .util .Random ;
2727import java .util .Set ;
28+ import java .util .stream .Collectors ;
2829
2930import org .eclipse .rdf4j .model .IRI ;
3031import org .eclipse .rdf4j .model .Literal ;
3132import org .eclipse .rdf4j .model .Value ;
33+ import org .eclipse .rdf4j .model .util .Values ;
34+ import org .eclipse .rdf4j .model .vocabulary .RDF ;
35+ import org .eclipse .rdf4j .model .vocabulary .RDFS ;
3236import org .eclipse .rdf4j .model .vocabulary .XSD ;
3337import org .eclipse .rdf4j .sail .lmdb .config .LmdbStoreConfig ;
3438import org .eclipse .rdf4j .sail .lmdb .model .LmdbLiteral ;
3539import org .eclipse .rdf4j .sail .lmdb .model .LmdbValue ;
40+ import org .junit .Assert ;
3641import org .junit .jupiter .api .AfterEach ;
3742import org .junit .jupiter .api .BeforeEach ;
3843import org .junit .jupiter .api .Test ;
@@ -58,49 +63,53 @@ private ValueStore createValueStore() throws IOException {
5863
5964 @ Test
6065 public void testGcValues () throws Exception {
61- Random random = new Random (1337 );
62- LmdbValue values [] = new LmdbValue [1000 ];
66+ Value values [] = new Value [] {
67+ RDF .TYPE , RDFS .CLASS ,
68+ Values .iri ("some:iri" ),
69+ Values .literal ("This is a literal." )
70+ };
71+ long ids [] = new long [values .length ];
6372 valueStore .startTransaction (true );
6473 for (int i = 0 ; i < values .length ; i ++) {
65- values [i ] = valueStore .createLiteral ("This is a random literal:" + random .nextLong ());
66- valueStore .storeValue (values [i ]);
74+ ids [i ] = valueStore .storeValue (values [i ]);
6775 }
6876 valueStore .commit ();
6977
7078 ValueStoreRevision revBefore = valueStore .getRevision ();
7179
7280 valueStore .startTransaction (true );
73- Set <Long > ids = new HashSet <>();
74- for (int i = 0 ; i < 30 ; i ++) {
75- ids .add (values [i ].getInternalID ());
76- }
77- valueStore .gcIds (ids , new HashSet <>());
81+ Set <Long > idsToGc = new HashSet <>(Arrays .stream (ids ).boxed ().collect (Collectors .toList ()));
82+ valueStore .gcIds (idsToGc , new HashSet <>());
7883 valueStore .commit ();
7984
8085 ValueStoreRevision revAfter = valueStore .getRevision ();
81-
8286 assertNotEquals ("revisions must change after gc of IDs" , revBefore , revAfter );
8387
84- Arrays .fill (values , null );
85- // GC would collect revision at some point in time
86- // just add revision ID to free list for this test as forcing GC is not possible
88+ for (int i = 0 ; i < values .length ; i ++) {
89+ Assert .assertEquals (LmdbValue .UNKNOWN_ID , valueStore .getId (values [i ]));
90+ Assert .assertTrue (valueStore .getValue (ids [i ]) != null );
91+ }
92+
93+ // simulate GC of unused revisions
8794 valueStore .unusedRevisionIds .add (revBefore .getRevisionId ());
8895
8996 valueStore .forceEvictionOfValues ();
9097 valueStore .startTransaction (true );
9198 valueStore .commit ();
9299
100+ for (int i = 0 ; i < values .length ; i ++) {
101+ Assert .assertEquals (LmdbValue .UNKNOWN_ID , valueStore .getId (values [i ]));
102+ Assert .assertTrue (valueStore .getValue (ids [i ]) != null );
103+ }
104+
93105 valueStore .startTransaction (true );
94- for (int i = 0 ; i < 30 ; i ++) {
95- LmdbValue value = valueStore .createLiteral ("This is a random literal:" + random .nextLong ());
96- values [i ] = value ;
97- valueStore .storeValue (value );
106+ for (int i = 0 ; i < values .length ; i ++) {
98107 // this ID should have been reused
99- ids .remove (value . getInternalID ( ));
108+ idsToGc .remove (valueStore . storeValue ( values [ i ] ));
100109 }
101110 valueStore .commit ();
102111
103- assertEquals ("IDs should have been reused" , Collections .emptySet (), ids );
112+ assertEquals ("IDs should have been reused" , Collections .emptySet (), idsToGc );
104113 }
105114
106115 @ Test
0 commit comments