Skip to content

Commit

Permalink
Issue #1761 was partially fixed. Without tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii0lomakin committed Sep 22, 2014
1 parent 11e0cdf commit 21e91b5
Show file tree
Hide file tree
Showing 19 changed files with 779 additions and 648 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,28 @@ public OIndexInternal<?> createIndex(ODatabaseRecordInternal database, String in
}

private OIndexInternal<?> createSBTreeIndex(String indexType, String valueContainerAlgorithm, ODocument metadata) {
Boolean durableInNonTxMode;
Object durable = null;
if (metadata != null)
durable = metadata.field("durableInNonTxMode");

if (durable instanceof Boolean)
durableInNonTxMode = (Boolean) durable;
else
durableInNonTxMode = null;

if (OClass.INDEX_TYPE.UNIQUE.toString().equals(indexType)) {
return new OIndexUnique(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<OIdentifiable>(), valueContainerAlgorithm, metadata);
return new OIndexUnique(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<OIdentifiable>(durableInNonTxMode),
valueContainerAlgorithm, metadata);
} else if (OClass.INDEX_TYPE.NOTUNIQUE.toString().equals(indexType)) {
return new OIndexNotUnique(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<Set<OIdentifiable>>(), valueContainerAlgorithm, metadata);
return new OIndexNotUnique(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<Set<OIdentifiable>>(durableInNonTxMode),
valueContainerAlgorithm, metadata);
} else if (OClass.INDEX_TYPE.FULLTEXT.toString().equals(indexType)) {
return new OIndexFullText(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<Set<OIdentifiable>>(), valueContainerAlgorithm,
metadata);
return new OIndexFullText(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<Set<OIdentifiable>>(durableInNonTxMode),
valueContainerAlgorithm, metadata);
} else if (OClass.INDEX_TYPE.DICTIONARY.toString().equals(indexType)) {
return new OIndexDictionary(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<OIdentifiable>(), valueContainerAlgorithm, metadata);
return new OIndexDictionary(indexType, SBTREE_ALGORITHM, new OSBTreeIndexEngine<OIdentifiable>(durableInNonTxMode),
valueContainerAlgorithm, metadata);
}

throw new OConfigurationException("Unsupported type : " + indexType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.db.record.ODatabaseRecordInternal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
Expand Down Expand Up @@ -220,6 +219,7 @@ public OIndexInternal<?> create(final String name, final OIndexDefinition indexD
else
this.clustersToIndex = new HashSet<String>();

markStorageDirty();
indexEngine.create(this.name, indexDefinition, clusterIndexName, valueSerializer, isAutomatic());

if (rebuild)
Expand Down Expand Up @@ -360,6 +360,8 @@ public long rebuild(final OProgressListener iProgressListener) {
try {
acquireExclusiveLock();
try {
markStorageDirty();

rebuildThread = Thread.currentThread();
rebuilding = true;

Expand Down Expand Up @@ -419,17 +421,39 @@ public long rebuild(final OProgressListener iProgressListener) {
}

public boolean remove(Object key, final OIdentifiable value) {
checkForRebuild();
return remove(key);
}

key = getCollatingValue(key);
protected void startStorageAtomicOperation() {
try {
((OAbstractPaginatedStorage) getDatabase().getStorage()).startAtomicOperation();
} catch (IOException e) {
throw new OIndexException("Error during start of atomic operation", e);
}
}

modificationLock.requestModificationLock();
protected void commitStorageAtomicOperation() {
try {
return remove(key);
} finally {
modificationLock.releaseModificationLock();
((OAbstractPaginatedStorage) getDatabase().getStorage()).commitAtomicOperation();
} catch (IOException e) {
throw new OIndexException("Error during commit of atomic operation", e);
}
}

protected void rollbackStorageAtomicOperation() {
try {
((OAbstractPaginatedStorage) getDatabase().getStorage()).rollbackAtomicOperation();
} catch (IOException e) {
throw new OIndexException("Error during rollback of atomic operation", e);
}
}

protected void markStorageDirty() {
try {
((OAbstractPaginatedStorage) getDatabase().getStorage()).markDirty();
} catch (IOException e) {
throw new OIndexException("Can not mark storage as dirty", e);
}
}

public boolean remove(Object key) {
Expand All @@ -438,10 +462,10 @@ public boolean remove(Object key) {
key = getCollatingValue(key);

modificationLock.requestModificationLock();

try {
acquireSharedLock();
try {
markStorageDirty();
return indexEngine.remove(key);
} finally {
releaseSharedLock();
Expand All @@ -459,6 +483,7 @@ public OIndex<T> clear() {
try {
acquireSharedLock();
try {
markStorageDirty();
indexEngine.clear();
return this;
} finally {
Expand All @@ -476,6 +501,7 @@ public OIndexInternal<T> delete() {
acquireExclusiveLock();

try {
markStorageDirty();
indexEngine.delete();

// REMOVE THE INDEX ALSO FROM CLASS MAP
Expand All @@ -500,6 +526,7 @@ public void deleteWithoutIndexLoad(String indexName) {
try {
acquireExclusiveLock();
try {
markStorageDirty();
indexEngine.deleteWithoutLoad(indexName);
} finally {
releaseExclusiveLock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
*/
public class OIndexDictionary extends OIndexOneValue {

public OIndexDictionary(String typeId, String algorithm, OIndexEngine<OIdentifiable> engine, String valueContainerAlgorithm, ODocument metadata) {
public OIndexDictionary(String typeId, String algorithm, OIndexEngine<OIdentifiable> engine, String valueContainerAlgorithm,
ODocument metadata) {
super(typeId, algorithm, engine, valueContainerAlgorithm, metadata);
}

Expand All @@ -42,6 +43,7 @@ public OIndexOneValue put(Object key, final OIdentifiable value) {
checkForKeyType(key);
acquireSharedLock();
try {
markStorageDirty();
indexEngine.put(key, value);
return this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public OIndexFullText put(Object key, final OIdentifiable iSingleValue) {
// FOREACH WORD CREATE THE LINK TO THE CURRENT DOCUMENT
for (final String word : words) {
acquireExclusiveLock();

startStorageAtomicOperation();
try {
Set<OIdentifiable> refs;

Expand All @@ -109,6 +109,10 @@ public OIndexFullText put(Object key, final OIdentifiable iSingleValue) {
// SAVE THE INDEX ENTRY
indexEngine.put(word, refs);

commitStorageAtomicOperation();
} catch (RuntimeException e) {
rollbackStorageAtomicOperation();
throw new OIndexException("Error during put of key - value entry", e);
} finally {
releaseExclusiveLock();
}
Expand Down Expand Up @@ -143,6 +147,7 @@ public boolean remove(Object key, final OIdentifiable value) {

for (final String word : words) {
acquireExclusiveLock();
startStorageAtomicOperation();
try {

final Set<OIdentifiable> recs = indexEngine.get(word);
Expand All @@ -155,7 +160,11 @@ public boolean remove(Object key, final OIdentifiable value) {
removed = true;
}
}
} finally {
commitStorageAtomicOperation();
} catch (RuntimeException e) {
rollbackStorageAtomicOperation();
throw new OIndexException("Error during removal of entry by key and value", e);
} finally {
releaseExclusiveLock();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public OIndex<?> createIndex(final String iName, final String iType, final OInde
if (indexes.containsKey(iName.toLowerCase()))
throw new OIndexException("Index with name " + iName.toLowerCase() + " already exists.");

// manual indexes are always durable
if (clusterIdsToIndex == null || clusterIdsToIndex.length == 0) {
if (metadata == null)
metadata = new ODocument();

Object durable = metadata.field("durableInNonTxMode");
if (!(durable instanceof Boolean))
metadata.field("durableInNonTxMode", true);
}

index = OIndexes.createIndex(getDatabase(), iType, algorithm, valueContainerAlgorithm, metadata);

// decide which cluster to use ("index" - for automatic and "manindex" for manual)
Expand Down Expand Up @@ -533,7 +543,6 @@ private OIndexInternal<?> createIndex(ODocument idx) {
String algorithm = idx.field(OIndexInternal.ALGORITHM);
String valueContainerAlgorithm = idx.field(OIndexInternal.VALUE_CONTAINER_ALGORITHM);


ODocument metadata = idx.field(OIndexInternal.METADATA);
if (indexType == null) {
OLogManager.instance().error(this, "Index type is null, will process other record.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public OIndexMultiValues put(Object key, final OIdentifiable iSingleValue) {
try {
checkForKeyType(key);
acquireExclusiveLock();
startStorageAtomicOperation();
try {
Set<OIdentifiable> values = indexEngine.get(key);

Expand All @@ -117,8 +118,12 @@ public OIndexMultiValues put(Object key, final OIdentifiable iSingleValue) {
values.add(iSingleValue.getIdentity());
indexEngine.put(key, values);

commitStorageAtomicOperation();
return this;

} catch (RuntimeException e) {
rollbackStorageAtomicOperation();
throw new OIndexException("Error during insertion of key in index", e);
} finally {
releaseExclusiveLock();
}
Expand Down Expand Up @@ -166,23 +171,32 @@ public boolean remove(Object key, final OIdentifiable value) {

try {
acquireExclusiveLock();
startStorageAtomicOperation();
try {

Set<OIdentifiable> values = indexEngine.get(key);

if (values == null)
if (values == null) {
commitStorageAtomicOperation();
return false;
}

if (values.remove(value)) {
if (values.isEmpty())
indexEngine.remove(key);
else
indexEngine.put(key, values);

commitStorageAtomicOperation();
return true;
}

commitStorageAtomicOperation();
return false;

} catch (RuntimeException e) {
rollbackStorageAtomicOperation();
throw new OIndexException("Error during removal of entry by key", e);
} finally {
releaseExclusiveLock();
}
Expand Down Expand Up @@ -393,19 +407,19 @@ public OIndexCursor cursor() {
}
}

@Override
public OIndexCursor descCursor() {
checkForRebuild();
@Override
public OIndexCursor descCursor() {
checkForRebuild();

acquireSharedLock();
try {
return indexEngine.descCursor(MultiValuesTransformer.INSTANCE);
} finally {
releaseSharedLock();
}
}
acquireSharedLock();
try {
return indexEngine.descCursor(MultiValuesTransformer.INSTANCE);
} finally {
releaseSharedLock();
}
}

private static final class MultiValuesTransformer implements OIndexEngine.ValuesTransformer<Set<OIdentifiable>> {
private static final class MultiValuesTransformer implements OIndexEngine.ValuesTransformer<Set<OIdentifiable>> {
private static final MultiValuesTransformer INSTANCE = new MultiValuesTransformer();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public OIndexOneValue put(Object key, final OIdentifiable iSingleValue) {
if (!iSingleValue.getIdentity().isPersistent())
((ORecord) iSingleValue.getRecord()).save();

markStorageDirty();

indexEngine.put(key, iSingleValue.getIdentity());
return this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ public final class OHashTableIndexEngine<V> implements OIndexEngine<V> {

private volatile ORID identity;

public OHashTableIndexEngine() {
public OHashTableIndexEngine(Boolean durableInNonTxMode) {
hashFunction = new OMurmurHash3HashFunction<Object>();

boolean durableInNonTx;
if (durableInNonTxMode == null)
durableInNonTx = OGlobalConfiguration.INDEX_DURABLE_IN_NON_TX_MODE.getValueAsBoolean();
else
durableInNonTx = durableInNonTxMode;

hashTable = new OLocalHashTable<Object, V>(METADATA_FILE_EXTENSION, TREE_FILE_EXTENSION, BUCKET_FILE_EXTENSION,
NULL_BUCKET_FILE_EXTENSION, hashFunction, OGlobalConfiguration.INDEX_DURABLE_IN_NON_TX_MODE.getValueAsBoolean());
NULL_BUCKET_FILE_EXTENSION, hashFunction, durableInNonTx);
}

@Override
Expand Down
Loading

0 comments on commit 21e91b5

Please sign in to comment.