1 /*  $Id: huge_asn_loader.cpp 636820 2021-08-31 18:41:37Z ivanov $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors:  Sergiy Gotvyanskyy
27 *
28 * File Description:
29 *
30 *
31 */
32 
33 #include <ncbi_pch.hpp>
34 
35 #include "huge_asn_loader.hpp"
36 #include "huge_asn_reader.hpp"
37 #include <objmgr/impl/tse_loadlock.hpp>
38 
39 BEGIN_NCBI_SCOPE
40 BEGIN_SCOPE(objects)
41 
42 namespace
43 {
44 
45 class CHugeAsnDataLoaderMaker : public CLoaderMaker_Base
46 {
47 public:
CHugeAsnDataLoaderMaker(const string & name,const string & filename)48     CHugeAsnDataLoaderMaker(const string& name, const string& filename):
49         m_filename(filename)
50     {
51         m_Name = name;
52     }
53 
CreateLoader(void) const54     virtual CDataLoader* CreateLoader(void) const
55     {
56         return new CHugeAsnDataLoader(m_Name, m_filename);
57     }
58 
59     typedef CHugeAsnDataLoader::TRegisterLoaderInfo TRegisterInfo;
GetRegisterInfo(void)60     TRegisterInfo GetRegisterInfo(void)
61     {
62         TRegisterInfo info;
63         info.Set(m_RegisterInfo.GetLoader(), m_RegisterInfo.IsCreated());
64         return info;
65     }
66 private:
67     const string m_filename;
68 };
69 
70 }
71 
CHugeAsnDataLoader(const string & name,const string & filename)72 CHugeAsnDataLoader::CHugeAsnDataLoader(const string& name, const string& filename):
73     CDataLoader(name),
74     m_filename{filename}
75 {
76 }
77 
GetBlobId(const CSeq_id_Handle & idh)78 CDataLoader::TBlobId CHugeAsnDataLoader::GetBlobId(const CSeq_id_Handle& idh)
79 {
80     if (m_reader.get() == nullptr)
81     {
82 #       ifdef _DEBUG
83         std::cout << "Indexing " << m_filename << "\n";
84 #       endif
85         //m_reader.reset(new CHugeAsnReader(m_filename));
86         //m_reader->PrintAllSeqIds();
87     }
88     size_t info = m_reader->FindTopObject(idh.GetSeqId());
89     if (info != std::numeric_limits<size_t>::max()) {
90         TBlobId blob_id = new CBlobIdInt(info);
91         return blob_id;
92     }
93     return {};
94 }
95 
GetBlobById(const TBlobId & blob_id)96 CDataLoader::TTSE_Lock CHugeAsnDataLoader::GetBlobById(const TBlobId& blob_id)
97 {
98     // Load data, get the lock
99     CTSE_LoadLock lock = GetDataSource()->GetTSE_LoadLock(blob_id);
100     if ( !lock.IsLoaded() ) {
101         auto id = (const CBlobIdInt*)&*blob_id;
102         size_t info = id->GetValue();
103         auto entry = m_reader->LoadSeqEntry(info);
104 #       if _DEBUG
105         std::cerr << "Loading " << info << "\n";
106 #       endif
107         CTSE_Info& tse_info = *lock;
108         tse_info.SetSeq_entry(*entry);
109         lock.SetLoaded();
110     }
111     return lock;
112 }
113 
114 CDataLoader::TTSE_LockSet
GetRecords(const CSeq_id_Handle & idh,EChoice choice)115 CHugeAsnDataLoader::GetRecords(const CSeq_id_Handle& idh, EChoice choice)
116 {
117     TTSE_LockSet locks;
118     TBlobId blob_id = GetBlobId(idh);
119     if ( blob_id ) {
120         TTSE_Lock lock = GetBlobById(blob_id);
121         if ( lock ) {
122             locks.insert(lock);
123         }
124     }
125     return locks;
126 }
127 
RegisterInObjectManager(CObjectManager & om,const string & loader_name,const string & filename,CObjectManager::EIsDefault is_default,CObjectManager::TPriority priority)128 CHugeAsnDataLoader::TRegisterLoaderInfo CHugeAsnDataLoader::RegisterInObjectManager(
129     CObjectManager& om,
130     const string& loader_name,
131     const string& filename,
132     CObjectManager::EIsDefault is_default,
133     CObjectManager::TPriority priority)
134 {
135     CHugeAsnDataLoaderMaker maker(loader_name, filename);
136     CDataLoader::RegisterInObjectManager(om, maker, is_default, priority);
137     return maker.GetRegisterInfo();
138 }
139 
140 END_SCOPE(objects)
141 END_NCBI_SCOPE
142