Login | Register
My pages Projects Community openCollabNet

Discussions > commits > svn commit: r86 - trunk/src: main/java/org/tigris/limax test/java/org/tigris/limax

limax
Discussion topic

Back to topic list

svn commit: r86 - trunk/src: main/java/org/tigris/limax test/java/org/tigris/limax

Reply

Author wettin
Full name Karl Wettin
Date 2007-09-30 13:57:12 PDT
Message Author: wettin
Date: 2007-09-30 13:57:11-0700
New Revision: 86

Modified:
   trunk/src/main/java/​org/tigris/limax/Pri​maryEntityIndex.java​
   trunk/src/test/java/​org/tigris/limax/Tes​tAttributes.java

Log:
loadSingleEntity, finds a single instance containing a set of @Indexed @Attribute:s with specific values.

Modified: trunk/src/main/java/​org/tigris/limax/Pri​maryEntityIndex.java​
Url: http://limax.tigris.​org/source/browse/li​max/trunk/src/main/j​ava/org/tigris/limax​/PrimaryEntityIndex.​java?view=diff&r​ev=86&p1=trunk/s​rc/main/java/org/tig​ris/limax/PrimaryEnt​ityIndex.java&p2​=trunk/src/main/java​/org/tigris/limax/Pr​imaryEntityIndex.jav​a&r1=85&r2=8​6
====================​====================​====================​==================
--- trunk/src/main/java/​org/tigris/limax/Pri​maryEntityIndex.java​ (original)
+++ trunk/src/main/java/​org/tigris/limax/Pri​maryEntityIndex.java​ 2007-09-30 13:57:11-0700
@@ -29,10 +29,7 @@
 import org.tigris.limax.ent​ity.Identifiable;
 import org.tigris.limax.ent​ity.LazyLoading;
 import org.tigris.limax.tok​enization.Strategy;
-import org.tigris.limax.uti​l.Denormalizer;
-import org.tigris.limax.uti​l.ReflectionTool;
-import org.tigris.limax.uti​l.SingleHitCollector​;
-import org.tigris.limax.uti​l.SoftReferencedValu​eMap;
+import org.tigris.limax.util.*;
 import org.tigris.silvertej​p.metamodel.classdia​gram.*;
 import org.tigris.silvertej​p.metamodel.classdia​gram.Class;
 import org.tigris.silvertej​p.metamodel.classdia​gram.annotation.Bina​ryAssociationClassEn​d;
@@ -98,7 +95,7 @@
     return txn.isDeleted(getType(), oid);
   }
 
