}
}
private static void updateSegment(String segment, BitSet docs, Diff diff) throws IOException {
JRedisClient redis = new JRedisClient();
try {
for (Term add : diff.getAdds()) {
String key = segment + "_" + add.text();
if (!redis.exists(key)) {
// New key - just add it with the current bitset
//int[] docset = new int[docs.cardinality()];
ByteBuffer bytes = ByteBuffer.allocate(docs.cardinality() * 4);
IntBuffer docset = bytes.asIntBuffer();
for (int i = docs.nextSetBit(0), j = 0; i >= 0; i = docs.nextSetBit(i + 1), j++) {
docset.put(j, i);
}
redis.set(key, bytes.array());
}
else {
byte[] orig = redis.get(key);
int[] origpostings = new int[orig.length / 4];
int[] newpostings = new int[origpostings.length + docs.cardinality()];
ByteBuffer.wrap(orig).asIntBuffer().get(origpostings);
//ByteBuffer buffer = ByteBuffer.allocate(orig.length + docs.cardinality() * 4);
//IntBuffer postings = buffer.asIntBuffer();
int spos = 0, dpos = 0, ndoc = -1;
while ((ndoc = docs.nextSetBit(ndoc + 1)) >= 0) {
if (spos >= origpostings.length) {
newpostings[dpos++] = ndoc;
}
else {
int upto = Arrays.binarySearch(origpostings, ndoc);
if (upto < 0) {
upto = -(upto + 1);
System.arraycopy(origpostings, spos, newpostings, dpos, upto - spos);
dpos += upto - spos;
spos = upto;
newpostings[dpos++] = ndoc;
}
else {
// We already exist in this document, so just copy the old stuff up
System.arraycopy(origpostings, spos, newpostings, dpos, upto - spos);
dpos += upto - spos;
spos = upto;
}
}
}
ByteBuffer bb = ByteBuffer.allocate(newpostings.length * 4);
bb.asIntBuffer().put(newpostings);
redis.set(key, bb.array());
}
}
for (Term del : diff.getDeletes()) {
String key = segment + "_" + del.text();
if (!redis.exists(key)) {
continue;
}
byte[] orig = redis.get(key);
int[] origpostings = new int[orig.length / 4];
int[] newpostings = new int[origpostings.length - docs.cardinality()];
ByteBuffer.wrap(orig).asIntBuffer().get(origpostings);
int spos = 0, dpos = 0, ndoc = -1;
while ((ndoc = docs.nextSetBit(ndoc + 1)) >= 0) {
if (spos >= origpostings.length)
break;
while (origpostings[spos++] < ndoc) {
newpostings[dpos++] = origpostings[spos];
}
spos++;
}
if (spos < origpostings.length) {
System.arraycopy(origpostings, spos, newpostings, dpos, origpostings.length - spos);
}
ByteBuffer bb = ByteBuffer.allocate(newpostings.length * 4);
bb.asIntBuffer().put(newpostings);
redis.set(key, bb.array());
}
} catch (RedisException e) {
throw new IOException(e);
}
}