/
kelondroRowBufferedSet.java
199 lines (178 loc) · 6.27 KB
/
kelondroRowBufferedSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// kelondroRowBufferedSet.java
// (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
// first published 21.06.2006 on http://www.anomic.de
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.kelondro;
import java.util.Map;
import java.util.Iterator;
import java.util.TreeMap;
public class kelondroRowBufferedSet extends kelondroRowSet {
private static final long memBlockLimit = 2000000; // do not fill cache further if the amount of available memory is less that this
private static final int bufferFlushLimit = 10000;
private static final int bufferFlushMinimum = 1000;
private final boolean useRowCollection = true;
private kelondroProfile profile;
private TreeMap buffer;
public kelondroRowBufferedSet(kelondroRow rowdef) {
super(rowdef);
buffer = new TreeMap(kelondroNaturalOrder.naturalOrder);
profile = new kelondroProfile();
}
public kelondroRowBufferedSet(kelondroRow rowdef, int objectCount) {
super(rowdef, objectCount);
buffer = new TreeMap(kelondroNaturalOrder.naturalOrder);
profile = new kelondroProfile();
}
private final void flush() {
// call only in synchronized environment
Iterator i = buffer.entrySet().iterator();
Map.Entry entry;
while (i.hasNext()) {
entry = (Map.Entry) i.next();
super.add((kelondroRow.Entry) entry.getValue());
}
buffer.clear();
}
public final void trim() {
synchronized (buffer) {
flush();
super.trim();
}
}
public void removeOne() {
synchronized (buffer) {
if (buffer.size() == 0) {
super.removeOne();
} else {
//buffer.remove(buffer.keySet().iterator().next());
buffer.remove(buffer.lastKey());
}
}
}
public void clear() {
synchronized (buffer) {
super.clear();
buffer.clear();
}
}
public int size() {
synchronized (buffer) {
return buffer.size() + super.size();
}
}
public Iterator rows() {
synchronized (buffer) {
flush();
}
return super.rows();
}
public void uniq() {
synchronized (buffer) {
flush();
super.uniq();
}
}
public String toString() {
synchronized (buffer) {
flush();
return super.toString();
}
}
public kelondroRow.Entry get(byte[] key) {
long handle = profile.startRead();
kelondroRow.Entry entry = null;
synchronized (buffer) {
entry = (kelondroRow.Entry) buffer.get(key);
if ((entry == null) && (useRowCollection)) entry = super.get(key);
}
profile.stopRead(handle);
return entry;
}
public kelondroRow.Entry put(kelondroRow.Entry newentry) {
long handle = profile.startWrite();
byte[] key = newentry.getColBytes(super.sortColumn);
kelondroRow.Entry oldentry = null;
synchronized (buffer) {
if (useRowCollection) {
oldentry = (kelondroRow.Entry) buffer.get(key);
if (oldentry == null) {
// try the collection
oldentry = super.get(key);
if (oldentry == null) {
// this was not anywhere
buffer.put(key, newentry);
if (((buffer.size() > bufferFlushMinimum) && (kelondroRecords.availableMemory() > memBlockLimit)) ||
(buffer.size() > bufferFlushLimit)) flush();
} else {
// replace old entry
super.put(newentry);
}
} else {
// the entry is already in buffer
// simply replace old entry
buffer.put(key, newentry);
}
} else {
oldentry = (kelondroRow.Entry) buffer.put(key, newentry);
}
}
profile.stopWrite(handle);
return oldentry;
}
public kelondroRow.Entry removeShift(byte[] key) {
long handle = profile.startDelete();
kelondroRow.Entry oldentry = null;
synchronized (buffer) {
oldentry = (kelondroRow.Entry) buffer.remove(key);
if ((oldentry == null) && (useRowCollection)) {
// try the collection
oldentry = super.removeShift(key);
}
}
profile.stopDelete(handle);
return oldentry;
}
public kelondroRow.Entry removeMarked(byte[] key) {
long handle = profile.startDelete();
kelondroRow.Entry oldentry = null;
synchronized (buffer) {
oldentry = (kelondroRow.Entry) buffer.remove(key);
if ((oldentry == null) && (useRowCollection)) {
// try the collection
return super.removeMarked(key);
}
}
profile.stopDelete(handle);
return oldentry;
}
public void removeMarkedAll(kelondroRowCollection c) {
long handle = profile.startDelete();
synchronized (buffer) {
flush();
super.removeMarkedAll(c);
}
profile.stopDelete(handle);
}
public kelondroProfile profile() {
return profile;
}
}