1 8 9 package com.sleepycat.persist; 10 11 import java.util.Map ; 12 import java.util.SortedMap ; 13 14 import com.sleepycat.bind.EntityBinding; 15 import com.sleepycat.bind.EntryBinding; 16 import com.sleepycat.collections.StoredSortedMap; 17 import com.sleepycat.je.Database; 18 import com.sleepycat.je.DatabaseConfig; 19 import com.sleepycat.je.DatabaseEntry; 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.LockMode; 22 import com.sleepycat.je.OperationStatus; 23 import com.sleepycat.je.SecondaryDatabase; 24 import com.sleepycat.je.Transaction; 25 import com.sleepycat.persist.model.DeleteAction; 26 import com.sleepycat.persist.model.Relationship; 27 import com.sleepycat.persist.model.SecondaryKey; 28 29 765 public class SecondaryIndex<SK,PK,E> extends BasicIndex<SK,E> { 766 767 private SecondaryDatabase secDb; 768 private Database keysDb; 769 private PrimaryIndex priIndex; 770 private EntityBinding entityBinding; 771 private EntityIndex<SK,PK> keysIndex; 772 private SortedMap <SK,E> map; 773 774 802 public SecondaryIndex(SecondaryDatabase database, 803 Database keysDatabase, 804 PrimaryIndex<PK,E> primaryIndex, 805 Class <SK> secondaryKeyClass, 806 EntryBinding secondaryKeyBinding) 807 throws DatabaseException { 808 809 super(database, secondaryKeyClass, secondaryKeyBinding, 810 new EntityValueAdapter(primaryIndex.getEntityClass(), 811 primaryIndex.getEntityBinding(), 812 true)); 813 secDb = database; 814 keysDb = keysDatabase; 815 priIndex = primaryIndex; 816 entityBinding = primaryIndex.getEntityBinding(); 817 } 818 819 824 public SecondaryDatabase getDatabase() { 825 return secDb; 826 } 827 828 834 public Database getKeysDatabase() { 835 return keysDb; 836 } 837 838 843 public PrimaryIndex<PK,E> getPrimaryIndex() { 844 return priIndex; 845 } 846 847 852 public Class <SK> getKeyClass() { 853 return keyClass; 854 } 855 856 861 public EntryBinding getKeyBinding() { 862 return keyBinding; 863 } 864 865 884 public synchronized EntityIndex<SK,PK> keysIndex() 885 throws DatabaseException { 886 887 if (keysIndex == null) { 888 if (keysDb == null) { 889 DatabaseConfig config = secDb.getConfig(); 890 config.setReadOnly(true); 891 keysDb = db.getEnvironment().openDatabase 892 (null, secDb.getDatabaseName(), config); 893 } 894 keysIndex = new KeysIndex<SK,PK> 895 (keysDb, keyClass, keyBinding, 896 priIndex.getKeyClass(), priIndex.getKeyBinding()); 897 } 898 return keysIndex; 899 } 900 901 916 public EntityIndex<PK,E> subIndex(SK key) 917 throws DatabaseException { 918 919 return new SubIndex(this, entityBinding, key); 920 } 921 922 926 927 public E get(SK key) 928 throws DatabaseException { 929 930 return get(null, key, null); 931 } 932 933 public E get(Transaction txn, SK key, LockMode lockMode) 934 throws DatabaseException { 935 936 DatabaseEntry keyEntry = new DatabaseEntry(); 937 DatabaseEntry pkeyEntry = new DatabaseEntry(); 938 DatabaseEntry dataEntry = new DatabaseEntry(); 939 keyBinding.objectToEntry(key, keyEntry); 940 941 OperationStatus status = 942 secDb.get(txn, keyEntry, pkeyEntry, dataEntry, lockMode); 943 944 if (status == OperationStatus.SUCCESS) { 945 return (E) entityBinding.entryToObject(pkeyEntry, dataEntry); 946 } else { 947 return null; 948 } 949 } 950 951 public Map <SK,E> map() { 952 return sortedMap(); 953 } 954 955 public synchronized SortedMap <SK,E> sortedMap() { 956 if (map == null) { 957 map = new StoredSortedMap(db, keyBinding, entityBinding, true); 958 } 959 return map; 960 } 961 } 962 | Popular Tags |