Search in sources :

Example 1 with WriteLock

use of org.qi4j.library.locking.WriteLock in project qi4j-sdk by Qi4j.

the class JdbmEntityStoreMixin method applyChanges.

@WriteLock
@Override
public void applyChanges(MapChanges changes) throws IOException {
    try {
        changes.visitMap(new MapChanger() {

            @Override
            public Writer newEntity(final EntityReference ref, EntityDescriptor descriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        super.close();
                        byte[] stateArray = toString().getBytes("UTF-8");
                        long stateIndex = recordManager.insert(stateArray, serializer);
                        String indexKey = ref.toString();
                        index.insert(indexKey.getBytes("UTF-8"), stateIndex, false);
                    }
                };
            }

            @Override
            public Writer updateEntity(final EntityReference ref, EntityDescriptor descriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        super.close();
                        Long stateIndex = getStateIndex(ref.toString());
                        byte[] stateArray = toString().getBytes("UTF-8");
                        recordManager.update(stateIndex, stateArray, serializer);
                    }
                };
            }

            @Override
            public void removeEntity(EntityReference ref, EntityDescriptor descriptor) throws EntityNotFoundException {
                try {
                    Long stateIndex = getStateIndex(ref.toString());
                    recordManager.delete(stateIndex);
                    index.remove(ref.toString().getBytes("UTF-8"));
                } catch (IOException e) {
                    throw new EntityStoreException(e);
                }
            }
        });
        recordManager.commit();
    } catch (Exception e) {
        e.printStackTrace();
        recordManager.rollback();
        if (e instanceof IOException) {
            throw (IOException) e;
        } else if (e instanceof EntityStoreException) {
            throw (EntityStoreException) e;
        } else {
            throw new IOException(e);
        }
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) WriteLock(org.qi4j.library.locking.WriteLock)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 EntityReference (org.qi4j.api.entity.EntityReference)1 WriteLock (org.qi4j.library.locking.WriteLock)1 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)1 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)1