Android Open Source - Jello Simple Schema 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.schema;
/*from  w  w  w.  j  a v a  2s . c om*/
import java.util.ArrayList;

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.assistedinject.Assisted;

public class SimpleSchemaManager implements SchemaManager {
  private final ArrayList<Schema> schemas;
  private final ArrayList<Integer> pageIds;
  private final PagedFile pagedFile;
  private final ListPage listPage;
  private final SpaceManagerPolicy spaceManagerPolicy;

  @Inject
  public SimpleSchemaManager(final SpaceManagerPolicy spaceManagerPolicy,
      final ListPage listPage, final PagedFile pagedFile,
      @Assisted final int klassSchemaPageId) {
    this.listPage = listPage;
    this.pagedFile = pagedFile;
    this.spaceManagerPolicy = spaceManagerPolicy;

    pageIds = new ArrayList<Integer>();
    pageIds.add(klassSchemaPageId);

    schemas = new ArrayList<Schema>();
  }

  public int addSchema(final Schema schema) {
    final int l = schemas.size();
    for (int i = 0; i < l; i++)
      if (schema.equals(schemas.get(i)))
        return schemas.get(i).version;

    schema.version = schemas.size();
    schemas.add(schema);
    return schemas.size() - 1;
  }

  public void commit() {
    int schemasOnPage = 0;
    int currentPage = 0;
    final int l = schemas.size();

    int free = listPage.getCapacity() - 4;
    listPage.position(4);

    for (int i = 0; i < l; i++) {
      final Schema schema = schemas.get(i);
      final int fieldsLength = schema.fields.length;
      int required = 4;
      final String names[] = schema.names;
      final int fields[] = schema.fields;

      for (int j = 0; j < fieldsLength; j++)
        required += names[j].length() + 8;

      if (free < required) {
        if (currentPage + 1 >= pageIds.size())
          pageIds.add(spaceManagerPolicy.acquirePage());
        listPage.setNext(pageIds.get(currentPage + 1));
        listPage.setId(pageIds.get(currentPage));
        listPage.reset();
        listPage.putInt(schemasOnPage);
        pagedFile.writePage(listPage);
        currentPage++;
        free = listPage.getCapacity();
        schemasOnPage = 0;
        listPage.position(4);

      }

      free -= required;
      schemasOnPage++;
      listPage.putInt(fieldsLength);
      for (int j = 0; j < fieldsLength; j++) {
        listPage.putInt(names[j].length());
        listPage.putString(names[j]);
        listPage.putInt(fields[j]);
      }

    }

    listPage.setNext(ListPage.NO_MORE_PAGES);
    listPage.setId(pageIds.get(currentPage));
    listPage.reset();
    listPage.putInt(schemasOnPage);
    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 Schema getSchema(final int version) {
    if (version < schemas.size())
      return schemas.get(version);
    else
      return null;
  }

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

    listPage.reset();
    readSchemasFromPage(listPage);

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

    return false;

  }

  public void removeSchema(final int version) {
    final int l = schemas.size();
    for (int i = 0; i < l; i++)
      if (version == schemas.get(i).version)
        schemas.remove(i);
  }

  private void readSchemasFromPage(final ListPage page) {
    final int schemasOnPage = page.getInt();
    int l;
    int nameLen;
    for (int i = 0; i < schemasOnPage; i++) {
      final Schema schema = new Schema();
      l = page.getInt();
      schema.fields = new int[l];
      schema.names = new String[l];

      for (int j = 0; j < l; j++) {
        nameLen = page.getInt();
        schema.names[j] = page.getString(nameLen);
        schema.fields[j] = page.getInt();
      }
      schemas.add(schema);
    }
  }

}




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