MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / Open

Method Open

driver/ipfs-log/db.go:42–110  ·  view source on GitHub ↗

Open initializes and opens the database

(path string, cfg *config.Config)

Source from the content-addressed store, hash-verified

40
41// Open initializes and opens the database
42func (s Store) Open(path string, cfg *config.Config) (driver.IDB, error) {
43 // Check if the environment variable for Dbname is set
44 DbnameENV := os.Getenv("IPFS_LOG_DBNAME")
45 if DbnameENV != "" {
46 Dbname = DbnameENV
47 }
48
49 // Create the directory if it doesn't exist
50 if err := os.MkdirAll(path, fs.ModePerm); err != nil {
51 return nil, fmt.Errorf("failed to create directory: %w", err)
52 }
53
54 // Initialize the DB struct
55 db := &DB{
56 ctx: context.Background(),
57 path: path,
58 }
59
60 var err error
61 ctx := context.Background()
62 // Create the IPFS node
63 node, api, err := iflog.CreateNode(ctx, db.path)
64 if err != nil {
65 return nil, fmt.Errorf("failed to create IPFS node: %w", err)
66 }
67
68 // Build host multiaddress
69 hostAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", node.PeerHost.ID()))
70 if err != nil {
71 return nil, fmt.Errorf("failed to create multiaddress: %w", err)
72 }
73 for _, a := range node.PeerHost.Addrs() {
74 fmt.Println(a.Encapsulate(hostAddr).String())
75 }
76 fmt.Println("multi node communication identifier:", Dbname)
77
78 // Initialize the logger
79 logger := zap.NewNop()
80
81 // Create the IPFS log
82 ev, err := iflog.NewIpfsLog(ctx, api, Dbname, &iflog.EventOptions{
83 Directory: db.path,
84 Logger: logger,
85 })
86 if err != nil {
87 return nil, fmt.Errorf("failed to create IPFS log: %w", err)
88 }
89
90 // Announce and connect to the IPFS network
91 if err := ev.AnnounceConnect(ctx, node); err != nil {
92 return nil, fmt.Errorf("failed to announce and connect: %w", err)
93 }
94
95 // Initialize the LevelKVDB
96 db.db, err = levelkv.NewLevelKVDB(ctx, ev, logger)
97 if err != nil {
98 return nil, fmt.Errorf("failed to create LevelKVDB: %w", err)
99 }

Callers

nothing calls this directly

Calls 2

StringMethod · 0.65
AnnounceConnectMethod · 0.45

Tested by

no test coverage detected