You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by ie...@apache.org on 2008/09/17 16:48:29 UTC

svn commit: r696329 - in /incubator/shindig/trunk/java/samples/src/main: java/org/apache/shindig/social/opensocial/jpa/ resources/META-INF/

Author: ieb
Date: Wed Sep 17 07:48:28 2008
New Revision: 696329

URL: http://svn.apache.org/viewvc?rev=696329&view=rev
Log:
Added application data storage to map to the structure of DataCollection. The storage approach use the DB directly and it
might make more sense in real production to store this as blobs somewhere to avoid too many DB accesses, however it probably
depends on the nature of the access once some tests have been performed since retrieving each map, may be a single query once
JPA gets its hands on it... and that could be as efficient as pulling a single blob and performing the field serialization inside
the JVM. Also I have a feeling that this area will be subject to change... once I find out exactly what the keys are supposed to be
representing. Just need to look, nothing complicated.

Added:
    incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataDb.java
    incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapDb.java
    incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapValueDb.java
Modified:
    incubator/shindig/trunk/java/samples/src/main/resources/META-INF/orm.xml

Added: incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataDb.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataDb.java?rev=696329&view=auto
==============================================================================
--- incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataDb.java (added)
+++ incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataDb.java Wed Sep 17 07:48:28 2008
@@ -0,0 +1,118 @@
+/*
+ * 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.shindig.social.opensocial.jpa;
+
+import static javax.persistence.GenerationType.IDENTITY;
+
+import org.apache.shindig.social.opensocial.jpa.api.DbObject;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.MapKey;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+import java.util.Map;
+
+/**
+ * Represents application data. 
+ */
+@Entity
+@Table(name="application_data")
+public class ApplicationDataDb implements DbObject {
+  /**
+   * The internal object ID used for references to this object. Should be generated by the
+   * underlying storage mechanism
+   */
+  @Id
+  @GeneratedValue(strategy = IDENTITY)
+  @Column(name = "oid")
+  protected long objectId;
+
+  /**
+   * An optimistic locking field.
+   */
+  @Version
+  @Column(name = "version")
+  protected long version;
+  
+  /**
+   * Application data is associated with an application
+   */
+  @ManyToOne(targetEntity=ApplicationDb.class)
+  @JoinColumn(name="application_id", referencedColumnName="oid")
+  protected ApplicationDb application;
+  
+  /**
+   * There is a map of sets of values within an application (question, what are they mapped on ?) 
+   * If this is userID we may have this mapping wrong as we certainly dont want to load all data
+   * for all users to access a single users set of data. Need to check this.
+   * TODO: check what the key really is.
+   */
+  @OneToMany(targetEntity=ApplicationDataMapDb.class, mappedBy="applicationData")
+  @MapKey(name="name")
+  protected Map<String, Map<String, String>> entry;
+
+  /**
+   * @return the application
+   */
+  public ApplicationDb getApplication() {
+    return application;
+  }
+
+  /**
+   * @param application the application to set
+   */
+  public void setApplication(ApplicationDb application) {
+    this.application = application;
+  }
+
+  /**
+   * @return the entry
+   */
+  public Map<String, Map<String, String>> getEntry() {
+    return entry;
+  }
+
+  /**
+   * @param entry the entry to set
+   */
+  public void setEntry(Map<String, Map<String, String>> entry) {
+    this.entry = entry;
+  }
+
+  /**
+   * @return the objectId
+   */
+  public long getObjectId() {
+    return objectId;
+  }
+
+  /**
+   * @return the version
+   */
+  public long getVersion() {
+    return version;
+  }
+
+}

