001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: JoinTest.java,v 1.4.2.3 2008/01/07 15:14:35 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.test;
010:
011: import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import junit.framework.Test;
017:
018: import com.sleepycat.je.DatabaseException;
019: import com.sleepycat.je.Transaction;
020: import com.sleepycat.je.test.TxnTestCase;
021: import com.sleepycat.persist.EntityJoin;
022: import com.sleepycat.persist.EntityStore;
023: import com.sleepycat.persist.ForwardCursor;
024: import com.sleepycat.persist.PrimaryIndex;
025: import com.sleepycat.persist.SecondaryIndex;
026: import com.sleepycat.persist.StoreConfig;
027: import com.sleepycat.persist.model.Entity;
028: import com.sleepycat.persist.model.PrimaryKey;
029: import com.sleepycat.persist.model.SecondaryKey;
030:
031: /**
032: * @author Mark Hayes
033: */
034: public class JoinTest extends TxnTestCase {
035:
036: private static final int N_RECORDS = 5;
037:
038: public static Test suite() {
039: return txnTestSuite(JoinTest.class, null, null);
040: }
041:
042: private EntityStore store;
043: private PrimaryIndex<Integer, MyEntity> primary;
044: private SecondaryIndex<Integer, Integer, MyEntity> sec1;
045: private SecondaryIndex<Integer, Integer, MyEntity> sec2;
046: private SecondaryIndex<Integer, Integer, MyEntity> sec3;
047:
048: /**
049: * Opens the store.
050: */
051: private void open() throws DatabaseException {
052:
053: StoreConfig config = new StoreConfig();
054: config.setAllowCreate(envConfig.getAllowCreate());
055: config.setTransactional(envConfig.getTransactional());
056:
057: store = new EntityStore(env, "test", config);
058:
059: primary = store.getPrimaryIndex(Integer.class, MyEntity.class);
060: sec1 = store.getSecondaryIndex(primary, Integer.class, "k1");
061: sec2 = store.getSecondaryIndex(primary, Integer.class, "k2");
062: sec3 = store.getSecondaryIndex(primary, Integer.class, "k3");
063: }
064:
065: /**
066: * Closes the store.
067: */
068: private void close() throws DatabaseException {
069:
070: store.close();
071: }
072:
073: public void testJoin() throws DatabaseException {
074:
075: open();
076:
077: /*
078: * Primary keys: { 0, 1, 2, 3, 4 }
079: * Secondary k1: { 0:0, 0:1, 0:2, 0:3, 0:4 }
080: * Secondary k2: { 0:0, 1:1, 0:2, 1:3, 0:4 }
081: * Secondary k3: { 0:0, 1:1, 2:2, 0:3, 1:4 }
082: */
083: Transaction txn = txnBegin();
084: for (int i = 0; i < N_RECORDS; i += 1) {
085: MyEntity e = new MyEntity(i, 0, i % 2, i % 3);
086: boolean ok = primary.putNoOverwrite(txn, e);
087: assertTrue(ok);
088: }
089: txnCommit(txn);
090:
091: /*
092: * k1, k2, k3, -> { primary keys }
093: * -1 means don't include the key in the join.
094: */
095: doJoin(0, 0, 0, new int[] { 0 });
096: doJoin(0, 0, 1, new int[] { 4 });
097: doJoin(0, 0, -1, new int[] { 0, 2, 4 });
098: doJoin(-1, 1, 1, new int[] { 1 });
099: doJoin(-1, 2, 2, new int[] {});
100: doJoin(-1, -1, 2, new int[] { 2 });
101:
102: close();
103: }
104:
105: private void doJoin(int k1, int k2, int k3, int[] expectKeys)
106: throws DatabaseException {
107:
108: List<Integer> expect = new ArrayList<Integer>();
109: for (int i : expectKeys) {
110: expect.add(i);
111: }
112: EntityJoin join = new EntityJoin(primary);
113: if (k1 >= 0) {
114: join.addCondition(sec1, k1);
115: }
116: if (k2 >= 0) {
117: join.addCondition(sec2, k2);
118: }
119: if (k3 >= 0) {
120: join.addCondition(sec3, k3);
121: }
122: List<Integer> found;
123: Transaction txn = txnBegin();
124:
125: /* Keys */
126: found = new ArrayList<Integer>();
127: ForwardCursor<Integer> keys = join.keys(txn, null);
128: for (int i : keys) {
129: found.add(i);
130: }
131: keys.close();
132: assertEquals(expect, found);
133:
134: /* Entities */
135: found = new ArrayList<Integer>();
136: ForwardCursor<MyEntity> entities = join.entities(txn, null);
137: for (MyEntity e : entities) {
138: found.add(e.id);
139: }
140: entities.close();
141: assertEquals(expect, found);
142:
143: txnCommit(txn);
144: }
145:
146: @Entity
147: private static class MyEntity {
148: @PrimaryKey
149: int id;
150: @SecondaryKey(relate=MANY_TO_ONE)
151: int k1;
152: @SecondaryKey(relate=MANY_TO_ONE)
153: int k2;
154: @SecondaryKey(relate=MANY_TO_ONE)
155: int k3;
156:
157: private MyEntity() {
158: }
159:
160: MyEntity(int id, int k1, int k2, int k3) {
161: this .id = id;
162: this .k1 = k1;
163: this .k2 = k2;
164: this .k3 = k3;
165: }
166:
167: @Override
168: public String toString() {
169: return "MyEntity " + id + ' ' + k1 + ' ' + k2 + ' ' + k3;
170: }
171: }
172: }
|