-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d961456
commit 48afdbc
Showing
7 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bats | ||
|
||
load helper | ||
|
||
setup() { | ||
$HDFS mkdir -p /_test_cmd/truncate | ||
$HDFS touch /_test_cmd/truncate/a | ||
} | ||
|
||
@test "truncate larger" { | ||
skip "Not support until Hadoop-2.7.2" | ||
run $HDFS truncate -s 10 /_test_cmd/a | ||
assert_failure | ||
} | ||
|
||
@test "truncate nonexistent" { | ||
skip "Not support until Hadoop-2.7.2" | ||
run $HDSF truncate -s 10 /_test_cmd/nonexistent | ||
assert_failure | ||
} | ||
|
||
@test "truncate" { | ||
skip "Not support until Hadoop-2.7.2" | ||
run $HDFS put $ROOT_TEST_DIR/test/foo.txt /_test_cmd/truncate/1 | ||
run $HDFS truncate -s 2 /_test_cmd/truncate/1 | ||
assert_success | ||
} | ||
|
||
teardown() { | ||
$HDFS rm -r /_test_cmd/truncate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
func truncate(name string, newLength uint64) { | ||
client, err := getClient("") | ||
if err != nil { | ||
fatal(err) | ||
} | ||
|
||
err = client.Truncate(name, newLength) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hdfs | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
hdfs "github.com/colinmarc/hdfs/protocol/hadoop_hdfs" | ||
"github.com/colinmarc/hdfs/rpc" | ||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
func (c *Client) Truncate(name string, newLength uint64) error { | ||
req := &hdfs.TruncateRequestProto { | ||
Src: proto.String(name), | ||
NewLength: proto.Uint64(newLength), | ||
ClientName: proto.String(c.namenode.ClientName()), | ||
} | ||
resp := &hdfs.TruncateResponseProto{} | ||
|
||
err := c.namenode.Execute("truncate", req, resp) | ||
if err != nil { | ||
if nnErr, ok := err.(*rpc.NamenodeError); ok { | ||
err = interpretException(nnErr.Exception, err) | ||
} | ||
|
||
return &os.PathError{"truncate", name, err} | ||
} else if resp.Result == nil { | ||
return &os.PathError{ | ||
"truncate", | ||
name, | ||
errors.New("Unexpected empty response to 'truncate' rpc call"), | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package hdfs | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTruncate(t *testing.T) { | ||
t.Skip("Truncate not support in Hadoop-2.6") | ||
client := getClient(t) | ||
|
||
baleet(t, "/_test/truncate/1.txt") | ||
mkdirp(t, "/_test/truncate") | ||
writer, _ := client.Create("/_test/truncate/1.txt") | ||
_, _ = writer.Write([]byte("foobar\nfoobar\n")) | ||
_ = writer.Close() | ||
|
||
err := client.Truncate("/_test/truncate/1.txt", 4) | ||
require.NoError(t, err) | ||
|
||
stat, err := client.Stat("/_test/truncate/1.txt") | ||
require.NoError(t, err) | ||
assert.EqualValues(t, 4, stat.Size()) | ||
|
||
err = client.Truncate("/_test/truncate/1.txt", 10) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestTruncateNoExistent(t *testing.T) { | ||
if os.Getenv("HADOOP_DISTRO") == "cdh" { | ||
t.Skip("Truncate not support in Hadoop-2.6") | ||
} | ||
client := getClient(t) | ||
|
||
err := client.Truncate("/_test/nonexistent", 100) | ||
assertPathError(t, err, "truncate", "/_test/nonexistent", os.ErrNotExist) | ||
} |