Example #1
0
func (ls *LevelDBStore) AddDocument(filename string, ips ipset.Set) error {
	exists, err := ls.HasDocument(filename)
	if err != nil {
		return err
	}
	if exists {
		return nil
	}

	nextID, err := ls.nextDocID()
	if err != nil {
		return err
	}
	ls.batch = new(leveldb.Batch)
	ls.setDocId(filename, nextID)
	for _, ip := range ips.SortedStrings() {
		//fmt.Printf("Add %#v to document\n", ip)
		err = ls.addIP(nextID, ip)
		if err != nil {
			return err
		}
	}
	err = ls.db.Write(ls.batch, nil)
	ls.batch = nil
	return err

}
Example #2
0
func (bs *BoltStore) AddDocument(filename string, ips ipset.Set) error {
	exists, err := bs.HasDocument(filename)
	if err != nil {
		return err
	}
	if exists {
		return nil
	}
	err = bs.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("docs"))
		nextID, err := nextDocID(b)
		if err != nil {
			return err
		}
		fmt.Printf("NextDocID should be %d\n", nextID)
		setDocId(b, filename, nextID)
		ipBucket := tx.Bucket([]byte("ips"))
		for _, ip := range ips.SortedStrings() {
			//fmt.Printf("Add %#v to document\n", k)
			addIP(ipBucket, nextID, ip)
		}
		return nil
	})

	return nil
}