001: // kelondroObjectBuffer.java
002: // ------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2006
006: //
007: // This is a part of the kelondro database, which is a part of YaCy
008: //
009: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
010: // $LastChangedRevision: 1986 $
011: // $LastChangedBy: orbiter $
012: //
013: //
014: // LICENSE
015: //
016: // This program is free software; you can redistribute it and/or modify
017: // it under the terms of the GNU General Public License as published by
018: // the Free Software Foundation; either version 2 of the License, or
019: // (at your option) any later version.
020: //
021: // This program is distributed in the hope that it will be useful,
022: // but WITHOUT ANY WARRANTY; without even the implied warranty of
023: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
024: // GNU General Public License for more details.
025: //
026: // You should have received a copy of the GNU General Public License
027: // along with this program; if not, write to the Free Software
028: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
029: //
030: //
031: // A NOTE FROM THE AUTHOR TO THE USERS:
032: //
033: // Using this software in any meaning (reading, learning, copying, compiling,
034: // running) means that you agree that the Author(s) is (are) not responsible
035: // for cost, loss of data or any harm that may be caused directly or indirectly
036: // by usage of this softare or this documentation. The usage of this software
037: // is on your own risk. The installation and usage (starting/running) of this
038: // software may allow other people or application to access your computer and
039: // any attached devices and is highly dependent on the configuration of the
040: // software which must be done by the user of the software; the author(s) is
041: // (are) also not responsible for proper configuration and usage of the
042: // software, even if provoked by documentation provided together with
043: // the software.
044: //
045: //
046: // A NOTE FROM THE AUTHOR TO DEVELOPERS:
047: //
048: // Contributions and changes to the program code should be marked as such:
049: // Please enter your own (C) notice below; they must be compatible with the GPL.
050: // Please mark also all changes in the code; if you don't mark them then they
051: // can't be identified; thus all unmarked code belong to the copyright holder
052: // as mentioned above. A good documentation of code authorities will also help
053: // to maintain the code and the project.
054: // A re-distribution must contain the intact and unchanged copyright statement.
055:
056: package de.anomic.kelondro;
057:
058: public class kelondroObjectBuffer {
059:
060: // this is a buffer for a single (only one) key/value object
061: // without an index-backend
062:
063: private int readHit, readMiss, writeUnique, writeDouble;
064: private String name;
065: private byte[] key;
066: private Object value;
067:
068: public kelondroObjectBuffer(String name) {
069: this .name = name;
070: this .readHit = 0;
071: this .readMiss = 0;
072: this .writeUnique = 0;
073: this .writeDouble = 0;
074: this .key = null;
075: this .value = null;
076: }
077:
078: public String getName() {
079: return name;
080: }
081:
082: public String[] status() {
083: return new String[] { Integer.toString(readHit),
084: Integer.toString(readMiss),
085: Integer.toString(writeUnique),
086: Integer.toString(writeDouble) };
087: }
088:
089: private static String[] combinedStatus(String[] a, String[] b) {
090: return new String[] {
091: Integer.toString(Integer.parseInt(a[0])
092: + Integer.parseInt(b[0])),
093: Integer.toString(Integer.parseInt(a[1])
094: + Integer.parseInt(b[1])),
095: Integer.toString(Integer.parseInt(a[2])
096: + Integer.parseInt(b[2])),
097: Integer.toString(Integer.parseInt(a[3])
098: + Integer.parseInt(b[3])) };
099: }
100:
101: public static String[] combinedStatus(String[][] a, int l) {
102: if ((a == null) || (a.length == 0) || (l == 0))
103: return null;
104: if ((a.length >= 1) && (l == 1))
105: return a[0];
106: if ((a.length >= 2) && (l == 2))
107: return combinedStatus(a[0], a[1]);
108: return combinedStatus(combinedStatus(a, l - 1), a[l - 1]);
109: }
110:
111: public void put(byte[] key, Object value) {
112: if ((key == null) || (value == null))
113: return;
114: synchronized (this ) {
115: if (kelondroNaturalOrder.equal(this .key, key)) {
116: this .writeDouble++;
117: } else {
118: this .writeUnique++;
119: }
120: this .key = key;
121: this .value = value;
122: }
123: }
124:
125: public void put(String key, Object value) {
126: if ((key == null) || (value == null))
127: return;
128: synchronized (this ) {
129: if (kelondroNaturalOrder.equal(this .key, key.getBytes())) {
130: this .writeDouble++;
131: } else {
132: this .writeUnique++;
133: }
134: this .key = key.getBytes();
135: this .value = value;
136: }
137: }
138:
139: public Object get(byte[] key) {
140: if (key == null)
141: return null;
142: synchronized (this ) {
143: if (kelondroNaturalOrder.equal(this .key, key)) {
144: this .readHit++;
145: return this .value;
146: } else {
147: this .readMiss++;
148: return null;
149: }
150: }
151: }
152:
153: public Object get(String key) {
154: if (key == null)
155: return null;
156: synchronized (this ) {
157: if (kelondroNaturalOrder.equal(this .key, key.getBytes())) {
158: this .readHit++;
159: return this .value;
160: } else {
161: this .readMiss++;
162: return null;
163: }
164: }
165: }
166:
167: public void remove(byte[] key) {
168: if (key == null)
169: return;
170: synchronized (this ) {
171: if (kelondroNaturalOrder.equal(this .key, key)) {
172: this .key = null;
173: this .value = null;
174: }
175: }
176: }
177:
178: public void remove(String key) {
179: if (key == null)
180: return;
181: synchronized (this) {
182: if (kelondroNaturalOrder.equal(this.key, key.getBytes())) {
183: this.key = null;
184: this.value = null;
185: }
186: }
187: }
188:
189: }
|