001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.database; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import org.syncany.database.ChunkEntry.ChunkChecksum; 024 025/** 026 * The multichunk entry represents the chunk container in which a set of 027 * {@link ChunkEntry}s is stored. On a file, level, a multichunk is represented 028 * by a file (container format) and chunks are added to this file. 029 * 030 * <p>A multichunk is identified by a unique identifier (random, not a checksum), 031 * and contains references to {@link ChunkEntry}s. 032 * 033 * @author Philipp C. Heckel (philipp.heckel@gmail.com) 034 */ 035public class MultiChunkEntry { 036 private static final byte MULTICHUNK_ID_LENGTH = 20; 037 038 private MultiChunkId id; 039 private long size; 040 private List<ChunkChecksum> chunks; 041 042 public MultiChunkEntry(MultiChunkId id, long size) { 043 this.id = id; 044 this.size = size; 045 this.chunks = new ArrayList<ChunkChecksum>(); 046 } 047 048 public void addChunk(ChunkChecksum chunk) { 049 chunks.add(chunk); 050 } 051 052 public MultiChunkId getId() { 053 return id; 054 } 055 056 public void setId(MultiChunkId id) { 057 this.id = id; 058 } 059 060 public List<ChunkChecksum> getChunks() { 061 return chunks; 062 } 063 064 public long getSize() { 065 return size; 066 } 067 068 public void setSize(long size) { 069 this.size = size; 070 } 071 072 @Override 073 public int hashCode() { 074 final int prime = 31; 075 int result = 1; 076 result = prime * result + ((chunks == null) ? 0 : chunks.hashCode()); 077 result = prime * result + ((id == null) ? 0 : id.hashCode()); 078 return result; 079 } 080 081 @Override 082 public boolean equals(Object obj) { 083 if (this == obj) { 084 return true; 085 } 086 if (obj == null) { 087 return false; 088 } 089 if (!(obj instanceof MultiChunkEntry)) { 090 return false; 091 } 092 MultiChunkEntry other = (MultiChunkEntry) obj; 093 if (chunks == null) { 094 if (other.chunks != null) { 095 return false; 096 } 097 } 098 else if (!chunks.equals(other.chunks)) { 099 return false; 100 } 101 if (id == null) { 102 if (other.id != null) { 103 return false; 104 } 105 } 106 else if (!id.equals(other.id)) { 107 return false; 108 } 109 return true; 110 } 111 112 @Override 113 public String toString() { 114 return "MultiChunkEntry [id=" + id + ", chunks=" + chunks + "]"; 115 } 116 117 public static class MultiChunkId extends ObjectId { 118 public MultiChunkId(byte[] array) { 119 super(array); 120 } 121 122 public static MultiChunkId secureRandomMultiChunkId() { 123 return new MultiChunkId(ObjectId.secureRandomBytes(MULTICHUNK_ID_LENGTH)); 124 } 125 126 public static MultiChunkId parseMultiChunkId(String s) { 127 return new MultiChunkId(ObjectId.parseObjectId(s)); 128 } 129 } 130 131}