public synchronized ISchemaPage getNearestFitPage(short size, boolean withLock) {
    ISchemaPage targetPage;
    int elemToCheck;
    for (int i = 0; i < buckets.length && pageContainer.size() > 0; i++) {
      // buckets[i] stores pages with spare space less than bounds[i]
      elemToCheck = buckets[i].size();
      while (size < bounds[i] && elemToCheck > 0) {
        // find roughly fit page
        targetPage = pageContainer.getOrDefault(buckets[i].poll(), null);
        elemToCheck--;

        if (targetPage == null || targetPage.getAsSegmentedPage() == null) {
          // act as lazy remove on buckets
          continue;
        }

        // seek in global container thus other page could be read locked
        if (withLock
            && targetPage.getAsSegmentedPage().isCapableForSegSize(size)
            && targetPage.getLock().writeLock().tryLock()) {
          if (targetPage.getAsSegmentedPage().isCapableForSegSize(size)) {
            return targetPage.getAsSegmentedPage();
          }
          targetPage.getLock().writeLock().unlock();
        }

        // only in local dirty pages which are always write locked by itself
        if (!withLock && targetPage.getAsSegmentedPage().isCapableForSegSize(size)) {
          return targetPage;
        }

        // not large as expected, fit into suitable bucket
        if (i > 0 && targetPage.getAsSegmentedPage().isCapableForSegSize(bounds[0])) {
          sortIntoBucket(targetPage, (short) -1);
        }
      }
    }
    return null;
  }
@param withLock set if page container is a global/shared object
@return the page index will be removed from the bucket.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/pagemgr/PageIndexSortBuckets.java