ei.ne.ke.cassandra.cql3.AstyanaxCql3PagingAndSortingTest.java Source code

Java tutorial

Introduction

Here is the source code for ei.ne.ke.cassandra.cql3.AstyanaxCql3PagingAndSortingTest.java

Source

/*
 * Copyright 2013 EK3 Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ei.ne.ke.cassandra.cql3;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.math.RandomUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.mysema.query.types.ConstantImpl;
import com.mysema.query.types.Ops;
import com.mysema.query.types.expr.BooleanOperation;
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.Keyspace;

import ei.ne.ke.cassandra.cql3.example.PagingAndSortingEntity;
import ei.ne.ke.cassandra.cql3.example.PagingAndSortingEntityRepository;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans/astyanax-cql3-paging-and-sorting-test.xml")
public class AstyanaxCql3PagingAndSortingTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(AstyanaxCql3PagingAndSortingTest.class);
    private static final int N = 3000;

    @Autowired
    private AstyanaxContext<Keyspace> keyspaceContext;

    @Autowired
    private PagingAndSortingEntityRepository repository;

    @Before
    public void initializeContext() throws Exception {
        Cassandra.initialize(keyspaceContext.getClient());
    }

    @After
    public void truncateTables() throws Exception {
        Cassandra.truncateTables(keyspaceContext.getClient());
    }

    @Before
    public void populateTable() throws Exception {
        List<PagingAndSortingEntity> entities = Lists.newArrayListWithExpectedSize(N);
        for (int i = 0; i < N; i++) {
            PagingAndSortingEntity entity = new PagingAndSortingEntity("foo", Integer.toString(i), i,
                    RandomUtils.nextInt(N));
            entities.add(entity);
        }
        repository.save(entities);
        Thread.sleep(1000);
    }

    @Test
    @Ignore
    public void everyPage() {
        int pageSize = 25;
        int totalPages = N / 25;
        Stopwatch sw = new Stopwatch();
        for (int pageNumber = 0; pageNumber < N / pageSize; pageNumber++) {
            sw.start();
            Page<PagingAndSortingEntity> page = repository.findAll(new PagingAndSortingEntity.CompoundKey("foo"),
                    new PageableImpl(pageNumber, 25, 0, null));
            Assert.assertEquals(pageNumber, page.getNumber());
            Assert.assertEquals(pageSize, page.getSize());
            Assert.assertEquals(totalPages, page.getTotalPages());
            LOGGER.info("Fetched page {} in {}ms...", pageNumber, sw.elapsed(TimeUnit.MILLISECONDS));
            sw.reset();
        }
    }

    @Test
    @Ignore
    public void everyPagePredicate() {
        int pageSize = 25;
        int totalPages = N / 25;
        Stopwatch sw = new Stopwatch();
        for (int pageNumber = 0; pageNumber < N / pageSize; pageNumber++) {
            sw.start();
            Page<PagingAndSortingEntity> page = repository.findAll(BooleanOperation.create(Ops.EQ,
                    new ConstantImpl<String>("foo"), new ConstantImpl<String>("foo")),
                    new PageableImpl(pageNumber, 25, 0, null));
            Assert.assertEquals(pageNumber, page.getNumber());
            Assert.assertEquals(pageSize, page.getSize());
            Assert.assertEquals(totalPages, page.getTotalPages());
            LOGGER.info("Fetched page {} in {}ms...", pageNumber, sw.elapsed(TimeUnit.MILLISECONDS));
            sw.reset();
        }
    }

}