Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setTimes support #116

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions python/python/hdfs_native/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ def delete(self, path: str, recursive: bool) -> bool:
is a non-empty directory, this will fail.
"""
return self.inner.delete(path, recursive)

def set_times(self, path: str, mtime: int, atime: int) -> None:
"""
Changes the modification time and access time of the file at `path` to `mtime` and `atime`, respectively.
"""
return self.inner.set_times(path, mtime, atime)
4 changes: 3 additions & 1 deletion python/python/hdfs_native/_internal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ class RawClient:

def rename(self, src: str, dst: str, overwrite: bool) -> None: ...

def delete(self, path: str, recursive: bool) -> bool: ...
def delete(self, path: str, recursive: bool) -> bool: ...

def set_times(self, path: str, mtime: int, atime: int) -> None: ...
4 changes: 4 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ impl RawClient {
pub fn delete(&self, path: &str, recursive: bool) -> PyHdfsResult<bool> {
Ok(self.rt.block_on(self.inner.delete(path, recursive))?)
}

pub fn set_times(&self, path: &str, mtime: u64, atime: u64) -> PyHdfsResult<()> {
Ok(self.rt.block_on(self.inner.set_times(path, mtime, atime))?)
}
}

/// A Python module implemented in Rust.
Expand Down
11 changes: 10 additions & 1 deletion python/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ def test_integration(minidfs: str):
for i in range(0, 33 * 1024 * 1024):
assert data.read(4) == i.to_bytes(4, 'big')

client.delete("/testfile", False)

mtime = 1717641455
atime = 1717641456
client.set_times("/testfile", mtime, atime)
file_info = client.get_file_info("/testfile")
assert file_info.modification_time == mtime
assert file_info.access_time == atime

client.delete("/testfile", False)

1 change: 1 addition & 0 deletions rust/minidfs/src/main/java/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public static MiniDFSNNTopology generateTopology(Set<String> flags, Configuratio
conf.set(DFSConfigKeys.DFS_NAMENODE_STATE_CONTEXT_ENABLED_KEY, "true");
conf.set(DFSConfigKeys.DFS_HA_TAILEDITS_INPROGRESS_KEY, "true");
conf.set(DFSConfigKeys.DFS_HA_TAILEDITS_PERIOD_KEY, "0ms");
conf.set(DFSConfigKeys.DFS_NAMENODE_ACCESSTIME_PRECISION_KEY, "0");
}
return nnTopology;
}
Expand Down
9 changes: 9 additions & 0 deletions rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,15 @@ impl Client {
.await
.map(|r| r.result)
}

/// Sets the modified and access times for a file. Times should be in milliseconds from the epoch.
pub async fn set_times(&self, path: &str, mtime: u64, atime: u64) -> Result<()> {
let (link, resolved_path) = self.mount_table.resolve(path);
link.protocol
.set_times(&resolved_path, mtime, atime)
.await?;
Ok(())
}
}

impl Default for Client {
Expand Down
23 changes: 23 additions & 0 deletions rust/src/hdfs/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,29 @@ impl NamenodeProtocol {
debug!("renewLease response: {:?}", &decoded);
Ok(decoded)
}

pub(crate) async fn set_times(
&self,
src: &str,
mtime: u64,
atime: u64,
) -> Result<hdfs::SetTimesResponseProto> {
let message = hdfs::SetTimesRequestProto {
src: src.to_string(),
mtime,
atime,
};
debug!("setTimes request: {:?}", &message);

let response = self
.proxy
.call("setTimes", message.encode_length_delimited_to_vec())
.await?;

let decoded = hdfs::SetTimesResponseProto::decode_length_delimited(response)?;
debug!("setTimes response: {:?}", &decoded);
Ok(decoded)
}
}

impl Drop for NamenodeProtocol {
Expand Down
21 changes: 21 additions & 0 deletions rust/tests/test_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ mod test {
test_read_write(&client).await?;
// We use writing to create files, so do this after
test_recursive_listing(&client).await?;
test_set_times(&client).await?;

Ok(())
}
Expand Down Expand Up @@ -329,4 +330,24 @@ mod test {

Ok(())
}

async fn test_set_times(client: &Client) -> Result<()> {
client
.create("/test", WriteOptions::default())
.await?
.close()
.await?;

let mtime = 1717641455;
let atime = 1717641456;

client.set_times("/test", mtime, atime).await?;

let file_info = client.get_file_info("/test").await?;

assert_eq!(file_info.modification_time, mtime);
assert_eq!(file_info.access_time, atime);

Ok(())
}
}
Loading