Skip to content

Commit 79f72a9

Browse files
committed
bitMap 调整
1 parent f3ad3a5 commit 79f72a9

18 files changed

Lines changed: 1757 additions & 0 deletions

File tree

db/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616
<artifactId>gbase-connector</artifactId>
1717
<version>8.8</version>
1818
</dependency>
19+
<dependency>
20+
<groupId>cn.hutool</groupId>
21+
<artifactId>hutool-db</artifactId>
22+
<version>5.8.6</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>mysql</groupId>
26+
<artifactId>mysql-connector-java</artifactId>
27+
<version>8.0.31</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.testng</groupId>
31+
<artifactId>testng</artifactId>
32+
<version>7.5</version>
33+
</dependency>
1934
</dependencies>
2035

2136

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.salk.lib.db.gbase;
2+
3+
public class BitMapByInt {
4+
5+
int[] data;
6+
int max; //表示最大的那个数
7+
8+
public BitMapByInt(int max) {
9+
this.max = max;
10+
data = new int[(max >> 5) + 1]; //max/32 + 1
11+
}
12+
public void add(int n) { //往bitmap里面添加数字
13+
int index = n >> 5; // 除以32 就可以知道在那个int
14+
int loc = n & 31; ///这里其实还可以用&运算
15+
//接下来就是要把数组里面的 index这个下标的int里面的 第loc 个bit位置为1
16+
data[index] |= 1 << loc; //或运算
17+
}
18+
public void delete(int n){
19+
int index = n >> 5;
20+
int loc = n & 31;
21+
data[index] ^= 1 << (loc);//异或运算
22+
}
23+
public boolean find(int n) {
24+
int index = n >> 5; // 除以8 就可以知道在那个byte
25+
int loc = n & 31; ///这里其实还可以用&运算,等同于%32
26+
int flag = data[index] & (1 << loc); //如果原来的那个位置是0 那肯定就是0 只有那个位置是1 才行
27+
if(flag == 0) return false;
28+
return true;
29+
}
30+
public static void main(String[] args) {
31+
BitMapByInt map = new BitMapByInt(200000001); //10亿
32+
map.add(2);
33+
map.add(3);
34+
map.add(65);
35+
map.add(66);
36+
map.delete(2);
37+
System.out.println(map.find(2));
38+
System.out.println(map.find(3));
39+
}
40+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.salk.lib.db.gbase;
2+
3+
import com.salk.lib.db.mysql.bit.BitUtil;
4+
5+
/**
6+
* @author salkli
7+
* @since 2023/3/30
8+
**/
9+
public class ByteBitMap {
10+
11+
public static void main(String[] args) {
12+
ByteBitMap bitMap = new ByteBitMap(100); //10亿
13+
bitMap.add(2);
14+
bitMap.add(4);
15+
bitMap.add(65);
16+
bitMap.add(66);
17+
bitMap.add(99);
18+
bitMap.add(100);
19+
byte[] bits = bitMap.bits;
20+
long l = BitUtil.byteArrayToLong(bits);
21+
System.out.println("long========"+l);
22+
23+
long[] longs = BitUtil.toLongArray(bits);
24+
System.out.println("longs========"+longs);
25+
26+
ByteBitMap bitMap2 = new ByteBitMap(100); //10亿
27+
bitMap2.add(2);
28+
bitMap2.add(4);
29+
bitMap2.add(65);
30+
bitMap2.add(66);
31+
bitMap2.add(99);
32+
bitMap2.add(100);
33+
byte[] bits1 = bitMap2.bits;
34+
byte[] yihuo=new byte[bits.length];
35+
for (int i = 0; i < Math.min(bits.length, bits1.length); i++) {
36+
yihuo[i]=(byte)(bits[i] ^ bits1[i]);
37+
38+
}
39+
boolean result=true;
40+
for (int i = 0; i < Math.min(bits.length, yihuo.length); i++) {
41+
if((yihuo[i]|bits[i])!=bits[i]){
42+
result=false;
43+
}
44+
45+
}
46+
47+
System.out.println(result);
48+
}
49+
50+
byte[] bits; //如果是byte那就一个只能存8个数,一个字节是8位
51+
int max; //表示最大的那个数
52+
53+
public ByteBitMap(int max) {
54+
this.max = max;
55+
bits = new byte[(max >> 3) + 1]; //max/8 + 1
56+
}
57+
public void add(int n) { //往bitmap里面添加数字
58+
int bitsIndex = n >> 3; // 除以8 就可以知道在那个byte
59+
int loc = n & 7; ///这里其实还可以用&运算
60+
//接下来就是要把bit数组里面的 bisIndex这个下标的byte里面的 第loc 个bit位置为1
61+
bits[bitsIndex] |= 1 << loc; //或运算
62+
}
63+
public void delete(int n){
64+
int bitsIndex = n >> 3;
65+
int loc = n & 7;
66+
bits[bitsIndex] ^= 1 << (loc);//异或运算
67+
}
68+
public boolean find(int n) {
69+
int bitsIndex = n >> 3; // 除以8 就可以知道在那个byte
70+
int loc = n & 7; ///这里其实还可以用求余运算,等同于%8
71+
int flag = bits[bitsIndex] & (1 << loc); //如果原来的那个位置是0 那肯定就是0 只有那个位置是1 才行
72+
if(flag == 0) return false;
73+
return true;
74+
}
75+
76+
77+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.salk.lib.db.gbase;
2+
3+
/**
4+
* @author salkli
5+
* @since 2023/3/30
6+
**/
7+
import com.salk.lib.db.mysql.bit.BitUtil;
8+
9+
import java.io.FileInputStream;
10+
import java.io.FileOutputStream;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.sql.*;
14+
import java.util.Properties;
15+
16+
public class InsertData {
17+
public static void main(String[] args) {
18+
InsertData insertData=new InsertData();
19+
//insertData.getBlob();
20+
insertData.insertBlob();
21+
}
22+
public void getBlob(){//读取Blob数据
23+
Connection con = null;
24+
PreparedStatement ps = null;
25+
ResultSet rs = null;
26+
try {
27+
con = JDBCTools.getConnection();
28+
String sql = "SELECT varb1,varb2,f_binary_l FROM t_fields where varb1 is not null";
29+
ps = con.prepareStatement(sql);
30+
rs = ps.executeQuery();
31+
if(rs.next()){
32+
byte[] picture = rs.getBytes(1);//得到Blob对象
33+
System.out.println(picture);
34+
35+
byte[] picture1 = rs.getBytes(2);//得到Blob对象
36+
37+
byte[] yihuo=new byte[picture.length];
38+
for (int i = 0; i < Math.min(picture.length, picture1.length); i++) {
39+
yihuo[i]=(byte)(picture[i] ^ picture1[i]);
40+
41+
}
42+
boolean result=true;
43+
for (int i = 0; i < Math.min(picture.length, yihuo.length); i++) {
44+
if((yihuo[i]|picture[i])!=picture[i]){
45+
result=false;
46+
}
47+
48+
}
49+
System.out.println(result);
50+
//开始读入文件
51+
System.out.println(picture1);
52+
}
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
58+
59+
public void insertBlob(){//插入Blob
60+
Connection con = null;
61+
PreparedStatement ps = null;
62+
try {
63+
con = JDBCTools.getConnection();
64+
String sql = "INSERT INTO t_bit(f_binary_str,f_blob,f_binary_l,f_binary_l2) VALUES(?,?,?,?)";
65+
ps = con.prepareStatement(sql);
66+
ByteBitMap bitMap = new ByteBitMap(44);
67+
bitMap.add(2);
68+
bitMap.add(3);
69+
bitMap.add(43);
70+
//bitMap.add(64);
71+
//bitMap.add(456);
72+
//bitMap.add(32332);
73+
byte[] bits = bitMap.bits;
74+
ps.setBytes(1,bits);
75+
76+
ByteBitMap bitMap2 = new ByteBitMap(44);
77+
bitMap2.add(2);
78+
bitMap2.add(3);
79+
bitMap2.add(5);
80+
bitMap2.add(43);
81+
//bitMap2.add(64);
82+
//bitMap2.add(456);
83+
//bitMap2.add(32332);
84+
byte[] bits2 = bitMap2.bits;
85+
86+
ps.setBytes(2, bits2);
87+
long l = BitUtil.bytesToLong(bits);
88+
System.out.println(l);
89+
ps.setLong(3, l);
90+
long l2 = BitUtil.bytesToLong(bits2);
91+
ps.setLong(4, l2);
92+
System.out.println(l2);
93+
ps.executeUpdate();
94+
} catch (Exception e) {
95+
e.printStackTrace();
96+
}finally{
97+
JDBCTools.release(con, ps);
98+
}
99+
}
100+
public static class JDBCTools {//JDBC工具类 用来建立连接和释放连接
101+
public static Connection getConnection() throws Exception{//连接数据库
102+
String driverClass = null;
103+
String url = null;
104+
String user = null;
105+
String password = null;
106+
107+
Properties properties = new Properties();
108+
109+
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
110+
properties.load(in);
111+
driverClass = properties.getProperty("driver");
112+
url = properties.getProperty("jdbcurl");
113+
user = properties.getProperty("user");
114+
password = properties.getProperty("password");
115+
Class.forName(driverClass);
116+
return DriverManager.getConnection(url, user, password);
117+
}
118+
public static void release(Connection con , Statement state){//关闭数据库连接
119+
if(state != null){
120+
try {
121+
state.close();
122+
} catch (SQLException e) {
123+
e.printStackTrace();
124+
}
125+
}
126+
if(con != null){
127+
try {
128+
con.close();
129+
} catch (SQLException e) {
130+
e.printStackTrace();
131+
}
132+
}
133+
134+
}
135+
public static void release(ResultSet rs , Connection con , Statement state){//关闭数据库连接
136+
if(rs != null)
137+
{
138+
try {
139+
rs.close();
140+
} catch (SQLException e) {
141+
e.printStackTrace();
142+
}
143+
}
144+
if(state != null){
145+
try {
146+
state.close();
147+
} catch (SQLException e) {
148+
e.printStackTrace();
149+
}
150+
}
151+
if(con != null){
152+
try {
153+
con.close();
154+
} catch (SQLException e) {
155+
e.printStackTrace();
156+
}
157+
}
158+
}
159+
}
160+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.salk.lib.db.gbase;
2+
3+
import com.salk.lib.db.mysql.bit.BitUtil;
4+
5+
/**
6+
* @author salkli
7+
* @since 2023/3/30
8+
**/
9+
public class LongBitMap {
10+
11+
public static void main(String[] args) {
12+
LongBitMap bitMap = new LongBitMap(100); //10亿
13+
bitMap.add(2);
14+
bitMap.add(4);
15+
bitMap.add(65);
16+
bitMap.add(66);
17+
bitMap.add(99);
18+
bitMap.add(100);
19+
byte[] bits = bitMap.bits;
20+
long l = BitUtil.byteArrayToLong(bits);
21+
System.out.println("long========"+l);
22+
23+
long[] longs = BitUtil.toLongArray(bits);
24+
System.out.println("longs========"+longs);
25+
26+
LongBitMap bitMap2 = new LongBitMap(100); //10亿
27+
bitMap2.add(2);
28+
bitMap2.add(4);
29+
bitMap2.add(65);
30+
bitMap2.add(66);
31+
bitMap2.add(99);
32+
bitMap2.add(100);
33+
byte[] bits1 = bitMap2.bits;
34+
byte[] yihuo=new byte[bits.length];
35+
for (int i = 0; i < Math.min(bits.length, bits1.length); i++) {
36+
yihuo[i]=(byte)(bits[i] ^ bits1[i]);
37+
38+
}
39+
boolean result=true;
40+
for (int i = 0; i < Math.min(bits.length, yihuo.length); i++) {
41+
if((yihuo[i]|bits[i])!=bits[i]){
42+
result=false;
43+
}
44+
45+
}
46+
47+
System.out.println(result);
48+
}
49+
50+
byte[] bits; //如果是byte那就一个只能存8个数,一个字节是8位
51+
int max; //表示最大的那个数
52+
53+
public LongBitMap(int max) {
54+
this.max = max;
55+
bits = new byte[(max >> 3) + 1]; //max/8 + 1
56+
}
57+
public void add(int n) { //往bitmap里面添加数字
58+
int bitsIndex = n >> 3; // 除以8 就可以知道在那个byte
59+
int loc = n & 7; ///这里其实还可以用&运算
60+
//接下来就是要把bit数组里面的 bisIndex这个下标的byte里面的 第loc 个bit位置为1
61+
bits[bitsIndex] |= 1 << loc; //或运算
62+
}
63+
public void delete(int n){
64+
int bitsIndex = n >> 3;
65+
int loc = n & 7;
66+
bits[bitsIndex] ^= 1 << (loc);//异或运算
67+
}
68+
public boolean find(int n) {
69+
int bitsIndex = n >> 3; // 除以8 就可以知道在那个byte
70+
int loc = n & 7; ///这里其实还可以用求余运算,等同于%8
71+
int flag = bits[bitsIndex] & (1 << loc); //如果原来的那个位置是0 那肯定就是0 只有那个位置是1 才行
72+
if(flag == 0) return false;
73+
return true;
74+
}
75+
76+
77+
}

0 commit comments

Comments
 (0)