Search in sources :

Example 26 with ArrayByteIterable

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();
    }
}
Also used : PropertyTypes(jetbrains.exodus.entitystore.tables.PropertyTypes) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Environment(jetbrains.exodus.env.Environment) SingleColumnTable(jetbrains.exodus.entitystore.tables.SingleColumnTable) IndexField(jetbrains.exodus.query.metadata.IndexField)

Example 27 with ArrayByteIterable

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;
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 28 with ArrayByteIterable

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;
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 29 with ArrayByteIterable

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;
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)29 ByteIterable (jetbrains.exodus.ByteIterable)15 Transaction (jetbrains.exodus.env.Transaction)4 TransactionalExecutable (jetbrains.exodus.env.TransactionalExecutable)4 Nullable (org.jetbrains.annotations.Nullable)4 Test (org.junit.Test)4 File (java.io.File)3 ByteIterator (jetbrains.exodus.ByteIterator)3 Cursor (jetbrains.exodus.env.Cursor)3 Store (jetbrains.exodus.env.Store)3 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)2 ITreeCursor (jetbrains.exodus.tree.ITreeCursor)2 NotNull (org.jetbrains.annotations.NotNull)2 ArrayList (java.util.ArrayList)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)1 StringBinding.entryToString (jetbrains.exodus.bindings.StringBinding.entryToString)1 Pair (jetbrains.exodus.core.dataStructures.Pair)1 PersistentLong23TreeSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLong23TreeSet)1 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)1