📄 paged.java
字号:
* in order to create an appropriate subclass instance of a FileHeader. * * @param pageCount The number of pages to allocate for primary storage * @param pageSize The size of a Page (should be a multiple of a FS block) * @return a new FileHeader */ public abstract FileHeader createFileHeader(long pageCount, int pageSize); /** * createPageHeader must be implemented by a Paged implementation * in order to create an appropriate subclass instance of a PageHeader. * * @return a new PageHeader */ public abstract PageHeader createPageHeader(); // These are a bunch of utility methods for subclasses public static Value[] insertArrayValue(Value[] vals, Value val, int idx) { Value[] newVals = new Value[vals.length + 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } newVals[idx] = val; if (idx < vals.length) { System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx); } return newVals; } public static Value[] deleteArrayValue(Value[] vals, int idx) { Value[] newVals = new Value[vals.length - 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } if (idx < newVals.length) { System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx); } return newVals; } public static long[] insertArrayLong(long[] vals, long val, int idx) { long[] newVals = new long[vals.length + 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } newVals[idx] = val; if (idx < vals.length) { System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx); } return newVals; } public static long[] deleteArrayLong(long[] vals, int idx) { long[] newVals = new long[vals.length - 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } if (idx < newVals.length) { System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx); } return newVals; } public static int[] insertArrayInt(int[] vals, int val, int idx) { int[] newVals = new int[vals.length + 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } newVals[idx] = val; if (idx < vals.length) { System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx); } return newVals; } public static int[] deleteArrayInt(int[] vals, int idx) { int[] newVals = new int[vals.length - 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } if (idx < newVals.length) { System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx); } return newVals; } public static short[] insertArrayShort(short[] vals, short val, int idx) { short[] newVals = new short[vals.length + 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } newVals[idx] = val; if (idx < vals.length) { System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx); } return newVals; } public static short[] deleteArrayShort(short[] vals, int idx) { short[] newVals = new short[vals.length - 1]; if (idx > 0) { System.arraycopy(vals, 0, newVals, 0, idx); } if (idx < newVals.length) { System.arraycopy(vals, idx + 1, newVals, idx, newVals.length - idx); } return newVals; } /** * Paged file's header */ public abstract class FileHeader { private boolean dirty = false; private int workSize; private short headerSize; private int pageSize; private long pageCount; private long totalCount; private long firstFreePage = -1; private long lastFreePage = -1; private byte pageHeaderSize = 64; private short maxKeySize = 256; private long recordCount; public FileHeader() { this(1024); } public FileHeader(long pageCount) { this(pageCount, 4096); } public FileHeader(long pageCount, int pageSize) { this.pageSize = pageSize; this.pageCount = pageCount; totalCount = pageCount; headerSize = (short) 4096; calculateWorkSize(); } public FileHeader(boolean read) throws IOException { if (read) { read(); } } public synchronized final void read() throws IOException { RandomAccessFile raf = null; try { raf = getDescriptor(); raf.seek(0); read(raf); calculateWorkSize(); } finally { putDescriptor(raf); } } public synchronized void read(RandomAccessFile raf) throws IOException { headerSize = raf.readShort(); pageSize = raf.readInt(); pageCount = raf.readLong(); totalCount = raf.readLong(); firstFreePage = raf.readLong(); lastFreePage = raf.readLong(); pageHeaderSize = raf.readByte(); maxKeySize = raf.readShort(); recordCount = raf.readLong(); } public synchronized final void write() throws IOException { if (!dirty) { return; } RandomAccessFile raf = null; try { raf = getDescriptor(); raf.seek(0); write(raf); dirty = false; } finally { putDescriptor(raf); } } public synchronized void write(RandomAccessFile raf) throws IOException { raf.writeShort(headerSize); raf.writeInt(pageSize); raf.writeLong(pageCount); raf.writeLong(totalCount); raf.writeLong(firstFreePage); raf.writeLong(lastFreePage); raf.writeByte(pageHeaderSize); raf.writeShort(maxKeySize); raf.writeLong(recordCount); } public synchronized final void setDirty() { dirty = true; } public synchronized final boolean isDirty() { return dirty; } /** * The size of the FileHeader. Usually 1 OS Page. * This method should be called only while initializing Paged, not during normal processing. * @param headerSize the new header size */ public synchronized final void setHeaderSize(short headerSize) { this.headerSize = headerSize; dirty = true; } /** * The size of the FileHeader. Usually 1 OS Page * @return the header size */ public synchronized final short getHeaderSize() { return headerSize; } /** * The size of a page. Usually a multiple of a FS block. * This method should be called only while initializing Paged, not during normal processing. * @param pageSize the new page size */ public synchronized final void setPageSize(int pageSize) { this.pageSize = pageSize; calculateWorkSize(); dirty = true; } /** * The size of a page. Usually a multiple of a FS block * @return the page size */ public synchronized final int getPageSize() { return pageSize; } /** * The number of pages in primary storage. * This method should be called only while initializing Paged, not during normal processing. * @param pageCount the new page count */ public synchronized final void setPageCount(long pageCount) { this.pageCount = pageCount; dirty = true; } /** * The number of pages in primary storage * @return the page count */ public synchronized final long getPageCount() { return pageCount; } /** * The number of total pages in the file. * This method should be called only while initializing Paged, not during normal processing. * @param totalCount the new total count */ public synchronized final void setTotalCount(long totalCount) { this.totalCount = totalCount; dirty = true; } public synchronized final long incTotalCount() { dirty = true; return this.totalCount++; } /** * The number of total pages in the file * @return the total count */ public synchronized final long getTotalCount() { return totalCount; } /** * The first free page in unused secondary space * @param firstFreePage the new first free page */ public synchronized final void setFirstFreePage(long firstFreePage) { this.firstFreePage = firstFreePage; dirty = true; } /** * The first free page in unused secondary space * @return the first free page */ public synchronized final long getFirstFreePage() { return firstFreePage; } /** * The last free page in unused secondary space * @param lastFreePage sets the last free page */ public synchronized final void setLastFreePage(long lastFreePage) { this.lastFreePage = lastFreePage; dirty = true; } /** * The last free page in unused secondary space * @return the last free page */ public synchronized final long getLastFreePage() { return lastFreePage; } /** * Set the size of a page header. * <p/> * Normally, 64 is sufficient. * @param pageHeaderSize the new page header size */ public synchronized final void setPageHeaderSize(byte pageHeaderSize) { this.pageHeaderSize = pageHeaderSize; calculateWorkSize(); dirty = true; } /** * Get the size of a page header. * <p/> * Normally, 64 is sufficient * @return the page header size */ public synchronized final byte getPageHeaderSize() { return pageHeaderSize; } /** * Set the maximum number of bytes a key can be. * <p/> * Normally, 256 is good * @param maxKeySize the new max key size */ public synchronized final void setMaxKeySize(short maxKeySize) { this.maxKeySize = maxKeySize; dirty = true; } /** * Get the maximum number of bytes. * <p/> * Normally, 256 is good. * @return max key size */ public synchronized final short getMaxKeySize() { return maxKeySize; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -