Class SchemaPage
- java.lang.Object
-
- org.apache.iotdb.db.metadata.mtree.store.disk.schemafile.SchemaPage
-
- All Implemented Interfaces:
ISchemaPage
public class SchemaPage extends java.lang.Object implements ISchemaPage
This class is aimed to manage space inside one page.A segment inside a page has 3 representation: index, offset and instance.
- Index is meant to decouple file-wide indexing with in-page compaction
- Offset is meant for in-page indexing
- Segment instance is meant for records manipulations
-
-
Constructor Summary
Constructors Constructor Description SchemaPage(java.nio.ByteBuffer buffer, int index, boolean override)This method will init page header for a blank page buffer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description shortallocNewSegment(short size)Allocate space for a new segment inside this pagevoiddeleteSegment(short segId)protected voidextendsSegmentTo(java.nio.ByteBuffer dstBuffer, short segId)java.util.Queue<IMNode>getChildren(short segId)longgetNextSegAddress(short segId)voidgetPageBuffer(java.nio.ByteBuffer dst)intgetPageIndex()longgetPrevSegAddress(short segId)shortgetSegmentSize(short segId)SegmentgetSegmentTest(short idx)shortgetSpareSize()Calculated with accurate total segment size by examine segment buffers.
This accuracy will save much space for schema file at the cost of more frequent rearrangement.booleanhasRecordKeyInSegment(java.lang.String key, short segId)Check if record exists with name or alias.static ISchemaPageinitPage(java.nio.ByteBuffer buffer, int index)java.lang.Stringinspect()Invoke all segments, translate into string, concatenate and return.booleanisCapableForSize(short size)booleanisSegmentCapableFor(short segId, short size)static ISchemaPageloadPage(java.nio.ByteBuffer buffer, int index)IMNoderead(short segIdx, java.lang.String key)voidremoveRecord(short segId, java.lang.String key)voidsetNextSegAddress(short segId, long address)voidsetPrevSegAddress(short segId, long address)voidsyncPageBuffer()While segments are always synchronized with buffer pageBuffer, header and tail are not.longtransplantSegment(ISchemaPage srcPage, short segId, short newSegSize)Transplant designated segment from srcPage, to spare space of the pagevoidupdate(short segIdx, java.lang.String key, java.nio.ByteBuffer buffer)The record is definitely inside specified segment.protected voidupdateRecordSegAddr(short segId, java.lang.String key, long newSegAddr)longwrite(short segIdx, java.lang.String key, java.nio.ByteBuffer buffer)Insert a content directly into specified segment, without considering preallocate and reallocate segment.
-
-
-
Constructor Detail
-
SchemaPage
public SchemaPage(java.nio.ByteBuffer buffer, int index, boolean override)This method will init page header for a blank page buffer.Page Header Structure:
- 1 int (4 bytes): page index, a non-negative number
- 1 short (2 bytes): pageSpareOffset, spare offset
- 1 short (2 bytes): segNum, amount of the segment
- 1 short (2 bytes): last deleted segment offset
- 1 boolean (1 bytes): delete flag
- fixed length: Page Header
- var length: Segment
... spare space ...- var length: Segment Offset List, a sorted list of Short, length at 2*segNum
-
-
Method Detail
-
initPage
public static ISchemaPage initPage(java.nio.ByteBuffer buffer, int index)
-
loadPage
public static ISchemaPage loadPage(java.nio.ByteBuffer buffer, int index)
-
write
public long write(short segIdx, java.lang.String key, java.nio.ByteBuffer buffer) throws org.apache.iotdb.commons.exception.MetadataExceptionDescription copied from interface:ISchemaPageInsert a content directly into specified segment, without considering preallocate and reallocate segment.
Find the right segment instance which MUST exists, cache the segment and insert the record.
If not enough, reallocate inside page first, or throw exception for new page then.Notice that, since
SchemaFile.reEstimateSegSize(int)may increase segment with very small extent, which originates from design ofSchemaFile.estimateSegmentSize(IMNode), a twice relocate will suffice any nodes smaller than 1024 KiB.
This reason works forISchemaPage.update(short, String, ByteBuffer)as well.- Specified by:
writein interfaceISchemaPage- Returns:
- return 0 if write succeed, a positive for next segment address
- Throws:
SchemaPageOverflowException- no next segment, no spare space inside pageorg.apache.iotdb.commons.exception.MetadataException
-
read
public IMNode read(short segIdx, java.lang.String key) throws SegmentNotFoundException
- Specified by:
readin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
hasRecordKeyInSegment
public boolean hasRecordKeyInSegment(java.lang.String key, short segId) throws SegmentNotFoundExceptionDescription copied from interface:ISchemaPageCheck if record exists with name or alias.- Specified by:
hasRecordKeyInSegmentin interfaceISchemaPage- Parameters:
key- name or alias of target childsegId- target segment index- Throws:
SegmentNotFoundException
-
getChildren
public java.util.Queue<IMNode> getChildren(short segId) throws SegmentNotFoundException
- Specified by:
getChildrenin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
removeRecord
public void removeRecord(short segId, java.lang.String key) throws SegmentNotFoundException- Specified by:
removeRecordin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
getPageIndex
public int getPageIndex()
- Specified by:
getPageIndexin interfaceISchemaPage
-
update
public void update(short segIdx, java.lang.String key, java.nio.ByteBuffer buffer) throws org.apache.iotdb.commons.exception.MetadataExceptionDescription copied from interface:ISchemaPageThe record is definitely inside specified segment. This method compare old and new buffer to decide whether update in place.
If segment not enough, it will reallocate in this page first, and update segment offset list.
If no more space for reallocation, throwSchemaPageOverflowExceptionand return a negative for new page.See ISchemaPage.write(short, String, ByteBuffer) for the detail reason of a twice try.
- Specified by:
updatein interfaceISchemaPage- Throws:
org.apache.iotdb.commons.exception.MetadataException
-
getSpareSize
public short getSpareSize()
Calculated with accurate total segment size by examine segment buffers.
This accuracy will save much space for schema file at the cost of more frequent rearrangement.TODO: improve with a substitute variable rather than calculating on every call
- Specified by:
getSpareSizein interfaceISchemaPage
-
isCapableForSize
public boolean isCapableForSize(short size)
- Specified by:
isCapableForSizein interfaceISchemaPage
-
isSegmentCapableFor
public boolean isSegmentCapableFor(short segId, short size) throws SegmentNotFoundException- Specified by:
isSegmentCapableForin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
getPageBuffer
public void getPageBuffer(java.nio.ByteBuffer dst)
- Specified by:
getPageBufferin interfaceISchemaPage
-
syncPageBuffer
public void syncPageBuffer()
Description copied from interface:ISchemaPageWhile segments are always synchronized with buffer pageBuffer, header and tail are not. This method will synchronize them with in mem attributes.- Specified by:
syncPageBufferin interfaceISchemaPage
-
allocNewSegment
public short allocNewSegment(short size) throws java.io.IOException, SchemaPageOverflowExceptionDescription copied from interface:ISchemaPageAllocate space for a new segment inside this page- Specified by:
allocNewSegmentin interfaceISchemaPage- Parameters:
size- expected segment size- Returns:
- segment index in this page, negative for not enough space
- Throws:
java.io.IOExceptionSchemaPageOverflowException
-
getSegmentSize
public short getSegmentSize(short segId) throws SegmentNotFoundException- Specified by:
getSegmentSizein interfaceISchemaPage- Throws:
SegmentNotFoundException
-
deleteSegment
public void deleteSegment(short segId) throws SegmentNotFoundException- Specified by:
deleteSegmentin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
transplantSegment
public long transplantSegment(ISchemaPage srcPage, short segId, short newSegSize) throws org.apache.iotdb.commons.exception.MetadataException
Description copied from interface:ISchemaPageTransplant designated segment from srcPage, to spare space of the page- Specified by:
transplantSegmentin interfaceISchemaPage- Parameters:
srcPage- source page conveys source segmentsegId- id of the target segmentnewSegSize- size of new segment in this page- Throws:
org.apache.iotdb.commons.exception.MetadataException- if spare not enough, segment not found or inconsistency
-
inspect
public java.lang.String inspect() throws SegmentNotFoundExceptionInvoke all segments, translate into string, concatenate and return.- Specified by:
inspectin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
setNextSegAddress
public void setNextSegAddress(short segId, long address) throws SegmentNotFoundException- Specified by:
setNextSegAddressin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
setPrevSegAddress
public void setPrevSegAddress(short segId, long address) throws SegmentNotFoundException- Specified by:
setPrevSegAddressin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
getNextSegAddress
public long getNextSegAddress(short segId) throws SegmentNotFoundException- Specified by:
getNextSegAddressin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
getPrevSegAddress
public long getPrevSegAddress(short segId) throws SegmentNotFoundException- Specified by:
getPrevSegAddressin interfaceISchemaPage- Throws:
SegmentNotFoundException
-
extendsSegmentTo
protected void extendsSegmentTo(java.nio.ByteBuffer dstBuffer, short segId) throws SegmentNotFoundException- Throws:
SegmentNotFoundException
-
updateRecordSegAddr
protected void updateRecordSegAddr(short segId, java.lang.String key, long newSegAddr) throws SegmentNotFoundException- Throws:
SegmentNotFoundException
-
getSegmentTest
public Segment getSegmentTest(short idx) throws SegmentNotFoundException
- Throws:
SegmentNotFoundException
-
-