Mahuta Java API

October 31, 2019 ยท View on GitHub

Configuration

Before starting, the client needs to instantiate a Mahuta object:

Mahuta mahuta = new MahutaFactory()
    .configureStorage(IPFSService.connect("localhost", 5001).configureThreadPool(2))
    .configureIndexer(ElasticSearchService.connect("localhost", 9300, "cluster-name").withIndex("article"))
    .defaultImplementation();

Storage configureStorage

Storage represents the storage layer (aka IPFS)

StorageService storage = IPFSService.connect(host, port) or .connect(multiaddress)
    .configureTimeout(timeout) 
    .configureRetry(maxRetry, delay) 
    .addReplica(pinningService)
propertytypeoptionaldefaultdescription
connect(host, port)String, Integerfalselocalhost, 5001connect to the IPFS node via it HTTP address host:port
connect(multiaddress)Stringfalseconnect to the IPFS node via its multiaddress
configureTimeout(timeout)Integertrue5000Configure timeout (in milliseconds)
configureRetry(maxRetry, delay)Integer, Durationtrue3, 0 (sec)Configure retry on error with delay between each retry attempt
addReplica(pinningService)PinningServicetrueAdd Replica service. files are pinned on the connection node (connect)and on each replica service (can be another IPFS node or a IPFS-cluster node

Indexer configureIndexer

Indexer represents the indexing layer (ElasticSearch)

IndexerService indexer = ElasticSearchService.connect(host, port, clusterName)
    .configureIndexNullValue(indexNullValue)
    .withIndex(indexName) or withIndex(indexName, configuration) 
propertytypeoptionaldefaultdescription
connect(host, port, clusterName)String, Integer, Stringfalselocalhost, 9200, nullconnect to ElasticSearch
configureIndexNullValue(indexNullValue)BooleantruefalseConfigure Index null value (null
withIndex(indexName)StringtrueCreate an index during the connection
withIndex(indexName, configuration)String, InputStreamtrueCreate an index during the connection with configuration

Service implementation

Select the service implementation to use:

  • DefaultMahutaService: Default implementation (synchronous functions)
    .defaultImplementation()
  • AsynchonousPinningMahutaService: Implementation with asynchronous pinning
    .asynchronousPinningImplementation(long schedulerPeriod)

Operations

Create Index

Create an index

CreateIndexResponse response = mahuta.prepareCreateIndex(indexName)
    .configuration(configuration)
    .execute();
propertytypeoptionaldefaultdescription
indexNameStringfalseIndex name
configurationInputStreamtrueConfiguration file (on ElasticSearch index mapping JSON)

Get Indexes

Retrieve all indexed from the indexer

GetIndexesResponse response = mahuta.prepareGetIndexes()
    .execute();
propertytypeoptionaldefaultdescription

Index (String)

Index a String document

IndexingResponse response = mahuta.prepareStringIndexing(indexName, content)
    .indexDocId(indexDocId) 
    .contentType(contentType) 
    .indexFields(indexFields) 
    .indexContent(indexContent) 
    .execute();
propertytypeoptionaldefaultdescription
indexName, contentString, StringfalseIndex name and content
indexDocIdStringtrueDocument ID to index the document against (if null, autogenerated ID)
contentTypeStringtrueMimetype (if null, content type guessing function)
indexFieldsMap<String, Object>trueIndex field
indexContentBooleantruefalseStore content in the indexer (caching) - Converted into Base64

Index (CID)

Index a document already on IPFS by its CID

IndexingResponse response = mahuta.prepareCIDndexing(indexName, cid)
    .indexDocId(indexDocId) 
    .contentType(contentType) 
    .indexFields(indexFields) 
    .indexContent(indexContent) 
    .execute();
propertytypeoptionaldefaultdescription
indexName, cidString, StringfalseIndex name and cid
indexDocIdStringtrueDocument ID to index the document against (if null, autogenerated ID)
contentTypeStringtrueMimetype (if null, content type guessing function)
indexFieldsMap<String, Object>trueIndex field
indexContentBooleantruefalseStore content in the indexer (caching) - Converted into Base64

Index (InputStream)

Index any type of content

IndexingResponse response = mahuta.prepareInputStreamIndexing(indexName, content)
    .indexDocId(indexDocId) 
    .contentType(contentType) 
    .indexFields(indexFields) 
    .indexContent(indexContent) 
    .execute();
propertytypeoptionaldefaultdescription
indexName, contentString, InputStreamfalseIndex name and content
indexDocIdStringtrueID to identify the the content in the index (if null, autogenerated ID)
contentTypeStringtrueMimetype (if null, content type guessing function)
indexFieldsMap<String, Object>trueIndex field
indexContentBooleantruefalseStore content in the indexer (caching) - Converted into Base64

Deindexing

Remove a document from the index and unpin

DeindexingResponse response = mahuta.prepareDeindexing(indexName, indexDocIn)
    .execute();
propertytypeoptionaldefaultdescription
indexNameStringfalseIndex name
indexDocIdStringfalseID identifying the file to unindex and unpin

Update field

Update a field in the index without touching the file

UpdateFieldResponse response = mahuta.prepareUpdateField(String indexName, String indexDocId, String key, Object value)
    .execute();
propertytypeoptionaldefaultdescription
indexNameStringfalseIndex name
indexDocIdStringfalseDocument ID identifying the file
keyStringfalseIndex Field name
valueObjectfalseNew value (nullable)

Get

Retrieve a file by its IndexDocId or ContentsID (IPFS hash)

GetResponse response = mahuta.prepareGet()
    .indexName(index)
    .indexDocId(index) or .contentId(index)
    .loadFile(index)
    .execute();
propertytypeoptionaldefaultdescription
indexNameStringfalseIndex name
indexDocIdStringtrueDocument ID identifying the file
contentIdStringtrueContent ID (IPFS hash)
loadFileBooleantruefalseLoad Metadata only (index fields) or Metadata and file

Search content indexed by query

SearchResponse response = mahuta.prepareSearch()
    .indexName(indexName)
    .query(query)
    .loadFile(index)
    .execute();
propertytypeoptionaldefaultdescription
indexNameStringfalseIndex name
queryQueryfalseQuery
loadFileBooleanfalsefalseLoad Metadata only (index fields) or Metadata and file