Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump #4

Open
TanmoySG opened this issue Aug 31, 2022 · 2 comments
Open

Dump #4

TanmoySG opened this issue Aug 31, 2022 · 2 comments

Comments

@TanmoySG
Copy link
Owner

Put everything here, that wont go in

@TanmoySG
Copy link
Owner Author

package main

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "os"
  "path/filepath"
)

type Document struct {
  // The fields and values of the document
  Fields map[string]interface{}
}

func (d *Document) Save() error {
  // Convert the document to JSON
  jsonBytes, err := json.Marshal(d.Fields)
  if err != nil {
    return err
  }

  // Save the JSON to a file
  err = ioutil.WriteFile(d.Fields["id"].(string)+".json", jsonBytes, 0644)
  if err != nil {
    return err
  }

  return nil
}

func (d *Document) Load(id string) error {
  // Read the JSON file
  jsonBytes, err := ioutil.ReadFile(id + ".json")
  if err != nil {
    return err
  }

  // Unmarshal the JSON into the document
  err = json.Unmarshal(jsonBytes, &d.Fields)
  if err != nil {
    return err
  }

  return nil
}

func (d *Document) Delete() error {
  // Delete the JSON file
  err := os.Remove(d.Fields["id"].(string) + ".json")
  if err != nil {
    return err
  }

  return nil
}

func main() {
  // Create a new document
  doc := Document{
    Fields: map[string]interface{}{
      "id":   "1",
      "name": "John Doe",
      "age":  30,
    },
  }

  // Save the document
  err := doc.Save()
  if err != nil {
    panic(err)
  }

  // Load the document
  err = doc.Load("1")
  if err != nil {
    panic(err)
  }

  // Print the fields of the document
  fmt.Println(doc.Fields)

  // Delete the document
  err = doc.Delete()
  if err != nil {
    panic(err)
  }

  // Check if the document still exists
  _, err = os.Stat(filepath.Join("1.json"))
  if os.IsNotExist(err) {
    fmt.Println("Document has been deleted")
  }
}

From ChatGPT

This program defines a Document struct that represents a document in the database. The struct has methods to save, load, and delete documents from the database. The database itself is simply a collection of JSON files, one for each document, stored in the local file system.

Please note that this is a very simple example and does not include all of the features and functionality of a full-fledged DBMS. It is intended only as a demonstration of how to create a document-oriented DBMS using Go

@TanmoySG
Copy link
Owner Author

package main

import (
"fmt"
"github.com/google/btree"
)

type Item struct {
Key string
Value string
}

// Implement the btree.Item interface for our Item type
func (i Item) Less(than btree.Item) bool {
return i.Key < than.(Item).Key
}

func main() {
tree := btree.New(3) // Create a new B-tree with a degree of 3

// Insert some items into the tree
tree.ReplaceOrInsert(Item{"apple", "red"})
tree.ReplaceOrInsert(Item{"banana", "yellow"})
tree.ReplaceOrInsert(Item{"cherry", "red"})

// Look up an item by its key
item := tree.Get(Item{"banana", ""})
fmt.Println(item.(Item).Value) // Output: "yellow"

// Iterate over all items in the tree
tree.Ascend(func(i btree.Item) bool {
	item := i.(Item)
	fmt.Println(item.Key, item.Value)
	return true
})

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant