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