initial commit

This commit is contained in:
Alex Shvid
2013-11-10 19:51:25 -08:00
parent 276b809dc5
commit 7bf6e948a8
89 changed files with 6442 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package org.springframework.data.cassandra.config;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.cassandra.core.Keyspace;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
import com.datastax.driver.core.Cluster;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CassandraNamespaceTests {
@Autowired
private ApplicationContext ctx;
@Test
public void testSingleton() throws Exception {
Object cluster = ctx.getBean("cassandra-cluster");
Assert.notNull(cluster);
Assert.isInstanceOf(Cluster.class, cluster);
Object ks = ctx.getBean("cassandra-keyspace");
Assert.notNull(ks);
Assert.isInstanceOf(Keyspace.class, ks);
Cluster c = (Cluster) cluster;
System.out.println(org.apache.commons.beanutils.BeanUtils.describe(c.getConfiguration()));
}
}

View File

@@ -0,0 +1,28 @@
package org.springframework.data.cassandra.config;
import org.junit.Test;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
public class DriverTests {
@Test
public void test() throws Exception {
Cluster.Builder builder = Cluster.builder().addContactPoint("127.0.0.1");
//builder.withCompression(ProtocolOptions.Compression.SNAPPY);
Cluster cluster = builder.build();
Session session = cluster.connect();
session.shutdown();
cluster.shutdown();
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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 org.springframework.data.cassandra.mapping;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.data.util.ClassTypeInformation;
/**
* Unit tests for {@link BasicCassandraPersistentEntity}.
*
* @author Alex Shvid
*/
@RunWith(MockitoJUnitRunner.class)
public class BasicCassandraPersistentEntityUnitTests {
@Mock
ApplicationContext context;
@Test
public void subclassInheritsAtDocumentAnnotation() {
BasicCassandraPersistentEntity<Notification> entity = new BasicCassandraPersistentEntity<Notification>(
ClassTypeInformation.from(Notification.class));
assertThat(entity.getTable(), is("messages"));
}
@Test
public void evaluatesSpELExpression() {
BasicCassandraPersistentEntity<Area> entity = new BasicCassandraPersistentEntity<Area>(
ClassTypeInformation.from(Area.class));
assertThat(entity.getTable(), is("123"));
}
@Test
public void collectionAllowsReferencingSpringBean() {
MappingBean bean = new MappingBean();
bean.userLine = "user_line";
when(context.getBean("mappingBean")).thenReturn(bean);
when(context.containsBean("mappingBean")).thenReturn(true);
BasicCassandraPersistentEntity<UserLine> entity = new BasicCassandraPersistentEntity<UserLine>(
ClassTypeInformation.from(UserLine.class));
entity.setApplicationContext(context);
assertThat(entity.getTable(), is("user_line"));
}
@Table(name = "messages")
class Message {
}
class Notification extends Message {
}
@Table(name = "#{123}")
class Area {
}
@Table(name = "#{mappingBean.userLine}")
class UserLine {
}
class MappingBean {
String userLine;
public String getUserLine() {
return userLine;
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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 org.springframework.data.cassandra.mapping;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Field;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.util.ReflectionUtils;
/**
* Unit test for {@link BasicCassandraPersistentProperty}.
*
* @author Alex Shvid
*/
public class BasicCassandraPersistentPropertyUnitTests {
CassandraPersistentEntity<Timeline> entity;
@Before
public void setup() {
entity = new BasicCassandraPersistentEntity<Timeline>(ClassTypeInformation.from(Timeline.class));
}
@Test
public void usesAnnotatedColumnName() {
Field field = ReflectionUtils.findField(Timeline.class, "text");
assertThat(getPropertyFor(field).getColumnName(), is("message"));
}
@Test
public void checksIdProperty() {
Field field = ReflectionUtils.findField(Timeline.class, "id");
CassandraPersistentProperty property = getPropertyFor(field);
assertThat(property.isIdProperty(), is(true));
}
@Test
public void returnsPropertyNameForUnannotatedProperties() {
Field field = ReflectionUtils.findField(Timeline.class, "time");
assertThat(getPropertyFor(field).getColumnName(), is("time"));
}
@Test
public void checksColumnIdProperty() {
CassandraPersistentProperty property = getPropertyFor(ReflectionUtils.findField(Timeline.class, "time"));
assertThat(property.isColumnId(), is(true));
}
private CassandraPersistentProperty getPropertyFor(Field field) {
return new BasicCassandraPersistentProperty(field, null, entity, new SimpleTypeHolder());
}
class Timeline {
@Id
String id;
@ColumnId
Date time;
@Column("message")
String text;
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* 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 org.springframework.data.cassandra.test;
import java.util.Date;
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.ColumnId;
import org.springframework.data.cassandra.mapping.Qualify;
import org.springframework.data.cassandra.mapping.Table;
import com.datastax.driver.core.DataType;
/**
* This is an example of dynamic table that creates each time new column
* with Post timestamp annotated by @ColumnId.
*
* It is possible to use a static table for posts and identify them by PostId(UUID),
* but in this case we need to use MapReduce for Big Data to find posts for
* particular user, so it is better to have index (userId) -> index (post time)
* architecture. It helps a lot to build eventually a search index for the particular user.
*
* @author Alex Shvid
*/
@Table(name = "comments")
public class Comment {
/*
* Primary Row ID
*/
@Id
private String author;
/*
* Column ID
*/
@ColumnId
@Qualify(type=DataType.Name.TIMESTAMP)
private Date time;
private String text;
@Qualify(type=DataType.Name.SET, typeArguments={DataType.Name.TEXT})
private Set<String> likes;
/*
* Reference to the Post
*/
private String postAuthor;
private Date postTime;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Set<String> getLikes() {
return likes;
}
public void setLikes(Set<String> likes) {
this.likes = likes;
}
public String getPostAuthor() {
return postAuthor;
}
public void setPostAuthor(String postAuthor) {
this.postAuthor = postAuthor;
}
public Date getPostTime() {
return postTime;
}
public void setPostTime(Date postTime) {
this.postTime = postTime;
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* 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 org.springframework.data.cassandra.test;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.ColumnId;
import org.springframework.data.cassandra.mapping.Index;
import org.springframework.data.cassandra.mapping.Table;
/**
* This is an example of dynamic table that creates each time new column
* with Notification timestamp annotated by @ColumnId.
*
* By default it is active Notification until user deactivate it.
* This table uses index on the field active to access in WHERE cause only
* for active notifications.
*
* @author Alex Shvid
*/
@Table(name = "notifications")
public class Notification {
/*
* Primary Row ID
*/
@Id
private String username;
/*
* Column ID
*/
@ColumnId
private Date time;
@Index
private boolean active;
/*
* Reference data
*/
private String type; // comment, post
private String refAuthor;
private Date refTime;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getRefAuthor() {
return refAuthor;
}
public void setRefAuthor(String refAuthor) {
this.refAuthor = refAuthor;
}
public Date getRefTime() {
return refTime;
}
public void setRefTime(Date refTime) {
this.refTime = refTime;
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* 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 org.springframework.data.cassandra.test;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.ColumnId;
import org.springframework.data.cassandra.mapping.Table;
/**
* This is an example of dynamic table that creates each time new column
* with Post timestamp annotated by @ColumnId.
*
* It is possible to use a static table for posts and identify them by PostId(UUID),
* but in this case we need to use MapReduce for Big Data to find posts for
* particular user, so it is better to have index (userId) -> index (post time)
* architecture. It helps a lot to build eventually a search index for the particular user.
*
* @author Alex Shvid
*/
@Table(name = "posts")
public class Post {
/*
* Primary Row ID
*/
@Id
private String author;
/*
* Column ID
*/
@ColumnId
private Date time;
private String type; // status, share
private String text;
private Set<String> resources;
private Map<Date, String> comments;
private Set<String> likes;
private Set<String> followers;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Set<String> getResources() {
return resources;
}
public void setResources(Set<String> resources) {
this.resources = resources;
}
public Map<Date, String> getComments() {
return comments;
}
public void setComments(Map<Date, String> comments) {
this.comments = comments;
}
public Set<String> getLikes() {
return likes;
}
public void setLikes(Set<String> likes) {
this.likes = likes;
}
public Set<String> getFollowers() {
return followers;
}
public void setFollowers(Set<String> followers) {
this.followers = followers;
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* 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 org.springframework.data.cassandra.test;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.ColumnId;
import org.springframework.data.cassandra.mapping.Table;
/**
* This is an example of the users timeline dynamic table, where all columns
* are dynamically created by @ColumnId field value. The rest fields are places
* in Cassandra value.
*
* Timeline entity is used to store user's status updates that it follows in the site.
* Timeline always ordered by @ColumnId field and we can retrieve last top status
* updates by using limits.
*
* @author Alex Shvid
*/
@Table(name="timeline")
public class Timeline {
/*
* Primary Row ID
*/
@Id
private String username;
/*
* Column ID
*/
@ColumnId
private Date time;
/*
* Reference to the post by author and postUID
*/
private String author;
private Date postTime;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getPostTime() {
return postTime;
}
public void setPostTime(Date postTime) {
this.postTime = postTime;
}
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* 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 org.springframework.data.cassandra.test;
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.mapping.Index;
import org.springframework.data.cassandra.mapping.Table;
/**
* This is an example of the Users statis table, where all fields are columns
* in Cassandra row. Some fields can be Set,List,Map like emails.
*
* User contains base information related for separate user, like
* names, additional information, emails, following users, friends.
*
* @author Alex Shvid
*/
@Table(name="users")
public class User {
/*
* Primary Row ID
*/
@Id
private String username;
/*
* Public information
*/
private String firstName;
private String lastName;
/*
* Secondary index, used only on fields with common information,
* not effective on email, username
*/
@Index
private String place;
/*
* User emails
*/
private Set<String> emails;
/*
* Password
*/
private String password;
/*
* Following other users in userline
*/
private Set<String> following;
/*
* Friends of the user
*/
private Set<String> friends;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public Set<String> getEmails() {
return emails;
}
public void setEmails(Set<String> emails) {
this.emails = emails;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<String> getFollowing() {
return following;
}
public void setFollowing(Set<String> following) {
this.following = following;
}
public Set<String> getFriends() {
return friends;
}
public void setFriends(Set<String> friends) {
this.friends = friends;
}
}