1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.persistance; import java.io.RandomAccessFile; import java.util.Hashtable; import net.jot.logger.JOTLogger; /** * @deprecated * Represents an index for an FSDB database * Used to find data faster and index/vacuum it. * * Note that the data file contains all it needs on it's own. * The index can actually be rebuilt from the data file (ie: using JOTFSQueryImpl.vacuum()) * @author tcolar */ public class JOTFSIndex { /** * @deprecated */ public JOTFSIndex() { } /** * Stores he current highest ID found in the data file, so we can get the next unique higher value using nextval() * @deprecated */ public long highestId = 0; /** * The index file. */ private RandomAccessFile indexFile = null; /** * Use for fast find of a data record * Key: ID / primarey key of the Indexed model. * Value: Offest of that ID data row in the data file. */ private Hashtable dataIndex = new Hashtable(); /** * This indexes the index file itself. * So that when a data offset is updated we can update the corresponsing "pointer" in the index file * This way when we add/delete a value in the table we can quicly update the index on file as well. */ private Hashtable indexIndex = new Hashtable(); /** * Returns the position od a data row given it's index * @deprecated * @param index * @return */ public long getIndexValue(long index) { Long value = (Long) dataIndex.get(new Long(index)); if (value == null) { value = new Long(-1); } return value.longValue(); } /** * Updates the offset of a data file row in the index * * @param index * @param value * @throws java.lang.Exception */ public synchronized void setDataOffset(long id, long offset) throws Exception { // update dataIndex dataIndex.put(new Long(id), new Long(offset)); if (id > highestId) { highestId = id; } } /** * When adding a new entry, call this method to get the next unique index value. * @return */ public synchronized long nextVal() { highestId++; return highestId; } /** * Sets the index file * @param indexFile */ public void setFile(RandomAccessFile indexFile) { this.indexFile = indexFile; } /** * hen a new entry is added to the data file(table), this should be called as well to update the index with it. * @param indexFile * @param id * @param offset * @throws java.lang.Exception */ public synchronized void addEntry(__JOT_PIECE_14__long id, long offset) throws Exception { JOTLogger.log(JOTLogger.CAT_DB, JOTLogger.DEBUG_LEVEL, JOTFSIndexManager.class, "AddIndex " + id); long indexPos = indexFile.length(); // append to index file indexFile.seek(indexFile.length()); indexFile.writeLong(id); indexFile.writeLong(offset); // update indexIndex setIndexIndexOffset(id, indexPos); // add to data index setDataOffset(id, offset); } public void closeFile() { try { indexFile.close(); } catch (Exception e) { JOTLogger.logException(JOTLogger.CAT_DB, JOTLogger.DEBUG_LEVEL, JOTFSIndexManager.class, "Failed to close index file.", e); } } /** * Call this after removing an entry from the data(table) * This will remove it from the index as well. * @param id * @throws java.lang.Exception */ public void deleteEntry(long id) throws Exception { Long indexPtr = (Long) indexIndex.get(new Long(id)); // remove from index on file indexFile.seek(indexPtr.longValue()); indexFile.writeLong(0); indexFile.writeLong(0); // remove from memory dataIndex.remove(new Long(id)); indexIndex.remove(new Long(id)); } /** * Updates the IndexIndex * @param id * @param indexPos */ public void setIndexIndexOffset(long id, long indexPos) { indexIndex.put(new Long(id), new Long(indexPos)); } /*public void save() throws Exception { JOTLogger.log(JOTConstants.LOG_LVL_DEBUG,JOTFSIndexManager.class,"SaveIndex model"); synchronized(JOTFSIndex.class) { indexFile.seek(0); Enumeration e=keys(); while(e.hasMoreElements()) { Long key=(Long)e.nextElement(); long dataId=key.longValue(); long value=getIndexValue(key.longValue()); JOTLogger.log(JOTConstants.LOG_LVL_JDBC_TRACE,JOTFSIndexManager.class,"Writing to Index: "+dataId+" : "+value); indexFile.writeLong(dataId); indexFile.writeLong(value); } } }*/ }