[Groonga-commit] groonga/grnxx at 209a941 [master] Gnx: add db.selectGroongaDB().

Back to archive index

susumu.yata null+****@clear*****
Mon Apr 6 11:37:15 JST 2015


susumu.yata	2015-04-06 11:37:15 +0900 (Mon, 06 Apr 2015)

  New Revision: 209a941c69fa6bba958231839f77618643a5649e
  https://github.com/groonga/grnxx/commit/209a941c69fa6bba958231839f77618643a5649e

  Message:
    Gnx: add db.selectGroongaDB().

  Modified files:
    go2/gnx/gnx.go

  Modified: go2/gnx/gnx.go (+24 -26)
===================================================================
--- go2/gnx/gnx.go    2015-04-06 11:13:53 +0900 (ea1259d)
+++ go2/gnx/gnx.go    2015-04-06 11:37:15 +0900 (abe2bcf)
@@ -576,6 +576,20 @@ func (db *DB) hashText(value Text) int {
 	return int(hasher.Sum32())
 }
 
+func (db *DB) selectGroongaDB(key Valuer) (int, error) {
+	switch value := key.(type) {
+	case nil:
+		return rand.Int() % len(db.groongaDBs), nil
+	case Int:
+		return db.hashInt(value) % len(db.groongaDBs), nil
+	case Float:
+		return db.hashFloat(value) % len(db.groongaDBs), nil
+	case Text:
+		return db.hashText(value) % len(db.groongaDBs), nil
+	}
+	return -1, fmt.Errorf("unsupported key type")
+}
+
 func (db *DB) load(
 	tableName string, columnNames []string, records [][]Valuer) (int, error) {
 	idID := -1
@@ -611,23 +625,15 @@ func (db *DB) load(
 		}
 	case keyID != -1:
 		for _, record := range records {
-			switch key := record[keyID].(type) {
-			case Int:
-				dbID := db.hashInt(key) % len(db.groongaDBs)
-				recordsPerDBs[dbID] = append(recordsPerDBs[dbID], record)
-			case Float:
-				dbID := db.hashFloat(key) % len(db.groongaDBs)
-				recordsPerDBs[dbID] = append(recordsPerDBs[dbID], record)
-			case Text:
-				dbID := db.hashText(key) % len(db.groongaDBs)
-				recordsPerDBs[dbID] = append(recordsPerDBs[dbID], record)
-			default:
-				return 0, fmt.Errorf("unsupported key type")
+			dbID, err := db.selectGroongaDB(record[keyID])
+			if err != nil {
+				return 0, err
 			}
+			recordsPerDBs[dbID] = append(recordsPerDBs[dbID], record)
 		}
 	default:
 		for _, record := range records {
-			dbID := rand.Int() % len(db.groongaDBs)
+			dbID, _ := db.selectGroongaDB(nil)
 			recordsPerDBs[dbID] = append(recordsPerDBs[dbID], record)
 		}
 	}
@@ -669,21 +675,13 @@ func (db *DB) loadMap(
 			}
 			recordMapsPerDBs[dbID] = append(recordMapsPerDBs[dbID], dummyRecordMap)
 		case hasKey:
-			switch key := keyValue.(type) {
-			case Int:
-				dbID := db.hashInt(key) % len(db.groongaDBs)
-				recordMapsPerDBs[dbID] = append(recordMapsPerDBs[dbID], recordMap)
-			case Float:
-				dbID := db.hashFloat(key) % len(db.groongaDBs)
-				recordMapsPerDBs[dbID] = append(recordMapsPerDBs[dbID], recordMap)
-			case Text:
-				dbID := db.hashText(key) % len(db.groongaDBs)
-				recordMapsPerDBs[dbID] = append(recordMapsPerDBs[dbID], recordMap)
-			default:
-				return 0, fmt.Errorf("unsupported key type")
+			dbID, err := db.selectGroongaDB(keyValue)
+			if err != nil {
+				return 0, err
 			}
+			recordMapsPerDBs[dbID] = append(recordMapsPerDBs[dbID], recordMap)
 		default:
-			dbID := rand.Int() % len(db.groongaDBs)
+			dbID, _ := db.selectGroongaDB(nil)
 			recordMapsPerDBs[dbID] = append(recordMapsPerDBs[dbID], recordMap)
 		}
 	}
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index