Return-Path: Delivered-To: apmail-incubator-cassandra-commits-archive@minotaur.apache.org Received: (qmail 67038 invoked from network); 12 Feb 2010 18:26:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 12 Feb 2010 18:26:28 -0000 Received: (qmail 17801 invoked by uid 500); 12 Feb 2010 18:26:28 -0000 Delivered-To: apmail-incubator-cassandra-commits-archive@incubator.apache.org Received: (qmail 17754 invoked by uid 500); 12 Feb 2010 18:26:27 -0000 Mailing-List: contact cassandra-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cassandra-dev@incubator.apache.org Delivered-To: mailing list cassandra-commits@incubator.apache.org Received: (qmail 17744 invoked by uid 99); 12 Feb 2010 18:26:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Feb 2010 18:26:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Feb 2010 18:26:15 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id BAE4623888FE; Fri, 12 Feb 2010 18:25:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r909549 - in /incubator/cassandra/trunk/contrib/javautils: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/cassandra/ src/main/java/org/apache/cassandra/contrib/ src/main/java/org/apach... Date: Fri, 12 Feb 2010 18:25:55 -0000 To: cassandra-commits@incubator.apache.org From: gdusbabek@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100212182555.BAE4623888FE@eris.apache.org> Author: gdusbabek Date: Fri Feb 12 18:25:52 2010 New Revision: 909549 URL: http://svn.apache.org/viewvc?rev=909549&view=rev Log: clean up data directories + unit test for CassandraService. Patch by Ran Tavory, reviewed by Gary Dusbabek. CASSANDRA-782 Added: incubator/cassandra/trunk/contrib/javautils/ incubator/cassandra/trunk/contrib/javautils/README incubator/cassandra/trunk/contrib/javautils/build.xml incubator/cassandra/trunk/contrib/javautils/src/ incubator/cassandra/trunk/contrib/javautils/src/main/ incubator/cassandra/trunk/contrib/javautils/src/main/java/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java incubator/cassandra/trunk/contrib/javautils/src/test/ incubator/cassandra/trunk/contrib/javautils/src/test/java/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java Added: incubator/cassandra/trunk/contrib/javautils/README URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/README?rev=909549&view=auto ============================================================================== --- incubator/cassandra/trunk/contrib/javautils/README (added) +++ incubator/cassandra/trunk/contrib/javautils/README Fri Feb 12 18:25:52 2010 @@ -0,0 +1,8 @@ +Java utilities for cassandra clients + +BUILD and test: + +1. ant the top-level cassandra project +2. cd here +3. ant test + Added: incubator/cassandra/trunk/contrib/javautils/build.xml URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/build.xml?rev=909549&view=auto ============================================================================== --- incubator/cassandra/trunk/contrib/javautils/build.xml (added) +++ incubator/cassandra/trunk/contrib/javautils/build.xml Fri Feb 12 18:25:52 2010 @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Added: incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java?rev=909549&view=auto ============================================================================== --- incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java (added) +++ incubator/cassandra/trunk/contrib/javautils/src/main/java/org/apache/cassandra/contrib/utils/service/CassandraServiceDataCleaner.java Fri Feb 12 18:25:52 2010 @@ -0,0 +1,101 @@ +/* +* 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.contrib.utils.service; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.io.util.FileUtils; + +/** + * A cleanup utility that wipes the cassandra data directories. + * + * @author Ran Tavory (rantav@gmail.com) + * + */ +public class CassandraServiceDataCleaner { + + /** + * Creates all data dir if they don't exist and cleans them + * @throws IOException + */ + public void prepare() throws IOException { + makeDirsIfNotExist(); + cleanupDataDirectories(); + } + + /** + * Deletes all data from cassandra data directories, including the commit log. + * @throws IOException in case of permissions error etc. + */ + public void cleanupDataDirectories() throws IOException { + for (String s: getDataDirs()) { + cleanDir(s); + } + } + /** + * Creates the data diurectories, if they didn't exist. + * @throws IOException if directories cannot be created (permissions etc). + */ + public void makeDirsIfNotExist() throws IOException { + for (String s: getDataDirs()) { + mkdir(s); + } + } + + /** + * Collects all data dirs and returns a set of String paths on the file system. + * + * @return + */ + private Set getDataDirs() { + Set dirs = new HashSet(); + for (String s : DatabaseDescriptor.getAllDataFileLocations()) { + dirs.add(s); + } + dirs.add(DatabaseDescriptor.getLogFileLocation()); + return dirs; + } + /** + * Creates a directory + * + * @param dir + * @throws IOException + */ + private void mkdir(String dir) throws IOException { + FileUtils.createDirectory(dir); + } + + /** + * Removes all directory content from file the system + * + * @param dir + * @throws IOException + */ + private void cleanDir(String dir) throws IOException { + File dirFile = new File(dir); + if (dirFile.exists() && dirFile.isDirectory()) { + FileUtils.delete(dirFile.listFiles()); + } + } +} Added: incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java?rev=909549&view=auto ============================================================================== --- incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java (added) +++ incubator/cassandra/trunk/contrib/javautils/src/test/java/org/apache/cassandra/contrib/utils/service/CassandraServiceTest.java Fri Feb 12 18:25:52 2010 @@ -0,0 +1,119 @@ +/* +* 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.contrib.utils.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import org.apache.cassandra.service.EmbeddedCassandraService; +import org.apache.cassandra.thrift.Cassandra; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.apache.cassandra.thrift.ColumnPath; +import org.apache.cassandra.thrift.ConsistencyLevel; +import org.apache.cassandra.thrift.InvalidRequestException; +import org.apache.cassandra.thrift.NotFoundException; +import org.apache.cassandra.thrift.TimedOutException; +import org.apache.cassandra.thrift.UnavailableException; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Example how to use an embedded and a data cleaner. + * + * @author Ran Tavory (rantav@gmail.com) + * + */ +public class CassandraServiceTest { + + private static EmbeddedCassandraService cassandra; + + /** + * Set embedded cassandra up and spawn it in a new thread. + * + * @throws TTransportException + * @throws IOException + * @throws InterruptedException + */ + @BeforeClass + public static void setup() throws TTransportException, IOException, + InterruptedException { + // Tell cassandra where the configuration files are. + // Use the test configuration file. + System.setProperty("storage-config", "../../test/conf"); + + CassandraServiceDataCleaner cleaner = new CassandraServiceDataCleaner(); + cleaner.prepare(); + cassandra = new EmbeddedCassandraService(); + cassandra.init(); + Thread t = new Thread(cassandra); + t.setDaemon(true); + t.start(); + } + + + @Test + public void testInProcessCassandraServer() + throws UnsupportedEncodingException, InvalidRequestException, + UnavailableException, TimedOutException, TException, + NotFoundException { + Cassandra.Client client = getClient(); + + String key_user_id = "1"; + + long timestamp = System.currentTimeMillis(); + ColumnPath cp = new ColumnPath("Standard1"); + cp.setColumn("name".getBytes("utf-8")); + + // insert + client.insert("Keyspace1", key_user_id, cp, "Ran".getBytes("UTF-8"), + timestamp, ConsistencyLevel.ONE); + + // read + ColumnOrSuperColumn got = client.get("Keyspace1", key_user_id, cp, + ConsistencyLevel.ONE); + + // assert + assertNotNull("Got a null ColumnOrSuperColumn", got); + assertEquals("Ran", new String(got.getColumn().getValue(), "utf-8")); + } + + /** + * Gets a connection to the localhost client + * + * @return + * @throws TTransportException + */ + private Cassandra.Client getClient() throws TTransportException { + TTransport tr = new TSocket("localhost", 9170); + TProtocol proto = new TBinaryProtocol(tr); + Cassandra.Client client = new Cassandra.Client(proto); + tr.open(); + return client; + } +}