Browse files

Initial commit

  • Loading branch information...
0 parents commit f3d5692ef51f2acbec9af8a350b3359bc3bd9290 @fabriciotuosto committed Dec 16, 2010
4 .gitignore
@@ -0,0 +1,4 @@
+.settings
+target
+.classpath
+.project
1 README
@@ -0,0 +1 @@
+Interest in learning mongodb with java driver
49 pom.xml
@@ -0,0 +1,49 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.mongodb.test</groupId>
+ <artifactId>mongodb-learning</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ <filtering>true</filtering>
+ </resource>
+ </resources>
+ <testResources>
+ <testResource>
+ <directory>src/test/resources</directory>
+ <filtering>true</filtering>
+ </testResource>
+ </testResources>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.mongodb</groupId>
+ <artifactId>mongo-java-driver</artifactId>
+ <version>2.3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.jackson</groupId>
+ <artifactId>jackson-mapper-asl</artifactId>
+ <version>1.5.3</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.8.3</version>
+ </dependency>
+ </dependencies>
+</project>
39 src/main/java/org/mongodb/learn/Main.java
@@ -0,0 +1,39 @@
+package org.mongodb.learn;
+
+import java.util.Random;
+
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.Mongo;
+
+public class Main {
+
+ public static void main(String[] args) throws Exception{
+ Mongo mongo = new Mongo("localhost",27017);
+ DB db = mongo.getDB("user");
+
+ DBCollection collection = db.getCollection("user-stats");
+ System.out.println(collection.count());
+ long start = System.currentTimeMillis();
+ Random random = new Random();
+ for (int i = 0; i < 10000000; i++) {
+ UserStats userStats = new UserStats();
+ userStats.setBestPot(1000);
+ userStats.setCredit(9);
+ userStats.setXp(random.nextInt(29));
+ userStats.setFreeCredits(2000);
+ collection.insert(Mapper.toDbObject(userStats));
+ }
+ System.out.println(String.format("Time to insert %dms",(System.currentTimeMillis()-start)));
+
+ start = System.currentTimeMillis();
+ DBCursor cursor = collection.find();
+ for(DBObject object : cursor){
+// System.out.println(object);
+ UserStats userStats = Mapper.fromDbObject(object, UserStats.class);
+ }
+ System.out.println(String.format("Time to read %dms",(System.currentTimeMillis()-start)));
+ }
+}
31 src/main/java/org/mongodb/learn/Mapper.java
@@ -0,0 +1,31 @@
+package org.mongodb.learn;
+
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.commons.beanutils.BeanUtils;
+import org.codehaus.jackson.map.ObjectMapper;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBObject;
+
+public class Mapper {
+ private static final ObjectMapper mapper = new ObjectMapper();
+ public static <T> DBObject toDbObject(T value) throws Exception {
+ BasicDBObject object = new BasicDBObject();
+ @SuppressWarnings("unchecked")
+ Map<String, Object> describe = BeanUtils.describe(value);
+ String id = (String) describe.remove("id");
+ describe.remove("class");
+ if (id == null) {
+ id = UUID.randomUUID().toString();
+ }
+ describe.put("_id", id);
+ object.putAll(describe);
+ return object;
+ }
+
+ public static <T> T fromDbObject(DBObject object, Class<T> clazz) throws Exception {
+ return (T) mapper.readValue(object.toString(), clazz);
+ }
+}
41 src/main/java/org/mongodb/learn/Person.java
@@ -0,0 +1,41 @@
+package org.mongodb.learn;
+
+import org.codehaus.jackson.annotate.JsonProperty;
+
+public class Person{
+ enum Sex{
+ MALE,FEMALE;
+ }
+ @JsonProperty("_id")
+ private String id;
+ private String name;
+ private String dni;
+ private Sex sex;
+
+ public Sex getSex() {
+ return sex;
+ }
+ public void setSex(Sex sex) {
+ this.sex = sex;
+ }
+ public String getId() {
+ return id;
+ }
+ public void setId(String _id) {
+ this.id = _id;
+ }
+
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getDni() {
+ return dni;
+ }
+ public void setDni(String dni) {
+ this.dni = dni;
+ }
+
+}
109 src/main/java/org/mongodb/learn/UserStats.java
@@ -0,0 +1,109 @@
+package org.mongodb.learn;
+
+import java.util.Date;
+
+import org.codehaus.jackson.annotate.JsonProperty;
+
+public class UserStats {
+
+ @JsonProperty("_id")
+ private String id;
+ private long credit;
+ private long coins;
+ private int xp;
+ private int freeCredits;
+ private String bestHand;
+ private int bestPot;
+ private int handsPlayed;
+ private int handsWon;
+ private long totalChipsWon;
+ private long totalChipsLost;
+ private int dailyBonus;
+ private Date dailyBonusUpdate;
+ private int friends;
+
+ public String getId() {
+ return id;
+ }
+ public void setId(String id) {
+ this.id = id;
+ }
+ public long getCredit() {
+ return credit;
+ }
+ public void setCredit(long credit) {
+ this.credit = credit;
+ }
+ public long getCoins() {
+ return coins;
+ }
+ public void setCoins(long coins) {
+ this.coins = coins;
+ }
+ public int getXp() {
+ return xp;
+ }
+ public void setXp(int xp) {
+ this.xp = xp;
+ }
+ public int getFreeCredits() {
+ return freeCredits;
+ }
+ public void setFreeCredits(int freeCredirts) {
+ this.freeCredits = freeCredirts;
+ }
+ public String getBestHand() {
+ return bestHand;
+ }
+ public void setBestHand(String bestHand) {
+ this.bestHand = bestHand;
+ }
+ public int getBestPot() {
+ return bestPot;
+ }
+ public void setBestPot(int bestPot) {
+ this.bestPot = bestPot;
+ }
+ public int getHandsPlayed() {
+ return handsPlayed;
+ }
+ public void setHandsPlayed(int handsPlayed) {
+ this.handsPlayed = handsPlayed;
+ }
+ public int getHandsWon() {
+ return handsWon;
+ }
+ public void setHandsWon(int handsWon) {
+ this.handsWon = handsWon;
+ }
+ public long getTotalChipsWon() {
+ return totalChipsWon;
+ }
+ public void setTotalChipsWon(long totalChipsWon) {
+ this.totalChipsWon = totalChipsWon;
+ }
+ public long getTotalChipsLost() {
+ return totalChipsLost;
+ }
+ public void setTotalChipsLost(long totalChipsLost) {
+ this.totalChipsLost = totalChipsLost;
+ }
+ public int getDailyBonus() {
+ return dailyBonus;
+ }
+ public void setDailyBonus(int dailyBonus) {
+ this.dailyBonus = dailyBonus;
+ }
+ public Date getDailyBonusUpdate() {
+ return dailyBonusUpdate;
+ }
+ public void setDailyBonusUpdate(Date dailyBonusUpdate) {
+ this.dailyBonusUpdate = dailyBonusUpdate;
+ }
+ public int getFriends() {
+ return friends;
+ }
+ public void setFriends(int friends) {
+ this.friends = friends;
+ }
+}

0 comments on commit f3d5692

Please sign in to comment.