001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.ta.collections;
022:
023: import com.db4o.db4ounit.common.ta.*;
024:
025: /**
026: * Shared implementation for a paged collection.
027: */
028: public class PagedBackingStore extends ActivatableImpl {
029:
030: public final static int INITIAL_PAGE_COUNT = 16;
031:
032: private Page[] _pages = new Page[INITIAL_PAGE_COUNT];
033: private int _top = 0;
034:
035: public PagedBackingStore() {
036: addNewPage();
037: }
038:
039: public boolean add(Object item) {
040: // TA BEGIN
041: activate();
042: // TA END
043: return getPageForAdd().add(item);
044: }
045:
046: public int size() {
047: // TA BEGIN
048: activate();
049: // TA END
050: return _top * Page.PAGESIZE - lastPage().capacity();
051: }
052:
053: public Object get(int itemIndex) {
054: // TA BEGIN
055: activate();
056: // TA END
057: Page page = pageHolding(itemIndex);
058: return page.get(indexInPage(itemIndex));
059: }
060:
061: private Page lastPage() {
062: return _pages[_top - 1];
063: }
064:
065: private Page getPageForAdd() {
066: Page lastPage = lastPage();
067: if (lastPage.atCapacity()) {
068: lastPage = addNewPage();
069: }
070: return lastPage;
071: }
072:
073: private Page addNewPage() {
074: final Page page = new Page(_top);
075: if (_top == _pages.length) {
076: growPages();
077: }
078: _pages[_top] = page;
079: _top++;
080: return page;
081: }
082:
083: private void growPages() {
084: Page[] grown = new Page[_pages.length * 2];
085: System.arraycopy(_pages, 0, grown, 0, _pages.length);
086: _pages = grown;
087: }
088:
089: /**
090: * Will return the page that holds the index passed in.
091: * For example, if pagesize == 100 and index == 525, then this will return page 5.
092: *
093: * @param itemIndex
094: * @return
095: */
096: private Page pageHolding(int itemIndex) {
097: return _pages[pageIndex(itemIndex)];
098: }
099:
100: private int pageIndex(int itemIndex) {
101: return itemIndex / Page.PAGESIZE;
102: }
103:
104: private int indexInPage(int itemIndex) {
105: return itemIndex % Page.PAGESIZE;
106: }
107: }
|