Added: incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapDb.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapDb.java?rev=696329&view=auto
==============================================================================
--- incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapDb.java (added)
+++ incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapDb.java Wed Sep 17 07:48:28 2008
@@ -0,0 +1,189 @@
+/*
+ * 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.shindig.social.opensocial.jpa;
+
+import static javax.persistence.GenerationType.IDENTITY;
+
+import org.apache.shindig.social.opensocial.jpa.api.DbObject;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.MapKey;
+import javax.persistence.OneToMany;
+import javax.persistence.PostLoad;
+import javax.persistence.PrePersist;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * An application data map is the map of data for a single key within an application.
+ */
+@Entity
+@Table(name="application_datamap")
+public class ApplicationDataMapDb extends ConcurrentHashMap<String, String> implements DbObject {
+  /**
+   * The object needs to be seializable (map)
+   */
+  private static final long serialVersionUID = 8017568825925047318L;
+
+  /**
+   * The internal object ID used for references to this object. Should be generated by the
+   * underlying storage mechanism
+   */
+  @Id
+  @GeneratedValue(strategy = IDENTITY)
+  @Column(name = "oid")
+  protected long objectId;
+
+  /**
+   * An optimistic locking field.
+   */
+  @Version
+  @Column(name = "version")
+  protected long version;
+
+  /**
+   * A Application Data Map belongs to a set of maps associated with an application by name.
+   */
+  @ManyToOne(targetEntity=ApplicationDataDb.class)
+  @JoinColumn(name="application_data_id", referencedColumnName="oid")
+  protected ApplicationDataDb applicationData;
+  
+  /**
+   * A named application data map, contains as a map of values.
+   */
+  @OneToMany(targetEntity=ApplicationDataMapValueDb.class, mappedBy="applicationDataMap")
+  @MapKey(name="name")
+  protected Map<String, ApplicationDataMapValueDb> valuesDb;
+    
+  
+  /**
+   * Each map has a name
+   */
+  @Basic
+  @Column(name="name", length=255)
+  protected String name;
+
+  /**
+   * persist the state of object before sending to the db.
+   */
+  @PrePersist
+  public void prePersist() {
+    // add new entries
+    for (Entry<String, String> e : this.entrySet()) {
+      ApplicationDataMapValueDb a = valuesDb.get(e.getKey());
+      if (a == null) {
+        a = new ApplicationDataMapValueDb();
+        a.name = e.getKey();
+        a.value = e.getValue();
+        a.applicationDataMap = this;
+        valuesDb.put(e.getKey(), a);
+      } else {
+        a.value = e.getValue();
+      }
+    }
+    // remove old entries
+    List<String> toRemove = new ArrayList<String>();
+    for (Entry<String, ApplicationDataMapValueDb> e : valuesDb.entrySet()) {
+      if (!this.containsKey(e.getKey())) {
+        toRemove.add(e.getKey());
+      }
+    }
+    for (String r : toRemove) {
+      valuesDb.remove(r);
+    }
+  }
+
+  /**
+   * set the state of the object after load
+   */
+  @PostLoad
+  public void postLoad() {
+    this.clear();
+    for (Entry<String, ApplicationDataMapValueDb> e : valuesDb.entrySet()) {
+      put(e.getKey(), e.getValue().value);
+    }
+  }
+
+  /**
+   * @return the applicationData
+   */
+  public ApplicationDataDb getApplicationData() {
+    return applicationData;
+  }
+
+  /**
+   * @param applicationData the applicationData to set
+   */
+  public void setApplicationData(ApplicationDataDb applicationData) {
+    this.applicationData = applicationData;
+  }
+
+  /**
+   * @return the valuesDb
+   */
+  public Map<String, ApplicationDataMapValueDb> getValuesDb() {
+    return valuesDb;
+  }
+
+  /**
+   * @param valuesDb the valuesDb to set
+   */
+  public void setValuesDb(Map<String, ApplicationDataMapValueDb> valuesDb) {
+    this.valuesDb = valuesDb;
+  }
+
+  /**
+   * @return the objectId
+   */
+  public long getObjectId() {
+    return objectId;
+  }
+
+  /**
+   * @return the version
+   */
+  public long getVersion() {
+    return version;
+  }
+
+  /**
+   * @return the name
+   */
+  public String getName() {
+    return name;
+  }
+
+  /**
+   * @param name the name to set
+   */
+  public void setName(String name) {
+    this.name = name;
+  }
+
+}

