Search in sources :

Example 1 with ByteArraySizedInputStream

use of jetbrains.exodus.util.ByteArraySizedInputStream in project xodus by JetBrains.

the class PersistentEntityStoreImpl method setBlobString.

public void setBlobString(@NotNull final PersistentStoreTransaction txn, @NotNull final PersistentEntity entity, @NotNull final String blobName, @NotNull final String blobString) throws IOException {
    final int length = blobString.length();
    if (length == 0) {
        createBlobHandle(txn, entity, blobName, null, 0);
    } else {
        final LightByteArrayOutputStream memCopy = new LightByteArrayOutputStream();
        UTFUtil.writeUTF(memCopy, blobString);
        final int streamSize = memCopy.size();
        final ByteArraySizedInputStream copy = new ByteArraySizedInputStream(memCopy.toByteArray(), 0, streamSize);
        final long blobHandle = createBlobHandle(txn, entity, blobName, copy, streamSize);
        if (!isEmptyOrInPlaceBlobHandle(blobHandle)) {
            setBlobFileLength(txn, blobHandle, copy.size());
            txn.addBlob(blobHandle, copy);
        }
    }
}
Also used : LightByteArrayOutputStream(jetbrains.exodus.util.LightByteArrayOutputStream) ByteArraySizedInputStream(jetbrains.exodus.util.ByteArraySizedInputStream)

Example 2 with ByteArraySizedInputStream

use of jetbrains.exodus.util.ByteArraySizedInputStream in project xodus by JetBrains.

the class PersistentEntityStoreImpl method getInPlaceBlobStream.

