📄 database.java
字号:
* Does nothing if there is no such index.
* @param table class corresponding to the table
* @param key field of the class to be indexed
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @return <code>true</code> if index is deleted, <code>false</code> if index
* is not found
*/
public boolean dropIndex(Class table, String key) {
Table t = locateTable(table, true);
FieldIndex index = (FieldIndex)t.indicesMap.remove(key);
if (index != null) {
t.indices.remove(t.indices.indexOf(index));
return true;
}
return false;
}
/**
* Exclude record from specified index. This method is needed to perform update of indexed
* field (key). Before updating the record, it is necessary to exclude it from indices
* which keys are affected. After updating the field, record should be reinserted in these indices
* using includeInIndex method.<P>
* If there is not table associated with class of this object, then
* database will search for table associated with superclass and so on...<P>
* This method does nothing if there is no index for the specified field.
* @param record object to be excluded from the specified index
* @param key name of the indexed field
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* record class
* @return <code>true</code> if record is excluded from index, <code>false</code> if
* there is no such index
*/
public boolean excludeFromIndex(IPersistent record, String key) {
return excludeFromIndex(record.getClass(), record, key);
}
/**
* Exclude record from specified index. This method is needed to perform update of indexed
* field (key). Before updating the record, it is necessary to exclude it from indices
* which keys are affected. After updating the field, record should be reinserted in these indices
* using includeInIndex method.<P>
* If there is not table associated with class of this object, then
* database will search for table associated with superclass and so on...<P>
* This method does nothing if there is no index for the specified field.
* @param table class corresponding to the table
* @param record object to be excluded from the specified index
* @param key name of the indexed field
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @return <code>true</code> if record is excluded from index, <code>false</code> if
* there is no such index
*/
public boolean excludeFromIndex(Class table, IPersistent record, String key) {
Table t = locateTable(table, true);
FieldIndex index = (FieldIndex)t.indicesMap.get(key);
if (index != null) {
index.remove(record);
return true;
}
return false;
}
/**
* Include record in the specified index. This method is needed to perform update of indexed
* field (key). Before updating the record, it is necessary to exclude it from indices
* which keys are affected using excludeFromIndex method. After updating the field, record should be
* reinserted in these indices using this method.<P>
* If there is not table associated with class of this object, then
* database will search for table associated with superclass and so on...<P>
* This method does nothing if there is no index for the specified field.
* @param record object to be excluded from the specified index
* @param key name of the indexed field
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @return <code>true</code> if record is included in index, <code>false</code> if
* there is no such index
*/
public boolean includeInIndex(IPersistent record, String key) {
return includeInIndex(record.getClass(), record, key);
}
/**
* Include record in the specified index. This method is needed to perform update of indexed
* field (key). Before updating the record, it is necessary to exclude it from indices
* which keys are affected using excludeFromIndex method. After updating the field, record should be
* reinserted in these indices using this method.<P>
* If there is not table associated with class of this object, then
* database will search for table associated with superclass and so on...<P>
* This method does nothing if there is no index for the specified field.
* @param table class corresponding to the table
* @param record object to be excluded from the specified index
* @param key name of the indexed field
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @return <code>true</code> if record is included in index, <code>false</code> if
* there is no such index
*/
public boolean includeInIndex(Class table, IPersistent record, String key) {
Table t = locateTable(table, true);
FieldIndex index = (FieldIndex)t.indicesMap.get(key);
if (index != null) {
index.put(record);
return true;
}
return false;
}
/**
* Select record from specified table
* @param table class corresponding to the table
* @param predicate search predicate
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @return iterator through selected records. This iterator doesn't support remove() method
* @exception CompileError exception is thrown if predicate is not valid JSQL exception
* @exception JSQLRuntimeException exception is thrown if there is runtime error during query execution
*/
public <T extends IPersistent> Iterator<T> select(Class table, String predicate) {
return select(table, predicate, false);
}
/**
* Select record from specified table
* @param table class corresponding to the table
* @param predicate search predicate
* @param forUpdate <code>true</code> if records are selected for update - in this case eclusive lock is set
* for the table to avoid deadlock.
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @return iterator through selected records. This iterator doesn't support remove() method
* @exception CompileError exception is thrown if predicate is not valid JSQL exception
* @exception JSQLRuntimeException exception is thrown if there is runtime error during query execution
*/
public <T extends IPersistent> Iterator<T> select(Class table, String predicate, boolean forUpdate) {
Query q = prepare(table, predicate, forUpdate);
return q.execute(getRecords(table));
}
/**
* Prepare JSQL query. Prepare is needed for queries with parameters. Also
* preparing query can improve speed if query will be executed multiple times
* (using prepare, it is compiled only once).<P>
* To execute prepared query, you should use Query.execute(db.getRecords(XYZ.class)) method
* @param table class corresponding to the table
* @param predicate search predicate
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @exception CompileError exception is thrown if predicate is not valid JSQL exception
*/
public <T extends IPersistent> Query<T> prepare(Class table, String predicate) {
return prepare(table, predicate, false);
}
/**
* Prepare JSQL query. Prepare is needed for queries with parameters. Also
* preparing query can improve speed if query will be executed multiple times
* (using prepare, it is compiled only once).<P>
* To execute prepared query, you should use Query.execute(db.getRecords(XYZ.class)) method
* @param table class corresponding to the table
* @param predicate search predicate
* @param forUpdate <code>true</code> if records are selected for update - in this case eclusive lock is set
* for the table to avoid deadlock.
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
* @exception CompileError exception is thrown if predicate is not valid JSQL exception
*/
public <T extends IPersistent> Query<T> prepare(Class table, String predicate, boolean forUpdate) {
Table t = locateTable(table, forUpdate);
Query q = storage.createQuery();
q.prepare(table, predicate);
Iterator iterator = t.indicesMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
FieldIndex index = (FieldIndex)entry.getValue();
String key = (String)entry.getKey();
q.addIndex(key, index);
}
return q;
}
/**
* Get iterator through all table records
* @param table class corresponding to the table
* @return iterator through all table records.
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
*/
public <T extends IPersistent> Iterator<T> getRecords(Class table) {
return getRecords(table, false);
}
/**
* Get iterator through all table records
* @param table class corresponding to the table
* @param forUpdate <code>true</code> if records are selected for update - in this case eclusive lock is set
* for the table to avoid deadlock.
* @return iterator through all table records.
* @exception StorageError(CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to
* the specified class
*/
public <T extends IPersistent> Iterator<T> getRecords(Class table, boolean forUpdate) {
Table t = locateTable(table, forUpdate);
return t.extent.iterator();
}
/**
* Get storage associated with this database
* @return underlying storage
*/
public Storage getStorage() {
return storage;
}
static class Table extends Persistent {
IPersistentSet extent;
Link indices;
transient HashMap indicesMap = new HashMap();
public void onLoad() {
for (int i = indices.size(); --i >= 0;) {
FieldIndex index = (FieldIndex)indices.get(i);
indicesMap.put(index.getKeyFields()[0].getName(), index);
}
}
}
HashMap tables;
Storage storage;
Index metadata;
boolean multithreaded;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -