@@ -15,7 +15,6 @@ limitations under the License.
1515*/
1616#include " file.h"
1717#include < cstdint>
18- #include < openssl/bio.h>
1918#include < string.h>
2019#include < stdarg.h>
2120#include < memory>
@@ -165,7 +164,7 @@ struct HeaderTrailer {
165164
166165 UUID::String uuid; // 37 bytes.
167166 UUID::String parent_uuid; // 37 bytes.
168- uint16_t reserved; // Reserved.
167+ uint16_t reserved; // Reserved.
169168
170169 static const uint8_t LSMT_V1 = 1 ; // v1 (UUID check)
171170 static const uint8_t LSMT_SUB_V1 = 1 ; // .1 deprecated level range.
@@ -179,7 +178,8 @@ struct HeaderTrailer {
179178
180179class LSMTReadOnlyFile ;
181180static LSMTReadOnlyFile *open_file_ro (IFile *file, bool ownership, bool reserve_tag);
182- static HeaderTrailer *verify_ht (IFile *file, char *buf, bool is_trailer = false , ssize_t st_size = -1 );
181+ static HeaderTrailer *verify_ht (IFile *file, char *buf, bool is_trailer = false ,
182+ ssize_t st_size = -1 );
183183
184184static const uint32_t ALIGNMENT = 512 ; // same as trim block size.
185185static const uint32_t ALIGNMENT4K = 4096 ;
@@ -520,6 +520,10 @@ class LSMTReadOnlyFile : public IFileRW {
520520 return 0 ;
521521 }
522522
523+ virtual std::vector<IFile *> get_lower_files () const override {
524+ return m_files;
525+ }
526+
523527 template <typename T1, typename T2, typename T3>
524528 inline void forward (void *&buffer, T1 &offset, T2 &count, T3 step) {
525529 (char *&)buffer += step * ALIGNMENT;
@@ -618,7 +622,8 @@ class LSMTReadOnlyFile : public IFileRW {
618622 if (!pht->is_sealed ()) {
619623 LOG_ERROR_RETURN (ENOTSUP, -1 , " Commit a compacted LSMTReadonlyFile is not allowed." );
620624 }
621- CompactOptions opts (&m_files, (SegmentMapping*)m_index->buffer (), m_index->size (), m_vsize, &args);
625+ CompactOptions opts (&m_files, (SegmentMapping *)m_index->buffer (), m_index->size (), m_vsize,
626+ &args);
622627
623628 atomic_uint64_t _no_use_var (0 );
624629 return compact (opts, _no_use_var);
@@ -1053,9 +1058,9 @@ class LSMTSparseFile : public LSMTFile {
10531058class LSMTWarpFile : public LSMTFile {
10541059public:
10551060 const static int READ_BUFFER_SIZE = 65536 ;
1056- IFile* m_target_file = nullptr ;
1061+ IFile * m_target_file = nullptr ;
10571062
1058- LSMTWarpFile (){
1063+ LSMTWarpFile () {
10591064 m_filetype = LSMTFileType::WarpFile;
10601065 }
10611066 ~LSMTWarpFile () {
@@ -1074,11 +1079,11 @@ class LSMTWarpFile : public LSMTFile {
10741079 };
10751080 m.tag = tag;
10761081 auto file = m_files[tag];
1077- LOG_DEBUG (" insert segment: `, filePtr: `" , m,file);
1082+ LOG_DEBUG (" insert segment: `, filePtr: `" , m, file);
10781083 auto ret = file->pwrite (buf, count, offset);
10791084 if (ret != (ssize_t )count) {
1080- LOG_ERRNO_RETURN (0 , -1 , " write failed, file:`, ret:`, pos:`, count:`" ,
1081- file, ret, offset, count);
1085+ LOG_ERRNO_RETURN (0 , -1 , " write failed, file:`, ret:`, pos:`, count:`" , file, ret,
1086+ offset, count);
10821087 }
10831088 static_cast <IMemoryIndex0 *>(m_index)->insert (m);
10841089 append_index (m);
@@ -1102,8 +1107,8 @@ class LSMTWarpFile : public LSMTFile {
11021107 while (lba.count > 0 ) {
11031108 SegmentMapping m;
11041109 m.offset = lba.offset / ALIGNMENT;
1105- m.length = (Segment::MAX_LENGTH < lba.count / ALIGNMENT ?
1106- Segment::MAX_LENGTH : lba.count / ALIGNMENT);
1110+ m.length = (Segment::MAX_LENGTH < lba.count / ALIGNMENT ? Segment::MAX_LENGTH
1111+ : lba.count / ALIGNMENT);
11071112 m.moffset = lba.roffset / ALIGNMENT;
11081113 m.tag = m_rw_tag + (uint8_t )SegmentType::remoteData;
11091114 LOG_DEBUG (" insert segment: ` into findex: `" , m, m_findex);
@@ -1211,8 +1216,8 @@ static HeaderTrailer *verify_ht(IFile *file, char *buf, bool is_trailer, ssize_t
12111216 LOG_ERRNO_RETURN (0 , nullptr , " failed to read file trailer." );
12121217 if (!pht->verify_magic () || !pht->is_trailer () || !pht->is_data_file () || !pht->is_sealed ())
12131218 LOG_ERROR_RETURN (0 , nullptr ,
1214- " trailer magic, trailer type, "
1215- " file type or sealedness doesn't match" );
1219+ " trailer magic, trailer type, "
1220+ " file type or sealedness doesn't match" );
12161221 return pht;
12171222}
12181223
@@ -1266,13 +1271,14 @@ static SegmentMapping *do_load_index(IFile *file, HeaderTrailer *pheader_trailer
12661271 if (ibuf[i].offset != SegmentMapping::INVALID_OFFSET) {
12671272 ibuf[index_size] = ibuf[i];
12681273 ibuf[index_size].tag = (warp_file_tag ? ibuf[i].tag : 0 );
1269- if (min_tag > ibuf[index_size].tag ) min_tag = ibuf[index_size].tag ;
1274+ if (min_tag > ibuf[index_size].tag )
1275+ min_tag = ibuf[index_size].tag ;
12701276 index_size++;
12711277 }
12721278 }
12731279 if (warp_file_tag) {
12741280 LOG_INFO (" rebuild index tag for LSMTWarpFile." );
1275- for (size_t i = 0 ; i< index_size; i++) {
1281+ for (size_t i = 0 ; i < index_size; i++) {
12761282 if (warp_file_tag == 1 ) /* only fsmeta */
12771283 ibuf[i].tag = (uint8_t )SegmentType::fsMeta;
12781284 if (warp_file_tag == 2 ) /* only remote data */
@@ -1451,8 +1457,7 @@ IFileRW *open_warpfile_rw(IFile *findex, IFile *fsmeta_file, IFile *target_file,
14511457 auto rst = new LSMTWarpFile;
14521458 rst->m_files .resize (2 );
14531459 LSMT::HeaderTrailer ht;
1454- auto p = do_load_index (findex, &ht, false ,
1455- 3 );
1460+ auto p = do_load_index (findex, &ht, false , 3 );
14561461 auto pi = create_memory_index0 (p, ht.index_size , 0 , -1 );
14571462 if (!pi) {
14581463 delete[] p;
@@ -1785,4 +1790,46 @@ IFileRW *stack_files(IFileRW *upper_layer, IFileRO *lower_layers, bool ownership
17851790 return rst;
17861791}
17871792
1793+ IMemoryIndex *open_file_index (IFile *file) {
1794+ HeaderTrailer ht;
1795+ auto p = do_load_index (file, &ht, true );
1796+ if (!p) {
1797+ LOG_ERROR_RETURN (0 , nullptr , " failed to load index" );
1798+ }
1799+
1800+ auto pi = create_memory_index (p, ht.index_size , HeaderTrailer::SPACE / ALIGNMENT,
1801+ ht.index_offset / ALIGNMENT, true , ht.virtual_size );
1802+ if (!pi) {
1803+ delete[] p;
1804+ LOG_ERROR_RETURN (0 , nullptr , " failed to create memory index" );
1805+ }
1806+ return pi;
1807+ }
1808+
1809+ IFileRO *open_files_with_merged_index (IFile **src_files, size_t n, IMemoryIndex *index,
1810+ bool ownership) {
1811+ vector<IFile *> m_files (src_files, src_files + n);
1812+ auto rst = new LSMTReadOnlyFile;
1813+ rst->m_index = index;
1814+ rst->m_files = move (m_files);
1815+ rst->m_vsize = index->vsize ();
1816+ rst->m_uuid .resize (rst->m_files .size ());
1817+ rst->m_file_ownership = ownership;
1818+ return rst;
1819+ }
1820+
1821+ int is_lsmt (IFile *file) {
1822+ char buf[HeaderTrailer::SPACE];
1823+ auto ret = file->pread (buf, HeaderTrailer::SPACE, 0 );
1824+ if (ret < (ssize_t )HeaderTrailer::SPACE)
1825+ LOG_ERRNO_RETURN (0 , -1 , " failed to read file header." );
1826+ auto pht = (HeaderTrailer *)buf;
1827+ if (!pht->verify_magic () || !pht->is_header ()) {
1828+ LOG_DEBUG (" file: ` is not lsmt object" , file);
1829+ return 1 ;
1830+ }
1831+ LOG_DEBUG (" file: ` is lsmt object" , file);
1832+ return 0 ;
1833+ }
1834+
17881835} // namespace LSMT
0 commit comments