Search in sources :

Example 1 with Environment

use of jetbrains.exodus.env.Environment in project xodus by JetBrains.

the class UniqueKeyIndicesEngine method updateUniqueKeyIndices.

public void updateUniqueKeyIndices(@NotNull final Iterable<Index> indices) {
    final Environment environment = persistentStore.getEnvironment();
    environment.suspendGC();
    try {
        persistentStore.executeInTransaction(new StoreTransactionalExecutable() {

            @Override
            public void execute(@NotNull StoreTransaction txn) {
                final PersistentStoreTransaction t = (PersistentStoreTransaction) txn;
                final PersistentStoreTransaction snapshot = t.getSnapshot();
                try {
                    final Collection<String> indexNames = new HashSet<>();
                    for (final String dbName : environment.getAllStoreNames(t.getEnvironmentTransaction())) {
                        if (isUniqueKeyIndexName(dbName)) {
                            indexNames.add(dbName);
                        }
                    }
                    for (final Index index : indices) {
                        final String indexName = getUniqueKeyIndexName(index);
                        if (indexNames.contains(indexName)) {
                            indexNames.remove(indexName);
                        } else {
                            createUniqueKeyIndex(t, snapshot, index);
                        }
                    }
                    // remove obsolete indices
                    for (final String indexName : indexNames) {
                        removeObsoleteUniqueKeyIndex(t, indexName);
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("Flush index persistent transaction " + t);
                    }
                    t.flush();
                } finally {
                    // reading snapshot is obsolete now
                    snapshot.abort();
                }
            }
        });
    } finally {
        environment.resumeGC();
    }
}
Also used : Environment(jetbrains.exodus.env.Environment) Index(jetbrains.exodus.query.metadata.Index)

Example 2 with Environment

use of jetbrains.exodus.env.Environment in project xodus by JetBrains.

the class VFSBlobVault method refactorFromFS.

public void refactorFromFS(@NotNull final PersistentEntityStoreImpl store) throws IOException {
    final BlobVault sourceVault = new FileSystemBlobVaultOld(store.getConfig(), store.getLocation(), "blobs", ".blob", BlobHandleGenerator.IMMUTABLE);
    final LongSet allBlobs = store.computeInReadonlyTransaction(new StoreTransactionalComputable<LongSet>() {

        @Override
        public LongSet compute(@NotNull final StoreTransaction txn) {
            return loadAllBlobs(store, (PersistentStoreTransaction) txn);
        }
    });
    final Environment env = fs.getEnvironment();
    final Transaction txn = env.beginTransaction();
    try {
        int i = 0;
        for (final long blobId : allBlobs) {
            if (i++ % 100 == 0) {
                txn.flush();
            }
            final InputStream content = sourceVault.getContent(blobId, txn);
            if (content != null) {
                importBlob(txn, blobId, content);
            }
        }
        txn.flush();
    } catch (final IOException ioe) {
        throw new EntityStoreException(ioe);
    } finally {
        txn.abort();
    }
}
Also used : Transaction(jetbrains.exodus.env.Transaction) Environment(jetbrains.exodus.env.Environment)

Example 3 with Environment

use of jetbrains.exodus.env.Environment in project xodus by JetBrains.

the class FileSystemBlobVaultOld method flushBlobs.

@Override
public void flushBlobs(@Nullable final LongHashMap<InputStream> blobStreams, @Nullable final LongHashMap<File> blobFiles, @Nullable final LongSet deferredBlobsToDelete, @NotNull final Transaction txn) throws Exception {
    if (blobStreams != null) {
        blobStreams.forEachEntry(new ObjectProcedureThrows<Map.Entry<Long, InputStream>, Exception>() {

            @Override
            public boolean execute(final Map.Entry<Long, InputStream> object) throws Exception {
                final InputStream stream = object.getValue();
                stream.reset();
                setContent(object.getKey(), stream);
                return true;
            }
        });
    }
    // if there were blob files then move them
    if (blobFiles != null) {
        blobFiles.forEachEntry(new ObjectProcedureThrows<Map.Entry<Long, File>, Exception>() {

            @Override
            public boolean execute(final Map.Entry<Long, File> object) throws Exception {
                setContent(object.getKey(), object.getValue());
                return true;
            }
        });
    }
    // if there are deferred blobs to delete then defer their deletion
    if (deferredBlobsToDelete != null) {
        final LongArrayList copy = new LongArrayList(deferredBlobsToDelete.size());
        final LongIterator it = deferredBlobsToDelete.iterator();
        while (it.hasNext()) {
            copy.add(it.nextLong());
        }
        final Environment environment = txn.getEnvironment();
        environment.executeTransactionSafeTask(new Runnable() {

            @Override
            public void run() {
                DeferredIO.getJobProcessor().queueIn(new Job() {

                    @Override
                    protected void execute() {
                        final long[] blobHandles = copy.getInstantArray();
                        for (int i = 0; i < copy.size(); ++i) {
                            delete(blobHandles[i]);
                        }
                    }

                    @Override
                    public String getName() {
                        return "Delete obsolete blob files";
                    }

                    @Override
                    public String getGroup() {
                        return environment.getLocation();
                    }
                }, environment.getEnvironmentConfig().getGcFilesDeletionDelay());
            }
        });
    }
}
Also used : LongArrayList(jetbrains.exodus.core.dataStructures.LongArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) Environment(jetbrains.exodus.env.Environment) Job(jetbrains.exodus.core.execution.Job) LongHashMap(jetbrains.exodus.core.dataStructures.hash.LongHashMap) LongIterator(jetbrains.exodus.core.dataStructures.hash.LongIterator)

Example 4 with Environment

use of jetbrains.exodus.env.Environment in project xodus by JetBrains.

the class TestReplayTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // noinspection ResultOfMethodCallIgnored
    dbPath.mkdir();
    final Environment env = Environments.newInstance(dbPath.getPath());
    store = new TestTransactionReplayPersistentEntityStoreImpl(env, "test");
}
Also used : Environment(jetbrains.exodus.env.Environment)

Example 5 with Environment

use of jetbrains.exodus.env.Environment 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)

Aggregations

Environment (jetbrains.exodus.env.Environment)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)1 ByteIterable (jetbrains.exodus.ByteIterable)1 LongArrayList (jetbrains.exodus.core.dataStructures.LongArrayList)1 LongHashMap (jetbrains.exodus.core.dataStructures.hash.LongHashMap)1 LongIterator (jetbrains.exodus.core.dataStructures.hash.LongIterator)1 Job (jetbrains.exodus.core.execution.Job)1 PropertyTypes (jetbrains.exodus.entitystore.tables.PropertyTypes)1 SingleColumnTable (jetbrains.exodus.entitystore.tables.SingleColumnTable)1 Transaction (jetbrains.exodus.env.Transaction)1 Index (jetbrains.exodus.query.metadata.Index)1 IndexField (jetbrains.exodus.query.metadata.IndexField)1