001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.needle.gigaspaces.store;
018:
019: import java.io.Externalizable;
020: import java.io.IOException;
021: import java.io.ObjectInput;
022: import java.io.ObjectOutput;
023:
024: import net.jini.core.entry.Entry;
025:
026: /**
027: * A file bucket entry holds a bucket (part) of the file content.
028: *
029: * @author kimchy
030: */
031: public class FileBucketEntry implements Entry, Externalizable {
032:
033: public String indexName;
034:
035: public String fileName;
036:
037: public Long bucketIndex;
038:
039: public byte[] data;
040:
041: public FileBucketEntry() {
042:
043: }
044:
045: public FileBucketEntry(String indexName, String fileName) {
046: this .indexName = indexName;
047: this .fileName = fileName;
048: }
049:
050: public FileBucketEntry(String indexName, String fileName,
051: long bucketIndex, byte[] data) {
052: this .indexName = indexName;
053: this .fileName = fileName;
054: this .bucketIndex = bucketIndex;
055: this .data = data;
056: }
057:
058: public String getIndexName() {
059: return this .indexName;
060: }
061:
062: public String getFileName() {
063: return this .fileName;
064: }
065:
066: public byte[] getData() {
067: return this .data;
068: }
069:
070: public void writeExternal(ObjectOutput out) throws IOException {
071: out.writeUTF(indexName);
072: if (fileName == null) {
073: out.writeBoolean(false);
074: } else {
075: out.writeBoolean(true);
076: out.writeUTF(fileName);
077: }
078: if (bucketIndex == null) {
079: out.writeBoolean(false);
080: } else {
081: out.writeBoolean(true);
082: out.writeLong(bucketIndex);
083: }
084: if (data == null) {
085: out.writeInt(0);
086: } else {
087: out.writeInt(data.length);
088: out.write(data);
089: }
090: }
091:
092: public void readExternal(ObjectInput in) throws IOException,
093: ClassNotFoundException {
094: indexName = in.readUTF();
095: if (in.readBoolean()) {
096: fileName = in.readUTF();
097: }
098: if (in.readBoolean()) {
099: bucketIndex = in.readLong();
100: }
101: int size = in.readInt();
102: if (size > 0) {
103: data = new byte[size];
104: int index = 0;
105: while (true) {
106: int bytesRead = in.read(data, index, size);
107: if (bytesRead == size) {
108: break;
109: }
110: index += bytesRead;
111: size -= bytesRead;
112: }
113: }
114: }
115:
116: public static String[] __getSpaceIndexedFields() {
117: // bucketIndex is our routing index
118: // TODO: we might want to have fileName + bucketIndex as the routing index
119: return new String[] { "bucketIndex", "fileName" };
120: }
121: }
|