Search in sources :

Example 21 with ArrayByteIterable

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

the class BTreeReclaimSpecialTest method testStartAddress.

@Test
public void testStartAddress() {
    final long fileSize = log.getFileSize() * LogUtil.LOG_BLOCK_ALIGNMENT;
    log.beginWrite();
    for (long l = 1; l < fileSize; ++l) {
        // fill all file except for one byte with nulls
        log.write(NullLoggable.create());
    }
    log.flush();
    log.endWrite();
    Assert.assertEquals(1, log.getNumberOfFiles());
    Assert.assertTrue(log.getHighAddress() < fileSize);
    tm = new BTreeEmpty(log, true, 1).getMutableCopy();
    final ArrayByteIterable key = key("K");
    for (int i = 0; i <= COUNT; i++) {
        tm.put(key, v(i));
    }
    long saved = saveTree();
    reloadMutableTree(saved);
    Assert.assertEquals(4, log.getNumberOfFiles());
    final long address = 0L;
    log.forgetFile(address);
    // emulate gc of first file
    log.removeFile(address);
    Iterator<RandomAccessLoggable> loggables = log.getLoggableIterator(log.getFileAddress(fileSize * 2));
    // reclaim third file
    tm.reclaim(loggables.next(), loggables);
    saved = saveTree();
    reloadMutableTree(saved);
    log.forgetFile(fileSize * 2);
    // remove reclaimed file
    log.removeFile(fileSize * 2);
    loggables = log.getLoggableIterator(log.getFileAddress(fileSize));
    // reclaim second file
    tm.reclaim(loggables.next(), loggables);
    saved = saveTree();
    reloadMutableTree(saved);
    // make sure that some files were added
    Assert.assertTrue(log.getNumberOfFiles() > 2);
    log.forgetFile(fileSize);
    // remove reclaimed file
    log.removeFile(fileSize);
    try (ITreeCursor cursor = tm.openCursor()) {
        // access minimum key
        Assert.assertTrue(cursor.getNext());
    }
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) RandomAccessLoggable(jetbrains.exodus.log.RandomAccessLoggable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor) Test(org.junit.Test)

Example 22 with ArrayByteIterable

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

the class MetaTree method getAllStoreNames.

@NotNull
List<String> getAllStoreNames() {
    final ITree tree = this.tree;
    if (tree.getSize() == 0) {
        return Collections.emptyList();
    }
    final List<String> result = new ArrayList<>();
    final ITreeCursor cursor = tree.openCursor();
    while (cursor.getNext()) {
        final ArrayByteIterable key = new ArrayByteIterable(cursor.getKey());
        if (isStringKey(key)) {
            final String storeName = StringBinding.entryToString(key);
            if (!EnvironmentImpl.isUtilizationProfile(storeName)) {
                result.add(storeName);
            }
        }
    }
    return result;
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with ArrayByteIterable

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

the class StoreImpl method get.

@Override
@Nullable
public ByteIterable get(@NotNull final Transaction txn, @NotNull final ByteIterable key) {
    final TransactionBase tx = (TransactionBase) txn;
    final ITree tree = tx.getTree(this);
    final long treeRootAddress = tree.getRootAddress();
    final StoreGetCache storeGetCache = environment.getStoreGetCache();
    final boolean useStoreGetCache = treeRootAddress != Loggable.NULL_ADDRESS && storeGetCache != null;
    // if neither tree is empty nor mutable and StoreGetCache is on
    if (useStoreGetCache) {
        ByteIterable result = storeGetCache.tryKey(treeRootAddress, key);
        if (result != null) {
            return result == NULL_CACHED_VALUE ? null : result;
        }
        result = tree.get(key);
        storeGetCache.cacheObject(treeRootAddress, key, result == null ? NULL_CACHED_VALUE : new ArrayByteIterable(result));
        return result;
    }
    return tree.get(key);
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ITree(jetbrains.exodus.tree.ITree) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with ArrayByteIterable

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

the class RandomAccessByteIterable method compare.

private static int compare(final int offset, final int len, final ByteIterable right, Log log, final long address) {
    final LogCache cache = log.cache;
    final int pageSize = log.getCachePageSize();
    final int mask = pageSize - 1;
    long alignedAddress = address + offset;
    long endAddress = alignedAddress + len;
    endAddress -= ((int) endAddress) & mask;
    int leftStep = ((int) alignedAddress) & mask;
    alignedAddress -= leftStep;
    ArrayByteIterable left = cache.getPageIterable(log, alignedAddress);
    final int leftLen = left.getLength();
    if (leftLen <= leftStep) {
        // alignment is >= 0 for sure
        BlockNotFoundException.raise(log, alignedAddress);
    }
    byte[] leftArray = left.getBytesUnsafe();
    final byte[] rightArray = right.getBytesUnsafe();
    final int rightLen = right.getLength();
    int rightStep = 0;
    int limit = Math.min(len, Math.min(leftLen - leftStep, rightLen));
    while (true) {
        while (rightStep < limit) {
            byte b1 = leftArray[leftStep++];
            byte b2 = rightArray[rightStep++];
            if (b1 != b2) {
                return (b1 & 0xff) - (b2 & 0xff);
            }
        }
        if (rightStep == rightLen || alignedAddress >= endAddress) {
            return len - rightLen;
        }
        // move left array to next cache page
        left = cache.getPageIterable(log, alignedAddress += pageSize);
        leftArray = left.getBytesUnsafe();
        leftStep = 0;
        limit = Math.min(len, Math.min(left.getLength() + rightStep, rightLen));
    }
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 25 with ArrayByteIterable

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

the class Users method listUsersBy.

private static void listUsersBy(final Environment env, final Store store, final String key) {
    env.executeInTransaction(new TransactionalExecutable() {

        @Override
        public void execute(@NotNull final Transaction txn) {
            final ArrayByteIterable emailEntry = stringToEntry(key);
            try (Cursor cursor = store.openCursor(txn)) {
                if (cursor.getSearchKey(emailEntry) != null) {
                    boolean hasNext = true;
                    int i = 0;
                    for (; hasNext; ++i, hasNext = cursor.getNext()) {
                        if (!key.equalsIgnoreCase(entryToString(cursor.getKey()))) {
                            break;
                        }
                        System.out.println(entryToString(cursor.getValue()) + ' ' + key);
                    }
                    System.out.println("Total found: " + i);
                } else if (cursor.getSearchKeyRange(emailEntry) != null) {
                    System.out.println(key + " not found, nearest candidates: ");
                    boolean hasNext = true;
                    for (; hasNext; hasNext = cursor.getNext()) {
                        final String currentKey = entryToString(cursor.getKey());
                        if (!currentKey.startsWith(key)) {
                            break;
                        }
                        System.out.println(entryToString(cursor.getValue()) + ' ' + currentKey);
                    }
                } else {
                    System.out.println("Nothing found");
                }
            }
        }
    });
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) StringBinding.entryToString(jetbrains.exodus.bindings.StringBinding.entryToString)

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