needle_write_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package needle
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  7. )
  8. func TestAppend(t *testing.T) {
  9. n := &Needle{
  10. Cookie: types.Cookie(123), // Cookie Cookie `comment:"random number to mitigate brute force lookups"`
  11. Id: types.NeedleId(123), // Id NeedleId `comment:"needle id"`
  12. Size: 8, // Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  13. DataSize: 4, // DataSize uint32 `comment:"Data size"` //version2
  14. Data: []byte("abcd"), // Data []byte `comment:"The actual file data"`
  15. Flags: 0, // Flags byte `comment:"boolean flags"` //version2
  16. NameSize: 0, // NameSize uint8 //version2
  17. Name: nil, // Name []byte `comment:"maximum 256 characters"` //version2
  18. MimeSize: 0, // MimeSize uint8 //version2
  19. Mime: nil, // Mime []byte `comment:"maximum 256 characters"` //version2
  20. PairsSize: 0, // PairsSize uint16 //version2
  21. Pairs: nil, // Pairs []byte `comment:"additional name value pairs, json format, maximum 6
  22. LastModified: 123, // LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes
  23. Ttl: nil, // Ttl *TTL
  24. Checksum: 123, // Checksum CRC `comment:"CRC32 to check integrity"`
  25. AppendAtNs: 123, // AppendAtNs uint64 `comment:"append timestamp in nano seconds"` //version3
  26. Padding: nil, // Padding []byte `comment:"Aligned to 8 bytes"`
  27. }
  28. tempFile, err := os.CreateTemp("", ".dat")
  29. if err != nil {
  30. t.Errorf("Fail TempFile. %v", err)
  31. return
  32. }
  33. /*
  34. uint8 : 0 to 255
  35. uint16 : 0 to 65535
  36. uint32 : 0 to 4294967295
  37. uint64 : 0 to 18446744073709551615
  38. int8 : -128 to 127
  39. int16 : -32768 to 32767
  40. int32 : -2147483648 to 2147483647
  41. int64 : -9223372036854775808 to 9223372036854775807
  42. */
  43. fileSize := int64(4294967296) + 10000
  44. tempFile.Truncate(fileSize)
  45. defer func() {
  46. tempFile.Close()
  47. os.Remove(tempFile.Name())
  48. }()
  49. datBackend := backend.NewDiskFile(tempFile)
  50. defer datBackend.Close()
  51. offset, _, _, _ := n.Append(datBackend, CurrentVersion)
  52. if offset != uint64(fileSize) {
  53. t.Errorf("Fail to Append Needle.")
  54. }
  55. }