use of jetbrains.exodus.ArrayByteIterable in project xodus by JetBrains.
the class UniqueKeyIndicesEngine method createUniqueKeyIndex.
private void createUniqueKeyIndex(@NotNull final PersistentStoreTransaction txn, @NotNull final PersistentStoreTransaction snapshot, @NotNull final Index index) {
if (logger.isDebugEnabled()) {
logger.debug("Create index [" + index + ']');
}
final Environment environment = persistentStore.getEnvironment();
final PersistentEntityStoreConfig config = persistentStore.getConfig();
final PropertyTypes propertyTypes = persistentStore.getPropertyTypes();
final List<IndexField> fields = index.getFields();
final int propCount = fields.size();
if (propCount == 0) {
throw new EntityStoreException("Can't create unique key index on empty list of keys.");
}
SingleColumnTable indexTable = null;
Comparable[] props = new Comparable[propCount];
for (final String entityType : getEntityTypesToIndex(index)) {
int i = 0;
for (final Entity entity : snapshot.getAll(entityType)) {
for (int j = 0; j < propCount; ++j) {
final IndexField field = fields.get(j);
if (field.isProperty()) {
if ((props[j] = persistentStore.getProperty(txn, (PersistentEntity) entity, field.getName())) == null) {
throw new EntityStoreException("Can't create unique key index with null property value: " + entityType + '.' + field.getName());
}
} else {
if ((props[j] = entity.getLink(field.getName())) == null) {
throw new EntityStoreException("Can't create unique key index with null link: " + entityType + '.' + field.getName());
}
}
}
if (indexTable == null) {
final String uniqueKeyIndexName = getUniqueKeyIndexName(index);
indexTable = new SingleColumnTable(txn, uniqueKeyIndexName, environment.storeExists(uniqueKeyIndexName, txn.getEnvironmentTransaction()) ? StoreConfig.USE_EXISTING : StoreConfig.WITHOUT_DUPLICATES_WITH_PREFIXING);
}
ArrayByteIterable propsEntry = propertyTypes.dataArrayToEntry(props);
if (!indexTable.getDatabase().add(txn.getEnvironmentTransaction(), propsEntry, LongBinding.longToCompressedEntry(entity.getId().getLocalId()))) {
ByteIterable oldEntityIdEntry = indexTable.getDatabase().get(txn.getEnvironmentTransaction(), propsEntry);
assert oldEntityIdEntry != null;
long oldEntityId = LongBinding.compressedEntryToLong(oldEntityIdEntry);
throw new EntityStoreException("Failed to insert unique key (already exists), index: " + index + ", values = " + Arrays.toString(props) + ", new entity = " + entity + ", old entity id = " + oldEntityId + ", index owner entity type = " + index.getOwnerEntityType());
}
if (++i % 100 == 0) {
txn.flush();
}
}
txn.flush();
}
}
use of jetbrains.exodus.ArrayByteIterable in project xodus by JetBrains.
the class VirtualFileSystem method renameFile.
/**
* Renames {@code origin} file to the specified {@code newPath} and returns {@code true} if the file was actually
* renamed. Otherwise another file with the path {@code newPath} exists. File contents and file descriptor are
* not affected.
*
* @param txn {@linkplain Transaction} instance
* @param origin origin {@linkplain File}
* @param newPath new name of the file
* @return {@code true} if the file was actually renamed, otherwise another file with the path {@code newPath} exists
* @see File
* @see File#getDescriptor()
*/
public boolean renameFile(@NotNull final Transaction txn, @NotNull final File origin, @NotNull final String newPath) {
final ArrayByteIterable key = StringBinding.stringToEntry(newPath);
final ByteIterable value = pathnames.get(txn, key);
if (value != null) {
return false;
}
final File newFile = new File(newPath, origin.getDescriptor(), origin.getCreated(), System.currentTimeMillis());
pathnames.put(txn, key, newFile.toByteIterable());
pathnames.delete(txn, StringBinding.stringToEntry(origin.getPath()));
return true;
}
use of jetbrains.exodus.ArrayByteIterable in project xodus by JetBrains.
the class VirtualFileSystem method doCreateFile.
private File doCreateFile(@NotNull final Transaction txn, final long fileDescriptor, @NotNull String path) {
path = String.format(path, fileDescriptor);
final ArrayByteIterable key = StringBinding.stringToEntry(path);
final ByteIterable value = pathnames.get(txn, key);
if (value != null) {
throw new FileExistsException(path);
}
final long currentTime = System.currentTimeMillis();
final File result = new File(path, fileDescriptor, currentTime, currentTime);
pathnames.put(txn, key, result.toByteIterable());
saveFileDescriptorSequence(txn);
return result;
}
use of jetbrains.exodus.ArrayByteIterable in project xodus by JetBrains.
the class VirtualFileSystem method deleteFile.
/**
* Deletes existing file with the specified {@code path}.
*
* @param txn {@linkplain Transaction} instance
* @param path file path
* @return deleted {@linkplain File} or {@code null} if no file with specified {@code path}exists.
* @see File
*/
@Nullable
public File deleteFile(@NotNull final Transaction txn, @NotNull final String path) {
final ArrayByteIterable key = StringBinding.stringToEntry(path);
final ByteIterable fileMetadata;
try (Cursor cursor = pathnames.openCursor(txn)) {
fileMetadata = cursor.getSearchKey(key);
if (fileMetadata != null) {
cursor.deleteCurrent();
}
}
if (fileMetadata != null) {
final File result = new File(path, fileMetadata);
// at first delete contents
try (ClusterIterator iterator = new ClusterIterator(this, txn, result)) {
while (iterator.hasCluster()) {
iterator.deleteCurrent();
iterator.moveToNext();
}
}
return result;
}
return null;
}
Aggregations