DataNode.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.zookeeper.server;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import org.apache.jute.Index;
  24. import org.apache.jute.InputArchive;
  25. import org.apache.jute.OutputArchive;
  26. import org.apache.jute.Record;
  27. import org.apache.zookeeper.data.ACL;
  28. import org.apache.zookeeper.data.Stat;
  29. /**
  30. * This class contains the data for a node in the data tree.
  31. * <p>
  32. * A data node contains a reference to its parent, a byte array as its data, an
  33. * array of ACLs, a stat object, and a set of its children's paths.
  34. *
  35. */
  36. public class DataNode implements Record {
  37. DataNode() {
  38. }
  39. DataNode(DataNode parent, byte data[], List<ACL> acl, Stat stat) {
  40. this.parent = parent;
  41. this.data = data;
  42. this.acl = acl;
  43. this.stat = stat;
  44. this.children = new HashSet<String>();
  45. }
  46. DataNode parent;
  47. byte data[];
  48. List<ACL> acl;
  49. public Stat stat;
  50. HashSet<String> children = new HashSet<String>();
  51. public void deserialize(InputArchive archive, String tag)
  52. throws IOException {
  53. archive.startRecord("node");
  54. data = archive.readBuffer("data");
  55. Index i = archive.startVector("acl");
  56. if (i != null) {
  57. acl = new ArrayList<ACL>();
  58. while (!i.done()) {
  59. ACL a = new ACL();
  60. a.deserialize(archive, "aclEntry");
  61. acl.add(a);
  62. i.incr();
  63. }
  64. }
  65. archive.endVector("acl");
  66. stat = new Stat();
  67. stat.deserialize(archive, "stat");
  68. archive.endRecord("node");
  69. }
  70. synchronized public void serialize(OutputArchive archive, String tag)
  71. throws IOException {
  72. archive.startRecord(this, "node");
  73. archive.writeBuffer(data, "data");
  74. archive.startVector(acl, "acl");
  75. if (acl != null) {
  76. for (ACL a : acl) {
  77. a.serialize(archive, "aclEntry");
  78. }
  79. }
  80. archive.endVector(acl, "acl");
  81. stat.serialize(archive, "stat");
  82. archive.endRecord(this, "node");
  83. }
  84. }