Search in sources :

Example 61 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class PatriciaCursorDecorator method count.

@Override
public int count() {
    int result = 0;
    try (ITreeCursor cursor = patriciaCursor.getTree().openCursor()) {
        @Nullable ByteIterable value = cursor.getSearchKeyRange(new EscapingByteIterable(getKey()));
        while (value != null) {
            if (keyLength != CompressedUnsignedLongByteIterable.getInt(value)) {
                break;
            }
            final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
            if (ByteIterableUtil.compare(keyBytes, keyLength, noDupKey.getBytesUnsafe(), keyLength) != 0) {
                break;
            }
            ++result;
            value = cursor.getNext() ? cursor.getValue() : null;
        }
    }
    return result;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class BTreeCursorDupConcurrentModificationTest method testConcurrentDeleteCurrent.

@Test
public void testConcurrentDeleteCurrent() {
    Cursor c = tm.openCursor();
    final ByteIterable value = value("v51");
    assertTrue(c.getSearchBoth(key(5), value));
    deleteImpl(key(5), value);
    assertTrue(c.getNext());
    assertEquals(key(5), c.getKey());
    assertEquals(value("v52"), c.getValue());
}
Also used : ByteIterable(jetbrains.exodus.ByteIterable) Cursor(jetbrains.exodus.env.Cursor) Test(org.junit.Test)

Example 63 with ByteIterable

use of jetbrains.exodus.ByteIterable 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);
                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 64 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class EnvironmentReadWriteExample method main.

public static void main(String[] args) {
    // Create environment or open existing one
    final Environment env = Environments.newInstance("data");
    // Create or open existing store in environment
    final Store store = env.computeInTransaction(txn -> env.openStore("MyStore", WITHOUT_DUPLICATES, txn));
    @NotNull final ByteIterable key = stringToEntry("myKey");
    @NotNull final ByteIterable value = stringToEntry("myValue");
    // Put "myValue" string under the key "myKey"
    env.executeInTransaction(txn -> store.put(txn, key, value));
    // Read value by key "myKey"
    env.executeInTransaction(txn -> {
        final ByteIterable entry = store.get(txn, key);
        assert entry == value;
        System.out.println(entryToString(entry));
    });
    // Close environment when we are done
    env.close();
}
Also used : ByteIterable(jetbrains.exodus.ByteIterable) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class BTreeReclaimTest method testLeafNoDup.

@Test
public void testLeafNoDup() {
    int p;
    long rootAddress = init(p = 1000);
    tm = ((BTree) (t = new BTree(log, getTreeMutable().getBalancePolicy(), rootAddress, false, 1))).getMutableCopy();
    final ByteIterable key = key(0);
    final ILeafNode savedLeaf = getTree().getRoot().get(key);
    Assert.assertNotNull(savedLeaf);
    final Iterator<RandomAccessLoggable> iter = log.getLoggableIterator(savedLeaf.getAddress());
    Assert.assertTrue(tm.reclaim(iter.next(), iter));
    final AddressIterator addressIterator = getTreeAddresses(getTree());
    while (addressIterator.hasNext()) {
        final long address = addressIterator.next();
        isAffected(log.read(address), key, (BTreeTraverser) addressIterator.getTraverser());
    }
    rootAddress = saveTree();
    checkTree(tm = ((BTree) (t = new BTree(log, getTreeMutable().getBalancePolicy(), rootAddress, false, 1))).getMutableCopy(), p).run();
}
Also used : ByteIterable(jetbrains.exodus.ByteIterable) RandomAccessLoggable(jetbrains.exodus.log.RandomAccessLoggable) Test(org.junit.Test)

Aggregations

ByteIterable (jetbrains.exodus.ByteIterable)93 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)47 Test (org.junit.Test)26 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)16 Nullable (org.jetbrains.annotations.Nullable)11 Cursor (jetbrains.exodus.env.Cursor)8 ITreeCursor (jetbrains.exodus.tree.ITreeCursor)8 RandomAccessLoggable (jetbrains.exodus.log.RandomAccessLoggable)7 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)6 Store (jetbrains.exodus.env.Store)4 TreeSet (java.util.TreeSet)3 TokyoCabinetBenchmark (jetbrains.exodus.benchmark.TokyoCabinetBenchmark)3 PersistentStoreTransaction (jetbrains.exodus.entitystore.PersistentStoreTransaction)3 Exchange (com.persistit.Exchange)2 ExodusException (jetbrains.exodus.ExodusException)2 Pair (jetbrains.exodus.core.dataStructures.Pair)2 HashSet (jetbrains.exodus.core.dataStructures.hash.HashSet)2 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)2 Transaction (jetbrains.exodus.env.Transaction)2 Loggable (jetbrains.exodus.log.Loggable)2