Skip to content

Commit

Permalink
Fix variable names in line with Java norms.
Browse files Browse the repository at this point in the history
Replace Vector with typed List
  • Loading branch information
Chris Senior committed Sep 13, 2017
1 parent 7025ca0 commit e63f170
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 99 deletions.
101 changes: 50 additions & 51 deletions src/main/java/com/strangegizmo/cdb/Cdb.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
/* Java imports. */

import java.io.*;
import java.util.*;

/**
* CdbRunner implements a Java interface to D. J. Bernstein's CdbRunner
Expand All @@ -49,42 +48,42 @@ public class Cdb {
/**
* The RandomAccessFile for the CdbRunner file.
*/
private RandomAccessFile file_ = null;
private RandomAccessFile file = null;

/**
* The slot pointers, cached here for efficiency as we do not have
* mmap() to do it for us. These entries are paired as (pos, len)
* tuples.
*/
private int[] slotTable_ = null;
private int[] slotTable = null;


/**
* The number of hash slots searched under this key.
*/
private int loop_ = 0;
private int loop = 0;

/**
* The hash value for the current key.
*/
private int khash_ = 0;
private int khash = 0;


/**
* The number of hash slots in the hash table for the current key.
*/
private int hslots_ = 0;
private int hslots = 0;

/**
* The position of the hash table for the current key
*/
private int hpos_ = 0;
private int hpos = 0;


/**
* The position of the current key in the slot.
*/
private int kpos_ = 0;
private int kpos = 0;

/**
* Creates an instance of the CdbRunner class and loads the given CdbRunner
Expand All @@ -108,17 +107,17 @@ public Cdb(String filepath) throws IOException {
*/
public Cdb(File cdbFile) throws IOException {
/* Open the CdbRunner file. */
file_ = new RandomAccessFile(cdbFile, "r");
file = new RandomAccessFile(cdbFile, "r");

/* Read and parse the slot table. We do not throw an exception
* if this fails; the file might empty, which is not an error. */
try {
/* Read the table. */
byte[] table = new byte[2048];
file_.readFully(table);
file.readFully(table);

/* Create and parse the slot table. */
slotTable_ = new int[256 * 2];
slotTable = new int[256 * 2];

int offset = 0;
for (int i = 0; i < 256; i++) {
Expand All @@ -132,11 +131,11 @@ public Cdb(File cdbFile) throws IOException {
| ((table[offset++] & 0xff) << 16)
| ((table[offset++] & 0xff) << 24);

slotTable_[i << 1] = pos;
slotTable_[(i << 1) + 1] = len;
slotTable[i << 1] = pos;
slotTable[(i << 1) + 1] = len;
}
} catch (IOException ignored) {
slotTable_ = null;
slotTable = null;
}
}

Expand All @@ -147,9 +146,9 @@ public Cdb(File cdbFile) throws IOException {
public final void close() throws IOException {
/* Close the CdbRunner file. */
try {
file_.close();
file.close();
} finally {
file_ = null;
file = null;
}
}

Expand Down Expand Up @@ -188,7 +187,7 @@ static final int hash(byte[] key) {
* @param key The key to search for.
*/
public synchronized final void findstart(byte[] key) {
loop_ = 0;
loop = 0;
}

/**
Expand All @@ -212,83 +211,83 @@ public final synchronized byte[] find(byte[] key) {
*/
public final synchronized byte[] findnext(byte[] key) {
/* There are no keys if we could not read the slot table. */
if (slotTable_ == null)
if (slotTable == null)
return null;

/* Locate the hash entry if we have not yet done so. */
if (loop_ == 0) {
if (loop == 0) {
/* Get the hash value for the key. */
int u = hash(key);

/* Unpack the information for this record. */
int slot = u & 255;
hslots_ = slotTable_[(slot << 1) + 1];
if (hslots_ == 0)
hslots = slotTable[(slot << 1) + 1];
if (hslots == 0)
return null;
hpos_ = slotTable_[slot << 1];
hpos = slotTable[slot << 1];

/* Store the hash value. */
khash_ = u;
khash = u;

/* Locate the slot containing this key. */
u >>>= 8;
u %= hslots_;
u %= hslots;
u <<= 3;
kpos_ = hpos_ + u;
kpos = hpos + u;
}

/* Search all of the hash slots for this key. */
try {
while (loop_ < hslots_) {
while (loop < hslots) {
/* Read the entry for this key from the hash slot. */
file_.seek(kpos_);
file.seek(kpos);

int h = file_.readUnsignedByte()
| (file_.readUnsignedByte() << 8)
| (file_.readUnsignedByte() << 16)
| (file_.readUnsignedByte() << 24);
int h = file.readUnsignedByte()
| (file.readUnsignedByte() << 8)
| (file.readUnsignedByte() << 16)
| (file.readUnsignedByte() << 24);

int pos = file_.readUnsignedByte()
| (file_.readUnsignedByte() << 8)
| (file_.readUnsignedByte() << 16)
| (file_.readUnsignedByte() << 24);
int pos = file.readUnsignedByte()
| (file.readUnsignedByte() << 8)
| (file.readUnsignedByte() << 16)
| (file.readUnsignedByte() << 24);
if (pos == 0)
return null;

/* Advance the loop count and key position. Wrap the
* key position around to the beginning of the hash slot
* if we are at the end of the table. */
loop_ += 1;
loop += 1;

kpos_ += 8;
if (kpos_ == (hpos_ + (hslots_ << 3)))
kpos_ = hpos_;
kpos += 8;
if (kpos == (hpos + (hslots << 3)))
kpos = hpos;

/* Ignore this entry if the hash values do not match. */
if (h != khash_)
if (h != khash)
continue;

/* Get the length of the key and data in this hash slot
* entry. */
file_.seek(pos);
file.seek(pos);

int klen = file_.readUnsignedByte()
| (file_.readUnsignedByte() << 8)
| (file_.readUnsignedByte() << 16)
| (file_.readUnsignedByte() << 24);
int klen = file.readUnsignedByte()
| (file.readUnsignedByte() << 8)
| (file.readUnsignedByte() << 16)
| (file.readUnsignedByte() << 24);
if (klen != key.length)
continue;

int dlen = file_.readUnsignedByte()
| (file_.readUnsignedByte() << 8)
| (file_.readUnsignedByte() << 16)
| (file_.readUnsignedByte() << 24);
int dlen = file.readUnsignedByte()
| (file.readUnsignedByte() << 8)
| (file.readUnsignedByte() << 16)
| (file.readUnsignedByte() << 24);

/* Read the key stored in this entry and compare it to
* the key we were given. */
boolean match = true;
byte[] k = new byte[klen];
file_.readFully(k);
file.readFully(k);
for (int i = 0; i < k.length; i++) {
if (k[i] != key[i]) {
match = false;
Expand All @@ -302,7 +301,7 @@ public final synchronized byte[] findnext(byte[] key) {

/* The keys match, return the data. */
byte[] d = new byte[dlen];
file_.readFully(d);
file.readFully(d);
return d;
}
} catch (IOException ignored) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/strangegizmo/cdb/CdbElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public final class CdbElement {
/**
* The key value for this element.
*/
private final byte[] key_;
private final byte[] key;

/**
* The data value for this element.
*/
private final byte[] data_;
private final byte[] data;


/**
Expand All @@ -58,8 +58,8 @@ public final class CdbElement {
* @param data The data value for this element.
*/
public CdbElement(byte[] key, byte[] data) {
this.key_ = key;
this.data_ = data;
this.key = key;
this.data = data;
}


Expand All @@ -69,7 +69,7 @@ public CdbElement(byte[] key, byte[] data) {
* @return This element's key.
*/
public byte[] getKey() {
return key_;
return key;
}

/**
Expand All @@ -78,6 +78,6 @@ public byte[] getKey() {
* @return This element's data.
*/
public byte[] getData() {
return data_;
return data;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/strangegizmo/cdb/CdbElementEnumeration.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
public class CdbElementEnumeration implements Enumeration<CdbElement> {
private final InputStream in;
private final int eod;
/* Current data pointer. */
int pos;
/** Current data pointer. */
private int pos;

public CdbElementEnumeration(InputStream in, int eod) {
this.in = in;
Expand Down
Loading

0 comments on commit e63f170

Please sign in to comment.