NCBI C++ ToolKit
bv_merger.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef ALGO_BV_MERGE_HPP_
2 #define ALGO_BV_MERGE_HPP_
3 
4 /* $Id: bv_merger.hpp 85434 2019-02-11 15:08:11Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Mike DiCuccio, Anatoliy Kuznetsov
30  *
31  * File Description: IMergeBlob implementation for bit-vector
32  *
33  */
34 
37 #include <util/bitset/bmserial.h>
38 
39 #include <vector>
40 
42 
43 /**
44  Implementation of merger interface for bitsets.
45 */
46 template<class BV = bm::bvector<> >
48 {
49 public:
50  typedef BV TBitVector;
51  typedef vector<CMergeVolumes::TRawBuffer*> TRawBufferStack;
52 public:
54  virtual ~CMergeBitsetBlob();
55  virtual void Merge(CMergeVolumes::TRawBuffer* buffer);
57  virtual void Reset();
58 
59 protected:
60  bm::word_t* m_TmpBvBlock; ///< deserialization temp block
61  TBitVector m_TmpBv; ///< temp bit-vector
62  TRawBufferStack m_BlobStack; ///< blocks to merge
63 
64 };
65 
66 template<class BV>
67 inline
69 {
71 }
72 
73 template<class BV>
74 inline
76 {
77  bm::aligned_free(m_TmpBvBlock);
78 }
79 
80 template<class BV>
81 inline
83 {
84  m_BlobStack.push_back(buffer);
85 }
86 
87 template<class BV>
88 inline
91 {
92  _ASSERT(m_BlobStack.size());
93  // if it's just one buffer - we have nothing to merge
94  // (no reserialization required)
95  if (m_BlobStack.size() == 1) {
96  CMergeVolumes::TRawBuffer* buf = m_BlobStack[0];
97  m_BlobStack.resize(0);
98  return buf;
99  }
100  // OR all buffers into temp vector, return Blobs to the pool
101  m_TmpBv.clear(true);
102  NON_CONST_ITERATE(TRawBufferStack, it, m_BlobStack) {
104  const unsigned char* buffer = &((*buf)[0]);
105  bm::deserialize(m_TmpBv, buffer, m_TmpBvBlock);
106  m_BufResourcePool->Put(buf);
107  }
108  m_BlobStack.resize(0);
109 
110  // serialize
111  typename TBitVector::statistics st1;
112  m_TmpBv.optimize(0, TBitVector::opt_compress, &st1);
113 
114  CMergeVolumes::TRawBuffer* sbuf = m_BufResourcePool->Get();
115  CMergeVolumes::TBufPoolGuard guard(*m_BufResourcePool, sbuf);
116 
117  if (st1.max_serialize_mem > sbuf->size()) {
118  sbuf->resize(st1.max_serialize_mem);
119  }
120  size_t size = bm::serialize(m_TmpBv, &(*sbuf)[0],
121  m_TmpBvBlock,
123  sbuf->resize(size);
124  return guard.Release();
125 
126 }
127 
128 template<class BV>
129 inline
131 {
132  NON_CONST_ITERATE(TRawBufferStack, it, m_BlobStack) {
134  m_BufResourcePool->Put(buf);
135  }
136  m_BlobStack.resize(0);
137 }
138 
140 
141 #endif /* ALGO_MERGE_HPP */
Serialization / compression of bvector<>. Set theoretical operations on compressed BLOBs.
Implementation of merger interface for bitsets.
Definition: bv_merger.hpp:48
bm::word_t * m_TmpBvBlock
deserialization temp block
Definition: bv_merger.hpp:60
vector< CMergeVolumes::TRawBuffer * > TRawBufferStack
Definition: bv_merger.hpp:51
virtual ~CMergeBitsetBlob()
Definition: bv_merger.hpp:75
virtual void Reset()
Reset merging, forget all the accumulated buffers.
Definition: bv_merger.hpp:130
virtual void Merge(CMergeVolumes::TRawBuffer *buffer)
Merge request Implementation MUST return the buffer to the pool.
Definition: bv_merger.hpp:82
virtual CMergeVolumes::TRawBuffer * GetMergeBuffer()
Returns destination (merged) buffer Caller MUST return the buffer to the buffer pool.
Definition: bv_merger.hpp:90
TRawBufferStack m_BlobStack
blocks to merge
Definition: bv_merger.hpp:62
TBitVector m_TmpBv
temp bit-vector
Definition: bv_merger.hpp:61
Reallocable memory buffer (no memory copy overhead) Mimics vector<>, without the overhead of explicit...
void resize(size_type new_size)
size_type size() const
BLOB merge interface, merges one or more BLOBs together.
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
Pool::TValue * Release()
Return the pointer to the caller, not to the pool.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
size_t serialize(const BV &bv, unsigned char *buf, bm::word_t *temp_block=0, unsigned serialization_flags=0)
Saves bitvector into memory.
Definition: bmserial.h:3071
size_t deserialize(BV &bv, const unsigned char *buf, bm::word_t *temp_block=0, const bm::bv_ref_vector< BV > *ref_vect=0)
Bitvector deserialization from a memory BLOB.
Definition: bmserial.h:3137
@ BM_NO_GAP_LENGTH
save no GAP info (save some space)
Definition: bmserial.h:3025
@ BM_NO_BYTE_ORDER
save no byte-order info (save some space)
Definition: bmserial.h:3024
char * buf
unsigned int word_t
Definition: bmconst.h:39
void aligned_free(void *ptr) BMNOEXCEPT
Aligned free.
Definition: bmalloc.h:464
void * aligned_new_malloc(size_t size)
Aligned malloc (unlike classic malloc it throws bad_alloc exception)
Definition: bmalloc.h:436
const unsigned set_block_alloc_size
Definition: bmconst.h:61
const struct ncbi::grid::netcache::search::fields::SIZE size
Compressed bitset (entry point to bm.h)
static pcre_uint8 * buffer
Definition: pcretest.c:1051
#define _ASSERT
Modified on Tue Apr 23 07:39:37 2024 by modify_doxy.py rev. 669887