Skip to content

Commit

Permalink
feat(FAM): add string support for property
Browse files Browse the repository at this point in the history
  • Loading branch information
mcakircali committed Nov 10, 2024
1 parent 6d0c0cb commit 95413af
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/eckit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ io/fam/FamObjectName.cc
io/fam/FamObjectName.h
io/fam/FamPath.cc
io/fam/FamPath.h
io/fam/FamProperty.cc
io/fam/FamProperty.h
io/fam/FamRegion.cc
io/fam/FamRegion.h
Expand Down
20 changes: 12 additions & 8 deletions src/eckit/io/fam/FamName.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
#include "eckit/io/fam/FamName.h"

#include "eckit/filesystem/URI.h"
#include "eckit/io/fam/detail/FamSessionDetail.h"
#include "eckit/io/fam/FamPath.h"
#include "eckit/io/fam/FamSession.h"
#include "eckit/net/Endpoint.h"
#include "eckit/serialisation/Stream.h"

#include <ostream>
#include <sstream>
#include <string>
#include <utility>

namespace eckit {

//----------------------------------------------------------------------------------------------------------------------

FamName::FamName(const net::Endpoint& endpoint, FamPath path): endpoint_ {endpoint}, path_ {std::move(path)} { }
FamName::FamName(const net::Endpoint& endpoint, FamPath path) : endpoint_ {endpoint}, path_ {std::move(path)} { }

FamName::FamName(const URI& uri): FamName(uri.endpoint(), uri) { }
FamName::FamName(const URI& uri) : FamName(uri.endpoint(), uri) { }

FamName::FamName(Stream& stream): endpoint_ {stream}, path_ {stream} { }
FamName::FamName(Stream& stream) : endpoint_ {stream}, path_ {stream} { }

FamName::~FamName() = default;

Expand Down Expand Up @@ -61,10 +65,10 @@ std::ostream& operator<<(std::ostream& out, const FamName& name) {
return out;
}

auto operator<<(Stream& stream, const FamName& name) -> Stream& {
stream << name.endpoint_;
stream << name.path_;
return stream;
auto operator<<(Stream& out, const FamName& name) -> Stream& {
out << name.endpoint_;
out << name.path_;
return out;
}

//----------------------------------------------------------------------------------------------------------------------
Expand Down
10 changes: 8 additions & 2 deletions src/eckit/io/fam/FamName.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "eckit/io/fam/FamPath.h"
#include "eckit/io/fam/FamSession.h"
#include "eckit/net/Endpoint.h"

#include <iosfwd>
#include <string>
Expand All @@ -41,6 +42,9 @@ class FamName {

virtual auto exists() const -> bool = 0;

/// @todo implement
// virtual auto lookup() const -> FamItem = 0;

auto asString() const -> std::string;

auto uri() const -> URI;
Expand All @@ -52,13 +56,15 @@ class FamName {
protected: // methods
auto session() const -> FamSession::SPtr;

auto path() -> FamPath& { return path_; }

virtual void print(std::ostream& out) const;

friend auto operator<<(std::ostream& out, const FamName& name) -> std::ostream&;

friend auto operator<<(Stream& stream, const FamName& name) -> Stream&;
friend auto operator<<(Stream& out, const FamName& name) -> Stream&;

protected: // members
private: // members
net::Endpoint endpoint_;

FamPath path_;
Expand Down
10 changes: 5 additions & 5 deletions src/eckit/io/fam/FamObjectName.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ namespace eckit {
//----------------------------------------------------------------------------------------------------------------------

auto FamObjectName::withRegion(const std::string& regionName) -> FamObjectName& {
path_.regionName = regionName;
path().regionName = regionName;
return *this;
}

auto FamObjectName::withObject(const std::string& objectName) -> FamObjectName& {
path_.objectName = objectName;
path().objectName = objectName;
return *this;
}

auto FamObjectName::withUUID() -> FamObjectName& {
return withObject(path_.generateUUID());
return withObject(path().generateUUID());
}

auto FamObjectName::lookup() const -> FamObject {
return session()->lookupObject(path_.regionName, path_.objectName);
return session()->lookupObject(path().regionName, path().objectName);
}

auto FamObjectName::allocate(const fam::size_t objectSize, const bool overwrite) const -> FamObject {
return session()->lookupRegion(path_.regionName).allocateObject(objectSize, path_.objectName, overwrite);
return session()->lookupRegion(path().regionName).allocateObject(objectSize, path().objectName, overwrite);
}

auto FamObjectName::exists() const -> bool {
Expand Down
2 changes: 0 additions & 2 deletions src/eckit/io/fam/FamPath.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

#include "eckit/io/fam/FamPath.h"

#include "eckit/config/LibEcKit.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/filesystem/URI.h"
// #include "eckit/log/Log.h"
#include "eckit/serialisation/Stream.h"
#include "eckit/utils/Tokenizer.h"

Expand Down
67 changes: 67 additions & 0 deletions src/eckit/io/fam/FamProperty.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/

/*
* This software was developed as part of the Horizon Europe programme funded project OpenCUBE
* (Grant agreement: 101092984) horizon-opencube.eu
*/

#include "eckit/io/fam/FamProperty.h"

#include <sys/types.h>
#include <unistd.h>

#include <string.h>

namespace eckit {

//----------------------------------------------------------------------------------------------------------------------

namespace {

fam::perm_t stringToPerm(const std::string& perm) {
return static_cast<fam::perm_t>(std::stoul(perm, nullptr, 8));
}

std::string permToString(fam::perm_t perm) {
char buf[5];
snprintf(buf, sizeof(buf), "%04o", perm);
return std::string(buf);
}

} // namespace

//----------------------------------------------------------------------------------------------------------------------

FamProperty::FamProperty(fam::size_t size, fam::perm_t perm, std::string name, std::uint32_t uid, std::uint32_t gid)
: size {size}, perm {perm}, name {std::move(name)}, uid {uid}, gid {gid} { }

FamProperty::FamProperty(fam::size_t size, fam::perm_t perm, const std::string& name)
: FamProperty(size, perm, name, getuid(), getgid()) { }

FamProperty::FamProperty(fam::size_t size, fam::perm_t perm) : FamProperty(size, perm, "") { }

FamProperty::FamProperty(fam::size_t size, const std::string& perm) : FamProperty(size, stringToPerm(perm)) { }

void FamProperty::print(std::ostream& out) const {
out << "Property[size=" << size << ", perm=" << perm << "(" << permToString(perm) << ")" << ",name=" << name
<< ",uid=" << uid << ",gid=" << gid << "]";
}

//----------------------------------------------------------------------------------------------------------------------

std::ostream& operator<<(std::ostream& out, const FamProperty& prop) {
prop.print(out);
return out;
}

//----------------------------------------------------------------------------------------------------------------------

} // namespace eckit
31 changes: 19 additions & 12 deletions src/eckit/io/fam/FamProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <sys/types.h> // mode_t

#include <cstdint> // uint64_t
#include <cstdint> // uint64_t
#include <ostream>
#include <string>

Expand Down Expand Up @@ -57,22 +57,29 @@ struct FamDescriptor {
//----------------------------------------------------------------------------------------------------------------------

struct FamProperty {
fam::size_t size {0};
fam::perm_t perm {0640};
std::string name {""};
FamProperty() = default;

std::uint32_t uid {0};
std::uint32_t gid {0};
FamProperty(fam::size_t size, fam::perm_t perm, std::string name, std::uint32_t uid, std::uint32_t gid);

FamProperty(fam::size_t size, fam::perm_t perm, const std::string& name);

FamProperty(fam::size_t size, fam::perm_t perm);

FamProperty(fam::size_t size, const std::string& perm);

void print(std::ostream& out) const;

auto operator==(const FamProperty& other) const -> bool {
return (size == other.size && perm == other.perm && name == other.name);
return (size == other.size && perm == other.perm && name == other.name && uid == other.uid && gid == other.gid);
}

friend std::ostream& operator<<(std::ostream& os, const FamProperty& prop) {
os << "Property[size=" << prop.size << ", perm=" << prop.perm << ",name=" << prop.name << ",uid=" << prop.uid
<< ",gid=" << prop.gid << "]";
return os;
}
friend std::ostream& operator<<(std::ostream& out, const FamProperty& prop);

fam::size_t size {0};
fam::perm_t perm {0640};
std::string name {""};
std::uint32_t uid {0};
std::uint32_t gid {0};
};

//----------------------------------------------------------------------------------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions src/eckit/io/fam/FamRegionName.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@
#include "eckit/io/fam/detail/FamSessionDetail.h"
#include "eckit/log/Log.h"

#include <iostream>
#include <string>

namespace eckit {

//----------------------------------------------------------------------------------------------------------------------

auto FamRegionName::withRegion(const std::string& regionName) -> FamRegionName& {
path_.regionName = regionName;
path().regionName = regionName;
return *this;
}

auto FamRegionName::object(const std::string& objectName) const -> FamObjectName {
return {endpoint_, {path_.regionName, objectName}};
return {endpoint(), {path().regionName, objectName}};
}

auto FamRegionName::lookup() const -> FamRegion {
return session()->lookupRegion(path_.regionName);
return session()->lookupRegion(path().regionName);
}

auto FamRegionName::create(const fam::size_t regionSize,
const fam::perm_t regionPerm,
const bool overwrite) const -> FamRegion {
if (overwrite) { return session()->ensureCreateRegion(regionSize, regionPerm, path_.regionName); }
return session()->createRegion(regionSize, regionPerm, path_.regionName);
if (overwrite) { return session()->ensureCreateRegion(regionSize, regionPerm, path().regionName); }
return session()->createRegion(regionSize, regionPerm, path().regionName);
}

auto FamRegionName::exists() const -> bool {
Expand All @@ -59,7 +59,7 @@ auto FamRegionName::exists() const -> bool {

auto FamRegionName::uriBelongs(const URI& uri) const -> bool {
/// @todo check if usage requires nothrow
return (uri.endpoint() == endpoint_ && FamPath(uri).regionName == path_.regionName);
return (uri.endpoint() == endpoint() && FamPath(uri).regionName == path().regionName);
}

//----------------------------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/eckit/io/fam/FamRegionName.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#pragma once

#include "eckit/io/fam/FamName.h"
#include "eckit/io/fam/FamObjectName.h"
#include "eckit/io/fam/FamProperty.h"

#include <iosfwd>
#include <string>

namespace eckit {
Expand All @@ -31,7 +31,7 @@ class FamRegion;

//----------------------------------------------------------------------------------------------------------------------

class FamRegionName: public FamName {
class FamRegionName : public FamName {
public: // methods
using FamName::FamName;

Expand Down
11 changes: 4 additions & 7 deletions src/eckit/io/fam/detail/FamSessionDetail.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <fam/fam.h>

#include <iosfwd>
#include <memory>
#include <string>

Expand All @@ -36,7 +37,7 @@ using FamDescriptorStatus = openfam::Fam_Descriptor_Status;
//----------------------------------------------------------------------------------------------------------------------
// SESSION

class FamSessionDetail: public std::enable_shared_from_this<FamSessionDetail> {
class FamSessionDetail : public std::enable_shared_from_this<FamSessionDetail> {
public: // methods
FamSessionDetail(const FamConfig& config);

Expand All @@ -60,9 +61,7 @@ class FamSessionDetail: public std::enable_shared_from_this<FamSessionDetail> {
auto lookupRegion(const std::string& regionName) -> FamRegion;

[[nodiscard]]
auto createRegion(const fam::size_t regionSize,
const fam::perm_t regionPerm,
const std::string& regionName) -> FamRegion;
auto createRegion(fam::size_t regionSize, fam::perm_t regionPerm, const std::string& regionName) -> FamRegion;

[[nodiscard]]
auto createRegion(const FamProperty& property) -> FamRegion {
Expand All @@ -76,9 +75,7 @@ class FamSessionDetail: public std::enable_shared_from_this<FamSessionDetail> {
void destroyRegion(const std::string& regionName);

[[nodiscard]]
auto ensureCreateRegion(const fam::size_t regionSize,
const fam::perm_t regionPerm,
const std::string& regionName) -> FamRegion;
auto ensureCreateRegion(fam::size_t regionSize, fam::perm_t regionPerm, const std::string& regionName) -> FamRegion;

auto stat(FamRegionDescriptor& region) -> FamProperty;

Expand Down
4 changes: 2 additions & 2 deletions tests/io/test_fam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ CASE("FamPath: ctor and uuid generation") {
CASE("FamRegionName: ctor, lookup, and allocate") {
const auto regionName = fam::TestFam::makeRandomText("REGION");

FamRegionName region(fam::testEndpoint, regionName);
const FamRegionName region(fam::testEndpoint, regionName);

EXPECT_EQUAL(region.uri().scheme(), FamPath::scheme);
EXPECT_EQUAL(region.uri().hostport(), fam::testEndpoint);
Expand Down Expand Up @@ -102,7 +102,7 @@ CASE("FamObjectName: ctor, lookup, and allocate") {
// create region
EXPECT_NO_THROW(FamRegionName(fam::testEndpoint, "").withRegion(path.regionName).create(1024, 0640));

FamObjectName object(fam::testEndpoint, path);
const FamObjectName object(fam::testEndpoint, path);

EXPECT_EQUAL(object.uri().scheme(), FamPath::scheme);
EXPECT_EQUAL(object.uri().hostport(), fam::testEndpoint);
Expand Down

0 comments on commit 95413af

Please sign in to comment.