Android Open Source - Jello Simple Transaction Manager






From Project

Back to project page Jello.

License

The source code is released under:

Apache License

If you think the Android project Jello listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.atteo.jello.transaction;
/*from   w  ww . j av  a  2s  .  c om*/
import android.util.Pool;

import com.atteo.jello.PageUsage;
import com.atteo.jello.Record;
import com.atteo.jello.Storable;
import com.atteo.jello.index.BTree;
import com.atteo.jello.index.Index;
import com.atteo.jello.klass.KlassManager;
import com.atteo.jello.schema.Schema;
import com.atteo.jello.schema.SchemaManager;
import com.atteo.jello.schema.StorableWriter;
import com.atteo.jello.space.SpaceManagerPolicy;
import com.atteo.jello.store.Page;
import com.atteo.jello.store.PagedFile;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

@Singleton
public class SimpleTransactionManager implements TransactionManager {
  private final StorableWriter storableWriter;
  private final SpaceManagerPolicy spaceManagerPolicy;
  private final KlassManager klassManager;
  private final PagedFile pagedFile;
  private final int freeSpaceInfoSize, blockSize;
  private final Pool<Page> pagePool;
  private final byte[] data;
  Page page;

  @Inject
  public SimpleTransactionManager(final StorableWriter storableWriter,
      final SpaceManagerPolicy spaceManagerPolicy,
      final KlassManager klassManager, final PagedFile pagedFile,
      @Named("freeSpaceInfoSize") final int freeSpaceInfoSize,
      @Named("blockSize") final int blockSize, final Pool<Page> pagePool,
      @Named("maxRecordSize") final int maxRecordSize) {
    this.storableWriter = storableWriter;
    this.spaceManagerPolicy = spaceManagerPolicy;
    this.klassManager = klassManager;
    this.pagedFile = pagedFile;
    this.freeSpaceInfoSize = freeSpaceInfoSize;
    this.blockSize = blockSize;
    this.pagePool = pagePool;

    data = new byte[maxRecordSize];

    page = pagePool.acquire();
  }

  public void performDeleteTransaction(final Storable storable) {
    if (!klassManager.isKlassManaged(storable.getClassName()))
      return;
    
    final Record record = storable.getRecord();

    final Index index = klassManager.getIndexFor(storable.getClassName());
    if (!index.find(record))
      return;

    spaceManagerPolicy.releaseRecord(record);
    index.remove(record.getId());
    ((BTree)index).debug();
    spaceManagerPolicy.commit();
    index.commit();
  }

  public boolean performFindTransaction(final Storable storable) {
    if (!klassManager.isKlassManaged(storable.getClassName()))
      return false;

    final Record record = storable.getRecord();

    final Index index = klassManager.getIndexFor(storable.getClassName());

    if (!index.find(record))
      return false;

    readRecord(record, data);

    final SchemaManager schemaManager = klassManager
        .getSchemaManagerFor(storable.getClassName());
    final Schema schema = schemaManager
        .getSchema(record.getSchemaVersion());

    storableWriter.readStorable(data, storable, schema);

    return true;
  }

  public void performInsertTransaction(final Storable storable) {
    final Schema schema = storable.getSchema();
    final int len = storableWriter.writeStorable(data, storable, schema);

    final Record record = storable.getRecord();

    spaceManagerPolicy.acquireRecord(record, len);

    if (!klassManager.isKlassManaged(storable.getClassName())) {
      klassManager.addKlass(storable.getClassName());
      klassManager.commit();
    }

    final SchemaManager schemaManager = klassManager
        .getSchemaManagerFor(storable.getClassName());
    record.setSchemaVersion(schemaManager.addSchema(schema));

    record.setId(klassManager.getIdFor(storable.getClassName()));

    final Index index = klassManager.getIndexFor(storable.getClassName());
    index.insert(record);

    writeRecord(record, data);
    
    spaceManagerPolicy.commit();
    klassManager.commit();
    index.commit();
    schemaManager.commit();
  }

  public void performUpdateTransaction(final Storable storable) {
    if (!klassManager.isKlassManaged(storable.getClassName()))
      return;
    final Schema schema = storable.getSchema();
    final int len = storableWriter.writeStorable(data, storable, schema);

    final Record record = storable.getRecord();

    spaceManagerPolicy.reacquireRecord(record, len);

    final SchemaManager schemaManager = klassManager
        .getSchemaManagerFor(storable.getClassName());
    record.setSchemaVersion(schemaManager.addSchema(schema));

    final Index index = klassManager.getIndexFor(storable.getClassName());
    index.update(record);

    writeRecord(record, data);

    spaceManagerPolicy.commit();
    index.commit();
    schemaManager.commit();
  }

  private void readRecord(final Record record, final byte[] data) {
    final int u = record.getPagesUsed();
    int position = 0;

    final Page page = pagePool.acquire();

    for (int i = 0; i < u; i++) {
      final PageUsage usage = record.getPageUsage(i);
      page.setId(usage.pageId);
      pagedFile.readPage(page);

      final byte[] pageData = page.getData();

      for (int j = 0; j < freeSpaceInfoSize; j++)
        for (int k = 0; k < Byte.SIZE; k++)
          if ((usage.usage[j] & 1 << k) != 0) {
            System.arraycopy(pageData, (j * Byte.SIZE + k)
                * blockSize, data, position, blockSize);
            position += blockSize;
          }
    }

    pagePool.release(page);

  }

  private void writeRecord(final Record record, final byte data[]) {
    final int u = record.getPagesUsed();
    int position = 0;

    for (int i = 0; i < u; i++) {
      final PageUsage usage = record.getPageUsage(i);

      page.setId(usage.pageId);
      pagedFile.readPage(page);

      final byte[] pageData = page.getData();

      for (int j = 0; j < freeSpaceInfoSize; j++)
        for (int k = 0; k < Byte.SIZE; k++)
          if ((usage.usage[j] & 1 << k) != 0) {
            System.arraycopy(data, position, pageData, (j
                * Byte.SIZE + k)
                * blockSize, blockSize);

            position += blockSize;
          }

      pagedFile.writePage(page);

    }

  }
}




Java Source Code List

android.util.FinitePool.java
android.util.Pool.java
android.util.PoolableManager.java
android.util.Poolable.java
android.util.Pools.java
android.util.SynchronizedPool.java
com.atteo.jello.DatabaseFile.java
com.atteo.jello.Expression.java
com.atteo.jello.JelloModule.java
com.atteo.jello.Jello.java
com.atteo.jello.PageUsage.java
com.atteo.jello.RecordPoolableManager.java
com.atteo.jello.Record.java
com.atteo.jello.StorableCollection.java
com.atteo.jello.StorableFactory.java
com.atteo.jello.StorableInfo.java
com.atteo.jello.Storable.java
com.atteo.jello.associations.BelongsTo.java
com.atteo.jello.associations.DatabaseField.java
com.atteo.jello.associations.HasMany.java
com.atteo.jello.index.BTree.java
com.atteo.jello.index.IndexFactory.java
com.atteo.jello.index.IndexModule.java
com.atteo.jello.index.Index.java
com.atteo.jello.index.PagePoolProxy.java
com.atteo.jello.klass.KlassManager.java
com.atteo.jello.klass.SimpleKlassManager.java
com.atteo.jello.schema.SchemaManagerFactory.java
com.atteo.jello.schema.SchemaManager.java
com.atteo.jello.schema.SchemaModule.java
com.atteo.jello.schema.Schema.java
com.atteo.jello.schema.SimpleSchemaManager.java
com.atteo.jello.schema.StorableWriter.java
com.atteo.jello.schema.VanillaStorableWriter.java
com.atteo.jello.space.AppendOnlyCacheNative.java
com.atteo.jello.space.AppendOnlyCache.java
com.atteo.jello.space.AppendOnly.java
com.atteo.jello.space.Hybrid.java
com.atteo.jello.space.NextFitHistogramNative.java
com.atteo.jello.space.NextFitHistogram.java
com.atteo.jello.space.NextFit.java
com.atteo.jello.space.SpaceManagerNative.java
com.atteo.jello.space.SpaceManagerPolicy.java
com.atteo.jello.space.SpaceManager.java
com.atteo.jello.space.SpaceModule.java
com.atteo.jello.space.VanillaHistogram.java
com.atteo.jello.store.HeaderPage.java
com.atteo.jello.store.ListPage.java
com.atteo.jello.store.PagePoolableManager.java
com.atteo.jello.store.PageSizeProvider.java
com.atteo.jello.store.Page.java
com.atteo.jello.store.PagedFileNative.java
com.atteo.jello.store.PagedFileRAF.java
com.atteo.jello.store.PagedFile.java
com.atteo.jello.store.StoreModule.java
com.atteo.jello.transaction.LockManager.java
com.atteo.jello.transaction.SimpleLockManager.java
com.atteo.jello.transaction.SimpleTransactionManager.java
com.atteo.jello.transaction.TransactionManager.java
com.atteo.jello.transaction.TransactionModule.java