Added: incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapValueDb.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapValueDb.java?rev=696329&view=auto
==============================================================================
--- incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapValueDb.java (added)
+++ incubator/shindig/trunk/java/samples/src/main/java/org/apache/shindig/social/opensocial/jpa/ApplicationDataMapValueDb.java Wed Sep 17 07:48:28 2008
@@ -0,0 +1,132 @@
+/*
+ * 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.shindig.social.opensocial.jpa;
+
+import static javax.persistence.GenerationType.IDENTITY;
+
+import org.apache.shindig.social.opensocial.jpa.api.DbObject;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+/**
+ * The final storage of data in the application datamap. Values are limited here to 4K in size.
+ */
+@Entity
+@Table(name="application_datavalue")
+public class ApplicationDataMapValueDb implements DbObject {
+  /**
+   * The internal object ID used for references to this object. Should be generated by the
+   * underlying storage mechanism
+   */
+  @Id
+  @GeneratedValue(strategy = IDENTITY)
+  @Column(name = "oid")
+  protected long objectId;
+
+  /**
+   * An optimistic locking field.
+   */
+  @Version
+  @Column(name = "version")
+  protected long version;
+
+  /**
+   * Each entry is associated with an application Data Map
+   */
+  @ManyToOne(targetEntity=ApplicationDataMapDb.class)
+  @JoinColumn(name="application_datamap_id", referencedColumnName="oid")
+  protected ApplicationDataMapDb applicationDataMap;
+  
+  /**
+   * Each entry has a name
+   */
+  @Basic
+  @Column(name="name", length=255)
+  protected String name;
+  
+  /**
+   * Each entry has a value (4K limit to size)
+   */
+  @Basic
+  @Column(name="value", length=4094)
+  protected String value;
+
+  /**
+   * @return the applicationDataMap
+   */
+  public ApplicationDataMapDb getApplicationDataMap() {
+    return applicationDataMap;
+  }
+
+  /**
+   * @param applicationDataMap the applicationDataMap to set
+   */
+  public void setApplicationDataMap(ApplicationDataMapDb applicationDataMap) {
+    this.applicationDataMap = applicationDataMap;
+  }
+
+  /**
+   * @return the name
+   */
+  public String getName() {
+    return name;
+  }
+
+  /**
+   * @param name the name to set
+   */
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  /**
+   * @return the value
+   */
+  public String getValue() {
+    return value;
+  }
+
+  /**
+   * @param value the value to set
+   */
+  public void setValue(String value) {
+    this.value = value;
+  }
+
+  /**
+   * @return the objectId
+   */
+  public long getObjectId() {
+    return objectId;
+  }
+
+  /**
+   * @return the version
+   */
+  public long getVersion() {
+    return version;
+  }
+}

Modified: incubator/shindig/trunk/java/samples/src/main/resources/META-INF/orm.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/samples/src/main/resources/META-INF/orm.xml?rev=696329&r1=696328&r2=696329&view=diff
==============================================================================
--- incubator/shindig/trunk/java/samples/src/main/resources/META-INF/orm.xml (original)
+++ incubator/shindig/trunk/java/samples/src/main/resources/META-INF/orm.xml Wed Sep 17 07:48:28 2008
@@ -60,4 +60,10 @@
   </entity>
   <entity class="org.apache.shindig.social.opensocial.jpa.ApplicationPropertyDb">
   </entity>
+  <entity class="org.apache.shindig.social.opensocial.jpa.ApplicationDataDb">
+  </entity>
+  <entity class="org.apache.shindig.social.opensocial.jpa.ApplicationDataMapDb">
+  </entity>
+  <entity class="org.apache.shindig.social.opensocial.jpa.ApplicationDataMapValueDb">
+  </entity>
 </entity-mappings>