Android Open Source - Jello Simple Klass 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.klass;
/* w  w w .  j a  v  a  2  s .  c o m*/
import java.util.ArrayList;

import android.util.Log;

import com.atteo.jello.index.Index;
import com.atteo.jello.index.IndexFactory;
import com.atteo.jello.schema.SchemaManager;
import com.atteo.jello.schema.SchemaManagerFactory;
import com.atteo.jello.space.SpaceManagerPolicy;
import com.atteo.jello.store.ListPage;
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 SimpleKlassManager implements KlassManager {
  private final ArrayList<Integer> pageIds;
  private final ArrayList<KlassInfo> klasses;
  private final ListPage listPage;
  private final SpaceManagerPolicy spaceManagerPolicy;
  private final IndexFactory indexFactory;
  private final SchemaManagerFactory schemaManagerFactory;
  private final PagedFile pagedFile;

  @Inject
  public SimpleKlassManager(final IndexFactory indexFactory,
      final SchemaManagerFactory schemaManagerFactory,
      @Named("klassManagerPageId") final int klassManagerPageId,
      final ListPage listPage,
      final SpaceManagerPolicy spaceManagerPolicy,
      final PagedFile pagedFile) {
    pageIds = new ArrayList<Integer>();
    pageIds.add(klassManagerPageId);
    this.listPage = listPage;

    this.spaceManagerPolicy = spaceManagerPolicy;

    this.indexFactory = indexFactory;
    this.schemaManagerFactory = schemaManagerFactory;
    this.pagedFile = pagedFile;

    klasses = new ArrayList<KlassInfo>();

  }

  public void addKlass(final String klassName) {
    final KlassInfo klassInfo = new KlassInfo();
    klassInfo.index = null;
    klassInfo.schemaManager = null;
    klassInfo.schemaManagerPageId = spaceManagerPolicy.acquirePage();
    klassInfo.indexPageId = spaceManagerPolicy.acquirePage();
    klassInfo.name = klassName;
    klassInfo.nextId = 0;
    klasses.add(klassInfo);
  }

  public void commit() {
    int klassesOnPage = 0;
    int currentPage = 0;
    final int l = klasses.size();
    int free = listPage.getCapacity() - 4;
    listPage.position(4);
    for (int i = 0; i < l; i++) {
      final KlassInfo info = klasses.get(i);
      final int nameLength = info.name.length();
      if (free < nameLength + 16) {
        if (currentPage + 1 >= pageIds.size())
          pageIds.add(spaceManagerPolicy.acquirePage());
        listPage.setNext(pageIds.get(currentPage + 1));
        listPage.setId(pageIds.get(currentPage));
        listPage.reset();
        listPage.putInt(klassesOnPage);
        pagedFile.writePage(listPage);
        currentPage++;
        listPage.position(4);
        free = listPage.getCapacity();
        klassesOnPage = 0;
      }

      free -= nameLength + 16;
      klassesOnPage++;
      listPage.putInt(nameLength);
      listPage.putString(info.name);
      listPage.putInt(info.schemaManagerPageId);
      listPage.putInt(info.indexPageId);
      listPage.putInt(info.nextId);
    }

    listPage.setNext(ListPage.NO_MORE_PAGES);
    listPage.setId(pageIds.get(currentPage));
    listPage.position(0);
    listPage.putInt(klassesOnPage);
    pagedFile.writePage(listPage);

    for (int i = currentPage + 1; i < pageIds.size(); i++) {
      spaceManagerPolicy.releasePage(pageIds.get(i));
      pageIds.remove(i);
    }
  }

  public void create() {
    listPage.setNext(ListPage.NO_MORE_PAGES);
    listPage.setId(pageIds.get(0));
    listPage.reset();
    listPage.putInt(0);
    pagedFile.writePage(listPage);
  }

  public int getIdFor(final String klassName) {
    final int l = klasses.size();
    for (int i = 0; i < l; i++) {
      final KlassInfo storedKlass = klasses.get(i);
      if (klassName.equals(storedKlass.name)) {
        storedKlass.nextId++;
        return storedKlass.nextId - 1;
      }
    }
    throw new IllegalArgumentException("Class is not managed");
  }

  public Index getIndexFor(final String klassName) {
    final int l = klasses.size();
    for (int i = 0; i < l; i++)
      if (klassName.equals(klasses.get(i).name)) {
        if (klasses.get(i).index == null)
          klasses.get(i).index = indexFactory
              .create(klasses.get(i).indexPageId);
        return klasses.get(i).index;
      }

    return null;
  }

  public SchemaManager getSchemaManagerFor(final String klassName) {
    final int l = klasses.size();
    for (int i = 0; i < l; i++)
      if (klassName.equals(klasses.get(i).name)) {
        if (klasses.get(i).schemaManager == null)
          klasses.get(i).schemaManager = schemaManagerFactory
              .create(klasses.get(i).schemaManagerPageId);
        return klasses.get(i).schemaManager;
      }

    return null;
  }

  public boolean isKlassManaged(final String klassName) {
    final int l = klasses.size();
    for (int i = 0; i < l; i++)
      if (klassName.equals(klasses.get(i).name))
        return true;
    return false;
  }

  public boolean load() {
    int next;
    listPage.setId(pageIds.get(0));
    pagedFile.readPage(listPage);
    next = listPage.getNext();
    readKlassesFromPage(listPage);

    while (next != ListPage.NO_MORE_PAGES) {
      pageIds.add(next);
      listPage.setId(next);
      pagedFile.readPage(listPage);
      next = listPage.getNext();
      readKlassesFromPage(listPage);

    }

    return true;
  }

  public void removeKlass(final String klassName) {
    final int l = klasses.size();
    for (int i = 0; i < l; i++)
      if (klassName.equals(klasses.get(i).name)) {
        klasses.remove(i);
        break;
      }
  }

  private void readKlassesFromPage(final ListPage page) {
    page.reset();
    final int klassesOnPage = page.getInt();
    int l;
    for (int i = 0; i < klassesOnPage; i++) {
      final KlassInfo info = new KlassInfo();
      l = page.getInt();
      info.name = page.getString(l);
      info.schemaManagerPageId = page.getInt();
      info.indexPageId = page.getInt();
      info.nextId = page.getInt();
      info.schemaManager = schemaManagerFactory
          .create(info.schemaManagerPageId);
      info.schemaManager.load();
      info.index = indexFactory.create(info.indexPageId);
      Log.i("Jello", "loading index for class " + info.name);
      info.index.load();
      klasses.add(info);
    }
  }

  class KlassInfo {
    String name;
    int schemaManagerPageId;
    int indexPageId;
    int nextId;
    SchemaManager schemaManager;
    Index index;
  }

}




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