mirror of https://github.com/apache/cassandra
preparation for AntiEntropyService; patched by Stu Hood, reviewed by junrao for CASSANDRA-193
git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@887574 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c2709044d3
commit
f62d4ec14c
|
|
@ -57,17 +57,15 @@ public class ColumnFamilyStoreTest extends CleanupHelper
|
|||
@Test
|
||||
public void testGetColumnWithWrongBF() throws IOException, ExecutionException, InterruptedException
|
||||
{
|
||||
Table table = Table.open("Keyspace1");
|
||||
ColumnFamilyStore store = table.getColumnFamilyStore("Standard1");
|
||||
List<RowMutation> rms = new LinkedList<RowMutation>();
|
||||
RowMutation rm;
|
||||
|
||||
// add data
|
||||
rm = new RowMutation("Keyspace1", "key1");
|
||||
rm.add(new QueryPath("Standard1", null, "Column1".getBytes()), "asdf".getBytes(), 0);
|
||||
rm.add(new QueryPath("Standard1", null, "Column2".getBytes()), "asdf".getBytes(), 0);
|
||||
rm.apply();
|
||||
store.forceBlockingFlush();
|
||||
rms.add(rm);
|
||||
ColumnFamilyStore store = ColumnFamilyStoreUtils.writeColumnFamily(rms);
|
||||
|
||||
Table table = Table.open("Keyspace1");
|
||||
List<SSTableReader> ssTables = table.getAllSSTablesOnDisk();
|
||||
assertEquals(1, ssTables.size());
|
||||
ssTables.get(0).forceBloomFilterFailures();
|
||||
|
|
@ -106,18 +104,16 @@ public class ColumnFamilyStoreTest extends CleanupHelper
|
|||
*/
|
||||
private void testAntiCompaction(String columnFamilyName, int insertsPerTable) throws IOException, ExecutionException, InterruptedException
|
||||
{
|
||||
Table table = Table.open("Keyspace1");
|
||||
ColumnFamilyStore store = table.getColumnFamilyStore(columnFamilyName);
|
||||
|
||||
List<RowMutation> rms = new ArrayList<RowMutation>();
|
||||
for (int j = 0; j < insertsPerTable; j++)
|
||||
{
|
||||
String key = String.valueOf(j);
|
||||
RowMutation rm = new RowMutation("Keyspace1", key);
|
||||
rm.add(new QueryPath(columnFamilyName, null, "0".getBytes()), new byte[0], j);
|
||||
rm.apply();
|
||||
rms.add(rm);
|
||||
}
|
||||
ColumnFamilyStore store = ColumnFamilyStoreUtils.writeColumnFamily(rms);
|
||||
|
||||
store.forceBlockingFlush();
|
||||
List<Range> ranges = new ArrayList<Range>();
|
||||
IPartitioner partitioner = new CollatingOrderPreservingPartitioner();
|
||||
Range r = new Range(partitioner.getToken("0"), partitioner.getToken("zzzzzzz"));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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 org.apache.cassandra.db;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class ColumnFamilyStoreUtils
|
||||
{
|
||||
/**
|
||||
* Writes out a bunch of rows for a single column family.
|
||||
*
|
||||
* @param rows A group of RowMutations for the same table and column family.
|
||||
* @return The ColumnFamilyStore that was used.
|
||||
*/
|
||||
public static ColumnFamilyStore writeColumnFamily(List<RowMutation> rms) throws IOException, ExecutionException, InterruptedException
|
||||
{
|
||||
RowMutation first = rms.get(0);
|
||||
String tablename = first.getTable();
|
||||
String cfname = first.columnFamilyNames().iterator().next();
|
||||
|
||||
Table table = Table.open(tablename);
|
||||
ColumnFamilyStore store = table.getColumnFamilyStore(cfname);
|
||||
|
||||
for (RowMutation rm : rms)
|
||||
rm.apply();
|
||||
|
||||
store.forceBlockingFlush();
|
||||
return store;
|
||||
}
|
||||
}
|
||||
|
|
@ -23,26 +23,28 @@ import java.io.IOException;
|
|||
import java.util.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.cassandra.CleanupHelper;
|
||||
import org.apache.cassandra.db.DecoratedKey;
|
||||
import org.apache.cassandra.dht.OrderPreservingPartitioner;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
|
||||
public class SSTableTest extends CleanupHelper
|
||||
{
|
||||
@Test
|
||||
public void testSingleWrite() throws IOException {
|
||||
File f = tempSSTableFileName();
|
||||
|
||||
// write test data
|
||||
SSTableWriter writer = new SSTableWriter(f.getAbsolutePath(), 1, new OrderPreservingPartitioner());
|
||||
Random random = new Random();
|
||||
byte[] bytes = new byte[1024];
|
||||
random.nextBytes(bytes);
|
||||
|
||||
String key = Integer.toString(1);
|
||||
writer.append(writer.partitioner.decorateKey(key), bytes);
|
||||
SSTableReader ssTable = writer.closeAndOpenReader(0.01);
|
||||
byte[] bytes = new byte[1024];
|
||||
new Random().nextBytes(bytes);
|
||||
|
||||
TreeMap<String, byte[]> map = new TreeMap<String,byte[]>();
|
||||
map.put(key, bytes);
|
||||
SSTableReader ssTable = SSTableUtils.writeSSTable("singlewrite", map, 1,
|
||||
new OrderPreservingPartitioner(), 0.01);
|
||||
|
||||
// verify
|
||||
verifySingle(ssTable, bytes, key);
|
||||
|
|
@ -50,11 +52,6 @@ public class SSTableTest extends CleanupHelper
|
|||
verifySingle(ssTable, bytes, key);
|
||||
}
|
||||
|
||||
private File tempSSTableFileName() throws IOException
|
||||
{
|
||||
return File.createTempFile("sstable", "-" + SSTable.TEMPFILE_MARKER + "-Data.db");
|
||||
}
|
||||
|
||||
private void verifySingle(SSTableReader sstable, byte[] bytes, String key) throws IOException
|
||||
{
|
||||
BufferedRandomAccessFile file = new BufferedRandomAccessFile(sstable.path, "r");
|
||||
|
|
@ -68,8 +65,6 @@ public class SSTableTest extends CleanupHelper
|
|||
|
||||
@Test
|
||||
public void testManyWrites() throws IOException {
|
||||
File f = tempSSTableFileName();
|
||||
|
||||
TreeMap<String, byte[]> map = new TreeMap<String,byte[]>();
|
||||
for ( int i = 100; i < 1000; ++i )
|
||||
{
|
||||
|
|
@ -77,12 +72,8 @@ public class SSTableTest extends CleanupHelper
|
|||
}
|
||||
|
||||
// write
|
||||
SSTableWriter writer = new SSTableWriter(f.getAbsolutePath(), 1000, new OrderPreservingPartitioner());
|
||||
for (String key: map.navigableKeySet())
|
||||
{
|
||||
writer.append(writer.partitioner.decorateKey(key), map.get(key));
|
||||
}
|
||||
SSTableReader ssTable = writer.closeAndOpenReader(0.01);
|
||||
SSTableReader ssTable = SSTableUtils.writeSSTable("manywrites", map, 1000,
|
||||
new OrderPreservingPartitioner(), 0.01);
|
||||
|
||||
// verify
|
||||
verifyMany(ssTable, map);
|
||||
|
|
@ -105,4 +96,36 @@ public class SSTableTest extends CleanupHelper
|
|||
assert Arrays.equals(bytes2, map.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetIndexedDecoratedKeysFor() throws IOException {
|
||||
final String ssname = "indexedkeys";
|
||||
|
||||
int numkeys = 1000;
|
||||
TreeMap<String, byte[]> map = new TreeMap<String,byte[]>();
|
||||
for ( int i = 0; i < numkeys; i++ )
|
||||
{
|
||||
map.put(Integer.toString(i), "blah".getBytes());
|
||||
}
|
||||
|
||||
// write
|
||||
SSTableReader ssTable = SSTableUtils.writeSSTable(ssname, map, 1000,
|
||||
new OrderPreservingPartitioner(), 0.01);
|
||||
|
||||
|
||||
// verify
|
||||
Predicate<SSTable> cfpred;
|
||||
Predicate<DecoratedKey> dkpred;
|
||||
|
||||
cfpred = new Predicate<SSTable>() {
|
||||
public boolean apply(SSTable ss)
|
||||
{
|
||||
return ss.getColumnFamilyName().equals(ssname);
|
||||
}
|
||||
};
|
||||
dkpred = Predicates.alwaysTrue();
|
||||
int actual = SSTableReader.getIndexedDecoratedKeysFor(cfpred, dkpred).size();
|
||||
assert 0 < actual;
|
||||
assert actual <= Math.ceil((double)numkeys/SSTableReader.indexInterval());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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 org.apache.cassandra.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
|
||||
import org.apache.cassandra.dht.IPartitioner;
|
||||
|
||||
public class SSTableUtils
|
||||
{
|
||||
public static File tempSSTableFileName(String cfname) throws IOException
|
||||
{
|
||||
return File.createTempFile(cfname + "-", "-" + SSTable.TEMPFILE_MARKER + "-Data.db");
|
||||
}
|
||||
|
||||
public static SSTableReader writeSSTable(String cfname, SortedMap<String, byte[]> entries, int expectedKeys, IPartitioner partitioner, double cacheFraction) throws IOException
|
||||
{
|
||||
File f = tempSSTableFileName(cfname);
|
||||
SSTableWriter writer = new SSTableWriter(f.getAbsolutePath(), expectedKeys, partitioner);
|
||||
for (Map.Entry<String, byte[]> entry : entries.entrySet())
|
||||
{
|
||||
writer.append(writer.partitioner.decorateKey(entry.getKey()), entry.getValue());
|
||||
}
|
||||
return writer.closeAndOpenReader(cacheFraction);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue