Position Independent Source Code

Check-in [d9e7e9e5c5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:d9e7e9e5c58d225d9b61682e31983c3e1fe1ff65
Date: 2017-06-05 13:15:18
User: yumaikas
Comment:Doing some work on the key-value store
Tags And Properties
Context
2017-06-20
05:00
[8fa5156e35] Some basic KV work for a basic structured db store (user: yumaikas, tags: trunk)
2017-06-05
13:15
[d9e7e9e5c5] Doing some work on the key-value store (user: yumaikas, tags: trunk)
2017-05-29
05:12
[dbde373066] Getting a basic sorta-generic irc-ping server done (user: yumaikas, tags: trunk)
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to boltdb.go.

     1      1   package main
     2      2   
     3         -import "fmt"
            3  +import (
            4  +	"fmt"
            5  +
            6  +	"github.com/asdine/storm"
            7  +	"github.com/asdine/storm/codec/gob"
            8  +)
     4      9   
     5     10   var ModBoltDB = PISCModule{
     6     11   	Author:    "Andrew Owen",
     7     12   	Name:      "BoltDBExt",
     8     13   	License:   "MIT",
     9     14   	DocString: "WIP: This module is for interfacing with BoltDB",
    10     15   	Load:      loadBoltDB,
................................................................................
    12     17   
    13     18   // This default DB is intended to act like a single file registry
    14     19   
    15     20   // ErrBucketNotFound indicates that a database file doesn't have it's string bucket.
    16     21   var ErrBucketNotFound = fmt.Errorf("could not find strings bucket in database")
    17     22   
    18     23   type counter int
           24  +
           25  +type kvDB (storm.DB)
           26  +
           27  +type kvNode (storm.Node)
           28  +
           29  +func (kv *kvDB) Close(m *machine) error {
           30  +	return (*storm.DB)(kv).Close()
           31  +}
           32  +
           33  +func (kv *kvDB) SaveInt(m *machine) error {
           34  +	var i int
           35  +	k := m.popValue().String()
           36  +	err := (*storm.DB)(kv).Set("GLOBAL", k, i)
           37  +	m.pushValue(Integer(i))
           38  +	return err
           39  +}
           40  +
           41  +func (kv *kvDB) GetInt(m *machine) error {
           42  +	var i int
           43  +	k := m.popValue().String()
           44  +	err := (*storm.DB)(kv).Get("GLOBAL", k, &i)
           45  +	m.pushValue(Integer(i))
           46  +	return err
           47  +}
           48  +
           49  +// This function builds a KV store based on Storm/BoltDB
           50  +// TODO: Look into making this not depend on Storm
           51  +func initKVStore(m *machine) error {
           52  +	fileName := m.popValue().String()
           53  +	db, err := storm.Open(fileName, storm.Codec(gob.Codec))
           54  +	if err != nil {
           55  +		return err
           56  +	}
           57  +	kv := (*kvDB)(db)
           58  +
           59  +	kvWrapper := Dict{
           60  +		"Close":    GoFunc(kv.Close),
           61  +		"save-int": GoFunc(kv.SaveInt),
           62  +	}
           63  +	m.pushValue(kvWrapper)
           64  +	return nil
           65  +}
    19     66   
    20     67   func loadBoltDB(m *machine) error {
    21     68   
    22         -	//var err error
    23         -	return nil
    24         -	//m.db, err = storm.Open(".piscdb", storm.Codec(gob.Codec))
    25         -	// if err != nil {
    26         -	// 	return err
    27         -	// }
           69  +	/*
           70  +		m.db, err := storm.Open(".piscdb", storm.Codec(gob.Codec))
           71  +		if err != nil {
           72  +			return err
           73  +		}
           74  +
           75  +		m.addGoWord("incr-counter", "( key -- newval )", GoWord(func(m *machine) error {
           76  +			key := m.popValue().String()
           77  +			tx, err := m.db.Begin(true)
           78  +			defer tx.Rollback()
           79  +			var c counter
           80  +			err = tx.Get("counter", key, &c)
           81  +			if err == storm.ErrNotFound {
           82  +				tx.Set("counter", key, 0)
           83  +				c = 0
           84  +				err = nil
           85  +			}
           86  +			if err != nil {
           87  +				return err
           88  +			}
           89  +			c++
           90  +			err = tx.Set("counter", key, c)
           91  +			if err != nil {
           92  +				return err
           93  +			}
           94  +			err = tx.Commit()
           95  +			if err != nil {
           96  +				return err
           97  +			}
           98  +			m.pushValue(Integer(c))
           99  +			return nil
          100  +		}))
          101  +
          102  +		m.addGoWord("save-str", " ( key val -- ) ",
          103  +			GoWord(func(m *machine) error {
          104  +				val := m.popValue().String()
          105  +				key := m.popValue().String()
          106  +				return m.db.Set("strings", key, val)
          107  +			}))
          108  +
          109  +		m.addGoWord("load-str", " ( key -- val ) ",
          110  +			GoWord(func(m *machine) error {
          111  +				key := m.popValue().String()
          112  +				var val string
          113  +				err := m.db.Get("strings", key, &val)
          114  +				if err != nil {
          115  +					return err
          116  +				}
          117  +				m.pushValue(String(val))
          118  +				return nil
          119  +			}))
          120  +
          121  +		m.addGoWord("with-bucket", " ( str quot -- .. ) ", GoWord(func(m *machine) error {
          122  +			quot := m.popValue().(*quotation)
          123  +			bucketName := m.popValue().String()
          124  +			tx, err := m.db.Begin(true)
          125  +			if err != nil {
          126  +				return err
          127  +			}
          128  +			defer tx.Rollback()
          129  +			dict := make(Dict)
    28    130   
    29         -	// m.addGoWord("incr-counter", "( key -- newval )", GoWord(func(m *machine) error {
    30         -	// 	key := m.popValue().String()
    31         -	// 	tx, err := m.db.Begin(true)
    32         -	// 	defer tx.Rollback()
    33         -	// 	var c counter
    34         -	// 	err = tx.Get("counter", key, &c)
    35         -	// 	if err == storm.ErrNotFound {
    36         -	// 		tx.Set("counter", key, 0)
    37         -	// 		c = 0
    38         -	// 		err = nil
    39         -	// 	}
    40         -	// 	if err != nil {
    41         -	// 		return err
    42         -	// 	}
    43         -	// 	c++
    44         -	// 	err = tx.Set("counter", key, c)
    45         -	// 	if err != nil {
    46         -	// 		return err
    47         -	// 	}
    48         -	// 	err = tx.Commit()
    49         -	// 	if err != nil {
    50         -	// 		return err
    51         -	// 	}
    52         -	// 	m.pushValue(Integer(c))
    53         -	// 	return nil
    54         -	// }))
          131  +			// ( key val -- )
          132  +			dict["put-int"] = GoFunc(func(m *machine) error {
          133  +				val := m.popValue().(Integer)
          134  +				fmt.Println("value?", val)
          135  +				key := m.popValue().String()
          136  +				tx.Set(bucketName, key, int(val))
          137  +				return nil
          138  +			})
    55    139   
    56         -	// m.addGoWord("save-str", " ( key val -- ) ",
    57         -	// 	GoWord(func(m *machine) error {
    58         -	// 		val := m.popValue().String()
    59         -	// 		key := m.popValue().String()
    60         -	// 		return m.db.Set("strings", key, val)
    61         -	// 	}))
          140  +			// ( key -- val )
          141  +			dict["get-int"] = GoFunc(func(m *machine) error {
          142  +				var val int
          143  +				key := m.popValue().String()
          144  +				err := tx.Get(bucketName, key, &val)
          145  +				if err == storm.ErrNotFound {
          146  +					err = nil
          147  +					val = 0
          148  +				}
          149  +				if err != nil {
          150  +					return err
          151  +				}
          152  +				m.pushValue(Integer(val))
          153  +				return nil
          154  +			})
    62    155   
    63         -	// m.addGoWord("load-str", " ( key -- val ) ",
    64         -	// 	GoWord(func(m *machine) error {
    65         -	// 		key := m.popValue().String()
    66         -	// 		var val string
    67         -	// 		err := m.db.Get("strings", key, &val)
    68         -	// 		if err != nil {
    69         -	// 			return err
    70         -	// 		}
    71         -	// 		m.pushValue(String(val))
    72         -	// 		return nil
    73         -	// 	}))
    74         -
    75         -	// m.addGoWord("with-bucket", " ( str quot -- .. ) ", GoWord(func(m *machine) error {
    76         -	// 	quot := m.popValue().(*quotation)
    77         -	// 	bucketName := m.popValue().String()
    78         -	// 	tx, err := m.db.Begin(true)
    79         -	// 	if err != nil {
    80         -	// 		return err
    81         -	// 	}
    82         -	// 	defer tx.Rollback()
    83         -	// 	dict := make(Dict)
    84         -
    85         -	// 	// ( key val -- )
    86         -	// 	dict["put-int"] = GoFunc(func(m *machine) error {
    87         -	// 		val := m.popValue().(Integer)
    88         -	// 		fmt.Println("value?", val)
    89         -	// 		key := m.popValue().String()
    90         -	// 		tx.Set(bucketName, key, int(val))
    91         -	// 		return nil
    92         -	// 	})
          156  +			// ( key val -- )
          157  +			dict["put-str"] = GoFunc(func(m *machine) error {
          158  +				val := m.popValue().String()
          159  +				key := m.popValue().String()
          160  +				return tx.Set(bucketName, key, val)
          161  +			})
    93    162   
    94         -	// 	// ( key -- val )
    95         -	// 	dict["get-int"] = GoFunc(func(m *machine) error {
    96         -	// 		var val int
    97         -	// 		key := m.popValue().String()
    98         -	// 		err := tx.Get(bucketName, key, &val)
    99         -	// 		if err == storm.ErrNotFound {
   100         -	// 			err = nil
   101         -	// 			val = 0
   102         -	// 		}
   103         -	// 		if err != nil {
   104         -	// 			return err
   105         -	// 		}
   106         -	// 		m.pushValue(Integer(val))
   107         -	// 		return nil
   108         -	// 	})
   109         -
   110         -	// 	// ( key val -- )
   111         -	// 	dict["put-str"] = GoFunc(func(m *machine) error {
   112         -	// 		val := m.popValue().String()
   113         -	// 		key := m.popValue().String()
   114         -	// 		return tx.Set(bucketName, key, val)
   115         -	// 	})
   116         -
   117         -	// 	// ( key -- val )
   118         -	// 	dict["get-str"] = GoFunc(func(m *machine) error {
   119         -	// 		var val string
   120         -	// 		key := m.popValue().String()
   121         -	// 		err := tx.Get(bucketName, key, &val)
   122         -	// 		if err == storm.ErrNotFound {
   123         -	// 			err = nil
   124         -	// 			val = ""
   125         -	// 		}
   126         -	// 		if err != nil {
   127         -	// 			return err
   128         -	// 		}
   129         -	// 		m.pushValue(String(val))
   130         -	// 		return nil
   131         -	// 	})
   132         -
   133         -	// 	m.pushValue(dict)
   134         -	// 	m.pushValue(quot)
   135         -	// 	err = m.executeString("with", quot.toCode().codePositions[0])
   136         -	// 	if err != nil {
   137         -	// 		return err
   138         -	// 	} else {
   139         -	// 		tx.Commit()
   140         -	// 		return nil
   141         -	// 	}
   142         -	// }))
   143         -
   144         -	// TODO: Implement individual DBs..
   145         -	/*
   146         -			m.addGoWord("<bolt-db>", `( filepath -- db )
   147         -		    A bolt db has the following words available to it:
   148         -
   149         -		    - set-str (  )
   150         -		    `, GoWord(func(m *machine) error {
   151         -				path := m.popValue().(String).String()
   152         -				db, err := bolt.Open(path, 0600, nil)
   153         -				db.Update(func(tx *bolt.Tx) error {
   154         -					_, err := tx.CreateBucketIfNotExists([]byte("strings"))
   155         -					return err
   156         -				})
          163  +			// ( key -- val )
          164  +			dict["get-str"] = GoFunc(func(m *machine) error {
          165  +				var val string
          166  +				key := m.popValue().String()
          167  +				err := tx.Get(bucketName, key, &val)
          168  +				if err == storm.ErrNotFound {
          169  +					err = nil
          170  +					val = ""
          171  +				}
   157    172   				if err != nil {
   158    173   					return err
   159    174   				}
          175  +				m.pushValue(String(val))
          176  +				return nil
          177  +			})
          178  +
          179  +			m.pushValue(dict)
          180  +			m.pushValue(quot)
          181  +			err = m.executeString("with", quot.toCode().codePositions[0])
          182  +			if err != nil {
          183  +				return err
          184  +			} else {
          185  +				tx.Commit()
          186  +				return nil
          187  +			}
          188  +		}))
          189  +
          190  +		// TODO: Implement individual DBs..
          191  +		/*
          192  +				m.addGoWord("<bolt-db>", `( filepath -- db )
          193  +			    A bolt db has the following words available to it:
   160    194   
   161         -				strKey := []byte("strings")
   162         -				stackDB := Dict{}
   163         -				stackDB["set-str"] = GoFunc(func(m *machine) error {
   164         -					val, ok := m.popValue().(String)
   165         -					key := m.popValue().String()
   166         -					if !ok {
          195  +			    - set-str (  )
          196  +			    `, GoWord(func(m *machine) error {
          197  +					path := m.popValue().(String).String()
          198  +					db, err := bolt.Open(path, 0600, nil)
          199  +					db.Update(func(tx *bolt.Tx) error {
          200  +						_, err := tx.CreateBucketIfNotExists([]byte("strings"))
          201  +						return err
          202  +					})
          203  +					if err != nil {
   167    204   						return err
   168    205   					}
   169         -					db.Update(func(tx *bolt.Tx) error {
   170         -						b := tx.Bucket(strKey)
   171         -						if b == nil {
   172         -							return fmt.Errorf("unable to load strings bucket")
          206  +
          207  +					strKey := []byte("strings")
          208  +					stackDB := Dict{}
          209  +					stackDB["set-str"] = GoFunc(func(m *machine) error {
          210  +						val, ok := m.popValue().(String)
          211  +						key := m.popValue().String()
          212  +						if !ok {
          213  +							return err
   173    214   						}
   174         -						b.Put([]byte(key), []byte(val))
          215  +						db.Update(func(tx *bolt.Tx) error {
          216  +							b := tx.Bucket(strKey)
          217  +							if b == nil {
          218  +								return fmt.Errorf("unable to load strings bucket")
          219  +							}
          220  +							b.Put([]byte(key), []byte(val))
          221  +							return nil
          222  +						})
   175    223   						return nil
   176    224   					})
   177         -					return nil
   178         -				})
   179         -				stackDB["get-str"] = GoFunc(func(m *machine) error {
   180         -					key := m.popValue().String()
   181         -					db.View(func(tx *bolt.Tx) error {
   182         -						b := tx.Bucket(strKey)
   183         -						if b == nil {
   184         -							return fmt.Errorf("unable to load strings bucket")
   185         -						}
   186         -						val := b.Get([]byte(key))
   187         -						export := make([]byte, len(val))
   188         -						copy(val, export)
   189         -						m.pushValue(String(string(export)))
          225  +					stackDB["get-str"] = GoFunc(func(m *machine) error {
          226  +						key := m.popValue().String()
          227  +						db.View(func(tx *bolt.Tx) error {
          228  +							b := tx.Bucket(strKey)
          229  +							if b == nil {
          230  +								return fmt.Errorf("unable to load strings bucket")
          231  +							}
          232  +							val := b.Get([]byte(key))
          233  +							export := make([]byte, len(val))
          234  +							copy(val, export)
          235  +							m.pushValue(String(string(export)))
          236  +							return nil
          237  +						})
   190    238   						return nil
   191    239   					})
   192         -					return nil
   193         -				})
          240  +
          241  +					stackDB["close"] = GoFunc(func(m *machine) error {
          242  +						return db.Close()
          243  +					})
   194    244   
   195         -				stackDB["close"] = GoFunc(func(m *machine) error {
   196         -					return db.Close()
   197         -				})
   198         -
   199         -				return nil
   200         -			}))
          245  +					return nil
          246  +				}))
   201    247   	*/
   202    248   
   203    249   	return nil
   204    250   }

Changes to main.go.

    49     49   		},
    50     50   		cli.BoolFlag{
    51     51   			Name:   "benchmark",
    52     52   			Hidden: true,
    53     53   			Usage:  "Run various benchmarks, using pprof, and print out pertinent information",
    54     54   		},
    55     55   	}
    56         -	fmt.Println("???")
    57     56   	app.Run(os.Args)
    58         -	fmt.Println("???")
    59     57   }
    60     58   
    61     59   func initMachine() *machine {
    62     60   	m := &machine{
    63     61   		values:               make([]stackEntry, 0),
    64     62   		definedWords:         make(map[string]*codeQuotation),
    65     63   		definedStackComments: make(map[string]string),

Changes to types.go.