1919#include < memory>
2020#include < ostream>
2121#include < set>
22+ #include < sstream>
2223#include < string>
2324#include < vector>
2425
2930#include " eckit/io/fam/FamObject.h"
3031#include " eckit/io/fam/FamObjectName.h"
3132#include " eckit/io/fam/FamPath.h"
33+ #include " eckit/io/fam/FamRegion.h"
3234#include " eckit/log/Log.h"
3335
3436#include " fdb5/LibFdb5.h"
3537#include " fdb5/database/Field.h"
3638#include " fdb5/database/FieldLocation.h"
3739#include " fdb5/database/Key.h"
3840#include " fdb5/database/Store.h"
41+ #include " fdb5/database/WipeState.h"
3942#include " fdb5/fam/FamCommon.h"
4043#include " fdb5/fam/FamFieldLocation.h"
4144
@@ -45,12 +48,19 @@ static const StoreBuilder<FamStore> builder(FamCommon::type);
4548
4649// ----------------------------------------------------------------------------------------------------------------------
4750
48- FamStore::FamStore (const Key& key, const Config& config) : FamCommon(config, key), config_(config) {}
51+ FamStore::FamStore (const Key& key, const Config& config) : FamCommon(key, config) {}
52+
53+ FamStore::FamStore (const eckit::URI& uri, const Config& config) : FamCommon(uri, config) {}
4954
5055FamStore::~FamStore () = default ;
5156
5257// ----------------------------------------------------------------------------------------------------------------------
5358
59+ eckit::URI FamStore::uri (const eckit::URI& dataURI) {
60+ ASSERT (dataURI.scheme () == FamCommon::type);
61+ return eckit::FamObjectName (dataURI).uri ();
62+ }
63+
5464auto FamStore::uri () const -> eckit::URI {
5565 return FamCommon::uri ();
5666}
@@ -79,12 +89,22 @@ void FamStore::close() {
7989 LOG_DEBUG_LIB (LibFdb5) << " FamStore::close() nothing to do!" << ' \n ' ;
8090}
8191
82- auto FamStore::asCollocatedDataURIs (const std::vector<eckit::URI>& /* uriList */ ) const -> std::set<eckit::URI> {
83- NOTIMP;
92+ std::set<eckit::URI> FamStore::collocatedDataURIs () const {
93+ return {};
94+ }
95+
96+ std::set<eckit::URI> FamStore::asCollocatedDataURIs (const std::set<eckit::URI>& uris) const {
97+ std::set<eckit::URI> res;
98+ for (const auto & uri : uris) {
99+ ASSERT (uriBelongs (uri));
100+ res.insert (uri);
101+ }
102+ return res;
84103}
85104
86- auto FamStore::collocatedDataURIs () const -> std::vector<eckit::URI> {
87- NOTIMP;
105+ std::vector<eckit::URI> FamStore::getAuxiliaryURIs (const eckit::URI& uri, bool /* onlyExisting*/ ) const {
106+ ASSERT (uri.scheme () == type ());
107+ return {};
88108}
89109
90110auto FamStore::makeObject (const Key& key) const -> eckit::FamObjectName {
@@ -117,14 +137,6 @@ auto FamStore::archive(const Key& key, const void* data, eckit::Length length) -
117137 return std::make_unique<FamFieldLocation>(object.uri (), 0 , length, fdb5::Key ());
118138}
119139
120- void FamStore::remove (const Key& key) const {
121- auto object = makeObject (key);
122-
123- LOG_DEBUG_LIB (LibFdb5) << " FamStore removing object: " << object << ' \n ' ;
124-
125- object.lookup ().deallocate ();
126- }
127-
128140void FamStore::remove (const eckit::URI& uri, std::ostream& logAlways, std::ostream& logVerbose, bool doit) const {
129141 ASSERT (root_.uriBelongs (uri));
130142
@@ -136,6 +148,83 @@ void FamStore::remove(const eckit::URI& uri, std::ostream& logAlways, std::ostre
136148 }
137149}
138150
151+ void FamStore::finaliseWipeState (StoreWipeState& storeState, bool /* doit*/ , bool /* unsafeWipeAll*/ ) {
152+
153+ const std::set<eckit::URI>& dataURIs = storeState.includedDataURIs ();
154+
155+ if (dataURIs.empty ()) {
156+ return ;
157+ }
158+
159+ if (!root_.exists ()) {
160+ storeState.markAllMissing ();
161+ return ;
162+ }
163+
164+ for (const auto & uri : dataURIs) {
165+ if (!uriBelongs (uri)) {
166+ std::stringstream msg;
167+ msg << " FamStore::finaliseWipeState: Index to be deleted has pointers to fields that don't belong to the "
168+ " configured store."
169+ << ' \n ' ;
170+ msg << " Configured Store URI: " << this ->uri ().asString () << ' \n ' ;
171+ msg << " Pointed Store unit URI: " << uri.asString () << ' \n ' ;
172+ msg << " Impossible to delete such fields. Index deletion aborted to avoid leaking fields." << ' \n ' ;
173+ throw eckit::SeriousBug (msg.str (), Here ());
174+ }
175+
176+ if (!uriExists (uri)) {
177+ storeState.markAsMissing (uri);
178+ }
179+ }
180+ }
181+
182+ bool FamStore::doWipeUnknowns (const std::set<eckit::URI>& unknownURIs) const {
183+ // if (!wipeDoit_ || !wipeUnsafeWipeAll_) {
184+ // return true;
185+ // }
186+
187+ for (const auto & uri : unknownURIs) {
188+ if (!uriBelongs (uri)) {
189+ continue ;
190+ }
191+ if (uriExists (uri)) {
192+ remove (uri, eckit::Log::info (), eckit::Log::info (), true );
193+ }
194+ }
195+
196+ return true ;
197+ }
198+
199+ bool FamStore::doWipeURIs (const StoreWipeState& wipeState) const {
200+ const bool wipeall = wipeState.safeURIs ().empty ();
201+
202+ for (const auto & uri : wipeState.dataAuxiliaryURIs ()) {
203+ remove (uri, eckit::Log::info (), eckit::Log::info (), true );
204+ }
205+
206+ for (const auto & uri : wipeState.includedDataURIs ()) {
207+ remove (uri, eckit::Log::info (), eckit::Log::info (), true );
208+ }
209+
210+ if (wipeall) {
211+ cleanupEmptyDatabase_ = true ;
212+ }
213+
214+ return true ;
215+ }
216+
217+ void FamStore::doWipeEmptyDatabase () const {
218+ if (!cleanupEmptyDatabase_) {
219+ return ;
220+ }
221+
222+ if (root_.exists ()) {
223+ root_.lookup ().destroy ();
224+ }
225+ cleanupEmptyDatabase_ = false ;
226+ }
227+
139228void FamStore::print (std::ostream& out) const {
140229 out << " FamStore[root=" << root_ << ' ]' ;
141230}
0 commit comments