@Nullable
private Pair<Long, InputStream> getInPlaceBlobStream(@NotNull final PersistentStoreTransaction txn, @NotNull final PersistentEntity entity, @NotNull final String blobName) throws IOException {
    final Pair<Long, ByteIterator> blobInfo = getBlobHandleAndValue(txn, entity, blobName);
    if (blobInfo == null) {
        return null;
    }
    final long blobHandle = blobInfo.getFirst();
    if (blobHandle == EMPTY_BLOB_HANDLE) {
        return new Pair<>(blobHandle, null);
    }
    if (isInPlaceBlobHandle(blobHandle)) {
        final ByteIterator valueIterator = blobInfo.getSecond();
        final int size = (int) CompressedUnsignedLongByteIterable.getLong(valueIterator);
        if (blobHandle == IN_PLACE_BLOB_HANDLE) {
            return new Pair<>(blobHandle, new ByteArraySizedInputStream(ByteIterableBase.readIterator(valueIterator, size)));
        }
        final ByteIterable hashEntry = new ArrayByteIterable(valueIterator);
        final ByteIterable duplicateEntry = getBlobHashesTable(txn, entity.getId().getTypeId()).getDatabase().get(txn.getEnvironmentTransaction(), hashEntry);
        if (duplicateEntry == null) {
            throw new EntityStoreException("No duplicate entry is available to in-place blob reference");
        }
        return new Pair<>(blobHandle, new ByteArraySizedInputStream(ByteIterableBase.readIteratorSafe(duplicateEntry.iterator(), size)));
    }
    return new Pair<>(blobHandle, txn.getBlobStream(blobHandle));
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteArraySizedInputStream(jetbrains.exodus.util.ByteArraySizedInputStream) Pair(jetbrains.exodus.core.dataStructures.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ByteArraySizedInputStream

use of jetbrains.exodus.util.ByteArraySizedInputStream in project xodus by JetBrains.

the class EntityTests method testAsciiUTFDecodingBenchmark.

public void testAsciiUTFDecodingBenchmark() {
    final String s = "This is sample ASCII string of not that great size, but large enough to use in the benchmark";
    TestUtil.time("Constructing string from data input", new Runnable() {

        @Override
        public void run() {
            try {
                final LightByteArrayOutputStream out = new LightByteArrayOutputStream();
                DataOutputStream output = new DataOutputStream(out);
                output.writeUTF(s);
                final InputStream stream = new ByteArraySizedInputStream(out.toByteArray(), 0, out.size());
                stream.mark(Integer.MAX_VALUE);
                for (int i = 0; i < 10000000; i++) {
                    stream.reset();
                    assertEquals(s, new DataInputStream(stream).readUTF());
                }
            } catch (IOException e) {
                throw ExodusException.toEntityStoreException(e);
            }
        }
    });
    TestUtil.time("Constructing strings from bytes", new Runnable() {

        @Override
        public void run() {
            final byte[] bytes = s.getBytes();
            for (int i = 0; i < 10000000; i++) {
                assertEquals(s, UTFUtil.fromAsciiByteArray(bytes, 0, bytes.length));
            }
        }
    });
}
Also used : LightByteArrayOutputStream(jetbrains.exodus.util.LightByteArrayOutputStream) ByteArraySizedInputStream(jetbrains.exodus.util.ByteArraySizedInputStream) ByteArraySizedInputStream(jetbrains.exodus.util.ByteArraySizedInputStream)

Example 4 with ByteArraySizedInputStream

use of jetbrains.exodus.util.ByteArraySizedInputStream in project xodus by JetBrains.

the class BindingUtils method readString.

public static String readString(@NotNull final ByteArrayInputStream stream) {
    int next = stream.read();
    if (next == UTFUtil.NULL_STRING_UTF_VALUE) {
        next = stream.read();
        if (next == 0) {
            return null;
        }
        throw new IllegalArgumentException();
    }
    if (next == 0) {
        return "";
    }
    if (!(stream instanceof ByteArraySizedInputStream)) {
        throw new IllegalArgumentException("ByteArraySizedInputStream is expected");
    }
    final ByteArraySizedInputStream sizedStream = (ByteArraySizedInputStream) stream;
    final byte[] bytes = sizedStream.toByteArray();
    // minus trailing zero
    final char[] chars = new char[sizedStream.size() - 1];
    int i = sizedStream.pos();
    int j = 0;
    do {
        if (next < 128) {
            chars[j++] = (char) next;
        } else {
            final int high = next >> 4;
            if (high == 12 || high == 13) {
                final int char2 = bytes[i++] & 0xff;
                if ((char2 & 0xC0) != 0x80) {
                    throw new IllegalArgumentException();
                }
                chars[j++] = (char) (((next & 0x1F) << 6) | (char2 & 0x3F));
            } else if (high == 14) {
                final int char2 = bytes[i] & 0xff;
                final int char3 = bytes[i + 1] & 0xff;
                i += 2;
                if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
                    throw new IllegalArgumentException();
                }
                chars[j++] = (char) (((next & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F)));
            } else {
                throw new IllegalArgumentException();
            }
        }
        next = bytes[i++] & 0xff;
    } while (next != 0);
    sizedStream.setPos(i);
    return new String(chars, 0, j);
}
Also used : ByteArraySizedInputStream(jetbrains.exodus.util.ByteArraySizedInputStream)

Example 5 with ByteArraySizedInputStream

use of jetbrains.exodus.util.ByteArraySizedInputStream in project xodus by JetBrains.

the class PersistentEntityStoreImpl method findDuplicate.

@Nullable
ByteIterable findDuplicate(@NotNull final PersistentStoreTransaction txn, final int typeId, @NotNull final ByteArraySizedInputStream stream) {
    final ArrayByteIterable duplicateKey = IntegerBinding.intToEntry(stream.hashCode());
    final ByteIterable duplicateEntry = getBlobHashesTable(txn, typeId).getDatabase().get(txn.getEnvironmentTransaction(), duplicateKey);
    return (duplicateEntry != null && stream.equals(new ByteArraySizedInputStream(ByteIterableBase.readIterable(duplicateEntry)))) ? duplicateKey : null;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteArraySizedInputStream(jetbrains.exodus.util.ByteArraySizedInputStream) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ByteArraySizedInputStream (jetbrains.exodus.util.ByteArraySizedInputStream)7 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)2 LightByteArrayOutputStream (jetbrains.exodus.util.LightByteArrayOutputStream)2 Nullable (org.jetbrains.annotations.Nullable)2 ComparableValueType (jetbrains.exodus.bindings.ComparableValueType)1 Pair (jetbrains.exodus.core.dataStructures.Pair)1