📄 t_util.java
字号:
return cid; } /** Add a new container in the transaction with specified pageSize, spareSpace, minimumRecordSize, and reusableRecordId @exception T_Fail Unexpected behaviour from the API @exception StandardException Unexpected exception from the implementation */ public long t_addContainer(Transaction t, long segmentId, int pageSize, int spareSpace, int minimumRecordSize, boolean reusableRecordId) throws StandardException, T_Fail { Properties tableProperties = new Properties(); tableProperties.put(Property.PAGE_SIZE_PARAMETER, Integer.toString(pageSize)); tableProperties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER, Integer.toString(spareSpace)); tableProperties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER, Integer.toString(minimumRecordSize)); if (reusableRecordId) { tableProperties.put(RawStoreFactory.PAGE_REUSABLE_RECORD_ID, "true"); } long cid = t.addContainer( segmentId, ContainerHandle.DEFAULT_ASSIGN_ID, ContainerHandle.MODE_DEFAULT, tableProperties, 0); if (cid < 0) throw T_Fail.testFailMsg("add container"); return cid; } public long t_addContainer(T_TWC ctx, long segmentId, int pageSize, int spareSpace, int minimumRecordSize) throws StandardException, T_Fail { csFactory.setCurrentContextManager(ctx.cm); try { return t_addContainer(ctx.tran, segmentId, pageSize, spareSpace, minimumRecordSize, false); } finally { csFactory.resetCurrentContextManager(ctx.cm); } } /** Open a container. @exception T_Fail Unexpected behaviour from the API @exception StandardException Unexpected exception from the implementation */ public ContainerHandle t_openContainer(Transaction t, long segmentId, long containerId, boolean forUpdate) throws StandardException, T_Fail { ContainerKey id = new ContainerKey(segmentId, containerId); ContainerHandle c = t.openContainer(id, forUpdate ? (ContainerHandle.MODE_FORUPDATE | openContainerMode) : ContainerHandle.MODE_READONLY); if (c == null) throw T_Fail.testFailMsg("ContainerHandle failed to open: (" + segmentId + "," + containerId + ")"); return c; } public ContainerHandle t_openContainer(T_TWC ctx, long segmentId, long containerId, boolean forUpdate) throws StandardException, T_Fail { csFactory.setCurrentContextManager(ctx.cm); try { return t_openContainer(ctx.tran, segmentId, containerId, forUpdate); } finally { csFactory.resetCurrentContextManager(ctx.cm); } } /** Drop a container @exception T_Fail Unexpected behaviour from the API @exception StandardException Unexpected exception from the implementation */ public void t_dropContainer(Transaction t, long segmentId, long containerId) throws StandardException, T_Fail { t.dropContainer(new ContainerKey(segmentId, containerId)); } /** Get the last page in a container. Always returns a valid page or null if there is no page in the container. @exception T_Fail Unexpected behaviour from the API @exception StandardException Unexpected exception from the implementation */ public Page t_getLastPage(ContainerHandle c) throws T_Fail, StandardException { Page page = c.getFirstPage(); if (page != null) { Page nextPage; while((nextPage = c.getNextPage(page.getPageNumber())) != null) { page.unlatch(); page = nextPage; } } return page; } /** Get a specific page in a container. Always returns a valid page. @exception T_Fail Unexpected behaviour from the API @exception StandardException Unexpected exception from the implementation */ public Page t_getPage(ContainerHandle c, long pageNumber) throws T_Fail, StandardException { Page page = c.getPage(pageNumber); if (page == null) throw T_Fail.testFailMsg("fail to get page " + pageNumber + " from container " + c); if (page.getPageNumber() != pageNumber) throw T_Fail.testFailMsg("page expected to have page number " + pageNumber + ", has " + page.getPageNumber() + " Container " + c); return page; } /** Add a page to a container. @exception T_Fail Unexpected behaviour from the API @exception StandardException Unexpected exception from the implementation */ public Page t_addPage(ContainerHandle c) throws T_Fail, StandardException { Page page = c.addPage(); if (page == null) throw T_Fail.testFailMsg("addPage() returned null"); return page; } /** Remove a page from a container. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation */ public void t_removePage(ContainerHandle c, Page p) throws T_Fail, StandardException { long pnum = p.getPageNumber(); c.removePage(p); // we should not be able to get this page Page badp = c.getPage(pnum); if (badp != null) throw T_Fail.testFailMsg("got a deallcated page back"); } /** Call page.insert() and ensure that the return record handle is not null. This assumes the caller has called spaceForInsert. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#insert */ public static RecordHandle t_insert(Page page, T_RawStoreRow row) throws T_Fail, StandardException { RecordHandle rh = page.insert(row.getRow(), (FormatableBitSet) null, Page.INSERT_DEFAULT, 100); return rh; } /** Call page.insert() and ensure that the return record handle is not null. This assumes the caller has called spaceForInsert. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#insert */ public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row) throws T_Fail, StandardException { RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, (LogicalUndo) null, Page.INSERT_DEFAULT, 100); return rh; } /** Call page.insert() and ensure that the return record handle is not null. This assumes the caller has called spaceForInsert. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#insert */ public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row, byte insertFlag) throws T_Fail, StandardException { RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, (LogicalUndo) null, insertFlag, 100); return rh; } /** Call page.insert() and ensure that the return record handle is not null. This assumes the caller has called spaceForInsert. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#insert */ public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row, byte insertFlag, int overflowThreshold) throws T_Fail, StandardException { RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, (LogicalUndo) null, insertFlag, overflowThreshold); return rh; } /** Insert a record on the last page, if the row doesn't fit on the last page create a new page and insert there. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#insert */ public RecordHandle t_insert(ContainerHandle c, T_RawStoreRow row) throws T_Fail, StandardException { Page page = c.getPageForInsert(0); boolean addedPage = false; if (page == null) { page = t_addPage(c); addedPage = true; } else if (!page.spaceForInsert(row.getRow(), (FormatableBitSet) null, 100)) { page.unlatch(); page = t_addPage(c); addedPage = true; } RecordHandle rh = t_insert(page, row); page.unlatch(); if (rh == null) { if (addedPage) throw T_Fail.testFailMsg("insert returned null on an empty page"); page = t_addPage(c); rh = t_insert(page, row); page.unlatch(); } return rh; } /** Update a record. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#update */ public void t_update(ContainerHandle c, RecordHandle rh, T_RawStoreRow row) throws T_Fail, StandardException { Page page = t_getPage(c, rh.getPageNumber()); try { if (!page.update(rh, row.getRow(), (FormatableBitSet)null)) throw T_Fail.testFailMsg("update failed"); t_checkFetch(page, rh, row); } finally { page.unlatch(); } } /** Using sparse representation: Update a column of a record and check resulting value. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#update */ public void t_checkUpdateCol(Page page, RecordHandle rh, int colNum, int numCols, String data) throws T_Fail, StandardException { if (!page.recordExists(rh, false)) throw T_Fail.testFailMsg("Record does not exist"); T_RawStoreRow writeRow = new T_RawStoreRow(numCols); for (int i = 0; i < numCols; i++) writeRow.setColumn(i, (String) null); writeRow.setColumn(colNum, data); FormatableBitSet colList = new FormatableBitSet(numCols); colList.set(colNum); if (!page.update(rh, writeRow.getRow(), colList)) throw T_Fail.testFailMsg("update failed"); t_checkFetchCol(page, rh, colNum, numCols, data); } /** Delete a record. @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation @see Page#delete */ public void t_delete(ContainerHandle c, RecordHandle rh) throws T_Fail, StandardException { Page page = t_getPage(c, rh.getPageNumber()); try { if (!page.recordExists(rh, false)) throw T_Fail.testFailMsg("record does not exist"); if (!page.delete(rh, (LogicalUndo)null)) throw T_Fail.testFailMsg("delete failed"); if (page.recordExists(rh, false)) throw T_Fail.testFailMsg("recordExists() returns true after a delete"); } finally { page.unlatch(); } } /** Check to make sure a row (possibly with overflow) is of the correct length @exception T_Fail Record handle returned is null. @exception StandardException Unexpected exception from the implementation */ public void t_checkStringLengthFetch(Page page, int slot, int expectedLength) throws T_Fail, StandardException { T_RawStoreRow rr = new T_RawStoreRow((String) null); page.fetchFromSlot( (RecordHandle) null, slot, rr.getRow(), (FetchDescriptor) null, true); String s = ((SQLChar) (rr.getStorableColumn(0))).getString(); if ((s == null) && (expectedLength < 0)) return; if ((s != null) && (expectedLength < 0)) throw T_Fail.testFailMsg("Expected null string, fetched one of length " + s.length()); if (s == null) throw T_Fail.testFailMsg("Expected string length " + expectedLength + " got null string"); if (s.length() != expectedLength) throw T_Fail.testFailMsg("fetch string length incorrect expected " + expectedLength + " got " + s.length()); } /** Lazy people's random file generator: Generate a random file with specified name and file size @exception T_Fail Record handle returned is null. */ public void t_genRandomFile(String fileName, String mode, int size) throws T_Fail { RandomAccessFile iFile = null; try { iFile = new RandomAccessFile(fileName, mode); for (int i = 0; i < size; i++){ byte b = (byte) (i & 0xff); b = (byte) (((b >= ' ') && (b <= '~')) ? b : ' '); iFile.write(b); } iFile.close(); } catch (FileNotFoundException fnfe) { throw T_Fail.testFailMsg("cannot create new file"); } catch (IOException ioe) { throw T_Fail.testFailMsg("io error, test failed"); } } /** Return a string of stringLen characters that starts with data and is padded with nulls. */ public static String getStringFromData(String data, int stringLen) { char[] ca = new char[stringLen]; char[] sd = data.toCharArray(); System.arraycopy(sd, 0, ca, 0, sd.length); return new String(ca); } /** Make this thread wait a bit, probably for post commit to finish */ public static void t_wait(int milliSecond) { Thread.currentThread().yield(); try { Thread.currentThread().sleep(milliSecond); } catch (InterruptedException ie) { } } /** Add in encryption parameters to the startParam if "testDataEncryption" is set to a non-null string. */ public static Properties setEncryptionParam(Properties startParams) { // see if we are testing encryption String encryptionPassword = PropertyUtil.getSystemProperty("testDataEncryption"); //look for alternate encryption provider String encryptionProvider = PropertyUtil.getSystemProperty("testEncryptionProvider"); if (encryptionPassword != null) { if (startParams == null) startParams = new Properties(); startParams.put(Attribute.DATA_ENCRYPTION, "true"); startParams.put(Attribute.BOOT_PASSWORD, encryptionPassword); if (encryptionProvider != null) { startParams.put(Attribute.CRYPTO_PROVIDER, encryptionProvider); } // System.out.println("Setting encryption password to " + encryptionPassword); } return startParams; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -