public IndexOutput createOutput(String name, IOContext context, boolean raw) throws IOException { Directory directory; if (isChecksum(name)) { directory = distributor.primary(); } else { directory = distributor.any(); } IndexOutput out = directory.createOutput(name, context); synchronized (mutex) { StoreFileMetaData metaData = new StoreFileMetaData(name, -1, null, directory); filesMetadata = MapBuilder.newMapBuilder(filesMetadata).put(name, metaData).immutableMap(); files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]); boolean computeChecksum = !raw; if (computeChecksum) { // don't compute checksum for segment based files if ("segments.gen".equals(name) || name.startsWith("segments")) { computeChecksum = false; } } if (computeChecksum) { out = new BufferedChecksumIndexOutput(out, new Adler32()); } return new StoreIndexOutput(metaData, out, name); } }
/** * Creates a new Directory from the given distributor. The default implementation returns a new * {@link org.elasticsearch.index.store.DistributorDirectory} if there is more than one data path * in the distributor. */ public Directory newFromDistributor(final Distributor distributor) throws IOException { if (distributor.all().length == 1) { // use filter dir for consistent toString methods return new FilterDirectory(distributor.primary()) { @Override public String toString() { return distributor.toString(); } }; } return new DistributorDirectory(distributor); }
StoreDirectory(Distributor distributor) throws IOException { this.distributor = distributor; synchronized (mutex) { MapBuilder<String, StoreFileMetaData> builder = MapBuilder.newMapBuilder(); Map<String, String> checksums = readChecksums(distributor.all(), new HashMap<String, String>()); for (Directory delegate : distributor.all()) { for (String file : delegate.listAll()) { String checksum = checksums.get(file); builder.put( file, new StoreFileMetaData(file, delegate.fileLength(file), checksum, delegate)); } } filesMetadata = builder.immutableMap(); files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]); } }
@Override public void close() throws IOException { for (Directory delegate : distributor.all()) { delegate.close(); } synchronized (mutex) { filesMetadata = ImmutableMap.of(); files = Strings.EMPTY_ARRAY; } }
synchronized void closeInternal() throws IOException { if (isOpen) { isOpen = false; for (Directory delegate : distributor.all()) { delegate.close(); } synchronized (mutex) { filesMetadata = ImmutableOpenMap.of(); files = Strings.EMPTY_ARRAY; } } }
public IndexOutput createOutput(String name, IOContext context, boolean raw) throws IOException { ensureOpen(); Directory directory; // we want to write the segments gen file to the same directory *all* the time // to make sure we don't create multiple copies of it if (isChecksum(name) || IndexFileNames.SEGMENTS_GEN.equals(name)) { directory = distributor.primary(); } else { directory = distributor.any(); } IndexOutput out = directory.createOutput(name, context); boolean success = false; try { synchronized (mutex) { StoreFileMetaData metaData = new StoreFileMetaData(name, -1, null, directory); filesMetadata = ImmutableOpenMap.builder(filesMetadata).fPut(name, metaData).build(); files = filesMetadata.keys().toArray(String.class); boolean computeChecksum = !raw; if (computeChecksum) { // don't compute checksum for segment based files if (IndexFileNames.SEGMENTS_GEN.equals(name) || name.startsWith(IndexFileNames.SEGMENTS)) { computeChecksum = false; } } if (computeChecksum) { out = new BufferedChecksumIndexOutput(out, new Adler32()); } final StoreIndexOutput storeIndexOutput = new StoreIndexOutput(metaData, out, name); success = true; return storeIndexOutput; } } finally { if (!success) { IOUtils.closeWhileHandlingException(out); } } }
@Override public String getLockID() { return distributor.primary().getLockID(); }
@Override public LockFactory getLockFactory() { return distributor.primary().getLockFactory(); }
@Override public void setLockFactory(LockFactory lockFactory) throws IOException { distributor.primary().setLockFactory(lockFactory); }
@Override public void clearLock(String name) throws IOException { distributor.primary().clearLock(name); }
@Override public Lock makeLock(String name) { return distributor.primary().makeLock(name); }
public Directory[] delegates() { return distributor.all(); }
@Override public String toString() { return "store(" + distributor.toString() + ")"; }