001: // kelondroBytesIntMap.java
002: // (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
003: // first published 18.06.2006 on http://www.anomic.de
004: //
005: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
006: // $LastChangedRevision: 1986 $
007: // $LastChangedBy: orbiter $
008: //
009: // LICENSE
010: //
011: // This program is free software; you can redistribute it and/or modify
012: // it under the terms of the GNU General Public License as published by
013: // the Free Software Foundation; either version 2 of the License, or
014: // (at your option) any later version.
015: //
016: // This program is distributed in the hope that it will be useful,
017: // but WITHOUT ANY WARRANTY; without even the implied warranty of
018: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: // GNU General Public License for more details.
020: //
021: // You should have received a copy of the GNU General Public License
022: // along with this program; if not, write to the Free Software
023: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: package de.anomic.kelondro;
026:
027: import java.io.IOException;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030:
031: public class kelondroBytesIntMap {
032:
033: private kelondroRow rowdef;
034: private kelondroIndex index;
035:
036: public kelondroBytesIntMap(kelondroIndex ki) {
037: assert (ki.row().columns() == 2); // must be a key/index relation
038: assert (ki.row().width(1) == 4); // the value must be a b256-encoded int, 4 bytes long
039: this .index = ki;
040: this .rowdef = ki.row();
041: }
042:
043: public kelondroBytesIntMap(int keylength,
044: kelondroByteOrder objectOrder, int space) {
045: this .rowdef = new kelondroRow(
046: new kelondroColumn[] {
047: new kelondroColumn("key",
048: kelondroColumn.celltype_binary,
049: kelondroColumn.encoder_bytes,
050: keylength, "key"),
051: new kelondroColumn("int c-4 {b256}") },
052: objectOrder, 0);
053: this .index = new kelondroRAMIndex(rowdef, space);
054: }
055:
056: public kelondroRow row() {
057: return index.row();
058: }
059:
060: public synchronized int geti(byte[] key) throws IOException {
061: assert (key != null);
062: kelondroRow.Entry indexentry = index.get(key);
063: if (indexentry == null)
064: return -1;
065: return (int) indexentry.getColLong(1);
066: }
067:
068: public synchronized int puti(byte[] key, int i) throws IOException {
069: assert i >= 0 : "i = " + i;
070: assert (key != null);
071: kelondroRow.Entry newentry = index.row().newEntry();
072: newentry.setCol(0, key);
073: newentry.setCol(1, i);
074: kelondroRow.Entry oldentry = index.put(newentry);
075: if (oldentry == null)
076: return -1;
077: return (int) oldentry.getColLong(1);
078: }
079:
080: public synchronized void addi(byte[] key, int i) throws IOException {
081: assert i >= 0 : "i = " + i;
082: assert (key != null);
083: kelondroRow.Entry newentry = this .rowdef.newEntry();
084: newentry.setCol(0, key);
085: newentry.setCol(1, i);
086: index.addUnique(newentry);
087: }
088:
089: public synchronized ArrayList<Integer[]> removeDoubles()
090: throws IOException {
091: ArrayList<kelondroRowSet> indexreport = index.removeDoubles();
092: ArrayList<Integer[]> report = new ArrayList<Integer[]>();
093: Iterator<kelondroRowSet> i = indexreport.iterator();
094: kelondroRowSet rowset;
095: Integer[] is;
096: Iterator<kelondroRow.Entry> ei;
097: int c;
098: while (i.hasNext()) {
099: rowset = i.next();
100: is = new Integer[rowset.size()];
101: ei = rowset.rows();
102: c = 0;
103: while (ei.hasNext()) {
104: is[c++] = new Integer((int) ei.next().getColLong(1));
105: }
106: report.add(is);
107: }
108: return report;
109: }
110:
111: public synchronized int removei(byte[] key) throws IOException {
112: assert (key != null);
113: kelondroRow.Entry indexentry = index.remove(key, true); // keeping the order will prevent multiple re-sorts
114: if (indexentry == null)
115: return -1;
116: return (int) indexentry.getColLong(1);
117: }
118:
119: public synchronized int removeonei() throws IOException {
120: kelondroRow.Entry indexentry = index.removeOne();
121: if (indexentry == null)
122: return -1;
123: return (int) indexentry.getColLong(1);
124: }
125:
126: public synchronized int size() {
127: return index.size();
128: }
129:
130: public synchronized kelondroCloneableIterator<byte[]> keys(
131: boolean up, byte[] firstKey) throws IOException {
132: return index.keys(up, firstKey);
133: }
134:
135: public synchronized kelondroCloneableIterator<kelondroRow.Entry> rows(
136: boolean up, byte[] firstKey) throws IOException {
137: return index.rows(up, firstKey);
138: }
139:
140: public kelondroProfile profile() {
141: return index.profile();
142: }
143:
144: public synchronized void close() {
145: index.close();
146: index = null;
147: }
148:
149: }
|