![JAR search and dependency download from the Maven repository](/logo.png)
swim.db.QTreePageRef Maven / Gradle / Ivy
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swim.db;
import java.lang.ref.WeakReference;
import swim.codec.Output;
import swim.codec.Unicode;
import swim.concurrent.Cont;
import swim.recon.Recon;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Slot;
import swim.structure.Value;
import swim.util.Builder;
import swim.util.CombinerFunction;
import swim.util.Cursor;
public final class QTreePageRef extends PageRef {
final PageContext context;
final PageType pageType;
final int stem;
final int post;
final int zone;
final long base;
final long span;
final long x;
final long y;
final Value fold;
Object page;
int pageRefSize;
int pageSize;
int diffSize;
long treeSize;
public QTreePageRef(PageContext context, PageType pageType, int stem, int post,
int zone, long base, long span, long x, long y, Value fold,
Object page, int pageRefSize, int pageSize, int diffSize, long treeSize) {
this.context = context;
this.pageType = pageType;
this.stem = stem;
this.post = post;
this.zone = zone;
this.base = base;
this.span = span;
this.x = x;
this.y = y;
this.fold = fold;
this.page = page;
this.pageRefSize = pageRefSize;
this.pageSize = pageSize;
this.diffSize = diffSize;
this.treeSize = treeSize;
}
public QTreePageRef(PageContext context, PageType pageType, int stem, int post,
int zone, long base, long span, long x, long y, Value fold, Object page) {
this(context, pageType, stem, post, zone, base, span, x, y, fold, page, -1, -1, -1, -1L);
}
public QTreePageRef(PageContext context, PageType pageType, int stem, int post,
int zone, long base, long span, long x, long y, Value fold) {
this(context, pageType, stem, post, zone, base, span, x, y, fold, null, -1, -1, -1, -1L);
}
@Override
public PageContext pageContext() {
return this.context;
}
@Override
public PageType pageType() {
return this.pageType;
}
@Override
public int stem() {
return this.stem;
}
@Override
public int post() {
return this.post;
}
@Override
public int zone() {
return this.zone;
}
@Override
public long base() {
return this.base;
}
@Override
public long span() {
return this.span;
}
public long x() {
return this.x;
}
public int xRank() {
return Long.numberOfLeadingZeros(~this.x);
}
public long xBase() {
return this.x << this.xRank();
}
public long xMask() {
return ~((1L << this.xRank()) - 1L);
}
public long xSplit() {
return this.x << 1 & 1L;
}
public long y() {
return this.y;
}
public int yRank() {
return Long.numberOfLeadingZeros(~this.y);
}
public long yBase() {
return this.y << this.yRank();
}
public long yMask() {
return ~((1L << this.yRank()) - 1L);
}
public long ySplit() {
return this.y << 1 & 1L;
}
@Override
public Value fold() {
return this.fold;
}
@Override
public QTreePage page() {
Object page = this.page;
if (page instanceof WeakReference>) {
page = ((WeakReference>) page).get();
}
if (page instanceof QTreePage) {
this.context.hitPage((QTreePage) page);
return (QTreePage) page;
} else {
try (PageLoader pageLoader = this.context.openPageLoader(false)) {
return (QTreePage) pageLoader.loadPage(this);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
throw new StoreException(toDebugString(), error);
} else {
throw error;
}
}
}
}
@Override
public QTreePage hardPage() {
final Object page = this.page;
if (page instanceof QTreePage) {
return (QTreePage) page;
} else {
return null;
}
}
@Override
public QTreePage softPage() {
Object page = this.page;
if (page instanceof WeakReference>) {
page = ((WeakReference>) page).get();
}
if (page instanceof QTreePage) {
return (QTreePage) page;
} else {
return null;
}
}
@Override
public long softVersion() {
final QTreePage page = this.softPage();
if (page != null) {
return page.version();
} else {
return 0L;
}
}
@Override
public boolean isEmpty() {
return this.span == 0L;
}
@Override
public boolean isCommitted() {
return this.zone > 0 && this.base > 0L;
}
@Override
public int pageRefSize() {
int pageRefSize = this.pageRefSize;
if (pageRefSize < 0) {
pageRefSize = 5; // "@page"
if (this.post != this.zone) {
pageRefSize += 6; // "(post:"
pageRefSize += Recon.sizeOf(Num.from(this.post));
}
pageRefSize += 6; // "[(,]zone:"
pageRefSize += Recon.sizeOf(Num.from(this.zone));
pageRefSize += 6; // ",base:"
pageRefSize += Recon.sizeOf(Num.from(this.base));
pageRefSize += 6; // ",size:"
pageRefSize += Recon.sizeOf(Num.from(this.pageSize()));
pageRefSize += 6; // ",area:"
pageRefSize += Recon.sizeOf(Num.from(this.treeSize()));
pageRefSize += 6; // ",span:"
pageRefSize += Recon.sizeOf(Num.from(this.span));
pageRefSize += 3; // ",x:"
pageRefSize += 18; // "0xXXXXXXXXXXXXXXXX";
pageRefSize += 3; // ",y:"
pageRefSize += 18; // "0xXXXXXXXXXXXXXXXX";
final Value fold = this.fold();
if (fold.isDefined()) {
pageRefSize += 6; // ",fold:"
pageRefSize += Recon.sizeOf(fold);
}
pageRefSize += 1; // ')'
this.pageRefSize = pageRefSize; // Must match bytes written by writePageRef
}
return pageRefSize;
}
@Override
public int pageSize() {
if (this.pageSize < 0) {
this.page().memoizeSize(this);
}
return this.pageSize;
}
@Override
public int diffSize() {
if (this.diffSize < 0) {
this.page().memoizeSize(this);
}
return this.diffSize;
}
@Override
public long treeSize() {
if (this.treeSize < 0L) {
this.page().memoizeSize(this);
}
return this.treeSize;
}
@Override
public Value toValue() {
final Record header = Record.create(9);
if (this.post != this.zone) {
header.slot("post", this.post);
}
header.slot("zone", this.zone)
.slot("base", this.base)
.slot("size", this.pageSize())
.slot("area", this.treeSize())
.slot("span", this.span)
.slot("x", Num.uint64(this.x))
.slot("y", Num.uint64(this.y));
final Value fold = this.fold();
if (fold.isDefined()) {
header.slot("fold", fold);
}
return Record.create(1).attr(this.pageType.tag(), header);
}
public QTreePageRef reduced(Value identity, CombinerFunction super Value, Value> accumulator,
CombinerFunction combiner, long newVersion) {
if (!this.fold.isDefined()) {
return this.page().reduced(identity, accumulator, combiner, newVersion).pageRef();
} else {
return this;
}
}
@Override
public QTreePageRef evacuated(int post, long version) {
if (this.post != 0 && this.post < post) {
final QTreePage page = this.page();
try {
return page.evacuated(post, version).pageRef();
} catch (Throwable cause) {
if (Cont.isNonFatal(cause)) {
throw new StoreException(cause);
} else {
throw new StoreException(this.toDebugString(), cause);
}
}
} else {
return this;
}
}
@Override
public QTreePageRef committed(int zone, long base, long version) {
final QTreePage page = this.hardPage();
if (page != null) {
return page.committed(zone, base, version).pageRef();
} else {
return this;
}
}
@Override
public QTreePageRef uncommitted(long version) {
final QTreePage page = this.hardPage();
if (page != null && page.version() >= version) {
return page.uncommitted(version).pageRef();
} else {
return this;
}
}
@Override
public void writePageRef(Output> output) {
Recon.write(output, this.toValue());
}
@Override
public void writePage(Output> output) {
this.page().writePage(output);
}
@Override
public void writeDiff(Output> output) {
this.page().writeDiff(output);
}
@Override
public void buildDiff(Builder builder) {
this.page().buildDiff(builder);
}
@Override
public QTreePage setPageValue(Value value, boolean isResident) {
final QTreePage page = QTreePage.fromValue(this, value);
if (isResident) {
this.page = page;
} else {
this.context.hitPage(page);
this.page = new WeakReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy