
com.caucho.v5.bartender.files.FileServiceRootImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baratine Show documentation
Show all versions of baratine Show documentation
A reactive Java web server.
/*
* Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
*
* This file is part of Baratine(TM)
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Baratine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Baratine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Baratine; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package com.caucho.v5.bartender.files;
import io.baratine.db.BlobReader;
import io.baratine.db.Cursor;
import io.baratine.db.CursorPrepareSync;
import io.baratine.db.DatabaseWatch;
import io.baratine.files.BfsFile;
import io.baratine.files.BfsFileSync;
import io.baratine.files.Status;
import io.baratine.files.Watch;
import io.baratine.files.WriteOption;
import io.baratine.service.Cancel;
import io.baratine.service.OnInit;
import io.baratine.service.OnLookup;
import io.baratine.service.Result;
import io.baratine.service.ResultFuture;
import io.baratine.service.Service;
import io.baratine.service.Services;
import io.baratine.service.ServiceRef;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.caucho.v5.amp.Direct;
import com.caucho.v5.amp.ServicesAmp;
import com.caucho.v5.amp.ServiceRefAmp;
import com.caucho.v5.bartender.BartenderSystem;
import com.caucho.v5.bartender.ServerBartender;
import com.caucho.v5.bartender.pod.NodePodAmp;
import com.caucho.v5.bartender.pod.PodBartender;
import com.caucho.v5.io.TempOutputStream;
import com.caucho.v5.json.io.JsonReader;
import com.caucho.v5.kelp.TableListener;
import com.caucho.v5.kraken.KrakenSystem;
import com.caucho.v5.kraken.query.QueryKraken;
import com.caucho.v5.kraken.table.TableKraken;
import com.caucho.v5.kraken.table.TableManagerKraken;
import com.caucho.v5.util.BitsUtil;
import com.caucho.v5.util.Crc64;
import com.caucho.v5.util.Fnv128;
import com.caucho.v5.util.HashKey;
import com.caucho.v5.util.Hex;
import com.caucho.v5.util.LruCache;
import com.caucho.v5.util.Murmur32;
import com.caucho.v5.vfs.Crc64OutputStream;
import com.caucho.v5.vfs.ReadStream;
import com.caucho.v5.vfs.VfsOld;
import com.caucho.v5.vfs.WriteStream;
/**
* Root of a filesystem. Each filesystem belongs to a pod and has a
* consistent hashing function and its own kraken table.
*/
public class FileServiceRootImpl
{
private static final Logger log
= Logger.getLogger(FileServiceRootImpl.class.getName());
private static final int KEY_SIZE = 4 + 16 + 2;
private static final int DIR_LENGTH_CODE = -2;
private final BartenderFileSystem _system;
private final String _podName;
private final LruCache _fileEntryMap
= new LruCache<>(16 * 1024);
private final ConcurrentHashMap _bindMap
= new ConcurrentHashMap<>();
private final FileHash _hash;
private ServiceRefAmp _serviceRef;
private TableKraken _fileTable;
private FileServiceRoot _rootService;
private QueryKraken _fileInsert;
private QueryKraken _fileDelete;
private QueryKraken _fileRead;
private QueryKraken _dirInsert;
private QueryKraken _dirList;
private QueryKraken _fileWatch;
private QueryKraken _fileStatus;
private PodBartender _pod;
private ServerBartender _serverSelf;
// private QueryKraken _dirNotify;
private BfsFileImpl _rootNode;
private final String _address;
private final String _prefix;
private String _authority;
private QueryKraken _fileName;
FileServiceRootImpl(FileServiceBuilder builder)
{
_system = builder.getSystem();
_address = builder.getAddress();
_prefix = builder.getPrefix();
_authority = _address.substring(0, _address.length() - _prefix.length());
_podName = builder.getPodName();
_hash = builder.getHash();
BartenderSystem bartenderSystem = BartenderSystem.current();
_pod = bartenderSystem.findPod(_podName);
_serverSelf = bartenderSystem.serverSelf();
_fileTable = createFileTable();
// _dirTable = createDirTable();
Objects.requireNonNull(_fileTable);
// Objects.requireNonNull(_dirTable);
createFileQuery();
// createDirQuery();
ServicesAmp manager = builder.getManager();
String address = builder.getAddress();
_serviceRef = manager.newService(this).address(address).ref();
_rootService = _serviceRef.as(FileServiceRoot.class);
_rootNode = new BfsFileImpl(this, "", "");
}
FileServiceRootImpl(String address,
String podName,
ServicesAmp manager)
{
this(new FileServiceBuilder().address(address).pod(podName));
}
public String getPodName()
{
if (_podName != null) {
return _podName;
}
else {
return "";
}
}
public String getAddress()
{
return _address;
}
public PodBartender getPod()
{
return _pod;
}
public boolean isPrimary(int hash)
{
return _pod.getNode(hash).isServerPrimary(_serverSelf);
}
public boolean isOwner(int hash)
{
return _pod.getNode(hash).isServerOwner(_serverSelf);
}
private boolean isOwner(String path)
{
return isOwner(hash(path));
}
@OnLookup
public Object onLookup(String path)
{
String fullPath = _prefix + path;
// check for a static binding first (e.g. /proc)
BfsFile file = lookupBind(fullPath);
if (file != null) {
return file;
}
// binding not found, look in database
//return lookupImpl(path).getService();
//return lookupImpl(path);
return new BfsFileImpl(this, fullPath, path);
}
@OnInit
public void onActive()
{
FileChangeService listener = new FileChangeService();
Services manager = Services.current();
_fileTable.addListener(manager.newService(listener).as(TableListener.class));
// _dirTable.addListener(new DirChangeListener());
}
FileServiceRoot getService()
{
return _rootService;
}
//
// cache entry methods
//
public FileStatusImpl getStatusEntry(String path)
{
FileEntry entry = getOwnerEntry(path);
if (entry != null) {
return entry.getStatus();
}
else {
return null;
}
}
public void setStatusEntry(String path, FileStatusImpl status)
{
FileEntry entry = getOwnerEntry(path);
if (entry != null) {
entry.setStatus(status);
}
}
FileEntry getOwnerEntry(String path)
{
if (isOwner(path)) {
return createEntry(path);
}
else {
return null;
}
}
private FileEntry createEntry(String path)
{
HashKey key = HashKey.create(getFileKey(path));
FileEntry entry = _fileEntryMap.get(key);
if (entry == null) {
entry = new FileEntry(this, path, key);
_fileEntryMap.putIfNew(key, entry);
entry = _fileEntryMap.get(key);
}
return entry;
}
private FileEntry getEntry(String path)
{
HashKey key = HashKey.create(getFileKey(path));
return _fileEntryMap.get(key);
}
public void openReadFile(String path,
Result result)
{
BfsFile fileBind = lookupBind(path);
if (fileBind != null) {
fileBind.openRead(result);
return;
}
int parentHash = getParentHash(path);
byte []key = getPathKey(path);
int hash = hash(path);
FileEntry entry = getOwnerEntry(path);
if (entry == null) {
_fileRead.findOne(result.of(cursor->openReadStream(cursor)),
parentHash, key, hash);
}
else if (entry.isReadClean()) {
_fileRead.findOneDirect(result.of(cursor->openReadStream(cursor)),
parentHash, key, hash);
}
else {
long writeCount = entry.getWriteCount();
_fileRead.findOne(result.of(cursor->openReadStream(cursor, entry, writeCount)),
parentHash, key, hash);
}
}
private void openReadCursor(String path,
Result result)
{
/*
BfsFile fileBind = lookupBind(path);
if (fileBind != null) {
fileBind.openRead(result);
return;
}
*/
int parentHash = getParentHash(path);
byte []key = getPathKey(path);
int hash = hash(path);
FileEntry entry = getOwnerEntry(path);
if (entry == null) {
_fileRead.findOne(result, parentHash, key, hash);
}
else if (entry.isReadClean()) {
_fileRead.findOneDirect(result, parentHash, key, hash);
}
else {
long writeCount = entry.getWriteCount();
_fileRead.findOne(result.of(cursor->openReadCursor(cursor, entry, writeCount)),
parentHash, key, hash);
}
}
private InputStream openReadStream(Cursor cursor)
{
if (cursor != null) {
InputStream is = cursor.getInputStream(1);
return is;
}
else {
return null;
}
}
private InputStream openReadStream(Cursor cursor,
FileEntry entry,
long writeCount)
{
entry.setReadCount(writeCount);
if (cursor != null) {
return cursor.getInputStream(1);
}
else {
return null;
}
}
public void openReadBlob(String path,
Result result)
{
BfsFile fileBind = lookupBind(path);
if (fileBind != null) {
fileBind.openReadBlob(result);
return;
}
int parentHash = getParentHash(path);
byte []key = getPathKey(path);
int hash = hash(path);
FileEntry entry = getOwnerEntry(path);
if (entry == null) {
_fileRead.findOne(result.of(cursor->openReadBlobStream(cursor, entry)),
parentHash, key, hash);
}
else if (entry.isReadClean()) {
_fileRead.findOneDirect(result.of(cursor->openReadBlobStream(cursor, entry)),
parentHash, key, hash);
}
else {
long writeCount = entry.getWriteCount();
_fileRead.findOne(result.of(cursor->openReadBlobStream(cursor, entry, writeCount)),
parentHash, key, hash);
}
}
private BlobReader openReadBlobStream(Cursor cursor, FileEntry entry)
{
if (cursor == null) {
return null;
}
BlobReader dbBlobReader = cursor.getBlobReader(1);
if (dbBlobReader == null) {
return null;
}
return new BlobReaderFile(dbBlobReader, entry);
}
private BlobReader openReadBlobStream(Cursor cursor,
FileEntry entry,
long writeCount)
{
entry.setReadCount(writeCount);
if (cursor != null) {
return cursor.getBlobReader(1);
}
else {
return null;
}
}
private Cursor openReadCursor(Cursor cursor,
FileEntry entry,
long writeCount)
{
entry.setReadCount(writeCount);
return cursor;
}
@Direct
public void copyTo(String srcPath,
String dstPath,
Result result,
WriteOption[] options)
{
boolean isWaitForPut = isWaitForPut(options);
openReadCursor(srcPath,
result.of((c,r)->copyAfterRead(c, dstPath, r, isWaitForPut)));
}
@Direct
public void renameTo(String srcPath,
String dstPath,
Result result,
WriteOption[] options)
{
boolean isWaitForPut = isWaitForPut(options);
openReadCursor(srcPath,
result.of((c,r)->renameAfterRead(c, srcPath, dstPath, r, isWaitForPut)));
}
private void renameAfterRead(Cursor cursor,
String srcPath,
String dstPath,
Result result,
boolean isWaitForPut)
{
if (cursor == null) {
result.ok(false);
return;
}
copyAfterRead(cursor, dstPath, result.of((x,r)->remove(srcPath, r)), true);
}
private void copyAfterRead(Cursor cursor,
String path,
Result result,
boolean isWaitForPut)
{
if (cursor == null) {
result.ok(false);
return;
}
Result putResult;
if (isWaitForPut) {
putResult = result;
}
else {
putResult = Result.ignore();
}
_fileInsert.exec(putResult.of(v->afterWriteClose(path)),
getParentHash(path), getPathKey(path), hash(path),
getParent(path), getName(path),
cursor.getInputStream(1),
cursor.getLong(2),
cursor.getLong(3));
ServiceRef.flushOutbox();
if (! isWaitForPut) {
result.ok(true);
}
}
private boolean isEphemeral(WriteOption ...options)
{
if (options == null) {
return false;
}
for (WriteOption opt : options) {
if (opt == WriteOption.Standard.EPHEMERAL) {
return true;
}
}
return false;
}
public void remove(String path, Result result)
{
_fileDelete.exec(result.of((x,r)->deleteResult(path, r)),
getParentHash(path),
getPathKey(path),
hash(path));
//fileTableKelp.remove(cursor, backup, result);
/*
FileServiceImpl file = _fileMap.get(path);
if (file != null) {
file.onChange();
}
*/
}
public void removeAll(String path, Result result)
{
remove(path, result);
/*
_fileDelete.exec(x->deleteResult(path, result),
path);
*/
//result.complete(false);
}
private void deleteResult(String path, Result result)
{
notifyChange(path);
result.ok(Boolean.TRUE);
}
public void addDirectory(String path,
Result
© 2015 - 2025 Weber Informatics LLC | Privacy Policy