模拟oracle 索引平衡树叶子节点

import java.util.ArrayList;

import java.util.List;

// 模拟Oracle中的ROWID,唯一标识表中的一行数据

class RowId {

private String dataFile; // 数据文件号

private int blockNumber; // 块号

private int rowNumber; // 行号

public RowId(String dataFile, int blockNumber, int rowNumber) {

this.dataFile = dataFile;

this.blockNumber = blockNumber;

this.rowNumber = rowNumber;

}

@Override

public String toString() {

return dataFile + ":" + blockNumber + ":" + rowNumber;

}

}

// B树索引的叶子节点

class LeafNode {

private int key; // 索引键值

private RowId rowId; // 对应的行ID

private LeafNode prev; // 左指针,指向前一个叶子节点

private LeafNode next; // 右指针,指向后一个叶子节点

public LeafNode(int key, RowId rowId) {

this.key = key;

this.rowId = rowId;

}

// getter和setter方法

public int getKey() {

return key;

}

public RowId getRowId() {

return rowId;

}

public LeafNode getPrev() {

return prev;

}

public void setPrev(LeafNode prev) {

this.prev = prev;

}

public LeafNode getNext() {

return next;

}

public void setNext(LeafNode next) {

this.next = next;

}

@Override

public String toString() {

return "Key: " + key + ", ROWID: " + rowId;

}

}

// 模拟B树索引的叶子节点双向链表

public class BTreeLeafNodes {

public static void main(String\[\] args) {

// 创建几个叶子节点

LeafNode node1 = new LeafNode(10, new RowId("f1", 100, 5));

LeafNode node2 = new LeafNode(20, new RowId("f1", 102, 3));

LeafNode node3 = new LeafNode(30, new RowId("f2", 50, 8));

LeafNode node4 = new LeafNode(40, new RowId("f2", 55, 2));

// 构建双向链表

connectNodes(node1, node2);

connectNodes(node2, node3);

connectNodes(node3, node4);

// 从头部遍历链表

System.out.println("从头部遍历叶子节点:");

LeafNode current = node1;

while (current != null) {

System.out.println(current);

current = current.getNext();

}

// 从尾部遍历链表

System.out.println("\n从尾部遍历叶子节点:");

current = node4;

while (current != null) {

System.out.println(current);

current = current.getPrev();

}

// 模拟范围查询 (key between 20 and 30)

System.out.println("\n范围查询结果 (key between 20 and 30):");

List<LeafNode> result = rangeQuery(node1, 20, 30);

for (LeafNode node : result) {

System.out.println(node);

}

}

// 连接两个叶子节点,建立双向关系

private static void connectNodes(LeafNode prevNode, LeafNode nextNode) {

prevNode.setNext(nextNode);

nextNode.setPrev(prevNode);

}

// 模拟范围查询,利用双向链表特性

private static List<LeafNode> rangeQuery(LeafNode head, int minKey, int maxKey) {

List<LeafNode> result = new ArrayList<>();

LeafNode current = head;

// 找到起始节点

while (current != null && current.getKey() < minKey) {

current = current.getNext();

}

// 收集范围内的节点

while (current != null && current.getKey() <= maxKey) {

result.add(current);

current = current.getNext();

}

return result;

}

}

相关推荐
ClouGence2 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
曹牧2 天前
Oracle EXPLAIN PLAN
数据库·oracle
贤时间2 天前
codex 助力oracle ebs 开发
数据库·oracle
秉承初心3 天前
PostgreSQL 数据性能瓶颈突破实战
数据库·postgresql·oracle
Curvatureflight3 天前
MySQL 深分页越来越慢?从 LIMIT OFFSET 改成游标分页
数据库·oracle
XZ-0700013 天前
MySQL事务
数据库·mysql·oracle
tiancaijiben3 天前
阿里云函数计算FC如何实现网站的定时任务与自动化
数据库·oracle·dba
xfhuangfu3 天前
Oracle 19c 多租户体系架构介绍
数据库·oracle·架构
杨云龙UP3 天前
Spotlight 接入 Oracle 数据库监控操作指南 2026-06-16
数据库·oracle·性能监控·预警·阈值·spotlight·瓶颈分析
unique3 天前
AI Coding 采集方案探索
jvm·人工智能·oracle