- // todo: why is this class not used?
+
   public class ItemIterator implements Iterator<E> {
 
     private Transaction txn;
@@ -1066,7 +1063,7 @@
   }
 
   /**
- * Makes sure that the other end(s) of the association is bound back to the from end, given it is naviagtable.
+ * Makes sure that the other end of the association is bound back to the from end, given it is naviagtable.
    *
    * @param baend from end
    * @param fieldValue from end java field value of parameter baend
@@ -1083,6 +1080,9 @@
       return;
     }
 
+ // todo check delta prior to calls to re-registering to put() in order to avoid eternal loop calls to couple() via put()?
+
+
     if (baend.getBinaryAsso​ciation().getAssocia​tionClass() != null) {
       // has association class
 
@@ -1110,6 +1110,7 @@
               if (!entity.equals(asso​ciationClassEndBeanP​ropertyAccessor.get(​associationClass))) {
                 associationClassEndB​eanPropertyAccessor.​set(associationClass​, entity);
                 // re-register in transaction for creation or update
+ // todo check delta first to avoid eternal loop calls to couple() via put()?
                 getEntityStore().get​PrimaryEntityIndex(a​ssociationClass).put​(txn, associationClass);
               }
 
@@ -1368,11 +1369,71 @@
     return queryFactory(entity, occur);
   }
 
+
+ /**
+ * Attempts to load the one single instance that matches the @Indexed @Attributes of the entity
+ *
+ * @param txn
+ * @param entity
+ * @return
+ * @throws IOException
+ */
+ public E loadSingleInstance(Transaction txn, E entity) throws IOException {
+
+ BooleanQuery query = new BooleanQuery();
+ for (Attribute attribute : getReflection().getA​ttributes()) {
+ if (EntityTools.isIndex​ed(attribute)) {
+ Object fieldValue;
+ try {
+ fieldValue = EntityTools.getBeanP​ropertyAccessor(attr​ibute).get(entity);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ if (fieldValue != null) {
+ query.add(new BooleanClause(new TermQuery(new Term("@" + attribute.getName(), fieldValue.toString())), BooleanClause.Occur.MUST));
+ }
+ }
+ }
+
+ if (query.getClauses().length == 0) {
+ throw new IOException("No indexed attribute values set in entity");
+ }
+
+ query.add(new BooleanClause(new TermQuery(new Term("class", getReflection().toAb​soluteJavaClassPath(​))), BooleanClause.Occur.MUST));
+
+ ListHitCollector collector = new ListHitCollector();
+ txn.getCombinedSearc​her().search(query, collector);
+ if (collector.getDocume​ntNumbers().size() == 0) {
+ return null;
+ } else if (collector.getDocume​ntNumbers().size() > 1) {
+ throw new IOException("Multiple entities found");
+ }
+
+ return get(txn, ReflectionTool.readL​ong(txn.getCombinedR​eader().document(col​lector.getDocumentNu​mbers().get(0), new FieldSelector() {
+ public FieldSelectorResult accept(String fieldName) {
+ if ("@oid".equals(fieldName)) {
+ return FieldSelectorResult.LOAD;
+ } else {
+ return FieldSelectorResult.NO_LOAD;
+ }
+ }
+ }), "@oid"));
+
+ }
+
+ /**
+ * Creates a query on all attributes with data, tokenized with all strategies.
+ *
+ * @param entity
+ * @param occur
+ * @return
+ * @throws IOException
+ */
   public Query queryFactory(E entity, BooleanClause.Occur occur) throws IOException {
 
     BooleanQuery query = new BooleanQuery();
 
- Class _class = getEntityStore().ref​lect(entity.getClass​());
+ Class _class = getEntityStore().ref​lect(entity.getClass​()); // todo: consider why not just getReflection() ?
     for (Attribute attribute : _class.getAttributes()) {
       Object fieldValue;
       try {

Modified: trunk/src/test/java/​org/tigris/limax/Tes​tAttributes.java
Url: http://limax.tigris.​org/source/browse/li​max/trunk/src/test/j​ava/org/tigris/limax​/TestAttributes.java​?view=diff&rev=8​6&p1=trunk/src/t​est/java/org/tigris/​limax/TestAttributes​.java&p2=trunk/s​rc/test/java/org/tig​ris/limax/TestAttrib​utes.java&r1=85​&r2=86
====================​====================​====================​==================
--- trunk/src/test/java/​org/tigris/limax/Tes​tAttributes.java (original)
+++ trunk/src/test/java/​org/tigris/limax/Tes​tAttributes.java 2007-09-30 13:57:11-0700
@@ -51,6 +51,15 @@
 
     txn = store.newTransaction();
 
+ // test loadSingleInstance
+ Publication publication = new Publication();
+ publication.setAuthor("Erland Loe");
+ publication.setYear(2003);
+ publications.loadSin​gleInstance(txn, publication);
+ assertEquals("Fakta om Finland", publication.getTitle());
+
+ // test secondary entity index
+
     SecondaryEntityIndex​<Integer, Publication> byYear = store.getSecondaryEn​tityIndex(publicatio​ns, Integer.class, "@year");
 
     assertNull(byYear.get(txn, 1542));

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r86 - trunk/src: main/java/org/tigris/limax test/java/org/tigris/limax wettin Karl Wettin 2007-09-30 13:57:12 PDT
Messages per page: