下面列出了java.io.RandomAccessFile#writeShort ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Write a lookup page overflow to the page index; the node should
* have the following structure:
* <p>
* -- node type -- (2 bytes)
* -- next pointer -- (8 bytes)
* -- current capacity -- (4 bytes)
* <p>
* -- page indexes (in place of keys) (8 bytes)
*
* @param r an *already* open pointer which points to our B+ Tree file
* @param conf B+ Tree configuration
* @param bPerf instance of performance counter class
* @throws IOException is thrown when an I/O operation fails
*/
@Override
public void writeNode(RandomAccessFile r,
BPlusConfiguration conf,
BPlusTreePerformanceCounter bPerf)
throws IOException {
// account for the header page as well
r.seek(getPageIndex());
// write the node type
r.writeShort(getPageType());
// write the next pointer
r.writeLong(next);
// write current capacity
r.writeInt(getCurrentCapacity());
// now write the index values
for (int i = 0; i < getCurrentCapacity(); i++) {
r.writeLong(getKeyAt(i));
}
}
protected void write(RandomAccessFile raf) throws IOException {
raf.writeShort(_fhSize);
raf.writeInt(_pageSize);
raf.writeLong(_totalPageCount);
raf.writeLong(_firstFreePage);
raf.writeLong(_lastFreePage);
raf.writeByte(_pageHeaderSize);
}
public void save(RandomAccessFile writer) throws IOException {
writer.writeInt(Integer.reverseBytes(id));
long offset = writer.getFilePointer();
writer.write(new byte[4]);
writer.writeShort(Short.reverseBytes(owner));
writer.writeShort(Short.reverseBytes(group));
writer.writeShort(Short.reverseBytes(attributes));
writer.writeShort(Short.reverseBytes(condition));
writeString(writer, path);
int size = (int) (writer.getFilePointer() - offset - 4);
writer.seek(offset);
writer.writeInt(Integer.reverseBytes(size));
}
/**
*
* Leaf node write structure is as follows:
*
* -- node type -- (2 bytes)
* -- next pointer -- (8 bytes)
* -- prev pointer -- (8 bytes)
* -- key/value pairs -- (max size * (key size + satellite size))
*
* @param r pointer to *opened* B+ tree file
* @param conf configuration parameter
* @throws IOException is thrown when an I/O operation fails
*/
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
BPlusTreePerformanceCounter bPerf)
throws IOException {
// update root index in the file
if(this.isRoot()) {
r.seek(conf.getHeaderSize()-16L);
r.writeLong(getPageIndex());
}
// account for the header page as well.
r.seek(getPageIndex());
// now write the node type
r.writeShort(getPageType());
// write the next pointer
r.writeLong(nextPagePointer);
// write the prev pointer
r.writeLong(prevPagePointer);
// then write the current capacity
r.writeInt(getCurrentCapacity());
// now write the Key/Value pairs
for(int i = 0; i < getCurrentCapacity(); i++) {
r.writeLong(getKeyAt(i));
r.writeLong(getOverflowPointerAt(i));
r.write(valueList.get(i).getBytes(StandardCharsets.UTF_8));
}
// annoying correction
if(r.length() < getPageIndex()+conf.getPageSize())
{r.setLength(getPageIndex()+conf.getPageSize());}
bPerf.incrementTotalLeafNodeWrites();
}
/**
*
* Overflow node write structure is as follows:
*
* -- node type -- (2 bytes)
* -- next pointer -- (8 bytes)
* -- prev pointer -- (8 bytes)
* -- values -- (max size * satellite size)
*
* @param r pointer to *opened* B+ tree file
* @throws IOException is thrown when an I/O operation fails
*/
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
BPlusTreePerformanceCounter bPerf)
throws IOException {
// account for the header page as well.
r.seek(getPageIndex());
// now write the node type
r.writeShort(getPageType());
// write the next pointer
r.writeLong(nextPagePointer);
// write the prev pointer
r.writeLong(prevPagePointer);
// then write the current capacity
r.writeInt(getCurrentCapacity());
// now write the values
for(int i = 0; i < getCurrentCapacity(); i++)
{r.write(valueList.get(i).getBytes(StandardCharsets.UTF_8));}
// annoying correction
if(r.length() < getPageIndex()+conf.getPageSize())
{r.setLength(getPageIndex()+conf.getPageSize());}
bPerf.incrementTotalOverflowNodeWrites();
}
/**
*
* Internal node structure is as follows:
*
* -- node type -- (2 bytes)
* -- current capacity -- (4 bytes)
*
* -- Key -- (8 bytes max size)
*
* -- Pointers (8 bytes max size + 1)
*
* we go like: k1 -- p0 -- k2 -- p1 ... kn -- pn+1
*
* @param r pointer to *opened* B+ tree file
* @throws IOException is thrown when an I/O exception is captured.
*/
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
BPlusTreePerformanceCounter bPerf)
throws IOException {
// update root index in the file
if(this.isRoot()) {
r.seek(conf.getHeaderSize()-8);
r.writeLong(getPageIndex());
}
// account for the header page as well.
r.seek(getPageIndex());
// write the node type
r.writeShort(getPageType());
// write current capacity
r.writeInt(getCurrentCapacity());
// now write Key/Pointer pairs
for(int i = 0; i < getCurrentCapacity(); i++) {
r.writeLong(getKeyAt(i)); // Key
r.writeLong(getPointerAt(i)); // Pointer
}
// final pointer.
r.writeLong(getPointerAt(getCurrentCapacity()));
// annoying correction
if(r.length() < getPageIndex()+conf.getPageSize())
{r.setLength(getPageIndex()+conf.getPageSize());}
bPerf.incrementTotalInternalNodeWrites();
}
public void write(final RandomAccessFile file) throws IOException {
writeHeader(file, getSize());
file.writeInt(dataStart);
file.writeInt(dataLength);
file.writeShort(indexPoints);
file.writeByte(16);
for (int i = 0; i < indexPoints; i++) {
file.writeShort((int) fraction[i]);
}
}
/**
* java.io.RandomAccessFile#readShort()
*/
public void test_readShort() throws IOException {
// Test for method short java.io.RandomAccessFile.readShort()
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeShort(Short.MIN_VALUE);
raf.seek(0);
assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
.readShort());
raf.close();
}
/**
* java.io.RandomAccessFile#readUnsignedShort()
*/
public void test_readUnsignedShort() throws IOException {
// Test for method int java.io.RandomAccessFile.readUnsignedShort()
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeShort(-1);
raf.seek(0);
assertEquals("Incorrect byte read/written", 65535, raf
.readUnsignedShort());
raf.close();
}
/**
* java.io.RandomAccessFile#writeShort(int)
*/
public void test_writeShortI() throws IOException {
// Test for method void java.io.RandomAccessFile.writeShort(int)
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeShort(Short.MIN_VALUE);
raf.seek(0);
assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
.readShort());
raf.close();
}