@ -34,6 +34,7 @@ import org.junit.Test;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import java.util.List ;
import static org.hamcrest.CoreMatchers.equalTo ;
import static org.hamcrest.CoreMatchers.equalTo ;
@ -50,7 +51,6 @@ public class RocksDBSessionStoreTest {
@Before
@Before
public void before ( ) {
public void before ( ) {
final SessionKeySchema schema = new SessionKeySchema ( ) ;
final SessionKeySchema schema = new SessionKeySchema ( ) ;
schema . init ( "topic" ) ;
final RocksDBSegmentedBytesStore bytesStore = new RocksDBSegmentedBytesStore (
final RocksDBSegmentedBytesStore bytesStore = new RocksDBSegmentedBytesStore (
"session-store" ,
"session-store" ,
@ -94,11 +94,21 @@ public class RocksDBSessionStoreTest {
final List < KeyValue < Windowed < String > , Long > > expected =
final List < KeyValue < Windowed < String > , Long > > expected =
Arrays . asList ( KeyValue . pair ( a1 , 1L ) , KeyValue . pair ( a2 , 2L ) ) ;
Arrays . asList ( KeyValue . pair ( a1 , 1L ) , KeyValue . pair ( a2 , 2L ) ) ;
final KeyValueIterator < Windowed < String > , Long > values =
try ( final KeyValueIterator < Windowed < String > , Long > values =
sessionStore . findSessions ( key , 0 , 1000L ) ;
sessionStore . findSessions ( key , 0 , 1000L )
) {
assertEquals ( expected , toList ( values ) ) ;
assertEquals ( expected , toList ( values ) ) ;
}
}
final List < KeyValue < Windowed < String > , Long > > expected2 = Collections . singletonList ( KeyValue . pair ( a2 , 2L ) ) ;
try ( final KeyValueIterator < Windowed < String > , Long > values2 =
sessionStore . findSessions ( key , 400L , 600L )
) {
assertEquals ( expected2 , toList ( values2 ) ) ;
}
}
@Test
@Test
public void shouldFetchAllSessionsWithSameRecordKey ( ) {
public void shouldFetchAllSessionsWithSameRecordKey ( ) {
final List < KeyValue < Windowed < String > , Long > > expected = Arrays . asList (
final List < KeyValue < Windowed < String > , Long > > expected = Arrays . asList (
@ -114,8 +124,9 @@ public class RocksDBSessionStoreTest {
// add one that shouldn't appear in the results
// add one that shouldn't appear in the results
sessionStore . put ( new Windowed < > ( "aa" , new SessionWindow ( 0 , 0 ) ) , 5L ) ;
sessionStore . put ( new Windowed < > ( "aa" , new SessionWindow ( 0 , 0 ) ) , 5L ) ;
final List < KeyValue < Windowed < String > , Long > > results = toList ( sessionStore . fetch ( "a" ) ) ;
try ( final KeyValueIterator < Windowed < String > , Long > values = sessionStore . fetch ( "a" ) ) {
assertEquals ( expected , results ) ;
assertEquals ( expected , toList ( values ) ) ;
}
}
}
@Test
@Test
@ -123,15 +134,16 @@ public class RocksDBSessionStoreTest {
final String key = "a" ;
final String key = "a" ;
sessionStore . put ( new Windowed < > ( key , new SessionWindow ( 0L , 0L ) ) , 1L ) ;
sessionStore . put ( new Windowed < > ( key , new SessionWindow ( 0L , 0L ) ) , 1L ) ;
sessionStore . put ( new Windowed < > ( key , new SessionWindow ( 1000L , 1000L ) ) , 2L ) ;
sessionStore . put ( new Windowed < > ( key , new SessionWindow ( 1000L , 1000L ) ) , 2L ) ;
final KeyValueIterator < Windowed < String > , Long > results =
sessionStore . findSessions ( key , - 1 , 1000L ) ;
final List < KeyValue < Windowed < String > , Long > > expected = Arrays . asList (
final List < KeyValue < Windowed < String > , Long > > expected = Arrays . asList (
KeyValue . pair ( new Windowed < > ( key , new SessionWindow ( 0L , 0L ) ) , 1L ) ,
KeyValue . pair ( new Windowed < > ( key , new SessionWindow ( 0L , 0L ) ) , 1L ) ,
KeyValue . pair ( new Windowed < > ( key , new SessionWindow ( 1000L , 1000L ) ) , 2L ) ) ;
KeyValue . pair ( new Windowed < > ( key , new SessionWindow ( 1000L , 1000L ) ) , 2L ) ) ;
try ( final KeyValueIterator < Windowed < String > , Long > results =
sessionStore . findSessions ( key , - 1 , 1000L ) ) {
assertEquals ( expected , toList ( results ) ) ;
assertEquals ( expected , toList ( results ) ) ;
}
}
}
@Test
@Test
public void shouldRemove ( ) {
public void shouldRemove ( ) {
@ -139,9 +151,16 @@ public class RocksDBSessionStoreTest {
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 1500 , 2500 ) ) , 2L ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 1500 , 2500 ) ) , 2L ) ;
sessionStore . remove ( new Windowed < > ( "a" , new SessionWindow ( 0 , 1000 ) ) ) ;
sessionStore . remove ( new Windowed < > ( "a" , new SessionWindow ( 0 , 1000 ) ) ) ;
assertFalse ( sessionStore . findSessions ( "a" , 0 , 1000L ) . hasNext ( ) ) ;
assertTrue ( sessionStore . findSessions ( "a" , 1500 , 2500 ) . hasNext ( ) ) ;
try ( final KeyValueIterator < Windowed < String > , Long > results =
sessionStore . findSessions ( "a" , 0L , 1000L ) ) {
assertFalse ( results . hasNext ( ) ) ;
}
try ( final KeyValueIterator < Windowed < String > , Long > results =
sessionStore . findSessions ( "a" , 1500L , 2500L ) ) {
assertTrue ( results . hasNext ( ) ) ;
}
}
}
@Test
@Test
@ -156,12 +175,15 @@ public class RocksDBSessionStoreTest {
sessionStore . put ( session3 , 3L ) ;
sessionStore . put ( session3 , 3L ) ;
sessionStore . put ( session4 , 4L ) ;
sessionStore . put ( session4 , 4L ) ;
sessionStore . put ( session5 , 5L ) ;
sessionStore . put ( session5 , 5L ) ;
final KeyValueIterator < Windowed < String > , Long > results =
sessionStore . findSessions ( "a" , 150 , 300 ) ;
try ( final KeyValueIterator < Windowed < String > , Long > results =
sessionStore . findSessions ( "a" , 150 , 300 )
) {
assertEquals ( session2 , results . next ( ) . key ) ;
assertEquals ( session2 , results . next ( ) . key ) ;
assertEquals ( session3 , results . next ( ) . key ) ;
assertEquals ( session3 , results . next ( ) . key ) ;
assertFalse ( results . hasNext ( ) ) ;
assertFalse ( results . hasNext ( ) ) ;
}
}
}
@Test
@Test
public void shouldFetchExactKeys ( ) {
public void shouldFetchExactKeys ( ) {
@ -180,37 +202,34 @@ public class RocksDBSessionStoreTest {
sessionStore . init ( context , sessionStore ) ;
sessionStore . init ( context , sessionStore ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 0 , 0 ) ) , 1L ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 0 , 0 ) ) , 1L ) ;
sessionStore . put ( new Windowed < > ( "aa" , new SessionWindow ( 0 , 0 ) ) , 2L ) ;
sessionStore . put ( new Windowed < > ( "aa" , new SessionWindow ( 0 , 1 0) ) , 2L ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 10 , 20 ) ) , 3L ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 10 , 20 ) ) , 3L ) ;
sessionStore . put ( new Windowed < > ( "aa" , new SessionWindow ( 10 , 20 ) ) , 4L ) ;
sessionStore . put ( new Windowed < > ( "aa" , new SessionWindow ( 10 , 20 ) ) , 4L ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 0x7a00000000000000L - 2 , 0x7a00000000000000L - 1 ) ) , 5L ) ;
sessionStore . put ( new Windowed < > ( "a" , new SessionWindow ( 0x7a00000000000000L - 2 , 0x7a00000000000000L - 1 ) ) , 5L ) ;
KeyValueIterator < Windowed < String > , Long > iterator =
try ( final KeyValueIterator < Windowed < String > , Long > iterator =
sessionStore . findSessions ( "a" , 0 , Long . MAX_VALUE ) ;
sessionStore . findSessions ( "a" , 0 , Long . MAX_VALUE )
List < Long > results = new ArrayList < > ( ) ;
) {
while ( iterator . hasNext ( ) ) {
assertThat ( valuesToList ( iterator ) , equalTo ( Arrays . asList ( 1L , 3L , 5L ) ) ) ;
results . add ( iterator . next ( ) . value ) ;
}
}
assertThat ( results , equalTo ( Arrays . asList ( 1L , 3L , 5L ) ) ) ;
try ( final KeyValueIterator < Windowed < String > , Long > iterator =
sessionStore . findSessions ( "aa" , 0 , Long . MAX_VALUE )
) {
iterator = sessionStore . findSessions ( "aa" , 0 , Long . MAX_VALUE ) ;
assertThat ( valuesToList ( iterator ) , equalTo ( Arrays . asList ( 2L , 4L ) ) ) ;
results = new ArrayList < > ( ) ;
while ( iterator . hasNext ( ) ) {
results . add ( iterator . next ( ) . value ) ;
}
}
assertThat ( results , equalTo ( Arrays . asList ( 2L , 4L ) ) ) ;
try ( final KeyValueIterator < Windowed < String > , Long > iterator =
sessionStore . findSessions ( "a" , "aa" , 0 , Long . MAX_VALUE )
) {
assertThat ( valuesToList ( iterator ) , equalTo ( Arrays . asList ( 1L , 3L , 2L , 4L , 5L ) ) ) ;
}
final KeyValueIterator < Windowed < String > , Long > rangeIterator =
try ( final KeyValueIterator < Windowed < String > , Long > iterator =
sessionStore . findSessions ( "a" , "aa" , 0 , Long . MAX_VALUE ) ;
sessionStore . findSessions ( "a" , "aa" , 10 , 0 )
final List < Long > rangeResults = new ArrayList < > ( ) ;
) {
while ( rangeIterator . hasNext ( ) ) {
assertThat ( valuesToList ( iterator ) , equalTo ( Collections . singletonList ( 2L ) ) ) ;
rangeResults . add ( rangeIterator . next ( ) . value ) ;
}
}
assertThat ( rangeResults , equalTo ( Arrays . asList ( 1L , 3L , 2L , 4L , 5L ) ) ) ;
}
}
@Test ( expected = NullPointerException . class )
@Test ( expected = NullPointerException . class )
@ -261,4 +280,12 @@ public class RocksDBSessionStoreTest {
return results ;
return results ;
}
}
static < K , V > List < V > valuesToList ( final KeyValueIterator < Windowed < K > , V > iterator ) {
final List < V > results = new ArrayList < > ( ) ;
while ( iterator . hasNext ( ) ) {
results . add ( iterator . next ( ) . value ) ;
}
return results ;
}
}
}