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());
}
}
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;
}
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);
}
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));
}
}
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");
}
}
}
});
}
Aggregations