您好,登錄后才能下訂單哦!
這篇文章主要介紹“RoaringBitmap的使用方法”,在日常操作中,相信很多人在RoaringBitmap的使用方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”RoaringBitmap的使用方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
參考: RoaringBitmap
public class Demo { @Test public void test1(){ // 向r1中添加1、2、3、1000四個數字 RoaringBitmap r1 = RoaringBitmap.bitmapOf(1, 2, 3, 1000); // 返回第3個數字是1000 System.out.println(r1.select(3)); r1.add(5); // 返回10000的索引,是4 System.out.println(r1.rank(1000)); System.out.println(r1.rank(3)); System.out.println(r1.rank(2)); System.out.println(r1.rank(1)); // 是否包含1000和7,true和false System.out.println(r1.contains(1000)); System.out.println(r1.contains(7)); RoaringBitmap r2 = new RoaringBitmap(); // 向r2添加10000-12000共2000個數字 r2.add(10000L, 12000L); // 將兩個r1,r2進行合并,數值進行合并,合并產生新的RoaringBitmap RoaringBitmap r3 = RoaringBitmap.or(r1, r2); // r1和r2進行位運算,并將結果賦值給r1 r1.or(r2); // 判斷r1與r3是否相等,true System.out.println(r1.equals(r3)); // 查看r1中存儲了多少個值,2004 System.out.println(r1.getLongCardinality()); // 兩種遍歷方式 for(int i : r1){ System.out.println(i); } r1.forEach((Consumer<? super Integer>) i -> System.out.println(i.intValue())); } @Test public void test2(){ RoaringBitmap roaringBitmap = new RoaringBitmap(); roaringBitmap.add(1L, 10L); // 遍歷輸出 roaringBitmap.forEach((IntConsumer)i -> System.out.println(i)); // 遍歷放入List中 List<Integer> numbers = new ArrayList<>(); roaringBitmap.forEach((IntConsumer) numbers::add); System.out.println(numbers); roaringBitmap.runOptimize(); int size = roaringBitmap.serializedSizeInBytes(); ByteBuffer buffer = ByteBuffer.allocate(size); roaringBitmap.serialize(buffer); // 將RoaringBitmap的數據轉成字節數組,這樣就可以直接存入數據庫了,數據庫字段類型BLOB byte[] bitmapData = buffer.array(); } }
// List變成RoaringBitmap private RoaringBitmap buildBitmap(List<String> idList) { RoaringBitmap roaringBitmap = new RoaringBitmap(); idList.forEach(id -> roaringBitmap.add(Integer.valueOf(id.trim()))); roaringBitmap.runOptimize(); return roaringBitmap; }
// a + b - c private RoaringBitmap mergeBitmap(List<RoaringBitmap> a, List<RoaringBitmap> b, List<RoaringBitmap> c) { // a + b List<RoaringBitmap> blacklistBitMaps = Lists.newLinkedList(); blacklistBitMaps.addAll(a); blacklistBitMaps.addAll(b); RoaringBitmap blacklistBitMap = aggregationToBitMap(blacklistBitMaps); // c RoaringBitmap whitelistBitMap = aggregationToBitMap(c); // a + b - c blacklistBitMap.andNot(whitelistBitMap); return blacklistBitMap; } private RoaringBitmap aggregationToBitMap(List<RoaringBitmap> list) { if (CollectionUtils.isEmpty(list)) { return new RoaringBitmap(); } if (list.size() == 1) { return list.get(0); } return FastAggregation.or(list.listIterator()); }
create table hobby ( id int auto_increment primary key, name varchar(255) not null, hobbies blob null );
// 忽略get、set方法 public class HobbyDO { private Integer id; private String name; private RoaringBitmap bitmap; }
public interface HobbyDOMapper { int insert(HobbyDO record); HobbyDO selectByPrimaryKey(Integer id); Integer updateBitmap(@Param("id") Integer id, @Param("bitmap") byte[] bitmap); }
public class HobbyBitmapBlobHandler extends BaseTypeHandler<RoaringBitmap> { @Override public void setNonNullParameter(PreparedStatement ps, int i, RoaringBitmap parameter, JdbcType jdbcType) throws SQLException { byte[] content = serializeRoaringBitmap(parameter); if (content == null) { return; } ByteArrayInputStream bis = new ByteArrayInputStream(content); ps.setBinaryStream(i, bis, content.length); } @Override public RoaringBitmap getNullableResult(ResultSet rs, String columnName) throws SQLException { return deSerializeRoaringBitmap(rs.getBlob(columnName)); } @Override public RoaringBitmap getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return deSerializeRoaringBitmap(rs.getBlob(columnIndex)); } @Override public RoaringBitmap getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return deSerializeRoaringBitmap(cs.getBlob(columnIndex)); } private byte[] serializeRoaringBitmap(RoaringBitmap roaringBitmap) { if (roaringBitmap == null) { return null; } int size = roaringBitmap.serializedSizeInBytes(); ByteBuffer buffer = ByteBuffer.allocate(size); roaringBitmap.serialize(buffer); return buffer.array(); } private RoaringBitmap deSerializeRoaringBitmap(Blob blob) { if (blob == null) { return null; } byte[] content; try { content = blob.getBytes(1, (int) blob.length()); } catch (SQLException e) { e.printStackTrace(); return null; } ByteBuffer buffer = ByteBuffer.wrap(content); return new RoaringBitmap(new ImmutableRoaringBitmap(buffer)); } }
@Test public void insert() { HobbyDO hobby = new HobbyDO(); hobby.setName("lwh"); RoaringBitmap bitmap = new RoaringBitmap(); bitmap.add(111); bitmap.add(222); bitmap.add(333); hobby.setBitmap(bitmap); hobbyDOMapper.insert(hobby); } @Test public void update(){ RoaringBitmap bitmap = new RoaringBitmap(); bitmap.add(133); bitmap.add(222); bitmap.add(333); int size = bitmap.serializedSizeInBytes(); ByteBuffer buffer = ByteBuffer.allocate(size); bitmap.serialize(buffer); byte[] bitmapData = buffer.array(); hobbyDOMapper.updateBitmap(1, bitmapData); } @Test public void select(){ HobbyDO hobbyDO = hobbyDOMapper.selectByPrimaryKey(1); hobbyDO.getBitmap().forEach((IntConsumer) System.out::println); }
到此,關于“RoaringBitmap的使用方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。