added base changes for the merge cirectory api

This commit is contained in:
2023-03-15 00:30:44 +00:00
parent b34b2e2b7c
commit 712f7cee56
4 changed files with 139 additions and 88 deletions

View File

@@ -0,0 +1,24 @@
package search
import (
"github.com/PeernetOfficial/core/protocol"
"github.com/google/uuid"
)
// SearchNodeIDBasedOnHash Provides a list of NodeIDs
// based on the hash provided
// This is used to find out which nodes are hosting
// which files based on the hash provided
func (index *SearchIndexStore) SearchNodeIDBasedOnHash(hash []byte) (NodeIDs [][]byte, err error) {
var resultMap map[uuid.UUID]*SearchIndexRecord
err = index.LookupHash(SearchSelector{Hash: hash}, resultMap)
if err != nil {
return
}
for i := range resultMap {
NodeIDs = append(NodeIDs, protocol.PublicKey2NodeID(resultMap[i].PublicKey))
}
return
}

View File

@@ -81,6 +81,7 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi
api.Router.HandleFunc("/blockchain/file/delete", api.apiBlockchainFileDelete).Methods("POST")
api.Router.HandleFunc("/blockchain/file/update", api.apiBlockchainFileUpdate).Methods("POST")
api.Router.HandleFunc("/blockchain/view", api.apiExploreNodeID).Methods("GET")
api.Router.HandleFunc("/merge/directory", api.apiMergeDirectory).Methods("GET")
api.Router.HandleFunc("/profile/list", api.apiProfileList).Methods("GET")
api.Router.HandleFunc("/profile/read", api.apiProfileRead).Methods("GET")
api.Router.HandleFunc("/profile/write", api.apiProfileWrite).Methods("POST")

View File

@@ -7,7 +7,6 @@ Author: Peter Kleissner
package webapi
import (
"bytes"
"encoding/hex"
"net/http"
"strconv"
@@ -148,38 +147,3 @@ func (api *WebapiInstance) apiExploreNodeID(w http.ResponseWriter, r *http.Reque
EncodeJSON(api.Backend, w, r, result)
}
/*
apiExploreNodeID returns the shared files of a particular node in Peernet. Results are returned in real-time. The file type is an optional filter. See TypeX.
Special type -2 = Binary, Compressed, Container, Executable. This special type includes everything except Documents, Video, Audio, Ebooks, Picture, Text.
Request: GET /blockchain/view?limit=[max records]&type=[file type]&offset=[offset]&node=[node id]
Result: 200 with JSON structure SearchResult. Check the field status.
*/
func (api *WebapiInstance) apiExploreNodeID(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
offset, _ := strconv.Atoi(r.Form.Get("offset"))
limit, err := strconv.Atoi(r.Form.Get("limit"))
if err != nil {
limit = 100
}
// ID fields for results for a specific node ID.
NodeId, _ := DecodeBlake3Hash(r.Form.Get("node"))
fileType, err := strconv.Atoi(r.Form.Get("type"))
if err != nil {
fileType = -1
}
// check if the node IDs are the same
if bytes.Compare(NodeId, api.Backend.SelfNodeID()) == 0 {
// setting the value of block to 0
r.Form.Add("block", "0")
api.apiBlockchainRead(w, r)
return
}
result := api.ExploreHelper(fileType, limit, offset, NodeId, true)
EncodeJSON(api.Backend, w, r, result)
}

62
webapi/Merge directory.go Normal file
View File

@@ -0,0 +1,62 @@
package webapi
import (
"net/http"
"strconv"
)
// SearchResultMergedDirectory contains results for the merged directory.
type SearchResultMergedDirectory struct {
Status int `json:"status"` // Status: 0 = Success with results, 1 = No more results available, 2 = Search ID not found, 3 = No results yet available keep trying
Files [][]apiFile `json:"files"` // List of files found
Statistic interface{} `json:"statistic"` // Statistics of all results (independent from applied filters), if requested. Only set if files are returned (= if statistics changed). See SearchStatisticData.
}
func (api *WebapiInstance) apiMergeDirectory(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
offset, _ := strconv.Atoi(r.Form.Get("offset"))
limit, err := strconv.Atoi(r.Form.Get("limit"))
if err != nil {
limit = 100
}
// ID fields for results for a specific node ID.
NodeId, _ := DecodeBlake3Hash(r.Form.Get("node"))
hash, _ := DecodeBlake3Hash(r.Form.Get("hash"))
fileType, err := strconv.Atoi(r.Form.Get("type"))
if err != nil {
fileType = -1
}
result := api.ExploreFileSharedByNodeThatSharedSimilarFile(fileType, limit, offset, NodeId, hash, true)
EncodeJSON(api.Backend, w, r, result)
}
// ExploreFileSharedByNodeThatSharedSimilarFile lists files shared by a nodes which share the same file as a common point
// Currently this is a greedy search and requires work for optimization
func (api *WebapiInstance) ExploreFileSharedByNodeThatSharedSimilarFile(fileType int, limit, offset int, nodeID []byte, hash []byte, nodeIDState bool) *SearchResultMergedDirectory {
// lookup all NodeID blockchains which have the similar hash
// do a search to get all the node IDs sharing the particular file
NodeIDs, _ := api.Backend.SearchIndex.SearchNodeIDBasedOnHash(nodeID)
var result SearchResultMergedDirectory
for i := range NodeIDs {
resultFiles := api.queryRecentShared(api.Backend, fileType, uint64(limit*20/100), uint64(offset), uint64(limit), NodeIDs[i], nodeIDState)
var ApiFile []apiFile
// loop over results
for n := range resultFiles {
ApiFile = append(ApiFile, blockRecordFileToAPI(resultFiles[n]))
}
result.Files = append(result.Files, ApiFile)
}
result.Status = 1 // No more results to expect
return &result
}